github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/pkg/fileutil/xattrs_linux.go (about)

     1  // Copyright 2014 Red Hat, Inc
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build linux
    16  
    17  // These functions are from github.com/docker/docker/pkg/system
    18  
    19  package fileutil
    20  
    21  import (
    22  	"syscall"
    23  	"unsafe"
    24  )
    25  
    26  // Returns a nil slice and nil error if the xattr is not set
    27  func Lgetxattr(path string, attr string) ([]byte, error) {
    28  	pathBytes, err := syscall.BytePtrFromString(path)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	attrBytes, err := syscall.BytePtrFromString(attr)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	dest := make([]byte, 128)
    38  	destBytes := unsafe.Pointer(&dest[0])
    39  	sz, _, errno := syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
    40  	if errno == syscall.ENODATA {
    41  		return nil, nil
    42  	}
    43  	if errno == syscall.ERANGE {
    44  		dest = make([]byte, sz)
    45  		destBytes := unsafe.Pointer(&dest[0])
    46  		sz, _, errno = syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
    47  	}
    48  	if errno != 0 {
    49  		return nil, errno
    50  	}
    51  
    52  	return dest[:sz], nil
    53  }
    54  
    55  var _zero uintptr
    56  
    57  func Lsetxattr(path string, attr string, data []byte, flags int) error {
    58  	pathBytes, err := syscall.BytePtrFromString(path)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	attrBytes, err := syscall.BytePtrFromString(attr)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	var dataBytes unsafe.Pointer
    67  	if len(data) > 0 {
    68  		dataBytes = unsafe.Pointer(&data[0])
    69  	} else {
    70  		dataBytes = unsafe.Pointer(&_zero)
    71  	}
    72  	_, _, errno := syscall.Syscall6(syscall.SYS_LSETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(dataBytes), uintptr(len(data)), uintptr(flags), 0)
    73  	if errno != 0 {
    74  		return errno
    75  	}
    76  	return nil
    77  }