Skip to content

Commit 69f2c06

Browse files
Use system.ToSlash() instead of filepath.ToSlash()
When running on Linux, filepath.ToSlash() is not suitable to convert paths containing Windows path delimiter to Unix delimiters. To enable this, we export system.ToSlash() that takes a platform flag, based on which we use the proper file path separator, regardless of the platform we run on. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 805b993 commit 69f2c06

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo
10031003
d.image.Config.WorkingDir = wd
10041004

10051005
// From this point forward, we can use UNIX style paths.
1006-
wd = filepath.ToSlash(wd)
1006+
wd = system.ToSlash(wd, d.platform.OS)
10071007
d.state = d.state.Dir(wd)
10081008

10091009
if commit {
@@ -1421,7 +1421,6 @@ func pathRelativeToWorkingDir(s llb.State, p string, platform ocispecs.Platform)
14211421
if len(p) == 0 {
14221422
return dir, nil
14231423
}
1424-
14251424
p, err = system.CheckSystemDriveAndRemoveDriveLetter(p, platform.OS)
14261425
if err != nil {
14271426
return "", errors.Wrap(err, "remving drive letter")

util/system/path.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func NormalizePath(parent, newPath, inputOS string, keepSlash bool) (string, err
3737
inputOS = "linux"
3838
}
3939

40-
newPath = toSlash(newPath, inputOS)
41-
parent = toSlash(parent, inputOS)
40+
newPath = ToSlash(newPath, inputOS)
41+
parent = ToSlash(parent, inputOS)
4242
origPath := newPath
4343

4444
if parent == "" {
@@ -82,18 +82,18 @@ func NormalizePath(parent, newPath, inputOS string, keepSlash bool) (string, err
8282
}
8383
}
8484

85-
return toSlash(newPath, inputOS), nil
85+
return ToSlash(newPath, inputOS), nil
8686
}
8787

88-
func toSlash(inputPath, inputOS string) string {
88+
func ToSlash(inputPath, inputOS string) string {
8989
separator := "/"
9090
if inputOS == "windows" {
9191
separator = "\\"
9292
}
9393
return strings.Replace(inputPath, separator, "/", -1)
9494
}
9595

96-
func fromSlash(inputPath, inputOS string) string {
96+
func FromSlash(inputPath, inputOS string) string {
9797
separator := "/"
9898
if inputOS == "windows" {
9999
separator = "\\"
@@ -119,7 +119,7 @@ func NormalizeWorkdir(current, wd string, inputOS string) (string, error) {
119119

120120
// Make sure we use the platform specific path separator. HCS does not like forward
121121
// slashes in CWD.
122-
return fromSlash(wd, inputOS), nil
122+
return FromSlash(wd, inputOS), nil
123123
}
124124

125125
// IsAbs returns a boolean value indicating whether or not the path
@@ -142,7 +142,7 @@ func IsAbs(pth, inputOS string) bool {
142142
if err != nil {
143143
return false
144144
}
145-
cleanedPath = toSlash(cleanedPath, inputOS)
145+
cleanedPath = ToSlash(cleanedPath, inputOS)
146146
// We stripped any potential drive letter and converted any backslashes to
147147
// forward slashes. We can safely use path.IsAbs() for both Windows and Linux.
148148
return path.IsAbs(cleanedPath)
@@ -189,14 +189,14 @@ func CheckSystemDriveAndRemoveDriveLetter(path string, inputOS string) (string,
189189
}
190190

191191
// UNC paths should error out
192-
if len(path) >= 2 && toSlash(path[:2], inputOS) == "//" {
192+
if len(path) >= 2 && ToSlash(path[:2], inputOS) == "//" {
193193
return "", errors.Errorf("UNC paths are not supported")
194194
}
195195

196196
parts := strings.SplitN(path, ":", 2)
197197
// Path does not have a drive letter. Just return it.
198198
if len(parts) < 2 {
199-
return toSlash(filepath.Clean(path), inputOS), nil
199+
return ToSlash(filepath.Clean(path), inputOS), nil
200200
}
201201

202202
// We expect all paths to be in C:
@@ -221,5 +221,5 @@ func CheckSystemDriveAndRemoveDriveLetter(path string, inputOS string) (string,
221221
//
222222
// We must return the second element of the split path, as is, without attempting to convert
223223
// it to an absolute path. We have no knowledge of the CWD; that is treated elsewhere.
224-
return toSlash(filepath.Clean(parts[1]), inputOS), nil
224+
return ToSlash(filepath.Clean(parts[1]), inputOS), nil
225225
}

0 commit comments

Comments
 (0)