示例#1
0
DIR_ITER *ext2_diropen_r (struct _reent *r, DIR_ITER *dirState, const char *path)
{
    ext2_log_trace("dirState %p, path %s\n", dirState, path);

    if(!dirState)
    {
        r->_errno = EINVAL;
        return NULL;
    }

    ext2_dir_state* dir = STATE(dirState);

    if(!dir)
    {
        r->_errno = EINVAL;
        return NULL;
    }

    // Get the volume descriptor for this path
    dir->vd = ext2GetVolume(path);
    if (!dir->vd) {
        r->_errno = ENODEV;
        return NULL;
    }

    // Lock
    ext2Lock(dir->vd);

    // Find the directory
    dir->ni = ext2OpenEntry(dir->vd, path);
    if (!dir->ni) {
        ext2Unlock(dir->vd);
        r->_errno = ENOENT;
        return NULL;
    }

    // Ensure that this directory is indeed a directory
    if (!LINUX_S_ISDIR(dir->ni->ni.i_mode)) {
        ext2CloseEntry(dir->vd, dir->ni);
        ext2Unlock(dir->vd);
        r->_errno = ENOTDIR;
        return NULL;
    }

    // Read the directory
    dir->first = dir->current = NULL;
    if (ext2fs_dir_iterate(dir->vd->fs, dir->ni->ino, 0, 0, DirIterateCallback, dirState) != EXT2_ET_OK) {
        ext2CloseDir(dir);
        ext2Unlock(dir->vd);
        r->_errno = errno;
        return NULL;
    }

    // Move to the first entry in the directory
    dir->current = dir->first;

    // Update directory times
    ext2UpdateTimes(dir->vd, dir->ni, EXT2_UPDATE_ATIME);

    // Insert the directory into the double-linked FILO list of open directories
    if (dir->vd->firstOpenDir) {
        dir->nextOpenDir = dir->vd->firstOpenDir;
        dir->vd->firstOpenDir->prevOpenDir = dir;
    } else {
        dir->nextOpenDir = NULL;
    }
    dir->prevOpenDir = NULL;
    dir->vd->cwd_ni = dir->ni;
    dir->vd->firstOpenDir = dir;
    dir->vd->openDirCount++;

    // Unlock
    ext2Unlock(dir->vd);

    return dirState;
}
示例#2
0
int ext2_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
{
    ext2_log_trace("path %s, buf %p\n", path, buf);

    ext2_vd *vd = NULL;

    // Get the volume descriptor for this path
    vd = ext2GetVolume(path);
    if (!vd) {
        r->_errno = ENODEV;
        return -1;
    }

    // Short circuit cases were we don't actually have to do anything
    if (!buf)
        return 0;

    // Lock
    ext2Lock(vd);

    // Zero out the stat buffer
    memset(buf, 0, sizeof(struct statvfs));

    // File system block size
    switch(vd->fs->super->s_log_block_size)
    {
        case 1:
            buf->f_bsize = 2048;
            break;
        case 2:
            buf->f_bsize = 4096;
            break;
        case 3:
            buf->f_bsize = 8192;
            break;
        default:
        case 0:
            buf->f_bsize = 1024;
            break;
    }

    // Fundamental file system block size
    buf->f_frsize = buf->f_bsize;

    // Total number of blocks on file system in units of f_frsize
    buf->f_blocks = vd->fs->super->s_blocks_count | (((u64) vd->fs->super->s_blocks_count_hi) << 32);

    // Free blocks available for all and for non-privileged processes
    buf->f_bfree = vd->fs->super->s_free_blocks_count | (((u64) vd->fs->super->s_free_blocks_hi) << 32);

    // Number of inodes at this point in time
    buf->f_files = vd->fs->super->s_inodes_count;

    // Free inodes available for all and for non-privileged processes
    buf->f_ffree = vd->fs->super->s_free_inodes_count;

    // File system id
    buf->f_fsid = vd->fs->super->s_magic;

    // Bit mask of f_flag values.
    buf->f_flag = vd->fs->super->s_flags;

    // Maximum length of filenames
    buf->f_namemax = EXT2_NAME_LEN;

    // Unlock
    ext2Unlock(vd);

    return 0;
}
示例#3
0
ext2_inode_t *ext2Create(ext2_vd *vd, const char *path, mode_t type, const char *target)
{
    ext2_inode_t *dir_ni = NULL, *ni = NULL;
    char *dir = NULL;
    char *targetdir = NULL;
    char *name = NULL;
    ext2_ino_t newentry = 0;

    // Sanity check
    if (!vd || !vd->fs) {
        errno = ENODEV;
        return NULL;
    }

    if(!(vd->fs->flags & EXT2_FLAG_RW)) {
        errno = EACCES;
        return NULL;
    }

    // You cannot link between devices
    if(target) {
        if(vd != ext2GetVolume(target)) {
            errno = EXDEV;
            return NULL;
        }
        
        // Check if existing
        dir_ni = ext2OpenEntry(vd, target);
        if (dir_ni) {
			errno = EEXIST;
            goto cleanup;
        }

        targetdir = strdup(target);
        if (!targetdir) {
            errno = ENOMEM;
            goto cleanup;
        }

		target = ext2RealPath(target);
    }

    // Get the actual paths of the entry
    path = ext2RealPath(path);
    if (!path)
		goto cleanup;

    // Lock
    ext2Lock(vd);

    // Clean me
    // NOTE: this looks horrible right now and need a cleanup
    dir = strdup(path);
    if (!dir) {
        errno = ENOMEM;
        goto cleanup;
    }

    char * tmp_path = (targetdir && (type == S_IFLNK)) ? targetdir : dir;
    
    if (strrchr(tmp_path, '/') != NULL)
    {
        char * ptr = strrchr(tmp_path, '/');
        name = strdup(ptr+1);
        *ptr = '\0';
    }
    else
        name = strdup(tmp_path);


    // Open the entries parent directory
    dir_ni = ext2OpenEntry(vd, dir);
    if (!dir_ni)
        goto cleanup;

    // If not yet read, read the inode and block bitmap
    if((!vd->fs->inode_map || !vd->fs->block_map))
        ext2fs_read_bitmaps(vd->fs);

    // Symbolic link
    if(type == S_IFLNK)
    {
        if (!target) {
            errno = EINVAL;
            goto cleanup;
        }

        newentry = ext2CreateSymlink(vd, path, targetdir, name, type);
    }
    // Directory
    else if(type == S_IFDIR)
    {
        newentry = ext2CreateMkDir(vd, dir_ni, LINUX_S_IFDIR | (0755 & ~vd->fs->umask), name);
    }
    // File
    else if(type == S_IFREG)
    {
        
        newentry = ext2CreateFile(vd, dir_ni, LINUX_S_IFREG | (0755 & ~vd->fs->umask), name);
        
    }

    // If the entry was created
    if (newentry != 0)
    {
        // Sync the entry to disc
        ext2Sync(vd, NULL);

        ni = ext2OpenEntry(vd, target ? target : path);
    }

cleanup:

    if(dir_ni)
        ext2CloseEntry(vd, dir_ni);

    if(name)
        mem_free(name);

    if(dir)
        mem_free(dir);

    if(targetdir)
        mem_free(targetdir);

    // Unlock
    ext2Unlock(vd);

    return ni;
}
示例#4
0
int ext2Link(ext2_vd *vd, const char *old_path, const char *new_path)
{
    ext2_inode_t *dir_ni = NULL, *ni = NULL;
    char *dir = NULL;
    char *name = NULL;
    errcode_t err = 0;

    // Sanity check
    if (!vd || !vd->fs) {
        errno = ENODEV;
        return -1;
    }

    if(!(vd->fs->flags & EXT2_FLAG_RW)) {
        errno = EACCES;
        return -1;
    }

    // You cannot link between devices
    if(vd != ext2GetVolume(new_path)) {
        errno = EXDEV;
        return -1;
    }

    // Get the actual paths of the entry
    old_path = ext2RealPath(old_path);
    new_path = ext2RealPath(new_path);
    if (!old_path || !new_path)
        return -1;

    // Lock
    ext2Lock(vd);

    //check for existing in new path
    ni = ext2OpenEntry(vd, new_path);
    if (ni) {
        errno = EEXIST;
        goto cleanup;
    }

    dir = strdup(new_path);
    if (!dir) {
        errno = ENOMEM;
        err = -1;
        goto cleanup;
    }
    char * ptr = strrchr(dir, '/');
    if (ptr)
    {
        name = strdup(ptr+1);
        *ptr = 0;
    }
    else
        name = strdup(dir);

    // Find the entry
    ni = ext2OpenEntry(vd, old_path);
    if (!ni) {
        err = -1;
        goto cleanup;
    }

    // Open the entries new parent directory
    dir_ni = ext2OpenEntry(vd, dir);
    if (!dir_ni) {
        err = -1;
        goto cleanup;
    }

    do
    {
        // Link the entry to its new parent
        err = ext2fs_link(vd->fs, dir_ni->ino, name, ni->ino, ext2_file_type(ni->ni.i_mode));
        if (err == EXT2_ET_DIR_NO_SPACE)
        {
            if (ext2fs_expand_dir(vd->fs, dir_ni->ino) != 0)
                goto cleanup;
        }
        else if(err != EXT2_ET_OK)
        {
            errno = EMLINK;
            goto cleanup;
        }
    }
    while(err == EXT2_ET_DIR_NO_SPACE);

    ni->ni.i_links_count++;

    // Update entry times
    ext2UpdateTimes(vd, ni, EXT2_UPDATE_MCTIME);

    // Sync the entry to disc
    ext2Sync(vd, ni);

cleanup:

    if(dir_ni)
        ext2CloseEntry(vd, dir_ni);

    if(ni)
        ext2CloseEntry(vd, ni);

    if(dir)
        mem_free(dir);

    if(name)
        mem_free(name);

    // Unlock
    ext2Unlock(vd);

    return err;
}
示例#5
0
int ext2_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode)
{
    ext2_log_trace("fileStruct %p, path %s, flags %i, mode %i\n", fileStruct, path, flags, mode);

    ext2_file_state* file = STATE(fileStruct);

    // Get the volume descriptor for this path
    file->vd = ext2GetVolume(path);
    if (!file->vd) {
        r->_errno = ENODEV;
        return -1;
    }

    // Lock
    ext2Lock(file->vd);

    // Determine which mode the file is opened for
    file->flags = flags;
    if ((flags & 0x03) == O_RDONLY) {
        file->read = true;
        file->write = false;
        file->append = false;
    } else if ((flags & 0x03) == O_WRONLY) {
        file->read = false;
        file->write = true;
        file->append = (flags & O_APPEND);
    } else if ((flags & 0x03) == O_RDWR) {
        file->read = true;
        file->write = true;
        file->append = (flags & O_APPEND);
    } else {
        r->_errno = EACCES;
        ext2Unlock(file->vd);
        return -1;
    }

    // Try and find the file and (if found) ensure that it is not a directory
    file->ni = ext2OpenEntry(file->vd, path);
    if (file->ni && LINUX_S_ISDIR(file->ni->ni.i_mode))
    {
        ext2CloseEntry(file->vd, file->ni);
        ext2Unlock(file->vd);
        r->_errno = EISDIR;
        return -1;
    }

    // Are we creating this file?
    if ((flags & O_CREAT) && !file->ni)
        // Create the file
        file->ni = ext2Create(file->vd, path, S_IFREG, NULL);

    // Sanity check, the file should be open by now
    if (!file->ni) {
        ext2Unlock(file->vd);
        r->_errno = ENOENT;
        return -1;
    }

    // Make sure we aren't trying to write to a read-only file
    if (!(file->vd->fs->flags & EXT2_FLAG_RW) && file->write)
    {
        ext2CloseEntry(file->vd, file->ni);
        ext2Unlock(file->vd);
        r->_errno = EROFS;
        return -1;
    }

    errcode_t err = ext2fs_file_open2(file->vd->fs, file->ni->ino, &file->ni->ni,
                                      file->write ? EXT2_FLAG_RW : 0, &file->fd);
    if(err != 0)
    {
        ext2CloseEntry(file->vd, file->ni);
        ext2Unlock(file->vd);
        r->_errno = ENOENT;
        return -1;
    }


    // Truncate the file if requested
    if ((flags & O_TRUNC) && file->write) {
        if (ext2fs_file_set_size2(file->fd, 0) != 0) {
            ext2CloseEntry(file->vd, file->ni);
            ext2Unlock(file->vd);
            r->_errno = errno;
            return -1;
        }
        file->ni->ni.i_size = file->ni->ni.i_size_high = 0;
    }

    // Set the files current position
	ext2fs_file_llseek(file->fd, file->append ? EXT2_I_SIZE(&file->ni->ni) : 0, SEEK_SET, 0);

    ext2_log_trace("file->len %lld\n", EXT2_I_SIZE(&file->ni->ni));

    // Update file times
    ext2UpdateTimes(file->vd, file->ni, EXT2_UPDATE_ATIME);

    // Insert the file into the double-linked FILO list of open files
    if (file->vd->firstOpenFile) {
        file->nextOpenFile = file->vd->firstOpenFile;
        file->vd->firstOpenFile->prevOpenFile = file;
    } else {
        file->nextOpenFile = NULL;
    }
    file->prevOpenFile = NULL;
    file->vd->firstOpenFile = file;
    file->vd->openFileCount++;

    // Sync access time
    ext2Sync(file->vd, file->ni);

    // Unlock
    ext2Unlock(file->vd);

    return (int)fileStruct;
}