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)
| 55 | |
| 56 | // copyFile copies a file in chunks from src path to dst path, using umask to create the dst file |
| 57 | func 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 | } |