ext2/vfs: implement read fs call
also implement the cat command!
This commit is contained in:
parent
0029621ada
commit
ad2de927e2
8 changed files with 128 additions and 9 deletions
43
fs/ext2.c
43
fs/ext2.c
|
|
@ -359,14 +359,51 @@ int ext2_close_file(struct fs *vfs, struct file *file)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t count)
|
||||
int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t count)
|
||||
int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
|
||||
{
|
||||
return 0;
|
||||
struct ext2fs *fs = vfs->fs;
|
||||
|
||||
if ((file->inode.mode & 0xF000) != EXT2_INO_REG)
|
||||
return -EISDIR;
|
||||
|
||||
size_t block = offset / fs->block_size;
|
||||
offset %= fs->block_size;
|
||||
|
||||
if (offset > file->size)
|
||||
return -EINVAL;
|
||||
|
||||
if (offset + count > file->size)
|
||||
count = (file->size - offset);
|
||||
|
||||
size_t ret = count;
|
||||
|
||||
void *block_buf = kmalloc(fs->block_size, ALLOC_KERN);
|
||||
|
||||
if (!block_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
size_t bufoffset = 0;
|
||||
|
||||
while (count > 0) {
|
||||
int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&file->inode, block_buf, block);
|
||||
if (retval < 0) {
|
||||
kfree(block_buf);
|
||||
return retval;
|
||||
}
|
||||
|
||||
memcpy(buf + bufoffset, block_buf + offset, MIN(count, fs->block_size - offset));
|
||||
bufoffset += fs->block_size;
|
||||
offset = 0;
|
||||
|
||||
count -= MIN(fs->block_size, count);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ext2_seek_file(struct fs *vfs, struct file *file, size_t offset)
|
||||
|
|
|
|||
25
fs/vfs.c
25
fs/vfs.c
|
|
@ -21,7 +21,27 @@ int write(int fd, const void *buf, size_t count)
|
|||
|
||||
int read(int fd, void *buf, size_t count)
|
||||
{
|
||||
return 0;
|
||||
struct rbnode *fdnode = rbt_search(kfd, fd);
|
||||
|
||||
if (!fdnode)
|
||||
return -EBADF;
|
||||
|
||||
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||
|
||||
if(fdesc->file.type == VFS_FILE_DIR)
|
||||
return -EISDIR;
|
||||
|
||||
if (fdesc->file.type != VFS_FILE_FILE)
|
||||
return -EBADF;
|
||||
|
||||
int ret = fdesc->fs->ops.read(fdesc->fs, &fdesc->file, buf, fdesc->pos, count);
|
||||
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
fdesc->pos = MIN(fdesc->pos + ret, fdesc->file.size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int open(const char *pathname, int flags)
|
||||
|
|
@ -107,7 +127,7 @@ DIR *opendir(const char *name)
|
|||
|
||||
struct dirent *readdir(DIR *dir)
|
||||
{
|
||||
if(!dir)
|
||||
if (!dir)
|
||||
return NULL;
|
||||
|
||||
struct fs *fs = dir->fs;
|
||||
|
|
@ -171,4 +191,3 @@ void vfs_init(const char *rootdev_name)
|
|||
/* init file descriptor slab */
|
||||
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue