[ext2] Read files from deep directories.

This commit is contained in:
Kevin Lange 2011-01-29 14:52:44 -06:00
parent f6fdaf49aa
commit d067e3d8a6
6 changed files with 36 additions and 13 deletions

View File

@ -2,7 +2,7 @@ include Makefile.inc
DIRS = core
.PHONY: all clean install core run
.PHONY: all clean install core run curses
all: kernel
@ -17,6 +17,9 @@ install: kernel
run: bootdisk.img
qemu -fda bootdisk.img
curses: bootdisk.img
qemu -curses -fda bootdisk.img
kernel: start.o link.ld main.o core
${LD} -T link.ld -o kernel *.o core/*.o core/fs/*.o

View File

@ -1,5 +1,5 @@
CC = gcc
LD = ld -m elf_i386
CFLAGS = -Wall -Wextra -pedantic -m32 -O0 -std=c99 -finline-functions -nostdinc -ffreestanding
CFLAGS = -Wall -Wextra -pedantic -m32 -O0 -std=c99 -finline-functions -fno-stack-protector -nostdinc -ffreestanding
NASM = nasm -f elf

View File

@ -29,7 +29,17 @@ read_initrd(
uint32_t size,
uint8_t *buffer
) {
return 0;
ext2_inodetable_t * inode = ext2_get_inode(node->inode);
uint32_t end;
if (offset + size > inode->size) {
end = inode->size;
} else {
end = offset + size;
}
uint32_t size_to_read = end - offset;
// TODO: proper block reading, read files larger than one block
memcpy(buffer, ext2_get_block(inode->block[0]) + offset, size_to_read);
return size_to_read;
}
uint32_t
@ -130,7 +140,7 @@ finddir_initrd(
return NULL;
}
fs_node_t * outnode = malloc(sizeof(fs_node_t));
initrd_node_from_file(direntry->inode, direntry, outnode);
initrd_node_from_file(ext2_get_inode(direntry->inode), direntry, outnode);
return outnode;
}
@ -204,22 +214,22 @@ initrd_node_from_dirent(
/* File Flags */
fnode->flags = 0;
if (inode->mode & EXT2_S_IFREG) {
fnode->flags &= FS_FILE;
fnode->flags |= FS_FILE;
}
if (inode->mode & EXT2_S_IFDIR) {
fnode->flags &= FS_DIRECTORY;
fnode->flags |= FS_DIRECTORY;
}
if (inode->mode & EXT2_S_IFBLK) {
fnode->flags &= FS_BLOCKDEVICE;
fnode->flags |= FS_BLOCKDEVICE;
}
if (inode->mode & EXT2_S_IFCHR) {
fnode->flags &= FS_CHARDEVICE;
fnode->flags |= FS_CHARDEVICE;
}
if (inode->mode & EXT2_S_IFIFO) {
fnode->flags &= FS_PIPE;
fnode->flags |= FS_PIPE;
}
if (inode->mode & EXT2_S_IFLNK) {
fnode->flags &= FS_SYMLINK;
fnode->flags |= FS_SYMLINK;
}
fnode->read = read_initrd;
fnode->write = write_initrd;

View File

@ -84,8 +84,10 @@ kopen(
free((void *)path);
return NULL;
} else if (depth == path_depth - 1) {
open_fs(node_ptr, 1, 0);
return node_ptr;
}
path_offset += strlen(path_offset) + 1;
}
free((void *)path);
return NULL;

BIN
initrd

Binary file not shown.

14
main.c
View File

@ -93,11 +93,19 @@ main(struct multiboot *mboot_ptr) {
uint32_t module_end = *(uint32_t*)(mboot_ptr->mods_addr+4);
initrd_mount(module_start, module_end);
fs_node_t * test_file = kopen("/hello.txt", NULL);
kprintf("Opening /etc/kernel/hello.txt... ");
fs_node_t * test_file = kopen("/etc/kernel/hello.txt", NULL);
if (!test_file) {
kprintf("Couldn't find /hello.txt\n");
kprintf("Couldn't find hello.txt\n");
}
kprintf("Found %s at inode %d\n", test_file->name, test_file->inode);
kprintf("Found at inode %d\n", test_file->name, test_file->inode);
char buffer[256];
uint32_t bytes_read;
bytes_read = read_fs(test_file, 0, 255, &buffer);
kprintf("Read %d bytes from file:\n", bytes_read);
kprintf("%s\n", buffer);
kprintf("| end file\n");
close_fs(test_file);
#if 0
ext2_superblock_t * superblock = (ext2_superblock_t *)(module_start + 1024);