lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit b92b8fa19b10f8c0889dff7b995622bb8daf3ae3
parent 46e4a3d74846eac124148a35918ad2cd55221a86
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 19 Jan 2024 15:44:10 +0900

add client.File.Write

Diffstat:
Mclient/file.go | 37+++++++++++++++++++++++++++++++++----
Mclient/file_test.go | 28++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/client/file.go b/client/file.go @@ -88,11 +88,40 @@ func (cf *File) Read(b []byte) (int, error) { } } -/* -func (cf *File) Write([]byte) (int, error) { - +func (cf *File) Write(b []byte) (int, error) { + if cf.fid.omode == -1 { + return 0, errors.New("not open") + } + if len(b) == 0 { + return 0, nil + } + if cf.fid.omode&3 != lib9p.OWRITE && cf.fid.omode&3 != lib9p.ORDWR { + return 0, lib9p.ErrPerm + } + count := len(b) + cur := 0 + for count > 0 { + var c uint32 + if uint32(count) > cf.iounit { + c = cf.iounit + } else { + c = uint32(count) + } + tag, err := cf.fs.tPool.add() + if err != nil { + return 0, err + } + n, err := cf.fs.c.Write(tag, cf.fid.fid, cf.fid.offset, c, b[cur:cur+int(c)]) + cf.fs.tPool.delete(tag) + cf.fid.offset += uint64(n) + cur += int(n) + if err != nil { + return cur, err + } + count -= int(c) + } + return cur, nil } -*/ func (cf *File) ReadDir(n int) ([]fs.DirEntry, error) { if cf.qid.Type&lib9p.QTDIR == 0 { diff --git a/client/file_test.go b/client/file_test.go @@ -144,6 +144,34 @@ func TestFileRead(t *testing.T) { } } +func TestFileWrite(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cfs, err := mount(ctx, testfs) + if err != nil { + t.Fatal(err) + } + defer cancel() + tf, err := testfs.walkPath("dir/file") + if err != nil { + t.Fatal(err) + } + orig := bytes.Clone(tf.content) + cf, err := cfs.OpenFile("dir/file", lib9p.O_RDWR) + if err != nil { + t.Fatal(err) + } + ctnt := []byte("fuga") + _, err = cf.(lib9p.WriterFile).Write(ctnt) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(tf.content[:len(ctnt)], ctnt) { + t.Errorf("not written propperly: want: %v, got: %v", + string(ctnt), string(tf.content[:len(ctnt)])) + } + tf.content = orig +} + // TestReadDir tests whether ReadDir returns the same dir entries as testfs // by comparing their *lib9p.Stat. func TestReadDir(t *testing.T) {