Convert file descriptors to generic streams

This commit is contained in:
2026-06-18 21:46:18 +03:00
parent bcf2e5bac6
commit 9761e3e870
20 changed files with 150 additions and 79 deletions
+40
View File
@@ -2,6 +2,7 @@
#include "src/kernel/ata.h"
#include "src/kernel/fs.h"
#include "src/kernel/panic.h"
#include "src/kernel/stream.h"
#include "src/lib/memory.h"
#include "src/lib/string.h"
#include "src/lib/util.h"
@@ -200,6 +201,45 @@ fs_node_t *fat16_open_again(const fs_node_t *source) {
return result;
}
typedef struct {
stream_t stream;
fs_node_t *node;
uint64_t offset;
} fat16_stream_t;
static uint64_t stream_write(__attribute__((unused)) const stream_t *self, __attribute__((unused)) const char *from,
__attribute__((unused)) uint64_t bytes) {
return (uint64_t)-1;
}
static uint64_t stream_read(const stream_t *self, uint64_t max, char *to) {
fat16_stream_t *fss = (fat16_stream_t *)self;
if (fss->offset >= fss->node->size) {
return 0;
}
uint64_t remaining = fss->node->size - fss->offset;
uint64_t to_read = remaining > max ? max : remaining;
fat16_read(fss->node, fss->offset, to_read, to);
fss->offset += to_read;
return to_read;
}
static void stream_close(stream_t *self) {
fat16_stream_t *fss = (fat16_stream_t *)self;
fat16_close(fss->node);
memory_free(self);
}
stream_t *fat16_open_stream(const fs_node_t *source) {
fat16_stream_t *result = memory_allocate(sizeof(fat16_node_t));
result->stream.read = stream_read;
result->stream.write = stream_write;
result->stream.close = stream_close;
result->node = fat16_open_again(source);
result->offset = 0;
return (stream_t *)result;
}
void fat16_read(const fs_node_t *file, uint64_t offset, uint64_t size, void *to) {
ASSERT(file->type == FAT16, "fat16_read: file is not FAT16");
ASSERT(!file->is_dir, "fat16_read: can not read directory");