Skip to content

Commit

Permalink
refactor: move operation to the caller side
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Jul 26, 2024
1 parent a27d9d8 commit f3a6d39
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions internal/fs/tarfs/tarfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -98,26 +98,26 @@ 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
}
return entry.header.FileInfo(), nil
}

// getEntry returns the named entry.
func (tfs *TarFS) getEntry(name string) (*entry, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", 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{Op: "open", 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
}
Expand Down

0 comments on commit f3a6d39

Please sign in to comment.