def clean_up(file_handle, volume_handle, tmp_file_path): try: if file_handle: CloseHandle(file_handle) if volume_handle: CloseHandle(volume_handle) if tmp_file_path: DeleteFile(tmp_file_path) except: pass
def rmdirRecursiveWindows(dir): """Windows-specific version of rmdirRecursive that handles path lengths longer than MAX_PATH. """ dir = os.path.realpath(dir) # Make sure directory is writable SetFileAttributesW('\\\\?\\' + dir, FILE_ATTRIBUTE_NORMAL) for ffrec in FindFiles('\\\\?\\' + dir + '\\*.*'): file_attr = ffrec[0] name = ffrec[8] if name == '.' or name == '..': continue full_name = os.path.join(dir, name) if file_attr & FILE_ATTRIBUTE_DIRECTORY: rmdirRecursiveWindows(full_name) else: SetFileAttributesW('\\\\?\\' + full_name, FILE_ATTRIBUTE_NORMAL) DeleteFile('\\\\?\\' + full_name) RemoveDirectory('\\\\?\\' + dir)
def wipe_extent_by_defrag(volume_handle, lcn_start, lcn_end, cluster_size, total_clusters, tmp_file_path): assert cluster_size > 0 logger.debug("Examining extent from %d to %d for wipe...", lcn_start, lcn_end) write_length = (lcn_end - lcn_start + 1) * cluster_size # Check the state of the volume bitmap for the extent we want to # overwrite. If any sectors are allocated, reduce the task # into smaller parts. # We also reduce to smaller pieces if the extent is larger than # 2 megabytes. For no particular reason except to avoid the entire # request failing because one cluster became allocated. volume_bitmap, bitmap_size = get_volume_bitmap(volume_handle, total_clusters) # This option simulates another process that grabs clusters on disk # from time to time. # It should be moved away after QA is complete. if not simulate_concurrency: count_free, count_allocated = check_extents([(lcn_start, lcn_end)], volume_bitmap) else: count_free, count_allocated = check_extents_concurrency( [(lcn_start, lcn_end)], volume_bitmap, tmp_file_path, volume_handle, total_clusters) if count_allocated > 0 and count_free == 0: return False if count_allocated > 0 or write_length > write_buf_size * 4: if lcn_start < lcn_end: for split_s, split_e in split_extent(lcn_start, lcn_end): wipe_extent_by_defrag(volume_handle, split_s, split_e, cluster_size, total_clusters, tmp_file_path) return True else: return False # Put the zero-fill file in place. file_handle = CreateFile(tmp_file_path, GENERIC_READ | GENERIC_WRITE, 0, None, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, None) write_zero_fill(file_handle, write_length) new_extents = get_extents(file_handle) # We know the original extent was contiguous. # The new zero-fill file may not be contiguous, so it requires a # loop to be sure of reaching the end of the new file's clusters. new_vcn = 0 for new_lcn_start, new_lcn_end in new_extents: # logger.debug("Zero-fill wrote from %d to %d", # new_lcn_start, new_lcn_end) cluster_count = new_lcn_end - new_lcn_start + 1 cluster_dest = lcn_start + new_vcn if new_lcn_start != cluster_dest: logger.debug("Move %d clusters to %d", cluster_count, cluster_dest) try: move_file(volume_handle, file_handle, new_vcn, cluster_dest, cluster_count) except: # Move file failed, probably because another process # has allocated a cluster on disk. # Break into smaller pieces and do what we can. logger.debug("!! Move encountered an error !!") CloseHandle(file_handle) if lcn_start < lcn_end: for split_s, split_e in split_extent(lcn_start, lcn_end): wipe_extent_by_defrag(volume_handle, split_s, split_e, cluster_size, total_clusters, tmp_file_path) return True else: return False else: # If Windows put the zero-fill extent on the exact clusters we # intended to place it, no need to attempt a move. logging.debug("No need to move extent from %d", new_lcn_start) new_vcn += cluster_count CloseHandle(file_handle) DeleteFile(tmp_file_path) return True
def uninstall(): DeleteFile( r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\autorun.bat' % getuser())