Commit c4df48de authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Have Read() return Status separately.

parent fa85dd80
......@@ -161,7 +161,7 @@ type File interface {
// the inner file here.
InnerFile() File
Read(dest []byte, off int64) ReadResult
Read(dest []byte, off int64) (ReadResult, Status)
Write(data []byte, off int64) (written uint32, code Status)
Flush() Status
Release()
......@@ -281,7 +281,7 @@ type RawFileSystem interface {
// File handling.
Create(out *raw.CreateOut, header *raw.InHeader, input *raw.CreateIn, name string) (code Status)
Open(out *raw.OpenOut, header *raw.InHeader, input *raw.OpenIn) (status Status)
Read(*raw.InHeader, *raw.ReadIn, []byte) ReadResult
Read(*raw.InHeader, *raw.ReadIn, []byte) (ReadResult, Status)
Release(header *raw.InHeader, input *raw.ReleaseIn)
Write(*raw.InHeader, *raw.WriteIn, []byte) (written uint32, code Status)
......
......@@ -27,9 +27,9 @@ func CopyFile(srcFs, destFs FileSystem, srcFile, destFile string, context *Conte
buf := make([]byte, 128*(1<<10))
off := int64(0)
for {
res := src.Read(buf, off)
if !res.Ok() {
return res.Status
res, code := src.Read(buf, off)
if !code.Ok() {
return code
}
res.Read(buf)
......
......@@ -21,8 +21,8 @@ func (f *DefaultFile) String() string {
return "DefaultFile"
}
func (f *DefaultFile) Read(buf []byte, off int64) ReadResult {
return ReadResult{Status: ENOSYS}
func (f *DefaultFile) Read(buf []byte, off int64) (ReadResult, Status) {
return ReadResult{}, ENOSYS
}
func (f *DefaultFile) Write(data []byte, off int64) (uint32, Status) {
......
......@@ -96,8 +96,8 @@ func (fs *DefaultRawFileSystem) OpenDir(out *raw.OpenOut, header *raw.InHeader,
return ENOSYS
}
func (fs *DefaultRawFileSystem) Read(header *raw.InHeader, input *raw.ReadIn, buf []byte) ReadResult {
return ReadResult{}
func (fs *DefaultRawFileSystem) Read(header *raw.InHeader, input *raw.ReadIn, buf []byte) (ReadResult, Status) {
return ReadResult{}, ENOSYS
}
func (fs *DefaultRawFileSystem) Release(header *raw.InHeader, input *raw.ReleaseIn) {
......
......@@ -39,15 +39,14 @@ func NewDataFile(data []byte) *DataFile {
return f
}
func (f *DataFile) Read(buf []byte, off int64) (res ReadResult) {
func (f *DataFile) Read(buf []byte, off int64) (res ReadResult, code Status) {
end := int(off) + int(len(buf))
if end > len(f.data) {
end = len(f.data)
}
res.Data = f.data[off:end]
res.Status = OK
return res
return res, OK
}
////////////////
......@@ -67,8 +66,8 @@ func (f *DevNullFile) String() string {
return "DevNullFile"
}
func (f *DevNullFile) Read(buf []byte, off int64) ReadResult {
return ReadResult{}
func (f *DevNullFile) Read(buf []byte, off int64) (ReadResult, Status) {
return ReadResult{}, OK
}
func (f *DevNullFile) Write(content []byte, off int64) (uint32, Status) {
......@@ -100,13 +99,12 @@ func (f *LoopbackFile) String() string {
return fmt.Sprintf("LoopbackFile(%s)", f.File.Name())
}
func (f *LoopbackFile) Read(buf []byte, off int64) (res ReadResult) {
func (f *LoopbackFile) Read(buf []byte, off int64) (res ReadResult, code Status) {
return ReadResult{
Fd: f.File.Fd(),
FdOff: off,
FdSize: len(buf),
Status: OK,
}
}, OK
}
func (f *LoopbackFile) Write(data []byte, off int64) (uint32, Status) {
......
......@@ -22,13 +22,13 @@ func (f *MutableDataFile) String() string {
return "MutableDataFile"
}
func (f *MutableDataFile) Read(buf []byte, off int64) ReadResult {
func (f *MutableDataFile) Read(buf []byte, off int64) (ReadResult, Status) {
end := int(off)+len(buf)
if end > len(f.data) {
end = len(f.data)
}
return ReadResult{Data: f.data[off:end]}
return ReadResult{Data: f.data[off:end]}, OK
}
func (f *MutableDataFile) Write(d []byte, off int64) (uint32, Status) {
......
......@@ -350,7 +350,7 @@ func (c *FileSystemConnector) Write(header *raw.InHeader, input *raw.WriteIn, da
return opened.WithFlags.File.Write(data, int64(input.Offset))
}
func (c *FileSystemConnector) Read(header *raw.InHeader, input *raw.ReadIn, buf []byte) ReadResult {
func (c *FileSystemConnector) Read(header *raw.InHeader, input *raw.ReadIn, buf []byte) (ReadResult, Status) {
node := c.toInode(header.NodeId)
opened := node.mount.getOpenedFile(input.Fh)
......
......@@ -16,6 +16,8 @@ type LockingFileSystem struct {
lock sync.Mutex
}
var _ = ((FileSystem)((*LockingFileSystem)(nil)))
func NewLockingFileSystem(pfs FileSystem) *LockingFileSystem {
l := new(LockingFileSystem)
l.FileSystem = pfs
......@@ -276,7 +278,7 @@ func (fs *LockingRawFileSystem) ReleaseDir(header *raw.InHeader, h *raw.ReleaseI
fs.RawFileSystem.ReleaseDir(header, h)
}
func (fs *LockingRawFileSystem) Read(header *raw.InHeader, input *raw.ReadIn, buf []byte) ReadResult {
func (fs *LockingRawFileSystem) Read(header *raw.InHeader, input *raw.ReadIn, buf []byte) (ReadResult, Status) {
defer fs.locked()()
return fs.RawFileSystem.Read(header, input, buf)
}
......
......@@ -269,9 +269,7 @@ func doLink(state *MountState, req *request) {
func doRead(state *MountState, req *request) {
in := (*raw.ReadIn)(req.inData)
buf := state.AllocOut(req, in.Size)
res := state.fileSystem.Read(req.inHeader, in, buf)
req.flatData = res
req.status = res.Status
req.flatData, req.status = state.fileSystem.Read(req.inHeader, in, buf)
}
func doFlush(state *MountState, req *request) {
......
......@@ -14,9 +14,6 @@ import (
// If at any point, the raw data is needed, ReadResult.Read() will
// load the raw data into the Data member.
type ReadResult struct {
// Errno code for the read.
Status
// Raw bytes for the read.
Data []byte
......@@ -46,11 +43,10 @@ func (r *ReadResult) Size() int {
// Reads raw bytes from file descriptor if necessary, using the passed
// buffer as storage.
func (r *ReadResult) Read(buf []byte) Status {
if r.Data != nil || !r.Ok() {
return r.Status
if r.Data != nil {
return OK
}
if len(buf) < r.FdSize {
r.Status = ERANGE
return ERANGE
}
......@@ -58,13 +54,13 @@ func (r *ReadResult) Read(buf []byte) Status {
if err == io.EOF {
err = nil
}
r.Status = ToStatus(err)
if r.Ok() {
code := ToStatus(err)
if code.Ok() {
r.Data = buf[:n]
}
r.Fd = 0
r.FdOff = 0
r.FdSize = 0
return r.Status
return code
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment