def copyOrMove(elem, copy_move_flag, overwrite): if copy_move_flag == COPY: f = 0 if overwrite else win32file.COPY_FILE_FAIL_IF_EXISTS win32file.CopyFileEx(elem.path, elem.destpath, None, None, False, f) else: f = win32file.MOVEFILE_COPY_ALLOWED | win32file.MOVEFILE_REPLACE_EXISTING if overwrite else win32file.MOVEFILE_COPY_ALLOWED win32file.MoveFileEx(elem.path, elem.destpath, f)
def atomic_rename(oldpath, newpath): '''Replace the file newpath with the file oldpath. Can fail if the files are on different volumes. If succeeds, guaranteed to be atomic. newpath may or may not exist. If it exists, it is replaced. ''' if iswindows: import win32file win32file.MoveFileEx( oldpath, newpath, win32file.MOVEFILE_REPLACE_EXISTING | win32file.MOVEFILE_WRITE_THROUGH) else: os.rename(oldpath, newpath)
def rename(self, src, dest): try: if iswindows: import win32file, pywintypes try: win32file.MoveFileEx(src, dest, win32file.MOVEFILE_REPLACE_EXISTING|win32file.MOVEFILE_WRITE_THROUGH) except pywintypes.error as e: raise_winerror(e) else: os.rename(src, dest) except EnvironmentError as e: if e.errno != errno.ENOENT: # the source of the rename does not exist raise
def reverse_backup(filename): #{ try: #{ #Rename the file. #This is a fix for in use files and also race conditions in deletion #introduced by the "FILE_SHARE_DELETE" state on windows. tempFilename = filename + "." + str(time.time()) shutil.move(filename, tempFilename) #Move backup back in place. shutil.move(filename + ".bak", filename) #Remove modified binary. os.remove(tempFilename) return True #} except Exception as e: #{ if os.name == "nt": #{ try: #{ #Work around for in-use windows files. import win32file import win32api #Schedule it for deletion on reboot. win32file.MoveFileEx(tempFilename, None, win32file.MOVEFILE_DELAY_UNTIL_REBOOT) return True #} except Exception as e: #{ print CYAN + filename + RED + ":" + RED + " Failed to move backup in place (" + e.strerror + ")!" return False #} #} else: #{ print CYAN + filename + RED + ":" + RED + " Failed to move backup in place (" + e.strerror + ")!" autodoc.escalate_admin() #Set the path to the current directory. os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) return False
def atomic_rename(oldpath, newpath): '''Replace the file newpath with the file oldpath. Can fail if the files are on different volumes. If succeeds, guaranteed to be atomic. newpath may or may not exist. If it exists, it is replaced. ''' if iswindows: import win32file for i in xrange(10): try: win32file.MoveFileEx(oldpath, newpath, win32file.MOVEFILE_REPLACE_EXISTING|win32file.MOVEFILE_WRITE_THROUGH) break except Exception: if i > 8: raise # Try the rename repeatedly in case something like a virus # scanner has opened one of the files (I love windows) time.sleep(1) else: os.rename(oldpath, newpath)
def _unlink(path): make_writable(path) try: unlink(path) except (IOError, OSError) as e: if on_win and e.errno == 13: try: win32file.MoveFileEx(file_or_symlink_path, None, win32file.MOVEFILE_DELAY_UNTIL_REBOOT) log.info( "Windows thinks file %s is in use. We have scheduled it for " "removal at next reboot." % file_or_symlink_path) except pywintypes.error: log.info( "Windows thinks file %s is in use. We can't schedule it for removal" " because this is not an admin prompt (thanks Windows.)" % file_or_symlink_path) else: raise
def mf_impl(a, b): win32file.MoveFileEx( a, b, win32file.MOVEFILE_REPLACE_EXISTING | win32file.MOVEFILE_WRITE_THROUGH)
def move_file_ex(path, new_file_name, flags): # Windows API call win32file.MoveFileEx(path, new_file_name, flags)