From b194189c9be75a223cf9e0394915bb633b2cde72 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 15 Jul 2023 11:45:51 -0600 Subject: [PATCH] Cannot delete a directory if it contains a tagged FileGroup. --- wrolpi/conftest.py | 11 ++++++++--- wrolpi/errors.py | 4 ---- wrolpi/files/api.py | 3 ++- wrolpi/files/lib.py | 29 ++++++++++++++++++++--------- wrolpi/files/test/test_api.py | 29 ++++++++++++++++++++++++++++- 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/wrolpi/conftest.py b/wrolpi/conftest.py index edcec13c..cba572a1 100644 --- a/wrolpi/conftest.py +++ b/wrolpi/conftest.py @@ -35,7 +35,7 @@ from wrolpi.dates import set_test_now from wrolpi.db import postgres_engine, get_db_args from wrolpi.downloader import DownloadManager, DownloadResult, Download, Downloader, \ downloads_manager_config_context -from wrolpi.files.models import Directory +from wrolpi.files.models import Directory, FileGroup from wrolpi.root_api import BLUEPRINTS, api_app from wrolpi.tags import Tag from wrolpi.vars import PROJECT_DIR @@ -341,8 +341,8 @@ def example_mobi(test_directory) -> pathlib.Path: @pytest.fixture -def make_files_structure(test_directory) -> Callable[[Union[List, Dict]], List[pathlib.Path]]: - def create_files(paths: List) -> List[pathlib.Path]: +def make_files_structure(test_directory) -> Callable: + def create_files(paths: List, file_groups: bool = False, session: Session = None) -> List[pathlib.Path]: files = [] @iterify(list) @@ -366,6 +366,11 @@ def make_files_structure(test_directory) -> Callable[[Union[List, Dict]], List[p path.write_text(content) elif isinstance(content, bytes): path.write_bytes(content) + + if file_groups: + for idx, path in enumerate(files): + fg = FileGroup.from_paths(session, path) + files[idx] = fg return files return create_files diff --git a/wrolpi/errors.py b/wrolpi/errors.py index caa2ea42..775a2e17 100644 --- a/wrolpi/errors.py +++ b/wrolpi/errors.py @@ -192,10 +192,6 @@ class FileConflict(APIError): pass -class DirectoryConflict(APIError): - pass - - error_codes = iter(range(1, 1000)) API_ERRORS = { diff --git a/wrolpi/files/api.py b/wrolpi/files/api.py index aa0abe14..6cb1461c 100644 --- a/wrolpi/files/api.py +++ b/wrolpi/files/api.py @@ -257,10 +257,11 @@ async def post_rename(_: Request, body: schema.Rename): body=schema.Directory, ) @validate(schema.Directory) +@wrol_mode_check async def delete_directory(_: Request, body: schema.Directory): path = get_media_directory() / body.path if path.is_dir(): - shutil.rmtree(path) + lib.delete_directory(path, recursive=True) else: raise FileNotFoundError(f'No directory found: {path}') diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index fa26be3b..0c05a0c7 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -46,7 +46,7 @@ logger = logger.getChild(__name__) __all__ = ['list_directories_contents', 'delete', 'split_path_stem_and_suffix', 'refresh_files', 'search_files', 'get_mimetype', 'split_file_name_words', 'get_primary_file', 'get_file_statistics', 'estimate_search', - 'move', 'rename', 'remove_directory'] + 'move', 'rename', 'delete_directory'] @optional_session @@ -166,7 +166,7 @@ def delete(*paths: Union[str, pathlib.Path]): raise FileGroupIsTagged(f"Cannot delete {file_group} because it is tagged") for path in paths: if path.is_dir(): - remove_directory(path) + delete_directory(path) else: path.unlink() @@ -1073,7 +1073,7 @@ def _move(destination: pathlib.Path, *sources: pathlib.Path) -> List[Tuple[pathl plan = do_plan(plan) for source in sources: if source.is_dir(): - remove_directory(source) + delete_directory(source) logger.info(f'Move execution completed') return plan except Exception as e: @@ -1084,16 +1084,27 @@ def _move(destination: pathlib.Path, *sources: pathlib.Path) -> List[Tuple[pathl directories = sorted(walk(destination), key=lambda i: len(i.parents), reverse=True) for path in directories: if path.is_dir(): - remove_directory(path) + delete_directory(path) # Remove destination only if it did not exist at the start. if destination.is_dir() and destination_existed is False: - remove_directory(destination) + delete_directory(destination) raise -def remove_directory(directory: pathlib.Path): - """Remove a directory (only if it is empty) remove it's Directory record.""" - if directory.is_dir(): +def delete_directory(directory: pathlib.Path, recursive: bool = False): + """Remove a directory, remove it's Directory record. + + Will refuse to delete a directory if it contains Tagged Files.""" + if recursive: + with get_db_session() as session: + tagged = session.query(FileGroup) \ + .filter(FileGroup.primary_path.like(f'{directory}/%')) \ + .join(TagFile, TagFile.file_group_id == FileGroup.id) \ + .limit(1).one_or_none() + if tagged: + raise FileGroupIsTagged() + shutil.rmtree(directory) + else: directory.rmdir() with get_db_curs(commit=True) as curs: stmt = 'DELETE FROM directory WHERE path=%s' @@ -1165,7 +1176,7 @@ async def rename_directory(directory: pathlib.Path, new_name: str) -> pathlib.Pa paths = list(directory.iterdir()) await move(new_directory, *paths) # Remove the old directory. - remove_directory(directory) + delete_directory(directory) return new_directory diff --git a/wrolpi/files/test/test_api.py b/wrolpi/files/test/test_api.py index 7b863ed6..45014674 100644 --- a/wrolpi/files/test/test_api.py +++ b/wrolpi/files/test/test_api.py @@ -543,7 +543,7 @@ async def test_move(test_session, test_directory, make_files_structure, test_asy @pytest.mark.asyncio -async def test_rename(test_session, test_directory, make_files_structure, test_async_client): +async def test_rename_file(test_session, test_directory, make_files_structure, test_async_client): foo, = make_files_structure({ 'foo/bar/baz.txt': 'asdf', }) @@ -570,3 +570,30 @@ async def test_rename_directory(test_session, test_directory, make_files_structu assert not (test_directory / 'foo/bar').exists() assert (test_directory / 'foo/qux/baz.txt').is_file() assert (test_directory / 'foo/qux/baz.txt').read_text() == 'asdf' + + +@pytest.mark.asyncio +async def test_delete_directory_tagged(test_session, test_directory, make_files_structure, test_async_client, + tag_factory, insert_file_group): + """Cannot delete a directory if it contained a tagged FileGroup.""" + bar, = make_files_structure({ + 'foo/bar.txt': 'bar' + }, file_groups=True, session=test_session) + bar: FileGroup + tag = tag_factory() + bar.add_tag(tag) + test_session.commit() + + # Cannot deleted tagged file. + content = dict(path=str(bar.primary_path.parent)) + request, response = await test_async_client.post('/api/files/delete_directory', content=json.dumps(content)) + assert response.status_code == HTTPStatus.CONFLICT + assert (test_directory / 'foo').is_dir() + + # Remove tag, directory is deleted recursively. + bar.remove_tag(tag) + test_session.commit() + content = dict(path=str(bar.primary_path.parent)) + request, response = await test_async_client.post('/api/files/delete_directory', content=json.dumps(content)) + assert response.status_code == HTTPStatus.NO_CONTENT + assert not (test_directory / 'foo').exists()