better file writing/fix crontab -e

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-02-23 08:19:06 +00:00
parent f89d94867f
commit c5db9456ef
1 changed files with 19 additions and 18 deletions

View File

@ -580,10 +580,10 @@ class FileBuffer(Buffer):
raise Exception, "Path '%s' cannot be read" % (path)
f = open(path, 'r')
return f
def _open_file_w(self, path=None):
def _open_file_w(self, path=None, preserve=True):
if path is None:
path = self.path
if os.path.isfile(path):
if preserve and os.path.isfile(path):
raise Exception, "Path '%s' already exists" % (path)
d = os.path.dirname(path)
if not os.access(d, os.R_OK):
@ -660,23 +660,24 @@ class FileBuffer(Buffer):
raise Exception, "oh no! %r has changed on-disk!" % self.path
temp_path = self._temp_path()
data = self.make_string()
if self.windows[0].mode.savetabs:
data = data.replace(" ", "\t")
shutil.copyfile(self.path, temp_path)
data = self.write_filter(data)
try:
data = self.make_string()
if self.windows[0].mode.savetabs:
data = data.replace(" ", "\t")
data = self.write_filter(data)
f2 = self._open_file_w(temp_path)
f2.write(self.bytemark + data)
f2.close()
if self.path_exists():
mode = os.stat(self.path)[0]
os.chmod(temp_path, mode)
shutil.move(temp_path, self.path)
self.store_checksum(data)
self.modified = False
f2 = self._open_file_w(self.path, preserve=False)
f2.write(self.bytemark + data)
f2.close()
except:
shutil.copyfile(temp_path, self.path)
else:
self.store_checksum(data)
self.modified = False
finally:
os.unlink(temp_path)
def save_as(self, path):
self.path = path
self.save()