Пример #1
0
Файл: file.c Проект: bmiro/EmoFS
/** Obte la informacio d'un inode. 
 * @inode: nombre del fitxer.
 * @stat: el punter de sortida de les dades.
 * @return: 0 si exit.
 */
int stat_file(int inode, emofs_inode_stat *stat) {
	emofs_inode tmp;
	int error;
	
	error = read_inode(inode, &tmp);
	if (error > -1) {
		memcpy(stat, &tmp, sizeof(emofs_inode_stat));
	}
	return error;
}
Пример #2
0
//handling mkdir request
void mkdir_handler(Msg *msg, int sender_pid)
{
    char pathname[MAXPATHNAMELEN];
    CopyFrom(sender_pid,pathname,msg->ptr1,msg->num1+1);
    char* dir_name = pathname+msg->num1;
    int direct_len = msg->num1;
    while ((*dir_name)!='/' && dir_name!=pathname) {
        dir_name--;
        direct_len--;
    }
    if ((*dir_name)=='/') {
        direct_len++;
        dir_name++;
    }
    if (strlen(dir_name)==0) {
        perror("invalid pathname when creating file!");
        msg->type = ERROR;
        return;
    }
    int direct_inum = path_to_inum(pathname,direct_len,msg->num2,0);
    if (direct_inum<=0) {
        perror("invalid pathname when creating file!");
        msg->type = ERROR;
        return;
    }
    int new_inum = check_dir(direct_inum,dir_name);
    if (new_inum<0) {
        perror("invalid pathname when creating file!");
        msg->type = ERROR;
        return;
    }

    //exist same file name in the directory
    else if (new_inum>0) {
        perror("exist a directory with same name");
        msg->type = ERROR;
        return;
    }
    else if (new_inum==0) {
        new_inum = alloc_inode(INODE_DIRECTORY,direct_inum);
        struct dir_entry *d = empty_dir(direct_inum);
        if (d==NULL) {
            perror("no empty space for new directory");
            msg->type = ERROR;
            return;
        }
        d->inum = new_inum;
        memcpy(d->name,dir_name,strlen(dir_name));
        inode_cache *n = read_inode(direct_inum);
        n->data.nlink++;
        n->data.size+=sizeof(struct dir_entry);
        n->dirty = 1;
        msg->num1 = 0;
    }
}
Пример #3
0
ssize_t ext2_read(open_file_descriptor * ofd, void * buf, size_t size) {
	if ((ofd->flags & O_ACCMODE) == O_WRONLY) {
		return 0;
	}
	int inode = ofd->inode->i_ino;
	if (inode >= 0) {
		ext2_fs_instance_t *instance = (ext2_fs_instance_t*) ofd->fs_instance;
		off_t offset = ofd->current_octet;
		int count = 0;
		struct ext2_inode *einode = read_inode(instance, inode);
		
		if (offset >= einode->i_size) {
			return 0;
		}

		if (size + offset > einode->i_size) {
			size = einode->i_size - offset;
		}
		
		int n_blk = 0;
		while (offset >= (unsigned int)(1024 << instance->superblock.s_log_block_size)) {
			n_blk++;
			offset -= (1024 << instance->superblock.s_log_block_size);
		}
		//int n_blk = offset / (1024 << instance->superblock.s_log_block_size);
		//offset %= (1024 << instance->superblock.s_log_block_size);
		//
		while (size > 0) {
			int addr = addr_inode_data2(instance, einode, n_blk);
			if (addr == 0) break;
			n_blk++;

			size_t size2 = (1024 << instance->superblock.s_log_block_size) - offset;
			if (size2 > size) {
				size2 = size;
			}

			instance->read_data(instance->super.device, ((char*)buf) + count, size2, addr + offset);

			size -= size2;
			count += size2;
			offset = 0;
		}

		// Commenté pour des raisons évidentes de perf.
		//	struct timeval tv;
		//	gettimeofday(&tv, NULL);
		//	einode.i_atime = tv.tv_sec;
		//	write_inode(inode, &einode);
		ofd->current_octet += count;
		return count;
	} else {
		return inode;
	}
}
/**
 * @brief 输出文件 
 * @param[in] absolute_path 文件的绝对路径
 * @param[in] all           是否输出全部文件,包括隐藏文件,以及.和..
 * @param[in] almost_all    是否“几乎全部”输出,包括隐藏文件,不包括.和..
 * @param[in] long_list     是否按长列表方式输出
 */
