aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorMarvin Borner2020-11-20 23:05:07 +0100
committerMarvin Borner2020-11-20 23:05:07 +0100
commitcbdf41189c4627fbea719e96b93e2237f38ef5de (patch)
treeba159ea2e2f726d4cc10db95babf59279ff03fc8 /kernel
parent33d3ac6b5302f5e3a23cc7e318cf1f0605c398e5 (diff)
Added stat syscall
This currently only returns the file size, other stats will follow soon!
Diffstat (limited to 'kernel')
-rw-r--r--kernel/features/fs.c78
-rw-r--r--kernel/features/load.c4
-rw-r--r--kernel/features/syscall.c6
-rw-r--r--kernel/inc/fs.h6
4 files changed, 59 insertions, 35 deletions
diff --git a/kernel/features/fs.c b/kernel/features/fs.c
index 41aca78..c17a721 100644
--- a/kernel/features/fs.c
+++ b/kernel/features/fs.c
@@ -96,7 +96,40 @@ void *read_inode(struct inode *in)
return buf;
}
-void *read_file(char *path)
+u32 find_inode(const char *name, u32 dir_inode)
+{
+ if (!dir_inode)
+ return (unsigned)-1;
+
+ struct inode *i = get_inode(dir_inode);
+
+ char *buf = malloc(BLOCK_SIZE * i->blocks / 2);
+ memset(buf, 0, BLOCK_SIZE * i->blocks / 2);
+
+ for (u32 q = 0; q < i->blocks / 2; q++) {
+ char *data = buffer_read(i->block[q]);
+ memcpy((u32 *)((u32)buf + q * BLOCK_SIZE), data, BLOCK_SIZE);
+ }
+
+ struct dirent *d = (struct dirent *)buf;
+
+ u32 sum = 0;
+ do {
+ // Calculate the 4byte aligned size of each entry
+ sum += d->total_len;
+ if (strlen(name) == d->name_len &&
+ strncmp((void *)d->name, name, d->name_len) == 0) {
+ free(buf);
+ return d->inode_num;
+ }
+ d = (struct dirent *)((u32)d + d->total_len);
+
+ } while (sum < (1024 * i->blocks / 2));
+ free(buf);
+ return (unsigned)-1;
+}
+
+struct inode *find_inode_by_path(char *path)
{
if (path[0] != '/')
return 0;
@@ -126,38 +159,27 @@ void *read_file(char *path)
if ((signed)inode <= 0)
return 0;
- return read_inode(get_inode(inode));
+ return get_inode(inode);
}
-u32 find_inode(const char *name, u32 dir_inode)
+void *file_read(char *path)
{
- if (!dir_inode)
- return (unsigned)-1;
-
- struct inode *i = get_inode(dir_inode);
-
- char *buf = malloc(BLOCK_SIZE * i->blocks / 2);
- memset(buf, 0, BLOCK_SIZE * i->blocks / 2);
+ return read_inode(find_inode_by_path(path));
+}
- for (u32 q = 0; q < i->blocks / 2; q++) {
- char *data = buffer_read(i->block[q]);
- memcpy((u32 *)((u32)buf + q * BLOCK_SIZE), data, BLOCK_SIZE);
- }
+u32 file_stat(char *path)
+{
+ struct inode *in = find_inode_by_path(path);
+ assert(in);
+ if (!in)
+ return 0;
- struct dirent *d = (struct dirent *)buf;
+ u32 num_blocks = in->blocks / (BLOCK_SIZE / SECTOR_SIZE);
- u32 sum = 0;
- do {
- // Calculate the 4byte aligned size of each entry
- sum += d->total_len;
- if (strlen(name) == d->name_len &&
- strncmp((void *)d->name, name, d->name_len) == 0) {
- free(buf);
- return d->inode_num;
- }
- d = (struct dirent *)((u32)d + d->total_len);
+ assert(num_blocks != 0);
+ if (!num_blocks)
+ return 0;
- } while (sum < (1024 * i->blocks / 2));
- free(buf);
- return (unsigned)-1;
+ u32 sz = BLOCK_SIZE * num_blocks;
+ return sz;
}
diff --git a/kernel/features/load.c b/kernel/features/load.c
index 2cbe2e7..d2f1961 100644
--- a/kernel/features/load.c
+++ b/kernel/features/load.c
@@ -11,7 +11,7 @@
int bin_load(char *path, struct proc *proc)
{
- char *data = read_file(path);
+ char *data = file_read(path);
u32 stack = (u32)malloc(0x2000) + 0x1000;
@@ -33,7 +33,7 @@ int elf_verify(struct elf_header *h)
// TODO: Fix elf loading
void elf_load(char *path, struct proc *proc)
{
- char *data = read_file(path);
+ char *data = file_read(path);
struct elf_header *h = (struct elf_header *)data;
if (!elf_verify(h)) {
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index ac89d04..3d70dbb 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -33,8 +33,12 @@ void syscall_handler(struct regs *r)
free((void *)r->ebx);
break;
}
+ case SYS_STAT: {
+ r->eax = (u32)file_stat((char *)r->ebx);
+ break;
+ }
case SYS_READ: {
- r->eax = (u32)read_file((char *)r->ebx);
+ r->eax = (u32)file_read((char *)r->ebx);
break;
}
case SYS_WRITE: {
diff --git a/kernel/inc/fs.h b/kernel/inc/fs.h
index 93d2013..156bb8c 100644
--- a/kernel/inc/fs.h
+++ b/kernel/inc/fs.h
@@ -93,9 +93,7 @@ struct file {
u32 curr_block_pos;
};
-u32 find_inode(const char *name, u32 dir_inode);
-struct inode *get_inode(u32 i);
-void *read_inode(struct inode *in);
-void *read_file(char *path);
+void *file_read(char *path);
+u32 file_stat(char *path);
#endif