Revision 081b1e69
b/lib/utils.py | ||
---|---|---|
748 | 748 |
raise errors.ProgrammerError("Can't make a backup of a non-file '%s'" % |
749 | 749 |
file_name) |
750 | 750 |
|
751 |
# Warning: the following code contains a race condition when we create more |
|
752 |
# than one backup of the same file in a second. |
|
753 |
backup_name = file_name + '.backup-%d' % int(time.time()) |
|
754 |
shutil.copyfile(file_name, backup_name) |
|
751 |
prefix = '%s.backup-%d.' % (os.path.basename(file_name), int(time.time())) |
|
752 |
dir = os.path.dirname(file_name) |
|
753 |
|
|
754 |
fsrc = open(file_name, 'rb') |
|
755 |
try: |
|
756 |
(fd, backup_name) = tempfile.mkstemp(prefix=prefix, dir=dir) |
|
757 |
fdst = os.fdopen(fd, 'wb') |
|
758 |
try: |
|
759 |
shutil.copyfileobj(fsrc, fdst) |
|
760 |
finally: |
|
761 |
fdst.close() |
|
762 |
finally: |
|
763 |
fsrc.close() |
|
764 |
|
|
755 | 765 |
return backup_name |
756 | 766 |
|
757 | 767 |
|
Also available in: Unified diff