commit 4eee0e21922c51be7a7352bb4344c524c8a2bfc0
parent a93eebf289aea525a9d752739504bc49affae3d9
Author: Matsuda Kenji <info@mtkn.jp>
Date: Mon, 8 Jan 2024 15:15:04 +0900
use unionFS
Diffstat:
M | main.go | | | 54 | ++++++++---------------------------------------------- |
M | unionfs.go | | | 10 | +++++++++- |
2 files changed, 17 insertions(+), 47 deletions(-)
diff --git a/main.go b/main.go
@@ -19,8 +19,8 @@ import (
type stat struct {
cwd string // current working directory. absolute path.
- f fs.File
- fstab map[string]fs.FS
+ f lib9p.File
+ fsys *unionFS
}
type token struct {
@@ -57,24 +57,14 @@ func (c *cmd) run(s *stat) error {
if s.f != nil {
s.f.Close()
}
- var fsys fs.FS
- var n int
- var subpath string
- for mtpt, f := range s.fstab {
- if strings.HasPrefix(p, mtpt) && len(mtpt) > n {
- fsys = f
- n = len(mtpt)
- subpath = strings.TrimPrefix(p, mtpt)
- }
- }
- fi, err := fs.Stat(fsys, subpath)
+ fi, err := fs.Stat(lib9p.ExportFS{s.fsys}, p)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("not a directory")
}
- f, err := fsys.Open(subpath)
+ f, err := s.fsys.OpenFile(p, lib9p.OREAD)
if err != nil {
return err
}
@@ -89,17 +79,7 @@ func (c *cmd) run(s *stat) error {
if len(c.args) > 1 {
p = absPath(p, c.args[1])
}
- var fsys fs.FS
- var n int
- var subpath string
- for mtpt, f := range s.fstab {
- if strings.HasPrefix(p, mtpt) && len(mtpt) > n {
- fsys = f
- n = len(mtpt)
- subpath = strings.TrimPrefix(p, mtpt)
- }
- }
- files, err := fs.Glob(fsys, path.Join(subpath, "*"))
+ files, err := fs.Glob(lib9p.ExportFS{s.fsys}, path.Join(p, "*"))
if err != nil {
return err
}
@@ -122,31 +102,14 @@ func (c *cmd) run(s *stat) error {
} else {
p = absPath(s.cwd, mtpt)
}
- var fsys fs.FS
- var n int
- var mpath string
- for m, f := range s.fstab {
- if strings.HasPrefix(p, m) && len(m) > n {
- fsys = f
- n = len(m)
- mpath = m
- }
- }
- fi, err := fs.Stat(fsys, relPath(mpath, p))
- if err != nil {
- return err
- }
- if !fi.IsDir() {
- return fmt.Errorf("mtpt not a directory")
- }
cfsys, err := client.Mount(context.TODO(), conn, conn, "kenji", "")
if err != nil {
return err
}
- s.fstab[p] = lib9p.ExportFS{cfsys}
+ s.fsys.Mount(cfsys, p)
return nil
case "fstab" :
- fmt.Println(s.fstab)
+ fmt.Println(s.fsys.fstab)
return nil
default:
return fmt.Errorf("unknown command %v", c.args[0])
@@ -169,9 +132,8 @@ func main() {
cc := runParser(ctx, tc)
s := &stat{
cwd: ".",
- fstab: make(map[string]fs.FS),
+ fsys: &unionFS{fstab: map[string]lib9p.FS{".": fsys}},
}
- s.fstab["."] = lib9p.ExportFS{fsys}
for {
fmt.Printf("9%% ")
c := <-cc
diff --git a/unionfs.go b/unionfs.go
@@ -75,10 +75,18 @@ func (fsys *unionFS) OpenFile(name string, omode lib9p.OpenMode) (lib9p.File, er
Err: fs.ErrNotExist,
}
}
- return subfs.OpenFile(name, omode)
+ f, err := subfs.OpenFile(subpath, omode)
+ return f, err
}
func (fsys *unionFS) Mount(fs9 lib9p.FS, mtpt string) error {
+ if !fs.ValidPath(mtpt) {
+ return &fs.PathError{
+ Op: "mount",
+ Path: mtpt,
+ Err: fs.ErrInvalid,
+ }
+ }
if _, ok := fsys.fstab[mtpt]; ok {
return fmt.Errorf("mtpt already used")
}