Esempio n. 1
0
def cat(inode_no):
    inode = Inode(int(inode_no))
    inode.read()
    content = ""
    blocks = inode.find_content_blocks()
    for i in range(len(blocks)):
        file = open(route + str(blocks[i]) + ".block", "r+b")
        if i == len(blocks) - 1:
            content += bytes_to_string(file.read(inode.size % 1024))
        else:
            content += bytes_to_string(file.read())
        file.close()
    return content
Esempio n. 2
0
 def read(self):
     file = open(route + str(self.get_block()) + ".block", "rb")
     file.seek(self.get_offset())
     self.owner = bytes_to_string(file.read(1))
     self.group = bytes_to_string(file.read(1))
     self.filetype = bytes_to_string(file.read(1))
     self.access_permissions = bytes_to_string(file.read(2))
     self.file_access_time = bytes_to_string(file.read(8))
     self.links = bytes_to_string(file.read(3))
     self.size = bytes_to_string(file.read(4))
     self.table_of_contents = bytes_to_string(file.read(44))
     self.parse_all()
     file.close()
     return True
Esempio n. 3
0
def ls(cwd, param = ""):
    content = [[], []]
    stored = ""
    for block in cwd.find_content_blocks():
        file = open(route + str(block) + ".block", "rb")
        offset = 0
        while offset < 1024:
            for line in file.readlines():
                line = bytes_to_string(line)
                if offset == 0:
                    offset += len(line)
                    line = stored + line
                    stored = ""
                else:
                    offset += len(line)
                if offset == 1024:
                   stored = line
                else:
                    content[0].append(string_to_int(line[0:4]))
                    content[1].append(line[4:])
                    offset +=1
    if content[0][-1] == 0:
        content[0].pop()
        if len(content[0]) != len(content[1]):
            content[1].pop()
    returnable=[[],[]]
    if param != 'd':
        for i in range(len(content[0])):
            if int(content[0][i]) != 0:
                returnable[0].append(content[0][i])
                returnable[1].append(content[1][i])
    else:
        return content
    content = returnable
    if param  == "-l":
        content.append([])
        c_inode = Inode(1)
        for i in range(len(content[0])):
            c_inode.id = (int(content[0][i]))
            c_inode.read()
            content[2].append(c_inode.size)
        return content
    return returnable
Esempio n. 4
0
def find_block(no_block, array):
    if no_block < 8:
        return array[no_block]
    elif no_block < 264:
        file = open(route+array[8]+".block", "rb")
        file.seek((no_block - 8) *4)
        data_block  = string_to_int(bytes_to_string(file.read(4)))
        file.close()
        return data_block
    elif no_block < 65800:
        offset_1 = (no_block - 264)//256
        offset_2 = (no_block - 264)%256
        file = open(route + array[9] + ".block", "rb")
        file.seek(offset_1 * 4)
        data_block = string_to_int(bytes_to_string(file.read(4)))
        file.close()
        file = open(route +data_block + ".block", "rb")
        file.seek(offset_2 * 4)
        data_block = string_to_int(bytes_to_string(file.read(4)))
        file.close()
        return data_block

    else:
        offset_1 = (no_block - 65800)//65536
        offset_2 = (no_block - 65800)%65536
        offset_3 = (no_block - 65800)%256
        file = open(route + array[9] + ".block", "rb")
        file.seek(offset_1 * 4)
        data_block = string_to_int(bytes_to_string(file.read(4)))
        file.close()
        file = open(route +data_block + ".block", "rb")
        file.seek(offset_2 * 4)
        data_block = string_to_int(bytes_to_string(file.read(4)))
        file.close()
        file = open(route +data_block + ".block", "rb")
        file.seek(offset_3 * 4)
        data_block = string_to_int(bytes_to_string(file.read(4)))
        file.close()
        return data_block
