small refactor*
This commit is contained in:
parent
b6b541e050
commit
24a4d30275
232 changed files with 2164 additions and 1906 deletions
server/pkg/go-nfs
86
server/pkg/go-nfs/nfs_onlookup.go
Normal file
86
server/pkg/go-nfs/nfs_onlookup.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package nfs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/willscott/go-nfs-client/nfs/xdr"
|
||||
)
|
||||
|
||||
func lookupSuccessResponse(ctx context.Context, handle []byte, entPath, dirPath []string, fs Filesystem) ([]byte, error) {
|
||||
writer := bytes.NewBuffer([]byte{})
|
||||
if err := xdr.Write(writer, uint32(NFSStatusOk)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := xdr.Write(writer, handle); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := WritePostOpAttrs(writer, tryStat(ctx, fs, entPath)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := WritePostOpAttrs(writer, tryStat(ctx, fs, dirPath)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return writer.Bytes(), nil
|
||||
}
|
||||
|
||||
func onLookup(ctx context.Context, w *response, userHandle Handler) error {
|
||||
w.errorFmt = opAttrErrorFormatter
|
||||
obj := DirOpArg{}
|
||||
err := xdr.Read(w.req.Body, &obj)
|
||||
if err != nil {
|
||||
return &NFSStatusError{NFSStatusInval, err}
|
||||
}
|
||||
|
||||
fs, p, err := userHandle.FromHandle(ctx, obj.Handle)
|
||||
if err != nil {
|
||||
return &NFSStatusError{NFSStatusStale, err}
|
||||
}
|
||||
dirInfo, err := fs.Lstat(ctx, fs.Join(p...))
|
||||
if err != nil || !dirInfo.IsDir() {
|
||||
return &NFSStatusError{NFSStatusNotDir, err}
|
||||
}
|
||||
|
||||
// Special cases for "." and ".."
|
||||
if bytes.Equal(obj.Filename, []byte(".")) {
|
||||
resp, err := lookupSuccessResponse(ctx, obj.Handle, p, p, fs)
|
||||
if err != nil {
|
||||
return &NFSStatusError{NFSStatusServerFault, err}
|
||||
}
|
||||
if err := w.Write(resp); err != nil {
|
||||
return &NFSStatusError{NFSStatusServerFault, err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if bytes.Equal(obj.Filename, []byte("..")) {
|
||||
if len(p) == 0 {
|
||||
return &NFSStatusError{NFSStatusAccess, os.ErrPermission}
|
||||
}
|
||||
pPath := p[0 : len(p)-1]
|
||||
pHandle := userHandle.ToHandle(ctx, fs, pPath)
|
||||
resp, err := lookupSuccessResponse(ctx, pHandle, pPath, p, fs)
|
||||
if err != nil {
|
||||
return &NFSStatusError{NFSStatusServerFault, err}
|
||||
}
|
||||
if err := w.Write(resp); err != nil {
|
||||
return &NFSStatusError{NFSStatusServerFault, err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
reqPath := append(p, string(obj.Filename))
|
||||
if _, err = fs.Lstat(ctx, fs.Join(reqPath...)); err != nil {
|
||||
return &NFSStatusError{NFSStatusNoEnt, os.ErrNotExist}
|
||||
}
|
||||
|
||||
newHandle := userHandle.ToHandle(ctx, fs, reqPath)
|
||||
resp, err := lookupSuccessResponse(ctx, newHandle, reqPath, p, fs)
|
||||
if err != nil {
|
||||
return &NFSStatusError{NFSStatusServerFault, err}
|
||||
}
|
||||
if err := w.Write(resp); err != nil {
|
||||
return &NFSStatusError{NFSStatusServerFault, err}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue