From 8709754d9065d5f2b1503169423c26f3333e3ce6 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 5 Dec 2009 02:36:11 +0100 Subject: [PATCH] Try to handle the case where an editor re-creates files. Untested. --- pws | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pws b/pws index 4a3b0ad..439eb7c 100755 --- 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 -- 2.39.5