Skip to content

Commit

Permalink
unix: remove Go 1.12 compatibility on darwin
Browse files Browse the repository at this point in the history
Go 1.12 was released more than 3 years ago. Dropping support for it will
simplify generating the wrappers needed for Getdirentries
implementation.

Change-Id: If24bf6ff9076fb4e281948c67bac87744df98d7a
Reviewed-on: https://go-review.googlesource.com/c/sys/+/430176
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
  • Loading branch information
tklauser authored and gopherbot committed Sep 15, 2022
1 parent 63ea559 commit 7b5979e
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 305 deletions.
5 changes: 0 additions & 5 deletions unix/mkall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ esac
if [ "$GOOSARCH" == "aix_ppc64" ]; then
# aix/ppc64 script generates files instead of writing to stdin.
echo "$mksyscall -tags $GOOS,$GOARCH $syscall_goos $GOOSARCH_in && gofmt -w zsyscall_$GOOSARCH.go && gofmt -w zsyscall_"$GOOSARCH"_gccgo.go && gofmt -w zsyscall_"$GOOSARCH"_gc.go " ;
elif [ "$GOOS" == "darwin" ]; then
# 1.12 and later, syscalls via libSystem
echo "$mksyscall -tags $GOOS,$GOARCH,go1.12 $syscall_goos $GOOSARCH_in |gofmt >zsyscall_$GOOSARCH.go";
# 1.13 and later, syscalls via libSystem (including syscallPtr)
echo "$mksyscall -tags $GOOS,$GOARCH,go1.13 syscall_darwin.1_13.go |gofmt >zsyscall_$GOOSARCH.1_13.go";
elif [ "$GOOS" == "illumos" ]; then
# illumos code generation requires a --illumos switch
echo "$mksyscall -illumos -tags illumos,$GOARCH syscall_illumos.go |gofmt > zsyscall_illumos_$GOARCH.go";
Expand Down
30 changes: 4 additions & 26 deletions unix/mkasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func archPtrSize(arch string) int {
}
}

func generateASMFile(arch string, inFileNames []string, outFileName, buildTags string) map[string]bool {
func generateASMFile(arch string, inFileNames []string, outFileName string) map[string]bool {
trampolines := map[string]bool{}
var orderedTrampolines []string
for _, inFileName := range inFileNames {
Expand Down Expand Up @@ -59,11 +59,6 @@ func generateASMFile(arch string, inFileNames []string, outFileName, buildTags s
fmt.Fprintf(&out, "// go run mkasm.go %s\n", strings.Join(os.Args[1:], " "))
fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
fmt.Fprintf(&out, "\n")
if buildTags != "" {
fmt.Fprintf(&out, "//go:build %s\n", buildTags)
fmt.Fprintf(&out, "// +build %s\n", buildTags)
fmt.Fprintf(&out, "\n")
}
fmt.Fprintf(&out, "#include \"textflag.h\"\n")
for _, fn := range orderedTrampolines {
fmt.Fprintf(&out, "\nTEXT %s_trampoline<>(SB),NOSPLIT,$0-0\n", fn)
Expand Down Expand Up @@ -121,7 +116,6 @@ func main() {
}
goos, arch := os.Args[1], os.Args[2]

buildTags := ""
syscallFilename := fmt.Sprintf("syscall_%s.go", goos)
syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch)
zsyscallArchFilename := fmt.Sprintf("zsyscall_%s_%s.go", goos, arch)
Expand All @@ -133,25 +127,9 @@ func main() {
zsyscallArchFilename,
}

if goos == "darwin" {
buildTags = "go1.12"
}
trampolines := generateASMFile(arch, inFileNames, zsyscallASMFileName, buildTags)

if goos != "darwin" {
return
}
trampolines := generateASMFile(arch, inFileNames, zsyscallASMFileName)

inFileNames = []string{
"syscall_darwin.1_13.go",
fmt.Sprintf("zsyscall_darwin_%s.1_13.go", arch),
}
trampolines2 := generateASMFile(arch, inFileNames, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13")

// merge trampolines
for trampoline := range trampolines2 {
trampolines[trampoline] = true
if goos == "darwin" {
writeDarwinTest(trampolines, fmt.Sprintf("darwin_%s_test.go", arch), arch)
}

writeDarwinTest(trampolines, fmt.Sprintf("darwin_%s_test.go", arch), arch)
}
32 changes: 0 additions & 32 deletions unix/syscall_darwin.1_12.go

This file was deleted.

100 changes: 0 additions & 100 deletions unix/syscall_darwin.1_13.go

This file was deleted.

90 changes: 90 additions & 0 deletions unix/syscall_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,96 @@ import (
"unsafe"
)

//sys closedir(dir uintptr) (err error)
//sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)

func fdopendir(fd int) (dir uintptr, err error) {
r0, _, e1 := syscall_syscallPtr(libc_fdopendir_trampoline_addr, uintptr(fd), 0, 0)
dir = uintptr(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}

var libc_fdopendir_trampoline_addr uintptr

//go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"

func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
// Simulate Getdirentries using fdopendir/readdir_r/closedir.
// We store the number of entries to skip in the seek
// offset of fd. See issue #31368.
// It's not the full required semantics, but should handle the case
// of calling Getdirentries or ReadDirent repeatedly.
// It won't handle assigning the results of lseek to *basep, or handle
// the directory being edited underfoot.
skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
if err != nil {
return 0, err
}

// We need to duplicate the incoming file descriptor
// because the caller expects to retain control of it, but
// fdopendir expects to take control of its argument.
// Just Dup'ing the file descriptor is not enough, as the
// result shares underlying state. Use Openat to make a really
// new file descriptor referring to the same directory.
fd2, err := Openat(fd, ".", O_RDONLY, 0)
if err != nil {
return 0, err
}
d, err := fdopendir(fd2)
if err != nil {
Close(fd2)
return 0, err
}
defer closedir(d)

var cnt int64
for {
var entry Dirent
var entryp *Dirent
e := readdir_r(d, &entry, &entryp)
if e != 0 {
return n, errnoErr(e)
}
if entryp == nil {
break
}
if skip > 0 {
skip--
cnt++
continue
}

reclen := int(entry.Reclen)
if reclen > len(buf) {
// Not enough room. Return for now.
// The counter will let us know where we should start up again.
// Note: this strategy for suspending in the middle and
// restarting is O(n^2) in the length of the directory. Oh well.
break
}

// Copy entry into return buffer.
s := unsafe.Slice((*byte)(unsafe.Pointer(&entry)), reclen)
copy(buf, s)

buf = buf[reclen:]
n += reclen
cnt++
}
// Set the seek offset of the input fd to record
// how many files we've already returned.
_, err = Seek(fd, cnt, 0 /* SEEK_SET */)
if err != nil {
return n, err
}

return n, nil
}

// SockaddrDatalink implements the Sockaddr interface for AF_LINK type sockets.
type SockaddrDatalink struct {
Len uint8
Expand Down
40 changes: 0 additions & 40 deletions unix/zsyscall_darwin_amd64.1_13.go

This file was deleted.

25 changes: 0 additions & 25 deletions unix/zsyscall_darwin_amd64.1_13.s

This file was deleted.

Loading

0 comments on commit 7b5979e

Please sign in to comment.