summaryrefslogtreecommitdiff
path: root/pathfs_frontend
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-12-06 15:05:52 +0100
committerJakob Unterwurzacher2015-12-06 15:05:52 +0100
commit56888d83dd50a10bad56eaa512ce0b6a2f2d41ed (patch)
tree51343937e7347dfa2d79813bbf9dee8f63168e0f /pathfs_frontend
parentedc289fb7578036ecc7571cb0207ceac17af9eec (diff)
fallocate the space needed for the file header beforehand
This makes sure writing to a file fails early if the underlying filesystem does not support fallocate. It also prevents partial header write due to ENOSPC.
Diffstat (limited to 'pathfs_frontend')
-rw-r--r--pathfs_frontend/file.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/pathfs_frontend/file.go b/pathfs_frontend/file.go
index 5c76fb1..16d83c1 100644
--- a/pathfs_frontend/file.go
+++ b/pathfs_frontend/file.go
@@ -93,7 +93,18 @@ func (f *file) readHeader() error {
func (f *file) createHeader() error {
h := cryptfs.RandomHeader()
buf := h.Pack()
- _, err := f.fd.WriteAt(buf, 0)
+
+ // Prevent partially written (=corrupt) header by preallocating the space beforehand
+ f.fdLock.Lock()
+ defer f.fdLock.Unlock()
+ err := syscall.Fallocate(int(f.fd.Fd()), FALLOC_FL_KEEP_SIZE, 0, cryptfs.HEADER_LEN)
+ if err != nil {
+ cryptfs.Warn.Printf("createHeader: Fallocate failed: %s\n", err.Error())
+ return err
+ }
+
+ // Actually write header
+ _, err = f.fd.WriteAt(buf, 0)
if err != nil {
return err
}