Skip to content

Commit 43bf65c

Browse files
committed
pkg/system: return even richer xattr errors
The names of extended attributes are not completely freeform. Attributes are namespaced, and the kernel enforces (among other things) that only attributes whose names are prefixed with a valid namespace are permitted. The name of the attribute therefore needs to be known in order to diagnose issues with lsetxattr. Include the name of the extended attribute in the errors returned from the Lsetxattr and Lgetxattr so users and us can more easily troubleshoot xattr-related issues. Include the name in a separate rich-error field to provide code handling the error enough information to determine whether or not the failure can be ignored. Signed-off-by: Cory Snider <[email protected]>
1 parent c87e0ad commit 43bf65c

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

builder/remotecontext/archive.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/docker/pkg/archive"
1010
"github.com/docker/docker/pkg/chrootarchive"
1111
"github.com/docker/docker/pkg/longpath"
12+
"github.com/docker/docker/pkg/system"
1213
"github.com/docker/docker/pkg/tarsum"
1314
"github.com/moby/sys/symlink"
1415
"github.com/pkg/errors"
@@ -24,9 +25,11 @@ func (c *archiveContext) Close() error {
2425
}
2526

2627
func convertPathError(err error, cleanpath string) error {
27-
if err, ok := err.(*os.PathError); ok {
28+
switch err := err.(type) {
29+
case *os.PathError:
30+
err.Path = cleanpath
31+
case *system.XattrError:
2832
err.Path = cleanpath
29-
return err
3033
}
3134
return err
3235
}

pkg/system/xattrs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package system // import "github.com/docker/docker/pkg/system"
2+
3+
type XattrError struct {
4+
Op string
5+
Attr string
6+
Path string
7+
Err error
8+
}
9+
10+
func (e *XattrError) Error() string { return e.Op + " " + e.Attr + " " + e.Path + ": " + e.Err.Error() }
11+
12+
func (e *XattrError) Unwrap() error { return e.Err }
13+
14+
// Timeout reports whether this error represents a timeout.
15+
func (e *XattrError) Timeout() bool {
16+
t, ok := e.Err.(interface{ Timeout() bool })
17+
return ok && t.Timeout()
18+
}

pkg/system/xattrs_linux.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package system // import "github.com/docker/docker/pkg/system"
22

33
import (
4-
"io/fs"
5-
64
"golang.org/x/sys/unix"
75
)
86

97
// Lgetxattr retrieves the value of the extended attribute identified by attr
108
// and associated with the given path in the file system.
119
// It will returns a nil slice and nil error if the xattr is not set.
1210
func Lgetxattr(path string, attr string) ([]byte, error) {
13-
pathErr := func(err error) ([]byte, error) {
14-
return nil, &fs.PathError{Op: "lgetxattr", Path: path, Err: err}
11+
sysErr := func(err error) ([]byte, error) {
12+
return nil, &XattrError{Op: "lgetxattr", Attr: attr, Path: path, Err: err}
1513
}
1614

1715
// Start with a 128 length byte array
@@ -22,7 +20,7 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
2220
// Buffer too small, use zero-sized buffer to get the actual size
2321
sz, errno = unix.Lgetxattr(path, attr, []byte{})
2422
if errno != nil {
25-
return pathErr(errno)
23+
return sysErr(errno)
2624
}
2725
dest = make([]byte, sz)
2826
sz, errno = unix.Lgetxattr(path, attr, dest)
@@ -32,7 +30,7 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
3230
case errno == unix.ENODATA:
3331
return nil, nil
3432
case errno != nil:
35-
return pathErr(errno)
33+
return sysErr(errno)
3634
}
3735

3836
return dest[:sz], nil
@@ -43,7 +41,7 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
4341
func Lsetxattr(path string, attr string, data []byte, flags int) error {
4442
err := unix.Lsetxattr(path, attr, data, flags)
4543
if err != nil {
46-
return &fs.PathError{Op: "lsetxattr", Path: path, Err: err}
44+
return &XattrError{Op: "lsetxattr", Attr: attr, Path: path, Err: err}
4745
}
4846
return nil
4947
}

0 commit comments

Comments
 (0)