If `unix.Lgetxattr` returns an error, then `sz == -1` which will cause a
runtime panic if `errno == unix.ERANGE`.
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
| ... | ... |
@@ -6,19 +6,28 @@ import "golang.org/x/sys/unix" |
| 6 | 6 |
// and associated with the given path in the file system. |
| 7 | 7 |
// It will returns a nil slice and nil error if the xattr is not set. |
| 8 | 8 |
func Lgetxattr(path string, attr string) ([]byte, error) {
|
| 9 |
+ // Start with a 128 length byte array |
|
| 9 | 10 |
dest := make([]byte, 128) |
| 10 | 11 |
sz, errno := unix.Lgetxattr(path, attr, dest) |
| 11 |
- if errno == unix.ENODATA {
|
|
| 12 |
+ |
|
| 13 |
+ switch {
|
|
| 14 |
+ case errno == unix.ENODATA: |
|
| 12 | 15 |
return nil, nil |
| 13 |
- } |
|
| 14 |
- if errno == unix.ERANGE {
|
|
| 16 |
+ case errno == unix.ERANGE: |
|
| 17 |
+ // 128 byte array might just not be good enough. A dummy buffer is used |
|
| 18 |
+ // to get the real size of the xattrs on disk |
|
| 19 |
+ sz, errno = unix.Lgetxattr(path, attr, []byte{})
|
|
| 20 |
+ if errno != nil {
|
|
| 21 |
+ return nil, errno |
|
| 22 |
+ } |
|
| 15 | 23 |
dest = make([]byte, sz) |
| 16 | 24 |
sz, errno = unix.Lgetxattr(path, attr, dest) |
| 17 |
- } |
|
| 18 |
- if errno != nil {
|
|
| 25 |
+ if errno != nil {
|
|
| 26 |
+ return nil, errno |
|
| 27 |
+ } |
|
| 28 |
+ case errno != nil: |
|
| 19 | 29 |
return nil, errno |
| 20 | 30 |
} |
| 21 |
- |
|
| 22 | 31 |
return dest[:sz], nil |
| 23 | 32 |
} |
| 24 | 33 |
|