apt-sync: move_file_in handling src/file being dir

Related to "2543b0f apt-sync: fix deep pkgidx_file download error: no parent dir"
In this commit, src/file can be some directory like .tmp/by-hash,
when dst/by-hash is not empty dir, rename would fail.

Not using replace() is suggested by @happyaron since this may behave
oddly when the disk is full as this function is not atomic.
This commit is contained in:
Zenithal 2021-10-07 01:50:16 +08:00
parent 225d555544
commit ef3a5bccac
No known key found for this signature in database
GPG Key ID: 1189C659F3D04C1C

View File

@ -105,6 +105,10 @@ def move_files_in(src: Path, dst: Path):
empty = False empty = False
print(f"moving {file} to {dst}") print(f"moving {file} to {dst}")
# shutil.move(str(file), str(dst)) # shutil.move(str(file), str(dst))
if file.is_dir():
(dst / file.name).mkdir(parents=True, exist_ok=True)
move_files_in(file, dst / file.name)
else:
file.rename(dst / file.name) # Overwrite files file.rename(dst / file.name) # Overwrite files
if empty: if empty:
print(f"{src} is empty") print(f"{src} is empty")