Esempio n. 1
0
int sys_open(char* path)
{
	u32 fd;
	struct file* fp;
	struct open_file* of;

	if (!(fp = path_to_file(path)))
	{
		// printk("DEBUG: sys_open(): can't open %s\n", path);
		return -1;
	}

	// printk("DEBUG: sys_open(): process[%d] opening file %s\n", current->pid, fp->name);

	fp->opened++;

	if (!fp->inode)
		fp->inode = ext2_read_inode(fp->disk, fp->inum);

	/* Lecture du fichier */
	fp->mmap = ext2_read_file(fp->disk, fp->inode);

	/*
	 * Recherche d'un descripteur libre.
	 */
	fd = 0;
	if (current->fd == 0)
	{
		current->fd = (struct open_file *) malloc(sizeof(struct open_file));
		current->fd->file = fp;
		current->fd->ptr = 0;
		current->fd->next = 0;
	}
	else
	{
		of = current->fd;
		while (of->file && of->next)
		{
			of = of->next;
			fd++;
		}

		if (of->file == 0) 	/* Reuse old descriptor */
		{
			of->file = fp;
			of->ptr = 0;
		}
		else				/* Use new one */
		{
			of->next = (struct open_file *) malloc(sizeof(struct open_file));
			of->next->file = fp;
			of->next->ptr = 0;
			of->next->next = 0;
			fd++;
		}
	}

	return fd;
}
Esempio n. 2
0
int ext2_read(int fd, void *read_start,size_t size)
{
	int real_size;

	memset(read_start, 0, size);
	if((_file[fd].posn + size) > the_inode->i_size) {
		size = the_inode->i_size - _file[fd].posn;
	}

	real_size = ext2_read_file(fd, read_start, size, _file[fd].posn, the_inode);
	if((_file[fd].posn + real_size) > the_inode->i_size) {
		real_size = the_inode->i_size - _file[fd].posn;
		_file[fd].posn = the_inode->i_size;
	} else
		_file[fd].posn += real_size;

	return real_size;
}
Esempio n. 3
0
static int ext2_load_linux(int fd,int index, const unsigned char *path)
{
	struct ext2_inode *ext2_raw_inode;
	ext2_dirent *de;
	unsigned char *bh;
	int i;
	unsigned int inode;
	int find = 1;
	unsigned char s[EXT2_NAME_LEN];
	unsigned char pathname[EXT2_NAME_LEN], *pathnameptr;
	unsigned char *directoryname;
	int showdir, lookupdir;

	showdir = 0;
	lookupdir = 0;
	bh = 0;

	if(read_super_block(fd,index))
		return -1;

	if((path[0]==0) || (path[strlen(path)-1] == '/'))
		lookupdir = 1;

	strncpy(pathname,path,sizeof(pathname));
	pathnameptr = pathname;
	for(inode = EXT2_ROOT_INO; find; ) {
		for(i = 0; pathnameptr[i] && pathnameptr[i] != '/'; i++);

		pathnameptr[i] = 0;

		directoryname = (unsigned char *)pathnameptr;
		pathnameptr = (unsigned char *)(pathnameptr + i + 1);

		if(!strlen(directoryname) && lookupdir)
			showdir = 1;
		if(ext2_get_inode(fd, inode, &ext2_raw_inode)) {
			printf("load EXT2_ROOT_INO error");
			return -1;
		}
		if(!bh)
			bh = (unsigned char *)malloc(sb_block_size + ext2_raw_inode->i_size);

		if(!bh) {
			printf("Error in allocting memory for file content!\n");
			return -1;
		}
		if(ext2_read_file(fd, bh, ext2_raw_inode->i_size, 0,
					ext2_raw_inode) != ext2_raw_inode->i_size)
			return -1;
		de = (ext2_dirent *)bh;
		find = 0;

		for ( ; ((unsigned char *) de < bh + ext2_raw_inode->i_size) &&
				(de->rec_len > 0) && (de->name_len > 0); de = ext2_next_entry(de)) {
			strncpy(s,de->name,de->name_len);
			s[de->name_len]='\0';//*(de->name+de->name_len)='\0';
#ifdef DEBUG_IDE
			printf("entry:name=%s,inode=%d,rec_len=%d,name_len=%d,file_type=%d\n",s,de->inode,de->rec_len,de->name_len,de->file_type);
#endif
			if(showdir)
				printf("%s%s",s,((de->file_type)&2)?"/ ":" ");

			if (!ext2_entrycmp(directoryname, de->name, de->name_len)) {
				if(de->file_type == EXT2_FT_REG_FILE) {
					if (ext2_get_inode(fd, de->inode, &the_inode)) {
						printf("load EXT2_ROOT_INO error");
						free(bh);
						return -1;
					}
					free(bh);
					return 0;
				}
				find = 1;
				inode = de->inode;
				break;
			}
		}
		if(!find) {
			free(bh);
			if(!lookupdir)
				printf("Not find the file or directory!\n");
			else
				printf("\n");
			return -1;
		}
	}
	return -1;
}