|
|
|
|
@ -205,39 +205,80 @@ typedef struct { |
|
|
|
|
stream_t stream; |
|
|
|
|
fs_node_t *node; |
|
|
|
|
uint64_t offset; |
|
|
|
|
} fat16_stream_t; |
|
|
|
|
} fat16_file_stream_t; |
|
|
|
|
|
|
|
|
|
static uint64_t stream_write(__attribute__((unused)) const stream_t *self, __attribute__((unused)) const char *from, |
|
|
|
|
static uint64_t file_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) { |
|
|
|
|
static uint64_t file_stream_read(const stream_t *self, uint64_t max, char *to) { |
|
|
|
|
fat16_file_stream_t *ffs = (fat16_file_stream_t *)self; |
|
|
|
|
if (ffs->offset >= ffs->node->size) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
uint64_t remaining = fss->node->size - fss->offset; |
|
|
|
|
uint64_t remaining = ffs->node->size - ffs->offset; |
|
|
|
|
uint64_t to_read = remaining > max ? max : remaining; |
|
|
|
|
fat16_read(fss->node, fss->offset, to_read, to); |
|
|
|
|
fss->offset += to_read; |
|
|
|
|
fat16_read(ffs->node, ffs->offset, to_read, to); |
|
|
|
|
ffs->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); |
|
|
|
|
static void file_stream_close(stream_t *self) { |
|
|
|
|
fat16_file_stream_t *ffs = (fat16_file_stream_t *)self; |
|
|
|
|
fat16_close(ffs->node); |
|
|
|
|
memory_free(self); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
typedef struct { |
|
|
|
|
stream_t stream; |
|
|
|
|
fs_node_t *node; |
|
|
|
|
uint64_t index; |
|
|
|
|
} fat16_directory_stream_t; |
|
|
|
|
|
|
|
|
|
static uint64_t directory_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 directory_stream_read(const stream_t *self, uint64_t max, char *to) { |
|
|
|
|
fat16_directory_stream_t *fds = (fat16_directory_stream_t *)self; |
|
|
|
|
fs_node_t *node = fat16_open_at(fds->node, fds->index++); |
|
|
|
|
if (!node) { |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
uint64_t length = string_length(node->name); |
|
|
|
|
uint64_t to_read = length + 1 > max ? max : length + 1; |
|
|
|
|
memory_copy(node->name, to_read, to); |
|
|
|
|
to[max - 1] = '\0'; |
|
|
|
|
|
|
|
|
|
return to_read; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void directory_stream_close(stream_t *self) { |
|
|
|
|
fat16_directory_stream_t *fds = (fat16_directory_stream_t *)self; |
|
|
|
|
fat16_close(fds->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; |
|
|
|
|
if (!source->is_dir) { |
|
|
|
|
fat16_file_stream_t *result = memory_allocate(sizeof(fat16_file_stream_t)); |
|
|
|
|
result->stream.read = file_stream_read; |
|
|
|
|
result->stream.write = file_stream_write; |
|
|
|
|
result->stream.close = file_stream_close; |
|
|
|
|
result->node = fat16_open_again(source); |
|
|
|
|
result->offset = 0; |
|
|
|
|
return (stream_t *)result; |
|
|
|
|
} else { |
|
|
|
|
fat16_directory_stream_t *result = memory_allocate(sizeof(fat16_directory_stream_t)); |
|
|
|
|
result->stream.read = directory_stream_read; |
|
|
|
|
result->stream.write = directory_stream_write; |
|
|
|
|
result->stream.close = directory_stream_close; |
|
|
|
|
result->node = fat16_open_again(source); |
|
|
|
|
result->index = 0; |
|
|
|
|
return (stream_t *)result; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void fat16_read(const fs_node_t *file, uint64_t offset, uint64_t size, void *to) { |
|
|
|
|
|