shithub: lwext4

Download patch

ref: d14de17c67922d1cf14b37356a2cad790181af29
parent: e702a129dc5b73885b53d4ab01d1d5fd4f53df1b
author: gkostka <kostka.grzegorz@gmail.com>
date: Sat Nov 8 12:03:52 EST 2014

Improved dir entry get function (ext4_dir_entry_next)

--- a/demos/generic/main.c
+++ b/demos/generic/main.c
@@ -133,7 +133,6 @@
 
 static void dir_ls(const char *path)
 {
-    int j = 0;
     char sss[255];
     ext4_dir d;
     ext4_direntry *de;
@@ -141,7 +140,7 @@
     printf("ls %s:\n", path);
 
     ext4_dir_open(&d, path);
-    de = ext4_dir_entry_get(&d, j++);
+    de = ext4_dir_entry_next(&d);
 
 
     while(de){
@@ -150,7 +149,7 @@
         printf("\t%s", entry_to_str(de->inode_type));
         printf("%s", sss);
         printf("\n");
-        de = ext4_dir_entry_get(&d, j++);
+        de = ext4_dir_entry_next(&d);
     }
     ext4_dir_close(&d);
 }
--- a/demos/stm32f429_disco/main.c
+++ b/demos/stm32f429_disco/main.c
@@ -110,7 +110,6 @@
 
 static void dir_ls(const char *path)
 {
-    int j = 0;
     char sss[255];
     ext4_dir d;
     ext4_direntry *de;
@@ -118,7 +117,7 @@
     printf("ls %s\n", path);
 
     ext4_dir_open(&d, path);
-    de = ext4_dir_entry_get(&d, j++);
+    de = ext4_dir_entry_next(&d);
 
     while(de){
         memcpy(sss, de->name, de->name_length);
@@ -126,7 +125,7 @@
         printf("  %s", entry_to_str(de->inode_type));
         printf("%s", sss);
         printf("\n");
-        de = ext4_dir_entry_get(&d, j++);
+        de = ext4_dir_entry_next(&d);
     }
     ext4_dir_close(&d);
 }
--- a/fs_test/lwext4_server.c
+++ b/fs_test/lwext4_server.c
@@ -824,8 +824,9 @@
     int idx = 0;
     ext4_direntry *d;
 
-    while(d = ext4_dir_entry_get(&dir_tab[did].fd, idx++)){
+    while(d = ext4_dir_entry_next(&dir_tab[did].fd)){
 
+        idx++;
         memcpy(name, d->name, d->name_length);
         name[d->name_length] = 0;
         if(verbose){
@@ -833,7 +834,6 @@
         }
     }
 
-    idx--;
 
     if(idx < 2){
         printf("Minumum dir entry error\n");
--- a/lwext4/ext4.c
+++ b/lwext4/ext4.c
@@ -1369,6 +1369,7 @@
 
     EXT4_MP_LOCK(mp);
     r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
+    d->next_off = 0;
     EXT4_MP_UNLOCK(mp);
     return r;
 }
@@ -1378,10 +1379,11 @@
     return ext4_fclose(&d->f);
 }
 
-ext4_direntry* ext4_dir_entry_get(ext4_dir *d, uint32_t id)
+ext4_direntry* ext4_dir_entry_next(ext4_dir *d)
 {
+#define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
+
     int r;
-    uint32_t i;
     ext4_direntry *de = 0;
     struct ext4_inode_ref dir;
     struct ext4_directory_iterator it;
@@ -1388,32 +1390,26 @@
 
     EXT4_MP_LOCK(d->f.mp);
 
+    if(d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM)
+        return 0;
+
     r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
     if(r != EOK){
         goto Finish;
     }
 
-    r = ext4_dir_iterator_init(&it, &dir, 0);
+    r = ext4_dir_iterator_init(&it, &dir, d->next_off);
     if(r != EOK){
         ext4_fs_put_inode_ref(&dir);
         goto Finish;
     }
 
-    i = 0;
-    while(r == EOK){
+    memcpy(&d->de, it.current, sizeof(ext4_direntry));
+    de = &d->de;
 
-        if(!it.current)
-            break;
+    ext4_dir_iterator_next(&it);
 
-        if(i == id){
-            memcpy(&d->de, it.current, sizeof(ext4_direntry));
-            de = &d->de;
-            break;
-        }
-
-        i++;
-        r = ext4_dir_iterator_next(&it);
-    }
+    d->next_off = it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
 
     ext4_dir_iterator_fini(&it);
     ext4_fs_put_inode_ref(&dir);
--- a/lwext4/ext4.h
+++ b/lwext4/ext4.h
@@ -147,6 +147,8 @@
     ext4_file f;
     /**@brief   Current directory entry.*/
     ext4_direntry de;
+    /**@brief   Next entry offset*/
+    uint64_t next_off;
 }ext4_dir;
 
 /********************************MOUNT OPERATIONS****************************/
@@ -337,12 +339,11 @@
  * @return  standard error code*/
 int ext4_dir_close(ext4_dir *d);
 
-
-/**@brief   Return directory entry by id.
+/**@brief   Return next directory entry.
  * @param   d directory handle
  * @param   id entry id
- * @return  directory entry id (NULL id no entry)*/
-ext4_direntry* ext4_dir_entry_get(ext4_dir *d, uint32_t id);
+ * @return  directory entry id (NULL if no entry)*/
+ext4_direntry* ext4_dir_entry_next(ext4_dir *d);
 
 #endif /* EXT4_H_ */