diff --git a/securesystemslib/storage.py b/securesystemslib/storage.py index 1fe2de7a..9bc5873e 100644 --- a/securesystemslib/storage.py +++ b/securesystemslib/storage.py @@ -93,6 +93,25 @@ def put(self, fileobj, filepath): raise NotImplementedError # pragma: no cover + @abc.abstractmethod + def remove(self, filepath): + """ + + Remove the file at 'filepath' from the storage. + + + filepath: + The full path to the file. + + + securesystemslib.exceptions.StorageError, if the file can not be removed. + + + None + """ + raise NotImplementedError # pragma: no cover + + @abc.abstractmethod def getsize(self, filepath): """ @@ -214,6 +233,14 @@ def put(self, fileobj, filepath): "Can't write file %s" % filepath) + def remove(self, filepath): + try: + os.remove(filepath) + except (FileNotFoundError, PermissionError, OSError): # pragma: no cover + raise securesystemslib.exceptions.StorageError( + "Can't remove file %s" % filepath) + + def getsize(self, filepath): try: return os.path.getsize(filepath) diff --git a/tests/test_storage.py b/tests/test_storage.py index 41a52d22..998cdd56 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -81,6 +81,10 @@ def test_files(self): with open(put_path, 'rb') as put_file: self.assertEqual(put_file.read(), self.fileobj.read()) + self.assertTrue(os.path.exists(put_path)) + self.storage_backend.remove(put_path) + self.assertFalse(os.path.exists(put_path)) + def test_folders(self): leaves = ['test1', 'test2', 'test3']