Source file src/internal/syscall/unix/at.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build dragonfly || freebsd || linux || netbsd || (openbsd && mips64)
     6  
     7  package unix
     8  
     9  import (
    10  	"syscall"
    11  	"unsafe"
    12  )
    13  
    14  func Unlinkat(dirfd int, path string, flags int) error {
    15  	p, err := syscall.BytePtrFromString(path)
    16  	if err != nil {
    17  		return err
    18  	}
    19  
    20  	_, _, errno := syscall.Syscall(unlinkatTrap, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags))
    21  	if errno != 0 {
    22  		return errno
    23  	}
    24  
    25  	return nil
    26  }
    27  
    28  func Openat(dirfd int, path string, flags int, perm uint32) (int, error) {
    29  	p, err := syscall.BytePtrFromString(path)
    30  	if err != nil {
    31  		return 0, err
    32  	}
    33  
    34  	fd, _, errno := syscall.Syscall6(openatTrap, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags), uintptr(perm), 0, 0)
    35  	if errno != 0 {
    36  		return 0, errno
    37  	}
    38  
    39  	return int(fd), nil
    40  }
    41  
    42  func Readlinkat(dirfd int, path string, buf []byte) (int, error) {
    43  	p0, err := syscall.BytePtrFromString(path)
    44  	if err != nil {
    45  		return 0, err
    46  	}
    47  	var p1 unsafe.Pointer
    48  	if len(buf) > 0 {
    49  		p1 = unsafe.Pointer(&buf[0])
    50  	} else {
    51  		p1 = unsafe.Pointer(&_zero)
    52  	}
    53  	n, _, errno := syscall.Syscall6(readlinkatTrap,
    54  		uintptr(dirfd),
    55  		uintptr(unsafe.Pointer(p0)),
    56  		uintptr(p1),
    57  		uintptr(len(buf)),
    58  		0, 0)
    59  	if errno != 0 {
    60  		return 0, errno
    61  	}
    62  
    63  	return int(n), nil
    64  }
    65  
    66  func Mkdirat(dirfd int, path string, mode uint32) error {
    67  	p, err := syscall.BytePtrFromString(path)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	_, _, errno := syscall.Syscall6(mkdiratTrap,
    73  		uintptr(dirfd),
    74  		uintptr(unsafe.Pointer(p)),
    75  		uintptr(mode),
    76  		0, 0, 0)
    77  	if errno != 0 {
    78  		return errno
    79  	}
    80  	return nil
    81  }
    82  

View as plain text