Skip to content

Commit

Permalink
gateway: restore original filename in ReadFile error message
Browse files Browse the repository at this point in the history
All messages returned by os.Open are guaranteed to return a PathError.
However, as these error messages are printed, they include the temporary
directory for the mounted reference which is not useful to the caller.

On an error, we can restore the filename in the PathError to the
requested filename, as also seen in os.DirFS.

Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed Jul 5, 2023
1 parent 66a4b94 commit 38040ae
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cache/util/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,22 @@ func ReadFile(ctx context.Context, mount snapshot.Mountable, req ReadRequest) ([
return errors.WithStack(err)
}

f, err := os.Open(fp)
if err != nil {
// The filename here is internal to the mount, so we can restore
// the request base path for error reporting.
// See os.DirFS.Open for details.
err.(*os.PathError).Path = req.Filename
return errors.WithStack(err)
}

if req.Range == nil {
dt, err = os.ReadFile(fp)
dt, err = io.ReadAll(f)
f.Close()
if err != nil {
return errors.WithStack(err)
}
} else {
f, err := os.Open(fp)
if err != nil {
return errors.WithStack(err)
}
dt, err = io.ReadAll(io.NewSectionReader(f, int64(req.Range.Offset), int64(req.Range.Length)))
f.Close()
if err != nil {
Expand Down

0 comments on commit 38040ae

Please sign in to comment.