Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement io.ReadAt for File #7

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions nfs/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright © 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
package nfs

import (
Expand All @@ -13,6 +12,12 @@ import (
"github.com/vmware/go-nfs-client/nfs/xdr"
)

var (
_ io.ReadWriteSeeker = &File{}
_ io.Closer = &File{}
_ io.ReaderAt = &File{}
)

// File wraps the NfsProc3Read and NfsProc3Write methods to implement a
// io.ReadWriteCloser.
type File struct {
Expand Down Expand Up @@ -68,6 +73,25 @@ func (f *File) Readlink() (string, error) {
}

func (f *File) Read(p []byte) (int, error) {
n, err := f.readAt(p, int64(f.curr))
if err == nil {
f.curr += uint64(n)
}
return n, err
}

func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
n, err = f.readAt(p, int64(f.curr))
if err != nil {
return
}
if n < len(p) {
err = io.EOF
}
return
}

func (f *File) readAt(p []byte, off int64) (n int, err error) {
type ReadArgs struct {
rpc.Header
FH []byte
Expand All @@ -84,8 +108,8 @@ func (f *File) Read(p []byte) (int, error) {
}
}

readSize := min(f.fsinfo.RTPref, uint32(len(p)))
util.Debugf("read(%x) len=%d offset=%d", f.fh, readSize, f.curr)
readSize := min(f.fsinfo.RTMax, uint32(len(p)))
util.Debugf("read(%x) len=%d offset=%d", f.fh, readSize, off)

r, err := f.call(&ReadArgs{
Header: rpc.Header{
Expand All @@ -97,7 +121,7 @@ func (f *File) Read(p []byte) (int, error) {
Verf: rpc.AuthNull,
},
FH: f.fh,
Offset: uint64(f.curr),
Offset: uint64(off),
Count: readSize,
})

Expand All @@ -111,16 +135,14 @@ func (f *File) Read(p []byte) (int, error) {
return 0, err
}

f.curr = f.curr + uint64(readres.Data.Length)
n, err := r.Read(p[:readres.Data.Length])
n, err = r.Read(p[:readres.Data.Length])
if err != nil {
return n, err
}

if readres.EOF != 0 {
err = io.EOF
}

return n, err
}

Expand Down