From 9b725c15cf50cfb85ec6ec88c47843092775dedc Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 3 Jul 2016 19:14:12 +0200 Subject: syscallcompat: OSX: add Fallocate and Openat wrappers ...and convert all calls to syscall.{Fallocate,Openat} to syscallcompat . Both syscalls are not available on OSX. We emulate Openat and just return EOPNOTSUPP for Fallocate. --- internal/syscallcompat/sys_darwin.go | 33 +++++++++++++++++++++++++++++++++ internal/syscallcompat/sys_linux.go | 19 ++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) (limited to 'internal/syscallcompat') diff --git a/internal/syscallcompat/sys_darwin.go b/internal/syscallcompat/sys_darwin.go index d437170..81e1b15 100644 --- a/internal/syscallcompat/sys_darwin.go +++ b/internal/syscallcompat/sys_darwin.go @@ -1,5 +1,11 @@ package syscallcompat +import ( + "os" + "sync" + "syscall" +) + // prealloc - preallocate space without changing the file size. This prevents // us from running out of space in the middle of an operation. func Prealloc(fd int, off int64, len int64) (err error) { @@ -10,3 +16,30 @@ func Prealloc(fd int, off int64, len int64) (err error) { // See https://github.com/rfjakob/gocryptfs/issues/18 if you want to help. return nil } + +var openatLock sync.Mutex + +// Poor man's Openat: +// 1) fchdir to the dirfd +// 2) open the file +// 3) chdir back. +func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { + openatLock.Lock() + defer openatLock.Unlock() + + oldWd, err := os.Getwd() + if err != nil { + return -1, err + } + err = syscall.Fchdir(dirfd) + if err != nil { + return -1, err + } + defer os.Chdir(oldWd) + + return syscall.Open(path, flags, mode) +} + +func Fallocate(fd int, mode uint32, off int64, len int64) (err error) { + return syscall.EOPNOTSUPP +} diff --git a/internal/syscallcompat/sys_linux.go b/internal/syscallcompat/sys_linux.go index 9fe9da5..ca6df95 100644 --- a/internal/syscallcompat/sys_linux.go +++ b/internal/syscallcompat/sys_linux.go @@ -11,9 +11,10 @@ const FALLOC_FL_KEEP_SIZE = 0x01 var preallocWarn sync.Once -// prealloc - preallocate space without changing the file size. This prevents -// us from running out of space in the middle of an operation. -func Prealloc(fd int, off int64, len int64) (err error) { +// EnospcPrealloc preallocates ciphertext space without changing the file +// size. This guarantees that we don't run out of space while writing a +// ciphertext block (that would corrupt the block). +func EnospcPrealloc(fd int, off int64, len int64) (err error) { for { err = syscall.Fallocate(fd, FALLOC_FL_KEEP_SIZE, off, len) if err == syscall.EINTR { @@ -23,8 +24,8 @@ func Prealloc(fd int, off int64, len int64) (err error) { continue } if err == syscall.EOPNOTSUPP { - // ZFS does not support fallocate which caused gocryptfs to abort - // every write operation: https://github.com/rfjakob/gocryptfs/issues/22 + // ZFS and ext3 do not support fallocate. Warn but continue anyway. + // https://github.com/rfjakob/gocryptfs/issues/22 preallocWarn.Do(func() { tlog.Warn.Printf("Warning: The underlying filesystem " + "does not support fallocate(2). gocryptfs will continue working " + @@ -35,3 +36,11 @@ func Prealloc(fd int, off int64, len int64) (err error) { return err } } + +func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { + return syscall.Openat(dirfd, path, flags, mode) +} + +func Fallocate(fd int, mode uint32, off int64, len int64) (err error) { + return syscall.Fallocate(fd, mode, off, len) +} -- cgit v1.2.3