k-tana-zero/k/iso.c

74 lines
1.6 KiB
C

#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;
}