Browse code

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 <vincent@sbr.pm>

Vincent Demeester authored on 2016/02/10 06:19:09
Showing 6 changed files
... ...
@@ -17,6 +17,7 @@ import (
17 17
 	"golang.org/x/net/context"
18 18
 
19 19
 	"github.com/docker/docker/api"
20
+	"github.com/docker/docker/builder"
20 21
 	"github.com/docker/docker/builder/dockerignore"
21 22
 	Cli "github.com/docker/docker/cli"
22 23
 	"github.com/docker/docker/opts"
... ...
@@ -143,7 +144,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
143 143
 			}
144 144
 		}
145 145
 
146
-		if err := validateContextDirectory(contextDir, excludes); err != nil {
146
+		if err := builder.ValidateContextDirectory(contextDir, excludes); err != nil {
147 147
 			return fmt.Errorf("Error checking context: '%s'.", err)
148 148
 		}
149 149
 
... ...
@@ -281,54 +282,6 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
281 281
 	return nil
282 282
 }
283 283
 
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
-
332 284
 // validateTag checks if the given image name can be resolved.
333 285
 func validateTag(rawRepo string) (string, error) {
334 286
 	_, err := reference.ParseNamed(rawRepo)
335 287
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-// +build !windows
2
-
3
-package client
4
-
5
-import (
6
-	"path/filepath"
7
-)
8
-
9
-func getContextRoot(srcPath string) (string, error) {
10
-	return filepath.Join(srcPath, "."), nil
11
-}
12 1
deleted file mode 100644
... ...
@@ -1,17 +0,0 @@
1
-// +build windows
2
-
3
-package client
4
-
5
-import (
6
-	"path/filepath"
7
-
8
-	"github.com/docker/docker/pkg/longpath"
9
-)
10
-
11
-func getContextRoot(srcPath string) (string, error) {
12
-	cr, err := filepath.Abs(srcPath)
13
-	if err != nil {
14
-		return "", err
15
-	}
16
-	return longpath.AddPrefix(cr), nil
17
-}
18 1
new file mode 100644
... ...
@@ -0,0 +1,57 @@
0
+package builder
1
+
2
+import (
3
+	"fmt"
4
+	"os"
5
+	"path/filepath"
6
+
7
+	"github.com/docker/docker/pkg/fileutils"
8
+)
9
+
10
+// ValidateContextDirectory checks if all the contents of the directory
11
+// can be read and returns an error if some files can't be read
12
+// symlinks which point to non-existing files don't trigger an error
13
+func ValidateContextDirectory(srcPath string, excludes []string) error {
14
+	contextRoot, err := getContextRoot(srcPath)
15
+	if err != nil {
16
+		return err
17
+	}
18
+	return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
19
+		// skip this directory/file if it's not in the path, it won't get added to the context
20
+		if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
21
+			return err
22
+		} else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
23
+			return err
24
+		} else if skip {
25
+			if f.IsDir() {
26
+				return filepath.SkipDir
27
+			}
28
+			return nil
29
+		}
30
+
31
+		if err != nil {
32
+			if os.IsPermission(err) {
33
+				return fmt.Errorf("can't stat '%s'", filePath)
34
+			}
35
+			if os.IsNotExist(err) {
36
+				return nil
37
+			}
38
+			return err
39
+		}
40
+
41
+		// skip checking if symlinks point to non-existing files, such symlinks can be useful
42
+		// also skip named pipes, because they hanging on open
43
+		if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
44
+			return nil
45
+		}
46
+
47
+		if !f.IsDir() {
48
+			currentFile, err := os.Open(filePath)
49
+			if err != nil && os.IsPermission(err) {
50
+				return fmt.Errorf("no permission to read from '%s'", filePath)
51
+			}
52
+			currentFile.Close()
53
+		}
54
+		return nil
55
+	})
56
+}
0 57
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+// +build !windows
1
+
2
+package builder
3
+
4
+import (
5
+	"path/filepath"
6
+)
7
+
8
+func getContextRoot(srcPath string) (string, error) {
9
+	return filepath.Join(srcPath, "."), nil
10
+}
0 11
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+// +build windows
1
+
2
+package builder
3
+
4
+import (
5
+	"path/filepath"
6
+
7
+	"github.com/docker/docker/pkg/longpath"
8
+)
9
+
10
+func getContextRoot(srcPath string) (string, error) {
11
+	cr, err := filepath.Abs(srcPath)
12
+	if err != nil {
13
+		return "", err
14
+	}
15
+	return longpath.AddPrefix(cr), nil
16
+}