186 # add enough data to the journal to do the truncate |
186 # add enough data to the journal to do the truncate |
187 self.file.write("%s\0%d\n" % (file, offset)) |
187 self.file.write("%s\0%d\n" % (file, offset)) |
188 self.file.flush() |
188 self.file.flush() |
189 |
189 |
190 @active |
190 @active |
191 def addbackup(self, file, hardlink=True, vfs=None): |
191 def addbackup(self, file, hardlink=True, location=''): |
192 """Adds a backup of the file to the transaction |
192 """Adds a backup of the file to the transaction |
193 |
193 |
194 Calling addbackup() creates a hardlink backup of the specified file |
194 Calling addbackup() creates a hardlink backup of the specified file |
195 that is used to recover the file in the event of the transaction |
195 that is used to recover the file in the event of the transaction |
196 aborting. |
196 aborting. |
203 raise RuntimeError(msg) |
203 raise RuntimeError(msg) |
204 |
204 |
205 if file in self.map or file in self._backupmap: |
205 if file in self.map or file in self._backupmap: |
206 return |
206 return |
207 dirname, filename = os.path.split(file) |
207 dirname, filename = os.path.split(file) |
208 |
|
209 backupfilename = "%s.backup.%s" % (self.journal, filename) |
208 backupfilename = "%s.backup.%s" % (self.journal, filename) |
210 backupfile = os.path.join(dirname, backupfilename) |
209 backupfile = os.path.join(dirname, backupfilename) |
211 if vfs is None: |
210 vfs = self._vfsmap[location] |
212 vfs = self.opener |
|
213 if vfs.exists(file): |
211 if vfs.exists(file): |
214 filepath = vfs.join(file) |
212 filepath = vfs.join(file) |
215 backuppath = vfs.join(backupfile) |
213 backuppath = vfs.join(backupfile) |
216 util.copyfiles(filepath, backuppath, hardlink=hardlink) |
214 util.copyfiles(filepath, backuppath, hardlink=hardlink) |
217 else: |
215 else: |
218 backupfile = '' |
216 backupfile = '' |
219 |
217 |
220 self._addbackupentry(('', file, backupfile, False)) |
218 self._addbackupentry((location, file, backupfile, False)) |
221 |
219 |
222 def _addbackupentry(self, entry): |
220 def _addbackupentry(self, entry): |
223 """register a new backup entry and write it to disk""" |
221 """register a new backup entry and write it to disk""" |
224 self._backupentries.append(entry) |
222 self._backupentries.append(entry) |
225 self._backupmap[file] = len(self._backupentries) - 1 |
223 self._backupmap[file] = len(self._backupentries) - 1 |