Browse code

Windows - make docker cp functional

Signed-off-by: John Howard <jhoward@microsoft.com>

Conflicts:
pkg/archive/copy.go

Make it compile

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2015/07/29 11:55:14
Showing 5 changed files
... ...
@@ -3,6 +3,7 @@ package server
3 3
 import (
4 4
 	"fmt"
5 5
 	"net/http"
6
+	"path/filepath"
6 7
 	"strconv"
7 8
 	"strings"
8 9
 )
... ...
@@ -43,7 +44,7 @@ func archiveFormValues(r *http.Request, vars map[string]string) (archiveOptions,
43 43
 	}
44 44
 
45 45
 	name := vars["name"]
46
-	path := r.Form.Get("path")
46
+	path := filepath.FromSlash(r.Form.Get("path"))
47 47
 
48 48
 	switch {
49 49
 	case name == "":
... ...
@@ -28,8 +28,12 @@ var (
28 28
 // path already ends in a `.` path segment, then another is not added. If the
29 29
 // clean path already ends in a path separator, then another is not added.
30 30
 func PreserveTrailingDotOrSeparator(cleanedPath, originalPath string) string {
31
-	if !SpecifiesCurrentDir(cleanedPath) && SpecifiesCurrentDir(originalPath) {
32
-		if !HasTrailingPathSeparator(cleanedPath) {
31
+	// Ensure paths are in platform semantics
32
+	cleanedPath = normalizePath(cleanedPath)
33
+	originalPath = normalizePath(originalPath)
34
+
35
+	if !specifiesCurrentDir(cleanedPath) && specifiesCurrentDir(originalPath) {
36
+		if !hasTrailingPathSeparator(cleanedPath) {
33 37
 			// Add a separator if it doesn't already end with one (a cleaned
34 38
 			// path would only end in a separator if it is the root).
35 39
 			cleanedPath += string(filepath.Separator)
... ...
@@ -37,29 +41,29 @@ func PreserveTrailingDotOrSeparator(cleanedPath, originalPath string) string {
37 37
 		cleanedPath += "."
38 38
 	}
39 39
 
40
-	if !HasTrailingPathSeparator(cleanedPath) && HasTrailingPathSeparator(originalPath) {
40
+	if !hasTrailingPathSeparator(cleanedPath) && hasTrailingPathSeparator(originalPath) {
41 41
 		cleanedPath += string(filepath.Separator)
42 42
 	}
43 43
 
44 44
 	return cleanedPath
45 45
 }
46 46
 
47
-// AssertsDirectory returns whether the given path is
47
+// assertsDirectory returns whether the given path is
48 48
 // asserted to be a directory, i.e., the path ends with
49 49
 // a trailing '/' or `/.`, assuming a path separator of `/`.
50
-func AssertsDirectory(path string) bool {
51
-	return HasTrailingPathSeparator(path) || SpecifiesCurrentDir(path)
50
+func assertsDirectory(path string) bool {
51
+	return hasTrailingPathSeparator(path) || specifiesCurrentDir(path)
52 52
 }
53 53
 
54
-// HasTrailingPathSeparator returns whether the given
54
+// hasTrailingPathSeparator returns whether the given
55 55
 // path ends with the system's path separator character.
56
-func HasTrailingPathSeparator(path string) bool {
56
+func hasTrailingPathSeparator(path string) bool {
57 57
 	return len(path) > 0 && os.IsPathSeparator(path[len(path)-1])
58 58
 }
59 59
 
60
-// SpecifiesCurrentDir returns whether the given path specifies
60
+// specifiesCurrentDir returns whether the given path specifies
61 61
 // a "current directory", i.e., the last path segment is `.`.
62
-func SpecifiesCurrentDir(path string) bool {
62
+func specifiesCurrentDir(path string) bool {
63 63
 	return filepath.Base(path) == "."
64 64
 }
65 65
 
... ...
@@ -67,9 +71,9 @@ func SpecifiesCurrentDir(path string) bool {
67 67
 // basename by first cleaning the path but preserves a trailing "." if the
68 68
 // original path specified the current directory.
69 69
 func SplitPathDirEntry(path string) (dir, base string) {
70
-	cleanedPath := filepath.Clean(path)
70
+	cleanedPath := filepath.Clean(normalizePath(path))
71 71
 
72
-	if SpecifiesCurrentDir(path) {
72
+	if specifiesCurrentDir(path) {
73 73
 		cleanedPath += string(filepath.Separator) + "."
74 74
 	}
75 75
 
... ...
@@ -90,6 +94,7 @@ func TarResource(sourceInfo CopyInfo) (content Archive, err error) {
90 90
 // TarResourceRebase is like TarResource but renames the first path element of
91 91
 // items in the resulting tar archive to match the given rebaseName if not "".
92 92
 func TarResourceRebase(sourcePath, rebaseName string) (content Archive, err error) {
93
+	sourcePath = normalizePath(sourcePath)
93 94
 	if _, err = os.Lstat(sourcePath); err != nil {
94 95
 		// Catches the case where the source does not exist or is not a
95 96
 		// directory if asserted to be a directory, as this also causes an
... ...
@@ -132,6 +137,7 @@ type CopyInfo struct {
132 132
 func CopyInfoSourcePath(path string) (CopyInfo, error) {
133 133
 	// Split the given path into its Directory and Base components. We will
134 134
 	// evaluate symlinks in the directory component then append the base.
135
+	path = normalizePath(path)
135 136
 	dirPath, basePath := filepath.Split(path)
136 137
 
137 138
 	resolvedDirPath, err := filepath.EvalSymlinks(dirPath)
... ...
@@ -144,7 +150,7 @@ func CopyInfoSourcePath(path string) (CopyInfo, error) {
144 144
 	resolvedPath := resolvedDirPath + string(filepath.Separator) + basePath
145 145
 
146 146
 	var rebaseName string
147
-	if HasTrailingPathSeparator(path) && filepath.Base(path) != filepath.Base(resolvedPath) {
147
+	if hasTrailingPathSeparator(path) && filepath.Base(path) != filepath.Base(resolvedPath) {
148 148
 		// In the case where the path had a trailing separator and a symlink
149 149
 		// evaluation has changed the last path component, we will need to
150 150
 		// rebase the name in the archive that is being copied to match the
... ...
@@ -170,6 +176,7 @@ func CopyInfoSourcePath(path string) (CopyInfo, error) {
170 170
 // operation. The given path should be an absolute local path.
171 171
 func CopyInfoDestinationPath(path string) (info CopyInfo, err error) {
172 172
 	maxSymlinkIter := 10 // filepath.EvalSymlinks uses 255, but 10 already seems like a lot.
173
+	path = normalizePath(path)
173 174
 	originalPath := path
174 175
 
175 176
 	stat, err := os.Lstat(path)
... ...
@@ -247,6 +254,10 @@ func CopyInfoDestinationPath(path string) (info CopyInfo, err error) {
247 247
 // described by dstInfo. Returns the possibly modified content archive along
248 248
 // with the path to the destination directory which it should be extracted to.
249 249
 func PrepareArchiveCopy(srcContent Reader, srcInfo, dstInfo CopyInfo) (dstDir string, content Archive, err error) {
250
+	// Ensure in platform semantics
251
+	srcInfo.Path = normalizePath(srcInfo.Path)
252
+	dstInfo.Path = normalizePath(dstInfo.Path)
253
+
250 254
 	// Separate the destination path between its directory and base
251 255
 	// components in case the source archive contents need to be rebased.
252 256
 	dstDir, dstBase := SplitPathDirEntry(dstInfo.Path)
... ...
@@ -276,7 +287,7 @@ func PrepareArchiveCopy(srcContent Reader, srcInfo, dstInfo CopyInfo) (dstDir st
276 276
 		// The source content entries will have to be renamed to have a
277 277
 		// basename which matches the destination path's basename.
278 278
 		return dstDir, rebaseArchiveEntries(srcContent, srcBase, dstBase), nil
279
-	case AssertsDirectory(dstInfo.Path):
279
+	case assertsDirectory(dstInfo.Path):
280 280
 		// The destination does not exist and is asserted to be created as a
281 281
 		// directory, but the source content is not a directory. This is an
282 282
 		// error condition since you cannot create a directory from a file
... ...
@@ -297,7 +308,7 @@ func PrepareArchiveCopy(srcContent Reader, srcInfo, dstInfo CopyInfo) (dstDir st
297 297
 // rebaseArchiveEntries rewrites the given srcContent archive replacing
298 298
 // an occurance of oldBase with newBase at the beginning of entry names.
299 299
 func rebaseArchiveEntries(srcContent Reader, oldBase, newBase string) Archive {
300
-	if oldBase == "/" {
300
+	if oldBase == string(os.PathSeparator) {
301 301
 		// If oldBase specifies the root directory, use an empty string as
302 302
 		// oldBase instead so that newBase doesn't replace the path separator
303 303
 		// that all paths will start with.
... ...
@@ -349,6 +360,10 @@ func CopyResource(srcPath, dstPath string) error {
349 349
 		err     error
350 350
 	)
351 351
 
352
+	// Ensure in platform semantics
353
+	srcPath = normalizePath(srcPath)
354
+	dstPath = normalizePath(dstPath)
355
+
352 356
 	// Clean the source and destination paths.
353 357
 	srcPath = PreserveTrailingDotOrSeparator(filepath.Clean(srcPath), srcPath)
354 358
 	dstPath = PreserveTrailingDotOrSeparator(filepath.Clean(dstPath), dstPath)
... ...
@@ -371,7 +386,7 @@ func CopyResource(srcPath, dstPath string) error {
371 371
 func CopyTo(content Reader, srcInfo CopyInfo, dstPath string) error {
372 372
 	// The destination path need not exist, but CopyInfoDestinationPath will
373 373
 	// ensure that at least the parent directory exists.
374
-	dstInfo, err := CopyInfoDestinationPath(dstPath)
374
+	dstInfo, err := CopyInfoDestinationPath(normalizePath(dstPath))
375 375
 	if err != nil {
376 376
 		return err
377 377
 	}
378 378
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+// +build !windows
1
+
2
+package archive
3
+
4
+import (
5
+	"path/filepath"
6
+)
7
+
8
+func normalizePath(path string) string {
9
+	return filepath.ToSlash(path)
10
+}
0 11
new file mode 100644
... ...
@@ -0,0 +1,9 @@
0
+package archive
1
+
2
+import (
3
+	"path/filepath"
4
+)
5
+
6
+func normalizePath(path string) string {
7
+	return filepath.FromSlash(path)
8
+}
... ...
@@ -14,13 +14,14 @@ import (
14 14
 	"strings"
15 15
 )
16 16
 
17
-// FollowSymlinkInScope is a wrapper around evalSymlinksInScope that returns an absolute path
17
+// FollowSymlinkInScope is a wrapper around evalSymlinksInScope that returns an
18
+// absolute path. This function handles paths in a platform-agnostic manner.
18 19
 func FollowSymlinkInScope(path, root string) (string, error) {
19
-	path, err := filepath.Abs(path)
20
+	path, err := filepath.Abs(filepath.FromSlash(path))
20 21
 	if err != nil {
21 22
 		return "", err
22 23
 	}
23
-	root, err = filepath.Abs(root)
24
+	root, err = filepath.Abs(filepath.FromSlash(root))
24 25
 	if err != nil {
25 26
 		return "", err
26 27
 	}