Skip to content

Commit

Permalink
Use shutil.move to replace os.rename or Path.rename whenever po…
Browse files Browse the repository at this point in the history
…ssible for cross fs support

resolved #5
  • Loading branch information
Fallen-Breath committed Dec 15, 2023
1 parent 2406a15 commit 4d4cf14
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
9 changes: 8 additions & 1 deletion prime_backup/action/create_backup_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,14 @@ def bp_rba(h: str) -> Path:

raw_size, blob_hash, stored_size = cr.read_size, cr.read_hash, cr.write_size
blob_path = bp_rba(blob_hash)
os.rename(temp_file_path, blob_path)

# reference: shutil.move
try:
os.rename(temp_file_path, blob_path)
except OSError:
# The temp dir is in the different file system to the blob store?
# Whatever, use file copy as the fallback
file_utils.copy_file_fast(temp_file_path, blob_path)

else:
misc_utils.assert_true(blob_hash is not None, 'blob_hash is None')
Expand Down
3 changes: 1 addition & 2 deletions prime_backup/action/create_backup_action_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def __init__(self):

def _remove_file(self, file_to_remove: Path):
try:
if file_to_remove.is_file():
file_to_remove.unlink(missing_ok=True)
file_to_remove.unlink(missing_ok=True)
except OSError as e:
self.logger.error('(rollback) remove file {!r} failed: {}'.format(file_to_remove, e))

Expand Down
17 changes: 14 additions & 3 deletions prime_backup/action/export_backup_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pathlib import Path
from typing import ContextManager, Optional, List, Tuple, IO, Any, NamedTuple

from prime_backup import constants
from prime_backup.action import Action
from prime_backup.compressors import Compressor, CompressMethod
from prime_backup.constants import BACKUP_META_FILE_NAME
Expand Down Expand Up @@ -82,14 +83,17 @@ def _i_am_root():

class _TrashBin:
def __init__(self, trash_bin_path: Path):
if trash_bin_path.exists():
shutil.rmtree(trash_bin_path)
trash_bin_path.mkdir(parents=True, exist_ok=True)

self.trash_bin_path = trash_bin_path
self.trashes: List[Tuple[Path, Path]] = [] # (trash path, original path)

def add(self, src_path: Path, relpath_in_bin: Path):
dst_path = self.trash_bin_path / relpath_in_bin
dst_path.parent.mkdir(parents=True, exist_ok=True)
src_path.rename(dst_path)
shutil.move(src_path, dst_path)
self.trashes.append((dst_path, src_path))

def erase(self):
Expand All @@ -102,7 +106,7 @@ def restore(self):
shutil.rmtree(original_path)
else:
original_path.unlink()
trash_path.rename(original_path)
shutil.move(trash_path, original_path)

self.trashes.clear()

Expand Down Expand Up @@ -232,7 +236,14 @@ def add_export_item(file_: schema.File, export_path: Path):

# 2. do the export

trash_bin = _TrashBin(self.config.storage_path / 'temp' / 'export_dir_{}_{}'.format(os.getpid(), threading.current_thread().ident))
self.output_path.mkdir(parents=True, exist_ok=True)
self.config.temp_path.mkdir(parents=True, exist_ok=True)
trash_bin_dir_name = 'export_trashes_{}_{}'.format(os.getpid(), threading.current_thread().ident)
trash_bin_path = self.config.temp_path / trash_bin_dir_name
if self.config.temp_path.stat().st_dev != self.output_path.stat().st_dev:
trash_bin_path = self.output_path / '.{}.{}'.format(constants.PLUGIN_ID, trash_bin_dir_name)
trash_bin = _TrashBin(trash_bin_path)

try:
if self.restore_mode:
# in restore mode, recover what it was like
Expand Down
2 changes: 1 addition & 1 deletion prime_backup/action/import_backup_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def open_file(self, path: Path) -> ContextManager[TarFileHolder]:
# zstd stream does not support seek operation, sowe need to extract the tar into a temp path first,
# then operate on it. requires extra spaces tho

temp_file = Config.get().storage_path / 'temp' / 'import_{}_{}.tmp'.format(os.getpid(), threading.current_thread().ident)
temp_file = Config.get().temp_path / 'import_{}_{}.tmp'.format(os.getpid(), threading.current_thread().ident)
temp_file.parent.mkdir(parents=True, exist_ok=True)
with contextlib.ExitStack() as exit_stack:
exit_stack.callback(functools.partial(temp_file.unlink, missing_ok=True))
Expand Down
5 changes: 3 additions & 2 deletions prime_backup/action/migrate_hash_method_action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import time
from typing import List, Dict, Set

Expand Down Expand Up @@ -40,7 +41,7 @@ def __migrate_blobs(self, session: DbSession, blob_hashes: List[str], old_hashes
old_hash, new_hash = blob.hash, hash_mapping[blob.hash]
old_path = blob_utils.get_blob_path(old_hash)
new_path = blob_utils.get_blob_path(new_hash)
old_path.rename(new_path)
shutil.move(old_path, new_path)

processed_hash_mapping[old_hash] = new_hash
blob.hash = new_hash
Expand Down Expand Up @@ -84,5 +85,5 @@ def run(self):
for old_hash, new_hash in processed_hash_mapping.items():
old_path = blob_utils.get_blob_path(old_hash)
new_path = blob_utils.get_blob_path(new_hash)
new_path.rename(old_path)
shutil.move(new_path, old_path)
raise
8 changes: 8 additions & 0 deletions prime_backup/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def get(cls) -> 'Config':
def storage_path(self) -> Path:
return Path(self.storage_root)

@property
def blobs_path(self) -> Path:
return self.storage_path / 'blobs'

@property
def temp_path(self) -> Path:
return self.storage_path / 'temp'

@property
def source_path(self) -> Path:
return Path(self.backup.source_root)
Expand Down
2 changes: 1 addition & 1 deletion prime_backup/utils/blob_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def get_blob_store() -> Path:
from prime_backup.config.config import Config
return Config.get().storage_path / 'blobs'
return Config.get().blobs_path


def get_blob_path(h: str) -> Path:
Expand Down

0 comments on commit 4d4cf14

Please sign in to comment.