forked from willscott/go-nfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfs_onreaddirplus.go
172 lines (155 loc) · 4.95 KB
/
nfs_onreaddirplus.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
package nfs
import (
"bytes"
"context"
"encoding/binary"
"github.com/willscott/go-nfs-client/nfs/xdr"
)
type readDirPlusArgs struct {
Handle []byte
Cookie uint64
CookieVerif uint64
DirCount uint32
MaxCount uint32
}
type readDirPlusEntity struct {
FileID uint64
Name []byte
Cookie uint64
HasAttributes uint32
Attributes *FileAttribute
HasHandle uint32
Handle []byte
Next uint32
}
func onReadDirPlus(ctx context.Context, w *response, userHandle Handler) error {
w.errorFmt = opAttrErrorFormatter
obj := readDirPlusArgs{}
if err := xdr.Read(w.req.Body, &obj); 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 {
return &NFSStatusError{NFSStatusNotDir, err}
}
if obj.DirCount < 1024 || obj.MaxCount < 4096 {
return &NFSStatusError{NFSStatusTooSmall, nil}
}
entities := make([]readDirPlusEntity, 0)
dirBytes := uint32(0)
maxBytes := uint32(100) // conservative overhead measure
isFirstRead := (obj.Cookie == 0)
startIndex := 0
if !isFirstRead {
startIndex = int(obj.Cookie) - 2
}
i := startIndex
for ; i < len(contents); i++ {
content := contents[i]
dirBytes += uint32(len(content.Name()) + 20)
maxBytes += 512 // TODO: better estimation.
if dirBytes > obj.DirCount || maxBytes > obj.MaxCount || len(entities) > userHandle.HandleLimit()/2 {
break
}
handle := userHandle.ToHandle(fs, append(p, content.Name()))
attrs := ToFileAttribute(content)
attrs.Fileid = binary.BigEndian.Uint64(handle[0:8])
entities = append(entities, readDirPlusEntity{
FileID: binary.BigEndian.Uint64(handle[0:8]),
Name: []byte(content.Name()),
Cookie: uint64(i + 3),
HasAttributes: 1,
Attributes: attrs,
HasHandle: 1,
Handle: handle,
Next: 1,
})
}
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}
}
cookieVerifcation := [8]byte{}
if err := xdr.Write(writer, cookieVerifcation); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
if isFirstRead {
// 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(0)); err != nil { // hasAttribute
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint32(0)); err != nil { // hasHandle
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 err := xdr.Write(writer, uint32(0)); err != nil { // hasAttribute
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint32(0)); err != nil { // hasHandle
return &NFSStatusError{NFSStatusServerFault, err}
}
}
if len(entities) > 0 || isFirstRead {
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}
}
}
isEof := uint32(0)
if len(entities) == 0 || i == len(contents) {
isEof = 1
}
if err := xdr.Write(writer, isEof); 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
}