Пример #1
0
    def mkdir(self, path, mode):
        self.files[path] = dict(st_mode=(S_IFDIR | mode),
                                st_nlink=2,
                                st_size=0,
                                st_ctime=time(),
                                st_mtime=time(),
                                st_atime=time())
        name = path
        mode = self.files[path].get('st_mode')
        ctime = self.files[path].get('st_ctime')
        mtime = self.files[path].get('st_mtime')
        atime = self.files[path].get('st_atime')
        nlink = self.files[path].get('st_nlink')
        size = self.files[path].get('st_size')
        uid = os.getuid()
        gid = os.getgid()
        self.chown(path, uid, gid)
        # get free block for the new created file to save the metadata.
        num_array = Format.get_free_block(Format, 1)
        block_num = num_array[0]
        # set up the location in the disk
        inode_data = Format.set_inode(Format, name, mode, ctime, mtime, atime,
                                      nlink, uid, gid, size, block_num)
        disktools.write_block(block_num, inode_data)
        # update the free block bitmap
        Format.update_bit_map(Format, num_array)

        # find the parent path and add the nlink by 1.
        parent_path = Format.find_parent_path(Format, path)
        self.files[parent_path]['st_nlink'] += 1
        Format.update_nlink(Format, parent_path, 1)
Пример #2
0
    def rmdir(self, path):
        # with multiple level support, need to raise ENOTEMPTY if contains any files
        length = len(path)

        flag = True
        for x in self.files:
            # if the first part is equal to path and the total length is longer than path
            # which mean it inside the path.
            if (x[0:length] == path) & (len(x) > length):
                flag = False

        if flag:
            self.files.pop(path)
            Format.clear_metadata_block(Format, path)
        else:
            raise ENOTEMPTY

        # find the parent path and reduce the nlink by 1.
        parent_path = Format.find_parent_path(Format, path)
        self.files[parent_path]['st_nlink'] -= 1
        Format.update_nlink(Format, parent_path, -1)