lib9p

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

commit 669130552c43e943fd5a1e58e4c64f809a1ce25d
parent 31aa93de58637350a551d29f1480b45362dfeca0
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 17 Jan 2024 15:27:57 +0900

make all path names slash-separated

Diffstat:
Mdiskfs/file.go | 2+-
Mdiskfs/fs.go | 18+++++++++---------
Mdiskfs/stat_windows.go | 7+++----
Mfs.go | 1+
4 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/diskfs/file.go b/diskfs/file.go @@ -11,7 +11,7 @@ import ( // File represents a file. type File struct { fs *FS // file system to which this file belongs - path string // relative path from the root of the fs. + path string // relative path from the root of the fs. slash-separated. file *os.File // underlying file. nil if not open. dirIndex int // index for ReadDir } diff --git a/diskfs/fs.go b/diskfs/fs.go @@ -7,19 +7,20 @@ import ( "io/fs" "os" "os/user" + "path" "path/filepath" - "strings" "git.mtkn.jp/lib9p" ) // FS is a file system to export OS's file system via 9P protocol. type FS struct { - rootPath string + rootPath string // slash-separated. qidPool *QidPool } // Open opens OS's file tree as *FS rooted at name. +// Name is either os.PathSeparator-separated or slash-separated. func Open(name string) (*FS, error) { f, err := os.Open(name) if err != nil { @@ -37,7 +38,7 @@ func Open(name string) (*FS, error) { path: ".", } fsys := &FS{ - rootPath: name, + rootPath: filepath.ToSlash(name), qidPool: newQidPool(), } root.fs = fsys @@ -45,6 +46,7 @@ func Open(name string) (*FS, error) { } // OpenFile opens the named file with specified omode by calling os.OpenFile. +// Name is slash separated. func (fsys *FS) OpenFile(name string, flag int) (lib9p.File, error) { if !fs.ValidPath(name) { return nil, &fs.PathError{ @@ -53,8 +55,7 @@ func (fsys *FS) OpenFile(name string, flag int) (lib9p.File, error) { Err: fs.ErrInvalid, } } - fp := filepath.Join(fsys.rootPath, name) - osf, err := os.OpenFile(fp, flag, 0) + osf, err := os.OpenFile(path.Join(fsys.rootPath, name), flag, 0) if err != nil { return nil, &fs.PathError{ Op: "openfile", @@ -65,6 +66,7 @@ func (fsys *FS) OpenFile(name string, flag int) (lib9p.File, error) { return &File{fs: fsys, path: name, file: osf}, nil } +// Name is slash-separated. func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p.FileMode) (lib9p.File, error) { usr, err := user.Current() if err != nil { @@ -89,8 +91,7 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p Err: fs.ErrInvalid, } } - paths := append([]string{fsys.rootPath}, strings.Split(name, "/")...) - ospath := filepath.Join(paths...) + ospath := path.Join(fsys.rootPath, name) var flag int switch omode & 3 { case lib9p.OREAD, lib9p.OEXEC: @@ -140,8 +141,7 @@ func (fsys *FS) Create(name string, uid string, omode lib9p.OpenMode, perm lib9p } func (fsys *FS) Remove(name string) error { - paths := append([]string{fsys.rootPath}, strings.Split(name, "/")...) - ospath := filepath.Join(paths...) + ospath := path.Join(fsys.rootPath, name) return os.Remove(ospath) } diff --git a/diskfs/stat_windows.go b/diskfs/stat_windows.go @@ -8,7 +8,6 @@ import ( "os" "os/user" "path" - "path/filepath" "strconv" "syscall" "time" @@ -67,7 +66,7 @@ func init() { // Stat is real implementation of File.Stat. func (f *File) stat() (*lib9p.FileInfo, error) { - ospath := filepath.Join(f.fs.rootPath, f.path) + ospath := path.Join(f.fs.rootPath, f.path) fsfi, err := os.Stat(ospath) if err != nil { return nil, fmt.Errorf("stat: %v", err) @@ -156,8 +155,8 @@ func (f *File) wstat(s *lib9p.Stat) error { } if s.Name != oldStat.Name { // TODO: check neither Names contains "/" - oldpath := filepath.Join(f.fs.rootPath, path.Dir(f.path), oldStat.Name) - newpath := filepath.Join(f.fs.rootPath, path.Dir(f.path), s.Name) + 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.Rename(oldpath, newpath); err != nil { return fmt.Errorf("rename: %v", err) } diff --git a/fs.go b/fs.go @@ -8,6 +8,7 @@ import ( // An FS is an file system to be exported by 9P server. type FS interface { // OpenFile opens file named name with specified flag (O_RDONLY etc.). + // The separator of name is slash in all platforms. // // When OpenFile returns an error, it should be of type *fs.PathError // with the Op field set to "openfile", the Path field set to name,