Browse Source

Refactored the updates of in-flight files/dirs

Updated to account for changes as a result of commits/compacts. And
changed instances of iteration over both files and dirs to use a single
nested loop.

This does rely implicitly on the structure layout of dirs/files and
their location in lfs_t, which isn't great. But it gets the job done
with less code duplication.
Christopher Haster 7 years ago
parent
commit
392b2ac79f
2 changed files with 16 additions and 21 deletions
  1. 14 19
      lfs.c
  2. 2 2
      lfs.h

+ 14 - 19
lfs.c

@@ -863,8 +863,6 @@ static int lfs_dir_compact(lfs_t *lfs,
         break;
 
 split:
-        // TODO update dirs that get split here?
-
         // commit no longer fits, need to split dir,
         // drop caches and create tail
         lfs->pcache.block = 0xffffffff;
@@ -923,6 +921,18 @@ relocate:
         }
     }
 
+    // update any dirs/files that are affected
+    for (int i = 0; i < 2; i++) {
+        for (lfs_file_t *f = ((lfs_file_t**)&lfs->files)[i]; f; f = f->next) {
+            if (lfs_paircmp(f->pair, dir->pair) == 0 &&
+                    f->id >= begin && f->id < end) {
+                f->pair[0] = dir->pair[0];
+                f->pair[1] = dir->pair[1];
+                f->id -= begin;
+            }
+        }
+    }
+
     return 0;
 }
 
@@ -1052,19 +1062,17 @@ compact:
     }
 
     // update any directories that are affected
-    // TODO what about pairs? what if we're splitting??
     for (lfs_dir_t *d = lfs->dirs; d; d = d->next) {
         if (lfs_paircmp(d->m.pair, dir->pair) == 0) {
             d->m = *dir;
             if (d->id > lfs_tagid(deletetag)) {
-                d->id -= 1;
                 d->pos -= 1;
             }
         }
     }
 
-    for (lfs_file_t *f = lfs->files; f; f = f->next) {
-        if (lfs_paircmp(f->pair, dir->pair) == 0) {
+    for (int i = 0; i < 2; i++) {
+        for (lfs_file_t *f = ((lfs_file_t**)&lfs->files)[i]; f; f = f->next) {
             if (f->id == lfs_tagid(deletetag)) {
                 f->pair[0] = 0xffffffff;
                 f->pair[1] = 0xffffffff;
@@ -3186,8 +3194,6 @@ static int lfs_relocate(lfs_t *lfs,
             lfs->root[1] = newpair[1];
         }
 
-        // TODO update dir list!!?
-
         // clean up bad block, which should now be a desync
         return lfs_deorphan(lfs);
     }
@@ -3212,17 +3218,6 @@ static int lfs_relocate(lfs_t *lfs,
         }
     }
 
-    // shift over any dirs/files that are affected
-    for (int i = 0; i < 2; i++) {
-        for (lfs_dir_t *d = ((void*[2]){lfs->dirs, lfs->files})[i];
-                d; d = d->next) {
-            if (lfs_paircmp(d->m.pair, oldpair) == 0) {
-                d->m.pair[0] = newpair[0];
-                d->m.pair[1] = newpair[1];
-            }
-        }
-    }
-
     return 0;
 }
 

+ 2 - 2
lfs.h

@@ -295,8 +295,8 @@ typedef struct lfs_cache {
 
 typedef struct lfs_file {
     struct lfs_file *next;
-    lfs_block_t pair[2];
     uint16_t id;
+    lfs_block_t pair[2];
     struct lfs_ctz {
         lfs_block_t head;
         lfs_size_t size;
@@ -313,10 +313,10 @@ typedef struct lfs_file {
 
 typedef struct lfs_dir {
     struct lfs_dir *next;
+    uint16_t id;
     struct lfs_mdir m;
 
     lfs_block_t head[2];
-    uint16_t id;
     lfs_off_t pos;
 } lfs_dir_t;