|
| 1 | +// +build linux |
| 2 | + |
| 3 | +package syscallcompat |
| 4 | + |
| 5 | +// Other implementations of getdents in Go: |
| 6 | +// https://github.com/ericlagergren/go-gnulib/blob/cb7a6e136427e242099b2c29d661016c19458801/dirent/getdents_unix.go |
| 7 | +// https://github.com/golang/tools/blob/5831d16d18029819d39f99bdc2060b8eff410b6b/imports/fastwalk_unix.go |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "syscall" |
| 12 | + "unsafe" |
| 13 | + |
| 14 | + "github.com/hanwen/go-fuse/fuse" |
| 15 | + |
| 16 | + "github.com/rfjakob/gocryptfs/internal/tlog" |
| 17 | +) |
| 18 | + |
| 19 | +// HaveGetdents is true if we have a working implementation of Getdents |
| 20 | +const HaveGetdents = true |
| 21 | + |
| 22 | +const sizeofDirent = int(unsafe.Sizeof(syscall.Dirent{})) |
| 23 | + |
| 24 | +// Getdents wraps syscall.Getdents and converts the result to []fuse.DirEntry. |
| 25 | +// The function takes a path instead of an fd because we need to be able to |
| 26 | +// call Lstat on files. Fstatat is not yet available in Go as of v1.9: |
| 27 | +// https://github.com/golang/go/issues/14216 |
| 28 | +func Getdents(dir string) ([]fuse.DirEntry, error) { |
| 29 | + fd, err := syscall.Open(dir, syscall.O_RDONLY, 0) |
| 30 | + if err != nil { |
| 31 | + return nil, err |
| 32 | + } |
| 33 | + defer syscall.Close(fd) |
| 34 | + // Collect syscall result in smartBuf. |
| 35 | + // "bytes.Buffer" is smart about expanding the capacity and avoids the |
| 36 | + // exponential runtime of simple append(). |
| 37 | + var smartBuf bytes.Buffer |
| 38 | + tmp := make([]byte, 10000) |
| 39 | + for { |
| 40 | + n, err := syscall.Getdents(fd, tmp) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + if n == 0 { |
| 45 | + break |
| 46 | + } |
| 47 | + smartBuf.Write(tmp[:n]) |
| 48 | + } |
| 49 | + // Make sure we have at least Sizeof(Dirent) of zeros after the last |
| 50 | + // entry. This prevents a cast to Dirent from reading past the buffer. |
| 51 | + smartBuf.Grow(sizeofDirent) |
| 52 | + buf := smartBuf.Bytes() |
| 53 | + // Count the number of directory entries in the buffer so we can allocate |
| 54 | + // a fuse.DirEntry slice of the correct size at once. |
| 55 | + var numEntries, offset int |
| 56 | + for offset < len(buf) { |
| 57 | + s := *(*syscall.Dirent)(unsafe.Pointer(&buf[offset])) |
| 58 | + if s.Reclen == 0 { |
| 59 | + tlog.Warn.Printf("Getdents: corrupt entry #%d: Reclen=0 at offset=%d. Returning EBADR", |
| 60 | + numEntries, offset) |
| 61 | + // EBADR = Invalid request descriptor |
| 62 | + return nil, syscall.EBADR |
| 63 | + } |
| 64 | + if int(s.Reclen) > sizeofDirent { |
| 65 | + tlog.Warn.Printf("Getdents: corrupt entry #%d: Reclen=%d > %d. Returning EBADR", |
| 66 | + numEntries, sizeofDirent, s.Reclen) |
| 67 | + return nil, syscall.EBADR |
| 68 | + } |
| 69 | + offset += int(s.Reclen) |
| 70 | + numEntries++ |
| 71 | + } |
| 72 | + // Parse the buffer into entries |
| 73 | + entries := make([]fuse.DirEntry, 0, numEntries) |
| 74 | + offset = 0 |
| 75 | + for offset < len(buf) { |
| 76 | + s := *(*syscall.Dirent)(unsafe.Pointer(&buf[offset])) |
| 77 | + name, err := getdentsName(s) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + offset += int(s.Reclen) |
| 82 | + if name == "." || name == ".." { |
| 83 | + // os.File.Readdir() drops "." and "..". Let's be compatible. |
| 84 | + continue |
| 85 | + } |
| 86 | + mode, err := convertDType(s.Type, dir+"/"+name) |
| 87 | + if err != nil { |
| 88 | + // The file may have been deleted in the meantime. Just skip it |
| 89 | + // and go on. |
| 90 | + continue |
| 91 | + } |
| 92 | + entries = append(entries, fuse.DirEntry{ |
| 93 | + Ino: s.Ino, |
| 94 | + Mode: mode, |
| 95 | + Name: name, |
| 96 | + }) |
| 97 | + } |
| 98 | + return entries, nil |
| 99 | +} |
| 100 | + |
| 101 | +// getdentsName extracts the filename from a Dirent struct and returns it as |
| 102 | +// a Go string. |
| 103 | +func getdentsName(s syscall.Dirent) (string, error) { |
| 104 | + // After the loop, l contains the index of the first '\0'. |
| 105 | + l := 0 |
| 106 | + for l = range s.Name { |
| 107 | + if s.Name[l] == 0 { |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + if l < 1 { |
| 112 | + tlog.Warn.Printf("Getdents: invalid name length l=%d. Returning EBADR", l) |
| 113 | + // EBADR = Invalid request descriptor |
| 114 | + return "", syscall.EBADR |
| 115 | + } |
| 116 | + // Copy to byte slice. |
| 117 | + name := make([]byte, l) |
| 118 | + for i := range name { |
| 119 | + name[i] = byte(s.Name[i]) |
| 120 | + } |
| 121 | + return string(name), nil |
| 122 | +} |
| 123 | + |
| 124 | +// convertDType converts a Dirent.Type to at Stat_t.Mode value. |
| 125 | +func convertDType(dtype uint8, file string) (uint32, error) { |
| 126 | + if dtype != syscall.DT_UNKNOWN { |
| 127 | + // Shift up by four octal digits = 12 bits |
| 128 | + return uint32(dtype) << 12, nil |
| 129 | + } |
| 130 | + // DT_UNKNOWN: we have to call Lstat() |
| 131 | + var st syscall.Stat_t |
| 132 | + err := syscall.Lstat(file, &st) |
| 133 | + if err != nil { |
| 134 | + return 0, err |
| 135 | + } |
| 136 | + // The S_IFMT bit mask extracts the file type from the mode. |
| 137 | + return st.Mode & syscall.S_IFMT, nil |
| 138 | +} |
0 commit comments