Show More
@@ -737,13 +737,28 b' def rename(src, dst):' | |||
|
737 | 737 | try: |
|
738 | 738 | os.rename(src, dst) |
|
739 | 739 | except OSError, err: # FIXME: check err (EEXIST ?) |
|
740 | # on windows, rename to existing file is not allowed, so we | |
|
741 | # must delete destination first. but if file is open, unlink | |
|
742 | # schedules it for delete but does not delete it. rename | |
|
740 | ||
|
741 | # On windows, rename to existing file is not allowed, so we | |
|
742 | # must delete destination first. But if a file is open, unlink | |
|
743 | # schedules it for delete but does not delete it. Rename | |
|
743 | 744 | # happens immediately even for open files, so we rename |
|
744 |
# destination to a temporary name, then delete that. |
|
|
745 | # destination to a temporary name, then delete that. Then | |
|
745 | 746 | # rename is safe to do. |
|
746 | temp = dst + "-force-rename" | |
|
747 | # The temporary name is chosen at random to avoid the situation | |
|
748 | # where a file is left lying around from a previous aborted run. | |
|
749 | # The usual race condition this introduces can't be avoided as | |
|
750 | # we need the name to rename into, and not the file itself. Due | |
|
751 | # to the nature of the operation however, any races will at worst | |
|
752 | # lead to the rename failing and the current operation aborting. | |
|
753 | ||
|
754 | def tempname(prefix): | |
|
755 | for tries in xrange(10): | |
|
756 | temp = '%s-%08x' % (prefix, random.randint(0, 0xffffffff)) | |
|
757 | if not os.path.exists(temp): | |
|
758 | return temp | |
|
759 | raise IOError, (errno.EEXIST, "No usable temporary filename found") | |
|
760 | ||
|
761 | temp = tempname(dst) | |
|
747 | 762 | os.rename(dst, temp) |
|
748 | 763 | os.unlink(temp) |
|
749 | 764 | os.rename(src, dst) |
General Comments 0
You need to be logged in to leave comments.
Login now