msd: using .incomplete files instead of .complete

This commit is contained in:
Maxim Devaev 2023-08-19 06:21:32 +03:00
parent 1f64f7b3ba
commit 61de01d892
2 changed files with 35 additions and 14 deletions

View File

@ -57,15 +57,24 @@ def _mkdir(path: str) -> None:
raise SystemExit(f"Can't create directory: {err}") raise SystemExit(f"Can't create directory: {err}")
def _rmdir(path: str) -> None: def _rmtree(path: str) -> None:
if exists(path): if exists(path):
_log(f"RMDIR --- {path}") _log(f"RMALL --- {path}")
try: try:
os.rmdir(path) shutil.rmtree(path)
except Exception as err: except Exception as err:
raise SystemExit(f"Can't remove directory: {err}") raise SystemExit(f"Can't remove directory: {err}")
def _rm(path: str) -> None:
if exists(path):
_log(f"RM --- {path}")
try:
os.remove(path)
except Exception as err:
raise SystemExit(f"Can't remove file: {err}")
def _move(src: str, dest: str) -> None: def _move(src: str, dest: str) -> None:
_log(f"MOVE --- {src} --> {dest}") _log(f"MOVE --- {src} --> {dest}")
try: try:
@ -85,15 +94,22 @@ def _chown(path: str, user: str) -> None:
# ===== # =====
def _fix_msd(part: Partition) -> None: def _fix_msd(part: Partition) -> None:
# First images migration
images_path = join(part.root_path, "images") images_path = join(part.root_path, "images")
meta_path = join(part.root_path, "meta") meta_path = join(part.root_path, "meta")
if exists(images_path) and exists(meta_path): if exists(images_path) and exists(meta_path):
for name in os.listdir(meta_path):
_move(join(meta_path, name), join(part.root_path, f".__{name}"))
_rmdir(meta_path)
for name in os.listdir(images_path): for name in os.listdir(images_path):
_move(join(images_path, name), os.path.join(part.root_path, name)) _move(join(images_path, name), os.path.join(part.root_path, name))
_rmdir(images_path) if not exists(join(meta_path, f"{name}.complete")):
open(os.path.join(part.root_path, f".__{name}.incomplete")).close() # pylint: disable=consider-using-with
_rmtree(images_path)
_rmtree(meta_path)
# Second images migration
for name in os.listdir(part.root_path):
if name.startswith(".__") and name.endswith(".complete"):
_rm(join(part.root_path, name))
if part.user: if part.user:
_chown(part.root_path, part.user) _chown(part.root_path, part.user)

View File

@ -54,7 +54,7 @@ class Image(_ImageDc):
super().__init__(name, path) super().__init__(name, path)
self.__storage = storage self.__storage = storage
(self.__dir_path, file_name) = os.path.split(path) (self.__dir_path, file_name) = os.path.split(path)
self.__complete_path = os.path.join(self.__dir_path, f".__{file_name}.complete") self.__incomplete_path = os.path.join(self.__dir_path, f".__{file_name}.incomplete")
self.__adopted = False self.__adopted = False
async def _reload(self) -> None: # Only for Storage() and set_complete() async def _reload(self) -> None: # Only for Storage() and set_complete()
@ -80,7 +80,7 @@ class Image(_ImageDc):
async def __is_complete(self) -> bool: async def __is_complete(self) -> bool:
if self.__storage: if self.__storage:
return (await aiofiles.os.path.exists(self.__complete_path)) return (not (await aiofiles.os.path.exists(self.__incomplete_path)))
return True return True
async def __is_removable(self) -> bool: async def __is_removable(self) -> bool:
@ -113,25 +113,30 @@ class Image(_ImageDc):
async def remove(self, fatal: bool) -> None: async def remove(self, fatal: bool) -> None:
assert self.__storage assert self.__storage
removed = False
try: try:
await aiofiles.os.remove(self.path) await aiofiles.os.remove(self.path)
removed = True
self.__storage.images.pop(self.name, None) self.__storage.images.pop(self.name, None)
except FileNotFoundError: except FileNotFoundError:
pass pass
except Exception: except Exception:
if fatal: if fatal:
raise raise
await self.set_complete(False) finally:
# Удаляем .incomplete вместе с файлом
if removed:
await self.set_complete(True)
async def set_complete(self, flag: bool) -> None: async def set_complete(self, flag: bool) -> None:
assert self.__storage assert self.__storage
if flag: if flag:
async with aiofiles.open(self.__complete_path, "w"): try:
await aiofiles.os.remove(self.__incomplete_path)
except FileNotFoundError:
pass pass
else: else:
try: async with aiofiles.open(self.__incomplete_path, "w"):
await aiofiles.os.remove(self.__complete_path)
except FileNotFoundError:
pass pass
await self._reload() await self._reload()