Skip to content

Commit e920afa

Browse files
Nils WireklintNils Wireklint
Nils Wireklint
authored and
Nils Wireklint
committed
Implement Windows drive letter support
1 parent 3fd41b6 commit e920afa

16 files changed

+373
-24
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

+86-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package path
22

33
import (
44
"strings"
5+
6+
"google.golang.org/grpc/codes"
7+
"google.golang.org/grpc/status"
58
)
69

710
// Builder for normalized pathname strings.
@@ -20,6 +23,7 @@ import (
2023
// system.
2124
type Builder struct {
2225
absolute bool
26+
driveLetter rune
2327
components []string
2428
firstReversibleIndex int
2529
suffix string
@@ -41,8 +45,17 @@ var EmptyBuilder = Builder{
4145
// Builder that use this path as their starting point can be created by
4246
// calling RootBuilder.Join().
4347
var RootBuilder = Builder{
44-
absolute: true,
45-
suffix: "/",
48+
absolute: true,
49+
suffix: "/",
50+
}
51+
52+
// NewDriveLetterBuilder returns a builder rooted at a Windows drive.
53+
func NewDriveLetterBuilder(drive rune) Builder {
54+
return Builder{
55+
absolute: false,
56+
driveLetter: drive,
57+
suffix: "/",
58+
}
4659
}
4760

4861
// GetUNIXString returns a string representation of the path for use on
@@ -66,10 +79,63 @@ func (b *Builder) GetUNIXString() string {
6679
return out.String()
6780
}
6881

82+
// GetWindowsString returns a string representation of the path for use on
83+
// Windows.
84+
func (b *Builder) GetWindowsString() (string, error) {
85+
// Emit pathname components.
86+
var out strings.Builder
87+
prefix := ""
88+
89+
if b.driveLetter != rune(0) {
90+
out.WriteString(string(b.driveLetter))
91+
out.WriteString(":")
92+
prefix = "\\"
93+
} else if b.absolute {
94+
prefix = "\\"
95+
}
96+
97+
for _, component := range b.components {
98+
for _, c := range component {
99+
if byte(c) < 31 {
100+
return "", status.Errorf(codes.InvalidArgument, "Path component contains a control character %#v.", component)
101+
}
102+
for _, forbidden := range []rune {
103+
'<',
104+
'>',
105+
':',
106+
'"',
107+
'/',
108+
'\\',
109+
'|',
110+
'?',
111+
'*',
112+
} {
113+
if c == forbidden {
114+
return "", status.Errorf(codes.InvalidArgument, "Path component contains a forbidden character %#v %#v", component, forbidden)
115+
}
116+
}
117+
}
118+
out.WriteString(prefix)
119+
out.WriteString(component)
120+
prefix = "\\"
121+
}
122+
123+
// Emit trailing slash in case the path refers to a directory, or a dot or
124+
// slash if the path is empty. The suffix may have been constructed by
125+
// platform-independent code that uses forward slashes. To construct a
126+
// Windows path we must use a backslash.
127+
suffix := b.suffix
128+
if suffix == "/" {
129+
suffix = "\\"
130+
}
131+
out.WriteString(suffix)
132+
return out.String(), nil
133+
}
134+
69135
func (b *Builder) addTrailingSlash() {
70136
if len(b.components) == 0 {
71137
// An empty path. Ensure we either emit a "/" or ".",
72-
// depending on whether the path is absolute.
138+
// depending on whether the path is absolute/drive letter.
73139
if b.absolute {
74140
b.suffix = "/"
75141
} else {
@@ -104,7 +170,9 @@ func (b *Builder) getComponentWalker(base ComponentWalker) ComponentWalker {
104170
// directly to Resolve(). This can be used to replay resolution of a
105171
// previously constructed path.
106172
func (b *Builder) ParseScope(scopeWalker ScopeWalker) (next ComponentWalker, remainder RelativeParser, err error) {
107-
if b.absolute {
173+
if b.driveLetter != rune(0) {
174+
next, err = scopeWalker.OnDriveLetter(b.driveLetter)
175+
} else if b.absolute {
108176
next, err = scopeWalker.OnAbsolute()
109177
} else {
110178
next, err = scopeWalker.OnRelative()
@@ -154,6 +222,20 @@ func (w *buildingScopeWalker) OnAbsolute() (ComponentWalker, error) {
154222
return w.b.getComponentWalker(componentWalker), nil
155223
}
156224

225+
func (w *buildingScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
226+
componentWalker, err := w.base.OnDriveLetter(drive)
227+
if err != nil {
228+
return nil, err
229+
}
230+
*w.b = Builder{
231+
absolute: true,
232+
driveLetter: drive,
233+
components: w.b.components[:0],
234+
suffix: "/",
235+
}
236+
return w.b.getComponentWalker(componentWalker), nil
237+
}
238+
157239
func (w *buildingScopeWalker) OnRelative() (ComponentWalker, error) {
158240
componentWalker, err := w.base.OnRelative()
159241
if err != nil {

pkg/filesystem/path/builder_test.go

+82-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ import (
1111

1212
func TestBuilder(t *testing.T) {
1313
ctrl := gomock.NewController(t)
14+
getWindowsString := func(t *testing.T, builder *path.Builder) string {
15+
s, err := builder.GetWindowsString()
16+
require.NoError(t, err)
17+
return s
18+
}
1419

1520
// The following paths should remain completely identical when
1621
// resolved without making any assumptions about the layout of
1722
// the underlying file system. ".." elements should not be
1823
// removed from paths.
19-
t.Run("Identity", func(t *testing.T) {
24+
t.Run("UNIXIdentity", func(t *testing.T) {
2025
for _, p := range []string{
2126
".",
2227
"..",
@@ -31,6 +36,7 @@ func TestBuilder(t *testing.T) {
3136
"/hello/../world/foo",
3237
} {
3338
t.Run(p, func(t *testing.T) {
39+
// Unix Parser
3440
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
3541
require.NoError(t, path.Resolve(path.MustNewUNIXParser(p), scopewalker1))
3642
require.Equal(t, p, builder1.GetUNIXString())
@@ -42,6 +48,81 @@ func TestBuilder(t *testing.T) {
4248
}
4349
})
4450

51+
t.Run("WindowsParseUNIXPaths", func(t *testing.T) {
52+
for _, data := range [][]string{
53+
{".", "."},
54+
{"..", ".."},
55+
{"/", "\\"},
56+
{"hello", "hello"},
57+
{"hello/", "hello\\"},
58+
{"hello/..", "hello\\.."},
59+
{"/hello/", "\\hello\\"},
60+
{"/hello/..", "\\hello\\.."},
61+
{"/hello/../world", "\\hello\\..\\world"},
62+
{"/hello/../world/", "\\hello\\..\\world\\"},
63+
{"/hello/../world/foo", "\\hello\\..\\world\\foo"},
64+
} {
65+
p := data[0]
66+
expected := data[1]
67+
t.Run(p, func(t *testing.T) {
68+
// Windows Parser, compare Windows and UNIX string identity.
69+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
70+
require.NoError(t, path.Resolve(path.MustNewWindowsParser(p), scopewalker1))
71+
require.Equal(t, expected, getWindowsString(t, builder1))
72+
require.Equal(t, p, builder1.GetUNIXString())
73+
74+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
75+
require.NoError(t, path.Resolve(builder1, scopewalker2))
76+
require.Equal(t, expected, getWindowsString(t, builder2))
77+
require.Equal(t, p, builder2.GetUNIXString())
78+
})
79+
}
80+
})
81+
82+
t.Run("WindowsIdentity", func(t *testing.T) {
83+
for _, p := range []string{
84+
"C:\\",
85+
"C:\\hello\\",
86+
"C:\\hello\\..",
87+
"C:\\hello\\..\\world",
88+
"C:\\hello\\..\\world\\",
89+
"C:\\hello\\..\\world\\foo",
90+
} {
91+
t.Run(p, func(t *testing.T) {
92+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
93+
require.NoError(t, path.Resolve(path.MustNewWindowsParser(p), scopewalker1))
94+
require.Equal(t, p, getWindowsString(t, builder1))
95+
96+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
97+
require.NoError(t, path.Resolve(builder1, scopewalker2))
98+
require.Equal(t, p, getWindowsString(t, builder2))
99+
})
100+
}
101+
})
102+
103+
t.Run("WindowsParseAndWriteUNIXPaths", func(t *testing.T) {
104+
for _, data := range [][]string{
105+
{"C:\\", "/", },
106+
{"C:\\hello\\", "/hello/", },
107+
{"C:\\hello\\..", "/hello/..", },
108+
{"C:\\hello\\..\\world", "/hello/../world", },
109+
{"C:\\hello\\..\\world\\", "/hello/../world/", },
110+
{"C:\\hello\\..\\world\\foo", "/hello/../world/foo", },
111+
} {
112+
p := data[0]
113+
expected := data[1]
114+
t.Run(p, func(t *testing.T) {
115+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
116+
require.NoError(t, path.Resolve(path.MustNewWindowsParser(p), scopewalker1))
117+
require.Equal(t, expected, builder1.GetUNIXString())
118+
119+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
120+
require.NoError(t, path.Resolve(builder1, scopewalker2))
121+
require.Equal(t, expected, builder2.GetUNIXString())
122+
})
123+
}
124+
})
125+
45126
// The following paths can be normalized, even when making no
46127
// assumptions about the layout of the underlying file system.
47128
t.Run("Normalized", func(t *testing.T) {

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(), OnRelative() 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()
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 has a 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, error)
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, error) {
50+
if t == nil {
51+
return ".", nil
52+
}
53+
var sb strings.Builder
54+
t.writeToStringBuilder(&sb)
55+
return sb.String(), nil
56+
}

0 commit comments

Comments
 (0)