ext2: add ext2_open_file

This commit is contained in:
Michael Shaw 2023-10-19 00:38:32 -04:00
commit e3efdde2df
2 changed files with 94 additions and 1 deletions

View file

@ -113,6 +113,89 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode)
return 0; return 0;
} }
int ext2_open_file(struct ext2fs *fs, struct ext2_file *file, const char *path)
{
/* find the inode of the file */
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
if (!inode)
return -ENOMEM;
int ret = ext2_read_inode(fs, inode, 2);
if (ret < 0) {
kfree(inode);
return ret;
}
char *path_copy = kmalloc(strlen(path) + 1, ALLOC_KERN);
if (!path_copy) {
kfree(inode);
return -ENOMEM;
}
strcpy(path_copy, path);
spinlock_acquire(&strtok_lock);
char *token = strtok(path_copy, "/");
while (token) {
struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA);
if (!entry) {
kfree(inode);
kfree(path_copy);
return -ENOMEM;
}
ext2_read_block(fs, entry, inode->block[0]);
while (entry->inode) {
char *name = kmalloc(entry->name_len + 1, ALLOC_KERN);
if (!name) {
kfree(inode);
kfree(path_copy);
kfree(entry);
return -ENOMEM;
}
memcpy(name, entry->name, entry->name_len);
name[entry->name_len] = '\0';
if (!strcmp(name, token)) {
kfree(name);
break;
}
kfree(name);
entry = (void *)entry + entry->rec_len;
}
if (!entry->inode) {
kfree(inode);
kfree(path_copy);
kfree(entry);
return -ENOENT;
}
ext2_read_inode(fs, inode, entry->inode);
token = strtok(NULL, "/");
}
spinlock_release(&strtok_lock);
kfree(path_copy);
file->inode = *inode;
file->offset = 0;
file->size = inode->size;
/* check for long filesize */
if (fs->sb.feature_ro_compat & EXT2_FLAG_LONG_FILESIZE)
file->size |= ((uint64_t)inode->dir_acl << 32);
return 0;
}
#ifdef KDEBUG #ifdef KDEBUG
static void ext2_print_dir(struct ext2fs *fs, struct ext2_inode *inode, int indent) static void ext2_print_dir(struct ext2fs *fs, struct ext2_inode *inode, int indent)
{ {
@ -218,7 +301,7 @@ struct ext2fs *ext2_init_fs(struct block_device *bdev)
ext2_read_block(bdev->fs, buf, bgdt_block); ext2_read_block(bdev->fs, buf, bgdt_block);
ret->bgdt = (struct block_group_desc *)kmalloc(bgdt_size, ALLOC_KERN); ret->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN);
memcpy(ret->bgdt, buf, bgdt_size); memcpy(ret->bgdt, buf, bgdt_size);
kfree(buf); kfree(buf);

View file

@ -19,6 +19,8 @@
#define EXT2_DE_SOCK 6 #define EXT2_DE_SOCK 6
#define EXT2_DE_SYMLINK 7 #define EXT2_DE_SYMLINK 7
#define EXT2_FLAG_LONG_FILESIZE 0x0002
struct ext2_superblock { struct ext2_superblock {
uint32_t inodes_count; uint32_t inodes_count;
uint32_t blocks_count; uint32_t blocks_count;
@ -130,6 +132,14 @@ struct ext2_dir_entry {
char name[]; char name[];
} PACKED; } PACKED;
struct ext2_file {
struct ext2_inode inode;
uint32_t block;
uint32_t offset;
uint64_t size;
uint32_t pos;
};
struct ext2fs { struct ext2fs {
struct ext2_superblock sb; struct ext2_superblock sb;
struct block_device *bdev; struct block_device *bdev;