commit 6bb0c54f45ad73569895e28fe3fcaf977839371c
parent 2d7950f629c4fc57175786e7b16817dba607ef3f
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 10 Sep 2023 08:08:47 +0900
fix bug for ReadAt
Diffstat:
1 file changed, 25 insertions(+), 7 deletions(-)
diff --git a/cmd/numfs.go b/cmd/numfs.go
@@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
+ "io"
"io/fs"
"log"
"net"
@@ -20,11 +21,12 @@ func init() {
fs: fsys,
id: -1,
text: "",
+ offset: 0,
children: []*numFile{
- &numFile{fsys, 0, "0", nil},
- &numFile{fsys, 1, "1", nil},
- &numFile{fsys, 2, "2", nil},
- &numFile{fsys, 3, "3", nil},
+ &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},
},
}
}
@@ -58,6 +60,7 @@ type numFile struct {
fs *numFS
id int
text string
+ offset int
children []*numFile
}
@@ -107,12 +110,17 @@ func (f *numFile) Read(p []byte) (int, error) {
return 0, fmt.Errorf("is a directory")
}
var n int
- for n = 0; n < len(p) && n < len(f.text); n++ {
- p[n] = f.text[n]
+ for n = 0; n < len(p) && n < len(f.text) - f.offset; n++ {
+ p[n] = f.text[f.offset + n]
}
+ f.offset += n
return n, nil
}
-func (f *numFile) Close() error { return nil }
+
+func (f *numFile) Close() error {
+ f.offset = 0
+ return nil
+}
func (f *numFile) PathName() string {
if f.id == -1 {
@@ -121,6 +129,16 @@ func (f *numFile) PathName() string {
return f.text
}
+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 }
func (f *numFile) Child() ([]lib9p.File, error) {