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 _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 wipe_contents(path, truncate=True): """Wipe files contents http://en.wikipedia.org/wiki/Data_remanence 2006 NIST Special Publication 800-88 (p. 7): "Studies have shown that most of today's media can be effectively cleared by one overwrite" """ def wipe_write(): size = getsize(path) try: f = open(path, 'wb') except IOError as e: if e.errno == errno.EACCES: # permission denied os.chmod(path, 0o200) # user write only f = open(path, 'wb') else: raise blanks = chr(0) * 4096 while size > 0: f.write(blanks) size -= 4096 f.flush() # flush to OS buffer os.fsync(f.fileno()) # force write to disk return f if 'nt' == os.name: from win32com.shell.shell import IsUserAnAdmin if 'nt' == os.name and IsUserAnAdmin(): from bleachbit.WindowsWipe import file_wipe, UnsupportedFileSystemError import warnings from bleachbit import _ try: file_wipe(path) except pywinerror as e: # 32=The process cannot access the file because it is being used by another process. # 33=The process cannot access the file because another process has # locked a portion of the file. if not e.winerror in (32, 33): # handle only locking errors raise # Try to truncate the file. This makes the behavior consistent # with Linux and with Windows when IsUserAdmin=False. try: with open(path, 'wb') as f: truncate_f(f) except IOError as e2: if errno.EACCES == e2.errno: # Common when the file is locked # Errno 13 Permission Denied pass # translate exception to mark file to deletion in Command.py raise WindowsError(e.winerror, e.strerror) except UnsupportedFileSystemError as e: warnings.warn( _('There was at least one file on a file system that does not support advanced overwriting.'), UserWarning) f = wipe_write() else: # The wipe succeed, so prepare to truncate. f = open(path, 'wb') else: f = wipe_write() if truncate: truncate_f(f) f.close()
def wipe_contents(path, truncate=True): """Wipe files contents http://en.wikipedia.org/wiki/Data_remanence 2006 NIST Special Publication 800-88 (p. 7): "Studies have shown that most of today's media can be effectively cleared by one overwrite" """ def wipe_write(): size = getsize(path) try: f = open(path, 'wb') except IOError as e: if e.errno == errno.EACCES: # permission denied os.chmod(path, 0o200) # user write only f = open(path, 'wb') else: raise blanks = b'\0' * 4096 while size > 0: f.write(blanks) size -= 4096 f.flush() # flush to OS buffer os.fsync(f.fileno()) # force write to disk return f if 'nt' == os.name: from win32com.shell.shell import IsUserAnAdmin if 'nt' == os.name and IsUserAnAdmin(): from bleachbit.WindowsWipe import file_wipe, UnsupportedFileSystemError import warnings from bleachbit import _ try: file_wipe(path) except pywinerror as e: # 32=The process cannot access the file because it is being used by another process. # 33=The process cannot access the file because another process has # locked a portion of the file. if not e.winerror in (32, 33): # handle only locking errors raise # Try to truncate the file. This makes the behavior consistent # with Linux and with Windows when IsUserAdmin=False. try: with open(path, 'w') as f: truncate_f(f) except IOError as e2: if errno.EACCES == e2.errno: # Common when the file is locked # Errno 13 Permission Denied pass # translate exception to mark file to deletion in Command.py raise WindowsError(e.winerror, e.strerror) except UnsupportedFileSystemError as e: warnings.warn( _('There was at least one file on a file system that does not support advanced overwriting.' ), UserWarning) f = wipe_write() else: # The wipe succeed, so prepare to truncate. f = open(path, 'w') else: f = wipe_write() if truncate: truncate_f(f) f.close()