Ejemplo n.º 1
0
def mkdir(name, cwd):
    LIL = B.super_block(1)
    LIL.load()
    LBL = B.super_block(2)
    LBL.load()
    dir = Inode(id = LIL.free_inode(), filetype='d')
    dir.read()
    dir.filetype = 'd'
    dir.size = 13
    dir.table_of_contents[0] = LBL.free_block()
    file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(dir.table_of_contents[0]) + ".block", "r+b")
    write_str_bin(file,int_to_string(dir.id,4)+'.'+'\n')
    write_str_bin(file,int_to_string(cwd.id,4)+'..'+'\n')
    dir.binarize_all()
    dir.write()
    add_file_to_dir(cwd.id,dir.id,name)
Ejemplo n.º 2
0
def mk_file(user, file_content, ref, parent_inode):
    LIL = B.super_block(1)
    LBL = B.super_block(2)
    LIL.load()
    LBL.load()
    no_inodo = LIL.free_inode()
    inodo = Inode(id=no_inodo)
    inodo.read()
    inodo.filetype = 'x'
    inodo.owner = user
    inodo.size = len(file_content)
    no_blocks = math.ceil(len(file_content) / 1024)
    block = 0
    while block < no_blocks:
        if block < 8:
            inodo.table_of_contents[block] = LBL.free_block()
            file = open(
                "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                str(inodo.table_of_contents[block]) + ".block", "r+b")
            if (block + 1) * 1024 < len(file_content):
                write_str_bin(file,
                              file_content[block * 1024:(block + 1) * 1024])
            else:
                write_str_bin(file, file_content[block * 1024:])
            file.close()
            block += 1
        elif block < 264:
            inodo.table_of_contents[8] = LBL.free_block()
            offset = 0
            file_1 = open(
                "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                str(inodo.table_of_contents[8]) + ".block", "r+b")
            while offset < 1024 and block < no_blocks:
                offset += 4
                no_block = LBL.free_block()
                write_str_bin(file_1, int_to_string(no_block, 4))
                file_2 = open(
                    "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                    str(no_block) + ".block", "r+b")
                if (block + 1) * 1024 < len(file_content):
                    write_str_bin(
                        file_2, file_content[block * 1024:(block + 1) * 1024])
                else:
                    write_str_bin(file_2, file_content[block * 1024:])
                block += 1
                file_2.close()
            file_1.close()
        elif block < 65800:
            inodo.table_of_contents[9] = LBL.free_block()
            offset_1 = 0
            file_1 = open(
                "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                str(inodo.table_of_contents[9]) + ".block", "r+b")
            while offset_1 < 1024 and block < no_blocks:
                offset_1 += 4
                no_block = LBL.free_block()
                write_str_bin(file_1, int_to_string(no_block, 4))
                file_2 = open(
                    "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                    str(no_block) + ".block", "r+b")
                offset_2 = 0
                while offset_2 < 1024 and block < no_blocks:
                    offset_2 += 4
                    no_block = LBL.free_block()
                    write_str_bin(file_2, int_to_string(no_block, 4))
                    file_3 = open(
                        "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                        str(no_block) + ".block", "r+b")
                    if (block + 1) * 1024 < len(file_content):
                        write_str_bin(
                            file_3,
                            file_content[block * 1024:(block + 1) * 1024])
                    else:
                        write_str_bin(file_3, file_content[block * 1024:])
                    block += 1
                    file_3.close()
                file_2.close()
            file_1.close()
        else:
            inodo.table_of_contents[10] = LBL.free_block()
            offset_1 = 0
            file_1 = open(
                "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                str(inodo.table_of_contents[10]) + ".block", "r+b")
            while offset_1 < 1024 and block < no_blocks:
                offset_1 += 4
                no_block = LBL.free_block()
                write_str_bin(file_1, int_to_string(no_block, 4))
                file_2 = open(
                    "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                    str(no_block) + ".block", "r+b")
                offset_2 = 0
                while offset_2 < 1024 and block < no_blocks:
                    offset_2 += 4
                    no_block = LBL.free_block()
                    write_str_bin(file_2, int_to_string(no_block, 4))
                    file_3 = open(
                        "/home/zikin/Documents/Final_project_OS/hard_drive/" +
                        str(no_block) + ".block", "r+b")
                    offset_3 = 0
                    while offset_3 < 1024 and block < no_blocks:
                        offset_3 += 4
                        no_block = LBL.free_block()
                        write_str_bin(file_3, int_to_string(no_block, 4))
                        file_4 = open(
                            "/home/zikin/Documents/Final_project_OS/hard_drive/"
                            + str(no_block) + ".block", "r+b")
                        if (block + 1) * 1024 < len(file_content):
                            write_str_bin(
                                file_4,
                                file_content[block * 1024:(block + 1) * 1024])
                        else:
                            write_str_bin(file_4, file_content[block * 1024:])
                        block += 1
                        file_4.close()
                    file_3.close()
                file_2.close()
            file_1.close()
    inodo.binarize_all()
    inodo.write()
    add_file_to_dir(i_dir=parent_inode, i_file=inodo.id, name=ref)
    return True
Ejemplo n.º 3
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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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("/home/zikin/Documents/Final_project_OS/hard_drive/" + 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