void output_files( const char * absolute_path, bool all, bool almost_all, bool long_list )  // ls
{
	struct ext2_group_desc group;
	struct ext2_inode inode;
	void * block = NULL;

	int inode_no = get_file_inode_no(absolute_path);
	if ( inode_no == -1 )
	{
		fprintf(stderr, "Current directory does not exist.\n");
		return;
	}
	read_group_descriptor(&group);
	read_inode(&group, &inode, inode_no);

	if (S_ISDIR(inode.i_mode))
	{
		struct ext2_dir_entry_2 * entry = NULL;
		unsigned int size = 0;
		if ((block = malloc(block_size)) == NULL)
		{
			fprintf(stderr, "Insufficient memory.\n");
			exit(EXIT_FAILURE);
		}

		lseek(fd_ext2, BLOCK_OFFSET(inode.i_block[0]), SEEK_SET);
		read(fd_ext2, block, block_size);                // read block from disk

		entry = (struct ext2_dir_entry_2 *) block;  // first entry in the directory
		// Notice that the list may be terminated with a NULL entry (entry->inode == NULL)
		while((size < inode.i_size) && entry->inode)
		{
			if ( entry->name[0] == '.' )
			{
				if ( all )
					output_entry(entry, long_list, entry->inode);
				else if ( almost_all )
				{
					if ( strcmp(entry->name, ".") && strcmp(entry->name, "..") )
						output_entry(entry, long_list, entry->inode);
				}
			}
			else
				output_entry(entry, long_list, entry->inode);
			entry = (struct ext2_dir_entry_2 *)((void *)entry + entry->rec_len);
			size += entry->rec_len;
		}
		puts("");

		free(block);
		block = entry = NULL;
	}
	else
		fprintf(stderr, "Current directory does not exist.\n");
}
Пример #5
0
void ext2_open_inode(uint32_t inode_num, Ext2_file *file)
{
	read_inode(&file->inode, inode_num);
	file->pos            = 0;
	file->block_index    = 0;
	file->buf            = kmalloc(block_size);
	file->curr_block_pos = 0;

	// Read in the first block immediately
	read_block(file->inode.dbp[0], file->buf);
}
Пример #6
0
static int msfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, fuse_file_info *fi) {
	reset_error();
	printf("msfs_readdir(%s) inode %lu\n", path, fi->fh);
	inode_t entry_inode, inode = read_inode((addr_t)fi->fh);
	if(msfs_error != 0) return msfs_error;

	bump_atime(&inode);

	addr_t addr = 0;
	file_entry_t * entry;

	entry = next_file_entry(&inode, &addr);

	while(entry != NULL) {
		entry_inode = read_inode(entry->address);
		filler(buf, entry->name, &entry_inode.attributes, 0);
		free_file_entry(entry);
		entry = next_file_entry(&inode, &addr);
	}
	return msfs_error;
}
Пример #7
0
static int msfs_read(const char *path, char *buf, size_t size, off_t offset, fuse_file_info *fi) {
	printf("msfs_read(%s)\n", path);
	reset_error();
	inode_t inode = read_inode((addr_t)fi->fh);
	if(msfs_error != 0) return msfs_error;

	bump_atime(&inode);

	int bytes = read_inode_data(&inode, offset, size, buf);
	if(msfs_error != 0) return msfs_error;
	return bytes;
}
Пример #8
0
int ext2_symlink(inode_t *dir, dentry_t *dentry, const char* target) {
	int ret = ext2_mknod(dir, dentry, 0777 | EXT2_S_IFLNK, 0);
	if (ret) {
		return ret;
	}

	int inode = dentry->d_inode->i_ino;
	ext2_fs_instance_t* instance = (ext2_fs_instance_t*)dir->i_instance;
	struct ext2_inode *einode = read_inode(instance, inode);
	ext2_write2inode(instance, inode, einode, 0, target, strlen(target));
	return 0;
}
Пример #9
0
Файл: lfs.c Проект: kitalda/lfs
ssize_t lfs_write(int filedes, const void *buf, size_t count) {
	int err, write = 0;
	struct inode* node;

	unsigned int* block = malloc(BLOCK_SIZE);
	if (!block) {
		return -ENOMEM;
	} else {
		node = malloc(INODE_SIZE);
		if (!node) {
			err = -ENOMEM;
		} else {
			err = read_inode(log_system, filedes, node);
			if (node->file_size % BLOCK_SIZE == 0) {
				/*All blocks started on are filled*/
				write = count;
				if (node->number_of_blocks >= BLOCKS_PR_INODE) {
					return -ENOMEM;
				}
				if (count > BLOCK_SIZE) {
					write = BLOCK_SIZE;
				}
				memcpy(block, buf, write);
				node->blocks_changed[node->number_of_blocks] = 1;
				node->number_of_blocks++;
				node->file_size += write;
				err = buff_write_inode_with_changes(log_system, node, block);
				if (!err) {
					int start = (node->file_size % BLOCK_SIZE);
					write = BLOCK_SIZE - start;
					if (write > count) {
						write = count;
					}
					err = read_block(log_system, block,
							node->block_placements[node->number_of_blocks - 1]);
					if (!err) {
						node->blocks_changed[node->number_of_blocks - 1] = 1;
						memcpy(&block[start], buf, write);
						node->file_size += write;
						err = buff_write_inode_with_changes(log_system, node, block);
					}
				}
			}
			free(node);
		}
		free(block);
	}
	if (err < 0) {
		return err;
	}
	return write;
}
Пример #10
0
void
close_ifile(file_desc_t *fd)
{
    struct inode_s inode;
    
    /* if the buffer is dirty, flush the file */
    flush_ifile(fd);
    
    /* update the inode information (size) */
    read_inode(fd->fds_inumber, &inode);
    inode.ind_size = fd->fds_size;
    write_inode(fd->fds_inumber, &inode);
}
Пример #11
0
int ext2_mknod(inode_t *dir, dentry_t *dentry, mode_t mode, dev_t dev) {
        int ino =  mknod_inode((ext2_fs_instance_t*)dir->i_instance, dir->i_ino, dentry->d_name, mode, dev);
        if (ino == 0) {
                return -ENOTDIR; //XXX
        }
        struct ext2_inode *einode = read_inode((ext2_fs_instance_t*)dir->i_instance, ino);

        dentry->d_inode = (inode_t*)kmalloc(sizeof(inode_t),0);
        ext2inode_2_inode(dentry->d_inode, dir->i_instance, ino, einode);
        dentry->d_inode->i_count = 0;

        return 0;
}
Пример #12
0
struct inode *__iget(register struct super_block *sb,
		     ino_t inr /*,int crossmntp */ )
{
    register struct inode *inode;

    debug3("iget called(%x, %d, %d)\n", sb, inr, 0 /* crossmntp */ );
    if (!sb)
	panic("VFS: iget with sb==NULL");

  repeat:
    do {
	inode = first_inode;
	do {
	    if (inode->i_dev == sb->s_dev && inode->i_ino == inr)
		goto found_it;
	} while ((inode = inode->i_prev) != first_inode);
	debug("iget: getting an empty inode...\n");
    } while (!(inode = get_empty_inode()));
    debug1("iget: got one... (%x)!\n", empty);

    inode->i_sb = sb;
    inode->i_dev = sb->s_dev;
    inode->i_flags = ((unsigned short int) sb->s_flags);
    inode->i_ino = inr;
    put_last_lru(inode);
    debug("iget: Reading inode\n");
    read_inode(inode);
    debug("iget: Read it\n");
    goto return_it;

  found_it:
    if (!inode->i_count)
	nr_free_inodes--;
    inode->i_count++;
    wait_on_inode(inode);
    if (inode->i_dev != sb->s_dev || inode->i_ino != inr) {
	printk("Whee.. inode changed from under us. Tell _.\n");
	iput(inode);
	goto repeat;
    }
    if ( /* crossmntp && */ inode->i_mount) {
	struct inode *tmp = inode->i_mount;
	tmp->i_count++;
	iput(inode);
	inode = tmp;
	wait_on_inode(inode);
    }

  return_it:
    return inode;
}
Пример #13
0
int main(int argc, char **argv)
{
	FILE *boot, *img;
	char boot_buf[512];
	short buf2[512];
	short blocks[256];
	struct inode inode;
	int i = 0;

	if (argc != 3) {
		fprintf(stderr, "install-boot boot.bin dev_name\n");
		exit(0);
	}

	boot = fopen(argv[1], "rb");
	if (boot == NULL) {
		fprintf(stderr, "File Open Error:%s", argv[3]);
		exit(0);
	}

	img = fopen(argv[2], "r+");
	if (img == NULL) {
		fprintf(stderr, "File Open Error:%s", argv[1]);
		exit(0);
	}

	fread(boot_buf, 512, 1, boot);
	fclose(boot);

	read_super(img);

	read_inode(img, 1, &inode);

	look_up(img, &inode, "boot");
	look_up(img, &inode, "kernel");

	memset(blocks, 0, 512);
	for (i = 0; i < 7; i++)
		blocks[i] = inode.i_zone[i];

	read_block(img, inode.i_zone[7], (char*) buf2);
	while (i * 1024 < inode.i_size) {
		blocks[i] = buf2[i - 7];
		i++;
	}

	fseek(img, 0, 0);
	fwrite(boot_buf, 1, 512, img);
	fwrite(blocks, 1, 512, img);
	fclose(img);
}
Пример #14
0
//search for dir_entry from parent dir
struct dir_entry *search_dir_entry(int direct_inum, char* filename)
{
    inode_cache *dir = read_inode(direct_inum);
    
