Commit 5d921aae authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Add and use ReadOnlyFile.

parent d01aaf4d
...@@ -175,10 +175,8 @@ type ZipFile struct { ...@@ -175,10 +175,8 @@ type ZipFile struct {
fuse.DefaultRawFuseFile fuse.DefaultRawFuseFile
} }
func NewZipFile(f *zip.File) *ZipFile { func NewZipFile(f *zip.File) fuse.RawFuseFile {
z := ZipFile{ data := make([]byte, f.UncompressedSize)
data: make([]byte, f.UncompressedSize),
}
rc, err := f.Open() rc, err := f.Open()
if err != nil { if err != nil {
panic("zip open") panic("zip open")
...@@ -186,25 +184,14 @@ func NewZipFile(f *zip.File) *ZipFile { ...@@ -186,25 +184,14 @@ func NewZipFile(f *zip.File) *ZipFile {
start := 0 start := 0
for { for {
n, err := rc.Read(z.data[start:]) n, err := rc.Read(data[start:])
start += n start += n
if err == os.EOF { if err == os.EOF {
break break
} }
if err != nil && err != os.EOF { if err != nil && err != os.EOF {
panic(fmt.Sprintf("read err: %v, n %v, sz %v", err, n, len(z.data))) panic(fmt.Sprintf("read err: %v, n %v, sz %v", err, n, len(data)))
} }
} }
return &z return fuse.NewReadOnlyFile(data)
} }
func (self *ZipFile) Read(input *fuse.ReadIn, bp *fuse.BufferPool) ([]byte, fuse.Status) {
end := int(input.Offset) + int(input.Size)
if end > len(self.data) {
end = len(self.data)
}
return self.data[input.Offset:end], fuse.OK
}
...@@ -11,7 +11,8 @@ GOFILES=misc.go\ ...@@ -11,7 +11,8 @@ GOFILES=misc.go\
types.go\ types.go\
pathfilesystem.go \ pathfilesystem.go \
bufferpool.go \ bufferpool.go \
default.go default.go \
datafile.go
include $(GOROOT)/src/Make.pkg include $(GOROOT)/src/Make.pkg
package fuse
// A FUSE file for read-only filesystems. This assumes we already have the data in memory.
type ReadOnlyFile struct {
data []byte
DefaultRawFuseFile
}
func NewReadOnlyFile(data []byte) *ReadOnlyFile {
f := new(ReadOnlyFile)
f.data = data
return f
}
func (self *ReadOnlyFile) Read(input *ReadIn, bp *BufferPool) ([]byte, Status) {
end := int(input.Offset) + int(input.Size)
if end > len(self.data) {
end = len(self.data)
}
return self.data[input.Offset:end], OK
}
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