summaryrefslogtreecommitdiff
path: root/pathfs_frontend/file_holes.go
blob: 8c9c83fb3e4bb529908927964b75df7e5c9f47f4 (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
31
32
33
34
35
36
37
38
package pathfs_frontend

import (
	"fmt"
	"github.com/hanwen/go-fuse/fuse"
	"github.com/rfjakob/gocryptfs/cryptfs"
)

// Will a write to offset "off" create a file hole?
func (f *file) createsHole(cipherSize uint64, off int64) bool {
	nextBlock := f.cfs.BlockNoCipherOff(cipherSize)
	targetBlock := f.cfs.BlockNoPlainOff(uint64(off))
	if targetBlock > nextBlock {
		return true
	}
	return false
}

// Zero-pad the file if a write to "off" creates a file hole
func (f *file) conditionalZeroPad(off int64) fuse.Status {
	fi, err := f.fd.Stat()
	if err != nil {
		cryptfs.Warn.Printf("conditionalZeroPad: Stat: %v\n", err)
		return fuse.ToStatus(err)
	}
	cipherSize := uint64(fi.Size())

	if f.createsHole(cipherSize, off) == false {
		return fuse.OK
	}

	plainSize := f.cfs.PlainSize(cipherSize)
	lastBlockLen := plainSize % f.cfs.PlainBS()
	missing := f.cfs.PlainBS() - lastBlockLen
	pad := make([]byte, missing)
	_, status := f.doWrite(pad, int64(plainSize))
	return status
}