summaryrefslogtreecommitdiff
path: root/src/user/lib/fs/dir.c
diff options
context:
space:
mode:
authordzwdz2022-07-30 20:18:51 +0200
committerdzwdz2022-07-30 20:18:51 +0200
commitfdeac40f867ca0c43ade53cce4b77bb146b10aae (patch)
tree9845e8708cb12d7e27a633d37a83b40eab7ea396 /src/user/lib/fs/dir.c
parentb26399ad055aae9ef4b01694967515235568cd85 (diff)
user/fs: create a shared library for handling directory reads
Diffstat (limited to 'src/user/lib/fs/dir.c')
-rw-r--r--src/user/lib/fs/dir.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/user/lib/fs/dir.c b/src/user/lib/fs/dir.c
new file mode 100644
index 0000000..25ae606
--- /dev/null
+++ b/src/user/lib/fs/dir.c
@@ -0,0 +1,37 @@
+#include <errno.h>
+#include <string.h>
+#include <user/lib/fs/dir.h>
+
+void dir_start(struct dirbuild *db, long offset, char *buf, size_t buflen) {
+ db->offset = offset;
+ db->buf = buf;
+ db->bpos = 0;
+ db->blen = buflen;
+ db->error = 0;
+
+ if (offset < 0)
+ db->error = -ENOSYS; // TODO
+}
+
+bool dir_append(struct dirbuild *db, const char *name) {
+ if (db->error) return true;
+
+ long len = strlen(name) + 1;
+
+ if (db->offset < len) {
+ name += db->offset;
+ len -= db->offset;
+ db->offset = 0;
+
+ // TODO no buffer overrun check
+ memcpy(db->buf + db->bpos, name, len);
+ db->bpos += len;
+ } else {
+ db->offset -= len;
+ }
+ return false;
+}
+
+long dir_finish(struct dirbuild *db) {
+ return db->error ? db->error : db->bpos;
+}