royalcat
bc4b39b1c1
Some checks failed
docker / build-docker (linux/amd64) (push) Successful in 2m36s
docker / build-docker (linux/386) (push) Successful in 2m45s
docker / build-docker (linux/arm/v7) (push) Has been cancelled
docker / build-docker (linux/arm64/v8) (push) Has been cancelled
docker / build-docker (linux/arm64) (push) Has been cancelled
120 lines
3.3 KiB
Go
120 lines
3.3 KiB
Go
package nfs
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"reflect"
|
|
|
|
"github.com/go-git/go-billy/v5"
|
|
"github.com/willscott/go-nfs-client/nfs/xdr"
|
|
)
|
|
|
|
var doubleWccErrorBody = [16]byte{}
|
|
|
|
func onRename(ctx context.Context, w *response, userHandle Handler) error {
|
|
w.errorFmt = errFormatterWithBody(doubleWccErrorBody[:])
|
|
from := DirOpArg{}
|
|
err := xdr.Read(w.req.Body, &from)
|
|
if err != nil {
|
|
return &NFSStatusError{NFSStatusInval, err}
|
|
}
|
|
fs, fromPath, err := userHandle.FromHandle(ctx, from.Handle)
|
|
if err != nil {
|
|
return &NFSStatusError{NFSStatusStale, err}
|
|
}
|
|
|
|
to := DirOpArg{}
|
|
if err = xdr.Read(w.req.Body, &to); err != nil {
|
|
return &NFSStatusError{NFSStatusInval, err}
|
|
}
|
|
fs2, toPath, err := userHandle.FromHandle(ctx, to.Handle)
|
|
if err != nil {
|
|
return &NFSStatusError{NFSStatusStale, err}
|
|
}
|
|
// check the two fs are the same
|
|
if !reflect.DeepEqual(fs, fs2) {
|
|
return &NFSStatusError{NFSStatusNotSupp, os.ErrPermission}
|
|
}
|
|
|
|
if !CapabilityCheck(fs, billy.WriteCapability) {
|
|
return &NFSStatusError{NFSStatusROFS, os.ErrPermission}
|
|
}
|
|
|
|
if len(string(from.Filename)) > PathNameMax || len(string(to.Filename)) > PathNameMax {
|
|
return &NFSStatusError{NFSStatusNameTooLong, os.ErrInvalid}
|
|
}
|
|
|
|
fromDirPath := fs.Join(fromPath...)
|
|
fromDirInfo, err := fs.Stat(ctx, fromDirPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return &NFSStatusError{NFSStatusNoEnt, err}
|
|
}
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
return &NFSStatusError{timeoutStatus, err}
|
|
}
|
|
return &NFSStatusError{NFSStatusIO, err}
|
|
}
|
|
if !fromDirInfo.IsDir() {
|
|
return &NFSStatusError{NFSStatusNotDir, nil}
|
|
}
|
|
preCacheData := ToFileAttribute(fromDirInfo, fromDirPath).AsCache()
|
|
|
|
toDirPath := fs.Join(toPath...)
|
|
toDirInfo, err := fs.Stat(ctx, toDirPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return &NFSStatusError{NFSStatusNoEnt, err}
|
|
}
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
return &NFSStatusError{timeoutStatus, err}
|
|
}
|
|
return &NFSStatusError{NFSStatusIO, err}
|
|
}
|
|
if !toDirInfo.IsDir() {
|
|
return &NFSStatusError{NFSStatusNotDir, nil}
|
|
}
|
|
preDestData := ToFileAttribute(toDirInfo, toDirPath).AsCache()
|
|
|
|
oldHandle := userHandle.ToHandle(ctx, fs, append(fromPath, string(from.Filename)))
|
|
|
|
fromLoc := fs.Join(append(fromPath, string(from.Filename))...)
|
|
toLoc := fs.Join(append(toPath, string(to.Filename))...)
|
|
|
|
err = fs.Rename(ctx, fromLoc, toLoc)
|
|
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) {
|
|
return &NFSStatusError{timeoutStatus, err}
|
|
}
|
|
return &NFSStatusError{NFSStatusIO, err}
|
|
}
|
|
|
|
if err := userHandle.InvalidateHandle(ctx, fs, oldHandle); err != nil {
|
|
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, fromPath)); err != nil {
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
}
|
|
if err := WriteWcc(writer, preDestData, tryStat(ctx, fs, toPath)); err != nil {
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
}
|
|
|
|
if err := w.Write(writer.Bytes()); err != nil {
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
}
|
|
return nil
|
|
}
|