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())
|
||||
|
||||
Reference in New Issue
Block a user