Framework for exporting system header to user space (#59)
[lunaix-os.git] / lunaix-os / kernel / fs / iso9660 / directory.c
index 5a06fc8b39eab5308d975ee4565359e0d0a6e530..a4dcb10ba32b5a0a0906ecd3e8e305ecc1552ebe 100644 (file)
@@ -1,12 +1,12 @@
 #include <lunaix/fs.h>
-#include <lunaix/fs/iso9660.h>
+#include "iso9660.h"
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/spike.h>
 
 #include <klibc/string.h>
 
-#include <usr/lunaix/dirent_defs.h>
+#include <usr/lunaix/dirent.h>
 
 extern struct cake_pile* drec_cache_pile;
 
@@ -47,26 +47,41 @@ iso9660_fill_drecache(struct iso_drecache* cache,
     }
 
 done:
-    if (!cache->name.len) {
-        // Load ISO9660 file id if no NM found.
-        u32_t l = drec->name.len;
-        while (l < (u32_t)-1 && drec->name.content[l--] != ';')
-            ;
+    if (cache->name.len) {
+        return;
+    }
 
-        l = (l + 1) ? l : drec->name.len;
-        l = MIN(l, ISO9660_IDLEN - 1);
+    // Load ISO9660 file id if no NM found.
 
-        strncpy(cache->name_val, (const char*)drec->name.content, l);
+    ;
+    char name_val = drec->name.content[0];
+    u32_t l = drec->name.len;
 
-        cache->name = HSTR(cache->name_val, l);
-        hstr_rehash(&cache->name, HSTR_FULL_HASH);
+    if (l == 1 && !name_val) {
+        cache->name = vfs_dot;
+        return;
+    }
+    
+    if(l == 1 && name_val == 1) {
+        cache->name = vfs_ddot;
+        return;
     }
+    
+    while (l < (u32_t)-1 && drec->name.content[l--] != ';')
+        ;
+
+    l = (l + 1) ? l : drec->name.len;
+    l = MIN(l, ISO9660_IDLEN - 1);
+
+    strncpy(cache->name_val, (const char*)drec->name.content, l);
+    cache->name = HSTR(cache->name_val, l);
+    hstr_rehash(&cache->name, HSTR_FULL_HASH);
 }
 
 int
 iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
 {
-    if (!(inode->itype & VFS_IFDIR)) {
+    if (!check_directory_node(inode)) {
         vfs_assign_inode(dnode, inode);
         return 0;
     }
@@ -88,7 +103,8 @@ iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
     do {
         if (blk_offset >= ISO9660_BLKSZ - sizeof(struct iso_drecord)) {
             current_pos += ISO9660_BLKSZ;
-            errno = dev->read(dev, records, blk + current_pos, ISO9660_BLKSZ);
+            errno =
+              dev->ops.read(dev, records, blk + current_pos, ISO9660_BLKSZ);
             if (errno < 0) {
                 errno = EIO;
                 goto done;
@@ -103,16 +119,11 @@ iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
             break;
         }
 
-        // ignore the '.', '..' as we have built-in support
-        if (drec->name.len == 1) {
-            goto cont;
-        }
-
         struct iso_drecache* cache = cake_grab(drec_cache_pile);
 
         iso9660_fill_drecache(cache, drec, mdu->len);
         llist_append(&isoino->drecaches, &cache->caches);
-    cont:
+
         blk_offset += mdu->len;
     } while (current_pos + blk_offset < max_pos);
 
@@ -165,19 +176,20 @@ __get_dtype(struct iso_drecache* pos)
 
 int
 iso9660_readdir(struct v_file* file, struct dir_context* dctx)
-{
+{    
     struct llist_header* lead = file->dnode->data;
     struct iso_drecache *pos, *n;
-    u32_t counter = dctx->index - 1;
+    u32_t counter = 0;
 
     llist_for_each(pos, n, lead, caches)
     {
-        if (counter == (u32_t)-1 && !(pos->flags & ISO_FHIDDEN)) {
+        if (counter == file->f_pos && !(pos->flags & ISO_FHIDDEN)) {
             dctx->read_complete_callback(
-              dctx, pos->name_val, pos->name.len, __get_dtype(pos));
+              dctx, HSTR_VAL(pos->name), HSTR_LEN(pos->name), __get_dtype(pos));
             return 1;
         }
-        counter--;
+        counter++;
     }
+
     return 0;
 }
\ No newline at end of file