def read(self): """read data blocks""" self.data_blks = [] want_seq_num = 1 total_size = 0 is_ffs = self.volume.is_ffs byte_size = self.block.byte_size data = "" for blk in self.data_blk_nums: if is_ffs: # ffs has raw data blocks dat_blk = self.volume.blkdev.read_block(blk) total_size += len(dat_blk) # shrink last read id necessary if total_size > byte_size: shrink = total_size - byte_size dat_blk = dat_blk[:-shrink-1] total_size = byte_size data += dat_blk else: # ofs dat_blk = FileDataBlock(self.block.blkdev, blk) dat_blk.read() if not dat_blk.valid: raise FSError(INVALID_FILE_DATA_BLOCK, block=dat_blk) # check sequence number if dat_blk.seq_num != want_seq_num: raise FSError(INVALID_SEQ_NUM, block=dat_blk, extra="got=%d wanted=%d" % (dat_blk.seq_num, want_seq_num)) # store data blocks self.data_blks.append(dat_blk) total_size += dat_blk.data_size data += dat_blk.get_block_data() want_seq_num += 1 # store full contents of file self.data = data
def write(self): self.data_blks = [] off = 0 left = self.data_size blk_idx = 0 bs = self.get_data_block_contents_bytes() is_ffs = self.volume.is_ffs while off < self.data_size: # number of data block blk_num = self.data_blk_nums[blk_idx] # extract file data size = left if size > bs: size = bs d = self.data[off:off+size] if is_ffs: # pad block if size < bs: d += '\0' * (bs-size) # write raw block data in FFS self.blkdev.write_block(blk_num, d) else: # old FS: create and write data block fdb = FileDataBlock(self.blkdev, blk_num) if blk_idx == self.num_data_blks - 1: next_data = 0 else: next_data = self.data_blk_nums[blk_idx+1] fdb.create(self.block.blk_num, blk_idx+1, d, next_data) fdb.write() self.data_blks.append(fdb) blk_idx += 1 off += bs left -= bs