MCPcopy
hub / github.com/hashicorp/go-getter / copyFile

Function copyFile

get_file_copy.go:57–92  ·  view source on GitHub ↗

copyFile copies a file in chunks from src path to dst path, using umask to create the dst file

(ctx context.Context, dst, src string, disableSymlinks bool, fmode, umask os.FileMode)

Source from the content-addressed store, hash-verified

55
56// copyFile copies a file in chunks from src path to dst path, using umask to create the dst file
57func copyFile(ctx context.Context, dst, src string, disableSymlinks bool, fmode, umask os.FileMode) (int64, error) {
58
59 if disableSymlinks {
60 fileInfo, err := os.Lstat(src)
61 if err != nil {
62 return 0, fmt.Errorf("failed to check copy file source for symlinks: %w", err)
63 }
64
65 if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
66 return 0, ErrSymlinkCopy
67 }
68 }
69
70 srcF, err := os.Open(src)
71 if err != nil {
72 return 0, err
73 }
74 defer srcF.Close()
75
76 dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode)
77 if err != nil {
78 return 0, err
79 }
80 defer dstF.Close()
81
82 count, err := Copy(ctx, dstF, srcF)
83 if err != nil {
84 return 0, err
85 }
86
87 // Explicitly chmod; the process umask is unconditionally applied otherwise.
88 // We'll mask the mode with our own umask, but that may be different than
89 // the process umask
90 err = os.Chmod(dst, mode(fmode, umask))
91 return count, err
92}

Callers 1

copyDirFunction · 0.85

Calls 4

CopyFunction · 0.85
modeFunction · 0.85
ModeMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected