Skip to content

Commit

Permalink
Fix file_wipe() for special files
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhon committed Apr 26, 2020
1 parent 12523a7 commit 1b7668a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 6 additions & 1 deletion bleachbit/WindowsWipe.py
Expand Up @@ -399,7 +399,7 @@ def spike_cluster(volume_handle, cluster, tmp_file_path):
# should look.
def check_mapped_bit(volume_bitmap, lcn):
assert isinstance(lcn, int)
mapped_bit = ord(volume_bitmap[lcn // 8])
mapped_bit = volume_bitmap[lcn // 8]
bit_location = lcn % 8 # zero-based
if bit_location > 0:
mapped_bit = mapped_bit >> bit_location
Expand Down Expand Up @@ -436,6 +436,11 @@ def open_file(file_name, mode=GENERIC_READ):
return file_handle


# Close file
def close_file(file_handle):
CloseHandle(file_handle)


# Get some basic information about a file.
def get_file_basic_info(file_name, file_handle):
file_attributes = GetFileAttributesW(file_name)
Expand Down
16 changes: 12 additions & 4 deletions tests/TestWindows.py
Expand Up @@ -373,7 +373,8 @@ def test_file_wipe(self):
There are more tests in testwipe.py
"""

from bleachbit.WindowsWipe import file_wipe
from bleachbit.WindowsWipe import file_wipe, open_file, close_file, file_make_sparse
from win32file import GENERIC_WRITE

dirname = tempfile.mkdtemp(prefix='bleachbit-file-wipe')

Expand All @@ -390,10 +391,14 @@ def _write_file(longname, contents):
self.assertExists(shortname)
return shortname

def _test_wipe(contents):
def _test_wipe(contents, is_sparse=False):
shortname = _write_file(longname, contents)
logger.debug('test_file_wipe(): filename length={}, shortname length ={}, contents length={}'.format(
len(longname), len(shortname), len(contents)))
if is_sparse:
fh = open_file(extended_path(longname), mode=GENERIC_WRITE)
file_make_sparse(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)
Expand All @@ -412,6 +417,9 @@ def _test_wipe(contents):
# requires wiping of extents
_test_wipe(b'secret' * 100000)

# requires wiping of extents: special file case
_test_wipe(b'secret' * 100000, is_sparse=True)

shutil.rmtree(dirname, True)

if shell.IsUserAnAdmin():
Expand Down

0 comments on commit 1b7668a

Please sign in to comment.