Skip to content

Commit

Permalink
Merge pull request #29 from willscott/feat/dirpage
Browse files Browse the repository at this point in the history
off-by-one issues with dir cookies
  • Loading branch information
willscott committed Mar 21, 2021
2 parents bc34151 + 3ba0536 commit 08e2b9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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

0 comments on commit 08e2b9d

Please sign in to comment.