Ejemplo n.º 1
0
fsi_file_t *
ext2lib_open(fsi_t *fsi, const char *path)
{
	ext2_ino_t ino;
	ext2_filsys *fs = fsip_fs_data(fsi);
	ext2_file_t *f;
	fsi_file_t *file;
	int err;

	err = ext2fs_namei_follow(*fs, EXT2_ROOT_INO, EXT2_ROOT_INO,
	    path, &ino);

	if (err != 0) {
		errno = ENOENT;
		return (NULL);
	}

	f = malloc(sizeof (*f));
	if (f == NULL)
		return (NULL);

	err = ext2fs_file_open(*fs, ino, 0, f);

	if (err != 0) {
		free(f);
		errno = EINVAL;
		return (NULL);
	}

	file = fsip_file_alloc(fsi, f);
	if (file == NULL)
		free(f);
	return (file);
}
Ejemplo n.º 2
0
static ext2_ino_t ext2CreateFile(ext2_vd *vd, ext2_inode_t * parent, int type, const char * name)
{
    errcode_t retval = -1;
    ext2_ino_t newfile = 0;
    ext2_ino_t existing;

    if(ext2fs_namei_follow(vd->fs, vd->root, parent->ino, name, &existing) == 0) {
		errno = EEXIST;
        return 0;
    }

	retval = ext2fs_new_inode(vd->fs, parent->ino, type, 0, &newfile);
	if (retval) {
		errno = EFAULT;
        return 0;
	}
    

    while((retval = ext2fs_link(vd->fs, parent->ino, name, newfile, EXT2_FT_REG_FILE)) == EXT2_ET_DIR_NO_SPACE)
    {
		if (ext2fs_expand_dir(vd->fs, parent->ino) != EXT2_ET_OK) {
			errno = EMLINK;
			return 0;
		}
    }


    if (retval != EXT2_ET_OK) {
		errno = EMLINK;
        return 0;
    }

	ext2fs_inode_alloc_stats2(vd->fs, newfile, +1, 0);

	struct ext2_inode inode;
	memset(&inode, 0, sizeof(inode));
	inode.i_mode = type;
	inode.i_atime = inode.i_ctime = inode.i_mtime = time(0);
	inode.i_links_count = 1;
	inode.i_size = 0;
    inode.i_uid = parent->ni.i_uid;
    inode.i_gid = parent->ni.i_gid;

	if (ext2fs_write_new_inode(vd->fs, newfile, &inode) != 0)
        return 0;

    return newfile;
}
Ejemplo n.º 3
0
static ext2_ino_t ext2CreateMkDir(ext2_vd *vd, ext2_inode_t * parent, int type, const char * name)
{
    ext2_ino_t newentry = 0;
    ext2_ino_t existing;

    if(ext2fs_namei_follow(vd->fs, vd->root, parent->ino, name, &existing) == 0){
		errno = EEXIST;
        return 0;
    }

    errcode_t err = ext2fs_new_inode(vd->fs, parent->ino, type, 0, &newentry);
    if(err != EXT2_ET_OK) {
		errno = EFAULT;
        return 0;
    }

    while((err = ext2fs_mkdir(vd->fs, parent->ino, newentry, name)) == EXT2_ET_DIR_NO_SPACE)
    {
		if(ext2fs_expand_dir(vd->fs, parent->ino) != EXT2_ET_OK) {
			errno = EMLINK;
			return 0;
		}
    }

    if(err != EXT2_ET_OK) {
		errno = EMLINK;
        return 0;
    }

    struct ext2_inode inode;
    if(ext2fs_read_inode(vd->fs, newentry, &inode) == EXT2_ET_OK)
    {
        inode.i_mode = type;
        inode.i_uid = parent->ni.i_uid;
        inode.i_gid = parent->ni.i_gid;
        ext2fs_write_new_inode(vd->fs, newentry, &inode);
    }

    return newentry;
}
Ejemplo n.º 4
0
static ext2_ino_t ext2PathToInode(ext2_vd *vd, const char * path)
{
    //Sanity check
    if(!vd || !path)
        return 0;

    char filename[EXT2_NAME_LEN];
    errcode_t errorcode = 0;
    ext2_ino_t ino = 0, parent = vd->cwd_ni && *path != '/' && *path != '\0' ? vd->cwd_ni->ino : vd->root;
    const char * ptr = path;
    int i;

    while(*ptr == '/') ++ptr;

    if(*ptr == '\0')
        return parent;

    while(*ptr != '\0')
    {
        for(i = 0; *ptr != '\0' && *ptr != '/' && (i < EXT2_NAME_LEN-1); ++ptr, ++i)
            filename[i] = *ptr;

        filename[i] = '\0';

        errorcode = ext2fs_namei_follow(vd->fs, vd->root, parent, filename, &ino);
        if(errorcode != EXT2_ET_OK)
            return 0;

        parent = ino;

        while(*ptr == '/') ++ptr;

    }


    return ino;
}