From bacf8f0a4e3967b877d14c4e5051ff96a61c966e Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 30 Jul 2024 13:42:02 +0800 Subject: [PATCH] fix: add missing operation to fs path error (#799) When opening a folder as OCI image layout and there is no `oci-layout` file exists, the error would be > failed to open OCI layout file: oci-layout: file does not exist There is a duplicated space in the error string, because the `Op` field is missing in the returned `fs.PathError`. With fix in this PR, the return error would be > failed to open OCI layout file: open oci-layout: file does not exist Fixes #640 Signed-off-by: Billy Zha --- internal/fs/tarfs/tarfs.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/fs/tarfs/tarfs.go b/internal/fs/tarfs/tarfs.go index a7a0a6c1..ecaa6bdd 100644 --- a/internal/fs/tarfs/tarfs.go +++ b/internal/fs/tarfs/tarfs.go @@ -67,7 +67,7 @@ func New(path string) (*TarFS, error) { // ValidPath(name), returning a *PathError with Err set to // ErrInvalid or ErrNotExist. func (tfs *TarFS) Open(name string) (file fs.File, openErr error) { - entry, err := tfs.getEntry(name) + entry, err := tfs.getEntry("open", name) if err != nil { return nil, err } @@ -98,7 +98,7 @@ func (tfs *TarFS) Open(name string) (file fs.File, openErr error) { // Stat returns a FileInfo describing the file. // If there is an error, it should be of type *PathError. func (tfs *TarFS) Stat(name string) (fs.FileInfo, error) { - entry, err := tfs.getEntry(name) + entry, err := tfs.getEntry("stat", name) if err != nil { return nil, err } @@ -106,18 +106,18 @@ func (tfs *TarFS) Stat(name string) (fs.FileInfo, error) { } // getEntry returns the named entry. -func (tfs *TarFS) getEntry(name string) (*entry, error) { - if !fs.ValidPath(name) { - return nil, &fs.PathError{Path: name, Err: fs.ErrInvalid} +func (tfs *TarFS) getEntry(operation string, path string) (*entry, error) { + if !fs.ValidPath(path) { + return nil, &fs.PathError{Op: operation, Path: path, Err: fs.ErrInvalid} } - entry, ok := tfs.entries[name] + entry, ok := tfs.entries[path] if !ok { - return nil, &fs.PathError{Path: name, Err: fs.ErrNotExist} + return nil, &fs.PathError{Op: operation, Path: path, Err: fs.ErrNotExist} } if entry.header.Typeflag != tar.TypeReg { // support regular files only return nil, fmt.Errorf("%s: type flag %c is not supported: %w", - name, entry.header.Typeflag, errdef.ErrUnsupported) + path, entry.header.Typeflag, errdef.ErrUnsupported) } return entry, nil }