github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/xattr_unix.go (about)

     1  // Package fs provides mountpath and FQN abstractions and methods to resolve/map stored content
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package fs
     6  
     7  import (
     8  	"github.com/NVIDIA/aistore/cmn/cos"
     9  	"github.com/NVIDIA/aistore/cmn/nlog"
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  //
    14  // xattrs
    15  //
    16  
    17  // GetXattr gets xattr by name - see also the buffered version below
    18  func GetXattr(fqn, attrName string) ([]byte, error) {
    19  	const maxAttrSize = 4096
    20  	buf := make([]byte, maxAttrSize)
    21  	return GetXattrBuf(fqn, attrName, buf)
    22  }
    23  
    24  // GetXattr gets xattr by name via provided buffer
    25  func GetXattrBuf(fqn, attrName string, buf []byte) (b []byte, err error) {
    26  	var n int
    27  	n, err = unix.Getxattr(fqn, attrName, buf)
    28  	if err == nil { // returns ERANGE if len(buf) is not enough
    29  		b = buf[:n]
    30  	}
    31  	return
    32  }
    33  
    34  // SetXattr sets xattr name = value
    35  func SetXattr(fqn, attrName string, data []byte) (err error) {
    36  	return unix.Setxattr(fqn, attrName, data, 0)
    37  }
    38  
    39  // removeXattr removes xattr
    40  func removeXattr(fqn, attrName string) error {
    41  	err := unix.Removexattr(fqn, attrName)
    42  	if err != nil && !cos.IsErrXattrNotFound(err) {
    43  		nlog.Errorf("failed to remove %q from %s: %v", attrName, fqn, err)
    44  		return err
    45  	}
    46  	return nil
    47  }