feat(fd): add fd table and register/destroy function

This commit is contained in:
Malo Lecomte 2022-04-23 18:28:25 +02:00
parent edbf5121e2
commit 28cd244b45
2 changed files with 55 additions and 0 deletions

37
k/fd.c Normal file
View File

@ -0,0 +1,37 @@
#include "fd.h"
static struct file_entry fd_table[MAX_FD] = { 0 };
static int find_next_fd(void)
{
for (int fd = 0; fd < MAX_FD; ++fd)
{
if (!fd_table[fd].open)
return fd;
}
return -1;
}
int register_fd(u32 lba)
{
int free_fd = find_next_fd();
if (free_fd < 0)
return -1;
struct file_entry f = { .open = 1, .lba = lba, .file_offset = 0 };
fd_table[free_fd] = f;
return free_fd;
}
int destroy_fd(int fd)
{
if (fd > MAX_FD || !fd_table[fd].open)
return -1;
fd_table[fd].open = 0;
fd_table[fd].lba = 0;
fd_table[fd].file_offset = 0;
return 0;
}

18
k/fd.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef FD_H
#define FD_H
#include <k/types.h>
#define MAX_FD 65535
struct file_entry
{
u8 open;
u32 lba;
u64 file_offset;
};
int register_fd(u32 lba);
int destroy_fd(int fd);
#endif /* !FD_H */