forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
72 lines (63 loc) · 1.41 KB
/
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
package vendor
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/constabulary/gb/fileutils"
)
const debugCopypath = true
const debugCopyfile = false
// Copypath copies the contents of src to dst, excluding any file or
// directory that starts with a period.
func Copypath(dst string, src string) error {
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasPrefix(filepath.Base(path), ".") {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if info.IsDir() {
return nil
}
if info.Mode()&os.ModeSymlink != 0 {
if debugCopypath {
fmt.Printf("skipping symlink: %v\n", path)
}
return nil
}
dst := filepath.Join(dst, path[len(src):])
return copyfile(dst, path)
})
if err != nil {
// if there was an error during copying, remove the partial copy.
fileutils.RemoveAll(dst)
}
return err
}
func copyfile(dst, src string) error {
err := mkdir(filepath.Dir(dst))
if err != nil {
return fmt.Errorf("copyfile: mkdirall: %v", err)
}
r, err := os.Open(src)
if err != nil {
return fmt.Errorf("copyfile: open(%q): %v", src, err)
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return fmt.Errorf("copyfile: create(%q): %v", dst, err)
}
defer w.Close()
if debugCopyfile {
fmt.Printf("copyfile(dst: %v, src: %v)\n", dst, src)
}
_, err = io.Copy(w, r)
return err
}