summaryrefslogtreecommitdiff
path: root/internal/fusefrontend/file_holes.go
blob: 34f702fe1df3e0fd249d3027d2c59f85514f7f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package fusefrontend

// Helper functions for sparse files (files with holes)

import (
	"github.com/hanwen/go-fuse/fuse"

	"github.com/rfjakob/gocryptfs/internal/tlog"
)

// Will a write to offset "off" create a file hole?
func (f *file) createsHole(plainSize uint64, off int64) bool {
	nextBlock := f.contentEnc.PlainOffToBlockNo(plainSize)
	targetBlock := f.contentEnc.PlainOffToBlockNo(uint64(off))
	return targetBlock > nextBlock
}

// Zero-pad the file of size plainSize to the next block boundary
func (f *file) zeroPad(plainSize uint64) fuse.Status {
	lastBlockLen := plainSize % f.contentEnc.PlainBS()
	missing := f.contentEnc.PlainBS() - lastBlockLen
	if missing == 0 {
		// Already block-aligned
		return fuse.OK
	}
	pad := make([]byte, missing)
	tlog.Debug.Printf("zeroPad: Writing %d bytes\n", missing)
	_, status := f.doWrite(pad, int64(plainSize))
	return status
}