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 file handles
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-10-04 14:36:20 +02:00
|
|
|
"io"
|
2015-09-08 00:54:24 +02:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/toggledlog"
|
2015-09-08 00:54:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// File - based on loopbackFile in go-fuse/fuse/nodefs/files.go
|
|
|
|
type file struct {
|
|
|
|
fd *os.File
|
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
// fdLock prevents the fd to be closed while we are in the middle of
|
|
|
|
// an operation.
|
|
|
|
// Every FUSE entrypoint should RLock(). The only user of Lock() is
|
|
|
|
// Release(), which closes the fd, hence has to acquire an exclusive lock.
|
|
|
|
fdLock sync.RWMutex
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
// Was the file opened O_WRONLY?
|
|
|
|
writeOnly bool
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
// Content encryption helper
|
|
|
|
contentEnc *contentenc.ContentEnc
|
2015-10-03 13:36:49 +02:00
|
|
|
|
|
|
|
// Inode number
|
|
|
|
ino uint64
|
2015-11-01 01:32:33 +01:00
|
|
|
|
|
|
|
// File header
|
2016-02-06 19:20:54 +01:00
|
|
|
header *contentenc.FileHeader
|
2016-01-25 00:51:28 +01:00
|
|
|
|
|
|
|
forgotten bool
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
func NewFile(fd *os.File, writeOnly bool, contentEnc *contentenc.ContentEnc) nodefs.File {
|
2015-10-03 13:36:49 +02:00
|
|
|
var st syscall.Stat_t
|
|
|
|
syscall.Fstat(int(fd.Fd()), &st)
|
2016-01-24 13:08:08 +01:00
|
|
|
wlock.register(st.Ino)
|
2015-10-03 13:36:49 +02:00
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
return &file{
|
2016-02-06 20:23:36 +01:00
|
|
|
fd: fd,
|
|
|
|
writeOnly: writeOnly,
|
2016-02-06 19:20:54 +01:00
|
|
|
contentEnc: contentEnc,
|
2016-02-06 20:23:36 +01:00
|
|
|
ino: st.Ino,
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
// intFd - return the backing file descriptor as an integer. Used for debug
|
|
|
|
// messages.
|
|
|
|
func (f *file) intFd() int {
|
|
|
|
return int(f.fd.Fd())
|
|
|
|
}
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
func (f *file) InnerFile() nodefs.File {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) SetInode(n *nodefs.Inode) {
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
// readHeader - load the file header from disk
|
|
|
|
//
|
|
|
|
// Returns io.EOF if the file is empty
|
|
|
|
func (f *file) readHeader() error {
|
2016-02-06 19:20:54 +01:00
|
|
|
buf := make([]byte, contentenc.HEADER_LEN)
|
2015-11-01 01:32:33 +01:00
|
|
|
_, err := f.fd.ReadAt(buf, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
h, err := contentenc.ParseHeader(buf)
|
2015-11-01 01:32:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.header = h
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createHeader - create a new random header and write it to disk
|
|
|
|
func (f *file) createHeader() error {
|
2016-02-06 19:20:54 +01:00
|
|
|
h := contentenc.RandomHeader()
|
2015-11-01 01:32:33 +01:00
|
|
|
buf := h.Pack()
|
2015-12-06 15:05:52 +01:00
|
|
|
|
|
|
|
// Prevent partially written (=corrupt) header by preallocating the space beforehand
|
2016-02-06 19:20:54 +01:00
|
|
|
err := prealloc(int(f.fd.Fd()), 0, contentenc.HEADER_LEN)
|
2015-12-06 15:05:52 +01:00
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d: createHeader: prealloc failed: %s\n", f.ino, err.Error())
|
2015-12-08 13:51:06 +01:00
|
|
|
return err
|
2015-12-06 15:05:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actually write header
|
|
|
|
_, err = f.fd.WriteAt(buf, 0)
|
2015-11-01 01:32:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.header = h
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
func (f *file) String() string {
|
|
|
|
return fmt.Sprintf("cryptFile(%s)", f.fd.Name())
|
|
|
|
}
|
|
|
|
|
2015-10-03 13:36:49 +02:00
|
|
|
// doRead - returns "length" plaintext bytes from plaintext offset "off".
|
2015-11-01 01:32:33 +01:00
|
|
|
// Arguments "length" and "off" do not have to be block-aligned.
|
2015-09-30 22:36:53 +02:00
|
|
|
//
|
2015-11-01 01:32:33 +01:00
|
|
|
// doRead reads the corresponding ciphertext blocks from disk, decrypts them and
|
2015-10-03 13:36:49 +02:00
|
|
|
// returns the requested part of the plaintext.
|
|
|
|
//
|
2015-11-01 01:32:33 +01:00
|
|
|
// Called by Read() for normal reading,
|
|
|
|
// by Write() and Truncate() for Read-Modify-Write
|
2015-09-08 21:35:06 +02:00
|
|
|
func (f *file) doRead(off uint64, length uint64) ([]byte, fuse.Status) {
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
// Read file header
|
|
|
|
if f.header == nil {
|
|
|
|
err := f.readHeader()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil, fuse.OK
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
// Read the backing ciphertext in one go
|
2016-02-06 19:20:54 +01:00
|
|
|
blocks := f.contentEnc.ExplodePlainRange(off, length)
|
2015-11-01 12:11:36 +01:00
|
|
|
alignedOffset, alignedLength := blocks[0].JointCiphertextRange(blocks)
|
|
|
|
skip := blocks[0].Skip
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Debug.Printf("JointCiphertextRange(%d, %d) -> %d, %d, %d", off, length, alignedOffset, alignedLength, skip)
|
2015-09-08 00:54:24 +02:00
|
|
|
ciphertext := make([]byte, int(alignedLength))
|
2015-09-08 21:35:06 +02:00
|
|
|
n, err := f.fd.ReadAt(ciphertext, int64(alignedOffset))
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil && err != io.EOF {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("read: ReadAt: %s", err.Error())
|
2015-09-08 00:54:24 +02:00
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2015-10-03 13:36:49 +02:00
|
|
|
// Truncate ciphertext buffer down to actually read bytes
|
|
|
|
ciphertext = ciphertext[0:n]
|
2015-10-06 22:27:37 +02:00
|
|
|
|
2015-11-01 12:11:36 +01:00
|
|
|
firstBlockNo := blocks[0].BlockNo
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Debug.Printf("ReadAt offset=%d bytes (%d blocks), want=%d, got=%d", alignedOffset, firstBlockNo, alignedLength, n)
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
// Decrypt it
|
2016-02-06 19:20:54 +01:00
|
|
|
plaintext, err := f.contentEnc.DecryptBlocks(ciphertext, firstBlockNo, f.header.Id)
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
curruptBlockNo := firstBlockNo + f.contentEnc.PlainOffToBlockNo(uint64(len(plaintext)))
|
|
|
|
cipherOff := f.contentEnc.BlockNoToCipherOff(curruptBlockNo)
|
|
|
|
plainOff := f.contentEnc.BlockNoToPlainOff(curruptBlockNo)
|
|
|
|
toggledlog.Warn.Printf("ino%d: doRead: corrupt block #%d (plainOff=%d, cipherOff=%d)",
|
2015-11-01 12:11:36 +01:00
|
|
|
f.ino, curruptBlockNo, plainOff, cipherOff)
|
2015-09-08 00:54:24 +02:00
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Crop down to the relevant part
|
|
|
|
var out []byte
|
|
|
|
lenHave := len(plaintext)
|
2015-11-01 12:11:36 +01:00
|
|
|
lenWant := int(skip + length)
|
2015-09-08 00:54:24 +02:00
|
|
|
if lenHave > lenWant {
|
2015-11-01 12:11:36 +01:00
|
|
|
out = plaintext[skip:lenWant]
|
|
|
|
} else if lenHave > int(skip) {
|
2015-09-08 00:54:24 +02:00
|
|
|
out = plaintext[skip:lenHave]
|
2015-09-08 21:35:06 +02:00
|
|
|
}
|
2015-11-01 01:32:33 +01:00
|
|
|
// else: out stays empty, file was smaller than the requested offset
|
2015-09-08 21:35:06 +02:00
|
|
|
|
|
|
|
return out, fuse.OK
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read - FUSE call
|
|
|
|
func (f *file) Read(buf []byte, off int64) (resultData fuse.ReadResult, code fuse.Status) {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2015-11-01 01:32:33 +01:00
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Debug.Printf("ino%d: FUSE Read: offset=%d length=%d", f.ino, len(buf), off)
|
2015-09-08 21:35:06 +02:00
|
|
|
|
|
|
|
if f.writeOnly {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d: Tried to read from write-only file", f.ino)
|
2015-09-08 21:35:06 +02:00
|
|
|
return nil, fuse.EBADF
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 21:35:06 +02:00
|
|
|
out, status := f.doRead(uint64(off), uint64(len(buf)))
|
2015-09-30 20:32:24 +02:00
|
|
|
|
|
|
|
if status == fuse.EIO {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d: Read failed with EIO, offset=%d, length=%d", f.ino, len(buf), off)
|
2015-09-30 20:32:24 +02:00
|
|
|
}
|
2015-09-08 21:35:06 +02:00
|
|
|
if status != fuse.OK {
|
|
|
|
return nil, status
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Debug.Printf("ino%d: Read: status %v, returning %d bytes", f.ino, status, len(out))
|
2015-09-08 21:35:06 +02:00
|
|
|
return fuse.ReadResultData(out), status
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 22:36:29 +01:00
|
|
|
const FALLOC_FL_KEEP_SIZE = 0x01
|
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
// doWrite - encrypt "data" and write it to plaintext offset "off"
|
|
|
|
//
|
|
|
|
// Arguments do not have to be block-aligned, read-modify-write is
|
|
|
|
// performed internally as neccessary
|
|
|
|
//
|
|
|
|
// Called by Write() for normal writing,
|
|
|
|
// and by Truncate() to rewrite the last file block.
|
2015-10-04 11:39:35 +02:00
|
|
|
func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
|
2015-11-01 01:32:33 +01:00
|
|
|
|
|
|
|
// Read header from disk, create a new one if the file is empty
|
|
|
|
if f.header == nil {
|
|
|
|
err := f.readHeader()
|
|
|
|
if err == io.EOF {
|
|
|
|
err = f.createHeader()
|
|
|
|
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
var written uint32
|
2015-09-30 22:36:53 +02:00
|
|
|
status := fuse.OK
|
2015-09-08 00:54:24 +02:00
|
|
|
dataBuf := bytes.NewBuffer(data)
|
2016-02-06 19:20:54 +01:00
|
|
|
blocks := f.contentEnc.ExplodePlainRange(uint64(off), uint64(len(data)))
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, b := range blocks {
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
blockData := dataBuf.Next(int(b.Length))
|
|
|
|
|
|
|
|
// Incomplete block -> Read-Modify-Write
|
|
|
|
if b.IsPartial() {
|
|
|
|
// Read
|
|
|
|
o, _ := b.PlaintextRange()
|
2016-02-06 19:20:54 +01:00
|
|
|
oldData, status := f.doRead(o, f.contentEnc.PlainBS())
|
2015-09-08 00:54:24 +02:00
|
|
|
if status != fuse.OK {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d fh%d: RMW read failed: %s", f.ino, f.intFd(), status.String())
|
2015-09-08 00:54:24 +02:00
|
|
|
return written, status
|
|
|
|
}
|
|
|
|
// Modify
|
2016-02-06 19:20:54 +01:00
|
|
|
blockData = f.contentEnc.MergeBlocks(oldData, blockData, int(b.Skip))
|
|
|
|
toggledlog.Debug.Printf("len(oldData)=%d len(blockData)=%d", len(oldData), len(blockData))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
2015-09-09 19:32:59 +02:00
|
|
|
|
2016-01-24 13:08:08 +01:00
|
|
|
// Encrypt
|
2015-11-08 22:36:29 +01:00
|
|
|
blockOffset, blockLen := b.CiphertextRange()
|
2016-02-06 19:20:54 +01:00
|
|
|
blockData = f.contentEnc.EncryptBlock(blockData, b.BlockNo, f.header.Id)
|
|
|
|
toggledlog.Debug.Printf("ino%d: Writing %d bytes to block #%d",
|
|
|
|
f.ino, uint64(len(blockData))-f.contentEnc.BlockOverhead(), b.BlockNo)
|
2015-11-08 22:36:29 +01:00
|
|
|
|
|
|
|
// Prevent partially written (=corrupt) blocks by preallocating the space beforehand
|
2016-01-07 21:39:41 +01:00
|
|
|
err := prealloc(int(f.fd.Fd()), int64(blockOffset), int64(blockLen))
|
2015-11-08 22:36:29 +01:00
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d fh%d: doWrite: prealloc failed: %s", f.ino, f.intFd(), err.Error())
|
2015-11-08 22:36:29 +01:00
|
|
|
status = fuse.ToStatus(err)
|
|
|
|
break
|
|
|
|
}
|
2015-12-13 20:10:52 +01:00
|
|
|
|
|
|
|
// Write
|
2015-11-08 22:36:29 +01:00
|
|
|
_, err = f.fd.WriteAt(blockData, int64(blockOffset))
|
2016-01-24 13:08:08 +01:00
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("doWrite: Write failed: %s", err.Error())
|
2015-09-08 00:54:24 +02:00
|
|
|
status = fuse.ToStatus(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
written += uint32(b.Length)
|
|
|
|
}
|
|
|
|
return written, status
|
|
|
|
}
|
|
|
|
|
2015-10-04 11:39:35 +02:00
|
|
|
// Write - FUSE call
|
|
|
|
func (f *file) Write(data []byte, off int64) (uint32, fuse.Status) {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2016-01-24 13:08:08 +01:00
|
|
|
wlock.lock(f.ino)
|
|
|
|
defer wlock.unlock(f.ino)
|
2015-10-04 14:21:07 +02:00
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Debug.Printf("ino%d: FUSE Write: offset=%d length=%d", f.ino, off, len(data))
|
2016-01-25 00:51:28 +01:00
|
|
|
|
2015-10-04 14:21:07 +02:00
|
|
|
fi, err := f.fd.Stat()
|
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("Write: Fstat failed: %v", err)
|
2015-10-04 14:21:07 +02:00
|
|
|
return 0, fuse.ToStatus(err)
|
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
plainSize := f.contentEnc.CipherSizeToPlainSize(uint64(fi.Size()))
|
2015-10-04 14:21:07 +02:00
|
|
|
if f.createsHole(plainSize, off) {
|
|
|
|
status := f.zeroPad(plainSize)
|
|
|
|
if status != fuse.OK {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("zeroPad returned error %v", status)
|
2015-10-04 14:21:07 +02:00
|
|
|
return 0, status
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 11:39:35 +02:00
|
|
|
return f.doWrite(data, off)
|
|
|
|
}
|
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
// Release - FUSE call, close file
|
2015-09-08 00:54:24 +02:00
|
|
|
func (f *file) Release() {
|
2015-11-01 01:32:33 +01:00
|
|
|
f.fdLock.Lock()
|
2016-01-25 00:51:28 +01:00
|
|
|
defer f.fdLock.Unlock()
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
f.fd.Close()
|
2016-01-25 00:51:28 +01:00
|
|
|
f.forgotten = true
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush - FUSE call
|
|
|
|
func (f *file) Flush() fuse.Status {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
// Since Flush() may be called for each dup'd fd, we don't
|
|
|
|
// want to really close the file, we just want to flush. This
|
|
|
|
// is achieved by closing a dup'd fd.
|
|
|
|
newFd, err := syscall.Dup(int(f.fd.Fd()))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
err = syscall.Close(newFd)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Fsync(flags int) (code fuse.Status) {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
return fuse.ToStatus(syscall.Fsync(int(f.fd.Fd())))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2016-01-24 13:08:08 +01:00
|
|
|
// Truncate - FUSE call
|
2015-09-30 22:36:53 +02:00
|
|
|
func (f *file) Truncate(newSize uint64) fuse.Status {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2016-01-24 13:08:08 +01:00
|
|
|
wlock.lock(f.ino)
|
|
|
|
defer wlock.unlock(f.ino)
|
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
if f.forgotten {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d fh%d: Truncate on forgotten file", f.ino, f.intFd())
|
2016-01-25 00:51:28 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
// Common case first: Truncate to zero
|
|
|
|
if newSize == 0 {
|
|
|
|
err := syscall.Ftruncate(int(f.fd.Fd()), 0)
|
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d fh%d: Ftruncate(fd, 0) returned error: %v", f.ino, f.intFd(), err)
|
2015-11-01 01:32:33 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-01-24 13:08:08 +01:00
|
|
|
// Truncate to zero kills the file header
|
2015-11-01 01:32:33 +01:00
|
|
|
f.header = nil
|
|
|
|
return fuse.OK
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2015-09-30 22:36:53 +02:00
|
|
|
// We need the old file size to determine if we are growing or shrinking
|
|
|
|
// the file
|
|
|
|
fi, err := f.fd.Stat()
|
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("ino%d fh%d: Truncate: Fstat failed: %v", f.ino, f.intFd(), err)
|
2015-09-30 22:36:53 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
oldSize := f.contentEnc.CipherSizeToPlainSize(uint64(fi.Size()))
|
2015-10-04 00:26:20 +02:00
|
|
|
{
|
2016-02-06 19:20:54 +01:00
|
|
|
oldB := float32(oldSize) / float32(f.contentEnc.PlainBS())
|
|
|
|
newB := float32(newSize) / float32(f.contentEnc.PlainBS())
|
|
|
|
toggledlog.Debug.Printf("ino%d: FUSE Truncate from %.2f to %.2f blocks (%d to %d bytes)", f.ino, oldB, newB, oldSize, newSize)
|
2015-10-04 00:26:20 +02:00
|
|
|
}
|
2015-10-04 14:21:07 +02:00
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
// File size stays the same - nothing to do
|
|
|
|
if newSize == oldSize {
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:21:07 +02:00
|
|
|
// File grows
|
2015-09-30 22:36:53 +02:00
|
|
|
if newSize > oldSize {
|
2015-11-01 01:32:33 +01:00
|
|
|
|
|
|
|
// File was empty, create new header
|
|
|
|
if oldSize == 0 {
|
|
|
|
err := f.createHeader()
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
blocks := f.contentEnc.ExplodePlainRange(oldSize, newSize-oldSize)
|
2015-10-04 14:36:20 +02:00
|
|
|
for _, b := range blocks {
|
2015-10-04 14:21:07 +02:00
|
|
|
// First and last block may be partial
|
|
|
|
if b.IsPartial() {
|
|
|
|
off, _ := b.PlaintextRange()
|
2015-10-04 14:24:43 +02:00
|
|
|
off += b.Skip
|
2015-10-04 14:21:07 +02:00
|
|
|
_, status := f.doWrite(make([]byte, b.Length), int64(off))
|
|
|
|
if status != fuse.OK {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
off, length := b.CiphertextRange()
|
2015-10-04 14:36:20 +02:00
|
|
|
err := syscall.Ftruncate(int(f.fd.Fd()), int64(off+length))
|
2015-10-04 14:21:07 +02:00
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("grow Ftruncate returned error: %v", err)
|
2015-10-04 14:21:07 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
2015-10-04 14:21:07 +02:00
|
|
|
return fuse.OK
|
|
|
|
} else {
|
2015-10-04 16:04:25 +02:00
|
|
|
// File shrinks
|
2016-02-06 19:20:54 +01:00
|
|
|
blockNo := f.contentEnc.PlainOffToBlockNo(newSize)
|
|
|
|
cipherOff := f.contentEnc.BlockNoToCipherOff(blockNo)
|
|
|
|
plainOff := f.contentEnc.BlockNoToPlainOff(blockNo)
|
2015-10-04 16:04:25 +02:00
|
|
|
lastBlockLen := newSize - plainOff
|
2015-10-04 14:21:07 +02:00
|
|
|
var data []byte
|
|
|
|
if lastBlockLen > 0 {
|
|
|
|
var status fuse.Status
|
2015-10-04 16:04:25 +02:00
|
|
|
data, status = f.doRead(plainOff, lastBlockLen)
|
2015-09-30 22:36:53 +02:00
|
|
|
if status != fuse.OK {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("shrink doRead returned error: %v", err)
|
2015-09-30 22:36:53 +02:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
}
|
2015-11-01 01:32:33 +01:00
|
|
|
// Truncate down to last complete block
|
2015-10-04 16:04:25 +02:00
|
|
|
err = syscall.Ftruncate(int(f.fd.Fd()), int64(cipherOff))
|
2015-09-30 22:36:53 +02:00
|
|
|
if err != nil {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("shrink Ftruncate returned error: %v", err)
|
2015-09-30 22:36:53 +02:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2015-11-01 01:32:33 +01:00
|
|
|
// Append partial block
|
2015-10-04 14:21:07 +02:00
|
|
|
if lastBlockLen > 0 {
|
2015-10-04 16:04:25 +02:00
|
|
|
_, status := f.doWrite(data, int64(plainOff))
|
2015-10-04 14:21:07 +02:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Chmod(mode uint32) fuse.Status {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
return fuse.ToStatus(f.fd.Chmod(os.FileMode(mode)))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Chown(uid uint32, gid uint32) fuse.Status {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2016-01-25 00:51:28 +01:00
|
|
|
return fuse.ToStatus(f.fd.Chown(int(uid), int(gid)))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) GetAttr(a *fuse.Attr) fuse.Status {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Debug.Printf("file.GetAttr()")
|
2015-09-08 00:54:24 +02:00
|
|
|
st := syscall.Stat_t{}
|
|
|
|
err := syscall.Fstat(int(f.fd.Fd()), &st)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
a.FromStat(&st)
|
2016-02-06 19:20:54 +01:00
|
|
|
a.Size = f.contentEnc.CipherSizeToPlainSize(a.Size)
|
2015-09-08 00:54:24 +02:00
|
|
|
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:32:33 +01:00
|
|
|
// Allocate - FUSE call, fallocate(2)
|
2016-01-24 19:43:21 +01:00
|
|
|
var allocateWarned bool
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
func (f *file) Allocate(off uint64, sz uint64, mode uint32) fuse.Status {
|
2016-01-24 19:43:21 +01:00
|
|
|
// Only warn once
|
|
|
|
if !allocateWarned {
|
2016-02-06 19:20:54 +01:00
|
|
|
toggledlog.Warn.Printf("fallocate(2) is not supported, returning ENOSYS - see https://github.com/rfjakob/gocryptfs/issues/1")
|
2016-01-24 19:43:21 +01:00
|
|
|
allocateWarned = true
|
|
|
|
}
|
2015-10-04 17:14:40 +02:00
|
|
|
return fuse.ENOSYS
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const _UTIME_OMIT = ((1 << 30) - 2)
|
|
|
|
|
|
|
|
func (f *file) Utimens(a *time.Time, m *time.Time) fuse.Status {
|
2016-01-25 00:51:28 +01:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
|
2015-10-04 20:32:15 +02:00
|
|
|
ts := make([]syscall.Timespec, 2)
|
|
|
|
|
2015-09-08 00:54:24 +02:00
|
|
|
if a == nil {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[0].Nsec = _UTIME_OMIT
|
2015-09-08 00:54:24 +02:00
|
|
|
} else {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[0].Sec = a.Unix()
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m == nil {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[1].Nsec = _UTIME_OMIT
|
2015-09-08 00:54:24 +02:00
|
|
|
} else {
|
2015-10-04 20:32:15 +02:00
|
|
|
ts[1].Sec = m.Unix()
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
2015-10-04 20:32:15 +02:00
|
|
|
fn := fmt.Sprintf("/proc/self/fd/%d", f.fd.Fd())
|
2016-01-25 00:51:28 +01:00
|
|
|
return fuse.ToStatus(syscall.UtimesNano(fn, ts))
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|