@@ -27,10 +27,17 @@ func timestampToTime(ts int64) *time.Time {
27
27
return & tm
28
28
}
29
29
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
+
31
38
p , err := fs .RootPath (d , action .Path )
32
39
if err != nil {
33
- return err
40
+ return errors . WithStack ( err )
34
41
}
35
42
36
43
ch , err := mapUserToChowner (user , idmap )
@@ -47,23 +54,31 @@ func mkdir(d string, action pb.FileActionMkDir, user *copy.User, idmap *idtools.
47
54
if errors .Is (err , os .ErrExist ) {
48
55
return nil
49
56
}
50
- return err
57
+ return errors . WithStack ( err )
51
58
}
52
59
if err := copy .Chown (p , nil , ch ); err != nil {
53
- return err
60
+ return errors . WithStack ( err )
54
61
}
55
62
if err := copy .Utimes (p , timestampToTime (action .Timestamp )); err != nil {
56
- return err
63
+ return errors . WithStack ( err )
57
64
}
58
65
}
59
66
60
67
return nil
61
68
}
62
69
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
+
64
79
p , err := fs .RootPath (d , filepath .Join ("/" , action .Path ))
65
80
if err != nil {
66
- return err
81
+ return errors . WithStack ( err )
67
82
}
68
83
69
84
ch , err := mapUserToChowner (user , idmap )
@@ -72,29 +87,37 @@ func mkfile(d string, action pb.FileActionMkFile, user *copy.User, idmap *idtool
72
87
}
73
88
74
89
if err := os .WriteFile (p , action .Data , os .FileMode (action .Mode )& 0777 ); err != nil {
75
- return err
90
+ return errors . WithStack ( err )
76
91
}
77
92
78
93
if err := copy .Chown (p , nil , ch ); err != nil {
79
- return err
94
+ return errors . WithStack ( err )
80
95
}
81
96
82
97
if err := copy .Utimes (p , timestampToTime (action .Timestamp )); err != nil {
83
- return err
98
+ return errors . WithStack ( err )
84
99
}
85
100
86
101
return nil
87
102
}
88
103
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
+
90
113
if action .AllowWildcard {
91
114
src , err := cleanPath (action .Path )
92
115
if err != nil {
93
116
return errors .Wrap (err , "cleaning path" )
94
117
}
95
118
m , err := copy .ResolveWildcards (d , src , false )
96
119
if err != nil {
97
- return err
120
+ return errors . WithStack ( err )
98
121
}
99
122
100
123
for _ , s := range m {
@@ -117,22 +140,22 @@ func rmPath(root, src string, allowNotFound bool) error {
117
140
}
118
141
dir , err := fs .RootPath (root , filepath .Join ("/" , dir ))
119
142
if err != nil {
120
- return err
143
+ return errors . WithStack ( err )
121
144
}
122
145
p := filepath .Join (dir , base )
123
146
124
147
if ! allowNotFound {
125
148
_ , err := os .Stat (p )
126
149
127
150
if errors .Is (err , os .ErrNotExist ) {
128
- return err
151
+ return errors . WithStack ( err )
129
152
}
130
153
}
131
154
132
- return os .RemoveAll (p )
155
+ return errors . WithStack ( os .RemoveAll (p ) )
133
156
}
134
157
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 ) {
136
159
srcPath , err := cleanPath (action .Src )
137
160
if err != nil {
138
161
return errors .Wrap (err , "cleaning source path" )
@@ -144,7 +167,7 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
144
167
if ! action .CreateDestPath {
145
168
p , err := fs .RootPath (dest , filepath .Join ("/" , action .Dest ))
146
169
if err != nil {
147
- return err
170
+ return errors . WithStack ( err )
148
171
}
149
172
if _ , err := os .Lstat (filepath .Dir (p )); err != nil {
150
173
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 *
177
200
copy .WithXAttrErrorHandler (xattrErrorHandler ),
178
201
}
179
202
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
+
180
212
var m []string
181
213
if ! action .AllowWildcard {
182
214
m = []string {srcPath }
183
215
} else {
184
216
var err error
185
217
m , err = copy .ResolveWildcards (src , srcPath , action .FollowSymlink )
186
218
if err != nil {
187
- return err
219
+ return errors . WithStack ( err )
188
220
}
189
221
190
222
if len (m ) == 0 {
@@ -198,13 +230,13 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
198
230
for _ , s := range m {
199
231
if action .AttemptUnpackDockerCompatibility {
200
232
if ok , err := unpack (src , s , dest , destPath , ch , timestampToTime (action .Timestamp ), idmap ); err != nil {
201
- return err
233
+ return errors . WithStack ( err )
202
234
} else if ok {
203
235
continue
204
236
}
205
237
}
206
238
if err := copy .Copy (ctx , src , s , dest , destPath , opt ... ); err != nil {
207
- return err
239
+ return errors . WithStack ( err )
208
240
}
209
241
}
210
242
0 commit comments