tstor/pkg/go-nfs/nfs_onremove.go

86 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-03-28 13:09:42 +00:00
package nfs
import (
"bytes"
"context"
"errors"
"os"
"github.com/willscott/go-nfs-client/nfs/xdr"
)
func onRemove(ctx context.Context, w *response, userHandle Handler) error {
w.errorFmt = wccDataErrorFormatter
obj := DirOpArg{}
if err := xdr.Read(w.req.Body, &obj); err != nil {
return &NFSStatusError{NFSStatusInval, err}
}
2024-06-16 21:34:46 +00:00
fs, path, err := userHandle.FromHandle(ctx, obj.Handle)
2024-03-28 13:09:42 +00:00
if err != nil {
return &NFSStatusError{NFSStatusStale, err}
}
// TODO
// if !CapabilityCheck(fs, billy.WriteCapability) {
// return &NFSStatusError{NFSStatusROFS, os.ErrPermission}
// }
if len(string(obj.Filename)) > PathNameMax {
return &NFSStatusError{NFSStatusNameTooLong, nil}
}
fullPath := fs.Join(path...)
dirInfo, err := fs.Stat(ctx, fullPath)
if err != nil {
if os.IsNotExist(err) {
return &NFSStatusError{NFSStatusNoEnt, err}
}
if os.IsPermission(err) {
return &NFSStatusError{NFSStatusAccess, err}
}
if errors.Is(err, context.DeadlineExceeded) {
2024-04-06 13:51:17 +00:00
return &NFSStatusError{timeoutStatus, err}
2024-03-28 13:09:42 +00:00
}
return &NFSStatusError{NFSStatusIO, err}
}
if !dirInfo.IsDir() {
return &NFSStatusError{NFSStatusNotDir, nil}
}
preCacheData := ToFileAttribute(dirInfo, fullPath).AsCache()
toDelete := fs.Join(append(path, string(obj.Filename))...)
2024-06-16 21:34:46 +00:00
toDeleteHandle := userHandle.ToHandle(ctx, fs, append(path, string(obj.Filename)))
2024-03-28 13:09:42 +00:00
err = fs.Remove(ctx, toDelete)
if err != nil {
if os.IsNotExist(err) {
return &NFSStatusError{NFSStatusNoEnt, err}
}
if os.IsPermission(err) {
return &NFSStatusError{NFSStatusAccess, err}
}
if errors.Is(err, context.DeadlineExceeded) {
2024-04-06 13:51:17 +00:00
return &NFSStatusError{timeoutStatus, err}
2024-03-28 13:09:42 +00:00
}
return &NFSStatusError{NFSStatusIO, err}
}
2024-06-16 21:34:46 +00:00
if err := userHandle.InvalidateHandle(ctx, fs, toDeleteHandle); err != nil {
2024-03-28 13:09:42 +00:00
return &NFSStatusError{NFSStatusServerFault, err}
}
writer := bytes.NewBuffer([]byte{})
if err := xdr.Write(writer, uint32(NFSStatusOk)); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := WriteWcc(writer, preCacheData, tryStat(ctx, fs, path)); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
if err := w.Write(writer.Bytes()); err != nil {
return &NFSStatusError{NFSStatusServerFault, err}
}
return nil
}