Skip to content

Commit 14e297d

Browse files
committed
feat: enhance metadata retrieval in BackupManager with error handling and fallback
1 parent 4468bd9 commit 14e297d

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

main.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,21 +219,38 @@ def _get_file_metadata(self, file_path: Path) -> FileMetadata:
219219
"""Get complete file metadata including ownership and permissions"""
220220
stat = file_path.stat()
221221
try:
222-
owner = pwd.getpwuid(stat.st_uid).pw_name
223-
group = grp.getgrgid(stat.st_gid).gr_name
224-
except KeyError:
225-
owner = str(stat.st_uid)
226-
group = str(stat.st_gid)
227-
228-
return FileMetadata(
229-
path=str(file_path),
230-
owner=owner,
231-
group=group,
232-
mode=stat.st_mode,
233-
size=stat.st_size,
234-
modified=datetime.fromtimestamp(stat.st_mtime).isoformat(),
235-
checksum=self._calculate_file_hash(file_path)
236-
)
222+
# Try to get user/group names, fallback to IDs if not found
223+
try:
224+
owner = pwd.getpwuid(stat.st_uid).pw_name
225+
except KeyError:
226+
owner = str(stat.st_uid)
227+
228+
try:
229+
group = grp.getgrgid(stat.st_gid).gr_name
230+
except KeyError:
231+
group = str(stat.st_gid)
232+
233+
return FileMetadata(
234+
path=str(file_path),
235+
owner=owner,
236+
group=group,
237+
mode=stat.st_mode,
238+
size=stat.st_size,
239+
modified=datetime.fromtimestamp(stat.st_mtime).isoformat(),
240+
checksum=self._calculate_file_hash(file_path)
241+
)
242+
except Exception as e:
243+
logging.warning(f"Error getting metadata for {file_path}: {e}")
244+
# Return basic metadata if detailed lookup fails
245+
return FileMetadata(
246+
path=str(file_path),
247+
owner=str(stat.st_uid),
248+
group=str(stat.st_gid),
249+
mode=stat.st_mode,
250+
size=stat.st_size,
251+
modified=datetime.fromtimestamp(stat.st_mtime).isoformat(),
252+
checksum=self._calculate_file_hash(file_path)
253+
)
237254

238255
def _apply_metadata(self, file_path: Path, metadata: FileMetadata):
239256
"""Apply stored metadata to restored file"""
@@ -388,10 +405,7 @@ def backup_directory(self, source_dir: Path, backup_dir: Path) -> bool:
388405
def restore_directory(self, restore_dir: Path, output_dir: Path) -> bool:
389406
"""Restore from backup with progress tracking"""
390407
try:
391-
# Create output directory
392408
output_dir.mkdir(parents=True, exist_ok=True)
393-
394-
total_progress = 0
395409
archives = list(restore_dir.glob("*.tar.gz"))
396410

397411
# Calculate total size first
@@ -406,14 +420,12 @@ def restore_directory(self, restore_dir: Path, output_dir: Path) -> bool:
406420
restored_dirs = set()
407421

408422
for archive in archives:
409-
# Create a directory for this archive based on archive name without .tar.gz
410-
archive_name = archive.stem.replace('.tar', '') # Remove both .tar and .gz
423+
archive_name = archive.stem.replace('.tar', '')
411424
extract_dir = output_dir / archive_name
412425

413426
archive_size = archive.stat().st_size
414427
logging.info(f"Restoring {archive_name} ({humanize.naturalsize(archive_size)})")
415428

416-
# Create or clean the extraction directory
417429
if extract_dir.exists():
418430
import shutil
419431
shutil.rmtree(extract_dir)
@@ -428,8 +440,8 @@ def restore_directory(self, restore_dir: Path, output_dir: Path) -> bool:
428440
file_path = extract_dir / member.name
429441
metadata = FileMetadata(
430442
path=str(file_path),
431-
owner=pwd.getpwuid(member.uid).pw_name if member.uid >= 0 else str(member.uid),
432-
group=grp.getgrgid(member.gid).gr_name if member.gid >= 0 else str(member.gid),
443+
owner=str(member.uid), # Use ID directly
444+
group=str(member.gid), # Use ID directly
433445
mode=member.mode,
434446
size=member.size,
435447
modified=datetime.fromtimestamp(member.mtime).isoformat(),
@@ -450,7 +462,6 @@ def restore_directory(self, restore_dir: Path, output_dir: Path) -> bool:
450462
dir_size = self._get_dir_size(dir_path)
451463
logging.info(f" {dir_path.name}: {humanize.naturalsize(dir_size)}")
452464

453-
logging.info(f"Restore completed at {self._get_timestamp()}")
454465
return True
455466

456467
except Exception as e:

0 commit comments

Comments
 (0)