MCPcopy
hub / github.com/pocketbase/pocketbase / MoveDirContent

Function MoveDirContent

tools/osutils/dir.go:20–79  ·  view source on GitHub ↗

MoveDirContent moves the src dir content, that is not listed in the exclude list, to dest dir (it will be created if missing). The rootExclude argument is used to specify a list of src root entries to exclude. Note that this method doesn't delete the old src dir. It is an alternative to os.Rename

(src string, dest string, rootExclude ...string)

Source from the content-addressed store, hash-verified

18// It is an alternative to os.Rename() for the cases where we can't
19// rename/delete the src dir (see https://github.com/pocketbase/pocketbase/issues/2519).
20func MoveDirContent(src string, dest string, rootExclude ...string) error {
21 entries, err := os.ReadDir(src)
22 if err != nil {
23 return err
24 }
25
26 // make sure that the dest dir exist
27 manuallyCreatedDestDir := false
28 if _, err := os.Stat(dest); err != nil {
29 if err := os.Mkdir(dest, os.ModePerm); err != nil {
30 return err
31 }
32 manuallyCreatedDestDir = true
33 }
34
35 moved := map[string]string{}
36
37 tryRollback := func() []error {
38 errs := []error{}
39
40 for old, new := range moved {
41 if err := os.Rename(new, old); err != nil {
42 errs = append(errs, err)
43 }
44 }
45
46 // try to delete manually the created dest dir if all moved files were restored
47 if manuallyCreatedDestDir && len(errs) == 0 {
48 if err := os.Remove(dest); err != nil {
49 errs = append(errs, err)
50 }
51 }
52
53 return errs
54 }
55
56 for _, entry := range entries {
57 basename := entry.Name()
58
59 if list.ExistInSlice(basename, rootExclude) {
60 continue
61 }
62
63 old := filepath.Join(src, basename)
64 new := filepath.Join(dest, basename)
65
66 if err := os.Rename(old, new); err != nil {
67 if errs := tryRollback(); len(errs) > 0 {
68 errs = append(errs, err)
69 err = errors.Join(errs...)
70 }
71
72 return err
73 }
74
75 moved[old] = new
76 }
77

Callers 2

TestMoveDirContentFunction · 0.92
RestoreBackupMethod · 0.92

Calls 2

ExistInSliceFunction · 0.92
RemoveMethod · 0.45

Tested by 1

TestMoveDirContentFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…