    if (dir->data.type!=INODE_DIRECTORY) return NULL;
    int i,block_num;

    //check direct blocks
    for (i=0; i<NUM_DIRECT; i++) {
        if (dir->data.direct[i]<=0) break;
        block_num = dir->data.direct[i];
        block_cache *b = read_block(block_num);
        struct dir_entry *d = (struct dir_entry*)(b->data);
        int j;
        for (j=0; j<DIRS_PER_BLOCK; j++) {
            if (d[j].inum<=0) continue;
            if (isEqual(d[j].name,filename)){
                dir->dirty=1;
                b->dirty=1;
                return &d[j];
            }
        }
    }

    //check indirect bloc
    if (dir->data.indirect!=0) {
        block_num = dir->data.indirect;
        block_cache *b = read_block(block_num);
        int j;
        block_cache *tmp;
        for (j=0; j<BLOCKSIZE; j+=4) {
            block_num = *(int*)(b->data+j);
            if (block_num!=0) {
                tmp = read_block(block_num);
                struct dir_entry *d = (struct dir_entry*)(tmp->data);
                int k;
                for (k=0; k<DIRS_PER_BLOCK; k++) {
                    if (d[j].inum<=0) continue;
                    if (isEqual(d[j].name,filename)){
			dir->dirty=1;
                        tmp->dirty=1;
                        return &d[j];
                    }
                }
            }
            else break;
        }
    }

