correct the linearize-data script (minor)

this clips off unwanted final data in event of reorg and fixes the file open
mode to prevent the truncation of files reopened

small patch being passed around which I did not write but thought would be
helpful to the community at large
This commit is contained in:
midnight 2025-02-16 16:25:19 -08:00
parent d6fe5b28df
commit e9dc16194b

View file

@ -157,6 +157,7 @@ class BlockDataCopier:
def writeBlock(self, inhdr, blk_hdr, rawblock): def writeBlock(self, inhdr, blk_hdr, rawblock):
blockSizeOnDisk = len(inhdr) + len(blk_hdr) + len(rawblock) blockSizeOnDisk = len(inhdr) + len(blk_hdr) + len(rawblock)
if not self.fileOutput and ((self.outsz + blockSizeOnDisk) > self.maxOutSz): if not self.fileOutput and ((self.outsz + blockSizeOnDisk) > self.maxOutSz):
os.ftruncate(self.outF.fileno(), self.outF.tell())
self.outF.close() self.outF.close()
if self.setFileTime: if self.setFileTime:
os.utime(self.outFname, (int(time.time()), self.highTS)) os.utime(self.outFname, (int(time.time()), self.highTS))
@ -170,6 +171,7 @@ class BlockDataCopier:
print("New month " + blkDate.strftime("%Y-%m") + " @ " + self.hash_str) print("New month " + blkDate.strftime("%Y-%m") + " @ " + self.hash_str)
self.lastDate = blkDate self.lastDate = blkDate
if self.outF: if self.outF:
os.ftruncate(self.outF.fileno(), self.outF.tell())
self.outF.close() self.outF.close()
if self.setFileTime: if self.setFileTime:
os.utime(self.outFname, (int(time.time()), self.highTS)) os.utime(self.outFname, (int(time.time()), self.highTS))
@ -184,7 +186,10 @@ class BlockDataCopier:
else: else:
self.outFname = os.path.join(self.settings['output'], "blk%05d.dat" % self.outFn) self.outFname = os.path.join(self.settings['output'], "blk%05d.dat" % self.outFn)
print("Output file " + self.outFname) print("Output file " + self.outFname)
self.outF = open(self.outFname, "wb") try:
self.outF = open(self.outFname, "xb+")
except FileExistsError:
self.outF = open(self.outFname, "rb+")
self.outF.write(inhdr) self.outF.write(inhdr)
self.outF.write(blk_hdr) self.outF.write(blk_hdr)