]> err.no Git - pwstore/commitdiff
Try to handle the case where an editor re-creates files. Untested.
authorPeter Palfrader <peter@palfrader.org>
Sat, 5 Dec 2009 01:36:11 +0000 (02:36 +0100)
committerPeter Palfrader <peter@palfrader.org>
Sat, 5 Dec 2009 01:36:11 +0000 (02:36 +0100)
pws

diff --git a/pws b/pws
index 4a3b0ad0901c0e5e705e6e2348ae779fc919a649..439eb7c6c7212a8331f8b387d3f276e9c6e7fd41 100755 (executable)
--- a/pws
+++ b/pws
@@ -613,15 +613,27 @@ class Ed
         proceed = read_input("Warning: Editor did not exit successfully (exit code #{status.exitstatus}.  Proceed?")
         exit(0) unless proceed
       end
-      tempfile.seek(0, IO::SEEK_SET)
-      content = tempfile.read
 
-      # zero the file
+      # some editors do not write new content in place, but instead
+      # make a new file and more it in the old file's place.
+      begin
+        reopened = File.open(tempfile.path, "r+")
+      rescue Exception => e
+        STDERR.puts e
+        exit(1)
+      end
+      content = reopened.read
+
+      # zero the file, well, both of them.
       newsize = content.length
-      tempfile.seek(0, IO::SEEK_SET)
       clearsize = (newsize > oldsize) ? newsize : oldsize
-      tempfile.print "\0"*clearsize
-      tempfile.fsync
+
+      [tempfile, reopened].each do |f|
+        f.seek(0, IO::SEEK_SET)
+        f.print "\0"*clearsize
+        f.fsync
+      end
+      reopened.close
       tempfile.close(true)
 
       if content.length == 0