Include filesystem rootdirectory in PathNotFound errors (#5007)

This commit is contained in:
Milos Gajdos authored and GitHub committed 2026-10-11 17:18:39 -07:00
commit be78ef9417
4 files changed
+44 -11

No files matched your search

+5 -5
View File
@@ -192,7 +192,7 @@ func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.Read
file, err := os.OpenFile(d.fullPath(path), os.O_RDONLY, 0o644)
if err != nil {
if os.IsNotExist(err) {
return nil, storagedriver.PathNotFoundError{Path: path}
return nil, storagedriver.PathNotFoundError{Path: path, RootDirectory: d.rootDirectory}
}
return nil, err
@@ -250,7 +250,7 @@ func (d *driver) Stat(ctx context.Context, subPath string) (storagedriver.FileIn
fi, err := os.Stat(fullPath)
if err != nil {
if os.IsNotExist(err) {
return nil, storagedriver.PathNotFoundError{Path: subPath}
return nil, storagedriver.PathNotFoundError{Path: subPath, RootDirectory: d.rootDirectory}
}
return nil, err
@@ -270,7 +270,7 @@ func (d *driver) List(ctx context.Context, subPath string) ([]string, error) {
dir, err := os.Open(fullPath)
if err != nil {
if os.IsNotExist(err) {
return nil, storagedriver.PathNotFoundError{Path: subPath}
return nil, storagedriver.PathNotFoundError{Path: subPath, RootDirectory: d.rootDirectory}
}
return nil, err
}
@@ -297,7 +297,7 @@ func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) e
dest := d.fullPath(destPath)
if _, err := os.Stat(source); os.IsNotExist(err) {
return storagedriver.PathNotFoundError{Path: sourcePath}
return storagedriver.PathNotFoundError{Path: sourcePath, RootDirectory: d.rootDirectory}
}
if err := os.MkdirAll(filepath.Dir(dest), 0o777); err != nil {
@@ -316,7 +316,7 @@ func (d *driver) Delete(ctx context.Context, subPath string) error {
if err != nil && !os.IsNotExist(err) {
return err
} else if err != nil {
return storagedriver.PathNotFoundError{Path: subPath}
return storagedriver.PathNotFoundError{Path: subPath, RootDirectory: d.rootDirectory}
}
err = os.RemoveAll(fullPath)
@@ -1,7 +1,9 @@
package filesystem
import (
"context"
"reflect"
"strings"
"testing"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
@@ -22,6 +24,33 @@ func TestFilesystemDriverSuite(t *testing.T) {
testsuites.Driver(t, newDriverConstructor(t), false)
}
func TestPathNotFoundErrorIncludesRootDirectory(t *testing.T) {
root := t.TempDir()
d := New(DriverParameters{
RootDirectory: root,
MaxThreads: minThreads,
})
missingPath := "/docker/registry/v2/blobs"
_, err := d.Stat(context.Background(), missingPath)
if err == nil {
t.Fatal("expected error stating missing path")
}
pathNotFound, ok := err.(storagedriver.PathNotFoundError)
if !ok {
t.Fatalf("expected PathNotFoundError, got %T: %v", err, err)
}
errStr := pathNotFound.Error()
if !strings.Contains(errStr, root) {
t.Fatalf("expected error to contain rootdirectory %q, got %q", root, errStr)
}
if !strings.Contains(errStr, missingPath) {
t.Fatalf("expected error to contain storage path %q, got %q", missingPath, errStr)
}
}
func BenchmarkFilesystemDriverSuite(b *testing.B) {
testsuites.BenchDriver(b, newDriverConstructor(b))
}
+6 -2
View File
@@ -149,11 +149,15 @@ func (err ErrUnsupportedMethod) Error() string {
// PathNotFoundError is returned when operating on a nonexistent path.
type PathNotFoundError struct {
Path string
DriverName string
Path string
DriverName string
RootDirectory string
}
func (err PathNotFoundError) Error() string {
if err.RootDirectory != "" {
return fmt.Sprintf("%s: Path not found: %s (relative to rootdirectory %s)", err.DriverName, err.Path, err.RootDirectory)
}
return fmt.Sprintf("%s: Path not found: %s", err.DriverName, err.Path)
}
@@ -578,10 +578,10 @@ func (suite *DriverSuite) TestList() {
doesnotexist := path.Join(rootDirectory, "nonexistent")
_, err := suite.StorageDriver.List(suite.ctx, doesnotexist)
suite.Require().Equal(err, storagedriver.PathNotFoundError{
Path: doesnotexist,
DriverName: suite.StorageDriver.Name(),
})
var pathNotFoundErr storagedriver.PathNotFoundError
suite.Require().ErrorAs(err, &pathNotFoundErr)
suite.Require().Equal(doesnotexist, pathNotFoundErr.Path)
suite.Require().Equal(suite.StorageDriver.Name(), pathNotFoundErr.DriverName)
parentDirectory := rootDirectory + "/" + randomFilename(int64(8+rand.Intn(8)))
childFiles := make([]string, 50)