From 2f33026577d98dabea9c944640cf25f795f2ed97 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 2 Oct 2024 16:04:26 -0700 Subject: [PATCH] kiwix: Don't leave invalid .zim in library after a failed attempt Tests: - Without patch, upload an invalid zim file, 'Failed to add content'... message is shown. The library's content directory contains that invalid file. Try to add the file again and the message shown is 'File already exists'. - With patch, upload an invalid zim file, 'Failed to add content'... message is shown. The library's content directory does not contain that file. Try to add the file again and the same message is shown. - Functional tests for kiwix pass. Repeating just the test test_add_invalid_zim_file works. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/modules/kiwix/privileged.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plinth/modules/kiwix/privileged.py b/plinth/modules/kiwix/privileged.py index cc7ae117f..bcaad19e1 100644 --- a/plinth/modules/kiwix/privileged.py +++ b/plinth/modules/kiwix/privileged.py @@ -33,6 +33,7 @@ def add_package(file_name: str, temporary_file_path: str): aria2c is used for both HTTP and Magnet downloads. """ kiwix.validate_file_name(file_name) + # file_name is verified further by move_uploaded_file() # Moving files to the Kiwix library path ensures that # they can't be removed by other apps or users. @@ -42,7 +43,13 @@ def add_package(file_name: str, temporary_file_path: str): user='root', group='root', permissions=0o644) - _kiwix_manage_add(str(CONTENT_DIR / file_name)) + content_file = CONTENT_DIR / file_name + try: + _kiwix_manage_add(str(CONTENT_DIR / file_name)) + except subprocess.CalledProcessError: + # Remove the uploaded file if we could not added it to library + content_file.unlink() + raise def _kiwix_manage_add(zim_file: str):