lib9p

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

commit 1231b1592ccba8c2606599e8b26b180fe049e09c
parent caac65aae74a15bf81cf306419b50f7d45dea444
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 20 Oct 2024 18:03:51 +0900

delete qid when a file is removed

Diffstat:
Mdiskfs/fs.go | 9+++++++++
Mdiskfs/qid_unix.go | 4++++
Mdiskfs/stat_unix.go | 10++++++++++
Mserver.go | 4++++
4 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/diskfs/fs.go b/diskfs/fs.go @@ -114,6 +114,7 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p Err: fmt.Errorf("mkdir: %v", err), } } + fmt.Fprintf(os.Stderr, "mkdir(%s, %s)\n", ospath, perm) osfile, err = os.OpenFile(ospath, flag, 0) if err != nil { return nil, &fs.PathError{ @@ -122,6 +123,8 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p Err: err, } } + qid, _ := fsys.qidPool.lookup(&File{fs: fsys, path: name, file: osfile}) + fmt.Fprintf(os.Stderr, "qid: %v\n", qid) } else { flag |= os.O_CREATE osfile, err = os.OpenFile(ospath, flag, perm) @@ -142,6 +145,12 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p func (fsys *FS) Remove(name string) error { ospath := path.Join(fsys.rootPath, name) + fi, err := os.Stat(ospath) + if err != nil { + return fmt.Errorf("stat %s: %v\n", name, err) + } + id := idFromInfo(ospath, fi) + fsys.qidPool.deleteID(id) return os.Remove(ospath) } diff --git a/diskfs/qid_unix.go b/diskfs/qid_unix.go @@ -132,6 +132,10 @@ func (pool *QidPool) delete(f *File) { if err != nil { return } + pool.deleteID(id) +} + +func (pool *QidPool) deleteID(id fileID) { pool.Lock() defer pool.Unlock() delete(pool.m, id) diff --git a/diskfs/stat_unix.go b/diskfs/stat_unix.go @@ -3,6 +3,7 @@ package diskfs import ( + "errors" "fmt" "io/fs" "os" @@ -63,6 +64,7 @@ func init() { // Stat is real implementation of File.Stat. func (f *File) stat() (*lib9p.FileInfo, error) { + fmt.Fprintf(os.Stderr, "stat: nextQid: %d ", f.fs.qidPool.nextQid) ospath := filepath.Join(f.fs.rootPath, f.path) fsfi, err := os.Stat(ospath) if err != nil { @@ -76,12 +78,14 @@ func (f *File) stat() (*lib9p.FileInfo, error) { if err != nil { return nil, fmt.Errorf("fiStat: %v", err) } + fmt.Fprintf(os.Stderr, "-> %d\n", f.fs.qidPool.nextQid) return &lib9p.FileInfo{Stat: *stat}, nil } // wstat is the real implementation of File.WStat. // TODO: when error occurs, file stat should be restored. func (f *File) wstat(s *lib9p.Stat) error { + fmt.Fprintf(os.Stderr, "stat: nextQid: %d ", f.fs.qidPool.nextQid) var file *os.File fi, err := f.Stat() if err != nil { @@ -162,9 +166,15 @@ func (f *File) wstat(s *lib9p.Stat) error { // TODO: check neither Name contains "/" oldpath := path.Join(f.fs.rootPath, path.Dir(f.path), oldStat.Name) newpath := path.Join(f.fs.rootPath, path.Dir(f.path), s.Name) + if _, err := os.Stat(newpath); errors.Is(err, fs.ErrExist) { + if err := f.fs.Remove(newpath); err != nil { + return fmt.Errorf("remove old file: %v", err) + } + } if err := os.Rename(oldpath, newpath); err != nil { return fmt.Errorf("rename: %v", err) } } + fmt.Fprintf(os.Stderr, "-> %d\n", f.fs.qidPool.nextQid) return nil } diff --git a/server.go b/server.go @@ -672,6 +672,8 @@ func sCreate(ctx context.Context, c *conn, rc <-chan *request) { perm &= ^FileMode(0777) | (dirperm & FileMode(0777)) } cpath = path.Join(r.fid.path, ifcall.Name) + fmt.Fprintf(os.Stderr, "Create(%s, %s, %v, %v)\n", cpath, r.fid.uid, + ifcall.Mode, perm) r.fid.file, err = cfs.Create(cpath, r.fid.uid, ifcall.Mode, perm) if err != nil { r.err = fmt.Errorf("create: %v", err) @@ -684,6 +686,8 @@ func sCreate(ctx context.Context, c *conn, rc <-chan *request) { r.err = fmt.Errorf("stat: %v", err) goto resp } + fmt.Fprintf(os.Stderr, "qid: %v\n", fi.Sys().(*Stat).Qid) + fmt.Fprintf(os.Stderr, "filename: %s\n", fi.Sys().(*Stat).Name) r.fid.qidpath = fi.Sys().(*Stat).Qid.Path r.ofcall = &RCreate{ Qid: fi.Sys().(*Stat).Qid,