##// END OF EJS Templates
windows.rename: eliminate temp name race (issue2571)...
Adrian Buehlmann -
r13206:650314ed stable
parent child Browse files
Show More
@@ -299,20 +299,18 b' def rename(src, dst):'
299 # rename is safe to do.
299 # rename is safe to do.
300 # The temporary name is chosen at random to avoid the situation
300 # The temporary name is chosen at random to avoid the situation
301 # where a file is left lying around from a previous aborted run.
301 # where a file is left lying around from a previous aborted run.
302 # The usual race condition this introduces can't be avoided as
303 # we need the name to rename into, and not the file itself. Due
304 # to the nature of the operation however, any races will at worst
305 # lead to the rename failing and the current operation aborting.
306
302
307 def tempname(prefix):
303 for tries in xrange(10):
308 for tries in xrange(10):
304 temp = '%s-%08x' % (dst, random.randint(0, 0xffffffff))
309 temp = '%s-%08x' % (prefix, random.randint(0, 0xffffffff))
305 try:
310 if not os.path.exists(temp):
306 os.rename(dst, temp) # raises OSError EEXIST if temp exists
311 return temp
307 break
308 except OSError, e:
309 if e.errno != errno.EEXIST:
310 raise
311 else:
312 raise IOError, (errno.EEXIST, "No usable temporary filename found")
312 raise IOError, (errno.EEXIST, "No usable temporary filename found")
313
313
314 temp = tempname(dst)
315 os.rename(dst, temp)
316 try:
314 try:
317 os.unlink(temp)
315 os.unlink(temp)
318 except:
316 except:
General Comments 0
You need to be logged in to leave comments. Login now