    return NULL;
}
Пример #15
0
static u_int16_t find_file(struct minix_superblock *sb, unsigned long address, const char *fname)
{
	unsigned long offset = 0;
	struct minix_dentry dentry;
	struct minix_inode inode;
	unsigned long inode_tbl_bass = get_inode_table_address(*sb);
	const char *tmp;
	u_int16_t ret = 0;

	int len = 0;
	int ftype;
	while (1) {
		// read first entry.
		read_dentry(&dentry, address, offset);

		if (dentry.inode == 0)
			break;

		read_inode(dentry.inode, &inode, inode_tbl_bass);

		tmp = fname;
		if (tmp[0] == '/') 
			tmp = tmp + 1;

		ftype = get_file_type(&inode); 
		if (ftype == I_FT_DIR) {
			len = count_delimita_length(tmp, '/');
			if (len == -1) {
				if (!strcmp(tmp, dentry.name))
					return dentry.inode;
			} else if (!strncmp(tmp, dentry.name, len)) {
				ret = find_file(sb, get_data_zone(inode.i_zone[0]), tmp + len);
			} else {
				// if final character is '/', finish searching.
				if (!strcmp(tmp + len, "/"))
					return dentry.inode;
			}
		} else if (ftype == I_FT_REGULAR) {
			if (!strcmp(dentry.name, tmp))
				return dentry.inode;
		}
		if (ret)
			return ret;

		offset += sizeof(dentry) - 1;
	}

	return 0;

}
Пример #16
0
int do_read(int fd, char * buf, int n) {

	int inode_number, quant;
	inode_number = search_for_fd(fd);
	if (inode_number == -1) {
		return -1;
	}
	iNode * inode = fs_get_inode(inode_number);
	if (n == -1) { //to read all the file.
		n = inode->size;
	}
	quant = read_inode(inode, buf, n);
	return quant;
}
Пример #17
0
ssize_t fs_getdents(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
	off_t *pos)
{
	struct fsdriver_dentry fsdentry;
	struct inode *i_node, *i_node_tmp;
	size_t cur_pos, new_pos;
	int r, len;
	char *cp;

	if ((i_node = find_inode(ino_nr)) == NULL)
		return EINVAL;

	if (*pos < 0 || *pos > SSIZE_MAX)
		return EINVAL;

	fsdriver_dentry_init(&fsdentry, data, bytes, getdents_buf,
	    sizeof(getdents_buf));

	r = OK;

	for (cur_pos = (size_t)*pos; ; cur_pos = new_pos) {
		i_node_tmp = alloc_inode();
		r = read_inode(i_node_tmp, i_node->extent, cur_pos, &new_pos);
		if ((r != OK) || (new_pos >= i_node->i_stat.st_size)) {
			put_inode(i_node_tmp);
			break;
		}

		/* Compute the length of the name */
		cp = memchr(i_node_tmp->i_name, '\0', NAME_MAX);
		if (cp == NULL)
			len = NAME_MAX;
		else
			len = cp - i_node_tmp->i_name;

		r = fsdriver_dentry_add(&fsdentry, i_node_tmp->i_stat.st_ino,
		    i_node_tmp->i_name, len,
		    IFTODT(i_node_tmp->i_stat.st_mode));

		put_inode(i_node_tmp);

		if (r <= 0)
			break;
	}

	if (r >= 0 && (r = fsdriver_dentry_finish(&fsdentry)) >= 0)
		*pos = cur_pos;

	return r;
}
Пример #18
0
//handling open request
void open_handler(Msg *msg, int sender_pid)
{
    //msg->ptr1 is pathname, msg->num1 is length of pathname, msg->num2 is proc_inode
    char pathname[MAXPATHNAMELEN];
    CopyFrom(sender_pid,pathname,msg->ptr1,msg->num1+1);
    int open_inum = path_to_inum(pathname,msg->num1,msg->num2,0);
    if (open_inum<=0) {
        msg->type = ERROR;
    }
    else {
        msg->num1 = open_inum;
        inode_cache *n = read_inode(open_inum);
        msg->num2 = n->data.reuse;
    }
}
Пример #19
0
int free_inode() {

    int i = 0;
    inode in;
    int inode_val = -1;

    for (i=0; i<MAX_INODES && inode_val==-1; ++i){
	read_inode(i, &in);
	if(in.free){
	    inode_val = i;
	}
    }

    return inode_val;
}
Пример #20
0
main(int argc, char **argv)
{
        struct ux_inode           inode;
        char                      buf[512];
        char                      command[512];
        off_t                     nsectors;
        int                       error, i, blk;
        ino_t                     inum;

        devfd = open(argv[1], O_RDWR);
        if (devfd < 0) {
                fprintf(stderr, "uxmkfs: Failed to open device\n");
                exit(1);
        }

        /*
         * Read in and validate the superblock
         */

        read(devfd, (char *)&sb, sizeof(struct ux_superblock));
        if (sb.s_magic != UX_MAGIC) {
                printf("This is not a uxfs filesystem\n");
                exit(1);
        }

        while (1) {
                printf("uxfsdb > ") ;
                fflush(stdout);
                scanf("%s", command);
                if (command[0] == 'q') {
                        exit(0);
                }
                if (command[0] == 'i') {
                        inum = atoi(&command[1]);
                        read_inode(inum, &inode);
                        print_inode(inum, &inode);
                }
                if (command[0] == 's') {
                        printf("\nSuperblock contents:\n");
                        printf("  s_magic   = 0x%x\n", sb.s_magic);
                        printf("  s_mod     = %s\n",
                               (sb.s_mod == UX_FSCLEAN) ?
                               "UX_FSCLEAN" : "UX_FSDIRTY");
                        printf("  s_nifree  = %d\n", sb.s_nifree);
                        printf("  s_nbfree  = %d\n\n", sb.s_nbfree);
                }
        }
}
Пример #21
0
//check is file exists in dir
int check_dir(int direct_inum, char* filename)
{
    inode_cache *dir = read_inode(direct_inum);
    if (dir->data.type!=INODE_DIRECTORY) {
        //perror("check_dir: not a dir\n");
        return -1;
    }
    int i,block_num;

    //check direct blocks
    for (i=0; i<NUM_DIRECT; i++) {
        if (dir->data.direct[i]<=0) break;
        block_num = dir->data.direct[i];
        block_cache *b = read_block(block_num);
        struct dir_entry *d = (struct dir_entry*)(b->data);
        int j;
        for (j=0; j<DIRS_PER_BLOCK; j++) {
            if (d[j].inum<=0) continue;
            int re_equ=isEqual(d[j].name,filename);
            if (re_equ) return d[j].inum;
        }
    }

    //check indirect bloc
    if (dir->data.indirect!=0) {
        block_num = dir->data.indirect;
        block_cache *b = read_block(block_num);
        int j;
        block_cache *tmp;
        for (j=0; j<BLOCKSIZE; j+=4) {
            block_num = *(int*)(b->data+j);
            if (block_num!=0) {
                tmp = read_block(block_num);
                struct dir_entry *d = (struct dir_entry*)(tmp->data);
                int k;
                for (k=0; k<DIRS_PER_BLOCK; k++) {
                    if (d[j].inum<=0) continue;
                    if (isEqual(d[j].name,filename)) return d[j].inum;
                }
            }
            else break;
        }
    }

    //perror("check_dir: return 0\n");
    return 0;
}
Пример #22
0
Файл: lfs.c Проект: kitalda/lfs
ssize_t lfs_read(int fd, void *buf, size_t count) {
	int res, block_no;
	int left_to_read = 0;
	struct inode* node;
	unsigned int block_buff[BLOCK_SIZE];
	unsigned int* buffer = buf;

	res = block_no = 0;
	//printf("read: (path=%s)\n", path);

	node = malloc(BLOCK_SIZE);
	if (!node) {
		res = -ENOMEM;
	} else {
		left_to_read = count;
		res = read_inode(log_system, fd, node);
		if (!res) {
			while (count >= BLOCK_SIZE) {
				block_no++;
				count -= BLOCK_SIZE;
			}
			int blocks_read = 0;
			int start = count;
			while ((left_to_read > 0) && !res) {
				res = read_block(log_system, block_buff,
						node->block_placements[block_no]);
				if (!res) {
					if (left_to_read < BLOCK_SIZE) {
						memcpy(&buffer[blocks_read * BLOCK_SIZE], &block_buff[start],
								left_to_read);
						left_to_read = 0;
					} else {
						res = copy_one_block(block_buff, buffer, start,
								blocks_read * BLOCK_SIZE);
						if (!res) {
							blocks_read++;
							left_to_read -= (BLOCK_SIZE - start);
							start = 0;
						}
					}
				}
			}
		}
		free(node);
	}
	return res;
}
Пример #23
0
Файл: lfs.c Проект: kitalda/lfs
int traverse_path(struct file_system* lfs, const char* path,
		struct inode* parent, struct inode* file, char* new_path) {
	unsigned int i;
	int res = 0;
	int found = 0;
	struct inode* node;
	char* name = malloc(FILE_NAME_LENGTH_MAX);

	printf("traverse_path: path = %s\n", path);

	if (parent->number_of_children <= 0) {
		free(name);
		return -ENOENT;
	}
	if (!name) {
		return -ENOMEM;
	}
	node = malloc(INODE_SIZE);
	if (!node) {
		res = -ENOMEM;
	}
	res = get_filename(path, name);
	printf("filename is = %s\n", name);

	i = 0;
	while (!found && i < parent->number_of_children) {
		unsigned int child_inode_number = parent->block_placements[i];
		printf("child: %d\n", child_inode_number);
		res = read_inode(lfs, child_inode_number, node);
		printf("read node: %s\n", node->file_name);
		if (res) {
			free(name);
			free(node);
			return res;
		}
		if (strcmp(node->file_name, name) == 0) {
			found = 1;
		}
	}
	if (found) {
		file = node;
		free(node);
		free(name);
		return 0;
	}
	return -ENOENT;
}
Пример #24
0
	int file_read(struct file *fd,char *ptr, size_t nitems){
		if(fd == NULL)
			return 0;
		inode * inode_i;
		if(fd->inode_num != 0){ 
			inode_i = read_inode(fd->inode_num);
			printk("\n[%s]",inode_i->filename);
			int no_of_sectors = (nitems + fd->offset)/512 + 1;
			int i ; 
			int no_of_sectors_to_skip = fd->offset/512;
			int bytes_skip_in_fst_sec = fd->offset%512;
			int start_sector_index = no_of_sectors_to_skip;
			int bytes_read_in_lst_sec = (fd->offset + nitems)%512;
			int last_sector_index = start_sector_index + no_of_sectors - 1;
			int buffer_size_increament = 0;
			if(last_sector_index > 10)
				return 0; 
			for(i=start_sector_index; i<= last_sector_index; i++){
				if(inode_i->sector_loc[i] == 0)
					return 0;
				read(&abar->ports[0], inode_i->sector_loc[i], 0, 1, (uint64_t) data_mgmt_buffer_p);
				if(i == start_sector_index && i != last_sector_index){
					strncpy( ptr +  buffer_size_increament , (char *)data_mgmt_buffer + bytes_skip_in_fst_sec, 512 - bytes_skip_in_fst_sec);
					buffer_size_increament += 512 - bytes_skip_in_fst_sec;
				}
				else if(i == start_sector_index && i == last_sector_index){
					strncpy( ptr +  buffer_size_increament , (char *)data_mgmt_buffer + bytes_skip_in_fst_sec, nitems);
					buffer_size_increament += nitems;
				}   
				else if(i == last_sector_index){
					strncpy( ptr +  buffer_size_increament , (char *)data_mgmt_buffer, bytes_read_in_lst_sec);
					buffer_size_increament += bytes_read_in_lst_sec;
				}
				else{
					strncpy( ptr + buffer_size_increament , (char *)data_mgmt_buffer, 512);
					buffer_size_increament += 512;
				}
			}
		}
		else{
			printk("Error !!!");
			return 0;
		}
		return nitems;
	}
