WriteDirIV: delete incomplete gocryptfs.diriv file if write fails

If the underlying filesystem is full, writing to gocryptfs.diriv may
fail, and later fsck show this:

	OpenDir "xyz": could not read gocryptfs.diriv: wanted 16 bytes, got 0

Uncovered by xfstests generic/083.

Also fixes a fd leak in the error path.
This commit is contained in:
Jakob Unterwurzacher 2018-07-15 12:00:08 +02:00
parent bcca323cb7
commit bbf5b72fff
1 changed files with 5 additions and 0 deletions

View File

@ -99,12 +99,17 @@ func WriteDirIV(dirfd *os.File, dir string) error {
fd := os.NewFile(uintptr(fdRaw), file)
_, err = fd.Write(iv)
if err != nil {
fd.Close()
tlog.Warn.Printf("WriteDirIV: Write: %v", err)
// Delete incomplete gocryptfs.diriv file
syscallcompat.Unlinkat(int(dirfd.Fd()), file, 0)
return err
}
err = fd.Close()
if err != nil {
tlog.Warn.Printf("WriteDirIV: Close: %v", err)
// Delete incomplete gocryptfs.diriv file
syscallcompat.Unlinkat(int(dirfd.Fd()), file, 0)
return err
}
return nil