mirror of
https://github.com/leminlimez/Nugget.git
synced 2025-04-08 04:23:05 +08:00
read individual files rather than all at once
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user