...

Source file src/github.com/opencontainers/selinux/go-selinux/xattrs_linux.go

Documentation: github.com/opencontainers/selinux/go-selinux

     1  package selinux
     2  
     3  import (
     4  	"golang.org/x/sys/unix"
     5  )
     6  
     7  // lgetxattr returns a []byte slice containing the value of
     8  // an extended attribute attr set for path.
     9  func lgetxattr(path, attr string) ([]byte, error) {
    10  	// Start with a 128 length byte array
    11  	dest := make([]byte, 128)
    12  	sz, errno := doLgetxattr(path, attr, dest)
    13  	for errno == unix.ERANGE { //nolint:errorlint // unix errors are bare
    14  		// Buffer too small, use zero-sized buffer to get the actual size
    15  		sz, errno = doLgetxattr(path, attr, []byte{})
    16  		if errno != nil {
    17  			return nil, errno
    18  		}
    19  
    20  		dest = make([]byte, sz)
    21  		sz, errno = doLgetxattr(path, attr, dest)
    22  	}
    23  	if errno != nil {
    24  		return nil, errno
    25  	}
    26  
    27  	return dest[:sz], nil
    28  }
    29  
    30  // doLgetxattr is a wrapper that retries on EINTR
    31  func doLgetxattr(path, attr string, dest []byte) (int, error) {
    32  	for {
    33  		sz, err := unix.Lgetxattr(path, attr, dest)
    34  		if err != unix.EINTR { //nolint:errorlint // unix errors are bare
    35  			return sz, err
    36  		}
    37  	}
    38  }
    39  
    40  // getxattr returns a []byte slice containing the value of
    41  // an extended attribute attr set for path.
    42  func getxattr(path, attr string) ([]byte, error) {
    43  	// Start with a 128 length byte array
    44  	dest := make([]byte, 128)
    45  	sz, errno := dogetxattr(path, attr, dest)
    46  	for errno == unix.ERANGE { //nolint:errorlint // unix errors are bare
    47  		// Buffer too small, use zero-sized buffer to get the actual size
    48  		sz, errno = dogetxattr(path, attr, []byte{})
    49  		if errno != nil {
    50  			return nil, errno
    51  		}
    52  
    53  		dest = make([]byte, sz)
    54  		sz, errno = dogetxattr(path, attr, dest)
    55  	}
    56  	if errno != nil {
    57  		return nil, errno
    58  	}
    59  
    60  	return dest[:sz], nil
    61  }
    62  
    63  // dogetxattr is a wrapper that retries on EINTR
    64  func dogetxattr(path, attr string, dest []byte) (int, error) {
    65  	for {
    66  		sz, err := unix.Getxattr(path, attr, dest)
    67  		if err != unix.EINTR { //nolint:errorlint // unix errors are bare
    68  			return sz, err
    69  		}
    70  	}
    71  }
    72  

View as plain text