]> git.giacomolli.uk - onechat/blobdiff - memory.go
Write each line to the OS without an fsync
[onechat] / memory.go
index 9c8073f9b06fad474965ec2b9fb78812e0c9e2c9..9e38916b3a7aff1c0fd98940d5caef28c4c5b3c8 100644 (file)
--- a/memory.go
+++ b/memory.go
@@ -445,20 +445,16 @@ func (m *Memory) context(level, index int) string {
        return m.cview.Context(m.tree, end)
 }
 
        return m.cview.Context(m.tree, end)
 }
 
-// writeFileAtomic replaces the file at path with data. A reader or a crash sees
-// the old file or the new file, never a part of one.
+// writeFileAtomic replaces the file at path with data through a temporary file
+// and a rename. A reader or a crash of the process sees the old file or the new
+// file, never a part of one.
 func writeFileAtomic(path string, data []byte) error {
 func writeFileAtomic(path string, data []byte) error {
-       dir := filepath.Dir(path)
-       file, err := os.CreateTemp(dir, "."+filepath.Base(path)+".*")
+       file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
        if err != nil {
                return fmt.Errorf("create temporary file: %w", err)
        }
 
        _, err = file.Write(data)
        if err != nil {
                return fmt.Errorf("create temporary file: %w", err)
        }
 
        _, err = file.Write(data)
-       if err == nil {
-               err = file.Sync()
-       }
-
        err = errors.Join(err, file.Close())
        if err == nil {
                err = os.Rename(file.Name(), path)
        err = errors.Join(err, file.Close())
        if err == nil {
                err = os.Rename(file.Name(), path)
@@ -468,24 +464,5 @@ func writeFileAtomic(path string, data []byte) error {
                return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name()))
        }
 
                return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name()))
        }
 
-       return syncDir(dir)
-}
-
-// syncDir flushes the entries of dir to the disk.
-func syncDir(dir string) (err error) {
-       handle, err := os.Open(dir)
-       if err != nil {
-               return fmt.Errorf("open directory: %w", err)
-       }
-
-       defer func() {
-               err = errors.Join(err, handle.Close())
-       }()
-
-       err = handle.Sync()
-       if err != nil {
-               return fmt.Errorf("flush directory: %w", err)
-       }
-
        return nil
 }
        return nil
 }