diff --git a/services/app/domain/download_repairs.py b/services/app/domain/download_repairs.py index 625f25c4..f2b432db 100644 --- a/services/app/domain/download_repairs.py +++ b/services/app/domain/download_repairs.py @@ -65,6 +65,16 @@ def destination_shape(value): } +def same_destination(previous, current): + # Switching an unimported download from hardlink to copy moves nothing, and copy works + # where hardlinks cannot. Any other change still needs a new selection. + before, after = destination_shape(previous), destination_shape(current) + mode_ok = before.get("mode") == after.get("mode") or after.get("mode") == "copy" + before.pop("mode", None) + after.pop("mode", None) + return mode_ok and before == after + + async def proposal(db, user, attempt, selection): await member(db, user.id) if user.role != "admin": @@ -116,7 +126,7 @@ async def proposal(db, user, attempt, selection): "restore the submitted route before connection repair", ) configuration = await destination_configuration(db, destination) - if destination_shape(configuration) != destination_shape(frozen["destination"]): + if not same_destination(frozen["destination"], configuration): raise HTTPException( 409, "The library destination changed; this repair cannot move an existing import" ) diff --git a/tests/integration/test_download_repairs.py b/tests/integration/test_download_repairs.py index 621dbe51..fcdcd21c 100644 --- a/tests/integration/test_download_repairs.py +++ b/tests/integration/test_download_repairs.py @@ -57,6 +57,18 @@ async def change(database, selected, kind="downloader"): **destination.probe, "configuration_revision": fingerprint(configuration), } # Contract fixture; actual probes have their own filesystem/ABS suite. + elif kind == "copy": + # Hardlinks fail on some filesystems, so the library switches to copying files. + destination = await db.get(ImportDestination, selection.destination_id) + assert destination.mode == "hardlink" + destination.mode = "copy" + await db.flush() + configuration = await destination_configuration(db, destination) + destination.probe = { + **destination.probe, + "copy": True, + "configuration_revision": fingerprint(configuration), + } else: (await db.get(Integration, selection.downloader_id)).credential_generation += 1 @@ -82,7 +94,7 @@ async def state(database, identifier): return attempt, row -@pytest.mark.parametrize("kind", ["downloader", "source", "backend"]) +@pytest.mark.parametrize("kind", ["downloader", "source", "backend", "copy"]) async def test_reviewed_generations_resume_same_transfer_and_preserve_frozen_selection( client, database, selected, submitted, downloader, kind ): diff --git a/tests/unit/test_download_repair_destination.py b/tests/unit/test_download_repair_destination.py new file mode 100644 index 00000000..99c0d464 --- /dev/null +++ b/tests/unit/test_download_repair_destination.py @@ -0,0 +1,14 @@ +from app.domain.download_repairs import same_destination + +BEFORE = {"root_key": "audio", "mode": "hardlink", "backend": {"generation": 1, "url": "abs"}} + + +def test_switching_to_copy_keeps_the_destination(): + assert same_destination(BEFORE, {**BEFORE, "mode": "copy"}) + assert same_destination(BEFORE, {**BEFORE, "backend": {"generation": 2, "url": "abs"}}) + + +def test_other_changes_still_need_a_new_selection(): + assert not same_destination({**BEFORE, "mode": "copy"}, BEFORE) + assert not same_destination(BEFORE, {**BEFORE, "mode": "copy", "root_key": "other"}) + assert not same_destination(BEFORE, {**BEFORE, "backend": {"generation": 1, "url": "other"}})