lib/sup.rb in sup-0.12 vs lib/sup.rb in sup-0.12.1

- old
+ new

@@ -36,11 +36,11 @@ end end end module Redwood - VERSION = "0.12" + VERSION = "0.12.1" BASE_DIR = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup") CONFIG_FN = File.join(BASE_DIR, "config.yaml") COLOR_FN = File.join(BASE_DIR, "colors.yaml") SOURCE_FN = File.join(BASE_DIR, "sources.yaml") @@ -84,26 +84,49 @@ end 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 o.before_marshal else o end - if safe + mode = if File.exists? fn + File.stat(fn).mode + else + 0600 + end + + if backup + backup_fn = fn + '.bak' + if File.exists?(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 def load_yaml_obj fn, compress=false o = if File.exists? fn @@ -283,10 +306,10 @@ :poll_interval => 300, :wrap_width => 0, :slip_rows => 0 } begin - Redwood::save_yaml_obj config, filename + Redwood::save_yaml_obj config, filename, false, true rescue StandardError => e $stderr.puts "warning: #{e.message}" end config end