libgocryptfs/internal/syscallcompat/getdents_other.go
Jakob Unterwurzacher ec74d1d2f4 Update go-fuse import path to github.com/hanwen/go-fuse/v2
We need
fd7328faf9
to fix a crash reported in https://github.com/rfjakob/gocryptfs/issues/430 :

  2019/10/30 17:14:16 Unknown opcode 2016
  panic: runtime error: invalid memory address or nil pointer dereference
  [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x508d38]

This patch is only in the v2.x.x branch. Upgrade to v2, as the
old API is also supported there.

Running

  git grep hanwen/go-fuse | grep -v hanwen/go-fuse/v2

to check for forgotten references comes back clean.
2020-05-17 14:23:47 +02:00

50 lines
1.1 KiB
Go

package syscallcompat
import (
"os"
"syscall"
"golang.org/x/sys/unix"
"github.com/hanwen/go-fuse/v2/fuse"
)
// emulateGetdents reads all directory entries from the open directory "fd"
// and returns them in a fuse.DirEntry slice.
func emulateGetdents(fd int) (out []fuse.DirEntry, err error) {
// os.File closes the fd in its finalizer. Duplicate the fd to not affect
// the original fd.
newFd, err := syscall.Dup(fd)
if err != nil {
return nil, err
}
f := os.NewFile(uintptr(newFd), "")
defer f.Close()
// Get all file names in the directory
names, err := f.Readdirnames(0)
if err != nil {
return nil, err
}
// Stat all of them and convert to fuse.DirEntry
out = make([]fuse.DirEntry, 0, len(names))
for _, name := range names {
var st unix.Stat_t
err = Fstatat(fd, name, &st, unix.AT_SYMLINK_NOFOLLOW)
if err == syscall.ENOENT {
// File disappeared between readdir and stat. Pretend we did not
// see it.
continue
}
if err != nil {
return nil, err
}
newEntry := fuse.DirEntry{
Name: name,
Mode: uint32(st.Mode) & syscall.S_IFMT,
Ino: st.Ino,
}
out = append(out, newEntry)
}
return out, nil
}