github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/system/xattrs_linux.go (about)

     1  package system // import "github.com/demonoid81/moby/pkg/system"
     2  
     3  import "golang.org/x/sys/unix"
     4  
     5  // Lgetxattr retrieves the value of the extended attribute identified by attr
     6  // and associated with the given path in the file system.
     7  // It will returns a nil slice and nil error if the xattr is not set.
     8  func Lgetxattr(path string, attr string) ([]byte, error) {
     9  	// Start with a 128 length byte array
    10  	dest := make([]byte, 128)
    11  	sz, errno := unix.Lgetxattr(path, attr, dest)
    12  
    13  	for errno == unix.ERANGE {
    14  		// Buffer too small, use zero-sized buffer to get the actual size
    15  		sz, errno = unix.Lgetxattr(path, attr, []byte{})
    16  		if errno != nil {
    17  			return nil, errno
    18  		}
    19  		dest = make([]byte, sz)
    20  		sz, errno = unix.Lgetxattr(path, attr, dest)
    21  	}
    22  
    23  	switch {
    24  	case errno == unix.ENODATA:
    25  		return nil, nil
    26  	case errno != nil:
    27  		return nil, errno
    28  	}
    29  
    30  	return dest[:sz], nil
    31  }
    32  
    33  // Lsetxattr sets the value of the extended attribute identified by attr
    34  // and associated with the given path in the file system.
    35  func Lsetxattr(path string, attr string, data []byte, flags int) error {
    36  	return unix.Lsetxattr(path, attr, data, flags)
    37  }