Skip to content

Commit fc6122a

Browse files
committedFeb 9, 2016
Move validateContextDirectory to builder package.
This feels like it's where it belongs and it makes it exported again (which is needed for libcompose that was using it before 1.10). Signed-off-by: Vincent Demeester <[email protected]>
1 parent fa860c8 commit fc6122a

File tree

4 files changed

+61
-51
lines changed

4 files changed

+61
-51
lines changed
 

‎api/client/build.go

+2-49
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"golang.org/x/net/context"
1818

1919
"github.com/docker/docker/api"
20+
"github.com/docker/docker/builder"
2021
"github.com/docker/docker/builder/dockerignore"
2122
Cli "github.com/docker/docker/cli"
2223
"github.com/docker/docker/opts"
@@ -143,7 +144,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
143144
}
144145
}
145146

146-
if err := validateContextDirectory(contextDir, excludes); err != nil {
147+
if err := builder.ValidateContextDirectory(contextDir, excludes); err != nil {
147148
return fmt.Errorf("Error checking context: '%s'.", err)
148149
}
149150

@@ -281,54 +282,6 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
281282
return nil
282283
}
283284

284-
// validateContextDirectory checks if all the contents of the directory
285-
// can be read and returns an error if some files can't be read
286-
// symlinks which point to non-existing files don't trigger an error
287-
func validateContextDirectory(srcPath string, excludes []string) error {
288-
contextRoot, err := getContextRoot(srcPath)
289-
if err != nil {
290-
return err
291-
}
292-
return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
293-
// skip this directory/file if it's not in the path, it won't get added to the context
294-
if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
295-
return err
296-
} else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
297-
return err
298-
} else if skip {
299-
if f.IsDir() {
300-
return filepath.SkipDir
301-
}
302-
return nil
303-
}
304-
305-
if err != nil {
306-
if os.IsPermission(err) {
307-
return fmt.Errorf("can't stat '%s'", filePath)
308-
}
309-
if os.IsNotExist(err) {
310-
return nil
311-
}
312-
return err
313-
}
314-
315-
// skip checking if symlinks point to non-existing files, such symlinks can be useful
316-
// also skip named pipes, because they hanging on open
317-
if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
318-
return nil
319-
}
320-
321-
if !f.IsDir() {
322-
currentFile, err := os.Open(filePath)
323-
if err != nil && os.IsPermission(err) {
324-
return fmt.Errorf("no permission to read from '%s'", filePath)
325-
}
326-
currentFile.Close()
327-
}
328-
return nil
329-
})
330-
}
331-
332285
// validateTag checks if the given image name can be resolved.
333286
func validateTag(rawRepo string) (string, error) {
334287
_, err := reference.ParseNamed(rawRepo)

‎builder/context.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package builder
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/docker/docker/pkg/fileutils"
9+
)
10+
11+
// ValidateContextDirectory checks if all the contents of the directory
12+
// can be read and returns an error if some files can't be read
13+
// symlinks which point to non-existing files don't trigger an error
14+
func ValidateContextDirectory(srcPath string, excludes []string) error {
15+
contextRoot, err := getContextRoot(srcPath)
16+
if err != nil {
17+
return err
18+
}
19+
return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
20+
// skip this directory/file if it's not in the path, it won't get added to the context
21+
if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
22+
return err
23+
} else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
24+
return err
25+
} else if skip {
26+
if f.IsDir() {
27+
return filepath.SkipDir
28+
}
29+
return nil
30+
}
31+
32+
if err != nil {
33+
if os.IsPermission(err) {
34+
return fmt.Errorf("can't stat '%s'", filePath)
35+
}
36+
if os.IsNotExist(err) {
37+
return nil
38+
}
39+
return err
40+
}
41+
42+
// skip checking if symlinks point to non-existing files, such symlinks can be useful
43+
// also skip named pipes, because they hanging on open
44+
if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
45+
return nil
46+
}
47+
48+
if !f.IsDir() {
49+
currentFile, err := os.Open(filePath)
50+
if err != nil && os.IsPermission(err) {
51+
return fmt.Errorf("no permission to read from '%s'", filePath)
52+
}
53+
currentFile.Close()
54+
}
55+
return nil
56+
})
57+
}

‎api/client/utils_unix.go ‎builder/context_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build !windows
22

3-
package client
3+
package builder
44

55
import (
66
"path/filepath"

‎api/client/utils_windows.go ‎builder/context_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build windows
22

3-
package client
3+
package builder
44

55
import (
66
"path/filepath"

0 commit comments

Comments
 (0)
Please sign in to comment.