From 842af4463989ee6808d397433e9aba8517e49c89 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 1 Sep 2026 22:35:45 +0200 Subject: fsync gocryptfs.diriv files There is a performance penalty here, but the alternative is potential data loss on a system crash / power outage as shown in https://github.com/rfjakob/gocryptfs/issues/1033 . Also deduplicate the code a little using goto. Fixes https://github.com/rfjakob/gocryptfs/issues/1033 --- internal/nametransform/diriv.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/internal/nametransform/diriv.go b/internal/nametransform/diriv.go index 5dd4940..aaa65ee 100644 --- a/internal/nametransform/diriv.go +++ b/internal/nametransform/diriv.go @@ -83,16 +83,22 @@ func WriteDirIVAt(dirfd int) error { if !syscallcompat.IsENOSPC(err) { tlog.Warn.Printf("WriteDirIV: Write: %v", err) } - // Delete incomplete gocryptfs.diriv file - syscallcompat.Unlinkat(dirfd, DirIVFilename, 0) - return err + goto delete + } + err = f.Sync() + if err != nil { + tlog.Warn.Printf("WriteDirIV: Sync: %v", err) + goto delete } err = f.Close() if err != nil { tlog.Warn.Printf("WriteDirIV: Close: %v", err) - // Delete incomplete gocryptfs.diriv file - syscallcompat.Unlinkat(dirfd, DirIVFilename, 0) - return err + goto delete } return nil + +delete: + // Delete potentially incomplete gocryptfs.diriv file + syscallcompat.Unlinkat(dirfd, DirIVFilename, 0) + return err } -- cgit v1.2.3