Skip to content

Commit 18f336d

Browse files
Merge pull request opencontainers#1470 from tklauser/x-sys-unix-symlink-xattrs
Use symlink xattr functions from x/sys/unix
2 parents a6906d5 + d8b5c1c commit 18f336d

15 files changed

+1062
-82
lines changed

libcontainer/system/xattrs_linux.go

Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,14 @@
11
package system
22

3-
import (
4-
"unsafe"
5-
6-
"golang.org/x/sys/unix"
7-
)
8-
9-
var _zero uintptr
10-
11-
// Returns the size of xattrs and nil error
12-
// Requires path, takes allocated []byte or nil as last argument
13-
func Llistxattr(path string, dest []byte) (size int, err error) {
14-
pathBytes, err := unix.BytePtrFromString(path)
15-
if err != nil {
16-
return -1, err
17-
}
18-
var newpathBytes unsafe.Pointer
19-
if len(dest) > 0 {
20-
newpathBytes = unsafe.Pointer(&dest[0])
21-
} else {
22-
newpathBytes = unsafe.Pointer(&_zero)
23-
}
24-
25-
_size, _, errno := unix.Syscall6(unix.SYS_LLISTXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(newpathBytes), uintptr(len(dest)), 0, 0, 0)
26-
size = int(_size)
27-
if errno != 0 {
28-
return -1, errno
29-
}
30-
31-
return size, nil
32-
}
3+
import "golang.org/x/sys/unix"
334

345
// Returns a []byte slice if the xattr is set and nil otherwise
356
// Requires path and its attribute as arguments
367
func Lgetxattr(path string, attr string) ([]byte, error) {
378
var sz int
38-
pathBytes, err := unix.BytePtrFromString(path)
39-
if err != nil {
40-
return nil, err
41-
}
42-
attrBytes, err := unix.BytePtrFromString(attr)
43-
if err != nil {
44-
return nil, err
45-
}
46-
479
// Start with a 128 length byte array
48-
sz = 128
49-
dest := make([]byte, sz)
50-
destBytes := unsafe.Pointer(&dest[0])
51-
_sz, _, errno := unix.Syscall6(unix.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
10+
dest := make([]byte, 128)
11+
sz, errno := unix.Lgetxattr(path, attr, dest)
5212

5313
switch {
5414
case errno == unix.ENODATA:
@@ -57,44 +17,19 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
5717
return nil, errno
5818
case errno == unix.ERANGE:
5919
// 128 byte array might just not be good enough,
60-
// A dummy buffer is used ``uintptr(0)`` to get real size
20+
// A dummy buffer is used to get the real size
6121
// of the xattrs on disk
62-
_sz, _, errno = unix.Syscall6(unix.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(unsafe.Pointer(nil)), uintptr(0), 0, 0)
63-
sz = int(_sz)
64-
if sz < 0 {
22+
sz, errno = unix.Lgetxattr(path, attr, []byte{})
23+
if errno != nil {
6524
return nil, errno
6625
}
6726
dest = make([]byte, sz)
68-
destBytes := unsafe.Pointer(&dest[0])
69-
_sz, _, errno = unix.Syscall6(unix.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
70-
if errno != 0 {
27+
sz, errno = unix.Lgetxattr(path, attr, dest)
28+
if errno != nil {
7129
return nil, errno
7230
}
73-
case errno != 0:
31+
case errno != nil:
7432
return nil, errno
7533
}
76-
sz = int(_sz)
7734
return dest[:sz], nil
7835
}
79-
80-
func Lsetxattr(path string, attr string, data []byte, flags int) error {
81-
pathBytes, err := unix.BytePtrFromString(path)
82-
if err != nil {
83-
return err
84-
}
85-
attrBytes, err := unix.BytePtrFromString(attr)
86-
if err != nil {
87-
return err
88-
}
89-
var dataBytes unsafe.Pointer
90-
if len(data) > 0 {
91-
dataBytes = unsafe.Pointer(&data[0])
92-
} else {
93-
dataBytes = unsafe.Pointer(&_zero)
94-
}
95-
_, _, errno := unix.Syscall6(unix.SYS_LSETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(dataBytes), uintptr(len(data)), uintptr(flags), 0)
96-
if errno != 0 {
97-
return errno
98-
}
99-
return nil
100-
}

libcontainer/xattr/xattr_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ func stringsfromByte(buf []byte) (result []string) {
2727
}
2828

2929
func Listxattr(path string) ([]string, error) {
30-
size, err := system.Llistxattr(path, nil)
30+
size, err := unix.Llistxattr(path, nil)
3131
if err != nil {
3232
return nil, err
3333
}
3434
buf := make([]byte, size)
35-
read, err := system.Llistxattr(path, buf)
35+
read, err := unix.Llistxattr(path, buf)
3636
if err != nil {
3737
return nil, err
3838
}
@@ -49,5 +49,5 @@ func Getxattr(path, attr string) (string, error) {
4949
}
5050

5151
func Setxattr(path, xattr, value string) error {
52-
return system.Lsetxattr(path, xattr, []byte(value), 0)
52+
return unix.Lsetxattr(path, xattr, []byte(value), 0)
5353
}

vendor.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ github.com/golang/protobuf 18c9bb3261723cd5401db4d0c9fbc5c3b6c70fe8
1818
github.com/docker/docker 0f5c9d301b9b1cca66b3ea0f9dec3b5317d3686d
1919
github.com/docker/go-units v0.2.0
2020
github.com/urfave/cli d53eb991652b1d438abdd34ce4bfa3ef1539108e
21-
golang.org/x/sys a55a76086885b80f79961eacb876ebd8caf3868d https://github.com/golang/sys
21+
golang.org/x/sys b90f89a1e7a9c1f6b918820b3daa7f08488c8594 https://github.com/golang/sys

vendor/golang.org/x/sys/unix/syscall_linux.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/sys/unix/zsyscall_linux_386.go

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/sys/unix/zsyscall_linux_amd64.go

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)