read individual files rather than all at once

This commit is contained in:
leminlimez
2025-03-13 21:29:37 -04:00
parent 86bb3958de
commit fd4502dd4f
5 changed files with 44 additions and 23 deletions

View File

@@ -23,19 +23,35 @@ class BackupFile:
@dataclass
class ConcreteFile(BackupFile):
contents: bytes
src_path: Optional[str] = None
owner: int = 0
group: int = 0
inode: Optional[int] = None
mode: _FileMode = DEFAULT
hash: bytes = None
size: int = None
def read_contents(self) -> bytes:
contents = self.contents
if self.contents == None:
with open(self.src_path, "rb") as in_file:
contents = in_file.read()
# prepopulate hash and size
self.hash = sha1(contents).digest()
self.size = len(contents)
return contents
def to_record(self) -> mbdb.MbdbRecord:
if self.inode is None:
self.inode = int.from_bytes(randbytes(8), "big")
if self.hash == None or self.size == None:
self.read_contents()
return mbdb.MbdbRecord(
domain=self.domain,
filename=self.path,
link="",
hash=sha1(self.contents).digest(),
hash=self.hash,
key=b"",
mode=self.mode | _FileMode.S_IFREG,
#unknown2=0,
@@ -46,7 +62,7 @@ class ConcreteFile(BackupFile):
mtime=int(datetime.now().timestamp()),
atime=int(datetime.now().timestamp()),
ctime=int(datetime.now().timestamp()),
size=len(self.contents),
size=self.size,
flags=4,
properties=[]
)
@@ -126,7 +142,7 @@ class Backup:
if isinstance(file, ConcreteFile):
#print("Writing", file.path, "to", directory / sha1((file.domain + "-" + file.path).encode()).digest().hex())
with open(directory / sha1((file.domain + "-" + file.path).encode()).digest().hex(), "wb") as f:
f.write(file.contents)
f.write(file.read_contents())
with open(directory / "Manifest.mbdb", "wb") as f:
f.write(self.generate_manifest_db().to_bytes())

View File

@@ -4,8 +4,9 @@ from pymobiledevice3.services.installation_proxy import InstallationProxyService
import os
class FileToRestore:
def __init__(self, contents: str, restore_path: str, domain: str = None, owner: int = 501, group: int = 501):
def __init__(self, contents: str, restore_path: str, contents_path: str = None, domain: str = None, owner: int = 501, group: int = 501):
self.contents = contents
self.contents_path = contents_path
self.restore_path = restore_path
self.domain = domain
self.owner = owner
@@ -75,7 +76,8 @@ def concat_regular_file(file: FileToRestore, files_list: list[FileToRestore], la
file.domain,
owner=file.owner,
group=file.group,
contents=file.contents
contents=file.contents,
src_path=file.contents_path
))
return new_last_domain, full_path