| 220 | self.writeBlock(extent.inhdr, extent.blkhdr, rawblock) |
| 221 | |
| 222 | def run(self): |
| 223 | while self.blkCountOut < len(self.blkindex): |
| 224 | if not self.inF: |
| 225 | fname = self.inFileName(self.inFn) |
| 226 | print("Input file " + fname) |
| 227 | try: |
| 228 | self.inF = open(fname, "rb") |
| 229 | except IOError: |
| 230 | print("Premature end of block data") |
| 231 | return |
| 232 | |
| 233 | inhdr = self.inF.read(8) |
| 234 | if (not inhdr or (inhdr[0] == "\0")): |
| 235 | self.inF.close() |
| 236 | self.inF = None |
| 237 | self.inFn = self.inFn + 1 |
| 238 | continue |
| 239 | |
| 240 | inMagic = inhdr[:4] |
| 241 | if (inMagic != self.settings['netmagic']): |
| 242 | # Seek backwards 7 bytes (skipping the first byte in the previous search) |
| 243 | # and continue searching from the new position if the magic bytes are not |
| 244 | # found. |
| 245 | self.inF.seek(-7, os.SEEK_CUR) |
| 246 | continue |
| 247 | inLenLE = inhdr[4:] |
| 248 | su = struct.unpack("<I", inLenLE) |
| 249 | inLen = su[0] - 80 # length without header |
| 250 | blk_hdr = self.inF.read(80) |
| 251 | inExtent = BlockExtent(self.inFn, self.inF.tell(), inhdr, blk_hdr, inLen) |
| 252 | |
| 253 | self.hash_str = calc_hash_str(blk_hdr) |
| 254 | if not self.hash_str in blkmap: |
| 255 | # Because blocks can be written to files out-of-order as of 0.10, the script |
| 256 | # may encounter blocks it doesn't know about. Treat as debug output. |
| 257 | if settings['debug_output'] == 'true': |
| 258 | print("Skipping unknown block " + self.hash_str) |
| 259 | self.inF.seek(inLen, os.SEEK_CUR) |
| 260 | continue |
| 261 | |
| 262 | blkHeight = self.blkmap[self.hash_str] |
| 263 | self.blkCountIn += 1 |
| 264 | |
| 265 | if self.blkCountOut == blkHeight: |
| 266 | # If in-order block, just copy |
| 267 | rawblock = self.inF.read(inLen) |
| 268 | self.writeBlock(inhdr, blk_hdr, rawblock) |
| 269 | |
| 270 | # See if we can catch up to prior out-of-order blocks |
| 271 | while self.blkCountOut in self.blockExtents: |
| 272 | self.copyOneBlock() |
| 273 | |
| 274 | else: # If out-of-order, skip over block data for now |
| 275 | self.blockExtents[blkHeight] = inExtent |
| 276 | if self.outOfOrderSize < self.settings['out_of_order_cache_sz']: |
| 277 | # If there is space in the cache, read the data |
| 278 | # Reading the data in file sequence instead of seeking and fetching it later is preferred, |
| 279 | # but we don't want to fill up memory |