MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / MvFile

Method MvFile

agent/app/service/file.go:855–927  ·  view source on GitHub ↗
(m request.FileMove)

Source from the content-addressed store, hash-verified

853}
854
855func (f *FileService) MvFile(m request.FileMove) error {
856 fo := files.NewFileOp()
857 if !fo.Stat(m.NewPath) {
858 return buserr.New("ErrPathNotFound")
859 }
860 for _, oldPath := range m.OldPaths {
861 if !fo.Stat(oldPath) {
862 return buserr.WithName("ErrFileNotFound", oldPath)
863 }
864 if oldPath == m.NewPath || strings.Contains(m.NewPath, filepath.Clean(oldPath)+"/") {
865 return buserr.New("ErrMovePathFailed")
866 }
867 }
868 type moveSnapshot struct {
869 path string
870 content []byte
871 mode os.FileMode
872 record bool
873 }
874 var errs []error
875 if m.Type == "cut" {
876 snapshots := make([]moveSnapshot, 0, len(m.OldPaths))
877 for _, oldPath := range m.OldPaths {
878 content, mode, record := readEditableFileHistoryContent(oldPath)
879 snapshots = append(snapshots, moveSnapshot{path: oldPath, content: content, mode: mode, record: record})
880 }
881 if len(m.CoverPaths) > 0 {
882 for _, src := range m.CoverPaths {
883 if err := fo.CopyAndReName(src, m.NewPath, "", true); err != nil {
884 errs = append(errs, err)
885 global.LOG.Errorf("cut copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
886 }
887 }
888 }
889 if err := fo.Cut(m.OldPaths, m.NewPath, m.Name, m.Cover); err != nil {
890 return err
891 }
892 for _, snapshot := range snapshots {
893 if snapshot.record {
894 targetPath := buildHistoryMoveTargetPath(m.NewPath, m.Name, snapshot.path, len(m.OldPaths))
895 if histErr := historyService.RecordOperation(fileHistoryOpMove, snapshot.path, snapshot.content, snapshot.mode, snapshot.path, targetPath); histErr != nil {
896 global.LOG.Warnf("record file move history failed for %s: %v", snapshot.path, histErr)
897 }
898 }
899 }
900 return nil
901 }
902 if m.Type == "copy" {
903 for _, src := range m.OldPaths {
904 if err := fo.CopyAndReName(src, m.NewPath, m.Name, m.Cover); err != nil {
905 errs = append(errs, err)
906 global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
907 }
908 }
909 if len(m.CoverPaths) > 0 {
910 for _, src := range m.CoverPaths {
911 if err := fo.CopyAndReName(src, m.NewPath, "", true); err != nil {
912 errs = append(errs, err)

Callers

nothing calls this directly

Calls 8

StatMethod · 0.95
CopyAndReNameMethod · 0.95
CutMethod · 0.95
CleanMethod · 0.65
RecordOperationMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected