github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/internal/syscall/unix/at_libc.go (about)

     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 aix || solaris
     6  // +build aix solaris
     7  
     8  package unix
     9  
    10  import (
    11  	"syscall"
    12  	"unsafe"
    13  )
    14  
    15  //go:linkname procFstatat libc_fstatat
    16  //go:linkname procOpenat libc_openat
    17  //go:linkname procUnlinkat libc_unlinkat
    18  
    19  var (
    20  	procFstatat,
    21  	procOpenat,
    22  	procUnlinkat uintptr
    23  )
    24  
    25  func Unlinkat(dirfd int, path string, flags int) error {
    26  	p, err := syscall.BytePtrFromString(path)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	_, _, errno := syscall6(uintptr(unsafe.Pointer(&procUnlinkat)), 3, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags), 0, 0, 0)
    32  	if errno != 0 {
    33  		return errno
    34  	}
    35  
    36  	return nil
    37  }
    38  
    39  func Openat(dirfd int, path string, flags int, perm uint32) (int, error) {
    40  	p, err := syscall.BytePtrFromString(path)
    41  	if err != nil {
    42  		return 0, err
    43  	}
    44  
    45  	fd, _, errno := syscall6(uintptr(unsafe.Pointer(&procOpenat)), 4, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(flags), uintptr(perm), 0, 0)
    46  	if errno != 0 {
    47  		return 0, errno
    48  	}
    49  
    50  	return int(fd), nil
    51  }
    52  
    53  func Fstatat(dirfd int, path string, stat *syscall.Stat_t, flags int) error {
    54  	p, err := syscall.BytePtrFromString(path)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	_, _, errno := syscall6(uintptr(unsafe.Pointer(&procFstatat)), 4, uintptr(dirfd), uintptr(unsafe.Pointer(p)), uintptr(unsafe.Pointer(stat)), uintptr(flags), 0, 0)
    60  	if errno != 0 {
    61  		return errno
    62  	}
    63  
    64  	return nil
    65  }