2024-03-28 13:09:42 +00:00
|
|
|
package nfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/willscott/go-nfs-client/nfs/xdr"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nfsReadArgs struct {
|
|
|
|
Handle []byte
|
|
|
|
Offset uint64
|
|
|
|
Count uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type nfsReadResponse struct {
|
|
|
|
Count uint32
|
|
|
|
EOF uint32
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxRead is the advertised largest buffer the server is willing to read
|
|
|
|
const MaxRead = 1 << 24
|
|
|
|
|
|
|
|
// CheckRead is a size where - if a request to read is larger than this,
|
|
|
|
// the server will stat the file to learn it's actual size before allocating
|
|
|
|
// a buffer to read into.
|
|
|
|
const CheckRead = 1 << 15
|
|
|
|
|
|
|
|
func onRead(ctx context.Context, w *response, userHandle Handler) error {
|
|
|
|
w.errorFmt = opAttrErrorFormatter
|
|
|
|
var obj nfsReadArgs
|
|
|
|
err := xdr.Read(w.req.Body, &obj)
|
|
|
|
if 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}
|
|
|
|
}
|
|
|
|
|
2024-06-19 07:50:22 +00:00
|
|
|
fh, err := fs.OpenFile(ctx, fs.Join(path...), os.O_RDONLY, 0)
|
2024-03-28 13:09:42 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return &NFSStatusError{NFSStatusNoEnt, 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{NFSStatusAccess, err}
|
|
|
|
}
|
2024-06-18 21:17:43 +00:00
|
|
|
defer fh.Close(ctx)
|
2024-03-28 13:09:42 +00:00
|
|
|
|
|
|
|
resp := nfsReadResponse{}
|
|
|
|
|
|
|
|
if obj.Count > CheckRead {
|
|
|
|
info, err := fs.Stat(ctx, fs.Join(path...))
|
|
|
|
if err != nil {
|
|
|
|
return &NFSStatusError{NFSStatusAccess, err}
|
|
|
|
}
|
|
|
|
if info.Size()-int64(obj.Offset) < int64(obj.Count) {
|
|
|
|
obj.Count = uint32(uint64(info.Size()) - obj.Offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if obj.Count > MaxRead {
|
|
|
|
obj.Count = MaxRead
|
|
|
|
}
|
|
|
|
resp.Data = make([]byte, obj.Count)
|
|
|
|
// todo: multiple reads if size isn't full
|
|
|
|
cnt, err := fh.ReadAt(ctx, resp.Data, int64(obj.Offset))
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return &NFSStatusError{NFSStatusIO, err}
|
|
|
|
}
|
|
|
|
resp.Count = uint32(cnt)
|
|
|
|
resp.Data = resp.Data[:resp.Count]
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
resp.EOF = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
writer := bytes.NewBuffer([]byte{})
|
|
|
|
if err := xdr.Write(writer, uint32(NFSStatusOk)); err != nil {
|
|
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
|
|
}
|
|
|
|
if err := WritePostOpAttrs(writer, tryStat(ctx, fs, path)); err != nil {
|
|
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := xdr.Write(writer, resp); err != nil {
|
|
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
|
|
}
|
|
|
|
if err := w.Write(writer.Bytes()); err != nil {
|
|
|
|
return &NFSStatusError{NFSStatusServerFault, err}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|