Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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')
Exemplo n.º 9
0
 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')
Exemplo n.º 10
0
 def getTestPath(self, path):
     if 'nt' == os.name:
         return extended_path(os.path.normpath(path))
     return path
Exemplo n.º 11
0
def getTestPath(path):
    if 'nt' == os.name:
        return extended_path(os.path.normpath(path))
    return path
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
 def test_get_recycle_bin(self):
     """Unit test for get_recycle_bin"""
     for f in get_recycle_bin():
         self.assertExists(extended_path(f))