diff --git a/k/iso.c b/k/iso.c new file mode 100644 index 0000000..38111fd --- /dev/null +++ b/k/iso.c @@ -0,0 +1,73 @@ +#include + +#include + +#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; +} diff --git a/k/iso.h b/k/iso.h new file mode 100644 index 0000000..3445b6a --- /dev/null +++ b/k/iso.h @@ -0,0 +1,10 @@ +#ifndef ISO_H +#define ISO_H + +#include + +#define O_RDONLY 0 + +int open(const char *path, int flags); + +#endif /* !ISO_H */