2016-07-06 21:51:25 +02:00
|
|
|
// Package fusefrontend interfaces directly with the go-fuse library.
|
2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2015-12-19 13:21:15 +01:00
|
|
|
// FUSE operations on paths
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
import (
|
|
|
|
"os"
|
2016-04-10 19:32:10 +02:00
|
|
|
"path/filepath"
|
2015-11-27 22:20:01 +01:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2015-09-08 00:54:24 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/pathfs"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2016-02-06 20:23:36 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2017-03-18 16:01:50 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/serialize_reads"
|
2016-07-03 20:05:32 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-09-08 00:54:24 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// FS implements the go-fuse virtual filesystem interface.
|
2015-09-08 00:54:24 +02:00
|
|
|
type FS struct {
|
2015-11-29 21:55:20 +01:00
|
|
|
pathfs.FileSystem // loopbackFileSystem, see go-fuse/fuse/pathfs/loopback.go
|
|
|
|
args Args // Stores configuration arguments
|
2015-11-27 21:48:58 +01:00
|
|
|
// dirIVLock: Lock()ed if any "gocryptfs.diriv" file is modified
|
|
|
|
// Readers must RLock() it to prevent them from seeing intermediate
|
|
|
|
// states
|
|
|
|
dirIVLock sync.RWMutex
|
2016-02-06 19:20:54 +01:00
|
|
|
// Filename encryption helper
|
|
|
|
nameTransform *nametransform.NameTransform
|
|
|
|
// Content encryption helper
|
|
|
|
contentEnc *contentenc.ContentEnc
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
var _ pathfs.FileSystem = &FS{} // Verify that interface is implemented.
|
|
|
|
|
|
|
|
// NewFS returns a new encrypted FUSE overlay filesystem.
|
2015-11-28 16:52:57 +01:00
|
|
|
func NewFS(args Args) *FS {
|
2017-04-08 02:09:28 +02:00
|
|
|
cryptoCore := cryptocore.New(args.Masterkey, args.CryptoBackend, contentenc.DefaultIVBits, args.HKDF, args.ForceDecode)
|
|
|
|
contentEnc := contentenc.New(cryptoCore, contentenc.DefaultBS, args.ForceDecode)
|
2017-03-05 13:58:24 +01:00
|
|
|
nameTransform := nametransform.New(cryptoCore.EMECipher, args.LongNames, args.Raw64)
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2017-03-18 16:01:50 +01:00
|
|
|
if args.SerializeReads {
|
2017-04-29 14:50:58 +02:00
|
|
|
serialize_reads.InitSerializer()
|
2017-03-18 16:01:50 +01:00
|
|
|
}
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
return &FS{
|
2016-02-06 20:23:36 +01:00
|
|
|
FileSystem: pathfs.NewLoopbackFileSystem(args.Cipherdir),
|
|
|
|
args: args,
|
2016-02-06 19:20:54 +01:00
|
|
|
nameTransform: nameTransform,
|
2016-02-06 20:23:36 +01:00
|
|
|
contentEnc: contentEnc,
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// GetAttr implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("FS.GetAttr('%s')", name)
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(name) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return nil, fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
cName, err := fs.encryptPath(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2015-09-16 19:32:37 +02:00
|
|
|
a, status := fs.FileSystem.GetAttr(cName, context)
|
2015-09-08 00:54:24 +02:00
|
|
|
if a == nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("FS.GetAttr failed: %s", status.String())
|
2015-09-08 00:54:24 +02:00
|
|
|
return a, status
|
|
|
|
}
|
2015-09-16 19:32:37 +02:00
|
|
|
if a.IsRegular() {
|
2016-02-06 19:20:54 +01:00
|
|
|
a.Size = fs.contentEnc.CipherSizeToPlainSize(a.Size)
|
2015-09-16 19:32:37 +02:00
|
|
|
} else if a.IsSymlink() {
|
|
|
|
target, _ := fs.Readlink(name, context)
|
|
|
|
a.Size = uint64(len(target))
|
|
|
|
}
|
2017-05-30 23:01:06 +02:00
|
|
|
if fs.args.ForceOwner != nil {
|
|
|
|
a.Owner = *fs.args.ForceOwner
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return a, status
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:49:37 +02:00
|
|
|
// mangleOpenFlags is used by Create() and Open() to convert the open flags the user
|
|
|
|
// wants to the flags we internally use to open the backing file.
|
|
|
|
func (fs *FS) mangleOpenFlags(flags uint32) (newFlags int) {
|
2015-09-08 00:54:24 +02:00
|
|
|
newFlags = int(flags)
|
2017-05-01 17:49:37 +02:00
|
|
|
// Convert WRONLY to RDWR. We always need read access to do read-modify-write cycles.
|
2015-10-04 14:36:20 +02:00
|
|
|
if newFlags&os.O_WRONLY > 0 {
|
2015-09-08 00:54:24 +02:00
|
|
|
newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR
|
|
|
|
}
|
|
|
|
// We also cannot open the file in append mode, we need to seek back for RMW
|
|
|
|
newFlags = newFlags &^ os.O_APPEND
|
|
|
|
|
2017-05-01 17:49:37 +02:00
|
|
|
return newFlags
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Open implements pathfs.Filesystem.
|
2015-11-03 00:00:13 +01:00
|
|
|
func (fs *FS) Open(path string, flags uint32, context *fuse.Context) (fuseFile nodefs.File, status fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 00:00:13 +01:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2017-05-01 17:49:37 +02:00
|
|
|
newFlags := fs.mangleOpenFlags(flags)
|
2015-11-27 00:03:10 +01:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Open: getBackingPath: %v", err)
|
2015-11-27 00:03:10 +01:00
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Open: %s", cPath)
|
2017-05-01 17:49:37 +02:00
|
|
|
f, err := os.OpenFile(cPath, newFlags, 0666)
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
2017-05-03 23:46:52 +02:00
|
|
|
err2 := err.(*os.PathError)
|
|
|
|
if err2.Err == syscall.EMFILE {
|
|
|
|
var lim syscall.Rlimit
|
|
|
|
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
|
|
|
|
tlog.Warn.Printf("Open %q: too many open files. Current \"ulimit -n\": %d", cPath, lim.Cur)
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:49:37 +02:00
|
|
|
return NewFile(f, fs)
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Create implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) Create(path string, flags uint32, mode uint32, context *fuse.Context) (fuseFile nodefs.File, code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return nil, fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2017-05-01 17:49:37 +02:00
|
|
|
newFlags := fs.mangleOpenFlags(flags)
|
2015-11-27 00:03:10 +01:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
2016-05-29 22:41:46 +02:00
|
|
|
var fd *os.File
|
2016-04-10 19:32:10 +02:00
|
|
|
cName := filepath.Base(cPath)
|
2016-05-29 22:41:46 +02:00
|
|
|
|
|
|
|
// Handle long file name
|
2016-04-10 19:32:10 +02:00
|
|
|
if nametransform.IsLongContent(cName) {
|
2016-04-10 21:29:42 +02:00
|
|
|
var dirfd *os.File
|
|
|
|
dirfd, err = os.Open(filepath.Dir(cPath))
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer dirfd.Close()
|
|
|
|
|
|
|
|
// Create ".name"
|
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cName, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create content
|
2016-04-10 21:29:42 +02:00
|
|
|
var fdRaw int
|
2017-05-01 17:49:37 +02:00
|
|
|
fdRaw, err = syscallcompat.Openat(int(dirfd.Fd()), cName, newFlags|os.O_CREATE, mode)
|
2016-02-06 22:54:14 +01:00
|
|
|
if err != nil {
|
2016-04-10 19:32:10 +02:00
|
|
|
nametransform.DeleteLongName(dirfd, cName)
|
2016-02-06 22:54:14 +01:00
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-05-29 22:41:46 +02:00
|
|
|
fd = os.NewFile(uintptr(fdRaw), cName)
|
|
|
|
} else {
|
|
|
|
// Normal (short) file name
|
2017-05-01 17:49:37 +02:00
|
|
|
fd, err = os.OpenFile(cPath, newFlags|os.O_CREATE, os.FileMode(mode))
|
2016-05-29 22:41:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
2016-10-10 08:43:09 +02:00
|
|
|
// Set owner
|
2016-06-26 19:18:13 +02:00
|
|
|
if fs.args.PreserveOwner {
|
|
|
|
err = fd.Chown(int(context.Owner.Uid), int(context.Owner.Gid))
|
|
|
|
if err != nil {
|
2016-11-28 22:46:04 +01:00
|
|
|
tlog.Warn.Printf("Create: fd.Chown failed: %v", err)
|
2016-06-26 19:18:13 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-01 17:49:37 +02:00
|
|
|
return NewFile(fd, fs)
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Chmod implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) Chmod(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2016-06-26 20:13:21 +02:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-06-26 20:13:21 +02:00
|
|
|
// os.Chmod goes through the "syscallMode" translation function that messes
|
|
|
|
// up the suid and sgid bits. So use syscall.Chmod directly.
|
|
|
|
err = syscall.Chmod(cPath, mode)
|
|
|
|
return fuse.ToStatus(err)
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Chown implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) Chown(path string, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2016-06-08 00:17:18 +02:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2017-03-02 19:11:24 +01:00
|
|
|
code = fuse.ToStatus(os.Lchown(cPath, int(uid), int(gid)))
|
|
|
|
if !code.Ok() {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
if !fs.args.PlaintextNames {
|
|
|
|
// When filename encryption is active, every directory contains
|
|
|
|
// a "gocryptfs.diriv" file. This file should also change the owner.
|
|
|
|
// Instead of checking if "cPath" is a directory, we just blindly
|
|
|
|
// execute the Lchown on "cPath/gocryptfs.diriv" and ignore errors.
|
|
|
|
dirIVPath := filepath.Join(cPath, nametransform.DirIVFilename)
|
|
|
|
os.Lchown(dirIVPath, int(uid), int(gid))
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-09-18 22:22:11 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Mknod implements pathfs.Filesystem.
|
2015-11-27 00:03:10 +01:00
|
|
|
func (fs *FS) Mknod(path string, mode uint32, dev uint32, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2016-11-28 23:09:47 +01:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-11-28 23:09:47 +01:00
|
|
|
// Create ".name" file to store long file name
|
2016-04-10 19:32:10 +02:00
|
|
|
cName := filepath.Base(cPath)
|
|
|
|
if nametransform.IsLongContent(cName) {
|
2016-11-28 23:09:47 +01:00
|
|
|
var dirfd *os.File
|
|
|
|
dirfd, err = os.Open(filepath.Dir(cPath))
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
defer dirfd.Close()
|
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cName, path)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-11-28 23:09:47 +01:00
|
|
|
// Create "gocryptfs.longfile." device node
|
|
|
|
err = syscallcompat.Mknodat(int(dirfd.Fd()), cName, mode, int(dev))
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
nametransform.DeleteLongName(dirfd, cName)
|
|
|
|
}
|
2016-11-28 23:09:47 +01:00
|
|
|
} else {
|
|
|
|
// Create regular device node
|
|
|
|
err = syscall.Mknod(cPath, mode, int(dev))
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-10 19:32:10 +02:00
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-11-28 23:09:47 +01:00
|
|
|
// Set owner
|
|
|
|
if fs.args.PreserveOwner {
|
|
|
|
err = os.Lchown(cPath, int(context.Owner.Uid), int(context.Owner.Gid))
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Mknod: Lchown failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Truncate implements pathfs.Filesystem.
|
2016-06-26 18:35:19 +02:00
|
|
|
// Support truncate(2) by opening the file and calling ftruncate(2)
|
|
|
|
// While the glibc "truncate" wrapper seems to always use ftruncate, fsstress from
|
|
|
|
// xfstests uses this a lot by calling "truncate64" directly.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) Truncate(path string, offset uint64, context *fuse.Context) (code fuse.Status) {
|
2016-06-26 18:35:19 +02:00
|
|
|
file, code := fs.Open(path, uint32(os.O_RDWR), context)
|
|
|
|
if code != fuse.OK {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
code = file.Truncate(offset)
|
|
|
|
file.Release()
|
|
|
|
return code
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Utimens implements pathfs.Filesystem.
|
2016-08-09 22:18:46 +02:00
|
|
|
func (fs *FS) Utimens(path string, a *time.Time, m *time.Time, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2016-09-25 16:30:29 +02:00
|
|
|
cPath, err := fs.encryptPath(path)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-09-25 16:30:29 +02:00
|
|
|
return fs.FileSystem.Utimens(cPath, a, m, context)
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// StatFs implements pathfs.Filesystem.
|
2016-05-12 19:51:13 +02:00
|
|
|
func (fs *FS) StatFs(path string) *fuse.StatfsOut {
|
|
|
|
if fs.isFiltered(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cPath, err := fs.encryptPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fs.FileSystem.StatFs(cPath)
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Readlink implements pathfs.Filesystem.
|
2015-11-27 00:03:10 +01:00
|
|
|
func (fs *FS) Readlink(path string, context *fuse.Context) (out string, status fuse.Status) {
|
2016-09-25 17:01:04 +02:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
|
|
|
}
|
2016-09-25 17:01:04 +02:00
|
|
|
cTarget, err := os.Readlink(cPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
2015-09-09 19:32:59 +02:00
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
if fs.args.PlaintextNames {
|
|
|
|
return cTarget, fuse.OK
|
2015-11-28 16:52:57 +01:00
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
// Symlinks are encrypted like file contents (GCM) and base64-encoded
|
2017-03-05 22:59:25 +01:00
|
|
|
cBinTarget, err := fs.nameTransform.B64.DecodeString(cTarget)
|
2015-09-09 19:32:59 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Readlink: %v", err)
|
2015-09-09 19:32:59 +02:00
|
|
|
return "", fuse.EIO
|
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
target, err := fs.contentEnc.DecryptBlock([]byte(cBinTarget), 0, nil)
|
2015-11-28 16:47:30 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Readlink: %v", err)
|
2015-11-28 16:47:30 +01:00
|
|
|
return "", fuse.EIO
|
|
|
|
}
|
|
|
|
return string(target), fuse.OK
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Unlink implements pathfs.Filesystem.
|
2015-11-27 00:03:10 +01:00
|
|
|
func (fs *FS) Unlink(path string, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
2015-09-16 19:32:37 +02:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
|
|
|
cName := filepath.Base(cPath)
|
|
|
|
if nametransform.IsLongContent(cName) {
|
2016-04-10 21:29:42 +02:00
|
|
|
var dirfd *os.File
|
|
|
|
dirfd, err = os.Open(filepath.Dir(cPath))
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer dirfd.Close()
|
|
|
|
// Delete content
|
2016-07-03 20:17:40 +02:00
|
|
|
err = syscallcompat.Unlinkat(int(dirfd.Fd()), cName)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Delete ".name"
|
|
|
|
err = nametransform.DeleteLongName(dirfd, cName)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Unlink: could not delete .name file: %v", err)
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
|
|
|
err = syscall.Unlink(cPath)
|
2016-02-07 14:02:09 +01:00
|
|
|
return fuse.ToStatus(err)
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Symlink implements pathfs.Filesystem.
|
2015-11-27 00:03:10 +01:00
|
|
|
func (fs *FS) Symlink(target string, linkName string, context *fuse.Context) (code fuse.Status) {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Symlink(\"%s\", \"%s\")", target, linkName)
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(linkName) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-11-28 16:47:30 +01:00
|
|
|
cPath, err := fs.getBackingPath(linkName)
|
2015-11-27 00:03:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
if fs.args.PlaintextNames {
|
|
|
|
err = os.Symlink(target, cPath)
|
2015-11-28 16:52:57 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
// Symlinks are encrypted like file contents (GCM) and base64-encoded
|
2016-09-29 21:29:45 +02:00
|
|
|
cBinTarget := fs.contentEnc.EncryptBlock([]byte(target), 0, nil)
|
2017-03-05 22:59:25 +01:00
|
|
|
cTarget := fs.nameTransform.B64.EncodeToString(cBinTarget)
|
2016-04-10 19:32:10 +02:00
|
|
|
// Handle long file name
|
|
|
|
cName := filepath.Base(cPath)
|
|
|
|
if nametransform.IsLongContent(cName) {
|
2016-04-10 21:29:42 +02:00
|
|
|
var dirfd *os.File
|
|
|
|
dirfd, err = os.Open(filepath.Dir(cPath))
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
defer dirfd.Close()
|
2016-11-28 23:15:24 +01:00
|
|
|
// Create ".name" file
|
2016-04-10 19:32:10 +02:00
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cName, linkName)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-11-28 23:15:24 +01:00
|
|
|
// Create "gocryptfs.longfile." symlink
|
2016-04-10 19:32:10 +02:00
|
|
|
// TODO use syscall.Symlinkat once it is available in Go
|
|
|
|
err = syscall.Symlink(cTarget, cPath)
|
|
|
|
if err != nil {
|
|
|
|
nametransform.DeleteLongName(dirfd, cName)
|
|
|
|
}
|
2016-11-28 23:15:24 +01:00
|
|
|
} else {
|
|
|
|
// Create symlink
|
|
|
|
err = os.Symlink(cTarget, cPath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-04-10 19:32:10 +02:00
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-11-28 23:15:24 +01:00
|
|
|
// Set owner
|
|
|
|
if fs.args.PreserveOwner {
|
|
|
|
err = os.Lchown(cPath, int(context.Owner.Uid), int(context.Owner.Gid))
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Mknod: Lchown failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Rename implements pathfs.Filesystem.
|
2015-11-03 22:25:29 +01:00
|
|
|
func (fs *FS) Rename(oldPath string, newPath string, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(newPath) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
cOldPath, err := fs.getBackingPath(oldPath)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
cNewPath, err := fs.getBackingPath(newPath)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2015-11-29 21:41:38 +01:00
|
|
|
// The Rename may cause a directory to take the place of another directory.
|
|
|
|
// That directory may still be in the DirIV cache, clear it.
|
2016-02-06 19:20:54 +01:00
|
|
|
fs.nameTransform.DirIVCache.Clear()
|
2015-11-29 21:41:38 +01:00
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
// Handle long source file name
|
|
|
|
var oldDirFd *os.File
|
|
|
|
var finalOldDirFd int
|
|
|
|
var finalOldPath = cOldPath
|
|
|
|
cOldName := filepath.Base(cOldPath)
|
|
|
|
if nametransform.IsLongContent(cOldName) {
|
|
|
|
oldDirFd, err = os.Open(filepath.Dir(cOldPath))
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer oldDirFd.Close()
|
|
|
|
finalOldDirFd = int(oldDirFd.Fd())
|
2016-07-03 20:05:32 +02:00
|
|
|
// Use relative path
|
2016-04-10 19:32:10 +02:00
|
|
|
finalOldPath = cOldName
|
|
|
|
}
|
|
|
|
// Handle long destination file name
|
|
|
|
var newDirFd *os.File
|
|
|
|
var finalNewDirFd int
|
|
|
|
var finalNewPath = cNewPath
|
|
|
|
cNewName := filepath.Base(cNewPath)
|
|
|
|
if nametransform.IsLongContent(cNewName) {
|
|
|
|
newDirFd, err = os.Open(filepath.Dir(cNewPath))
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer newDirFd.Close()
|
|
|
|
finalNewDirFd = int(newDirFd.Fd())
|
2016-07-03 20:05:32 +02:00
|
|
|
// Use relative path
|
2016-04-10 19:32:10 +02:00
|
|
|
finalNewPath = cNewName
|
|
|
|
// Create destination .name file
|
|
|
|
err = fs.nameTransform.WriteLongName(newDirFd, cNewName, newPath)
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Actual rename
|
2016-07-03 20:08:09 +02:00
|
|
|
tlog.Debug.Printf("Renameat oldfd=%d oldpath=%s newfd=%d newpath=%s\n", finalOldDirFd, finalOldPath, finalNewDirFd, finalNewPath)
|
2016-07-03 20:05:32 +02:00
|
|
|
err = syscallcompat.Renameat(finalOldDirFd, finalOldPath, finalNewDirFd, finalNewPath)
|
2016-06-30 23:12:22 +02:00
|
|
|
if err == syscall.ENOTEMPTY || err == syscall.EEXIST {
|
|
|
|
// If an empty directory is overwritten we will always get an error as
|
2016-04-10 19:32:10 +02:00
|
|
|
// the "empty" directory will still contain gocryptfs.diriv.
|
2016-06-30 23:12:22 +02:00
|
|
|
// Interestingly, ext4 returns ENOTEMPTY while xfs returns EEXIST.
|
|
|
|
// We handle that by trying to fs.Rmdir() the target directory and trying
|
|
|
|
// again.
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rename: Handling ENOTEMPTY")
|
2015-12-19 13:21:15 +01:00
|
|
|
if fs.Rmdir(newPath, context) == fuse.OK {
|
2016-07-03 20:05:32 +02:00
|
|
|
err = syscallcompat.Renameat(finalOldDirFd, finalOldPath, finalNewDirFd, finalNewPath)
|
2015-12-19 13:21:15 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
if newDirFd != nil {
|
|
|
|
// Roll back .name creation
|
|
|
|
nametransform.DeleteLongName(newDirFd, cNewName)
|
|
|
|
}
|
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
if oldDirFd != nil {
|
|
|
|
nametransform.DeleteLongName(oldDirFd, cOldName)
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Link implements pathfs.Filesystem.
|
2015-11-27 00:03:10 +01:00
|
|
|
func (fs *FS) Link(oldPath string, newPath string, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(newPath) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
cOldPath, err := fs.getBackingPath(oldPath)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
cNewPath, err := fs.getBackingPath(newPath)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
// Handle long file name
|
|
|
|
cNewName := filepath.Base(cNewPath)
|
|
|
|
if nametransform.IsLongContent(cNewName) {
|
|
|
|
dirfd, err := os.Open(filepath.Dir(cNewPath))
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer dirfd.Close()
|
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cNewName, newPath)
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
// TODO Use syscall.Linkat once it is available in Go (it is not in Go
|
|
|
|
// 1.6).
|
|
|
|
err = syscall.Link(cOldPath, cNewPath)
|
|
|
|
if err != nil {
|
|
|
|
nametransform.DeleteLongName(dirfd, cNewName)
|
|
|
|
}
|
2017-01-26 20:56:42 +01:00
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
return fuse.ToStatus(os.Link(cOldPath, cNewPath))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// Access implements pathfs.Filesystem.
|
2015-11-27 00:03:10 +01:00
|
|
|
func (fs *FS) Access(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
2015-12-08 16:13:29 +01:00
|
|
|
if fs.isFiltered(path) {
|
2015-11-03 22:25:29 +01:00
|
|
|
return fuse.EPERM
|
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
cPath, err := fs.getBackingPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
return fuse.ToStatus(syscall.Access(cPath, mode))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// GetXAttr implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) GetXAttr(name string, attr string, context *fuse.Context) ([]byte, fuse.Status) {
|
|
|
|
return nil, fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// SetXAttr implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) SetXAttr(name string, attr string, data []byte, flags int, context *fuse.Context) fuse.Status {
|
|
|
|
return fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// ListXAttr implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) ListXAttr(name string, context *fuse.Context) ([]string, fuse.Status) {
|
|
|
|
return nil, fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// RemoveXAttr implements pathfs.Filesystem.
|
2015-09-08 00:54:24 +02:00
|
|
|
func (fs *FS) RemoveXAttr(name string, attr string, context *fuse.Context) fuse.Status {
|
|
|
|
return fuse.ENOSYS
|
|
|
|
}
|