|
| 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 | +constHaveGetdents=true |
| 21 | + |
| 22 | +constsizeofDirent=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 | +funcGetdents(dirstring) ([]fuse.DirEntry, error) { |
| 29 | +fd, err:=syscall.Open(dir, syscall.O_RDONLY, 0) |
| 30 | +iferr!=nil { |
| 31 | +returnnil, err |
| 32 | + } |
| 33 | +defersyscall.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 | +varsmartBuf bytes.Buffer |
| 38 | +tmp:=make([]byte, 10000) |
| 39 | +for { |
| 40 | +n, err:=syscall.Getdents(fd, tmp) |
| 41 | +iferr!=nil { |
| 42 | +returnnil, err |
| 43 | + } |
| 44 | +ifn==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 | +varnumEntries, offsetint |
| 56 | +foroffset<len(buf) { |
| 57 | +s:=*(*syscall.Dirent)(unsafe.Pointer(&buf[offset])) |
| 58 | +ifs.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 | +returnnil, syscall.EBADR |
| 63 | + } |
| 64 | +ifint(s.Reclen) >sizeofDirent { |
| 65 | +tlog.Warn.Printf("Getdents: corrupt entry #%d: Reclen=%d > %d. Returning EBADR", |
| 66 | +numEntries, sizeofDirent, s.Reclen) |
| 67 | +returnnil, 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 | +foroffset<len(buf) { |
| 76 | +s:=*(*syscall.Dirent)(unsafe.Pointer(&buf[offset])) |
| 77 | +name, err:=getdentsName(s) |
| 78 | +iferr!=nil { |
| 79 | +returnnil, err |
| 80 | + } |
| 81 | +offset+=int(s.Reclen) |
| 82 | +ifname=="."||name==".." { |
| 83 | +// os.File.Readdir() drops "." and "..". Let's be compatible. |
| 84 | +continue |
| 85 | + } |
| 86 | +mode, err:=convertDType(s.Type, dir+"/"+name) |
| 87 | +iferr!=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 | +returnentries, nil |
| 99 | +} |
| 100 | + |
| 101 | +// getdentsName extracts the filename from a Dirent struct and returns it as |
| 102 | +// a Go string. |
| 103 | +funcgetdentsName(s syscall.Dirent) (string, error) { |
| 104 | +// After the loop, l contains the index of the first '\0'. |
| 105 | +l:=0 |
| 106 | +forl=ranges.Name { |
| 107 | +ifs.Name[l] ==0 { |
| 108 | +break |
| 109 | + } |
| 110 | + } |
| 111 | +ifl<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 | +fori:=rangename { |
| 119 | +name[i] =byte(s.Name[i]) |
| 120 | + } |
| 121 | +returnstring(name), nil |
| 122 | +} |
| 123 | + |
| 124 | +// convertDType converts a Dirent.Type to at Stat_t.Mode value. |
| 125 | +funcconvertDType(dtypeuint8, filestring) (uint32, error) { |
| 126 | +ifdtype!=syscall.DT_UNKNOWN { |
| 127 | +// Shift up by four octal digits = 12 bits |
| 128 | +returnuint32(dtype) <<12, nil |
| 129 | + } |
| 130 | +// DT_UNKNOWN: we have to call Lstat() |
| 131 | +varst syscall.Stat_t |
| 132 | +err:=syscall.Lstat(file, &st) |
| 133 | +iferr!=nil { |
| 134 | +return0, err |
| 135 | + } |
| 136 | +// The S_IFMT bit mask extracts the file type from the mode. |
| 137 | +returnst.Mode&syscall.S_IFMT, nil |
| 138 | +} |
0 commit comments