From b2eb2df8e93593ca88e54c1b0f7c177718795f1d Mon Sep 17 00:00:00 2001 From: Malo Lecomte Date: Sun, 24 Apr 2022 02:18:17 +0200 Subject: [PATCH] feat(iso): add read function --- k/iso.c | 33 ++++++++++++++++++++++++++++++++- k/iso.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/k/iso.c b/k/iso.c index c9bc8ca..76fffa9 100644 --- a/k/iso.c +++ b/k/iso.c @@ -49,7 +49,10 @@ int open(const char *pathname, int flags) // we found the file if (!length) - return register_fd(curr->data_blk.le, curr->data_size.le); + { + return register_fd(curr->data_blk.le, curr->file_size.le, + (curr->type == ISO_FILE_ISDIR)); + } // iteration for one directory while (curr->dir_size != 0) @@ -72,6 +75,34 @@ int open(const char *pathname, int flags) return -1; } +s64 read(int fd, void *buf, size_t count) +{ + if (fd > MAX_FD) + return -1; + + struct file_entry *f = &fd_table[fd]; + if (!f->open) + return -1; + + u32 nb_block = f->file_offset / ISO_BLOCK_SIZE; + char *content = read_block(f->lba + nb_block); + + size_t i = 0; + for (; i < count && f->file_offset < f->file_size; ++i) + { + ((char *)buf)[i] = content[f->file_offset]; + ++(f->file_offset); + + if (f->file_offset / ISO_BLOCK_SIZE > nb_block) + { + ++nb_block; + content = read_block(f->lba + nb_block); + } + } + + return i; +} + int close(int fd) { return destroy_fd(fd); diff --git a/k/iso.h b/k/iso.h index 3445b6a..85dc524 100644 --- a/k/iso.h +++ b/k/iso.h @@ -6,5 +6,7 @@ #define O_RDONLY 0 int open(const char *path, int flags); +s64 read(int fd, void *buf, size_t count); +int close(int fd); #endif /* !ISO_H */