Debian Codegraph @

dockerclient/copyinfo.go

raw
1 package dockerclient
2
3 import (
4 "fmt"
5 "io"
6 "net/http"
7 "net/url"
8 "os"
9 "path"
10 "path/filepath"
11 "strings"
12 )
13
14 type CopyInfo struct {
15 os.FileInfo
16 Path string
17 Decompress bool // deprecated, is never set and is ignored
18 FromDir bool
19 }
20
21 // CalcCopyInfo identifies the source files selected by a Dockerfile ADD or COPY instruction.
22 func CalcCopyInfo(origPath, rootPath string, allowWildcards bool) ([]CopyInfo, error) {
23 explicitDir := origPath == "." || origPath == "/" || strings.HasSuffix(origPath, "/.") || strings.HasSuffix(origPath, "/")
24 // all CopyInfo resulting from this call will have FromDir set to explicitDir
25 infos, err := calcCopyInfo(origPath, rootPath, allowWildcards, explicitDir)
26 if err != nil {
27 return nil, err
28 }
29 return infos, nil
30 }
31
32 func calcCopyInfo(origPath, rootPath string, allowWildcards, explicitDir bool) ([]CopyInfo, error) {
33 origPath = trimLeadingPath(origPath)
34 if !filepath.IsAbs(rootPath) {
35 rootPath = trimLeadingPath(rootPath)
36 }
37 // Deal with wildcards
38 if allowWildcards && containsWildcards(origPath) {
39 matchPath := filepath.Join(rootPath, origPath)
40 var copyInfos []CopyInfo
41 if err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
42 if err != nil {
43 return err
44 }
45 if info.Name() == "" {
46 // Why are we doing this check?
47 return nil
48 }
49 if match, _ := filepath.Match(matchPath, path); !match {
50 return nil
51 }
52
53 // Note we set allowWildcards to false in case the name has
54 // a * in it
55 subInfos, err := calcCopyInfo(trimLeadingPath(strings.TrimPrefix(path, rootPath)), rootPath, false, explicitDir)
56 if err != nil {
57 return err
58 }
59 copyInfos = append(copyInfos, subInfos...)
60 return nil
61 }); err != nil {
62 return nil, err
63 }
64 return copyInfos, nil
65 }
66
67 // Must be a dir or a file
68 fi, err := os.Stat(filepath.Join(rootPath, origPath))
69 if err != nil {
70 return nil, err
71 }
72
73 // flatten the root directory so we can rebase it
74 if origPath == "." {
75 if !fi.IsDir() {
76 // we want to mount a single file as input
77 return []CopyInfo{{FileInfo: fi, Path: origPath, FromDir: false}}, nil
78 }
79 var copyInfos []CopyInfo
80 infos, err := os.ReadDir(rootPath)
81 if err != nil {
82 return nil, err
83 }
84 for _, info := range infos {
85 fileInfo, err := info.Info()
86 if err != nil {
87 return nil, err
88 }
89 copyInfos = append(copyInfos, CopyInfo{FileInfo: fileInfo, Path: info.Name(), FromDir: explicitDir})
90 }
91 return copyInfos, nil
92 }
93
94 origPath = trimTrailingDot(origPath)
95 return []CopyInfo{{FileInfo: fi, Path: origPath, FromDir: explicitDir}}, nil
96 }
97
98 func DownloadURL(src, dst, tempDir string) ([]CopyInfo, string, error) {
99 // get filename from URL
100 u, err := url.Parse(src)
101 if err != nil {
102 return nil, "", err
103 }
104 base := path.Base(u.Path)
105 if base == "." {
106 return nil, "", fmt.Errorf("cannot determine filename from url: %s", u)
107 }
108
109 resp, err := http.Get(src)
110 if err != nil {
111 return nil, "", err
112 }
113 defer resp.Body.Close()
114 if resp.StatusCode >= 400 {
115 return nil, "", fmt.Errorf("server returned a status code >= 400: %s", resp.Status)
116 }
117
118 tmpDir, err := os.MkdirTemp(tempDir, "dockerbuildurl-")
119 if err != nil {
120 return nil, "", err
121 }
122 tmpFileName := filepath.Join(tmpDir, base)
123 tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
124 if err != nil {
125 os.RemoveAll(tmpDir)
126 return nil, "", err
127 }
128 if _, err := io.Copy(tmpFile, resp.Body); err != nil {
129 os.RemoveAll(tmpDir)
130 return nil, "", err
131 }
132 if err := tmpFile.Close(); err != nil {
133 os.RemoveAll(tmpDir)
134 return nil, "", err
135 }
136 info, err := os.Stat(tmpFileName)
137 if err != nil {
138 os.RemoveAll(tmpDir)
139 return nil, "", err
140 }
141 return []CopyInfo{{FileInfo: info, Path: base}}, tmpDir, nil
142 }
143
144 func trimLeadingPath(origPath string) string {
145 // Work in daemon-specific OS filepath semantics
146 origPath = filepath.FromSlash(origPath)
147 if origPath != "" && origPath[0] == os.PathSeparator && len(origPath) > 1 {
148 origPath = origPath[1:]
149 }
150 origPath = strings.TrimPrefix(origPath, "."+string(os.PathSeparator))
151 return origPath
152 }
153
154 func trimTrailingSlash(origPath string) string {
155 if origPath == "/" {
156 return origPath
157 }
158 return strings.TrimSuffix(origPath, "/")
159 }
160
161 func trimTrailingDot(origPath string) string {
162 if strings.HasSuffix(origPath, string(os.PathSeparator)+".") {
163 return strings.TrimSuffix(origPath, ".")
164 }
165 return origPath
166 }
167
168 // containsWildcards checks whether the provided name has a wildcard.
169 func containsWildcards(name string) bool {
170 for i := 0; i < len(name); i++ {
171 ch := name[i]
172 if ch == '\\' {
173 i++
174 } else if ch == '*' || ch == '?' || ch == '[' {
175 return true
176 }
177 }
178 return false
179 }
180
181 // isURL returns true if the string appears to be a URL.
182 func isURL(s string) bool {
183 return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
184 }