2020-06-21 13:25:12 +02:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
// Helper functions for sparse files (files with holes)
|
|
|
|
|
|
|
|
import (
|
2020-07-23 22:55:07 +02:00
|
|
|
"context"
|
2020-06-21 13:25:12 +02:00
|
|
|
"syscall"
|
|
|
|
|
2020-07-12 12:59:01 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
2020-06-21 13:25:12 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Will a write to plaintext offset "targetOff" create a file hole in the
|
|
|
|
// ciphertext? If yes, zero-pad the last ciphertext block.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) writePadHole(targetOff int64) syscall.Errno {
|
2020-06-21 13:25:12 +02:00
|
|
|
// Get the current file size.
|
|
|
|
fi, err := f.fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("checkAndPadHole: Fstat failed: %v", err)
|
2020-07-12 12:59:01 +02:00
|
|
|
return fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
plainSize := f.contentEnc.CipherSizeToPlainSize(uint64(fi.Size()))
|
|
|
|
// Appending a single byte to the file (equivalent to writing to
|
|
|
|
// offset=plainSize) would write to "nextBlock".
|
|
|
|
nextBlock := f.contentEnc.PlainOffToBlockNo(plainSize)
|
|
|
|
// targetBlock is the block the user wants to write to.
|
|
|
|
targetBlock := f.contentEnc.PlainOffToBlockNo(uint64(targetOff))
|
|
|
|
// The write goes into an existing block or (if the last block was full)
|
|
|
|
// starts a new one directly after the last block. Nothing to do.
|
|
|
|
if targetBlock <= nextBlock {
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
// The write goes past the next block. nextBlock has
|
|
|
|
// to be zero-padded to the block boundary and (at least) nextBlock+1
|
|
|
|
// will contain a file hole in the ciphertext.
|
2020-07-12 12:59:01 +02:00
|
|
|
errno := f.zeroPad(plainSize)
|
|
|
|
if errno != 0 {
|
|
|
|
return errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Zero-pad the file of size plainSize to the next block boundary. This is a no-op
|
|
|
|
// if the file is already block-aligned.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) zeroPad(plainSize uint64) syscall.Errno {
|
2020-06-21 13:25:12 +02:00
|
|
|
lastBlockLen := plainSize % f.contentEnc.PlainBS()
|
|
|
|
if lastBlockLen == 0 {
|
|
|
|
// Already block-aligned
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
missing := f.contentEnc.PlainBS() - lastBlockLen
|
|
|
|
pad := make([]byte, missing)
|
|
|
|
tlog.Debug.Printf("zeroPad: Writing %d bytes\n", missing)
|
2020-07-12 12:59:01 +02:00
|
|
|
_, errno := f.doWrite(pad, int64(plainSize))
|
|
|
|
return errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 22:55:07 +02:00
|
|
|
// Lseek - FUSE call.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Lseek(ctx context.Context, off uint64, whence uint32) (uint64, syscall.Errno) {
|
2020-07-23 22:55:07 +02:00
|
|
|
cipherOff := f.rootNode.contentEnc.PlainSizeToCipherSize(off)
|
|
|
|
newCipherOff, err := syscall.Seek(f.intFd(), int64(cipherOff), int(whence))
|
2020-06-21 13:25:12 +02:00
|
|
|
if err != nil {
|
2020-08-16 19:58:47 +02:00
|
|
|
return 0, fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
2020-07-23 22:55:07 +02:00
|
|
|
newOff := f.contentEnc.CipherSizeToPlainSize(uint64(newCipherOff))
|
|
|
|
return newOff, 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|