MCPcopy
hub / github.com/filebrowser/filebrowser / CopyFile

Function CopyFile

fileutils/file.go:34–73  ·  view source on GitHub ↗

CopyFile copies a file from source to dest and returns an error if any.

(afs afero.Fs, source, dest string, fileMode, dirMode fs.FileMode)

Source from the content-addressed store, hash-verified

32// CopyFile copies a file from source to dest and returns
33// an error if any.
34func CopyFile(afs afero.Fs, source, dest string, fileMode, dirMode fs.FileMode) error {
35 // Open the source file.
36 src, err := afs.Open(source)
37 if err != nil {
38 return err
39 }
40 defer src.Close()
41
42 // Makes the directory needed to create the dst
43 // file.
44 err = afs.MkdirAll(filepath.Dir(dest), dirMode)
45 if err != nil {
46 return err
47 }
48
49 // Create the destination file.
50 dst, err := afs.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
51 if err != nil {
52 return err
53 }
54 defer dst.Close()
55
56 // Copy the contents of the file.
57 _, err = io.Copy(dst, src)
58 if err != nil {
59 return err
60 }
61
62 // Copy the mode
63 info, err := afs.Stat(source)
64 if err != nil {
65 return err
66 }
67 err = afs.Chmod(dest, info.Mode())
68 if err != nil {
69 return err
70 }
71
72 return nil
73}
74
75// CommonPrefix returns common directory path of provided files
76func CommonPrefix(sep byte, paths ...string) string {

Callers 2

CopyFunction · 0.85
CopyDirFunction · 0.85

Calls 6

MkdirAllMethod · 0.80
OpenFileMethod · 0.80
ChmodMethod · 0.80
CloseMethod · 0.65
OpenMethod · 0.45
StatMethod · 0.45

Tested by

no test coverage detected