commit 4e5d3ece2fb6f63988a05792ed7047158bcc894a
parent f27661b1656ae1f0d28fd89595b5a16f268d8d3d
Author: Gaute Hope <eg@gaute.vetsj.com>
Date: Thu, 10 Oct 2013 14:44:53 +0200
monkeypatch File with a safe_link and use it in maildir source
Diffstat:
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/lib/sup/maildir.rb b/lib/sup/maildir.rb
@@ -57,7 +57,7 @@ class Maildir < Source
f.fsync
end
- File.link tmp_path, new_path
+ File.safe_link tmp_path, new_path
stored = true
ensure
File.unlink tmp_path if File.exists? tmp_path
@@ -244,9 +244,9 @@ private
new_path = File.join @dir, new_loc
tmp_path = File.join @dir, "tmp", "#{md_base}:#{md_ver},#{flags}"
- File.link orig_path, tmp_path
+ File.safe_link orig_path, tmp_path
File.unlink orig_path
- File.link tmp_path, new_path
+ File.safe_link tmp_path, new_path
File.unlink tmp_path
new_loc
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
@@ -8,6 +8,7 @@ require 'set'
require 'enumerator'
require 'benchmark'
require 'unicode'
+require 'fileutils'
## time for some monkeypatching!
class Symbol
@@ -45,6 +46,17 @@ class Lockfile
def touch_yourself; touch path end
end
+class File
+ # platform safe file.link which attempts a copy if hard-linking fails
+ def self.safe_link src, dest
+ begin
+ File.link src, dest
+ rescue
+ FileUtils.copy src, dest
+ end
+ end
+end
+
class Pathname
def human_size
s =