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 (
|
|
|
|
"github.com/hanwen/go-fuse/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
|
|
|
)
|
|
|
|
|
|
|
|
// Will a write to offset "off" create a file hole?
|
2015-10-04 14:21:07 +02:00
|
|
|
func (f *file) createsHole(plainSize uint64, off int64) bool {
|
2016-02-06 19:20:54 +01:00
|
|
|
nextBlock := f.contentEnc.PlainOffToBlockNo(plainSize)
|
|
|
|
targetBlock := f.contentEnc.PlainOffToBlockNo(uint64(off))
|
2015-10-04 11:39:35 +02:00
|
|
|
if targetBlock > nextBlock {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:21:07 +02:00
|
|
|
// Zero-pad the file of size plainSize to the next block boundary
|
|
|
|
func (f *file) zeroPad(plainSize uint64) fuse.Status {
|
2016-02-06 19:20:54 +01:00
|
|
|
lastBlockLen := plainSize % f.contentEnc.PlainBS()
|
|
|
|
missing := f.contentEnc.PlainBS() - lastBlockLen
|
2016-07-01 23:29:31 +02:00
|
|
|
if missing == 0 {
|
|
|
|
// Already block-aligned
|
|
|
|
return fuse.OK
|
|
|
|
}
|
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
|
|
|
|
}
|