Пример #1
0
 def update_info(self): #Updates self.info to be up to date with LBL or LIL
     new_info = ""
     if self.id == 1:
         for num in self.LIL:
             new_info += int_to_string(num, 4)
     else:
         for num in self.LBL:
             new_info += int_to_string(num, 4)
     self.info = new_info + chr(0) * (1024-len(new_info))
     return True
Пример #2
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(route + 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)
Пример #3
0
 def change_rem_inode(self,inode_no):
     file  =  open(self.filepath, "r+b")
     self.LIL[0] = inode_no
     self.clear_inode(inode_no)
     write_str_bin(file= file, string=int_to_string(inode_no,4))
     file.close()
     return True
Пример #4
0
def mkroot():
    LIL = B.super_block(1)
    LIL.load()
    LBL = B.super_block(2)
    LBL.load()
    dir = Inode(id = 2)
    dir.read()
    dir.filetype = 'd'
    dir.size = 13
    dir.table_of_contents[0] = LBL.free_block()
    file = open(route + str(dir.table_of_contents[0]) + ".block", "r+b")
    write_str_bin(file,int_to_string(2,4)+'.'+'\n')
    write_str_bin(file,int_to_string(2,4)+'..'+'\n')
    dir.binarize_all()
    dir.write()
    mkdir('home', Inode(2))
    return
Пример #5
0
 def append(self, num):
     if self.LBL != None:
         file = open(self.filepath, "r+b")
         file.seek(len(self.LBL)*4, 0)
         write_str_bin(file = file, string= int_to_string(num, 4))
         self.LBL.append(num)
         self.update_info()
         file.close()
         return True
     else:
         file = open(self.filepath, "r+b")
         file.seek(len(self.LIL)*4, 0)
         self.clear_inode(num)
         write_str_bin(file=file, string=int_to_string(num, 4))
         self.LIL.append(num)
         self.update_info()
         return True
Пример #6
0
 def pop(self):
     if self.LBL != None:
         file = open(self.filepath, "r+b")
         num = self.LBL.pop() ##checar
         file.seek(len(self.LBL)*4, 0)
         write_str_bin(file = file, string= int_to_string(0, 4))
         self.update_info()
         file.close()
         return num
     else:
         file = open(self.filepath, "r+b")
         num = self.LIL.pop()
         file.seek(len(self.LIL)*4, 0)
         write_str_bin(file = file, string= int_to_string(0, 4))
         self.update_info()
         file.close()
         return num
Пример #7
0
def set_inodes_list(dir_path=route):
    filepath = dir_path + "/1.block"
    file = open(filepath, "wb")
    for i in range(264, 8, -1):
        for char in int_to_string(num=i, bytes=4):
            file.write(bytes([ord(char)]))
    file.close()
    return True
Пример #8
0
def set_block_list(dir_path=route):
    filepath = dir_path + "/2.block"
    file = open(filepath, "wb")
    for i in range(1003 + 255, 1003 - 1, -1):
        for char in int_to_string(num=i, bytes=4):
            file.write(bytes([ord(char)]))
    file.close()
    for block in range(1003 + 256, 1000000, 256):
        file = open(dir_path + "/" + str(block - 1) + ".block", "wb")
        for i in range(block + 255, block - 1, -1):
            print(i)
            if i <= 1000000:
                for char in int_to_string(num=i, bytes=4):
                    file.write(bytes([ord(char)]))
            else:
                for char in int_to_string(num=0, bytes=4):
                    file.write(bytes([ord(char)]))
        file.close()
    return True
Пример #9
0
def set_inodes():
    x = str(datetime.datetime.now())
    x = x[:10]
    x = x.replace("-", "")
    for i in range(1, 9):
        inode = Inode(owner=chr(1),
                      group=chr(1),
                      filetype="d",
                      access_permissions=chr(255) + chr(128),
                      file_access_time=x,
                      links=int_to_string(1, 3),
                      size=int_to_string(0, 4),
                      table_of_contents=int_to_string(0, 4) * 11,
                      id=i)
        inode.write()
        del inode
    for i in range(9, 16001):
        inode = Inode(owner=chr(0),
                      group=chr(0),
                      filetype="0",
                      access_permissions=chr(0) * 2,
                      file_access_time=x,
                      links=int_to_string(0, 3),
                      size=int_to_string(0, 4),
                      table_of_contents=int_to_string(0, 4) * 11,
                      id=i)
        inode.write()
        del inode

    return True
Пример #10
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
Пример #11
0
 def binarize_toc(self):
     string = ""
     for number in self.table_of_contents:
         string += int_to_string(number, 4)
     self.table_of_contents = string
     return True
Пример #12
0
 def binarize_size(self):
     self.size = int_to_string(self.size, 4)
     return True
Пример #13
0
 def binarize_links(self):
     self.links = int_to_string(self.links, 3)
     return True
Пример #14
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(route + 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(route + 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(route + 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(route + 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(route + 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(route + 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(route + 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(route + 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(route + 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(route + 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