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

[sup-devel] [PATCH 2/2] {config, sources}.yaml: preserve symlinks and permissions



If config.yaml resp. sources.yaml already exists, preserve the permissions.
Also alter the save algorithm to overwrite the file in-place, thus leaving
symlinks intact.

Signed-off-by: Sascha Silbe <sascha-pgp@silbe.org>
---
All invocations of save_yaml_obj now use the "backup" mode, so maybe we
should just hardcode it in save_yaml_obj, getting rid of the (existing)
safe and (new) backup parameters.

 bin/sup-config    |    2 +-
 lib/sup.rb        |   27 ++++++++++++++++++++++-----
 lib/sup/source.rb |    8 +-------
 3 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/bin/sup-config b/bin/sup-config
index 132fd77..c4a64a3 100755
--- 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
index 09744b4..21bddb2 100644
--- 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,16 +95,33 @@ 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) do |f|
         f.puts o.to_yaml
         f.fsync
       end
       FileUtils.mv safe_fn, fn
     else
-      File.open(fn, "w") do |f|
+      File.open(fn, "w", mode) do |f|
         f.puts o.to_yaml
         f.fsync
       end
@@ -292,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
index 9c398f7..f2379fb 100644
--- 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
-- 
1.7.2.3

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