[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sup-devel] [PATCH 1/2] 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 <sascha-pgp@silbe.org>
---
 lib/sup.rb |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/lib/sup.rb b/lib/sup.rb
index edd23c8..09744b4 100644
--- a/lib/sup.rb
+++ b/lib/sup.rb
@@ -98,10 +98,16 @@ module Redwood
     if 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") do |f|
+        f.puts o.to_yaml
+        f.fsync
+      end
     end
   end
 
-- 
1.7.2.3

_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel