-
Notifications
You must be signed in to change notification settings - Fork 239
/
copy_dir.go
88 lines (74 loc) · 2.06 KB
/
copy_dir.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package getter
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
)
// mode returns the file mode masked by the umask
func mode(mode, umask os.FileMode) os.FileMode {
return mode & ^umask
}
// copyDir copies the src directory contents into dst. Both directories
// should already exist.
//
// If ignoreDot is set to true, then dot-prefixed files/folders are ignored.
func copyDir(ctx context.Context, dst string, src string, ignoreDot bool, disableSymlinks bool, umask os.FileMode) error {
// We can safely evaluate the symlinks here, even if disabled, because they
// will be checked before actual use in walkFn and copyFile
var err error
src, err = filepath.EvalSymlinks(src)
if err != nil {
return err
}
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if disableSymlinks {
fileInfo, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("failed to check copy file source for symlinks: %w", err)
}
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
return ErrSymlinkCopy
}
// if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// return ErrSymlinkCopy
// }
}
if path == src {
return nil
}
if ignoreDot && strings.HasPrefix(filepath.Base(path), ".") {
// Skip any dot files
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
}
// The "path" has the src prefixed to it. We need to join our
// destination with the path without the src on it.
dstPath := filepath.Join(dst, path[len(src):])
// If we have a directory, make that subdirectory, then continue
// the walk.
if info.IsDir() {
if path == filepath.Join(src, dst) {
// dst is in src; don't walk it.
return nil
}
if err := os.MkdirAll(dstPath, mode(0755, umask)); err != nil {
return err
}
return nil
}
// If we have a file, copy the contents.
_, err = copyFile(ctx, dstPath, path, disableSymlinks, info.Mode(), umask)
return err
}
return filepath.Walk(src, walkFn)
}