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 Nils Wireklint committed Jun 18, 2024
1 parent bb96fcb commit b60a04d
Show file tree
Hide file tree
Showing 5 changed files with 34 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
7 changes: 6 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,11 @@ func (nc *simpleNestedBlobAccessCreator) newNestedBlobAccessBare(configuration *
} else {
// Persistency is enabled. Reload previous
// persistent state from disk.
persistentStateDirectory, err := filesystem.NewLocalDirectory(persistent.StateDirectoryPath)
parser, err := path.NewLocalParser(persistent.StateDirectoryPath)
if err != nil {
return BlobAccessInfo{}, "", util.StatusWrapf(err, "Failed to parse persistent state directory path %#v", persistent.StateDirectoryPath)
}
persistentStateDirectory, err := filesystem.NewLocalDirectory(parser)
if err != nil {
return BlobAccessInfo{}, "", util.StatusWrapf(err, "Failed to open persistent state directory %#v", persistent.StateDirectoryPath)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/filesystem/local_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (
)

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

func TestLocalDirectoryCreationFailure(t *testing.T) {
_, err := filesystem.NewLocalDirectory("/nonexistent")
parser, err := path.NewUNIXParser("/nonexistent")
require.NoError(t, err)
_, err = filesystem.NewLocalDirectory(parser)
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 b60a04d

Please sign in to comment.