Skip to content

Commit 392b2ac

Browse files
committed
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.
1 parent d9a24d0 commit 392b2ac

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

lfs.c

+14-19
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,6 @@ static int lfs_dir_compact(lfs_t *lfs,
863863
break;
864864

865865
split:
866-
// TODO update dirs that get split here?
867-
868866
// commit no longer fits, need to split dir,
869867
// drop caches and create tail
870868
lfs->pcache.block = 0xffffffff;
@@ -923,6 +921,18 @@ static int lfs_dir_compact(lfs_t *lfs,
923921
}
924922
}
925923

924+
// update any dirs/files that are affected
925+
for (int i = 0; i < 2; i++) {
926+
for (lfs_file_t *f = ((lfs_file_t**)&lfs->files)[i]; f; f = f->next) {
927+
if (lfs_paircmp(f->pair, dir->pair) == 0 &&
928+
f->id >= begin && f->id < end) {
929+
f->pair[0] = dir->pair[0];
930+
f->pair[1] = dir->pair[1];
931+
f->id -= begin;
932+
}
933+
}
934+
}
935+
926936
return 0;
927937
}
928938

@@ -1052,19 +1062,17 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
10521062
}
10531063

10541064
// update any directories that are affected
1055-
// TODO what about pairs? what if we're splitting??
10561065
for (lfs_dir_t *d = lfs->dirs; d; d = d->next) {
10571066
if (lfs_paircmp(d->m.pair, dir->pair) == 0) {
10581067
d->m = *dir;
10591068
if (d->id > lfs_tagid(deletetag)) {
1060-
d->id -= 1;
10611069
d->pos -= 1;
10621070
}
10631071
}
10641072
}
10651073

1066-
for (lfs_file_t *f = lfs->files; f; f = f->next) {
1067-
if (lfs_paircmp(f->pair, dir->pair) == 0) {
1074+
for (int i = 0; i < 2; i++) {
1075+
for (lfs_file_t *f = ((lfs_file_t**)&lfs->files)[i]; f; f = f->next) {
10681076
if (f->id == lfs_tagid(deletetag)) {
10691077
f->pair[0] = 0xffffffff;
10701078
f->pair[1] = 0xffffffff;
@@ -3186,8 +3194,6 @@ static int lfs_relocate(lfs_t *lfs,
31863194
lfs->root[1] = newpair[1];
31873195
}
31883196

3189-
// TODO update dir list!!?
3190-
31913197
// clean up bad block, which should now be a desync
31923198
return lfs_deorphan(lfs);
31933199
}
@@ -3212,17 +3218,6 @@ static int lfs_relocate(lfs_t *lfs,
32123218
}
32133219
}
32143220

3215-
// shift over any dirs/files that are affected
3216-
for (int i = 0; i < 2; i++) {
3217-
for (lfs_dir_t *d = ((void*[2]){lfs->dirs, lfs->files})[i];
3218-
d; d = d->next) {
3219-
if (lfs_paircmp(d->m.pair, oldpair) == 0) {
3220-
d->m.pair[0] = newpair[0];
3221-
d->m.pair[1] = newpair[1];
3222-
}
3223-
}
3224-
}
3225-
32263221
return 0;
32273222
}
32283223

lfs.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ typedef struct lfs_cache {
295295

296296
typedef struct lfs_file {
297297
struct lfs_file *next;
298-
lfs_block_t pair[2];
299298
uint16_t id;
299+
lfs_block_t pair[2];
300300
struct lfs_ctz {
301301
lfs_block_t head;
302302
lfs_size_t size;
@@ -313,10 +313,10 @@ typedef struct lfs_file {
313313

314314
typedef struct lfs_dir {
315315
struct lfs_dir *next;
316+
uint16_t id;
316317
struct lfs_mdir m;
317318

318319
lfs_block_t head[2];
319-
uint16_t id;
320320
lfs_off_t pos;
321321
} lfs_dir_t;
322322

0 commit comments

Comments
 (0)