From a8195f5d8034f82aef04d2da223fc0a9c0dc9d38 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 27 Jan 2014 22:19:50 +0000 Subject: Non-writtables test files are now chmod'ed to be properly removed. --- MAT/exceptions.py | 6 ++++++ MAT/mat.py | 10 +++++++++- test/libtest.py | 2 +- test/test.py | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/MAT/exceptions.py b/MAT/exceptions.py index 7972f20..47da15c 100644 --- a/MAT/exceptions.py +++ b/MAT/exceptions.py @@ -6,3 +6,9 @@ class UnableToRemoveFile(Exception): '''This exception is raised when a file could not be removed ''' pass + +class UnableToWriteFile(Exception): + '''This exception is raised when a file + can could not be chmod +w + ''' + pass diff --git a/MAT/mat.py b/MAT/mat.py index 4d64c2d..32f55d8 100644 --- a/MAT/mat.py +++ b/MAT/mat.py @@ -112,6 +112,13 @@ class XMLParser(xml.sax.handler.ContentHandler): def secure_remove(filename): ''' Securely remove the file ''' + # I want the file removed, even if it's ro + try: + os.chmod(filename, 0o777) + except OSError: + logging.error('Unable to add write rights to %s' % filename) + raise MAT.exceptions.UnableToWriteFile + try: if not subprocess.call(['shred', '--remove', filename]): return True @@ -122,11 +129,12 @@ def secure_remove(filename): try: os.remove(filename) - return True except OSError: logging.error('Unable to remove %s' % filename) raise MAT.exceptions.UnableToRemoveFile + return True + def create_class_file(name, backup, **kwargs): ''' Return a $FILETYPEStripper() class, diff --git a/test/libtest.py b/test/libtest.py index cbc807f..c2cb2f1 100644 --- a/test/libtest.py +++ b/test/libtest.py @@ -98,7 +98,7 @@ class TestSecureRemove(unittest.TestCase): def test_remove_fail(self): ''' test the secure removal of an non-removable file ''' - self.assertRaises(MAT.exceptions.UnableToRemoveFile, MAT.mat.secure_remove, '/NOTREMOVABLE') + self.assertRaises(MAT.exceptions.UnableToWriteFile, MAT.mat.secure_remove, '/NOTREMOVABLE') class TestArchiveProcessing(test.MATTest): diff --git a/test/test.py b/test/test.py index 1641f88..49a657b 100644 --- a/test/test.py +++ b/test/test.py @@ -62,6 +62,9 @@ class MATTest(unittest.TestCase): ''' Remove the tmp folder ''' + for root, dirs, files in os.walk(self.tmpdir): + for d in dirs + files: + os.chmod(os.path.join(root, d), 0o777) shutil.rmtree(self.tmpdir) -- cgit v1.3