feat(iso): add open function

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

73
k/iso.c Normal file

@ -0,0 +1,73 @@
#include <k/iso9660.h>
#include <k/types.h>
#include "atapi.h"
#include "fd.h"
#include "iso.h"
#include "stdio.h"
#include "string.h"
static u32 get_filename_length(const char *path, u32 *index)
{
u32 len = 0;
for (; path[*index]; ++(*index))
{
if (path[*index] != '/')
++len;
else {
while (path[*index] == '/')
++(*index);
break;
}
}
return len;
}
int open(const char *pathname, int flags)
{
if (flags != O_RDONLY)
return -1;
struct iso_prim_voldesc *iso_prim_block = read_block(ISO_PRIM_VOLDESC_BLOCK);
if (strncmp(iso_prim_block->std_identifier, "CD001", 5))
return -1;
struct iso_dir *root = &iso_prim_block->root_dir;
struct iso_dir *curr = read_block(root->data_blk.le);
u32 index = 0;
while (pathname[index] == '/')
++index;
for (int depth = 0; depth < MAX_DIR_DEPTH; ++depth)
{
char found = 0;
u32 start = index;
u32 length = get_filename_length(pathname, &index);
// we found the file
if (!length)
return register_fd(curr->data_blk.le);
// iteration for one directory
while (curr->dir_size != 0)
{
if (length == curr->idf_len &&
!strncmp(pathname + start, curr->idf, length))
{
curr = read_block(curr->data_blk.le);
found = 1;
break;
}
else
curr = (void *)curr + curr->dir_size;
}
if (!found)
return -1;
}
return -1;
}

10
k/iso.h Normal file

@ -0,0 +1,10 @@
#ifndef ISO_H
#define ISO_H
#include <k/types.h>
#define O_RDONLY 0
int open(const char *path, int flags);
#endif /* !ISO_H */