Skip to content

Commit

Permalink
Create NewLocalDirectory with path.Parser
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 0f210f0 commit 5ac9f82
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/blobstore/configuration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ go_library(
"//pkg/digest",
"//pkg/eviction",
"//pkg/filesystem",
"//pkg/filesystem/path",
"//pkg/grpc",
"//pkg/http",
"//pkg/program",
Expand Down
3 changes: 2 additions & 1 deletion pkg/blobstore/configuration/new_blob_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/buildbarn/bb-storage/pkg/digest"
"github.com/buildbarn/bb-storage/pkg/eviction"
"github.com/buildbarn/bb-storage/pkg/filesystem"
"github.com/buildbarn/bb-storage/pkg/filesystem/path"
"github.com/buildbarn/bb-storage/pkg/grpc"
"github.com/buildbarn/bb-storage/pkg/program"
pb "github.com/buildbarn/bb-storage/pkg/proto/configuration/blobstore"
Expand Down Expand Up @@ -217,7 +218,7 @@ func (nc *simpleNestedBlobAccessCreator) newNestedBlobAccessBare(configuration *
} else {
// Persistency is enabled. Reload previous
// persistent state from disk.
persistentStateDirectory, err := filesystem.NewLocalDirectory(persistent.StateDirectoryPath)
persistentStateDirectory, err := filesystem.NewLocalDirectory(path.NewLocalParser(persistent.StateDirectoryPath))
if err != nil {
return BlobAccessInfo{}, "", util.StatusWrapf(err, "Failed to open persistent state directory %#v", persistent.StateDirectoryPath)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/filesystem/local_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

func openTmpDir(t *testing.T) filesystem.DirectoryCloser {
d, err := filesystem.NewLocalDirectory(t.TempDir())
d, err := filesystem.NewLocalDirectory(path.NewLocalParser(t.TempDir()))
require.NoError(t, err)
return d
}

func TestLocalDirectoryCreationFailure(t *testing.T) {
_, err := filesystem.NewLocalDirectory("/nonexistent")
_, err := filesystem.NewLocalDirectory(path.NewUNIXParser("/nonexistent"))
require.True(t, os.IsNotExist(err))
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/filesystem/local_directory_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ func newLocalDirectoryFromFileDescriptor(fd int) (*localDirectory, error) {

// NewLocalDirectory creates a directory handle that corresponds to a
// local path on the system.
func NewLocalDirectory(path string) (DirectoryCloser, error) {
fd, err := unix.Openat(unix.AT_FDCWD, path, unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_RDONLY, 0)
func NewLocalDirectory(directoryParser path.Parser) (DirectoryCloser, error) {
directoryPath, scopeWalker := path.EmptyBuilder.Join(path.VoidScopeWalker)
if err := path.Resolve(directoryParser, scopeWalker); err != nil {
return nil, util.StatusWrap(err, "Failed to resolve directory")
}
pathString, err := path.GetLocalString(directoryPath)
if err != nil {
return nil, util.StatusWrap(err, "Failed to create local representation of build directory")
}

fd, err := unix.Openat(unix.AT_FDCWD, pathString, unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_RDONLY, 0)
if err != nil {
return nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/filesystem/local_directory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,16 @@ func newLocalDirectory(absPath string, openReparsePoint bool) (DirectoryCloser,
return newLocalDirectoryFromHandle(handle)
}

func NewLocalDirectory(path string) (DirectoryCloser, error) {
absPath := "\\??\\" + path
func NewLocalDirectory(directoryParser path.Parser) (DirectoryCloser, error) {
directoryPath, scopeWalker := path.EmptyBuilder.Join(path.VoidScopeWalker)
if err := path.Resolve(directoryParser, scopeWalker); err != nil {
return nil, util.StatusWrap(err, "Failed to resolve directory")
}
pathString, err := path.GetLocalString(directoryPath)
if err != nil {
return nil, util.StatusWrap(err, "Failed to create local representation of build directory")
}
absPath := "\\??\\" + pathString
return newLocalDirectory(absPath, true)
}

Expand Down

0 comments on commit 5ac9f82

Please sign in to comment.