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 (
|
2015-11-27 00:03:10 +01:00
|
|
|
"encoding/base64"
|
2015-09-08 00:54:24 +02:00
|
|
|
"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"
|
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 {
|
2016-09-25 18:04:44 +02:00
|
|
|
cryptoCore := cryptocore.New(args.Masterkey, args.CryptoBackend, contentenc.DefaultIVBits)
|
2016-02-06 20:22:45 +01:00
|
|
|
contentEnc := contentenc.New(cryptoCore, contentenc.DefaultBS)
|
2016-11-01 18:43:22 +01:00
|
|
|
nameTransform := nametransform.New(cryptoCore, args.LongNames, args.Raw64)
|
2016-02-06 19:20:54 +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))
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return a, status
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always need read access to do read-modify-write cycles
|
|
|
|
func (fs *FS) mangleOpenFlags(flags uint32) (newFlags int, writeOnly bool) {
|
|
|
|
newFlags = int(flags)
|
2015-10-04 14:36:20 +02:00
|
|
|
if newFlags&os.O_WRONLY > 0 {
|
2015-09-08 00:54:24 +02:00
|
|
|
writeOnly = true
|
|
|
|
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
|
|
|
|
|
|
|
|
return newFlags, writeOnly
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
iflags, writeOnly := 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)
|
2015-11-27 00:03:10 +01:00
|
|
|
f, err := os.OpenFile(cPath, iflags, 0666)
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
2016-05-29 22:41:46 +02:00
|
|
|
return NewFile(f, writeOnly, fs.contentEnc)
|
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
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
iflags, writeOnly := 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
|
2016-07-03 20:17:40 +02:00
|
|
|
fdRaw, err = syscallcompat.Openat(int(dirfd.Fd()), cName, iflags|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
|
|
|
|
fd, err = os.OpenFile(cPath, iflags|os.O_CREATE, os.FileMode(mode))
|
|
|
|
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-10-10 08:43:09 +02:00
|
|
|
tlog.Warn.Printf("Create: Chown failed: %v", err)
|
2016-06-26 19:18:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-29 22:41:46 +02:00
|
|
|
return NewFile(fd, writeOnly, fs.contentEnc)
|
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)
|
|
|
|
}
|
2016-06-08 00:17:18 +02:00
|
|
|
return fuse.ToStatus(os.Lchown(cPath, int(uid), int(gid)))
|
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
|
|
|
}
|
2015-11-27 00:03:10 +01:00
|
|
|
cPath, err := fs.encryptPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
|
|
|
// Handle long file name
|
|
|
|
cName := filepath.Base(cPath)
|
|
|
|
if nametransform.IsLongContent(cName) {
|
|
|
|
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()
|
|
|
|
|
|
|
|
// Create ".name"
|
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cName, path)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create device node
|
2016-07-03 20:21:29 +02:00
|
|
|
err = syscallcompat.Mknodat(int(dirfd.Fd()), cName, uint32(mode), int(dev))
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
nametransform.DeleteLongName(dirfd, cName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
2015-11-27 00:03:10 +01:00
|
|
|
return fs.FileSystem.Mknod(cPath, mode, dev, context)
|
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
|
2015-11-28 16:47:30 +01:00
|
|
|
cBinTarget, err := base64.URLEncoding.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)
|
2015-11-27 00:03:10 +01:00
|
|
|
cTarget := base64.URLEncoding.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()
|
|
|
|
|
|
|
|
// Create ".name"
|
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cName, linkName)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create symlink
|
|
|
|
// TODO use syscall.Symlinkat once it is available in Go
|
|
|
|
err = syscall.Symlink(cTarget, cPath)
|
|
|
|
if err != nil {
|
|
|
|
nametransform.DeleteLongName(dirfd, cName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuse.ToStatus(err)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
|
2015-11-28 16:47:30 +01:00
|
|
|
err = os.Symlink(cTarget, cPath)
|
|
|
|
return fuse.ToStatus(err)
|
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)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02: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
|
|
|
|
}
|