Skip to content

Commit

Permalink
Add open flags to operation structs
Browse files Browse the repository at this point in the history
The open flag indicates the type of access we need.

Add a test to the memfs server to make sure an open
is blocked if the flags and permissions don't match.

Signed-off-by: Ronald G. Minnich <[email protected]>
  • Loading branch information
rminnich committed May 16, 2019
1 parent cd39596 commit f13b68c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
8 changes: 8 additions & 0 deletions conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,16 @@ func convertInMessage(
}

case fusekernel.OpOpen:
type input fusekernel.OpenIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))

if in == nil {
err = errors.New("Corrupt OpOpen")
return
}
o = &fuseops.OpenFileOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Flag: int(in.Flags),
}

case fusekernel.OpOpendir:
Expand Down
3 changes: 3 additions & 0 deletions fuseops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ type OpenFileOp struct {
// layer. This allows for filesystems whose file sizes are not known in
// advance, for example, because contents are generated on the fly.
UseDirectIO bool

// Open flags. See os.OpenFile
Flag int
}

// Read data from a file previously opened with CreateFile or OpenFile.
Expand Down
6 changes: 3 additions & 3 deletions mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSuccessfulMount(t *testing.T) {
// Set up a temporary directory.
dir, err := ioutil.TempDir("", "mount_test")
if err != nil {
t.Fatal("ioutil.TempDir: %v", err)
t.Fatalf("ioutil.TempDir: %v", err)
}

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestNonEmptyMountPoint(t *testing.T) {
// Set up a temporary directory.
dir, err := ioutil.TempDir("", "mount_test")
if err != nil {
t.Fatal("ioutil.TempDir: %v", err)
t.Fatalf("ioutil.TempDir: %v", err)
}

defer os.RemoveAll(dir)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestNonexistentMountPoint(t *testing.T) {
// Set up a temporary directory.
dir, err := ioutil.TempDir("", "mount_test")
if err != nil {
t.Fatal("ioutil.TempDir: %v", err)
t.Fatalf("ioutil.TempDir: %v", err)
}

defer os.RemoveAll(dir)
Expand Down
19 changes: 19 additions & 0 deletions samples/memfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
"github.com/jacobsa/syncutil"
"golang.org/x/sys/unix"
)

type memFS struct {
Expand Down Expand Up @@ -615,6 +616,24 @@ func (fs *memFS) OpenFile(
if !inode.isFile() {
panic("Found non-file.")
}
// What perms do we need?
var perm int
switch op.Flag & unix.O_ACCMODE {
case unix.O_RDONLY:
perm = 0400
case unix.O_WRONLY:
perm = 0200
case unix.O_RDWR:
perm = 0600
// This is legal, at least on linux
case unix.O_ACCMODE:
perm = 0600
}

// We only check user permissions, since this is a sample FS
if int(inode.attrs.Mode.Perm())&perm != perm {
err = unix.EACCES
}

return
}
Expand Down
25 changes: 25 additions & 0 deletions samples/memfs/memfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
. "github.com/jacobsa/oglematchers"
. "github.com/jacobsa/ogletest"
"github.com/kahing/go-xattr"
"golang.org/x/sys/unix"
)

func TestMemFS(t *testing.T) { RunTests(t) }
Expand Down Expand Up @@ -1706,6 +1707,30 @@ func (t *MemFSTest) RenameIntoFileSystem() {
ExpectThat(err, Error(HasSubstr("cross-device")))
}

func (t *MemFSTest) TestPermissions() {
var e = unix.EACCES
var test = []struct {
name string
mode int
res [4]error
}{
{name: "ro", mode: 0400, res: [4]error{nil, e, e, e}},
{name: "wo", mode: 0200, res: [4]error{e, nil, e, e}},
{name: "rw", mode: 0600, res: [4]error{nil, nil, nil, nil}},
}

for _, tt := range test {
n := path.Join(t.Dir, tt.name)
err := ioutil.WriteFile(n, []byte(""), os.FileMode(tt.mode))
AssertEq(nil, err)
for f, r := range tt.res {
fd, err := unix.Open(n, f, 0)
unix.Close(fd)
AssertEq(r, err)
}
}
}

func (t *MemFSTest) RenameOverExistingFile() {
var err error

Expand Down

0 comments on commit f13b68c

Please sign in to comment.