libgocryptfs/frontend/fe_dir.go

132 lines
3.0 KiB
Go
Raw Normal View History

package frontend
2015-09-05 20:30:20 +02:00
// frontend sits between FUSE and ClueFS
// and uses cryptfs for all crypto operations
//
// cryptfs
// ^
// |
// v
// FUSE <-> frontend <-> ClueFS
//
// This file handles directories
import (
2015-09-05 11:49:05 +02:00
"fmt"
"github.com/rfjakob/gocryptfs/cryptfs"
"github.com/rfjakob/cluefs/lib/cluefs"
2015-09-05 11:49:05 +02:00
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
"golang.org/x/net/context"
)
type Dir struct {
*cluefs.Dir
2015-09-05 20:11:20 +02:00
2015-09-05 11:49:05 +02:00
crfs *cryptfs.CryptFS
}
func NewDir(parent string, name string, fs *FS) *Dir {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("NewDir parent=%s name=%s\n", parent, name)
2015-09-05 11:49:05 +02:00
return &Dir {
Dir: cluefs.NewDir(parent, name, fs.ClueFS),
crfs: fs.CryptFS,
}
}
func (d *Dir) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fusefs.Handle, error) {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("Open\n")
2015-09-05 11:49:05 +02:00
h, err := d.Dir.Open(ctx, req, resp)
if err != nil {
return nil, err
}
clueDir := h.(*cluefs.Dir)
2015-09-05 12:15:42 +02:00
return &Dir {
2015-09-05 11:49:05 +02:00
Dir: clueDir,
crfs: d.crfs,
}, nil
}
func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fusefs.Node, error) {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("Lookup %s\n", req.Name)
2015-09-05 11:49:05 +02:00
req.Name = d.crfs.EncryptPath(req.Name)
2015-09-05 20:11:20 +02:00
node, err := d.Dir.Lookup(ctx, req, resp)
2015-09-05 11:49:05 +02:00
if err != nil {
return nil, err
}
2015-09-05 20:11:20 +02:00
clueDir, ok := node.(*cluefs.Dir)
2015-09-05 11:49:05 +02:00
if ok {
2015-09-05 14:08:00 +02:00
return &Dir {
Dir: clueDir,
crfs: d.crfs,
}, nil
2015-09-05 11:49:05 +02:00
} else {
2015-09-05 20:11:20 +02:00
resp.Attr.Size = d.crfs.PlainSize(resp.Attr.Size)
clueFile := node.(*cluefs.File)
2015-09-05 14:08:00 +02:00
return &File {
File: clueFile,
crfs: d.crfs,
}, nil
2015-09-05 11:49:05 +02:00
}
}
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("ReadDirAll\n")
2015-09-05 11:49:05 +02:00
entries, err := d.Dir.ReadDirAll(ctx)
if err != nil {
return nil, err
}
var decrypted []fuse.Dirent
for _, e := range entries {
if e.Name == "." || e.Name == ".." {
decrypted = append(decrypted, e)
continue
}
newName, err := d.crfs.DecryptPath(e.Name)
if err != nil {
fmt.Printf("ReadDirAll: Error decoding \"%s\": %s\n", e.Name, err.Error())
continue
}
e.Name = newName
decrypted = append(decrypted, e)
}
return decrypted, nil
}
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fusefs.Node, error) {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("Mkdir %s\n", req.Name)
2015-09-05 11:49:05 +02:00
req.Name = d.crfs.EncryptPath(req.Name)
n, err := d.Dir.Mkdir(ctx, req)
if err != nil {
return nil, err
}
clueDir := n.(*cluefs.Dir)
return &Dir {
Dir: clueDir,
crfs: d.crfs,
}, nil
}
func (d *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("Remove\n")
2015-09-05 11:49:05 +02:00
req.Name = d.crfs.EncryptPath(req.Name)
return d.Dir.Remove(ctx, req)
}
func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fusefs.Node, fusefs.Handle, error) {
2015-09-05 20:36:26 +02:00
cryptfs.Debug.Printf("Create\n")
2015-09-05 19:07:20 +02:00
req.Flags, _ = fixFlags(req.Flags)
2015-09-05 11:49:05 +02:00
req.Name = d.crfs.EncryptPath(req.Name)
n, _, err := d.Dir.Create(ctx, req, resp)
if err != nil {
return nil, nil, err
}
clueFile := n.(*cluefs.File)
2015-09-05 14:08:00 +02:00
cryptFile := &File {
File: clueFile,
crfs: d.crfs,
}
2015-09-05 11:49:05 +02:00
return cryptFile, cryptFile, nil
}