diff --git a/src/syscall/export_freebsd_test.go b/src/syscall/export_freebsd_test.go new file mode 100644 index 0000000000..d47f09024f --- /dev/null +++ b/src/syscall/export_freebsd_test.go @@ -0,0 +1,12 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package syscall + +type Dirent_freebsd11 = dirent_freebsd11 + +var ( + Roundup = roundup + ConvertFromDirents11 = convertFromDirents11 +) diff --git a/src/syscall/syscall_freebsd.go b/src/syscall/syscall_freebsd.go index 9ae024131d..d6f75098c0 100644 --- a/src/syscall/syscall_freebsd.go +++ b/src/syscall/syscall_freebsd.go @@ -223,31 +223,31 @@ func Fstat(fd int, st *Stat_t) (err error) { return nil } -func Statfs(path string, stat *Statfs_t) (err error) { +func Statfs(path string, st *Statfs_t) (err error) { var oldStatfs statfs_freebsd11_t if supportsABI(_ino64First) { - return statfs_freebsd12(path, stat) + return statfs_freebsd12(path, st) } err = statfs(path, &oldStatfs) if err != nil { return err } - stat.convertFrom(&oldStatfs) + st.convertFrom(&oldStatfs) return nil } -func Fstatfs(fd int, stat *Statfs_t) (err error) { +func Fstatfs(fd int, st *Statfs_t) (err error) { var oldStatfs statfs_freebsd11_t if supportsABI(_ino64First) { - return fstatfs_freebsd12(fd, stat) + return fstatfs_freebsd12(fd, st) } err = fstatfs(fd, &oldStatfs) if err != nil { return err } - stat.convertFrom(&oldStatfs) + st.convertFrom(&oldStatfs) return nil } @@ -262,7 +262,7 @@ func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) { oldBuf := make([]byte, oldBufLen) n, err = getdirentries(fd, oldBuf, basep) if err == nil && n > 0 { - n = convertFromDirents11(oldBuf[:n], buf) + n = convertFromDirents11(buf, oldBuf[:n]) } return } @@ -344,17 +344,20 @@ func (s *Statfs_t) convertFrom(old *statfs_freebsd11_t) { copy(s.Mntonname[:], old.Mntonname[:n]) } -func convertFromDirents11(old []byte, buf []byte) int { - oldFixedSize := int(unsafe.Offsetof((*dirent_freebsd11)(nil).Name)) - fixedSize := int(unsafe.Offsetof((*Dirent)(nil).Name)) - srcPos := 0 +func convertFromDirents11(buf []byte, old []byte) int { + const ( + fixedSize = int(unsafe.Offsetof(Dirent{}.Name)) + oldFixedSize = int(unsafe.Offsetof(dirent_freebsd11{}.Name)) + ) + dstPos := 0 + srcPos := 0 for dstPos+fixedSize < len(buf) && srcPos+oldFixedSize < len(old) { - srcDirent := (*dirent_freebsd11)(unsafe.Pointer(&old[srcPos])) dstDirent := (*Dirent)(unsafe.Pointer(&buf[dstPos])) + srcDirent := (*dirent_freebsd11)(unsafe.Pointer(&old[srcPos])) reclen := roundup(fixedSize+int(srcDirent.Namlen)+1, 8) - if dstPos+reclen >= len(buf) { + if dstPos+reclen > len(buf) { break } diff --git a/src/syscall/syscall_freebsd_test.go b/src/syscall/syscall_freebsd_test.go new file mode 100644 index 0000000000..3ccfe5d463 --- /dev/null +++ b/src/syscall/syscall_freebsd_test.go @@ -0,0 +1,54 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build freebsd + +package syscall_test + +import ( + "fmt" + "syscall" + "testing" + "unsafe" +) + +func TestConvertFromDirent11(t *testing.T) { + const ( + filenameFmt = "%04d" + numFiles = 64 + fixedHdrSize = int(unsafe.Offsetof(syscall.Dirent_freebsd11{}.Name)) + ) + + namlen := len(fmt.Sprintf(filenameFmt, 0)) + reclen := syscall.Roundup(fixedHdrSize+namlen+1, 4) + old := make([]byte, numFiles*reclen) + for i := 0; i < numFiles; i++ { + dent := syscall.Dirent_freebsd11{ + Fileno: uint32(i + 1), + Reclen: uint16(reclen), + Type: syscall.DT_REG, + Namlen: uint8(namlen), + } + rec := make([]byte, reclen) + copy(rec, (*[fixedHdrSize]byte)(unsafe.Pointer(&dent))[:]) + copy(rec[fixedHdrSize:], fmt.Sprintf(filenameFmt, i+1)) + copy(old[i*reclen:], rec) + } + + buf := make([]byte, 2*len(old)) + n := syscall.ConvertFromDirents11(buf, old) + + names := make([]string, 0, numFiles) + _, _, names = syscall.ParseDirent(buf[:n], -1, names) + + if len(names) != numFiles { + t.Errorf("expected %d files, have %d; names: %q", numFiles, len(names), names) + } + + for i, name := range names { + if expected := fmt.Sprintf(filenameFmt, i+1); name != expected { + t.Errorf("expected names[%d] to be %q; got %q", i, expected, name) + } + } +}