MCPcopy Index your code
hub / github.com/1Panel-dev/1Panel / CopyFile

Function CopyFile

core/utils/files/files.go:22–50  ·  view source on GitHub ↗
(src, dst string, withName bool)

Source from the content-addressed store, hash-verified

20)
21
22func CopyFile(src, dst string, withName bool) error {
23 source, err := os.Open(src)
24 if err != nil {
25 return err
26 }
27 defer source.Close()
28
29 if path.Base(src) != path.Base(dst) && !withName {
30 dst = path.Join(dst, path.Base(src))
31 }
32 if _, err := os.Stat(path.Dir(dst)); err != nil {
33 if os.IsNotExist(err) {
34 _ = os.MkdirAll(path.Dir(dst), os.ModePerm)
35 }
36 }
37 target, err := os.OpenFile(dst+"_temp", os.O_RDWR|os.O_CREATE|os.O_TRUNC, constant.FilePerm)
38 if err != nil {
39 return err
40 }
41 defer target.Close()
42
43 if _, err = io.Copy(target, source); err != nil {
44 return err
45 }
46 if err = os.Rename(dst+"_temp", dst); err != nil {
47 return err
48 }
49 return nil
50}
51
52func CopyItem(isDir, withName bool, src, dst string) error {
53 if path.Base(src) != path.Base(dst) && !withName {

Callers

nothing calls this directly

Calls 6

MkdirAllMethod · 0.80
OpenFileMethod · 0.80
CopyMethod · 0.80
RenameMethod · 0.80
CloseMethod · 0.65
StatMethod · 0.45

Tested by

no test coverage detected