forked from willscott/go-nfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfs_onreaddir.go
174 lines (156 loc) · 4.85 KB
/
nfs_onreaddir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package nfs
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"io"
"os"
"sort"
"github.com/willscott/go-nfs-client/nfs/xdr"
)
type readDirArgs struct {
Handle []byte
Cookie uint64
CookieVerif uint64
Count uint32
}
type readDirEntity struct {
FileID uint64
Name []byte
Cookie uint64
Next uint32
}
func onReadDir(ctx context.Context, w *response, userHandle Handler) error {
w.errorFmt = opAttrErrorFormatter
obj := readDirArgs{}
err := xdr.Read(w.req.Body, &obj)
if err != nil {
return &NFSStatusError{NFSStatusInval, err}
}
fs, p, err := userHandle.FromHandle(obj.Handle)
if err != nil {
return &NFSStatusError{NFSStatusStale, err}
}
contents, err := fs.ReadDir(fs.Join(p...))
if err != nil {
if os.IsPermission(err) {
return &NFSStatusError{NFSStatusAccess, err}
}
return &NFSStatusError{NFSStatusNotDir, err}
}
sort.Slice(contents, func(i, j int) bool {
return contents[i].Name() < contents[j].Name()
})
if obj.Count < 1024 {
return &NFSStatusError{NFSStatusTooSmall, io.ErrShortBuffer}
}
entities := make([]readDirEntity, 0)
maxBytes := uint32(100) // conservative overhead measure
started := (obj.Cookie == 0)
everStarted := started
//calculate the cookieverifier for this read-dir exercise.
//Note: this is an inefficient way to do this for large directories where
//paging actually occurs. however, the billy interface doesn't expose the
//granularity to do better, either.
vHash := sha256.New()
for i, c := range contents {
if started {
entities = append(entities, readDirEntity{
FileID: 1337, //todo: does this matter?
Name: []byte(c.Name()),
Cookie: uint64(i + 3),
Next: 1,
})
maxBytes += 512 // TODO: better estimation.
} else if uint64(i) == obj.Cookie {
started = true
everStarted = true
}
if started && (maxBytes > obj.Count || len(entities) > userHandle.HandleLimit()/2) {
started = false
entities = entities[0 : len(entities)-1]
}
if _, err := vHash.Write([]byte(c.Name())); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
}
verif := vHash.Sum([]byte{})[0:8]
if obj.Cookie != 0 && (binary.BigEndian.Uint64(verif) != obj.CookieVerif || !everStarted) {
return &NFSStatusError{NFSStatusBadCookie, os.ErrInvalid}
}
writer := bytes.NewBuffer([]byte{})
if err := xdr.Write(writer, uint32(NFSStatusOk)); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := WritePostOpAttrs(writer, tryStat(fs, p)); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
var fixedVerif [8]byte
copy(fixedVerif[:], verif)
if err := xdr.Write(writer, fixedVerif); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
if obj.Cookie == 0 {
// prefix the special "." and ".." entries.
if err := xdr.Write(writer, uint32(1)); err != nil { //next
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(binary.BigEndian.Uint64(obj.Handle[0:8]))); err != nil { //fileID
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, []byte(".")); err != nil { // name
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(1)); err != nil { // cookie
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint32(1)); err != nil { // next
return &NFSStatusError{NFSStatusServerFault, err}
}
if len(p) > 0 {
ph := userHandle.ToHandle(fs, p[0:len(p)-1])
if err := xdr.Write(writer, uint64(binary.BigEndian.Uint64(ph[0:8]))); err != nil { //fileID
return &NFSStatusError{NFSStatusServerFault, err}
}
} else {
if err := xdr.Write(writer, uint64(0)); err != nil { //fileID
return &NFSStatusError{NFSStatusServerFault, err}
}
}
if err := xdr.Write(writer, []byte("..")); err != nil { //name
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(2)); err != nil { // cookie
return &NFSStatusError{NFSStatusServerFault, err}
}
}
if len(entities) > 0 || obj.Cookie == 0 {
if err := xdr.Write(writer, uint32(1)); err != nil { // next
return &NFSStatusError{NFSStatusServerFault, err}
}
}
if len(entities) > 0 {
entities[len(entities)-1].Next = 0
// the 'yes there is a 1st entity' bool
}
for _, e := range entities {
if err := xdr.Write(writer, e); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
}
if started || len(entities) == 0 {
if err := xdr.Write(writer, uint32(1)); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
} else {
if err := xdr.Write(writer, uint32(0)); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
}
// TODO: track writer size at this point to validate maxcount estimation and stop early if needed.
if err := w.Write(writer.Bytes()); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
return nil
}