Пример #25
0
void look_up(FILE *fp, struct inode *pinode, char *name)
{
	char buf[1024];
	struct dirent *de;
	int i;

	read_block(fp, pinode->i_zone[0], buf);
	de = (struct dirent *) buf;
	for (i = 0; i < 1024 / sizeof(struct dirent); i++) {
		if (!strncmp(de->d_name, name, 30)) {
			read_inode(fp, de->d_ino, pinode);
			return;
		}
		de++;
	}
	printf("can not find %s\n", name);
	exit(0);
}
Пример #26
0
//handling chdir request
void chdir_handler(Msg *msg, int sender_pid)
{
    char pathname[MAXPATHNAMELEN];
    CopyFrom(sender_pid,pathname,msg->ptr1,msg->num1+1);
    int target_inum = path_to_inum(pathname,msg->num1,msg->num2,0);
    if (target_inum<=0) {
        perror("illegal destination directory!");
        msg->type = ERROR;
        return;
    }
    inode_cache *n = read_inode(target_inum);
    if (n->data.type!=INODE_DIRECTORY) {
        perror("trying to change current directory to a non-directory place");
        msg->type = ERROR;
        return;
    }
    msg->num1 = target_inum;
}
Пример #27
0
Файл: file.c Проект: bmiro/EmoFS
/** Trunca un fitxer a partir del byte n.
 * Si n_bytes = 0 alliberam tots els blocs.
 * @inode: el nombre d'inode.
 * @n_byte: el byte final del fitxer.
 * @return: 0 si exit.
 */
