Skip to content

Commit

Permalink
Updates to ramfs.
Browse files Browse the repository at this point in the history
ramfs inode structure now uses map-s to hold
child names.  Reference counting is used to
manage file object lifecycle, but is not technically
necessary as go is garbage-collected.

The 9pr client now tracks file paths, and has an rm
command.
path information has been removed from cfilesys.

Signed-off-by: David M. Rogers <predictivestatmech@gmail.com>
  • Loading branch information
frobnitzem committed Apr 26, 2023
1 parent b2d0be5 commit 6f6979f
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 166 deletions.
16 changes: 8 additions & 8 deletions cfilesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (fs *fsState) Attach(ctx context.Context, uname, aname string,

//fs.fids = make(map[*cEnt]Fid)
return cEnt{
path: make([]string, 0),
//path: make([]string, 0),
fid: rootFid,
qid: qid,
fs: fs,
Expand All @@ -108,13 +108,13 @@ func (w Warning) Error() string {
}

type cEnt struct {
path []string // absolute path
//path []string // absolute path
fid Fid
qid Qid // FIXME(frobnitzem): stash qids here
fs *fsState
}

var noEnt cEnt = cEnt{nil, NOFID, Qid{}, nil}
var noEnt cEnt = cEnt{NOFID, Qid{}, nil}

type fileRef struct {
cEnt
Expand Down Expand Up @@ -237,7 +237,7 @@ func (ent cEnt) Create(ctx context.Context, name string,
if err != nil {
return noEnt, noFile, err
}
ent.path = append(ent.path, name)
//ent.path = append(ent.path, name)
ent.qid = qid

// TODO(frobnitzem): this appears twice, make a fileEnt function.
Expand All @@ -264,7 +264,7 @@ func (ent cEnt) Remove(ctx context.Context) error {
func (ent cEnt) Walk(ctx context.Context,
names ...string) ([]Qid, Dirent, error) {
steps, bsp := NormalizePath(names)
if bsp < 0 || bsp > len(ent.path) {
if bsp < 0 { //|| bsp > len(ent.path) {
return nil, ent, MessageRerror{"invalid path: " + strings.Join(names, "/")}
}

Expand All @@ -277,9 +277,9 @@ func (ent cEnt) Walk(ctx context.Context,
return qids, noEnt, Warning{"Incomplete walk result"}
}
// drop part of ent.path
steps = steps[:len(qids)]
remain := len(ent.path) - bsp
next.path = append(ent.path[:remain], steps[bsp:]...)
//steps = steps[:len(qids)]
//remain := len(ent.path) - bsp
//next.path = append(ent.path[:remain], steps[bsp:]...)
if len(qids) > 0 {
next.qid = qids[len(qids)-1]
} else {
Expand Down
126 changes: 118 additions & 8 deletions cmd/9pr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,18 @@ func main() {
stderr: os.Stderr,
root: root,
pwd: pwd,
path: "/",
}

completer := readline.NewPrefixCompleter(
readline.PcItem("ls"),
// readline.PcItem("find"),
readline.PcItem("stat"),
readline.PcItem("cat"),
readline.PcItem("cd"),
readline.PcItem("mkdir"),
readline.PcItem("pwd"),
readline.PcItem("rm"),
readline.PcItem("touch"),
readline.PcItem("write"),
)

rl, err := readline.NewEx(&readline.Config{
Expand Down Expand Up @@ -127,6 +130,12 @@ func main() {
cmd = commander.cmdpwd
case "cat":
cmd = commander.cmdcat
case "rm":
cmd = commander.cmdrm
case "touch":
cmd = commander.cmdtouch
case "mkdir":
cmd = commander.cmdmkdir
case "write":
cmd = commander.cmdwrite
default:
Expand Down Expand Up @@ -159,13 +168,20 @@ type fsCommander struct {
stderr io.Writer
}

func (c *fsCommander) toWalk(p string) (p9p.Dirent, []string, error) {
func (c *fsCommander) toWalk(p string) (p9p.Dirent, []string, string, error) {
isAbs, steps, err := p9p.ToWalk(c.pwd, p)
rel := c.pwd
path := c.path

if isAbs {
rel = c.root
path = "/"
}
path, err = p9p.WalkName(path, steps...)
if err != nil { // should not happen
return rel, steps, path, err
}
return rel, steps, err
return rel, steps, path, err
}

func (c *fsCommander) cmdls(ctx context.Context, args ...string) error {
Expand All @@ -182,7 +198,7 @@ func (c *fsCommander) cmdls(ctx context.Context, args ...string) error {
fmt.Fprintln(wr, p+":")
}

rel, steps, err := c.toWalk(p)
rel, steps, _, err := c.toWalk(p)
if err != nil {
return err
}
Expand Down Expand Up @@ -242,7 +258,7 @@ func (c *fsCommander) cmdcd(ctx context.Context, args ...string) error {
return fmt.Errorf("invalid args: %v", args)
}

rel, steps, err := c.toWalk(p)
rel, steps, path, err := c.toWalk(p)
if err != nil {
return err
}
Expand All @@ -258,10 +274,104 @@ func (c *fsCommander) cmdcd(ctx context.Context, args ...string) error {

c.pwd.Clunk(ctx)
c.pwd = next
c.path = path

return nil
}

func (c *fsCommander) cmdrm(ctx context.Context, args ...string) error {
var ret error
ret1 := errors.New("error")

for _, p := range args {
rel, steps, _, err := c.toWalk(p)
if err != nil {
fmt.Println("Invalid path: ", p)
ret = ret1
continue
}

_, ent, err := rel.Walk(ctx, steps...)
if err != nil {
fmt.Println(err)
ret = ret1
continue
}

err = ent.Remove(ctx)
if err != nil {
fmt.Println(err)
ret = ret1
continue
}
}
return ret
}

func (c *fsCommander) cmdtouch(ctx context.Context, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("invalid args: %v", args)
}
p := args[0]

rel, steps, _, err := c.toWalk(p)
if err != nil {
return err
}
if len(steps) < 1 {
return fmt.Errorf("Need a filename.")
}
p = steps[len(steps)-1]
steps = steps[:len(steps)-1]

qids, next, err := rel.Walk(ctx, steps...)
if err != nil || len(qids) != len(steps) {
return err
}
if !p9p.IsDir(next) {
next.Clunk(ctx)
return errors.New("not a directory.")
}
ref, _, err := next.Create(ctx, p, 0644, p9p.OREAD)
if err != nil {
ref.Clunk(ctx)
}

return err
}

func (c *fsCommander) cmdmkdir(ctx context.Context, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("invalid args: %v", args)
}
p := args[0]

rel, steps, _, err := c.toWalk(p)
if err != nil {
return err
}
if len(steps) < 1 {
return fmt.Errorf("Need a filename.")
}
p = steps[len(steps)-1]
steps = steps[:len(steps)-1]

qids, next, err := rel.Walk(ctx, steps...)
if err != nil || len(qids) != len(steps) {
return err
}
if !p9p.IsDir(next) {
next.Clunk(ctx)
return errors.New("not a directory.")
}
ref, _, err := next.Create(ctx, p, p9p.DMDIR | 0644, p9p.OREAD)
if err != nil {
ref.Clunk(ctx)
}

return err
}

func (c *fsCommander) cmdpwd(ctx context.Context, args ...string) error {
if len(args) != 0 {
return fmt.Errorf("pwd takes no arguments")
Expand All @@ -282,7 +392,7 @@ func (c *fsCommander) cmdcat(ctx context.Context, args ...string) error {
return fmt.Errorf("invalid args: %v", args)
}

rel, steps, err := c.toWalk(p)
rel, steps, _, err := c.toWalk(p)
if err != nil {
return err
}
Expand Down Expand Up @@ -318,7 +428,7 @@ func (c *fsCommander) cmdcat(ctx context.Context, args ...string) error {
func (c *fsCommander) cmdwrite(ctx context.Context, args ...string) error {
p := args[0]

rel, steps, err := c.toWalk(p)
rel, steps, _, err := c.toWalk(p)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/9ps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/frobnitzem/go-p9p"
"github.com/frobnitzem/go-p9p/ufs"
"github.com/frobnitzem/go-p9p/sleepfs"
"github.com/frobnitzem/go-p9p/ramfs"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -68,6 +69,8 @@ func main() {
var session p9p.Session
if root == "sleepfs" {
session = p9p.SFileSys(sleepfs.NewServer(ctx))
} else if root == "ramfs" {
session = p9p.SFileSys(ramfs.NewServer(ctx))
} else {
session = p9p.SFileSys(ufs.NewServer(ctx, root))
}
Expand Down
Loading

0 comments on commit 6f6979f

Please sign in to comment.