Skip to content

Commit 304b6ec

Browse files
committed
ops: improve error messages from fileop
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 2f8ab30 commit 304b6ec

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

solver/llbsolver/file/backend.go

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ func timestampToTime(ts int64) *time.Time {
2727
return &tm
2828
}
2929

30-
func mkdir(d string, action pb.FileActionMkDir, user *copy.User, idmap *idtools.IdentityMapping) error {
30+
func mkdir(d string, action pb.FileActionMkDir, user *copy.User, idmap *idtools.IdentityMapping) (err error) {
31+
defer func() {
32+
var osErr *os.PathError
33+
if errors.As(err, &osErr) {
34+
osErr.Path = strings.TrimPrefix(osErr.Path, d)
35+
}
36+
}()
37+
3138
p, err := fs.RootPath(d, action.Path)
3239
if err != nil {
33-
return err
40+
return errors.WithStack(err)
3441
}
3542

3643
ch, err := mapUserToChowner(user, idmap)
@@ -47,23 +54,31 @@ func mkdir(d string, action pb.FileActionMkDir, user *copy.User, idmap *idtools.
4754
if errors.Is(err, os.ErrExist) {
4855
return nil
4956
}
50-
return err
57+
return errors.WithStack(err)
5158
}
5259
if err := copy.Chown(p, nil, ch); err != nil {
53-
return err
60+
return errors.WithStack(err)
5461
}
5562
if err := copy.Utimes(p, timestampToTime(action.Timestamp)); err != nil {
56-
return err
63+
return errors.WithStack(err)
5764
}
5865
}
5966

6067
return nil
6168
}
6269

