sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit abae119526c5c705042679f17b04c39c28b97cb2
parent 74cf329d3b63a519eb86a422e9b3947b3cd1563d
Author: Sascha Silbe <sascha-pgp@silbe.org>
Date:   Tue, 18 Jan 2011 19:28:43 +0100

ensure sources.yaml gets flushed to disk

Before renaming sources.yaml we need to fsync() it, otherwise we could end up
with an empty file in case of a crash [1].

[1] http://thunk.org/tytso/blog/2009/03/12/delayed-allocation-and-the-zero-length-file-problem/

Signed-off-by: Sascha Silbe 

Diffstat:
M bin/sup-config | 2 +-
M lib/sup.rb | 35 +++++++++++++++++++++++++++++------
M lib/sup/source.rb | 8 +-------
3 files changed, 31 insertions(+), 14 deletions(-)
diff --git a/bin/sup-config b/bin/sup-config
@@ -191,7 +191,7 @@ else
   end
 end
 
-Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
+Redwood::save_yaml_obj $config, Redwood::CONFIG_FN, false, true
 
 say "Ok, I've saved you up a nice lil' #{Redwood::CONFIG_FN}."
 
diff --git a/lib/sup.rb b/lib/sup.rb
@@ -86,7 +86,7 @@ module Redwood
   module_function :reporting_thread, :record_exception, :exceptions
 
 ## one-stop shop for yamliciousness
-  def save_yaml_obj o, fn, safe=false
+  def save_yaml_obj o, fn, safe=false, backup=false
     o = if o.is_a?(Array)
       o.map { |x| (x.respond_to?(:before_marshal) && x.before_marshal) || x }
     elsif o.respond_to? :before_marshal
@@ -95,13 +95,36 @@ module Redwood
       o
     end
 
-    if safe
+    mode = if File.exists? fn
+      File.stat(fn).mode
+    else
+      0600
+    end
+
+    if backup
+      backup_fn = fn + '.bak'
+      unless File.exists?(backup_fn) && File.size(fn) == 0
+        File.open(backup_fn, "w", mode) do |f|
+          File.open(fn, "r") { |old_f| FileUtils.copy_stream old_f, f }
+          f.fsync
+        end
+      end
+      File.open(fn, "w") do |f|
+        f.puts o.to_yaml
+        f.fsync
+      end
+    elsif safe
       safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}"
-      mode = File.stat(fn).mode if File.exists? fn
-      File.open(safe_fn, "w", mode) { |f| f.puts o.to_yaml }
+      File.open(safe_fn, "w", mode) do |f|
+        f.puts o.to_yaml
+        f.fsync
+      end
       FileUtils.mv safe_fn, fn
     else
-      File.open(fn, "w") { |f| f.puts o.to_yaml }
+      File.open(fn, "w", mode) do |f|
+        f.puts o.to_yaml
+        f.fsync
+      end
     end
   end
 
@@ -286,7 +309,7 @@ EOS
         :col_jump => 2
       }
       begin
-        Redwood::save_yaml_obj config, filename
+        Redwood::save_yaml_obj config, filename, false, true
       rescue StandardError => e
         $stderr.puts "warning: #{e.message}"
       end
diff --git a/lib/sup/source.rb b/lib/sup/source.rb
@@ -212,13 +212,7 @@ class SourceManager
   def save_sources fn=Redwood::SOURCE_FN
     @source_mutex.synchronize do
       if @sources_dirty
-        bakfn = fn + ".bak"
-        if File.exists? fn
-          File.chmod 0600, fn
-          FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
-        end
-        Redwood::save_yaml_obj sources, fn, true
-        File.chmod 0600, fn
+        Redwood::save_yaml_obj sources, fn, false, true
       end
       @sources_dirty = false
     end