Skip to content

Commit

Permalink
cpio9p: do not use path/filepath
Browse files Browse the repository at this point in the history
This is a cpio archive, so we must use path.

Signed-off-by: Ronald G Minnich <rminnich@gmail.com>
  • Loading branch information
rminnich committed Nov 7, 2023
1 parent 13a4fbb commit f564211
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 48 deletions.
20 changes: 12 additions & 8 deletions client/cpio9p.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"io"
"io/fs"
"os"
"path/filepath"
"path"
"strings"
"syscall"

"github.com/hugelgupf/p9/fsimpl/templatefs"
Expand Down Expand Up @@ -166,7 +167,7 @@ func (l *CPIO9PFID) Walk(names []string) ([]p9.QID, p9.File, error) {
fullpath = r.Name
}
for _, name := range names {
fullpath = filepath.Join(fullpath, name)
fullpath = path.Join(fullpath, name)
ix, ok := l.fs.m[fullpath]
verbose("cpio:Walk %q get %v, %v", fullpath, ix, ok)
if !ok {
Expand Down Expand Up @@ -258,13 +259,16 @@ func (l *CPIO9PFID) readdir() ([]uint64, error) {
// contents of all subdirs.
var list []uint64
for i, r := range l.fs.recs[l.path+1:] {
// We can not use filepath.Rel, thank you Windows.
// filepath.Rel fails, we're done here.
b, err := filepath.Rel(dn, r.Name)
if err != nil {
verbose("cpio:r.Name %q: DONE", r.Name)
// b, err := filepath.Rel(dn, r.Name)
if dn != "." && !strings.HasPrefix(r.Name, dn) {
verbose("cpio:dn %q, r.Name %q: DONE", dn, r.Name)
break
}
dir, _ := filepath.Split(b)
b := strings.TrimPrefix(strings.TrimPrefix(r.Name, dn), "/")
dir, _ := path.Split(b)
verbose("readdir: dn %q, r.Name %q, dir %q, b %q", dn, r.Name, dir, b)
if len(dir) > 0 {
continue
}
Expand Down Expand Up @@ -309,11 +313,11 @@ func (l *CPIO9PFID) Readdir(offset uint64, count uint32) (p9.Dirents, error) {
if err != nil {
continue
}
verbose("cpio:add path %d %q", i, filepath.Base(r.Info.Name))
verbose("cpio:add path %d %q", i, path.Base(r.Info.Name))
dirents = append(dirents, p9.Dirent{
QID: qid,
Type: qid.Type,
Name: filepath.Base(r.Info.Name),
Name: path.Base(r.Info.Name),
Offset: i,
})
}
Expand Down
169 changes: 129 additions & 40 deletions client/cpio9p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hugelgupf/p9/p9"
)

func TestCPIO9P(t *testing.T) {
func TestCPIO9PBadFile(t *testing.T) {
d := t.TempDir()
bogus := filepath.Join(d, "bogus")
if _, err := NewCPIO9P(bogus); err == nil {
Expand All @@ -26,12 +26,85 @@ func TestCPIO9P(t *testing.T) {
if _, err := NewCPIO9P(bogus); err == nil {
t.Fatalf("Opening bad file: got nil, want err")
}
}

func TestCPIOBadOperators(t *testing.T) {
fs, err := NewCPIO9P("data/a.cpio")
if err != nil {
t.Fatalf("data/a.cpio: got %v, want nil", err)
}
// See if anything is there.
attach, err := fs.Attach()
if err != nil {
t.Fatalf("Attach: got %v, want nil", err)
}
t.Logf("root:%v", attach)

_, root, err := attach.Walk([]string{})
if err != nil {
t.Fatalf("walking '': want nil, got %v", err)
}

walkto := []string{"b", "c", "hi"}
_, hi, err := root.Walk(walkto)
if err != nil {
t.Fatalf("walking %v from /: want nil, got %v", walkto, err)
}
var data [2]byte
off := int64(1)
if n, err := hi.WriteAt(data[:], off); err == nil || n != -1 {
t.Fatalf("WriteAt: got (%d, nil), want (-1, err)", n)
}

if _, err := hi.Symlink("", "", p9.UID(0), p9.GID(0)); err == nil {
t.Fatalf("symlink: got nil, want err")
}

if err := hi.Link(root, ""); err == nil {
t.Fatalf("link: got nil, want err")
}

var m p9.FileMode
if _, err := hi.Mknod("", m, 0, 0, p9.UID(0), p9.GID(0)); err == nil {
t.Fatalf("Mknod: got nil, want err")
}

if err := hi.Rename(root, "k"); err == nil {
t.Fatalf("rename: got nil, want err")
}

if err := hi.RenameAt("", root, ""); err == nil {
t.Fatalf("renameat: got nil, want err")
}

if err := hi.UnlinkAt("hi", 0); err == nil {
t.Fatalf("unlinkat: got nil, want err")
}

if _, err := root.StatFS(); err == nil {
t.Fatalf("statfs: got nil, want err")
}

}

func TestCPIO9PAttach(t *testing.T) {
fs, err := NewCPIO9P("data/a.cpio")
if err != nil {
t.Fatalf("data/a.cpio: got %v, want nil", err)
}
// See if anything is there.
attach, err := fs.Attach()
if err != nil {
t.Fatalf("Attach: got %v, want nil", err)
}
t.Logf("root:%v", attach)
}

func TestCPIO9PAttachWalk(t *testing.T) {
fs, err := NewCPIO9P("data/a.cpio")
if err != nil {
t.Fatalf("data/a.cpio: got %v, want nil", err)
}
// See if anything is there.
attach, err := fs.Attach()
if err != nil {
Expand All @@ -54,15 +127,33 @@ func TestCPIO9P(t *testing.T) {
}
t.Logf("b %v", b)

q, c, err := root.Walk([]string{"b", "c"})
walkto := []string{"b", "c"}
q, c, err := root.Walk(walkto)
if err != nil {
t.Fatalf("walking a/b: want nil, got %v", err)
t.Fatalf("walking %v: want nil, got %v", walkto, err)
}
if len(q) != 2 {
t.Fatalf("walking a/b: want 2 qids, got (%v,%v)", q, err)
t.Fatalf("walking %v: want 2 qids, got (%v,%v)", walkto, q, err)
}
if c == nil {
t.Fatalf("walking a/b: want non-nil file, got nil")
t.Fatalf("walking %v: want non-nil file, got nil", walkto)
}
}

func TestCPIO9PAttachWalkCreate(t *testing.T) {
fs, err := NewCPIO9P("data/a.cpio")
if err != nil {
t.Fatalf("data/a.cpio: got %v, want nil", err)
}
// See if anything is there.
attach, err := fs.Attach()
if err != nil {
t.Fatalf("Attach: got %v, want nil", err)
}
t.Logf("root:%v", attach)
_, root, err := attach.Walk([]string{})
if err != nil {
t.Fatalf("walking '': want nil, got %v", err)
}

var (
Expand All @@ -77,13 +168,19 @@ func TestCPIO9P(t *testing.T) {
t.Fatalf("mkdir in root: got hil, want err")
}

walkto := []string{"b", "c"}
_, c, err := root.Walk(walkto)
if err != nil {
t.Fatalf("walking %v: want nil, got %v", walkto, err)
}

if _, _, err := c.Walk([]string{"d"}); err != nil {
t.Fatalf("walking d from b/c: want nil, got %v", err)
t.Fatalf("walking d from %v: want nil, got %v", walkto, err)
}

_, hi, err := c.Walk([]string{"hi"})
if err != nil {
t.Fatalf("walking hi from b/c: want nil, got %v", err)
t.Fatalf("walking hi from %v: want nil, got %v", walkto, err)
}
var data [2]byte
off := int64(1)
Expand All @@ -97,39 +194,6 @@ func TestCPIO9P(t *testing.T) {
t.Fatalf("Reading hi: want %q, got %q", "i\n", string(data[:]))
}

// test bad operators
if n, err := hi.WriteAt(data[:], 0); err == nil || n != -1 {
t.Fatalf("WriteAt: got (%d, nil), want (-1, err)", n)
}

if _, err := hi.Symlink("", "", p9.UID(0), p9.GID(0)); err == nil {
t.Fatalf("symlink: got nil, want err")
}

if err := hi.Link(root, ""); err == nil {
t.Fatalf("link: got nil, want err")
}

if _, err := hi.Mknod("", m, 0, 0, p9.UID(0), p9.GID(0)); err == nil {
t.Fatalf("Mknod: got nil, want err")
}

if err := hi.Rename(root, "k"); err == nil {
t.Fatalf("rename: got nil, want err")
}

if err := hi.RenameAt("", root, ""); err == nil {
t.Fatalf("renameat: got nil, want err")
}

if err := hi.UnlinkAt("hi", 0); err == nil {
t.Fatalf("unlinkat: got nil, want err")
}

if _, err := hi.StatFS(); err == nil {
t.Fatalf("statfs: got nil, want err")
}

var (
mask p9.SetAttrMask
attr p9.SetAttr
Expand All @@ -144,6 +208,31 @@ func TestCPIO9P(t *testing.T) {
t.Fatalf("getattr: want nil, got %v", err)
}

}

func TestCPIORootReaddir(t *testing.T) {
v = t.Logf
fs, err := NewCPIO9P("data/a.cpio")
if err != nil {
t.Fatalf("data/a.cpio: got %v, want nil", err)
}
// See if anything is there.
attach, err := fs.Attach()
if err != nil {
t.Fatalf("Attach: got %v, want nil", err)
}
t.Logf("root:%v", attach)
_, root, err := attach.Walk([]string{})
if err != nil {
t.Fatalf("walking '': want nil, got %v", err)
}

walkto := []string{"b", "c"}
_, c, err := root.Walk(walkto)
if err != nil {
t.Fatalf("walking %v: want nil, got %v", walkto, err)
}

dirs, err := c.Readdir(0, 64*1024)
if err != nil {
t.Fatalf("readdir on root: want nil, got %v", err)
Expand Down

0 comments on commit f564211

Please sign in to comment.