Browse code

ApplyLayer: Fix TestLookupImage

The TestLookupImage test seems to use a layer that contains
/etc/postgres/postgres.conf, but not e.g. /etc/postgres.

To handle this we ensure that the parent directory always
exists, and if not we create it.

Alexander Larsson authored on 2013/12/16 22:35:43
Showing 1 changed files
... ...
@@ -49,6 +49,23 @@ func ApplyLayer(dest string, layer Archive) error {
49 49
 			return err
50 50
 		}
51 51
 
52
+		// Normalize name, for safety and for a simple is-root check
53
+		hdr.Name = filepath.Clean(hdr.Name)
54
+
55
+		if !strings.HasSuffix(hdr.Name, "/") {
56
+			// Not the root directory, ensure that the parent directory exists
57
+			// This happened in some tests where an image had a tarfile without any
58
+			// parent directories
59
+			parent := filepath.Dir(hdr.Name)
60
+			parentPath := filepath.Join(dest, parent)
61
+			if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
62
+				err = os.MkdirAll(parentPath, 600)
63
+				if err != nil {
64
+					return err
65
+				}
66
+			}
67
+		}
68
+
52 69
 		// Skip AUFS metadata dirs
53 70
 		if strings.HasPrefix(hdr.Name, ".wh..wh.") {
54 71
 			continue