Skip to content

Commit 21eea46

Browse files
Nils WireklintNils Wireklint
Nils Wireklint
authored and
Nils Wireklint
committed
Implement Windows drive letter support
1 parent d5fefb3 commit 21eea46

16 files changed

+285
-25
lines changed

pkg/filesystem/local_directory_windows.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ func newLocalDirectory(absPath string, openReparsePoint bool) (DirectoryCloser,
109109
}
110110

111111
func NewLocalDirectory(path string) (DirectoryCloser, error) {
112-
absPath, err := filepath.Abs(path)
113-
if err != nil {
114-
return nil, err
115-
}
116-
absPath = "\\??\\" + absPath
112+
absPath := "\\??\\" + path
117113
return newLocalDirectory(absPath, true)
118114
}
119115

pkg/filesystem/path/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ go_library(
2121
"virtual_root_scope_walker_factory.go",
2222
"void_component_walker.go",
2323
"void_scope_walker.go",
24+
"windows_parser.go",
2425
],
2526
importpath = "github.com/buildbarn/bb-storage/pkg/filesystem/path",
2627
visibility = ["//visibility:public"],

pkg/filesystem/path/absolute_scope_walker.go

+4
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ func (pw *absoluteScopeWalker) OnRelative() (ComponentWalker, error) {
2424
func (pw *absoluteScopeWalker) OnAbsolute() (ComponentWalker, error) {
2525
return pw.componentWalker, nil
2626
}
27+
28+
func (pw *absoluteScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
29+
return pw.componentWalker, nil
30+
}

pkg/filesystem/path/builder.go

+79-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
// system.
2121
type Builder struct {
2222
absolute bool
23+
driveletter rune
2324
components []string
2425
firstReversibleIndex int
2526
suffix string
@@ -41,8 +42,18 @@ var EmptyBuilder = Builder{
4142
// Builder that use this path as their starting point can be created by
4243
// calling RootBuilder.Join().
4344
var RootBuilder = Builder{
44-
absolute: true,
45-
suffix: "/",
45+
absolute: true,
46+
driveletter: rune(0),
47+
suffix: "/",
48+
}
49+
50+
// NewDriveLetterBuilder returns a builder rooted at a Windows drive.
51+
func NewDriveLetterBuilder(drive rune) Builder {
52+
return Builder{
53+
absolute: false,
54+
driveletter: drive,
55+
suffix: "/",
56+
}
4657
}
4758

4859
// GetUNIXString returns a string representation of the path for use on
@@ -53,6 +64,12 @@ func (b *Builder) GetUNIXString() string {
5364
if b.absolute {
5465
prefix = "/"
5566
}
67+
if b.driveletter != rune(0) {
68+
// Drive letter is not meaningful in the REAPI. We just drop it.
69+
// Symlinks may point to the same drive but not other drives,
70+
// so it can be implicit.
71+
prefix = "/"
72+
}
5673
var out strings.Builder
5774
for _, component := range b.components {
5875
out.WriteString(prefix)
@@ -62,15 +79,57 @@ func (b *Builder) GetUNIXString() string {
6279

6380
// Emit trailing slash in case the path refers to a directory,
6481
// or a dot or slash if the path is empty.
65-
out.WriteString(b.suffix)
82+
suffix := b.suffix
83+
// The suffix may have been constructed by platform-specific code that
84+
// uses backslashes. To construct a UNIX path we must use a forward slash.
85+
// We can construct UNIX paths from Windows-native paths, where the On*
86+
// scope functions set the suffix to backslash.
87+
if suffix == "\\" {
88+
suffix = "/"
89+
}
90+
out.WriteString(suffix)
91+
return out.String()
92+
}
93+
94+
// GetWindowsString returns a string representation of the path for use on
95+
// UNIX-like operating systems. We do not create a windows-style path.
96+
func (b *Builder) GetWindowsString() string {
97+
// Emit pathname components.
98+
prefix := ""
99+
if b.absolute {
100+
prefix = "\\"
101+
}
102+
103+
var out strings.Builder
104+
if b.driveletter != rune(0) {
105+
out.WriteString(string(b.driveletter) + ":")
106+
prefix = "\\"
107+
}
108+
109+
for _, component := range b.components {
110+
out.WriteString(prefix)
111+
out.WriteString(component)
112+
prefix = "\\"
113+
}
114+
115+
// Emit trailing slash in case the path refers to a directory,
116+
// or a dot or slash if the path is empty.
117+
suffix := b.suffix
118+
// The suffix may have been constructed by platform-independent code that
119+
// uses forward slashes. To construct a Windows path we must use a
120+
// backslash.
121+
if suffix == "/" {
122+
suffix = "\\"
123+
}
124+
out.WriteString(suffix)
66125
return out.String()
67126
}
68127

69128
func (b *Builder) addTrailingSlash() {
70129
if len(b.components) == 0 {
71130
// An empty path. Ensure we either emit a "/" or ".",
72-
// depending on whether the path is absolute.
73-
if b.absolute {
131+
// depending on whether the path is absolute/driveletter.
132+
if b.absolute || b.driveletter != rune(0) {
74133
b.suffix = "/"
75134
} else {
76135
b.suffix = "."
@@ -106,6 +165,8 @@ func (b *Builder) getComponentWalker(base ComponentWalker) ComponentWalker {
106165
func (b *Builder) ParseScope(scopeWalker ScopeWalker) (next ComponentWalker, remainder RelativeParser, err error) {
107166
if b.absolute {
108167
next, err = scopeWalker.OnAbsolute()
168+
} else if b.driveletter != rune(0) {
169+
next, err = scopeWalker.OnDriveLetter(b.driveletter)
109170
} else {
110171
next, err = scopeWalker.OnRelative()
111172
}
@@ -154,6 +215,19 @@ func (w *buildingScopeWalker) OnAbsolute() (ComponentWalker, error) {
154215
return w.b.getComponentWalker(componentWalker), nil
155216
}
156217

218+
func (w *buildingScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
219+
componentWalker, err := w.base.OnDriveLetter(drive)
220+
if err != nil {
221+
return nil, err
222+
}
223+
*w.b = Builder{
224+
driveletter: drive,
225+
components: w.b.components[:0],
226+
suffix: "\\",
227+
}
228+
return w.b.getComponentWalker(componentWalker), nil
229+
}
230+
157231
func (w *buildingScopeWalker) OnRelative() (ComponentWalker, error) {
158232
componentWalker, err := w.base.OnRelative()
159233
if err != nil {

pkg/filesystem/path/builder_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package path_test
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/buildbarn/bb-storage/internal/mock"
@@ -31,13 +32,77 @@ func TestBuilder(t *testing.T) {
3132
"/hello/../world/foo",
3233
} {
3334
t.Run(p, func(t *testing.T) {
35+
// Unix Parser
3436
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
3537
require.NoError(t, path.Resolve(path.MustNewUNIXParser(p), scopewalker1))
3638
require.Equal(t, p, builder1.GetUNIXString())
3739

3840
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
3941
require.NoError(t, path.Resolve(builder1, scopewalker2))
4042
require.Equal(t, p, builder2.GetUNIXString())
43+
44+
// Windows Parser, compare Windows and UNIX string identity.
45+
windowsStyle := strings.Replace(p, "/", "\\", -1)
46+
builder3, scopewalker3 := path.EmptyBuilder.Join(path.VoidScopeWalker)
47+
require.NoError(t, path.Resolve(path.MustNewWindowsParser(p), scopewalker3))
48+
require.Equal(t, windowsStyle, builder3.GetWindowsString())
49+
require.Equal(t, p, builder3.GetUNIXString())
50+
51+
builder4, scopewalker4 := path.EmptyBuilder.Join(path.VoidScopeWalker)
52+
require.NoError(t, path.Resolve(builder3, scopewalker4))
53+
require.Equal(t, windowsStyle, builder4.GetWindowsString())
54+
require.Equal(t, p, builder4.GetUNIXString())
55+
})
56+
}
57+
58+
for _, p := range []string{
59+
"/C/",
60+
"/C/hello/",
61+
"/C/hello/..",
62+
"/C/hello/../world",
63+
"/C/hello/../world/",
64+
"/C/hello/../world/foo",
65+
"\\C\\",
66+
"\\C\\hello\\",
67+
"\\C\\hello\\..",
68+
"\\C\\hello\\..\\world",
69+
"\\C\\hello\\..\\world\\",
70+
"\\C\\hello\\..\\world\\foo",
71+
"C:\\",
72+
"C:\\hello\\",
73+
"C:\\hello\\..",
74+
"C:\\hello\\..\\world",
75+
"C:\\hello\\..\\world\\",
76+
"C:\\hello\\..\\world\\foo",
77+
} {
78+
t.Run(p, func(t *testing.T) {
79+
// Drive letter paths. These are printed as qualified absolute
80+
// paths by the Windows string formatter, and as bare absolute
81+
// UNIX paths without a drive letter.
82+
noDriveletter := strings.Replace(p[2:], "\\", "/", -1)
83+
windowsStyle := strings.Replace(noDriveletter, "/", "\\", -1)
84+
windowsDriveletter := "C:" + windowsStyle
85+
86+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
87+
require.NoError(t, path.Resolve(path.MustNewWindowsParser(p), scopewalker1))
88+
require.Equal(t, windowsDriveletter, builder1.GetWindowsString())
89+
require.Equal(t, noDriveletter, builder1.GetUNIXString())
90+
91+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
92+
require.NoError(t, path.Resolve(builder1, scopewalker2))
93+
require.Equal(t, windowsDriveletter, builder2.GetWindowsString())
94+
require.Equal(t, noDriveletter, builder2.GetUNIXString())
95+
})
96+
}
97+
for _, p := range []string{} {
98+
t.Run(p, func(t *testing.T) {
99+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
100+
require.NoError(t, path.Resolve(path.MustNewWindowsParser(p), scopewalker1))
101+
require.Equal(t, p, builder1.GetWindowsString())
102+
103+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
104+
require.NoError(t, path.Resolve(builder1, scopewalker2))
105+
require.Equal(t, p, builder2.GetWindowsString())
41106
})
42107
}
43108
})

pkg/filesystem/path/component_walker.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ type ComponentWalker interface {
5656
// If the pathname component refers to a symbolic link, this
5757
// function will return a GotSymlink containing a ScopeWalker, which
5858
// can be used to perform expansion of the symbolic link. The
59-
// Resolve() function will call into OnAbsolute() or OnRelative() to
60-
// signal whether resolution should continue at the root directory
61-
// or at the directory that contained the symbolic link.
59+
// Resolve() function will call into OnAbsolute(), OnRealtive() or
60+
// OnDriveLetter() to signal whether resolution should continue at
61+
// the root directory or at the directory that contained the
62+
// symbolic link.
6263
OnDirectory(name Component) (GotDirectoryOrSymlink, error)
6364

6465
// OnTerminal is called for the potentially last pathname

pkg/filesystem/path/local_windows.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import (
99
// NewLocalParser creates a pathname parser for paths that are native to
1010
// the locally running operating system.
1111
func NewLocalParser(path string) (Parser, error) {
12-
// TODO: Implement an actual Windows pathname parser.
13-
return NewUNIXParser(filepath.ToSlash(path))
12+
return NewWindowsParser(filepath.ToSlash(path))
1413
}
1514

1615
// GetLocalString converts a path to a string representation that is
1716
// supported by the locally running operating system.
1817
func GetLocalString(s Stringer) (string, error) {
19-
// TODO: Implement an actual Windows pathname formatter.
20-
return filepath.FromSlash(s.GetUNIXString()), nil
18+
return s.GetWindowsString(), nil
2119
}

pkg/filesystem/path/loop_detecting_scope_walker.go

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ func (w *loopDetectingScopeWalker) OnAbsolute() (ComponentWalker, error) {
3434
}, nil
3535
}
3636

37+
func (w *loopDetectingScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
38+
componentWalker, err := w.base.OnDriveLetter(drive)
39+
if err != nil {
40+
return nil, err
41+
}
42+
return &loopDetectingComponentWalker{
43+
base: componentWalker,
44+
symlinksLeft: w.symlinksLeft,
45+
}, nil
46+
}
47+
3748
func (w *loopDetectingScopeWalker) OnRelative() (ComponentWalker, error) {
3849
componentWalker, err := w.base.OnRelative()
3950
if err != nil {

pkg/filesystem/path/relative_scope_walker.go

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (pw *relativeScopeWalker) OnAbsolute() (ComponentWalker, error) {
2121
return nil, status.Error(codes.InvalidArgument, "Path is absolute, while a relative path was expected")
2222
}
2323

24+
func (pw *relativeScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
25+
return nil, status.Error(codes.InvalidArgument, "Path is absolute with drive letter, while a relative path was expected")
26+
}
27+
2428
func (pw *relativeScopeWalker) OnRelative() (ComponentWalker, error) {
2529
return pw.componentWalker, nil
2630
}

pkg/filesystem/path/scope_walker.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ type ScopeWalker interface {
77
// One of these functions is called right before processing the
88
// first component in the path (if any). Based on the
99
// characteristics of the path. Absolute paths are handled through
10-
// OnAbsolute(), and relative paths require OnRelative().
10+
// OnAbsolute(), and relative paths require OnRelative(), on Windows
11+
// absolute paths can also start with a drive letter, which is handled
12+
// through OnDriveLetter().
1113
//
1214
// These functions can be used by the implementation to determine
1315
// whether path resolution needs to be relative to the current
@@ -25,4 +27,5 @@ type ScopeWalker interface {
2527
// system.
2628
OnAbsolute() (ComponentWalker, error)
2729
OnRelative() (ComponentWalker, error)
30+
OnDriveLetter(drive rune) (ComponentWalker, error)
2831
}

pkg/filesystem/path/stringer.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ package path
44
// converted to string representations.
55
type Stringer interface {
66
GetUNIXString() string
7+
GetWindowsString() string
78
}

pkg/filesystem/path/trace.go

+10
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ func (t *Trace) GetUNIXString() string {
4444
t.writeToStringBuilder(&sb)
4545
return sb.String()
4646
}
47+
48+
// GetWindowsString returns a string representation of the path for use on Windows.
49+
func (t *Trace) GetWindowsString() string {
50+
if t == nil {
51+
return "."
52+
}
53+
var sb strings.Builder
54+
t.writeToStringBuilder(&sb)
55+
return sb.String()
56+
}

pkg/filesystem/path/unix_parser.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,34 @@ func (p unixParser) ParseScope(scopeWalker ScopeWalker) (next ComponentWalker, r
4949
return nil, nil, err
5050
}
5151

52-
return next, unixRelativeParser{stripOneOrMoreSlashes(p.path)}, nil
52+
return next, relativeParser{stripOneOrMoreSlashes(p.path), '/'}, nil
5353
}
5454

5555
next, err = scopeWalker.OnRelative()
5656
if err != nil {
5757
return nil, nil, err
5858
}
5959

60-
return next, unixRelativeParser{p.path}, nil
60+
return next, relativeParser{p.path, '/'}, nil
6161
}
6262

63-
type unixRelativeParser struct {
64-
path string
63+
type relativeParser struct {
64+
path string
65+
pathSeparator byte
6566
}
6667

67-
func (rp unixRelativeParser) ParseFirstComponent(componentWalker ComponentWalker, mustBeDirectory bool) (next GotDirectoryOrSymlink, remainder RelativeParser, err error) {
68+
func (rp relativeParser) ParseFirstComponent(componentWalker ComponentWalker, mustBeDirectory bool) (next GotDirectoryOrSymlink, remainder RelativeParser, err error) {
6869
var name string
6970
terminal := false
70-
if slash := strings.IndexByte(rp.path, '/'); slash == -1 {
71+
if slash := strings.IndexByte(rp.path, rp.pathSeparator); slash == -1 {
7172
// Path no longer contains a slash. Consume it entirely.
7273
terminal = true
7374
name = rp.path
7475
remainder = nil
7576
} else {
7677
name = rp.path[:slash]
7778
rp.path = stripOneOrMoreSlashes(rp.path[slash:])
78-
remainder = unixRelativeParser{rp.path}
79+
remainder = relativeParser{rp.path, rp.pathSeparator}
7980
}
8081

8182
switch name {

pkg/filesystem/path/virtual_root_scope_walker_factory.go

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ func (w *virtualRootScopeWalker) OnAbsolute() (ComponentWalker, error) {
207207
return w.getComponentWalker(w.rootNode)
208208
}
209209

210+
func (w *virtualRootScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
211+
return w.getComponentWalker(w.rootNode)
212+
}
213+
210214
func (w *virtualRootScopeWalker) OnRelative() (ComponentWalker, error) {
211215
// Attempted to resolve a relative path. There is no need to
212216
// rewrite any paths. Do wrap the ComponentWalker to ensure

0 commit comments

Comments
 (0)