def _test_wipe(contents, deny_access=False, is_sparse=False): shortname = _write_file(longname, contents) if deny_access or is_sparse: fh = open_file(extended_path(longname), mode=GENERIC_WRITE | WRITE_DAC) if is_sparse: file_make_sparse(fh) if deny_access: _deny_access(fh) close_file(fh) logger.debug( 'test_file_wipe(): filename length={}, shortname length ={}, contents length={}, is_sparse={}' .format(len(longname), len(shortname), len(contents), is_sparse)) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname)
def write_file(self, filename, contents=''): """Create a temporary file, optionally writing contents to it""" if not os.path.isabs(filename): filename = os.path.join(self.tempdir, filename) with open(extended_path(filename), 'wb') as f: f.write(contents) assert (os.path.exists(extended_path(filename))) return filename
def write_file(self, filename, contents=''): """Create a temporary file, optionally writing contents to it""" if not os.path.isabs(filename): filename = os.path.join(self.tempdir, filename) with open(extended_path(filename), 'wb') as f: f.write(contents) assert (os.path.exists(extended_path(filename))) return filename
def _write_file(longname, contents): self.write_file(longname, contents) import win32api shortname = extended_path_undo( win32api.GetShortPathName(extended_path(longname))) self.assertExists(shortname) return shortname
def _write_file(longname, contents): self.write_file(longname, contents) import win32api shortname = extended_path_undo( win32api.GetShortPathName(extended_path(longname))) self.assertExists(shortname) return shortname
def _test_wipe(contents): shortname = _write_file(longname, contents) logger.debug('test_file_wipe(): filename length={}, shortname length ={}, contents length={}'.format( len(longname), len(shortname), len(contents))) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname)
def _test_wipe(contents): shortname = _write_file(longname, contents) logger.debug('test_file_wipe(): filename length={}, shortname length ={}, contents length={}'.format( len(longname), len(shortname), len(contents))) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname)
def test_get_recycle_bin(self): """Unit test for get_recycle_bin""" for f in get_recycle_bin(): self.assertExists(extended_path(f)) if not common.destructive_tests('get_recycle_bin'): return put_files_into_recycle_bin() # clear recycle bin counter = 0 for f in get_recycle_bin(): counter += 1 FileUtilities.delete(f) self.assertGreaterEqual(counter, 3, 'deleted %d' % counter) # now it should be empty for f in get_recycle_bin(): self.fail('recycle bin should be empty, but it is not')
def test_get_recycle_bin(self): """Unit test for get_recycle_bin""" for f in get_recycle_bin(): self.assert_(os.path.exists(extended_path(f)), f) if not common.destructive_tests('get_recycle_bin'): return put_files_into_recycle_bin() # clear recycle bin counter = 0 for f in get_recycle_bin(): counter += 1 FileUtilities.delete(f) self.assert_(counter >= 3, 'deleted %d' % counter) # now it should be empty for f in get_recycle_bin(): self.fail('recycle bin should be empty, but it is not')
def getTestPath(self, path): if 'nt' == os.name: return extended_path(os.path.normpath(path)) return path
def getTestPath(path): if 'nt' == os.name: return extended_path(os.path.normpath(path)) return path
def file_wipe(file_name): # add \\?\ if it does not exist to support Unicode and long paths file_name = extended_path(file_name) check_os() win_version, _ = determine_win_version() volume = volume_from_file(file_name) volume_info = get_volume_information(volume) cluster_size = (volume_info.sectors_per_cluster * volume_info.bytes_per_sector) file_handle = open_file(file_name) file_size, is_special = get_file_basic_info(file_name, file_handle) orig_extents = get_extents(file_handle) if is_special: bridged_extents = [ x for x in logical_ranges_to_extents( get_extents(file_handle, False), True) ] CloseHandle(file_handle) #logger.debug('Original extents: {}'.format(orig_extents)) volume_handle = obtain_readwrite(volume) file_handle = open_file(file_name, GENERIC_READ | GENERIC_WRITE) if not is_special: # Direct overwrite when it's a regular file. #logger.info("Attempting direct file wipe.") wipe_file_direct(file_handle, orig_extents, cluster_size, file_size) new_extents = get_extents(file_handle) CloseHandle(file_handle) #logger.debug('New extents: {}'.format(new_extents)) if orig_extents == new_extents: clean_up(None, volume_handle, None) return # Expectation was that extents should be identical and file is wiped. # If OS didn't give that to us, continue below and use defrag wipe. # Any extent within new_extents has now been wiped by above. # It can be subtracted from the orig_extents list, and now we will # just clean up anything not yet overwritten. orig_extents = extents_a_minus_b(orig_extents, new_extents) else: # File needs special treatment. We can't just do a basic overwrite. # First we will truncate it. Then chase down the freed clusters to # wipe them, now that they are no longer part of the file. truncate_file(file_handle) CloseHandle(file_handle) # Poll to confirm that our clusters were freed. poll_clusters_freed(volume_handle, volume_info.total_clusters, orig_extents) # Chase down all the freed clusters we can, and wipe them. #logger.debug("Attempting defrag file wipe.") # Put the temp file in the same folder as the target wipe file. # Should be able to write this path if user can write the wipe file. tmp_file_path = os.path.dirname(file_name) + os.sep + tmp_file_name if is_special: orig_extents = choose_if_bridged(volume_handle, volume_info.total_clusters, orig_extents, bridged_extents) for lcn_start, lcn_end in orig_extents: result = wipe_extent_by_defrag(volume_handle, lcn_start, lcn_end, cluster_size, volume_info.total_clusters, tmp_file_path) # Clean up. clean_up(None, volume_handle, tmp_file_path) return
def file_wipe(file_name): # add \\?\ if it does not exist to support Unicode and long paths file_name = extended_path(file_name) check_os() win_version, _ = determine_win_version() volume = volume_from_file(file_name) volume_info = get_volume_information(volume) cluster_size = (volume_info.sectors_per_cluster * volume_info.bytes_per_sector) file_handle = open_file(file_name) file_size, is_special = get_file_basic_info(file_name, file_handle) orig_extents = get_extents(file_handle) if is_special: bridged_extents = [x for x in logical_ranges_to_extents( get_extents(file_handle, False), True)] CloseHandle(file_handle) #logger.debug('Original extents: {}'.format(orig_extents)) volume_handle = obtain_readwrite(volume) file_handle = open_file(file_name, GENERIC_READ | GENERIC_WRITE) if not is_special: # Direct overwrite when it's a regular file. #logger.info("Attempting direct file wipe.") wipe_file_direct(file_handle, orig_extents, cluster_size, file_size) new_extents = get_extents(file_handle) CloseHandle(file_handle) #logger.debug('New extents: {}'.format(new_extents)) if orig_extents == new_extents: clean_up(None, volume_handle, None) return # Expectation was that extents should be identical and file is wiped. # If OS didn't give that to us, continue below and use defrag wipe. # Any extent within new_extents has now been wiped by above. # It can be subtracted from the orig_extents list, and now we will # just clean up anything not yet overwritten. orig_extents = extents_a_minus_b(orig_extents, new_extents) else: # File needs special treatment. We can't just do a basic overwrite. # First we will truncate it. Then chase down the freed clusters to # wipe them, now that they are no longer part of the file. truncate_file(file_handle) CloseHandle(file_handle) # Poll to confirm that our clusters were freed. poll_clusters_freed(volume_handle, volume_info.total_clusters, orig_extents) # Chase down all the freed clusters we can, and wipe them. #logger.debug("Attempting defrag file wipe.") # Put the temp file in the same folder as the target wipe file. # Should be able to write this path if user can write the wipe file. tmp_file_path = os.path.dirname(file_name) + os.sep + tmp_file_name if is_special: orig_extents = choose_if_bridged(volume_handle, volume_info.total_clusters, orig_extents, bridged_extents) for lcn_start, lcn_end in orig_extents: result = wipe_extent_by_defrag(volume_handle, lcn_start, lcn_end, cluster_size, volume_info.total_clusters, tmp_file_path) # Clean up. clean_up(None, volume_handle, tmp_file_path) return
def test_get_recycle_bin(self): """Unit test for get_recycle_bin""" for f in get_recycle_bin(): self.assertExists(extended_path(f))