Mounts and show an empty dir

This commit is contained in:
Jakob Unterwurzacher 2015-09-03 23:54:12 +02:00
parent 4b98f74e3e
commit 6a110b8dde
3 changed files with 96 additions and 10 deletions

View File

@ -7,14 +7,20 @@ import (
type FS struct { type FS struct {
*cryptfs.FS *cryptfs.FS
backing string
} }
func New(key [16]byte) *FS { func New(key [16]byte, b string) *FS {
return &FS { return &FS {
FS: cryptfs.NewFS(key), FS: cryptfs.NewFS(key),
backing: b,
} }
} }
func (fs *FS) Root() (fs.Node, error) { func (fs *FS) Root() (fs.Node, error) {
return nil, nil n := Node{
backing: "",
parentFS: fs,
}
return n, nil
} }

79
frontend/node.go Normal file
View File

@ -0,0 +1,79 @@
package frontend
import (
"fmt"
"os"
"time"
"syscall"
"io/ioutil"
"path"
"golang.org/x/net/context"
//"github.com/rfjakob/gocryptfs/cryptfs"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
type Node struct {
fs.NodeRef
backing string
parentFS *FS
}
func StatToAttr(s *syscall.Stat_t, a *fuse.Attr) {
a.Inode = s.Ino
a.Size = uint64(s.Size)
a.Blocks = uint64(s.Blocks)
a.Atime = time.Unix(s.Atim.Sec, s.Atim.Nsec)
a.Mtime = time.Unix(s.Mtim.Sec, s.Mtim.Nsec)
a.Ctime = time.Unix(s.Ctim.Sec, s.Ctim.Nsec)
a.Mode = os.FileMode(s.Mode) | os.ModeDir
a.Nlink = uint32(s.Nlink)
a.Uid = uint32(s.Uid)
a.Gid = uint32(s.Gid)
a.Rdev = uint32(s.Rdev)
}
func (n Node) Attr(ctx context.Context, attr *fuse.Attr) error {
var err error
var st syscall.Stat_t
if n.backing == "" {
// When GetAttr is called for the toplevel directory, we always want
// to look through symlinks.
fmt.Printf("Attr %s\n", n.parentFS.backing)
//err = syscall.Stat(n.parentFS.backing, &st)
err = syscall.Stat("/", &st)
} else {
fmt.Printf("Attr %s\n", path.Join(n.parentFS.backing, n.backing))
p := path.Join(n.parentFS.backing, n.backing)
err = syscall.Lstat(p, &st)
}
if err != nil {
return err
}
StatToAttr(&st, attr)
return nil
}
func (n *Node) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
entries, err := ioutil.ReadDir(n.backing)
if err != nil {
return nil, err
}
var fuseEntries []fuse.Dirent
for _, e := range entries {
var d fuse.Dirent
d.Name = e.Name()
fuseEntries = append(fuseEntries, d)
}
return fuseEntries, err
}
func (n *Node) Lookup(ctx context.Context, name string) (fs.Node, error) {
if name == "hello" {
return Node{}, nil
}
return nil, fuse.ENOENT
}

17
main.go
View File

@ -18,11 +18,11 @@ import (
) )
// debug flag enables logging of debug messages to stderr. // debug flag enables logging of debug messages to stderr.
var debug = flag.Bool("debug", false, "enable debug log messages to stderr") var debug = flag.Bool("debug", true, "enable debug log messages to stderr")
func usage() { func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s MOUNTPOINT\n", os.Args[0]) fmt.Fprintf(os.Stderr, " %s BACKING MOUNTPOINT\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -34,18 +34,19 @@ func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
if flag.NArg() != 1 { if flag.NArg() != 2 {
usage() usage()
os.Exit(2) os.Exit(2)
} }
mountpoint := flag.Arg(0) backing := flag.Arg(0)
mountpoint := flag.Arg(1)
c, err := fuse.Mount( c, err := fuse.Mount(
mountpoint, mountpoint,
fuse.FSName("memfs"), fuse.FSName("gocryptfs"),
fuse.Subtype("memfs"), fuse.Subtype("gocryptfs"),
fuse.LocalVolume(), fuse.LocalVolume(),
fuse.VolumeName("Memory FS"), fuse.VolumeName("gocryptfs"),
) )
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -59,7 +60,7 @@ func main() {
srv := fs.New(c, cfg) srv := fs.New(c, cfg)
var key [16]byte var key [16]byte
filesys := frontend.New(key) filesys := frontend.New(key, backing)
if err := srv.Serve(filesys); err != nil { if err := srv.Serve(filesys); err != nil {
log.Fatal(err) log.Fatal(err)