aboutsummaryrefslogtreecommitdiff
path: root/internal/fusefrontend_reverse/node.go
blob: 0b2df12c1114203c5464ff5193e11d881677ca84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package fusefrontend_reverse

import (
	"context"
	"syscall"

	"golang.org/x/sys/unix"

	"github.com/hanwen/go-fuse/v2/fs"
	"github.com/hanwen/go-fuse/v2/fuse"

	"github.com/rfjakob/gocryptfs/internal/configfile"
	"github.com/rfjakob/gocryptfs/internal/pathiv"
	"github.com/rfjakob/gocryptfs/internal/syscallcompat"
)

// Node is a file or directory in the filesystem tree
// in a `gocryptfs -reverse` mount.
type Node struct {
	fs.Inode
}

// Lookup - FUSE call for discovering a file.
func (n *Node) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (ch *fs.Inode, errno syscall.Errno) {
	dirfd := int(-1)
	pName := ""
	t := n.lookupFileType(name)
	// gocryptfs.conf
	if t == typeConfig {
		var err error
		rn := n.rootNode()
		dirfd, err = syscallcompat.OpenDirNofollow(rn.args.Cipherdir, "")
		if err != nil {
			errno = fs.ToErrno(err)
			return
		}
		defer syscall.Close(dirfd)
		pName = configfile.ConfReverseName
	} else if t == typeDiriv {
		// gocryptfs.diriv
		dirfd, pName, errno = n.prepareAtSyscall("")
		if errno != 0 {
			return
		}
		defer syscall.Close(dirfd)
		st, err := syscallcompat.Fstatat2(dirfd, pName, unix.AT_SYMLINK_NOFOLLOW)
		if err != nil {
			errno = fs.ToErrno(err)
			return
		}
		content := pathiv.Derive(n.Path(), pathiv.PurposeDirIV)
		var vf *virtualFile
		vf, errno = n.newVirtualFile(content, st, inoTagDirIV)
		if errno != 0 {
			return nil, errno
		}
		out.Attr = vf.attr
		// Create child node
		id := fs.StableAttr{Mode: uint32(vf.attr.Mode), Gen: 1, Ino: vf.attr.Ino}
		ch = n.NewInode(ctx, vf, id)
		return
	} else if t == typeName {
		// gocryptfs.longname.*.name

		// TODO
	} else if t == typeReal {
		// real file
		dirfd, pName, errno = n.prepareAtSyscall(name)
		if errno != 0 {
			return
		}
		defer syscall.Close(dirfd)
	}

	// Get device number and inode number into `st`
	st, err := syscallcompat.Fstatat2(dirfd, pName, unix.AT_SYMLINK_NOFOLLOW)
	if err != nil {
		return nil, fs.ToErrno(err)
	}

	// Create new inode and fill `out`
	ch = n.newChild(ctx, st, out)

	if t == typeReal {
		// Translate ciphertext size in `out.Attr.Size` to plaintext size
		n.translateSize(dirfd, pName, &out.Attr)
	}

	return ch, 0
}

// GetAttr - FUSE call for stat()ing a file.
//
// GetAttr is symlink-safe through use of openBackingDir() and Fstatat().
func (n *Node) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) (errno syscall.Errno) {
	// If the kernel gives us a file handle, use it.
	if f != nil {
		return f.(fs.FileGetattrer).Getattr(ctx, out)
	}

	dirfd, pName, errno := n.prepareAtSyscall("")
	if errno != 0 {
		return
	}
	defer syscall.Close(dirfd)

	st, err := syscallcompat.Fstatat2(dirfd, pName, unix.AT_SYMLINK_NOFOLLOW)
	if err != nil {
		return fs.ToErrno(err)
	}

	// Fix inode number
	rn := n.rootNode()
	rn.inoMap.TranslateStat(st)
	out.Attr.FromStat(st)

	// Translate ciphertext size in `out.Attr.Size` to plaintext size
	n.translateSize(dirfd, pName, &out.Attr)

	if rn.args.ForceOwner != nil {
		out.Owner = *rn.args.ForceOwner
	}
	return 0
}