Add fopen and fd limits to the VFS and test it

This commit is contained in:
pjht 2019-06-29 09:55:02 -05:00
parent 097d86f3d5
commit c324d46885
3 changed files with 111 additions and 77 deletions

View File

@ -29,6 +29,36 @@ uint32_t getsize(const char *in) {
return size;
}
void display_msg(vfs_message* vfs_msg) {
vga_write_string("Message of type ");
char str[256];
str[0]='\0';
int_to_ascii(vfs_msg->type,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("ID ");
str[0]='\0';
int_to_ascii(vfs_msg->id,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("Mode ");
vga_write_string(&vfs_msg->mode[0]);
vga_write_string("\n");
vga_write_string("FD ");
str[0]='\0';
int_to_ascii(vfs_msg->fd,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("Path ");
vga_write_string(&vfs_msg->path[0]);
vga_write_string("\n");
vga_write_string("Flags ");
str[0]='\0';
hex_to_ascii(vfs_msg->flags,str);
vga_write_string(str);
vga_write_string("\n");
}
int main(char* initrd, uint32_t initrd_sz) {
text_fb_info info;
info.address=map_phys((void*)0xB8000,10);
@ -48,11 +78,9 @@ int main(char* initrd, uint32_t initrd_sz) {
uint32_t size=getsize(tar_hdr.size);
pos+=512;
if (strcmp(tar_hdr.filename,"vfs")==0) {
vga_write_string("VFS found, loading\n");
datapos=pos;
break;
} else {
vga_write_string("VFS not found\n");
// for(;;);
}
pos+=size;
@ -90,13 +118,10 @@ int main(char* initrd, uint32_t initrd_sz) {
}
copy_data(cr3,ptr,pheader.memsz,(void*)pheader.vaddr);
}
vga_write_string("Loaded VFS into memory, creating task\n");
createTaskCr3((void*)header.entry,cr3);
vga_write_string("Created VFS task, creating mailbox\n");
uint32_t box=mailbox_new(16);
vga_write_string("Created mailbox, yielding to VFS so it can create it's mailbox.\n");
yield();
vga_write_string("VFS yielded back, sending test message\n");
vga_write_string("Sending first test message\n");
vfs_message* msg_data=malloc(sizeof(vfs_message));
msg_data->type=VFS_OPEN;
msg_data->id=1;
@ -108,39 +133,29 @@ int main(char* initrd, uint32_t initrd_sz) {
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
vga_write_string("Sent test message, yielding to task\n");
yield();
vga_write_string("Yielded and got control, getting message\n");
vga_write_string("Getting message\n");
msg.msg=malloc(sizeof(vfs_message));
mailbox_get_msg(box,&msg,sizeof(vfs_message));
vfs_message* vfs_msg=(vfs_message*)msg.msg;
vga_write_string("Message of type ");
char str[256];
str[0]='\0';
int_to_ascii(vfs_msg->type,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("ID ");
str[0]='\0';
int_to_ascii(vfs_msg->id,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("Mode ");
vga_write_string(&vfs_msg->mode[0]);
vga_write_string("\n");
vga_write_string("FD ");
str[0]='\0';
int_to_ascii(vfs_msg->fd,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("Path ");
vga_write_string(&vfs_msg->path[0]);
vga_write_string("\n");
vga_write_string("Flags ");
str[0]='\0';
hex_to_ascii(vfs_msg->flags,str);
vga_write_string(str);
vga_write_string("\n");
display_msg(vfs_msg);
vga_write_string("Sending second test message\n");
msg_data=malloc(sizeof(vfs_message));
msg_data->type=VFS_OPEN;
msg_data->id=2;
strcpy(&msg_data->mode[0],"r");
strcpy(&msg_data->path[0],"/dev/sdb");
msg.from=box;
msg.to=1;
msg.msg=msg_data;
msg.size=sizeof(vfs_message);
mailbox_send_msg(&msg);
yield();
vga_write_string("Getting message\n");
msg.msg=malloc(sizeof(vfs_message));
mailbox_get_msg(box,&msg,sizeof(vfs_message));
vfs_msg=(vfs_message*)msg.msg;
display_msg(vfs_msg);
}
for(;;);
}

View File

@ -5,26 +5,17 @@
#include <stddef.h>
#include <stdint.h>
typedef struct {
char* mntpnt;
const char* path;
int wr;
int rd;
uint32_t type;
unsigned long pos;
int eof;
int error;
void* data;
} FILE;
#define FILE uint32_t
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 3
#define EOF -1
extern FILE* stdin;
extern FILE* stdout;
extern FILE* stderr;
#define stdin 0
#define stdout 1
#define stderr 2
FILE* fopen(const char* filename,const char* mode);
int fgetc(FILE* stream);
@ -42,7 +33,7 @@ int printf(const char* format,...);
int fseek(FILE* stream,long offset,int origin);
long ftell(FILE* stream);
int fclose(FILE* file);
int feof(FILE *stream);
int ferror(FILE *stream);
int feof(FILE* stream);
int ferror(FILE* stream);
#endif

View File

@ -3,18 +3,29 @@
#include <mailboxes.h>
#include <stdlib.h>
#define PROC_FD_LIMIT 1024
typedef struct _vfs_mapping_struct {
char* mntpnt;
uint32_t type;
struct _vfs_mapping_struct* next;
} vfs_mapping;
typedef struct {
char* mntpnt;
char* path;
uint32_t pos;
char error;
} vfs_file;
static const char** drv_names;
static uint32_t* drvs;
static uint32_t max_drvs;
static uint32_t next_drv_indx;
static vfs_mapping* head_mapping;
static vfs_mapping* tail_mapping;
uint32_t* fd_tables[32768];
uint16_t open_fds[32768];
vfs_message* get_message(Message* msg,uint32_t box) {
msg->msg=malloc(sizeof(vfs_message));
@ -55,36 +66,53 @@ uint32_t register_fs(uint32_t drv,const char* type) {
return next_drv_indx-1;
}
char fopen(vfs_message* vfs_msg,uint32_t from) {
if (fd_tables[from]==NULL) {
fd_tables[from]=malloc(PROC_FD_LIMIT*sizeof(vfs_file));
open_fds[from]=0;
} else {
if (open_fds[from]==PROC_FD_LIMIT) {
return 4;
}
}
uint16_t fd=open_fds[from];
open_fds[from]++;
vfs_msg->fd=fd;
return 0;
}
int main() {
init_vfs();
uint32_t box=mailbox_new(16);
yield();
Message msg;
vfs_message* vfs_msg=get_message(&msg,box);
switch (vfs_msg->type) {
case VFS_OPEN:
vfs_msg->flags=1;
break;
case VFS_PUTC:
vfs_msg->flags=1;
break;
case VFS_GETC:
vfs_msg->flags=1;
break;
case VFS_CLOSE:
vfs_msg->flags=1;
break;
case VFS_MOUNT:
vfs_msg->flags=1;
break;
case VFS_UMOUNT:
vfs_msg->flags=1;
break;
default:
vfs_msg->flags=2;
break;
while (1) {
Message msg;
vfs_message* vfs_msg=get_message(&msg,box);
switch (vfs_msg->type) {
case VFS_OPEN:
vfs_msg->flags=fopen(vfs_msg,msg.from);
break;
case VFS_PUTC:
vfs_msg->flags=1;
break;
case VFS_GETC:
vfs_msg->flags=1;
break;
case VFS_CLOSE:
vfs_msg->flags=1;
break;
case VFS_MOUNT:
vfs_msg->flags=1;
break;
case VFS_UMOUNT:
vfs_msg->flags=1;
break;
default:
vfs_msg->flags=2;
break;
}
mailbox_send_msg(&msg);
yield();
}
mailbox_send_msg(&msg);
yield();
for (;;);
}