-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathget_file.go
201 lines (171 loc) · 3.96 KB
/
get_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package getter
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
)
// FileGetter is a Getter implementation that will download a module from
// a file scheme.
type FileGetter struct{}
func (g *FileGetter) Mode(ctx context.Context, u *url.URL) (Mode, error) {
path := u.Path
if u.RawPath != "" {
path = u.RawPath
}
fi, err := os.Stat(path)
if err != nil {
return 0, err
}
// Check if the source is a directory.
if fi.IsDir() {
return ModeDir, nil
}
return ModeFile, nil
}
func (g *FileGetter) Get(ctx context.Context, req *Request) error {
path := req.u.Path
if req.u.RawPath != "" {
path = req.u.RawPath
}
// The source path must exist and be a directory to be usable.
if fi, err := os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
} else if !fi.IsDir() {
return fmt.Errorf("source path must be a directory")
}
fi, err := os.Lstat(req.Dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if req.Inplace {
req.Dst = path
return nil
}
// If the destination already exists, it must be a symlink
if err == nil {
mode := fi.Mode()
if mode&os.ModeSymlink == 0 {
return fmt.Errorf("destination exists and is not a symlink")
}
// Remove the destination
if err := os.Remove(req.Dst); err != nil {
return err
}
}
// Create all the parent directories
if err := os.MkdirAll(filepath.Dir(req.Dst), 0755); err != nil {
return err
}
return SymlinkAny(path, req.Dst)
}
func (g *FileGetter) GetFile(ctx context.Context, req *Request) error {
path := req.u.Path
if req.u.RawPath != "" {
path = req.u.RawPath
}
// The source path must exist and be a file to be usable.
if fi, err := os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
} else if fi.IsDir() {
return fmt.Errorf("source path must be a file")
}
if req.Inplace {
req.Dst = path
return nil
}
_, err := os.Lstat(req.Dst)
if err != nil && !os.IsNotExist(err) {
return err
}
// If the destination already exists, it must be a symlink
if err == nil {
// Remove the destination
if err := os.Remove(req.Dst); err != nil {
return err
}
}
// Create all the parent directories
if err := os.MkdirAll(filepath.Dir(req.Dst), 0755); err != nil {
return err
}
// If we're not copying, just symlink and we're done
if !req.Copy {
if err = os.Symlink(path, req.Dst); err == nil {
return err
}
lerr, ok := err.(*os.LinkError)
if !ok {
return err
}
switch lerr.Err {
case ErrUnauthorized:
// On windows this means we don't have
// symlink privilege, let's
// fallback to a copy to avoid an error.
break
default:
return err
}
}
// Copy
srcF, err := os.Open(path)
if err != nil {
return err
}
defer srcF.Close()
dstF, err := os.Create(req.Dst)
if err != nil {
return err
}
defer dstF.Close()
_, err = Copy(ctx, dstF, srcF)
return err
}
func (g *FileGetter) Detect(req *Request) (bool, error) {
var src, pwd string
src = req.Src
pwd = req.Pwd
if len(src) == 0 {
return false, nil
}
if req.Forced != "" {
// There's a getter being Forced
if !g.validScheme(req.Forced) {
// Current getter is not the Forced one
// Don't use it to try to download the artifact
return false, nil
}
}
isForcedGetter := req.Forced != "" && g.validScheme(req.Forced)
u, err := url.Parse(src)
if err == nil && u.Scheme != "" {
if isForcedGetter {
// Is the Forced getter and source is a valid url
return true, nil
}
if g.validScheme(u.Scheme) {
return true, nil
}
if !(runtime.GOOS == "windows" && len(u.Scheme) == 1) {
return false, nil
}
// For windows, we try to get the artifact
// if it has a non valid scheme with 1 char
// e.g. C:/foo/bar for other cases a prefix file:// is necessary
}
src, ok, err := new(FileDetector).Detect(src, pwd)
if err != nil {
return ok, err
}
if ok {
req.Src = src
return ok, nil
}
return true, nil
}
func (g *FileGetter) validScheme(scheme string) bool {
return scheme == "file"
}