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