forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_file_copy.go
93 lines (78 loc) · 2.47 KB
/
get_file_copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package getter
import (
"context"
"fmt"
"io"
"os"
)
// readerFunc is syntactic sugar for read interface.
type readerFunc func(p []byte) (n int, err error)
func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }
// Copy is a io.Copy cancellable by context
func Copy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {
// Copy will call the Reader and Writer interface multiple time, in order
// to copy by chunk (avoiding loading the whole file in memory).
return io.Copy(dst, readerFunc(func(p []byte) (int, error) {
select {
case <-ctx.Done():
// context has been canceled
// stop process and propagate "context canceled" error
return 0, ctx.Err()
default:
// otherwise just run default io.Reader implementation
return src.Read(p)
}
}))
}
// copyReader copies from an io.Reader into a file, using umask to create the dst file
func copyReader(dst string, src io.Reader, fmode, umask os.FileMode, fileSizeLimit int64) error {
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode)
if err != nil {
return err
}
defer dstF.Close()
if fileSizeLimit > 0 {
src = io.LimitReader(src, fileSizeLimit)
}
_, err = io.Copy(dstF, src)
if err != nil {
return err
}
// Explicitly chmod; the process umask is unconditionally applied otherwise.
// We'll mask the mode with our own umask, but that may be different than
// the process umask
return os.Chmod(dst, mode(fmode, umask))
}
// copyFile copies a file in chunks from src path to dst path, using umask to create the dst file
func copyFile(ctx context.Context, dst, src string, disableSymlinks bool, fmode, umask os.FileMode) (int64, error) {
if disableSymlinks {
fileInfo, err := os.Lstat(src)
if err != nil {
return 0, fmt.Errorf("failed to check copy file source for symlinks: %w", err)
}
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
return 0, ErrSymlinkCopy
}
}
srcF, err := os.Open(src)
if err != nil {
return 0, err
}
defer srcF.Close()
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode)
if err != nil {
return 0, err
}
defer dstF.Close()
count, err := Copy(ctx, dstF, srcF)
if err != nil {
return 0, err
}
// Explicitly chmod; the process umask is unconditionally applied otherwise.
// We'll mask the mode with our own umask, but that may be different than
// the process umask
err = os.Chmod(dst, mode(fmode, umask))
return count, err
}