From: Sascha Silbe <sascha-pgp@silbe.org>
To: sup-devel <sup-devel@rubyforge.org>
Subject: [sup-devel] [PATCH 2/2] {config, sources}.yaml: preserve symlinks and permissions
Date: Tue, 18 Jan 2011 19:28:44 +0100 [thread overview]
Message-ID: <1295375324-2496-2-git-send-email-sascha-pgp@silbe.org> (raw)
In-Reply-To: <1295375324-2496-1-git-send-email-sascha-pgp@silbe.org>
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
next prev parent reply other threads:[~2011-01-18 19:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-01-18 18:28 [sup-devel] [PATCH 1/2] ensure sources.yaml gets flushed to disk Sascha Silbe
2011-01-18 18:28 ` Sascha Silbe [this message]
2011-01-19 3:41 ` [sup-devel] [PATCH 2/2] {config, sources}.yaml: preserve symlinks and permissions Rich Lane
2011-01-19 10:05 ` Sascha Silbe
2011-01-19 15:38 ` William Morgan
2011-01-19 16:42 ` Sascha Silbe
2011-01-19 17:17 ` William Morgan
2011-01-19 18:44 ` Rich Lane
2011-01-19 17:19 ` William Morgan
2011-01-19 18:41 ` Ben Walton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1295375324-2496-2-git-send-email-sascha-pgp@silbe.org \
--to=sascha-pgp@silbe.org \
--cc=sascha-ml-reply-to-2011-1@silbe.org \
--cc=sup-devel@rubyforge.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox