From 1da132584a1ffaa6f0746007d795ce45971e4530 Mon Sep 17 00:00:00 2001 From: Joshua Lock Date: Tue, 12 May 2020 10:24:24 +0100 Subject: [PATCH 1/2] Add remove method to StorageBackendInterface Define a remove method for removing/deleting items from the storage backend. The tuf reference implementation needs to be able to delete metadata files which have been marked as removed. Signed-off-by: Joshua Lock --- securesystemslib/storage.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/securesystemslib/storage.py b/securesystemslib/storage.py index 1fe2de7a..fa2c0f12 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): """ From 807a7750fcd7388d13df17b95f06f30da3d8d2d6 Mon Sep 17 00:00:00 2001 From: Joshua Lock Date: Tue, 12 May 2020 10:27:13 +0100 Subject: [PATCH 2/2] Implement remove method in local storage backend Implement the new remove method of StorageBackendInterface in the FilesystemBackend with a simple os.remove() wrapper. Signed-off-by: Joshua Lock --- securesystemslib/storage.py | 8 ++++++++ tests/test_storage.py | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/securesystemslib/storage.py b/securesystemslib/storage.py index fa2c0f12..9bc5873e 100644 --- a/securesystemslib/storage.py +++ b/securesystemslib/storage.py @@ -233,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']