-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
99 lines (80 loc) · 1.75 KB
/
file.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
94
95
96
97
98
99
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
)
// copyDir copies a directory recursively.
func copyDir(ctx context.Context, path, destPath string) error {
if ctxCancelled(ctx) {
return errors.New("cancelled")
}
pathInfo, err := os.Stat(path)
if err != nil {
return err
}
if !pathInfo.IsDir() {
return errors.New("path is not a directory")
}
if _, err := os.Stat(destPath); err == nil || !os.IsNotExist(err) {
return errors.New("directory already exists")
}
if err = os.MkdirAll(destPath, pathInfo.Mode()); err != nil {
return err
}
filesInfo, err := ioutil.ReadDir(path)
if err != nil {
return err
}
if ctxCancelled(ctx) {
return errors.New("cancelled")
}
for _, i := range filesInfo {
fPath := path + "/" + i.Name()
dPath := destPath + "/" + i.Name()
if i.IsDir() {
if err := copyDir(ctx, fPath, dPath); err != nil {
return fmt.Errorf("cannot copy dir %s: %v", fPath, err)
}
continue
}
if err := copyFile(ctx, fPath, dPath); err != nil {
return fmt.Errorf("cannot copy file %s: %v", fPath, err)
}
}
return nil
}
// copyFile copies file to destPath.
func copyFile(ctx context.Context, path, destPath string) error {
if ctxCancelled(ctx) {
return errors.New("cancelled")
}
info, err := os.Stat(path)
if err != nil {
return err
}
destFile, err := os.Create(destPath)
if err != nil {
return err
}
defer destFile.Close()
if err := os.Chmod(destPath, info.Mode()); err != nil {
return err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if _, err := destFile.Write(data); err != nil {
return err
}
return nil
}
func vendorExists() bool {
if _, err := os.Stat(manifest.VendorPath); err == nil || !os.IsNotExist(err) {
return true
}
return false
}