Skip to content

Commit 998e0ba

Browse files
Nils WireklintEdSchouten
Nils Wireklint
authored andcommitted
Implement Windows drive letter support
1 parent 7b63970 commit 998e0ba

18 files changed

+476
-32
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

+67-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package path
22

33
import (
44
"strings"
5+
6+
"github.com/buildbarn/bb-storage/pkg/util"
57
)
68

79
// Builder for normalized pathname strings.
@@ -20,6 +22,7 @@ import (
2022
// system.
2123
type Builder struct {
2224
absolute bool
25+
driveLetter rune
2326
components []string
2427
firstReversibleIndex int
2528
suffix string
@@ -45,6 +48,15 @@ var RootBuilder = Builder{
4548
suffix: "/",
4649
}
4750

51+
// NewDriveLetterBuilder returns a builder rooted at a Windows drive.
52+
func NewDriveLetterBuilder(drive rune) Builder {
53+
return Builder{
54+
absolute: false,
55+
driveLetter: drive,
56+
suffix: "/",
57+
}
58+
}
59+
4860
// GetUNIXString returns a string representation of the path for use on
4961
// UNIX-like operating systems.
5062
func (b *Builder) GetUNIXString() string {
@@ -66,10 +78,47 @@ func (b *Builder) GetUNIXString() string {
6678
return out.String()
6779
}
6880

81+
// GetWindowsString returns a string representation of the path for use on
82+
// Windows.
83+
func (b *Builder) GetWindowsString() (string, error) {
84+
// Emit pathname components.
85+
var out strings.Builder
86+
prefix := ""
87+
if b.driveLetter != 0 {
88+
out.WriteString(string(b.driveLetter))
89+
out.WriteString(":")
90+
prefix = "\\"
91+
} else if b.absolute {
92+
prefix = "\\"
93+
}
94+
95+
for _, component := range b.components {
96+
if err := validateWindowsComponent(component); err != nil {
97+
return "", util.StatusWrapf(err, "Invalid pathname component %#v", component)
98+
}
99+
100+
out.WriteString(prefix)
101+
out.WriteString(component)
102+
prefix = "\\"
103+
}
104+
105+
// Emit trailing slash in case the path refers to a directory,
106+
// or a dot or slash if the path is empty. The suffix is been
107+
// constructed by platform-independent code that uses forward
108+
// slashes. To construct a Windows path we must use a
109+
// backslash.
110+
suffix := b.suffix
111+
if suffix == "/" {
112+
suffix = "\\"
113+
}
114+
out.WriteString(suffix)
115+
return out.String(), nil
116+
}
117+
69118
func (b *Builder) addTrailingSlash() {
70119
if len(b.components) == 0 {
71120
// An empty path. Ensure we either emit a "/" or ".",
72-
// depending on whether the path is absolute.
121+
// depending on whether the path is absolute/drive letter.
73122
if b.absolute {
74123
b.suffix = "/"
75124
} else {
@@ -104,7 +153,9 @@ func (b *Builder) getComponentWalker(base ComponentWalker) ComponentWalker {
104153
// directly to Resolve(). This can be used to replay resolution of a
105154
// previously constructed path.
106155
func (b *Builder) ParseScope(scopeWalker ScopeWalker) (next ComponentWalker, remainder RelativeParser, err error) {
107-
if b.absolute {
156+
if b.driveLetter != 0 {
157+
next, err = scopeWalker.OnDriveLetter(b.driveLetter)
158+
} else if b.absolute {
108159
next, err = scopeWalker.OnAbsolute()
109160
} else {
110161
next, err = scopeWalker.OnRelative()
@@ -154,6 +205,20 @@ func (w *buildingScopeWalker) OnAbsolute() (ComponentWalker, error) {
154205
return w.b.getComponentWalker(componentWalker), nil
155206
}
156207

208+
func (w *buildingScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
209+
componentWalker, err := w.base.OnDriveLetter(drive)
210+
if err != nil {
211+
return nil, err
212+
}
213+
*w.b = Builder{
214+
absolute: true,
215+
driveLetter: drive,
216+
components: w.b.components[:0],
217+
suffix: "/",
218+
}
219+
return w.b.getComponentWalker(componentWalker), nil
220+
}
221+
157222
func (w *buildingScopeWalker) OnRelative() (ComponentWalker, error) {
158223
componentWalker, err := w.base.OnRelative()
159224
if err != nil {

pkg/filesystem/path/builder_test.go

+150-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12+
func mustGetWindowsString(p path.Stringer) string {
13+
s, err := p.GetWindowsString()
14+
if err != nil {
15+
panic(err)
16+
}
17+
return s
18+
}
19+
1220
func TestBuilder(t *testing.T) {
1321
ctrl := gomock.NewController(t)
1422

1523
// The following paths should remain completely identical when
1624
// resolved without making any assumptions about the layout of
1725
// the underlying file system. ".." elements should not be
1826
// removed from paths.
19-
t.Run("Identity", func(t *testing.T) {
27+
t.Run("UNIXIdentity", func(t *testing.T) {
2028
for _, p := range []string{
2129
".",
2230
"..",
@@ -42,9 +50,122 @@ func TestBuilder(t *testing.T) {
4250
}
4351
})
4452

53+
t.Run("WindowsParseUNIXPaths", func(t *testing.T) {
54+
for _, data := range [][]string{
55+
{".", "."},
56+
{"..", ".."},
57+
{"/", "\\"},
58+
{"hello", "hello"},
59+
{"hello/", "hello\\"},
60+
{"hello/..", "hello\\.."},
61+
{"/hello/", "\\hello\\"},
62+
{"/hello/..", "\\hello\\.."},
63+
{"/hello/../world", "\\hello\\..\\world"},
64+
{"/hello/../world/", "\\hello\\..\\world\\"},
65+
{"/hello/../world/foo", "\\hello\\..\\world\\foo"},
66+
} {
67+
p := data[0]
68+
expected := data[1]
69+
t.Run(p, func(t *testing.T) {
70+
// Windows Parser, compare Windows and UNIX string identity.
71+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
72+
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
73+
require.Equal(t, expected, mustGetWindowsString(builder1))
74+
require.Equal(t, p, builder1.GetUNIXString())
75+
76+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
77+
require.NoError(t, path.Resolve(builder1, scopewalker2))
78+
require.Equal(t, expected, mustGetWindowsString(builder2))
79+
require.Equal(t, p, builder2.GetUNIXString())
80+
})
81+
}
82+
})
83+
84+
t.Run("WindowsIdentity", func(t *testing.T) {
85+
for _, p := range []string{
86+
"C:\\",
87+
"C:\\hello\\",
88+
"C:\\hello\\..",
89+
"C:\\hello\\..\\world",
90+
"C:\\hello\\..\\world\\",
91+
"C:\\hello\\..\\world\\foo",
92+
"C:\\hello\\..\\world\\foo",
93+
} {
94+
t.Run(p, func(t *testing.T) {
95+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
96+
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
97+
require.Equal(t, p, mustGetWindowsString(builder1))
98+
99+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
100+
require.NoError(t, path.Resolve(builder1, scopewalker2))
101+
require.Equal(t, p, mustGetWindowsString(builder2))
102+
})
103+
}
104+
})
105+
106+
t.Run("WindowsParseAndWriteUNIXPaths", func(t *testing.T) {
107+
for _, data := range [][]string{
108+
{"C:\\", "/"},
109+
{"C:\\.", "/"},
110+
{"C:\\hello\\", "/hello/"},
111+
{"C:\\hello\\.", "/hello/"},
112+
{"C:\\hello\\..", "/hello/.."},
113+
{"C:\\hello\\.\\world", "/hello/world"},
114+
{"C:\\hello\\..\\world", "/hello/../world"},
115+
{"C:\\hello\\..\\world\\", "/hello/../world/"},
116+
{"C:\\hello\\..\\world\\foo", "/hello/../world/foo"},
117+
{"C:\\hello\\\\..\\world\\foo", "/hello/../world/foo"},
118+
} {
119+
p := data[0]
120+
expected := data[1]
121+
t.Run(p, func(t *testing.T) {
122+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
123+
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
124+
require.Equal(t, expected, builder1.GetUNIXString())
125+
126+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
127+
require.NoError(t, path.Resolve(builder1, scopewalker2))
128+
require.Equal(t, expected, builder2.GetUNIXString())
129+
})
130+
}
131+
})
132+
133+
t.Run("WindowsParseCasing", func(t *testing.T) {
134+
for _, data := range [][]string{
135+
{"./bar", "bar"},
136+
{"./bar\\", "bar\\"},
137+
{"c:", "C:\\"},
138+
{"c:.", "C:\\"},
139+
{"c:Hello", "C:\\Hello"},
140+
{"c:\\", "C:\\"},
141+
{"c:\\.", "C:\\"},
142+
{"c:\\Hello\\", "C:\\Hello\\"},
143+
{"c:\\Hello\\.", "C:\\Hello\\"},
144+
{"c:\\Hello\\..", "C:\\Hello\\.."},
145+
{"c:\\Hello\\.\\world", "C:\\Hello\\world"},
146+
{"c:\\Hello\\..\\world", "C:\\Hello\\..\\world"},
147+
{"c:\\Hello\\..\\world", "C:\\Hello\\..\\world"},
148+
{"c:\\Hello\\..\\world\\", "C:\\Hello\\..\\world\\"},
149+
{"c:\\Hello\\..\\world\\foo", "C:\\Hello\\..\\world\\foo"},
150+
{"c:\\\\Hello\\\\..\\world\\foo", "C:\\Hello\\..\\world\\foo"},
151+
} {
152+
p := data[0]
153+
expected := data[1]
154+
t.Run(p, func(t *testing.T) {
155+
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
156+
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
157+
require.Equal(t, expected, mustGetWindowsString(builder1))
158+
159+
builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
160+
require.NoError(t, path.Resolve(builder1, scopewalker2))
161+
require.Equal(t, expected, mustGetWindowsString(builder2))
162+
})
163+
}
164+
})
165+
45166
// The following paths can be normalized, even when making no
46167
// assumptions about the layout of the underlying file system.
47-
t.Run("Normalized", func(t *testing.T) {
168+
t.Run("UNIXNormalized", func(t *testing.T) {
48169
for from, to := range map[string]string{
49170
"": ".",
50171
"./": ".",
@@ -71,6 +192,33 @@ func TestBuilder(t *testing.T) {
71192
}
72193
})
73194

195+
t.Run("WindowsNormalized", func(t *testing.T) {
196+
for from, to := range map[string]string{
197+
"": ".",
198+
"./": ".",
199+
"./.": ".",
200+
"../": "..",
201+
"../.": "..",
202+
"//": "\\",
203+
"/.": "\\",
204+
"/./": "\\",
205+
"/..": "\\",
206+
"/../": "\\",
207+
"/hello/.": "\\hello\\",
208+
"/hello/../.": "\\hello\\..",
209+
} {
210+
t.Run(from, func(t *testing.T) {
211+
builder1, scopeWalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
212+
require.NoError(t, path.Resolve(path.MustNewUNIXParser(from), scopeWalker1))
213+
require.Equal(t, to, mustGetWindowsString(builder1))
214+
215+
builder2, scopeWalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
216+
require.NoError(t, path.Resolve(builder1, scopeWalker2))
217+
require.Equal(t, to, mustGetWindowsString(builder2))
218+
})
219+
}
220+
})
221+
74222
// Paths generated by joining with RootBuilder should start the
75223
// resolution process at the root directory.
76224
t.Run("Root", func(t *testing.T) {

pkg/filesystem/path/component.go

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package path
22

33
import (
44
"strings"
5+
"unicode"
6+
7+
"google.golang.org/grpc/codes"
8+
"google.golang.org/grpc/status"
59
)
610

711
// Component of a pathname. This type is nothing more than a string that
@@ -33,3 +37,15 @@ func MustNewComponent(name string) Component {
3337
func (c Component) String() string {
3438
return c.name
3539
}
40+
41+
// validateWindowsComponent returns true if the provided pathname
42+
// component is valid for use on Windows.
43+
func validateWindowsComponent(component string) error {
44+
if strings.ContainsFunc(component, unicode.IsControl) {
45+
return status.Error(codes.InvalidArgument, "Pathname component contains control characters")
46+
}
47+
if strings.ContainsAny(component, "<>:\"/\\|?*") {
48+
return status.Error(codes.InvalidArgument, "Pathname component contains reserved characters")
49+
}
50+
return nil
51+
}

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)), nil
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 {

0 commit comments

Comments
 (0)