-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect Windows absolute paths on non-Windows CLI
When deploying a stack using a relative path as bind-mount source in the compose file, the CLI converts the relative path to an absolute path, relative to the location of the docker-compose file. This causes a problem when deploying a stack that uses an absolute Windows path, because a non-Windows client will fail to detect that the path (e.g. `C:\somedir`) is an absolute path (and not a relative directory named `C:\`). The existing code did already take Windows clients deploying a Linux stack into account (by checking if the path had a leading slash). This patch adds the reverse, and adds detection for Windows absolute paths on non-Windows clients. The code used to detect Windows absolute paths is copied from the Golang filepath package; https://github.com/golang/go/blob/1d0e94b1e13d5e8a323a63cd1cc1ef95290c9c36/src/path/filepath/path_windows.go#L12-L65 Signed-off-by: Sebastiaan van Stijn <[email protected]>
- Loading branch information
Showing
3 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package loader | ||
|
||
// Copyright 2010 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// This file contains utilities to check for Windows absolute paths on Linux. | ||
// The code in this file was copied from the Golang filepath package | ||
// https://github.com/golang/go/blob/1d0e94b1e13d5e8a323a63cd1cc1ef95290c9c36/src/path/filepath/path_windows.go#L12-L65 | ||
|
||
import "path" | ||
|
||
func isSlash(c uint8) bool { | ||
return c == '\\' || c == '/' | ||
} | ||
|
||
// isAbs reports whether the path is a Windows absolute path. | ||
func isAbs(filePath string) (b bool) { | ||
if path.IsAbs(filePath) { | ||
return true | ||
} | ||
l := volumeNameLen(filePath) | ||
if l == 0 { | ||
return false | ||
} | ||
filePath = filePath[l:] | ||
if filePath == "" { | ||
return false | ||
} | ||
return isSlash(filePath[0]) | ||
} | ||
|
||
// volumeNameLen returns length of the leading volume name on Windows. | ||
// It returns 0 elsewhere. | ||
func volumeNameLen(path string) int { | ||
if len(path) < 2 { | ||
return 0 | ||
} | ||
// with drive letter | ||
c := path[0] | ||
if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { | ||
return 2 | ||
} | ||
// is it UNC? https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx | ||
if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) && | ||
!isSlash(path[2]) && path[2] != '.' { | ||
// first, leading `\\` and next shouldn't be `\`. its server name. | ||
for n := 3; n < l-1; n++ { | ||
// second, next '\' shouldn't be repeated. | ||
if isSlash(path[n]) { | ||
n++ | ||
// third, following something characters. its share name. | ||
if !isSlash(path[n]) { | ||
if path[n] == '.' { | ||
break | ||
} | ||
for ; n < l; n++ { | ||
if isSlash(path[n]) { | ||
break | ||
} | ||
} | ||
return n | ||
} | ||
break | ||
} | ||
} | ||
} | ||
return 0 | ||
} |