github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/fsgofer/fsgofer_unsafe.go (about)

     1  // Copyright 2018 The gVisor Authors.
     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  package fsgofer
    16  
    17  import (
    18  	"unsafe"
    19  
    20  	"golang.org/x/sys/unix"
    21  	"github.com/SagerNet/gvisor/pkg/syserr"
    22  )
    23  
    24  func utimensat(dirFd int, name string, times [2]unix.Timespec, flags int) error {
    25  	// utimensat(2) doesn't accept empty name, instead name must be nil to make it
    26  	// operate directly on 'dirFd' unlike other *at syscalls.
    27  	var namePtr unsafe.Pointer
    28  	if name != "" {
    29  		nameBytes, err := unix.BytePtrFromString(name)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		namePtr = unsafe.Pointer(nameBytes)
    34  	}
    35  
    36  	timesPtr := unsafe.Pointer(&times[0])
    37  
    38  	if _, _, errno := unix.Syscall6(
    39  		unix.SYS_UTIMENSAT,
    40  		uintptr(dirFd),
    41  		uintptr(namePtr),
    42  		uintptr(timesPtr),
    43  		uintptr(flags),
    44  		0,
    45  		0); errno != 0 {
    46  
    47  		return syserr.FromHost(errno).ToError()
    48  	}
    49  	return nil
    50  }
    51  
    52  func renameat(oldDirFD int, oldName string, newDirFD int, newName string) error {
    53  	var oldNamePtr unsafe.Pointer
    54  	if oldName != "" {
    55  		nameBytes, err := unix.BytePtrFromString(oldName)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		oldNamePtr = unsafe.Pointer(nameBytes)
    60  	}
    61  	var newNamePtr unsafe.Pointer
    62  	if newName != "" {
    63  		nameBytes, err := unix.BytePtrFromString(newName)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		newNamePtr = unsafe.Pointer(nameBytes)
    68  	}
    69  
    70  	if _, _, errno := unix.Syscall6(
    71  		unix.SYS_RENAMEAT,
    72  		uintptr(oldDirFD),
    73  		uintptr(oldNamePtr),
    74  		uintptr(newDirFD),
    75  		uintptr(newNamePtr),
    76  		0,
    77  		0); errno != 0 {
    78  
    79  		return syserr.FromHost(errno).ToError()
    80  	}
    81  	return nil
    82  }