-
Notifications
You must be signed in to change notification settings - Fork 23
/
io.go
90 lines (84 loc) · 1.83 KB
/
io.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"gopkg.in/errgo.v2/fmt/errors"
)
func isEmptyDir(dir string) (bool, error) {
f, err := os.Open(dir)
if err != nil {
return false, errors.Wrap(err)
}
defer f.Close()
_, err = f.Readdir(1)
if err != nil && err != io.EOF {
return false, errors.Wrap(err)
}
return err == io.EOF, nil
}
func copyAll(dst, src string) error {
srcInfo, srcErr := os.Lstat(src)
if srcErr != nil {
return errors.Wrap(srcErr)
}
_, dstErr := os.Lstat(dst)
if dstErr == nil {
return errors.Newf("will not overwrite %q", dst)
}
if !os.IsNotExist(dstErr) {
return errors.Wrap(dstErr)
}
switch mode := srcInfo.Mode(); mode & os.ModeType {
case os.ModeSymlink:
return errors.Newf("will not copy symbolic link")
case os.ModeDir:
return copyDir(dst, src)
case 0:
return copyFile(dst, src)
default:
return fmt.Errorf("cannot copy file with mode %v", mode)
}
}
func copyFile(dst, src string) error {
srcf, err := os.Open(src)
if err != nil {
return errors.Wrap(err)
}
defer srcf.Close()
dstf, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return errors.Wrap(err)
}
defer dstf.Close()
if _, err := io.Copy(dstf, srcf); err != nil {
return fmt.Errorf("cannot copy %q to %q: %v", src, dst, err)
}
return nil
}
func copyDir(dst, src string) error {
srcf, err := os.Open(src)
if err != nil {
return errors.Wrap(err)
}
defer srcf.Close()
if err := os.MkdirAll(dst, 0777); err != nil {
return errors.Wrap(err)
}
for {
names, err := srcf.Readdirnames(100)
for _, name := range names {
if err := copyAll(filepath.Join(dst, name), filepath.Join(src, name)); err != nil {
return errors.Wrap(err)
}
}
if err == io.EOF {
break
}
if err != nil {
return errors.Newf("error reading directory %q: %v", src, err)
}
}
return nil
}