2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2015-12-11 19:54:53 +01:00
|
|
|
|
2015-12-19 13:21:15 +01:00
|
|
|
// Mkdir and Rmdir
|
|
|
|
|
2015-12-11 19:54:53 +01:00
|
|
|
import (
|
2015-12-13 20:10:52 +01:00
|
|
|
"fmt"
|
2016-06-27 00:25:56 +02:00
|
|
|
"io"
|
2017-09-05 21:47:05 +02:00
|
|
|
"runtime"
|
2015-12-11 19:54:53 +01:00
|
|
|
"syscall"
|
|
|
|
|
2017-11-29 12:23:40 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2015-12-11 19:54:53 +01:00
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2016-02-07 14:02:09 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
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-12-11 19:54:53 +01:00
|
|
|
)
|
|
|
|
|
2017-09-05 22:47:15 +02:00
|
|
|
const dsStoreName = ".DS_Store"
|
|
|
|
|
2017-11-30 20:53:38 +01:00
|
|
|
// mkdirWithIv - create a new directory and corresponding diriv file. dirfd
|
|
|
|
// should be a handle to the parent directory, cName is the name of the new
|
|
|
|
// directory and mode specifies the access permissions to use.
|
2019-01-12 20:57:31 +01:00
|
|
|
func (fs *FS) mkdirWithIv(dirfd int, cName string, mode uint32, context *fuse.Context) error {
|
2016-04-10 19:32:10 +02:00
|
|
|
// Between the creation of the directory and the creation of gocryptfs.diriv
|
2016-10-10 08:43:09 +02:00
|
|
|
// the directory is inconsistent. Take the lock to prevent other readers
|
|
|
|
// from seeing it.
|
2016-04-10 19:32:10 +02:00
|
|
|
fs.dirIVLock.Lock()
|
|
|
|
defer fs.dirIVLock.Unlock()
|
2019-01-12 20:57:31 +01:00
|
|
|
err := syscallcompat.MkdiratUser(dirfd, cName, mode, context)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-03 13:32:13 +01:00
|
|
|
dirfd2, err := syscallcompat.Openat(dirfd, cName, syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscallcompat.O_PATH, 0)
|
|
|
|
if err == nil {
|
|
|
|
// Create gocryptfs.diriv
|
|
|
|
err = nametransform.WriteDirIVAt(dirfd2)
|
|
|
|
syscall.Close(dirfd2)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2019-01-03 13:32:13 +01:00
|
|
|
// Delete inconsistent directory (missing gocryptfs.diriv!)
|
2018-09-22 20:10:34 +02:00
|
|
|
err2 := syscallcompat.Unlinkat(dirfd, cName, unix.AT_REMOVEDIR)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err2 != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("mkdirWithIv: rollback failed: %v", err2)
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:39:19 +02:00
|
|
|
// Mkdir - FUSE call. Create a directory at "newPath" with permissions "mode".
|
|
|
|
//
|
|
|
|
// Symlink-safe through use of Mkdirat().
|
2016-04-10 19:32:10 +02:00
|
|
|
func (fs *FS) Mkdir(newPath string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
2019-01-02 22:32:21 +01:00
|
|
|
defer fs.dirCache.Clear()
|
2016-04-10 19:32:10 +02:00
|
|
|
if fs.isFiltered(newPath) {
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.EPERM
|
|
|
|
}
|
fusefrontend: use OpenDirNofollow in openBackingDir
Rename openBackingPath to openBackingDir and use OpenDirNofollow
to be safe against symlink races. Note that openBackingDir is
not used in several important code paths like Create().
But it is used in Unlink, and the performance impact in the RM benchmark
to be acceptable:
Before
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.bYO: gocryptfs v1.6-12-g930c37e-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.07979 s, 243 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.882413 s, 297 MB/s
UNTAR: 16.703
MD5: 7.606
LS: 1.349
RM: 3.237
After
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.jK3: gocryptfs v1.6-13-g84d6faf-dirty; go-fuse v20170619-49-gb11e293; 2018-09-08 go1.10.3
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 1.06261 s, 247 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0.947228 s, 277 MB/s
UNTAR: 17.197
MD5: 7.540
LS: 1.364
RM: 3.410
2018-09-08 19:27:33 +02:00
|
|
|
dirfd, cName, err := fs.openBackingDir(newPath)
|
2015-12-11 19:54:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2018-09-22 20:10:34 +02:00
|
|
|
defer syscall.Close(dirfd)
|
2019-01-12 20:57:31 +01:00
|
|
|
// Make sure context is nil if we don't want to preserve the owner
|
|
|
|
if !fs.args.PreserveOwner {
|
|
|
|
context = nil
|
2019-01-09 02:33:06 +01:00
|
|
|
}
|
2016-06-27 00:25:56 +02:00
|
|
|
if fs.args.PlaintextNames {
|
2019-01-12 20:57:31 +01:00
|
|
|
err = syscallcompat.MkdiratUser(dirfd, cName, mode, context)
|
|
|
|
return fuse.ToStatus(err)
|
2016-06-27 00:25:56 +02:00
|
|
|
}
|
|
|
|
|
2019-01-12 20:57:31 +01:00
|
|
|
// We need write and execute permissions to create gocryptfs.diriv.
|
|
|
|
// Also, we need read permissions to open the directory (to avoid
|
|
|
|
// race-conditions between getting and setting the mode).
|
|
|
|
origMode := mode
|
|
|
|
mode = mode | 0700
|
2016-04-10 19:32:10 +02:00
|
|
|
|
|
|
|
// Handle long file name
|
|
|
|
if nametransform.IsLongContent(cName) {
|
|
|
|
// Create ".name"
|
2018-11-04 22:05:38 +01:00
|
|
|
err = fs.nameTransform.WriteLongNameAt(dirfd, cName, newPath)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create directory
|
2019-01-12 20:57:31 +01:00
|
|
|
err = fs.mkdirWithIv(dirfd, cName, mode, context)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2018-11-04 22:05:38 +01:00
|
|
|
nametransform.DeleteLongNameAt(dirfd, cName)
|
2016-04-10 19:32:10 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 20:57:31 +01:00
|
|
|
err = fs.mkdirWithIv(dirfd, cName, mode, context)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 20:57:31 +01:00
|
|
|
// Set mode
|
|
|
|
if origMode != mode {
|
|
|
|
dirfd2, err := syscallcompat.Openat(dirfd, cName,
|
|
|
|
syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
2016-10-10 08:43:09 +02:00
|
|
|
if err != nil {
|
2019-01-12 20:57:31 +01:00
|
|
|
tlog.Warn.Printf("Mkdir %q: Openat failed: %v", cName, err)
|
2019-01-09 02:33:06 +01:00
|
|
|
return fuse.ToStatus(err)
|
2016-10-10 08:43:09 +02:00
|
|
|
}
|
2019-01-12 20:57:31 +01:00
|
|
|
defer syscall.Close(dirfd2)
|
|
|
|
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err = syscall.Fstat(dirfd2, &st)
|
2019-01-09 02:33:06 +01:00
|
|
|
if err != nil {
|
2019-01-12 20:57:31 +01:00
|
|
|
tlog.Warn.Printf("Mkdir %q: Fstat failed: %v", cName, err)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preserve SGID bit if it was set due to inheritance.
|
|
|
|
origMode = uint32(st.Mode&^0777) | origMode
|
|
|
|
err = syscall.Fchmod(dirfd2, origMode)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Mkdir %q: Fchmod %#o -> %#o failed: %v", cName, mode, origMode, err)
|
|
|
|
return fuse.ToStatus(err)
|
2016-10-10 08:43:09 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2017-09-05 22:47:15 +02:00
|
|
|
// haveDsstore return true if one of the entries in "names" is ".DS_Store".
|
2018-09-22 20:10:34 +02:00
|
|
|
func haveDsstore(entries []fuse.DirEntry) bool {
|
|
|
|
for _, e := range entries {
|
|
|
|
if e.Name == dsStoreName {
|
2017-09-05 22:47:15 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-04 22:25:32 +01:00
|
|
|
// Rmdir - FUSE call.
|
|
|
|
//
|
|
|
|
// Symlink-safe through Unlinkat() + AT_REMOVEDIR.
|
|
|
|
func (fs *FS) Rmdir(relPath string, context *fuse.Context) (code fuse.Status) {
|
2019-01-02 22:32:21 +01:00
|
|
|
defer fs.dirCache.Clear()
|
2018-11-04 22:25:32 +01:00
|
|
|
parentDirFd, cName, err := fs.openBackingDir(relPath)
|
2015-12-11 19:54:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2018-11-04 22:25:32 +01:00
|
|
|
defer syscall.Close(parentDirFd)
|
2016-06-27 00:25:56 +02:00
|
|
|
if fs.args.PlaintextNames {
|
2018-11-04 22:25:32 +01:00
|
|
|
// Unlinkat with AT_REMOVEDIR is equivalent to Rmdir
|
|
|
|
err = unix.Unlinkat(parentDirFd, cName, unix.AT_REMOVEDIR)
|
2016-04-10 19:32:10 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2018-09-22 20:10:34 +02:00
|
|
|
dirfd, err := syscallcompat.Openat(parentDirFd, cName,
|
2019-01-03 17:53:22 +01:00
|
|
|
syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err == syscall.EACCES {
|
2015-12-11 19:54:53 +01:00
|
|
|
// We need permission to read and modify the directory
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: handling EACCESS")
|
2018-11-04 22:25:32 +01:00
|
|
|
var st unix.Stat_t
|
|
|
|
err = syscallcompat.Fstatat(parentDirFd, cName, &st, unix.AT_SYMLINK_NOFOLLOW)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: Stat: %v", err)
|
2016-04-10 19:32:10 +02:00
|
|
|
return fuse.ToStatus(err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
2018-11-04 22:35:17 +01:00
|
|
|
// This cast is needed on Darwin, where st.Mode is uint16.
|
2019-01-12 21:24:48 +01:00
|
|
|
origMode := uint32(st.Mode)
|
2018-11-04 22:25:32 +01:00
|
|
|
err = syscallcompat.Fchmodat(parentDirFd, cName, origMode|0700, unix.AT_SYMLINK_NOFOLLOW)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2018-11-04 22:25:32 +01:00
|
|
|
tlog.Debug.Printf("Rmdir: Fchmodat failed: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
// Retry open
|
2018-09-22 20:10:34 +02:00
|
|
|
dirfd, err = syscallcompat.Openat(parentDirFd, cName,
|
2019-01-03 17:53:22 +01:00
|
|
|
syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
2016-04-10 19:32:10 +02:00
|
|
|
// Undo the chmod if removing the directory failed
|
2015-12-13 20:10:52 +01:00
|
|
|
defer func() {
|
2015-12-11 19:54:53 +01:00
|
|
|
if code != fuse.OK {
|
2018-11-04 22:25:32 +01:00
|
|
|
err = syscallcompat.Fchmodat(parentDirFd, cName, origMode, unix.AT_SYMLINK_NOFOLLOW)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Rmdir: Chmod rollback failed: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: Open: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2018-09-22 20:10:34 +02:00
|
|
|
defer syscall.Close(dirfd)
|
2017-09-05 22:47:15 +02:00
|
|
|
retry:
|
2017-09-05 22:08:59 +02:00
|
|
|
// Check directory contents
|
2018-09-22 20:10:34 +02:00
|
|
|
children, err := syscallcompat.Getdents(dirfd)
|
2017-09-05 22:08:59 +02:00
|
|
|
if err == io.EOF {
|
2016-06-27 00:25:56 +02:00
|
|
|
// The directory is empty
|
2018-11-04 22:25:32 +01:00
|
|
|
tlog.Warn.Printf("Rmdir: %q: gocryptfs.diriv is missing", cName)
|
|
|
|
err = unix.Unlinkat(parentDirFd, cName, unix.AT_REMOVEDIR)
|
|
|
|
return fuse.ToStatus(err)
|
2017-09-05 22:08:59 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2016-06-27 00:25:56 +02:00
|
|
|
tlog.Warn.Printf("Rmdir: Readdirnames: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2017-09-05 22:47:15 +02:00
|
|
|
// MacOS sprinkles .DS_Store files everywhere. This is hard to avoid for
|
|
|
|
// users, so handle it transparently here.
|
|
|
|
if runtime.GOOS == "darwin" && len(children) <= 2 && haveDsstore(children) {
|
2018-11-04 22:25:32 +01:00
|
|
|
err = unix.Unlinkat(dirfd, dsStoreName, 0)
|
2017-09-05 22:47:15 +02:00
|
|
|
if err != nil {
|
2018-11-04 22:25:32 +01:00
|
|
|
tlog.Warn.Printf("Rmdir: failed to delete blocking file %q: %v", dsStoreName, err)
|
2017-09-05 22:47:15 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2018-11-04 22:25:32 +01:00
|
|
|
tlog.Warn.Printf("Rmdir: had to delete blocking file %q", dsStoreName)
|
2017-09-05 22:47:15 +02:00
|
|
|
goto retry
|
|
|
|
}
|
2017-09-05 22:08:59 +02:00
|
|
|
// If the directory is not empty besides gocryptfs.diriv, do not even
|
|
|
|
// attempt the dance around gocryptfs.diriv.
|
|
|
|
if len(children) > 1 {
|
|
|
|
return fuse.ToStatus(syscall.ENOTEMPTY)
|
|
|
|
}
|
|
|
|
// Move "gocryptfs.diriv" to the parent dir as "gocryptfs.diriv.rmdir.XYZ"
|
|
|
|
tmpName := fmt.Sprintf("gocryptfs.diriv.rmdir.%d", cryptocore.RandUint64())
|
|
|
|
tlog.Debug.Printf("Rmdir: Renaming %s to %s", nametransform.DirIVFilename, tmpName)
|
|
|
|
// The directory is in an inconsistent state between rename and rmdir.
|
|
|
|
// Protect against concurrent readers.
|
|
|
|
fs.dirIVLock.Lock()
|
|
|
|
defer fs.dirIVLock.Unlock()
|
2018-09-22 20:10:34 +02:00
|
|
|
err = syscallcompat.Renameat(dirfd, nametransform.DirIVFilename,
|
|
|
|
parentDirFd, tmpName)
|
2017-09-05 22:08:59 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Rmdir: Renaming %s to %s failed: %v",
|
|
|
|
nametransform.DirIVFilename, tmpName, err)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Actual Rmdir
|
2018-09-22 20:10:34 +02:00
|
|
|
err = syscallcompat.Unlinkat(parentDirFd, cName, unix.AT_REMOVEDIR)
|
2017-09-05 22:08:59 +02:00
|
|
|
if err != nil {
|
|
|
|
// This can happen if another file in the directory was created in the
|
|
|
|
// meantime, undo the rename
|
2018-09-22 20:10:34 +02:00
|
|
|
err2 := syscallcompat.Renameat(parentDirFd, tmpName,
|
|
|
|
dirfd, nametransform.DirIVFilename)
|
2018-12-26 22:08:14 +01:00
|
|
|
if err2 != nil {
|
2017-09-05 22:08:59 +02:00
|
|
|
tlog.Warn.Printf("Rmdir: Rename rollback failed: %v", err2)
|
|
|
|
}
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Delete "gocryptfs.diriv.rmdir.XYZ"
|
2018-09-22 20:10:34 +02:00
|
|
|
err = syscallcompat.Unlinkat(parentDirFd, tmpName, 0)
|
2017-09-05 22:08:59 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Rmdir: Could not clean up %s: %v", tmpName, err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
// Delete .name file
|
|
|
|
if nametransform.IsLongContent(cName) {
|
2018-11-04 22:05:38 +01:00
|
|
|
nametransform.DeleteLongNameAt(parentDirFd, cName)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.OK
|
|
|
|
}
|
2016-02-07 11:29:54 +01:00
|
|
|
|
2018-11-04 21:46:51 +01:00
|
|
|
// OpenDir - FUSE call
|
|
|
|
//
|
|
|
|
// This function is symlink-safe through use of openBackingDir() and
|
|
|
|
// ReadDirIVAt().
|
2016-02-07 11:29:54 +01:00
|
|
|
func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("OpenDir(%s)", dirName)
|
2018-11-04 21:46:51 +01:00
|
|
|
parentDirFd, cDirName, err := fs.openBackingDir(dirName)
|
2016-02-07 11:29:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2018-11-04 21:46:51 +01:00
|
|
|
defer syscall.Close(parentDirFd)
|
2016-02-07 11:29:54 +01:00
|
|
|
// Read ciphertext directory
|
2017-08-15 18:35:30 +02:00
|
|
|
var cipherEntries []fuse.DirEntry
|
|
|
|
var status fuse.Status
|
2019-01-03 17:53:46 +01:00
|
|
|
fd, err := syscallcompat.Openat(parentDirFd, cDirName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
2017-12-03 17:57:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(fd)
|
|
|
|
cipherEntries, err = syscallcompat.Getdents(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
2016-02-07 11:29:54 +01:00
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
// Get DirIV (stays nil if PlaintextNames is used)
|
2016-02-07 11:29:54 +01:00
|
|
|
var cachedIV []byte
|
2016-06-23 21:29:00 +02:00
|
|
|
if !fs.args.PlaintextNames {
|
2018-11-04 21:46:51 +01:00
|
|
|
// Read the DirIV from disk
|
|
|
|
cachedIV, err = nametransform.ReadDirIVAt(fd)
|
|
|
|
if err != nil {
|
|
|
|
// The directory itself does not exist
|
|
|
|
if err == syscall.ENOENT {
|
|
|
|
return nil, fuse.ENOENT
|
2017-09-03 13:56:11 +02:00
|
|
|
}
|
2018-11-04 21:46:51 +01:00
|
|
|
// Any other problem warrants an error message
|
|
|
|
tlog.Warn.Printf("OpenDir %q: could not read gocryptfs.diriv: %v", cDirName, err)
|
|
|
|
return nil, fuse.EIO
|
2016-02-07 11:29:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
// Decrypted directory entries
|
2016-02-07 11:29:54 +01:00
|
|
|
var plain []fuse.DirEntry
|
2016-06-04 16:39:27 +02:00
|
|
|
var errorCount int
|
|
|
|
// Filter and decrypt filenames
|
2016-02-07 11:29:54 +01:00
|
|
|
for i := range cipherEntries {
|
|
|
|
cName := cipherEntries[i].Name
|
|
|
|
if dirName == "" && cName == configfile.ConfDefaultName {
|
|
|
|
// silently ignore "gocryptfs.conf" in the top level dir
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fs.args.PlaintextNames {
|
|
|
|
plain = append(plain, cipherEntries[i])
|
|
|
|
continue
|
|
|
|
}
|
2017-05-23 20:46:24 +02:00
|
|
|
if cName == nametransform.DirIVFilename {
|
|
|
|
// silently ignore "gocryptfs.diriv" everywhere if dirIV is enabled
|
|
|
|
continue
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
// Handle long file name
|
|
|
|
isLong := nametransform.LongNameNone
|
2016-02-07 11:29:54 +01:00
|
|
|
if fs.args.LongNames {
|
2016-06-04 16:39:27 +02:00
|
|
|
isLong = nametransform.NameType(cName)
|
|
|
|
}
|
|
|
|
if isLong == nametransform.LongNameContent {
|
2018-10-01 21:28:54 +02:00
|
|
|
cNameLong, err := nametransform.ReadLongNameAt(fd, cName)
|
2016-06-04 16:39:27 +02:00
|
|
|
if err != nil {
|
2017-05-07 20:58:27 +02:00
|
|
|
tlog.Warn.Printf("OpenDir %q: invalid entry %q: Could not read .name: %v",
|
|
|
|
cDirName, cName, err)
|
2018-07-01 15:48:53 +02:00
|
|
|
fs.reportMitigatedCorruption(cName)
|
2016-06-04 16:39:27 +02:00
|
|
|
errorCount++
|
2016-02-07 11:29:54 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
cName = cNameLong
|
|
|
|
} else if isLong == nametransform.LongNameFilename {
|
|
|
|
// ignore "gocryptfs.longname.*.name"
|
|
|
|
continue
|
2016-02-07 11:29:54 +01:00
|
|
|
}
|
|
|
|
name, err := fs.nameTransform.DecryptName(cName, cachedIV)
|
|
|
|
if err != nil {
|
2017-05-07 20:58:27 +02:00
|
|
|
tlog.Warn.Printf("OpenDir %q: invalid entry %q: %v",
|
|
|
|
cDirName, cName, err)
|
2018-07-01 15:48:53 +02:00
|
|
|
fs.reportMitigatedCorruption(cName)
|
2017-09-05 22:47:15 +02:00
|
|
|
if runtime.GOOS == "darwin" && cName == dsStoreName {
|
2017-09-05 21:47:05 +02:00
|
|
|
// MacOS creates lots of these files. Log the warning but don't
|
|
|
|
// increment errorCount - does not warrant returning EIO.
|
|
|
|
continue
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
errorCount++
|
2016-02-07 11:29:54 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-05-07 20:58:27 +02:00
|
|
|
// Override the ciphertext name with the plaintext name but reuse the rest
|
|
|
|
// of the structure
|
2016-02-07 11:29:54 +01:00
|
|
|
cipherEntries[i].Name = name
|
|
|
|
plain = append(plain, cipherEntries[i])
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
|
|
|
|
if errorCount > 0 && len(plain) == 0 {
|
|
|
|
// Don't let the user stare on an empty directory. Report that things went
|
|
|
|
// wrong.
|
2017-05-07 20:58:27 +02:00
|
|
|
tlog.Warn.Printf("OpenDir %q: all %d entries were invalid, returning EIO",
|
|
|
|
cDirName, errorCount)
|
2016-06-04 16:39:27 +02:00
|
|
|
status = fuse.EIO
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:29:54 +01:00
|
|
|
return plain, status
|
|
|
|
}
|