2020-07-11 19:44:45 +02:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-31 13:24:25 +02:00
|
|
|
"sync/atomic"
|
2020-07-26 19:49:26 +02:00
|
|
|
"syscall"
|
2020-07-11 19:44:45 +02:00
|
|
|
|
2020-07-11 20:27:47 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
|
2020-07-11 19:44:45 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2020-07-26 19:49:26 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2020-07-11 19:44:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// toFuseCtx tries to extract a fuse.Context from a generic context.Context.
|
|
|
|
func toFuseCtx(ctx context.Context) (ctx2 *fuse.Context) {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if caller, ok := fuse.FromContext(ctx); ok {
|
|
|
|
ctx2 = &fuse.Context{
|
|
|
|
Caller: *caller,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx2
|
|
|
|
}
|
2020-07-11 20:27:47 +02:00
|
|
|
|
|
|
|
// toNode casts a generic fs.InodeEmbedder into *Node. Also handles *RootNode
|
|
|
|
// by return rn.Node.
|
|
|
|
func toNode(op fs.InodeEmbedder) *Node {
|
|
|
|
if r, ok := op.(*RootNode); ok {
|
|
|
|
return &r.Node
|
|
|
|
}
|
|
|
|
return op.(*Node)
|
|
|
|
}
|
2020-07-26 19:49:26 +02:00
|
|
|
|
|
|
|
// readlink reads and decrypts a symlink. Used by Readlink, Getattr, Lookup.
|
|
|
|
func (n *Node) readlink(dirfd int, cName string) (out []byte, errno syscall.Errno) {
|
|
|
|
cTarget, err := syscallcompat.Readlinkat(dirfd, cName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fs.ToErrno(err)
|
|
|
|
}
|
|
|
|
rn := n.rootNode()
|
|
|
|
if rn.args.PlaintextNames {
|
|
|
|
return []byte(cTarget), 0
|
|
|
|
}
|
|
|
|
// Symlinks are encrypted like file contents (GCM) and base64-encoded
|
|
|
|
target, err := rn.decryptSymlinkTarget(cTarget)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Readlink %q: decrypting target failed: %v", cName, err)
|
|
|
|
return nil, syscall.EIO
|
|
|
|
}
|
|
|
|
return []byte(target), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// translateSize translates the ciphertext size in `out` into plaintext size.
|
2021-06-06 19:22:16 +02:00
|
|
|
// Handles regular files & symlinks (and finds out what is what by looking at
|
|
|
|
// `out.Mode`).
|
2020-07-26 19:49:26 +02:00
|
|
|
func (n *Node) translateSize(dirfd int, cName string, out *fuse.Attr) {
|
|
|
|
if out.IsRegular() {
|
|
|
|
rn := n.rootNode()
|
|
|
|
out.Size = rn.contentEnc.CipherSizeToPlainSize(out.Size)
|
|
|
|
} else if out.IsSymlink() {
|
|
|
|
target, _ := n.readlink(dirfd, cName)
|
|
|
|
out.Size = uint64(len(target))
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 19:26:42 +02:00
|
|
|
|
|
|
|
// Path returns the relative plaintext path of this node
|
|
|
|
func (n *Node) Path() string {
|
|
|
|
return n.Inode.Path(n.Root())
|
|
|
|
}
|
|
|
|
|
|
|
|
// rootNode returns the Root Node of the filesystem.
|
|
|
|
func (n *Node) rootNode() *RootNode {
|
|
|
|
return n.Root().Operations().(*RootNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newChild attaches a new child inode to n.
|
2021-03-21 09:02:50 +01:00
|
|
|
// The passed-in `st` will be modified to get a unique inode number
|
|
|
|
// (or, in `-sharedstorage` mode, the inode number will be set to zero).
|
2020-08-01 19:26:42 +02:00
|
|
|
func (n *Node) newChild(ctx context.Context, st *syscall.Stat_t, out *fuse.EntryOut) *fs.Inode {
|
|
|
|
rn := n.rootNode()
|
2021-03-07 17:22:29 +01:00
|
|
|
// Get stable inode number based on underlying (device,ino) pair
|
2020-08-01 19:26:42 +02:00
|
|
|
rn.inoMap.TranslateStat(st)
|
|
|
|
out.Attr.FromStat(st)
|
2021-07-31 13:24:25 +02:00
|
|
|
|
|
|
|
var gen uint64 = 1
|
2021-08-11 20:21:32 +02:00
|
|
|
if rn.args.SharedStorage || rn.quirks&syscallcompat.QuirkDuplicateIno1 != 0 {
|
2021-07-31 13:24:25 +02:00
|
|
|
// Make each directory entry a unique node by using a unique generation
|
|
|
|
// value - see the comment at RootNode.gen for details.
|
|
|
|
gen = atomic.AddUint64(&rn.gen, 1)
|
|
|
|
}
|
|
|
|
|
2020-08-01 19:26:42 +02:00
|
|
|
// Create child node
|
|
|
|
id := fs.StableAttr{
|
|
|
|
Mode: uint32(st.Mode),
|
2021-07-31 13:24:25 +02:00
|
|
|
Gen: gen,
|
2020-08-01 19:26:42 +02:00
|
|
|
Ino: st.Ino,
|
|
|
|
}
|
|
|
|
node := &Node{}
|
|
|
|
return n.NewInode(ctx, node, id)
|
|
|
|
}
|