63-
func mkfile(d string, action pb.FileActionMkFile, user *copy.User, idmap *idtools.IdentityMapping) error {
70+
func mkfile(d string, action pb.FileActionMkFile, user *copy.User, idmap *idtools.IdentityMapping) (err error) {
71+
defer func() {
72+
var osErr *os.PathError
73+
if errors.As(err, &osErr) {
74+
// remove system root from error path if present
75+
osErr.Path = strings.TrimPrefix(osErr.Path, d)
76+
}
77+
}()
78+
6479
p, err := fs.RootPath(d, filepath.Join("/", action.Path))
6580
if err != nil {
66-
return err
81+
return errors.WithStack(err)
6782
}
6883

6984
ch, err := mapUserToChowner(user, idmap)
@@ -72,29 +87,37 @@ func mkfile(d string, action pb.FileActionMkFile, user *copy.User, idmap *idtool
7287
}
7388

7489
if err := os.WriteFile(p, action.Data, os.FileMode(action.Mode)&0777); err != nil {
75-
return err
90+
return errors.WithStack(err)
7691
}
7792

7893
if err := copy.Chown(p, nil, ch); err != nil {
79-
return err
94+
return errors.WithStack(err)
8095
}
8196

8297
if err := copy.Utimes(p, timestampToTime(action.Timestamp)); err != nil {
83-
return err
98+
return errors.WithStack(err)
8499
}
85100

86101
return nil
87102
}
88103

89-
func rm(d string, action pb.FileActionRm) error {
104+
func rm(d string, action pb.FileActionRm) (err error) {
105+
defer func() {
106+
var osErr *os.PathError
107+
if errors.As(err, &osErr) {
108+
// remove system root from error path if present
109+
osErr.Path = strings.TrimPrefix(osErr.Path, d)
110+
}
111+
}()
112+
90113
if action.AllowWildcard {
91114
src, err := cleanPath(action.Path)
92115
if err != nil {
93116
return errors.Wrap(err, "cleaning path")
94117
}
95118
m, err := copy.ResolveWildcards(d, src, false)
96119
if err != nil {
97-
return err
120+
return errors.WithStack(err)
98121
}
99122

100123
for _, s := range m {
@@ -117,22 +140,22 @@ func rmPath(root, src string, allowNotFound bool) error {
117140
}
118141
dir, err := fs.RootPath(root, filepath.Join("/", dir))
119142
if err != nil {
120-
return err
143+
return errors.WithStack(err)
121144
}
122145
p := filepath.Join(dir, base)
123146

124147
if !allowNotFound {
125148
_, err := os.Stat(p)
126149

127150
if errors.Is(err, os.ErrNotExist) {
128-
return err
151+
return errors.WithStack(err)
129152
}
130153
}
131154

132-
return os.RemoveAll(p)
155+
return errors.WithStack(os.RemoveAll(p))
133156
}
134157

135-
func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *copy.User, idmap *idtools.IdentityMapping) error {
158+
func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *copy.User, idmap *idtools.IdentityMapping) (err error) {
136159
srcPath, err := cleanPath(action.Src)
137160
if err != nil {
138161
return errors.Wrap(err, "cleaning source path")
@@ -144,7 +167,7 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
144167
if !action.CreateDestPath {
145168
p, err := fs.RootPath(dest, filepath.Join("/", action.Dest))
146169
if err != nil {
147-
return err
170+
return errors.WithStack(err)
148171
}
149172
if _, err := os.Lstat(filepath.Dir(p)); err != nil {
150173
return errors.Wrapf(err, "failed to stat %s", action.Dest)
@@ -177,14 +200,23 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
177200
copy.WithXAttrErrorHandler(xattrErrorHandler),
178201
}
179202

203+
defer func() {
204+
var osErr *os.PathError
205+
if errors.As(err, &osErr) {
206+
// remove system root from error path if present
207+
osErr.Path = strings.TrimPrefix(osErr.Path, src)
208+
osErr.Path = strings.TrimPrefix(osErr.Path, dest)
209+
}
210+
}()
211+
180212
var m []string
181213
if !action.AllowWildcard {
182214
m = []string{srcPath}
183215
} else {
184216
var err error
185217
m, err = copy.ResolveWildcards(src, srcPath, action.FollowSymlink)
186218
if err != nil {
187-
return err
219+
return errors.WithStack(err)
188220
}
189221

190222
if len(m) == 0 {
@@ -198,13 +230,13 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
198230
for _, s := range m {
199231
if action.AttemptUnpackDockerCompatibility {
200232
if ok, err := unpack(src, s, dest, destPath, ch, timestampToTime(action.Timestamp), idmap); err != nil {
201-
return err
233+
return errors.WithStack(err)
202234
} else if ok {
203235
continue
204236
}
205237
}
206238
if err := copy.Copy(ctx, src, s, dest, destPath, opt...); err != nil {
207-
return err
239+
return errors.WithStack(err)
208240
}
209241
}
210242

solver/llbsolver/file/backend_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"path/filepath"
66
"testing"
77

8+
"github.com/pkg/errors"
89
"github.com/stretchr/testify/require"
910
)
1011

1112
func TestRmPathNonExistentFileAllowNotFoundFalse(t *testing.T) {
1213
root := t.TempDir()
1314
err := rmPath(root, "doesnt_exist", false)
1415
require.Error(t, err)
15-
require.True(t, os.IsNotExist(err))
16+
require.True(t, errors.Is(err, os.ErrNotExist))
1617
}
1718

1819
func TestRmPathNonExistentFileAllowNotFoundTrue(t *testing.T) {

solver/llbsolver/file/backend_unix.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package file
55

66
import (
77
"github.com/docker/docker/pkg/idtools"
8+
"github.com/pkg/errors"
89
copy "github.com/tonistiigi/fsutil/copy"
910
)
1011

@@ -23,7 +24,7 @@ func mapUserToChowner(user *copy.User, idmap *idtools.IdentityMapping) (copy.Cho
2324
GID: old.GID,
2425
})
2526
if err != nil {
26-
return nil, err
27+
return nil, errors.WithStack(err)
2728
}
2829
return &copy.User{UID: identity.UID, GID: identity.GID}, nil
2930
}
@@ -38,7 +39,7 @@ func mapUserToChowner(user *copy.User, idmap *idtools.IdentityMapping) (copy.Cho
3839
GID: user.GID,
3940
})
4041
if err != nil {
41-
return nil, err
42+
return nil, errors.WithStack(err)
4243
}
4344
u.UID = identity.UID
4445
u.GID = identity.GID

solver/llbsolver/ops/file.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (f *fileOp) Exec(ctx context.Context, g session.Group, inputs []solver.Resu
169169

170170
backend, err := file.NewFileOpBackend(getReadUserFn(f.w))
171171
if err != nil {
172-
return nil, err
172+
return nil, errors.WithStack(err)
173173
}
174174

175175
fs := NewFileOpSolver(f.w, backend, f.refManager)
@@ -474,7 +474,7 @@ func (s *FileOpSolver) getInput(ctx context.Context, idx int, inputs []fileoptyp
474474
if inp.ref != nil {
475475
m, err := s.r.Prepare(ctx, inp.ref, false, g)
476476
if err != nil {
477-
return err
477+
return errors.WithStack(err)
478478
}
479479
inpMount = m
480480
return nil
@@ -493,7 +493,7 @@ func (s *FileOpSolver) getInput(ctx context.Context, idx int, inputs []fileoptyp
493493
if inp.ref != nil {
494494
m, err := s.r.Prepare(ctx, inp.ref, true, g)
495495
if err != nil {
496-
return err
496+
return errors.WithStack(err)
497497
}
498498
inpMountSecondary = m
499499
toRelease = append(toRelease, m)
@@ -521,7 +521,7 @@ func (s *FileOpSolver) getInput(ctx context.Context, idx int, inputs []fileoptyp
521521
if inp.ref != nil {
522522
mm, err := s.r.Prepare(ctx, inp.ref, true, g)
523523
if err != nil {
524-
return nil, err
524+
return nil, errors.WithStack(err)
525525
}
526526
toRelease = append(toRelease, mm)
527527
m = mm
@@ -572,7 +572,7 @@ func (s *FileOpSolver) getInput(ctx context.Context, idx int, inputs []fileoptyp
572572
if inpMount == nil {
573573
m, err := s.r.Prepare(ctx, nil, false, g)
574574
if err != nil {
575-
return input{}, err
575+
return input{}, errors.WithStack(err)
576576
}
577577
inpMount = m
578578
}

0 commit comments

Comments
 (0)