Skip to content

Commit

Permalink
Implement Windows drive letter support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Wireklint authored and EdSchouten committed Jun 21, 2024
1 parent 7b63970 commit 998e0ba
Show file tree
Hide file tree
Showing 18 changed files with 476 additions and 32 deletions.
6 changes: 1 addition & 5 deletions pkg/filesystem/local_directory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ func newLocalDirectory(absPath string, openReparsePoint bool) (DirectoryCloser,
}

func NewLocalDirectory(path string) (DirectoryCloser, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
absPath = "\\??\\" + absPath
absPath := "\\??\\" + path
return newLocalDirectory(absPath, true)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/filesystem/path/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ go_library(
"virtual_root_scope_walker_factory.go",
"void_component_walker.go",
"void_scope_walker.go",
"windows_parser.go",
],
importpath = "github.com/buildbarn/bb-storage/pkg/filesystem/path",
visibility = ["//visibility:public"],
Expand Down
4 changes: 4 additions & 0 deletions pkg/filesystem/path/absolute_scope_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func (pw *absoluteScopeWalker) OnRelative() (ComponentWalker, error) {
func (pw *absoluteScopeWalker) OnAbsolute() (ComponentWalker, error) {
return pw.componentWalker, nil
}

func (pw *absoluteScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
return pw.componentWalker, nil
}
69 changes: 67 additions & 2 deletions pkg/filesystem/path/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package path

import (
"strings"

"github.com/buildbarn/bb-storage/pkg/util"
)

// Builder for normalized pathname strings.
Expand All @@ -20,6 +22,7 @@ import (
// system.
type Builder struct {
absolute bool
driveLetter rune
components []string
firstReversibleIndex int
suffix string
Expand All @@ -45,6 +48,15 @@ var RootBuilder = Builder{
suffix: "/",
}

// NewDriveLetterBuilder returns a builder rooted at a Windows drive.
func NewDriveLetterBuilder(drive rune) Builder {
return Builder{
absolute: false,
driveLetter: drive,
suffix: "/",
}
}

// GetUNIXString returns a string representation of the path for use on
// UNIX-like operating systems.
func (b *Builder) GetUNIXString() string {
Expand All @@ -66,10 +78,47 @@ func (b *Builder) GetUNIXString() string {
return out.String()
}

// GetWindowsString returns a string representation of the path for use on
// Windows.
func (b *Builder) GetWindowsString() (string, error) {
// Emit pathname components.
var out strings.Builder
prefix := ""
if b.driveLetter != 0 {
out.WriteString(string(b.driveLetter))
out.WriteString(":")
prefix = "\\"
} else if b.absolute {
prefix = "\\"
}

for _, component := range b.components {
if err := validateWindowsComponent(component); err != nil {
return "", util.StatusWrapf(err, "Invalid pathname component %#v", component)
}

out.WriteString(prefix)
out.WriteString(component)
prefix = "\\"
}

// Emit trailing slash in case the path refers to a directory,
// or a dot or slash if the path is empty. The suffix is been
// constructed by platform-independent code that uses forward
// slashes. To construct a Windows path we must use a
// backslash.
suffix := b.suffix
if suffix == "/" {
suffix = "\\"
}
out.WriteString(suffix)
return out.String(), nil
}

func (b *Builder) addTrailingSlash() {
if len(b.components) == 0 {
// An empty path. Ensure we either emit a "/" or ".",
// depending on whether the path is absolute.
// depending on whether the path is absolute/drive letter.
if b.absolute {
b.suffix = "/"
} else {
Expand Down Expand Up @@ -104,7 +153,9 @@ func (b *Builder) getComponentWalker(base ComponentWalker) ComponentWalker {
// directly to Resolve(). This can be used to replay resolution of a
// previously constructed path.
func (b *Builder) ParseScope(scopeWalker ScopeWalker) (next ComponentWalker, remainder RelativeParser, err error) {
if b.absolute {
if b.driveLetter != 0 {
next, err = scopeWalker.OnDriveLetter(b.driveLetter)
} else if b.absolute {
next, err = scopeWalker.OnAbsolute()
} else {
next, err = scopeWalker.OnRelative()
Expand Down Expand Up @@ -154,6 +205,20 @@ func (w *buildingScopeWalker) OnAbsolute() (ComponentWalker, error) {
return w.b.getComponentWalker(componentWalker), nil
}

func (w *buildingScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
componentWalker, err := w.base.OnDriveLetter(drive)
if err != nil {
return nil, err
}
*w.b = Builder{
absolute: true,
driveLetter: drive,
components: w.b.components[:0],
suffix: "/",
}
return w.b.getComponentWalker(componentWalker), nil
}

func (w *buildingScopeWalker) OnRelative() (ComponentWalker, error) {
componentWalker, err := w.base.OnRelative()
if err != nil {
Expand Down
152 changes: 150 additions & 2 deletions pkg/filesystem/path/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import (
"github.com/stretchr/testify/require"
)

func mustGetWindowsString(p path.Stringer) string {
s, err := p.GetWindowsString()
if err != nil {
panic(err)
}
return s
}

func TestBuilder(t *testing.T) {
ctrl := gomock.NewController(t)

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

t.Run("WindowsParseUNIXPaths", func(t *testing.T) {
for _, data := range [][]string{
{".", "."},
{"..", ".."},
{"/", "\\"},
{"hello", "hello"},
{"hello/", "hello\\"},
{"hello/..", "hello\\.."},
{"/hello/", "\\hello\\"},
{"/hello/..", "\\hello\\.."},
{"/hello/../world", "\\hello\\..\\world"},
{"/hello/../world/", "\\hello\\..\\world\\"},
{"/hello/../world/foo", "\\hello\\..\\world\\foo"},
} {
p := data[0]
expected := data[1]
t.Run(p, func(t *testing.T) {
// Windows Parser, compare Windows and UNIX string identity.
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
require.Equal(t, expected, mustGetWindowsString(builder1))
require.Equal(t, p, builder1.GetUNIXString())

builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(builder1, scopewalker2))
require.Equal(t, expected, mustGetWindowsString(builder2))
require.Equal(t, p, builder2.GetUNIXString())
})
}
})

t.Run("WindowsIdentity", func(t *testing.T) {
for _, p := range []string{
"C:\\",
"C:\\hello\\",
"C:\\hello\\..",
"C:\\hello\\..\\world",
"C:\\hello\\..\\world\\",
"C:\\hello\\..\\world\\foo",
"C:\\hello\\..\\world\\foo",
} {
t.Run(p, func(t *testing.T) {
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
require.Equal(t, p, mustGetWindowsString(builder1))

builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(builder1, scopewalker2))
require.Equal(t, p, mustGetWindowsString(builder2))
})
}
})

t.Run("WindowsParseAndWriteUNIXPaths", func(t *testing.T) {
for _, data := range [][]string{
{"C:\\", "/"},
{"C:\\.", "/"},
{"C:\\hello\\", "/hello/"},
{"C:\\hello\\.", "/hello/"},
{"C:\\hello\\..", "/hello/.."},
{"C:\\hello\\.\\world", "/hello/world"},
{"C:\\hello\\..\\world", "/hello/../world"},
{"C:\\hello\\..\\world\\", "/hello/../world/"},
{"C:\\hello\\..\\world\\foo", "/hello/../world/foo"},
{"C:\\hello\\\\..\\world\\foo", "/hello/../world/foo"},
} {
p := data[0]
expected := data[1]
t.Run(p, func(t *testing.T) {
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
require.Equal(t, expected, builder1.GetUNIXString())

builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(builder1, scopewalker2))
require.Equal(t, expected, builder2.GetUNIXString())
})
}
})

t.Run("WindowsParseCasing", func(t *testing.T) {
for _, data := range [][]string{
{"./bar", "bar"},
{"./bar\\", "bar\\"},
{"c:", "C:\\"},
{"c:.", "C:\\"},
{"c:Hello", "C:\\Hello"},
{"c:\\", "C:\\"},
{"c:\\.", "C:\\"},
{"c:\\Hello\\", "C:\\Hello\\"},
{"c:\\Hello\\.", "C:\\Hello\\"},
{"c:\\Hello\\..", "C:\\Hello\\.."},
{"c:\\Hello\\.\\world", "C:\\Hello\\world"},
{"c:\\Hello\\..\\world", "C:\\Hello\\..\\world"},
{"c:\\Hello\\..\\world", "C:\\Hello\\..\\world"},
{"c:\\Hello\\..\\world\\", "C:\\Hello\\..\\world\\"},
{"c:\\Hello\\..\\world\\foo", "C:\\Hello\\..\\world\\foo"},
{"c:\\\\Hello\\\\..\\world\\foo", "C:\\Hello\\..\\world\\foo"},
} {
p := data[0]
expected := data[1]
t.Run(p, func(t *testing.T) {
builder1, scopewalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(path.NewWindowsParser(p), scopewalker1))
require.Equal(t, expected, mustGetWindowsString(builder1))

builder2, scopewalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(builder1, scopewalker2))
require.Equal(t, expected, mustGetWindowsString(builder2))
})
}
})

// The following paths can be normalized, even when making no
// assumptions about the layout of the underlying file system.
t.Run("Normalized", func(t *testing.T) {
t.Run("UNIXNormalized", func(t *testing.T) {
for from, to := range map[string]string{
"": ".",
"./": ".",
Expand All @@ -71,6 +192,33 @@ func TestBuilder(t *testing.T) {
}
})

t.Run("WindowsNormalized", func(t *testing.T) {
for from, to := range map[string]string{
"": ".",
"./": ".",
"./.": ".",
"../": "..",
"../.": "..",
"//": "\\",
"/.": "\\",
"/./": "\\",
"/..": "\\",
"/../": "\\",
"/hello/.": "\\hello\\",
"/hello/../.": "\\hello\\..",
} {
t.Run(from, func(t *testing.T) {
builder1, scopeWalker1 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(path.MustNewUNIXParser(from), scopeWalker1))
require.Equal(t, to, mustGetWindowsString(builder1))

builder2, scopeWalker2 := path.EmptyBuilder.Join(path.VoidScopeWalker)
require.NoError(t, path.Resolve(builder1, scopeWalker2))
require.Equal(t, to, mustGetWindowsString(builder2))
})
}
})

// Paths generated by joining with RootBuilder should start the
// resolution process at the root directory.
t.Run("Root", func(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/filesystem/path/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package path

import (
"strings"
"unicode"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// Component of a pathname. This type is nothing more than a string that
Expand Down Expand Up @@ -33,3 +37,15 @@ func MustNewComponent(name string) Component {
func (c Component) String() string {
return c.name
}

// validateWindowsComponent returns true if the provided pathname
// component is valid for use on Windows.
func validateWindowsComponent(component string) error {
if strings.ContainsFunc(component, unicode.IsControl) {
return status.Error(codes.InvalidArgument, "Pathname component contains control characters")
}
if strings.ContainsAny(component, "<>:\"/\\|?*") {
return status.Error(codes.InvalidArgument, "Pathname component contains reserved characters")
}
return nil
}
7 changes: 4 additions & 3 deletions pkg/filesystem/path/component_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ type ComponentWalker interface {
// If the pathname component refers to a symbolic link, this
// function will return a GotSymlink containing a ScopeWalker, which
// can be used to perform expansion of the symbolic link. The
// Resolve() function will call into OnAbsolute() or OnRelative() to
// signal whether resolution should continue at the root directory
// or at the directory that contained the symbolic link.
// Resolve() function will call into OnAbsolute(), OnRelative() or
// OnDriveLetter() to signal whether resolution should continue at
// the root directory or at the directory that contained the
// symbolic link.
OnDirectory(name Component) (GotDirectoryOrSymlink, error)

// OnTerminal is called for the potentially last pathname
Expand Down
6 changes: 2 additions & 4 deletions pkg/filesystem/path/local_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
// NewLocalParser creates a pathname parser for paths that are native to
// the locally running operating system.
func NewLocalParser(path string) (Parser, error) {
// TODO: Implement an actual Windows pathname parser.
return NewUNIXParser(filepath.ToSlash(path))
return NewWindowsParser(filepath.ToSlash(path)), nil
}

// GetLocalString converts a path to a string representation that is
// supported by the locally running operating system.
func GetLocalString(s Stringer) (string, error) {
// TODO: Implement an actual Windows pathname formatter.
return filepath.FromSlash(s.GetUNIXString()), nil
return s.GetWindowsString()
}
11 changes: 11 additions & 0 deletions pkg/filesystem/path/loop_detecting_scope_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func (w *loopDetectingScopeWalker) OnAbsolute() (ComponentWalker, error) {
}, nil
}

func (w *loopDetectingScopeWalker) OnDriveLetter(drive rune) (ComponentWalker, error) {
componentWalker, err := w.base.OnDriveLetter(drive)
if err != nil {
return nil, err
}
return &loopDetectingComponentWalker{
base: componentWalker,
symlinksLeft: w.symlinksLeft,
}, nil
}

func (w *loopDetectingScopeWalker) OnRelative() (ComponentWalker, error) {
componentWalker, err := w.base.OnRelative()
if err != nil {
Expand Down
Loading

0 comments on commit 998e0ba

Please sign in to comment.