Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

off-by-one issues with dir cookies #29

Merged
merged 2 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions helpers/cachinghandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (c *CachingHandler) FromHandle(fh []byte) (billy.Filesystem, []string, erro

if cache, ok := c.activeHandles.Get(id); ok {
f, ok := cache.(entry)
for _, k := range c.activeHandles.Keys() {
e, _ := c.activeHandles.Peek(k)
candidate := e.(entry)
if hasPrefix(f.p, candidate.p) {
_, _ = c.activeHandles.Get(k)
}
}
if ok {
return f.f, f.p, nil
}
Expand All @@ -60,3 +67,15 @@ func (c *CachingHandler) FromHandle(fh []byte) (billy.Filesystem, []string, erro
func (c *CachingHandler) HandleLimit() int {
return c.cacheLimit
}

func hasPrefix(path, prefix []string) bool {
if len(prefix) > len(path) {
return false
}
for i, e := range prefix {
if path[i] != e {
return false
}
}
return true
}
9 changes: 5 additions & 4 deletions nfs_onreaddir.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ func onReadDir(ctx context.Context, w *response, userHandle Handler) error {
vHash := sha256.New()

for i, c := range contents {
actualI := i + 2
if started {
entities = append(entities, readDirEntity{
FileID: 1337, //todo: does this matter?
Name: []byte(c.Name()),
Cookie: uint64(i + 3),
Cookie: uint64(actualI),
Next: 1,
})
maxBytes += 512 // TODO: better estimation.
} else if uint64(i) == obj.Cookie {
} else if uint64(actualI) == obj.Cookie {
started = true
everStarted = true
}
Expand Down Expand Up @@ -119,7 +120,7 @@ func onReadDir(ctx context.Context, w *response, userHandle Handler) error {
if err := xdr.Write(writer, []byte(".")); err != nil { // name
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(1)); err != nil { // cookie
if err := xdr.Write(writer, uint64(0)); err != nil { // cookie
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint32(1)); err != nil { // next
Expand All @@ -138,7 +139,7 @@ func onReadDir(ctx context.Context, w *response, userHandle Handler) error {
if err := xdr.Write(writer, []byte("..")); err != nil { //name
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(2)); err != nil { // cookie
if err := xdr.Write(writer, uint64(1)); err != nil { // cookie
return &NFSStatusError{NFSStatusServerFault, err}
}
}
Expand Down
10 changes: 6 additions & 4 deletions nfs_onreaddirplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ func onReadDirPlus(ctx context.Context, w *response, userHandle Handler) error {
vHash := sha256.New()

for i, c := range contents {
// index of contents doesn't include '.' and '..'
actualI := i + 2
if started {
handle := userHandle.ToHandle(fs, append(p, c.Name()))
attrs := ToFileAttribute(c)
attrs.Fileid = binary.BigEndian.Uint64(handle[0:8])
entities = append(entities, readDirPlusEntity{
FileID: binary.BigEndian.Uint64(handle[0:8]),
Name: []byte(c.Name()),
Cookie: uint64(i + 3),
Cookie: uint64(actualI),
HasAttributes: 1,
Attributes: attrs,
HasHandle: 1,
Expand All @@ -83,7 +85,7 @@ func onReadDirPlus(ctx context.Context, w *response, userHandle Handler) error {
})
dirBytes += uint32(len(c.Name()) + 20)
maxBytes += 512 // TODO: better estimation.
} else if uint64(i) == obj.Cookie {
} else if uint64(actualI) == obj.Cookie {
started = true
everStarted = true
}
Expand Down Expand Up @@ -127,7 +129,7 @@ func onReadDirPlus(ctx context.Context, w *response, userHandle Handler) error {
if err := xdr.Write(writer, []byte(".")); err != nil { // name
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(1)); err != nil { // cookie
if err := xdr.Write(writer, uint64(0)); err != nil { // cookie
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint32(0)); err != nil { // hasAttribute
Expand All @@ -152,7 +154,7 @@ func onReadDirPlus(ctx context.Context, w *response, userHandle Handler) error {
if err := xdr.Write(writer, []byte("..")); err != nil { //name
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint64(2)); err != nil { // cookie
if err := xdr.Write(writer, uint64(1)); err != nil { // cookie
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := xdr.Write(writer, uint32(0)); err != nil { // hasAttribute
Expand Down