From 7d3fd4f3c46e57c703128bbabb385424f338d6d9 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 25 Sep 2020 18:06:26 +0200 Subject: syscall: implement Getwd using getcwd from libSystem on darwin Directly wrap the getcwd implementation provided by libSystem.dylib on darwin and use it to implement Getwd like on the BSDs. This allows to drop the custom implementation using getAttrList and to merge the implementation of Getwd for darwin and the BSDs in syscall_bsd.go. Same as CL 257497 did for golang.org/x/sys/unix Change-Id: If30390c4c17cd463bb8fdcb5465f40d6fa11f391 Reviewed-on: https://go-review.googlesource.com/c/go/+/257637 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/syscall/syscall_darwin.go | 85 +------------------------------------------ 1 file changed, 2 insertions(+), 83 deletions(-) (limited to 'src/syscall/syscall_darwin.go') diff --git a/src/syscall/syscall_darwin.go b/src/syscall/syscall_darwin.go index c84547c628..afdadbf894 100644 --- a/src/syscall/syscall_darwin.go +++ b/src/syscall/syscall_darwin.go @@ -12,28 +12,7 @@ package syscall -import ( - errorspkg "errors" - "unsafe" -) - -const ImplementsGetwd = true - -func Getwd() (string, error) { - buf := make([]byte, 2048) - attrs, err := getAttrList(".", attrList{CommonAttr: attrCmnFullpath}, buf, 0) - if err == nil && len(attrs) == 1 && len(attrs[0]) >= 2 { - wd := string(attrs[0]) - // Sanity check that it's an absolute path and ends - // in a null byte, which we then strip. - if wd[0] == '/' && wd[len(wd)-1] == 0 { - return wd[:len(wd)-1], nil - } - } - // If pkg/os/getwd.go gets ENOTSUP, it will fall back to the - // slow algorithm. - return "", ENOTSUP -} +import "unsafe" type SockaddrDatalink struct { Len uint8 @@ -94,7 +73,6 @@ const ( attrBitMapCount = 5 attrCmnModtime = 0x00000400 attrCmnAcctime = 0x00001000 - attrCmnFullpath = 0x08000000 ) type attrList struct { @@ -107,66 +85,6 @@ type attrList struct { Forkattr uint32 } -func getAttrList(path string, attrList attrList, attrBuf []byte, options uint) (attrs [][]byte, err error) { - if len(attrBuf) < 4 { - return nil, errorspkg.New("attrBuf too small") - } - attrList.bitmapCount = attrBitMapCount - - var _p0 *byte - _p0, err = BytePtrFromString(path) - if err != nil { - return nil, err - } - - _, _, e1 := syscall6( - funcPC(libc_getattrlist_trampoline), - uintptr(unsafe.Pointer(_p0)), - uintptr(unsafe.Pointer(&attrList)), - uintptr(unsafe.Pointer(&attrBuf[0])), - uintptr(len(attrBuf)), - uintptr(options), - 0, - ) - if e1 != 0 { - return nil, e1 - } - size := *(*uint32)(unsafe.Pointer(&attrBuf[0])) - - // dat is the section of attrBuf that contains valid data, - // without the 4 byte length header. All attribute offsets - // are relative to dat. - dat := attrBuf - if int(size) < len(attrBuf) { - dat = dat[:size] - } - dat = dat[4:] // remove length prefix - - for i := uint32(0); int(i) < len(dat); { - header := dat[i:] - if len(header) < 8 { - return attrs, errorspkg.New("truncated attribute header") - } - datOff := *(*int32)(unsafe.Pointer(&header[0])) - attrLen := *(*uint32)(unsafe.Pointer(&header[4])) - if datOff < 0 || uint32(datOff)+attrLen > uint32(len(dat)) { - return attrs, errorspkg.New("truncated results; attrBuf too small") - } - end := uint32(datOff) + attrLen - attrs = append(attrs, dat[datOff:end]) - i = end - if r := i % 4; r != 0 { - i += (4 - r) - } - } - return -} - -func libc_getattrlist_trampoline() - -//go:linkname libc_getattrlist libc_getattrlist -//go:cgo_import_dynamic libc_getattrlist getattrlist "/usr/lib/libSystem.B.dylib" - //sysnb pipe(p *[2]int32) (err error) func Pipe(p []int) (err error) { @@ -341,6 +259,7 @@ func Kill(pid int, signum Signal) (err error) { return kill(pid, int(signum), 1) //sys fcntlPtr(fd int, cmd int, arg unsafe.Pointer) (val int, err error) = SYS_fcntl //sys unlinkat(fd int, path string, flags int) (err error) //sys openat(fd int, path string, flags int, perm uint32) (fdret int, err error) +//sys getcwd(buf []byte) (n int, err error) func init() { execveDarwin = execve -- cgit v1.3