Skip to content

Commit 38040ae

Browse files
committed
gateway: restore original filename in ReadFile error message
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]>
1 parent 66a4b94 commit 38040ae

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

cache/util/fsutil.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,22 @@ func ReadFile(ctx context.Context, mount snapshot.Mountable, req ReadRequest) ([
5757
return errors.WithStack(err)
5858
}
5959

60+
f, err := os.Open(fp)
61+
if err != nil {
62+
// The filename here is internal to the mount, so we can restore
63+
// the request base path for error reporting.
64+
// See os.DirFS.Open for details.
65+
err.(*os.PathError).Path = req.Filename
66+
return errors.WithStack(err)
67+
}
68+
6069
if req.Range == nil {
61-
dt, err = os.ReadFile(fp)
70+
dt, err = io.ReadAll(f)
71+
f.Close()
6272
if err != nil {
6373
return errors.WithStack(err)
6474
}
6575
} else {
66-
f, err := os.Open(fp)
67-
if err != nil {
68-
return errors.WithStack(err)
69-
}
7076
dt, err = io.ReadAll(io.NewSectionReader(f, int64(req.Range.Offset), int64(req.Range.Length)))
7177
f.Close()
7278
if err != nil {

0 commit comments

Comments
 (0)