MCPcopy Index your code
hub / github.com/TruthHun/BookStack / CopyDir

Method CopyDir

models/store/local.go:69–110  ·  view source on GitHub ↗

拷贝文件夹

(sourceDir, targetDir string)

Source from the content-addressed store, hash-verified

67
68// 拷贝文件夹
69func (this *Local) CopyDir(sourceDir, targetDir string) (err error) {
70 var fl []filetil.FileList
71
72 sourceDir = strings.TrimLeft(sourceDir, "./")
73 targetDir = strings.TrimLeft(targetDir, "./")
74
75 if fl, err = filetil.ScanFiles(sourceDir); err != nil {
76 return
77 }
78 if len(fl) == 0 {
79 return
80 }
81
82 for _, f := range fl {
83 if f.IsDir {
84 continue
85 }
86
87 // 不能用defer,因为在文件多的时候,会打开跟多文件句柄
88 targetFile := strings.ReplaceAll(filepath.Join(targetDir, strings.TrimPrefix(f.Path, sourceDir)), "\\", "/")
89 src, errOpen := os.Open(f.Path)
90 if errOpen != nil {
91 return errOpen
92 }
93
94 os.MkdirAll(filepath.Dir(targetFile), os.ModePerm)
95 target, errOpen := os.OpenFile(targetFile, os.O_WRONLY|os.O_CREATE, 0644)
96 if errOpen != nil {
97 src.Close()
98 return errOpen
99 }
100
101 if _, err = io.Copy(target, src); err != nil {
102 target.Close()
103 src.Close()
104 return
105 }
106 target.Close()
107 src.Close()
108 }
109 return
110}

Callers

nothing calls this directly

Calls 1

CopyMethod · 0.45

Tested by

no test coverage detected