forked from openshift/imagebuilder
/
copyinfo.go
178 lines (162 loc) · 4.55 KB
/
copyinfo.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
package dockerclient
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
)
type CopyInfo struct {
os.FileInfo
Path string
Decompress bool
FromDir bool
}
// CalcCopyInfo identifies the source files selected by a Dockerfile ADD or COPY instruction.
func CalcCopyInfo(origPath, rootPath string, allowWildcards bool) ([]CopyInfo, error) {
explicitDir := origPath == "." || origPath == "/" || strings.HasSuffix(origPath, "/.") || strings.HasSuffix(origPath, "/")
// all CopyInfo resulting from this call will have FromDir set to explicitDir
infos, err := calcCopyInfo(origPath, rootPath, allowWildcards, explicitDir)
if err != nil {
return nil, err
}
return infos, nil
}
func calcCopyInfo(origPath, rootPath string, allowWildcards, explicitDir bool) ([]CopyInfo, error) {
origPath = trimLeadingPath(origPath)
// Deal with wildcards
if allowWildcards && containsWildcards(origPath) {
matchPath := filepath.Join(rootPath, origPath)
var copyInfos []CopyInfo
if err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == "" {
// Why are we doing this check?
return nil
}
if match, _ := filepath.Match(matchPath, path); !match {
return nil
}
// Note we set allowWildcards to false in case the name has
// a * in it
subInfos, err := calcCopyInfo(trimLeadingPath(strings.TrimPrefix(path, rootPath)), rootPath, false, explicitDir)
if err != nil {
return err
}
copyInfos = append(copyInfos, subInfos...)
return nil
}); err != nil {
return nil, err
}
return copyInfos, nil
}
// Must be a dir or a file
fi, err := os.Stat(filepath.Join(rootPath, origPath))
if err != nil {
return nil, err
}
// flatten the root directory so we can rebase it
if origPath == "." {
if !fi.IsDir() {
// we want to mount a single file as input
return []CopyInfo{{FileInfo: fi, Path: origPath, FromDir: false}}, nil
}
var copyInfos []CopyInfo
infos, err := ioutil.ReadDir(rootPath)
if err != nil {
return nil, err
}
for _, info := range infos {
copyInfos = append(copyInfos, CopyInfo{FileInfo: info, Path: info.Name(), FromDir: explicitDir})
}
return copyInfos, nil
}
origPath = trimTrailingDot(origPath)
return []CopyInfo{{FileInfo: fi, Path: origPath, FromDir: explicitDir}}, nil
}
func DownloadURL(src, dst, tempDir string) ([]CopyInfo, string, error) {
// get filename from URL
u, err := url.Parse(src)
if err != nil {
return nil, "", err
}
base := path.Base(u.Path)
if base == "." {
return nil, "", fmt.Errorf("cannot determine filename from url: %s", u)
}
resp, err := http.Get(src)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, "", fmt.Errorf("server returned a status code >= 400: %s", resp.Status)
}
tmpDir, err := ioutil.TempDir(tempDir, "dockerbuildurl-")
if err != nil {
return nil, "", err
}
tmpFileName := filepath.Join(tmpDir, base)
tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
os.RemoveAll(tmpDir)
return nil, "", err
}
if _, err := io.Copy(tmpFile, resp.Body); err != nil {
os.RemoveAll(tmpDir)
return nil, "", err
}
if err := tmpFile.Close(); err != nil {
os.RemoveAll(tmpDir)
return nil, "", err
}
info, err := os.Stat(tmpFileName)
if err != nil {
os.RemoveAll(tmpDir)
return nil, "", err
}
return []CopyInfo{{FileInfo: info, Path: base}}, tmpDir, nil
}
func trimLeadingPath(origPath string) string {
// Work in daemon-specific OS filepath semantics
origPath = filepath.FromSlash(origPath)
if origPath != "" && origPath[0] == os.PathSeparator && len(origPath) > 1 {
origPath = origPath[1:]
}
origPath = strings.TrimPrefix(origPath, "."+string(os.PathSeparator))
return origPath
}
func trimTrailingSlash(origPath string) string {
if origPath == "/" {
return origPath
}
return strings.TrimSuffix(origPath, "/")
}
func trimTrailingDot(origPath string) string {
if strings.HasSuffix(origPath, string(os.PathSeparator)+".") {
return strings.TrimSuffix(origPath, ".")
}
return origPath
}
// containsWildcards checks whether the provided name has a wildcard.
func containsWildcards(name string) bool {
for i := 0; i < len(name); i++ {
ch := name[i]
if ch == '\\' {
i++
} else if ch == '*' || ch == '?' || ch == '[' {
return true
}
}
return false
}
// isURL returns true if the string appears to be a URL.
func isURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}