From eb70493a00fc6664dce3757945bc7eae3536bad5 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 5 Jul 2026 21:46:07 +0800 Subject: [PATCH] Execute storage migration copies --- core/storage.py | 39 +++++++++++++++++++++++++++++---------- tests/test_s2_routes.py | 12 ++++++++++-- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/core/storage.py b/core/storage.py index bd89f3a..adb4f92 100644 --- a/core/storage.py +++ b/core/storage.py @@ -2,6 +2,7 @@ from __future__ import annotations +import shutil from dataclasses import dataclass from pathlib import Path from typing import Any @@ -41,26 +42,23 @@ class VfsStorage: } def migration_plan(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]: - if not dry_run: - raise ApiError( - ErrorCode.VALIDATION_FAILED, - "storage migration execution is not enabled; run with dry_run=true", - {"field": "dry_run"}, - ) source_path = self.resolve_uri(source_uri) target_path = self.resolve_uri(target_uri) if not source_path.exists(): raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "source media not found", {"uri": source_uri}) files, total_bytes = _path_file_stats(source_path) + will_overwrite = target_path.exists() + if not dry_run: + _copy_path(source_path, target_path) return { - "dry_run": True, - "status": "planned", + "dry_run": dry_run, + "status": "planned" if dry_run else "completed", "source_uri": source_uri, "target_uri": target_uri, "files": files, "bytes": total_bytes, - "will_overwrite": target_path.exists(), - "execution_enabled": False, + "will_overwrite": will_overwrite, + "execution_enabled": True, } def resolve_uri(self, uri: str) -> Path: @@ -151,6 +149,27 @@ def _path_file_stats(path: Path) -> tuple[int, int]: return 0, 0 +def _copy_path(source_path: Path, target_path: Path) -> None: + if source_path.is_file(): + target_path.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(source_path, target_path) + return + + source_root = source_path.resolve() + target_root = target_path.resolve() + if target_root == source_root or source_root in target_root.parents: + raise ApiError(ErrorCode.FORBIDDEN, "target path cannot be inside source directory") + + target_root.mkdir(parents=True, exist_ok=True) + for child in source_root.rglob("*"): + if not child.is_file(): + continue + relative = child.relative_to(source_root) + destination = target_root / relative + destination.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(child, destination) + + def _is_writable(path: Path) -> bool: target = path if path.exists() else path.parent return target.exists() and target.is_dir() diff --git a/tests/test_s2_routes.py b/tests/test_s2_routes.py index ecebd2c..c05b861 100644 --- a/tests/test_s2_routes.py +++ b/tests/test_s2_routes.py @@ -694,8 +694,16 @@ database: }, self.media_service, ) - self.assertFalse(execution["ok"]) - self.assertEqual(execution["error_code"], "VALIDATION_FAILED") + self.assertTrue(execution["ok"]) + self.assertFalse(execution["data"]["dry_run"]) + self.assertEqual(execution["data"]["status"], "completed") + self.assertEqual(execution["data"]["files"], 1) + self.assertEqual(execution["data"]["bytes"], 5) + self.assertTrue(execution["data"]["will_overwrite"]) + self.assertTrue(execution["data"]["execution_enabled"]) + self.assertEqual((self.root / "archives" / "sample.bin").read_bytes(), b"12345") + self.assertEqual((self.root / "images" / "sample.bin").read_bytes(), b"12345") + self.assertNotIn(str(self.root), str(execution["data"])) def test_asset_import_pack_dry_run_is_safe(self): pack = self.root / "materials" / "pack"