commit 63a5ee93dc615a79111b2bb97129de2a6ed8736a
parent 91d4b1ef0e7175b23195e56937ede4b19137699b
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 15 Oct 2023 07:48:16 +0900
implement ClientFile.Parent()
Diffstat:
3 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/client2.go b/client2.go
@@ -45,7 +45,6 @@ func Mount(r io.Reader, w io.Writer, uname, aname string) (fs *Client, err error
qid: qid,
client: c,
}
- c.root.parent = c.root
fid.file = c.root
return c, nil
}
diff --git a/file.go b/file.go
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"path"
+ "strings"
)
type File interface {
@@ -47,9 +48,10 @@ type RemoverFile interface {
Remove() error
}
-// Walkfile walks file tree starting at f, following path specified by name.
-// It returns the destination File and nil, or nil with non-nil error.
-func walkfile(f File, name string) (File, error) {
+// Walkfile1 walks file tree one step down from f to the child specified by name.
+// It returns the destination File.
+// name must not "..".
+func walkfile1(f File, name string) (File, error) {
children, err := f.Child()
if children == nil {
return nil, fmt.Errorf("get children: %v", err)
@@ -69,10 +71,25 @@ func walkfile(f File, name string) (File, error) {
return nil, fmt.Errorf("not found")
}
+// Walkfile walks the file tree starting at f following pathname.
+// pathname must not contain "..".
+func walkfile(f File, pathname string) (File, error) {
+ pathname = path.Clean(pathname)
+ wnames := strings.Split(pathname, "/")
+ cwd := f
+ var err error
+ for _, p := range wnames {
+ cwd, err = walkfile1(cwd, p)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return cwd, nil
+}
+
type ClientFile struct {
name string
- path string
- parent *ClientFile
+ path string // must not contain trailing slash.
fid *clientFid
qid Qid
iounit uint32
@@ -80,7 +97,8 @@ type ClientFile struct {
}
func (cf *ClientFile) Parent() (File, error) {
- return cf.parent, nil
+ parentPath := path.Dir(cf.path)
+ return walkfile(cf.client.root, parentPath)
}
func (cf *ClientFile) Child() ([]File, error) {
@@ -113,7 +131,6 @@ func (cf *ClientFile) Child() ([]File, error) {
files = append(files, &ClientFile{
name: stat.Name,
path: path.Join(cf.name, stat.Name),
- parent: cf,
client: cf.client,
})
data = data[stat.size()+2:]
diff --git a/server.go b/server.go
@@ -298,7 +298,7 @@ func sWalk(s *Server, r *Req) {
cwd := oldFid.File
var n int
for i, name := range ifcall.WName() {
- child, err := walkfile(cwd, name)
+ child, err := walkfile1(cwd, name)
if err != nil {
log.Printf("walkfile: %v", err)
break