int truncate_file(int inode, int n_byte) {
	int how_many, block, f_offset, l_size;
	int n_block, n_bytes, n_inode;
	int i;
	emofs_inode tmp_inode;
	int blocks_to_truncate;

	read_inode(inode, &tmp_inode);

	if (tmp_inode.size <= n_byte) {
		/* Si fitxer es mes petit o del tamany a truncar
		 * no s'ha de fer res. */
		return 0;
	}

	blocks_to_truncate = (tmp_inode.size-n_byte)/BLOCK_SIZE;
	for(i = INDIRECT_POINTER_COUNT-1; i >= 0; i--) {
		if(tmp_inode.indirect_pointer[i] != NULL_POINTER) {
			truncate_assist(tmp_inode.indirect_pointer[i], i,  \
					&tmp_inode, &blocks_to_truncate);
			if (blocks_to_truncate > 0) {
				free_block(tmp_inode.indirect_pointer[i]);
				tmp_inode.indirect_pointer[i] = NULL_POINTER;
				tmp_inode.block_count--;
				blocks_to_truncate--;
			}
		}
	}
	for(i = DIRECT_POINTER_COUNT-1; i >= 0; i--) {
		/* El blocks_to_truncate > 0 no esta a la condicio
		 * del bucle per donar suport als fitxers esparsos. */
		if (blocks_to_truncate > 0 && \
		    tmp_inode.direct_pointer[i] != NULL_POINTER) {
			free_block(tmp_inode.direct_pointer[i]);
			tmp_inode.direct_pointer[i] = NULL_POINTER;
			tmp_inode.block_count--;
			blocks_to_truncate--;
		}
	}
	
	tmp_inode.size = n_byte;
	write_inode(inode, &tmp_inode);
	return 0;
}
Пример #28
0
bin_file_t *
bin_file_new (const char *filename)
{
    ElfParser *elf = NULL;
    bin_file_t *bf;

    bf = g_new0 (bin_file_t, 1);

    bf->inode_check = FALSE;
    bf->filename = g_strdup (filename);
    bf->undefined_name = g_strdup_printf ("In file %s", filename);
    bf->ref_count = 1;
    bf->elf_files = NULL;

    if (strcmp (filename, "[vdso]") == 0)
    {
        const guint8 *vdso_bytes;
        gsize length;

        vdso_bytes = get_vdso_bytes (&length);

        if (vdso_bytes)
            elf = elf_parser_new_from_data (vdso_bytes, length);
    }
    else
    {
        elf = elf_parser_new (filename, NULL);
    }

    if (elf)
    {
        /* We need the text offset of the actual binary, not the
         * (potential) debug binaries
         */
        bf->text_offset = elf_parser_get_text_offset (elf);

        bf->elf_files = get_debug_binaries (bf->elf_files, elf, filename);
        bf->elf_files = g_list_append (bf->elf_files, elf);

        bf->inode = read_inode (filename);
    }

    return bf;
}
Пример #29
0
int ext2_truncate(inode_t *inode, off_t off) {

        __u32 size = off;
        ext2_fs_instance_t *instance = (ext2_fs_instance_t*)inode->i_instance;
        struct ext2_inode *einode = read_inode(instance, inode->i_ino);
        if (einode) {

                __u32 n_blk;
                if (size > 0) {
                        n_blk   = (size - 1) / (1024 << instance->superblock.s_log_block_size) + 1;
                } else {
                        n_blk = 1;
                }

                __u32 addr = get_data_block (instance,einode, n_blk);;
                if (addr) {
                       
                        while (addr) {
                                if (off <= 0) {
                                        free_block(instance, addr / (1024 << instance->superblock.s_log_block_size));
                                } else {
                                        off -= 1024 << instance->superblock.s_log_block_size;
                                }
                                n_blk++;
                                addr = get_data_block (instance,einode, n_blk);
                        }
                } else {
                       
                        while (off > 0) {
                                set_block_inode_data(instance, einode, n_blk, alloc_block(instance));
                                n_blk++;
                                off -= 1024 << instance->superblock.s_log_block_size;
                        }
                }

                einode->i_size = size;
                write_inode(instance, inode->i_ino, einode);
                ext2inode_2_inode(inode, inode->i_instance, inode->i_ino, einode);
                return 0;
        }
        return -ENOENT;
}
Пример #30
0
int ext2_truncate(inode_t *inode, off_t off) {

	uint32_t size = off;
	ext2_fs_instance_t *instance = (ext2_fs_instance_t*)inode->i_instance;
	struct ext2_inode *einode = read_inode(instance, inode->i_ino);
	if (einode) {
// TODO vérifier le bon fonctionnement.
		int n_blk;
		if (size > 0) {
			n_blk	= (size - 1) / (1024 << instance->superblock.s_log_block_size) + 1;
		} else {
			n_blk = 1;
		}

		int addr = addr_inode_data2(instance, einode, n_blk);
		if (addr) {
			// 1er cas : off est plus petit que la taille du fichier.
			while (addr) {
				if (off <= 0) {
					free_block(instance, addr / (1024 << instance->superblock.s_log_block_size));
				} else {
					off -= 1024 << instance->superblock.s_log_block_size;
				}
				n_blk++;
				addr = addr_inode_data2(instance, einode, n_blk);
			}
		} else {
			// 2er cas : off est plus grand.
			while (off > 0) {
				set_block_inode_data(instance, einode, n_blk, alloc_block(instance));
				n_blk++;
				off -= 1024 << instance->superblock.s_log_block_size;
			}
		}

		einode->i_size = size;
		write_inode(instance, inode->i_ino, einode);
		ext2inode_2_inode(inode, inode->i_instance, inode->i_ino, einode);
		return 0;
	}
	return -ENOENT;
}