Esempio n. 5
0
def add_file_to_dir(i_dir, i_file,name):
    i_dir = Inode(i_dir)
    i_dir.read()
    i_file = Inode(i_file)
    i_file.read()
    current_size = i_dir.size
    future_size = current_size +4 + 1 + len(name)
    block = future_size//1024
    if (future_size //1024 == current_size //1024):
        offset = current_size % 1024
        writing_block = find_block(block, i_dir.table_of_contents)
        file = open(route + str(writing_block) + ".block", "r+b")
        file.seek(offset)
        write_str_bin(file, int_to_string(i_file.id, 4) + name + "\n")
        file.close()
    else:
        offset = current_size % 1024
        writing_block = find_block(block, i_dir.table_of_contents)
        file = open(route + str(writing_block) + ".block", "r+b")
        file.seek(offset)
        string1 = int_to_string(i_file.id, 4) + name + "\n"[0:1024 - offset]
        string2 = int_to_string(i_file.id, 4) + name + "\n"[1024 - offset:]
        write_str_bin(file, string1)
        file.close()
        LBL = B.super_block(2)
        LBL.load()
        if block < 8:
            i_dir.table_of_contents[block] =  LBL.free_block()
            last_block = i_dir.table_of_contents[block]
        if block < 264:
            offset_1 = (block - 8) % 256
            if  offset_1== 0:
                i_dir.table_of_contents[8] = LBL.free_block()
            file_1 = open(route + str(i_dir.table_of_contents[8]) + ".block", "r+b")
            file_1.seek(offset_1 *4)
            new_block = LBL.free_block()
            write_str_bin(file_1,int_to_string(new_block,4))
            last_block = new_block
            file_1.close()
        if block < 65800:
            offset_1 = (block - 264) % 65536
            offset_R1 = (block - 264) // 256
            offset_2 = (block - 264) % 256
            if  offset_1== 0:
                i_dir.table_of_contents[9] = LBL.free_block()
            file_1 = open(route + str(i_dir.table_of_contents[9]) + ".block", "r+b")
            file_1.seek(offset_R1 *4)
            if offset_2 == 0:
                new_block = LBL.free_block()
                write_str_bin(file_1, int_to_string(new_block, 4))
            else:
                new_block = string_to_int(bytes_to_string(file_1.read(4)))
            file_1.close()
            file_2 = open(route + str(new_block) + ".block", "r+b")
            file_2.seek(offset_2 *4)
            new_block = LBL.free_block()
            write_str_bin(file_2, int_to_string(new_block, 4))
            last_block = new_block
            file_2.close()
        else:
            offset_1 = (block - 65800) // 65536
            offset_2 = (block - 65800) // 256
            offset_3 = (block - 65800) % 256
            if  block == 65800:
                i_dir.table_of_contents[10] = LBL.free_block()
            file_1 = open(route + str(i_dir.table_of_contents[10]) + ".block", "r+b")
            file_1.seek(offset_1 *4)
            if offset_2 == 0:
                new_block = LBL.free_block()
                write_str_bin(file_1, int_to_string(new_block, 4))
            else:
                new_block = string_to_int(bytes_to_string(file_1.read(4)))
            file_1.close()
            file_2 = open(route + str(new_block) + ".block", "r+b")
            file_2.seek(offset_2 *4)
            if offset_3 == 0:
                new_block = LBL.free_block()
                write_str_bin(file_2, int_to_string(new_block, 4))
            else:
                new_block = string_to_int(bytes_to_string(file_2.read(4)))
            file_2.close()
            file_3 = open(route + str(new_block) + ".block", "r+b")
            file_3.seek(offset_3 *4)
            new_block = LBL.free_block()
            write_str_bin(file_3, int_to_string(new_block, 4))
            last_block = new_block
            file_3.close()
        file = open(route + str(last_block) + ".block", "r+b")
        write_str_bin(file, string2)
        file.close()
    i_dir.size = future_size
    i_dir.binarize_all()
    i_dir.write()
    return True
Esempio n. 6
0
    def find_content_blocks(self):
        queue = [[], [], [], []]
        queue2 = []
        blocks = ceil(self.size / 1024)
        block = 0
        while block < blocks:
            if block < 8:
                queue[0].append(self.table_of_contents[block])
                queue2.append(queue[0][-1])
                block += 1
            elif len(queue[1]) == 0:
                queue[0].append(self.table_of_contents[block])
                offset = 0
                file = open(route + str(self.table_of_contents[8]) + ".block",
                            "rb")
                while block < blocks and offset < 1024:
                    offset += 4
                    queue[1].append(
                        string_to_int(bytes_to_string(file.read(4))))
                    queue2.append(queue[1][-1])
                    block += 1
                file.close()

            elif len(queue[2]) == 0:
                queue[0].append(self.table_of_contents[9])
                file_1 = open(
                    route + str(self.table_of_contents[9]) + ".block", "rb")
                offset_1 = 0
                while block < blocks and offset_1 < 1024:
                    offset_1 += 4
                    queue[1].append(
                        string_to_int(bytes_to_string(file_1.read(4))))
                    offset_2 = 0
                    file_2 = open(route + str(queue[1][-1]) + ".block", "rb")
                    while block < blocks and offset_2 < 1024:
                        offset_2 += 4
                        queue[2].append(
                            string_to_int(bytes_to_string(file_2.read(4))))
                        queue2.append(queue[2][-1])
                        block += 1
                    file_2.close()
                file_1.close()

            else:
                queue[0].append(self.table_of_contents[10])
                file_1 = open(
                    route + str(self.table_of_contents[10]) + ".block", "rb")
                offset_1 = 0
                while block < blocks and offset_1 < 1024:
                    offset_1 += 4
                    queue[1].append(
                        string_to_int(bytes_to_string(file_1.read(4))))
                    offset_2 = 0
                    file_2 = open(route + str(queue[1][-1]) + ".block", "rb")
                    while block < blocks and offset_2 < 1024:
                        offset_2 += 4
                        queue[2].append(
                            string_to_int(bytes_to_string(file_2.read(4))))
                        file_3 = open(route + str(queue[2][-1]) + ".block",
                                      "rb")
                        offset_3 = 0
                        while block < blocks and offset_3 < 1024:
                            offset_3 += 4
                            queue[3].append(
                                string_to_int(bytes_to_string(file_3.read(4))))
                            queue2.append(queue[3][-1])
                            block += 1
                        file_3.close()
                    file_2.close()
                file_1.close()
        returnable = queue2
        return returnable
Esempio n. 7
0
 def read_info(self): #Reads binary from file and updates self.info with string formatted info
     file  =  open(self.filepath, "rb")
     self.info = bytes_to_string(file.read())
     self.info += chr(0) * (1024 - len(self.info))
     file.close()
     return True