def get_dir_block(): assert (dk.file_name != None), "NO DISK OPEN" sb = sfs.get_superblock() dentry = sb[4] dinode = ih.get_inode(dentry)[2] dblock = ih.get_inode(dinode)[2] data = dk.disk_read(dblock + sb[7]) return data, dblock
def get_inode(nbr): assert (dk.file_name != None), "NO DISK OPEN" sb = sfs.get_superblock() inode_block, inode_start = divmod(nbr, sfs.INODES_PER_BLOCK) assert (inode_block >= 0 and inode_block < sfs.INODE_BLOCK_COUNT * sfs.INODES_PER_BLOCK), "Invalid inode requested: {}".format(nbr) ib = dk.disk_read(inode_block + sb[8]) result = [] for i in range(8): result.append(sfs.bytes_to_int(ib, inode_start + (i * sfs.FIELD_LENGTH))) return result
def write_inode(nbr, data): assert (dk.file_name != None), "NO DISK OPEN" assert (len(data) == 8), "Inode has 8 fields, not {}".format(len(data)) sb = sfs.get_superblock() inode_block, inode_start = divmod(nbr, sfs.INODES_PER_BLOCK) assert (inode_block >= 0 and inode_block < sfs.INODE_BLOCK_COUNT * sfs.INODES_PER_BLOCK), "Invalid inode requested" ib = dk.disk_read(inode_block + sb[8]) for v in data: inode_start = sfs.add_field(ib, v, inode_start) dk.disk_write(inode_block + sb[8], ib) ibp.set_used(nbr)
def print_bitmap(): global ibitmap assert(ibitmap != None), NO_BITMAP cols = 8 superblock = sfs.get_superblock() nbr_inodes = superblock[2] * superblock[3] print("INODE BITMAP: 0 = UNUSED, 1 = USED, 2 = BAD") string = "" for i in range(nbr_inodes): string += str(ibitmap[i]) if i > 0 and i % cols == 0 and i < nbr_inodes - 1: print(string) string = "" else: string += ", "
def print_bitmap(): global bitmap assert (bitmap != None), NO_BITMAP cols = 8 superblock = sfs.get_superblock() nbr_blocks = superblock[1] print("BITMAP: 0 = UNUSED, 1 = USED, 2 = BAD") string = "" for i in range(nbr_blocks): string += str(bitmap[i]) if i > 0 and i % cols == 0 and i < nbr_blocks - 1: print(string) string = "" else: string += ", "
def init(): global ibitmap superblock = sfs.get_superblock() ibitmap_block = superblock[6] ibitmap = dk.disk_read(ibitmap_block) # First inode is bad ibitmap[0] = BAD total_inodes = superblock[2] * superblock[3] for i in range(1, total_inodes): inode = ih.get_inode(i) read_inode(inode, i) # Inodes outside range for i in range(total_inodes, len(ibitmap)): ibitmap[i] = BAD print("Inode bitmap created on block 2") save_to_disk()
def write_dir(inode, name, file=True): assert (dk.file_name != None), "NO DISK OPEN" if file: name = "f{}".format(name) else: name = "d{}".format(name) # Gets dir tuples and replaces one sb = sfs.get_superblock() dirs = get_dirs() for i, v in enumerate(dirs): if v[0] == sfs.INVALID_INODE: dirs[i] = (inode, name) break data, dblock = get_dir_block() field = 0 for i in dirs: field = sfs.add_field(data, i[0], field) field = sfs.add_string(data, i[1], field, 28) dk.disk_write(dblock + sb[7], data)
def init(): global bitmap superblock = sfs.get_superblock() bitmap_block = superblock[5] bitmap = dk.disk_read(bitmap_block) # Always used, meta-blocks datablock_start = superblock[7] for i in range(datablock_start): bitmap[i] = USED # Data blocks total_inodes = superblock[2] * superblock[3] for i in range(total_inodes): inode = ih.get_inode(i) read_inode(inode) # Blocks outside range for i in range(superblock[1], len(bitmap)): bitmap[i] = BAD print("Block bitmap created on block 1") save_to_disk()
def save_to_disk(): global ibitmap assert(ibitmap != None), NO_BITMAP superblock = sfs.get_superblock() ibitmap_block = superblock[6] dk.disk_write(ibitmap_block, ibitmap)
def save_to_disk(): global bitmap assert (bitmap != None), NO_BITMAP superblock = sfs.get_superblock() bitmap_block = superblock[5] dk.disk_write(bitmap_block, bitmap)