2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2015-10-04 11:39:35 +02:00
|
|
|
|
2015-12-19 13:21:15 +01:00
|
|
|
// Helper functions for sparse files (files with holes)
|
|
|
|
|
2015-10-04 11:39:35 +02:00
|
|
|
import (
|
2018-07-01 19:13:28 +02:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
|
2020-05-17 14:18:23 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-10-04 11:39:35 +02:00
|
|
|
)
|
|
|
|
|
2016-10-25 22:37:45 +02:00
|
|
|
// Will a write to plaintext offset "targetOff" create a file hole in the
|
|
|
|
// ciphertext? If yes, zero-pad the last ciphertext block.
|
2018-07-01 19:10:57 +02:00
|
|
|
func (f *File) writePadHole(targetOff int64) fuse.Status {
|
2016-10-25 22:37:45 +02:00
|
|
|
// Get the current file size.
|
|
|
|
fi, err := f.fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("checkAndPadHole: Fstat failed: %v", err)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
plainSize := f.contentEnc.CipherSizeToPlainSize(uint64(fi.Size()))
|
2016-10-25 21:19:37 +02:00
|
|
|
// Appending a single byte to the file (equivalent to writing to
|
|
|
|
// offset=plainSize) would write to "nextBlock".
|
2016-02-06 19:20:54 +01:00
|
|
|
nextBlock := f.contentEnc.PlainOffToBlockNo(plainSize)
|
2016-10-25 21:19:37 +02:00
|
|
|
// targetBlock is the block the user wants to write to.
|
2016-10-25 22:37:45 +02:00
|
|
|
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 {
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
// The write goes past the next block. nextBlock has
|
|
|
|
// to be zero-padded to the block boundary and (at least) nextBlock+1
|
2017-03-12 21:06:59 +01:00
|
|
|
// will contain a file hole in the ciphertext.
|
2016-10-25 22:37:45 +02:00
|
|
|
status := f.zeroPad(plainSize)
|
|
|
|
if status != fuse.OK {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
return fuse.OK
|
2015-10-04 11:39:35 +02:00
|
|
|
}
|
|
|
|
|
2017-03-12 21:06:59 +01: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.
|
2018-07-01 19:10:57 +02:00
|
|
|
func (f *File) zeroPad(plainSize uint64) fuse.Status {
|
2016-02-06 19:20:54 +01:00
|
|
|
lastBlockLen := plainSize % f.contentEnc.PlainBS()
|
2017-03-12 21:06:59 +01:00
|
|
|
if lastBlockLen == 0 {
|
2016-07-01 23:29:31 +02:00
|
|
|
// Already block-aligned
|
|
|
|
return fuse.OK
|
|
|
|
}
|
2017-03-12 21:06:59 +01:00
|
|
|
missing := f.contentEnc.PlainBS() - lastBlockLen
|
2015-10-04 11:39:35 +02:00
|
|
|
pad := make([]byte, missing)
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("zeroPad: Writing %d bytes\n", missing)
|
2015-10-04 11:39:35 +02:00
|
|
|
_, status := f.doWrite(pad, int64(plainSize))
|
|
|
|
return status
|
|
|
|
}
|
2018-07-01 19:13:28 +02:00
|
|
|
|
|
|
|
// SeekData calls the lseek syscall with SEEK_DATA. It returns the offset of the
|
|
|
|
// next data bytes, skipping over file holes.
|
|
|
|
func (f *File) SeekData(oldOffset int64) (int64, error) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
// Does MacOS support something like this?
|
|
|
|
return 0, syscall.EOPNOTSUPP
|
|
|
|
}
|
|
|
|
const SEEK_DATA = 3
|
2019-01-06 04:46:16 +01:00
|
|
|
|
|
|
|
// Convert plaintext offset to ciphertext offset and round down to the
|
|
|
|
// start of the current block. File holes smaller than a full block will
|
|
|
|
// be ignored.
|
|
|
|
blockNo := f.contentEnc.PlainOffToBlockNo(uint64(oldOffset))
|
|
|
|
oldCipherOff := int64(f.contentEnc.BlockNoToCipherOff(blockNo))
|
|
|
|
|
|
|
|
// Determine the next data offset. If the old offset points to (or beyond)
|
|
|
|
// the end of the file, the Seek syscall fails with syscall.ENXIO.
|
|
|
|
newCipherOff, err := syscall.Seek(f.intFd(), oldCipherOff, SEEK_DATA)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert ciphertext offset back to plaintext offset. At this point,
|
|
|
|
// newCipherOff should always be >= contentenc.HeaderLen. Round down,
|
|
|
|
// but ensure that the result is never smaller than the initial offset
|
|
|
|
// (to avoid endless loops).
|
|
|
|
blockNo = f.contentEnc.CipherOffToBlockNo(uint64(newCipherOff))
|
|
|
|
newOffset := int64(f.contentEnc.BlockNoToPlainOff(blockNo))
|
|
|
|
if newOffset < oldOffset {
|
|
|
|
newOffset = oldOffset
|
|
|
|
}
|
|
|
|
|
|
|
|
return newOffset, nil
|
2018-07-01 19:13:28 +02:00
|
|
|
}
|