github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/system/xattrs_linux.go (about)

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