:: commit bc7f7277cc6238e89068da54a5d6be176094b4cf

mintsuki <mintsuki@protonmail.com> — 2020-04-22 13:48

parents: d7c6aa5a9e

Reorganise ext2 directory code

diff --git a/src/fs/ext2fs.c b/src/fs/ext2fs.c
index a2917a5a..6d0a1541 100644
--- a/src/fs/ext2fs.c
+++ b/src/fs/ext2fs.c
@@ -1,4 +1,10 @@
+#include <stdint.h>

+#include <stddef.h>

+#include <stdbool.h>

 #include <fs/ext2fs.h>

+#include <drivers/disk.h>

+#include <lib/libc.h>

+#include <lib/blib.h>

 

 /* EXT2 Filesystem States */

 #define EXT2_FS_CLEAN   1

@@ -201,57 +207,52 @@ static int ext2fs_get_inode(struct ext2fs_inode *ret, uint64_t drive, struct par
 }

 

 static int ext2fs_parse_dirent(struct ext2fs_dir_entry *dir, struct ext2fs_file_handle *fd, struct ext2fs_superblock *sb, const char *path) {

-    int path_len = strlen(path);

+    if (*path == '/')

+        path++;

 

-    char *cpy = path;

+    struct ext2fs_inode current_inode = fd->root_inode;

 

-    if (*cpy == '/')

-        cpy++;

+    bool escape = false;

 

-    int token_count;

-    for (int i = 0; i < path_len; i++) {

-        if (cpy[i] == '/')

-            token_count++;

-    }

+    char token[128] = {0};

 

-    const char *delimiter = "/";

-    char *token;

-    struct ext2fs_inode *current = &fd->root_inode;

+next:

+    for (size_t i = 0; *path != '/' && *path != '\0'; i++, path++)

+        token[i] = *path;

 

-    for (int i = 0; i < (token_count + 1); i++) {

-        token = strtok(cpy, delimiter);

+    if (*path == '\0')

+        escape = true;

+    else

+        path++;

 

-        uint64_t offset = current->i_blocks[0] * fd->block_size;

+    uint64_t offset = current_inode.i_blocks[0] * fd->block_size;

 

-        while (offset < current->i_size + offset) {

-            // preliminary read

-            read_partition(fd->drive, &fd->part, dir, offset, sizeof(struct ext2fs_dir_entry));

+    while (offset < current_inode.i_size + offset) {

+        // preliminary read

+        read_partition(fd->drive, &fd->part, dir, offset, sizeof(struct ext2fs_dir_entry));

 

-            // name read

-            char* name = balloc(dir->name_len);

-            read_partition(fd->drive, &fd->part, name, offset + sizeof(struct ext2fs_dir_entry), dir->name_len);

+        // name read

+        char* name = balloc(dir->name_len);

+        read_partition(fd->drive, &fd->part, name, offset + sizeof(struct ext2fs_dir_entry), dir->name_len);

 

-            int r = strncmp(token, name, dir->name_len);

+        int r = strncmp(token, name, dir->name_len);

 

-            brewind(dir->name_len);

+        brewind(dir->name_len);

 

-            if (!r) {

-                if (i == token_count)

-                    return 0;

-                else

-                    break;

+        if (!r) {

+            if (escape) {

+                return 0;

+            } else {

+                // update the current inode

+                ext2fs_get_inode(&current_inode, fd->drive, &fd->part, dir->inode, sb);

+                goto next;

             }

-

-            offset += dir->rec_len;

         }

 

-        cpy = NULL;

-

-        // update the current inode

-        ext2fs_get_inode(current, fd->drive, &fd->part, dir->inode, sb);

+        offset += dir->rec_len;

     }

 

-    return 1;

+    return -1;

 }

 

 int ext2fs_open(struct ext2fs_file_handle *ret, int drive, int partition, const char *path) {

diff --git a/src/fs/ext2fs.h b/src/fs/ext2fs.h
index 2faf8e30..77609397 100644
--- a/src/fs/ext2fs.h
+++ b/src/fs/ext2fs.h
@@ -3,10 +3,6 @@
 

 #include <stdint.h>

 #include <stddef.h>

-#include <stdbool.h>

-#include <drivers/disk.h>

-#include <lib/libc.h>

-#include <lib/blib.h>

 #include <lib/part.h>

 

 /* EXT2 OS Specific Value 2 (only Linux support) */

diff --git a/src/lib/libc.c b/src/lib/libc.c
index f6ee8b32..28674fca 100644
--- a/src/lib/libc.c
+++ b/src/lib/libc.c
@@ -111,36 +111,3 @@ size_t strlen(const char *str) {
 
     return len;
 }
-
-char *strtok(char *str, const char *delimiter) {
-    static char* buffer;
-
-    if (str != NULL) {
-        buffer = str;
-    }
-
-    if (buffer[0] == '\0') {
-        return NULL;
-    }
-
-    char* ret = buffer;
-
-    for (char* b = buffer; *b != '\0'; b++) {
-        for (const char* d = delimiter; *d != '\0'; d++) {
-            if(*b == *d) {
-                *b     = '\0';
-                buffer = b + 1;
-
-                // Skip the beginning delimiters
-                if (b == ret) {
-                    ret++;
-                    continue;
-                }
-
-                return ret;
-            }
-        }
-    }
-
-    return ret;
-}
diff --git a/src/lib/libc.h b/src/lib/libc.h
index 2035ac41..9699f4ae 100644
--- a/src/lib/libc.h
+++ b/src/lib/libc.h
@@ -13,6 +13,5 @@ char *strncpy(char *, const char *, size_t);
 size_t strlen(const char *);
 int strcmp(const char *, const char *);
 int strncmp(const char *, const char *, size_t);
-char *strtok(char *str, const char *delimiter);
 
 #endif
tab: 248 wrap: offon