lib9p

Go 9P library.
Log | Files | Refs

commit c54bd7bd95c5e307ea841e1ad305380fadbacee6
parent 6bb0c54f45ad73569895e28fe3fcaf977839371c
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 10 Sep 2023 11:44:09 +0900

change ReadAt

Diffstat:
Mcmd/numfs.go | 42+++++++++++++++++-------------------------
1 file changed, 17 insertions(+), 25 deletions(-)

diff --git a/cmd/numfs.go b/cmd/numfs.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "flag" "fmt" "io" @@ -20,13 +21,12 @@ func init() { root = &numFile{ fs: fsys, id: -1, - text: "", - offset: 0, + reader: nil, children: []*numFile{ - &numFile{fsys, 0, "0\n", 0, nil}, - &numFile{fsys, 1, "1\n", 0, nil}, - &numFile{fsys, 2, "2\n", 0, nil}, - &numFile{fsys, 3, "3\n", 0, nil}, + &numFile{fsys, 0, bytes.NewReader([]byte("0\n")), nil}, + &numFile{fsys, 1, bytes.NewReader([]byte("1\n")), nil}, + &numFile{fsys, 2, bytes.NewReader([]byte("2\n")), nil}, + &numFile{fsys, 3, bytes.NewReader([]byte("3\n")), nil}, }, } } @@ -59,8 +59,7 @@ func (fsys *numFS) Open(name string) (lib9p.File, error) { type numFile struct { fs *numFS id int - text string - offset int + reader *bytes.Reader children []*numFile } @@ -95,7 +94,7 @@ func (f *numFile) Stat() (*lib9p.FileInfo, error) { if f.id == -1 { length = 0 } else { - length = int64(len(f.text)) + length = f.reader.Size() } stat.Length = length stat.Name = fmt.Sprintf("%d", f.id) @@ -109,16 +108,18 @@ func (f *numFile) Read(p []byte) (int, error) { if f.id == -1 { return 0, fmt.Errorf("is a directory") } - var n int - for n = 0; n < len(p) && n < len(f.text) - f.offset; n++ { - p[n] = f.text[f.offset + n] + return f.reader.Read(p) +} + +func (f *numFile) ReadAt(p []byte, off int64) (n int, err error) { + if f.id == -1 { + return 0, fmt.Errorf("is a directory") } - f.offset += n - return n, nil + return f.reader.ReadAt(p, off) } func (f *numFile) Close() error { - f.offset = 0 + f.reader.Seek(0, io.SeekStart) return nil } @@ -126,18 +127,9 @@ func (f *numFile) PathName() string { if f.id == -1 { return "." } - return f.text + return fmt.Sprintf("%d", f.id) } -func (f *numFile) ReadAt(p []byte, off int64) (n int, err error) { - if off >= int64(len(f.text)) { - return 0, io.EOF - } - for n = 0; n < len(p) && int64(n) < int64(len(f.text)) - off; n++ { - p[n] = f.text[off + int64(n)] - } - return n, nil -} func (f *numFile) Parent() (lib9p.File, error) { return root, nil }