Added lfsr_mid_isopened to dedup zombie/orphan checks

I think this also makes the logic easier to read. Less temporary
variables.

            code          stack
  before:  33816           2944
  after:   33812 (-0.0%)   2944 (+0.0%)
This commit is contained in:
Christopher Haster
2024-01-18 12:20:33 -06:00
parent 749f540a2b
commit 033d5545e9
+28 -49
View File
@@ -5144,6 +5144,18 @@ static void lfsr_removeopened(lfs_t *lfs, lfsr_opened_t *opened) {
} }
} }
static bool lfsr_mid_isopened(lfs_t *lfs, lfsr_smid_t mid) {
for (lfsr_opened_t *p = lfs->opened; p; p = p->next) {
// we really only care about regular open files here, all
// others are either transient (dirs) or fake (orphans)
if (p->type == LFS_TYPE_REG && p->mdir.mid == mid) {
return true;
}
}
return false;
}
/// Metadata-tree things /// /// Metadata-tree things ///
@@ -8236,18 +8248,7 @@ static int lfsr_fs_fixorphans(lfs_t *lfs) {
while (true) { while (true) {
// is this mid opened? skip // is this mid opened? skip
bool notopened = true; if (!lfsr_mid_isopened(lfs, mdir.mid)) {
for (lfsr_opened_t *opened = lfs->opened;
opened;
opened = opened->next) {
if (opened->type == LFS_TYPE_REG
&& opened->mdir.mid == mdir.mid) {
notopened = false;
break;
}
}
if (notopened) {
// are we an orphaned file? // are we an orphaned file?
err = lfsr_mdir_lookup(lfs, &mdir, mdir.mid, LFSR_TAG_ORPHAN, err = lfsr_mdir_lookup(lfs, &mdir, mdir.mid, LFSR_TAG_ORPHAN,
NULL); NULL);
@@ -8577,16 +8578,7 @@ int lfsr_remove(lfs_t *lfs, const char *path) {
} }
// are we removing an opened file? // are we removing an opened file?
bool zombie = false; bool zombie = lfsr_mid_isopened(lfs, mdir.mid);
for (lfsr_opened_t *opened = lfs->opened;
opened;
opened = opened->next) {
if (opened->type == LFS_TYPE_REG
&& opened->mdir.mid == mdir.mid) {
zombie = true;
break;
}
}
// remove the metadata entry // remove the metadata entry
err = lfsr_mdir_commit(lfs, &mdir, LFSR_ATTRS( err = lfsr_mdir_commit(lfs, &mdir, LFSR_ATTRS(
@@ -9376,35 +9368,22 @@ int lfsr_file_close(lfs_t *lfs, lfsr_file_t *file) {
lfs_free(file->buffer); lfs_free(file->buffer);
} }
// never synced? // are we orphaning a file?
if (lfsr_f_isorphan(file->flags)) { //
// are we orphaning a file? // make sure we check _after_ removing ourselves
// if (lfsr_f_isorphan(file->flags)
// make sure we check _after_ removing ourselves && !lfsr_mid_isopened(lfs, file->mdir.mid)) {
bool orphaned = true; // this gets a bit tricky, since we're not able to write to the
for (lfsr_opened_t *opened = lfs->opened; // filesystem if we're rdonly or desynced, fortunately we have
opened; // a few tricks
opened = opened->next) {
if (opened->type == LFS_TYPE_REG
&& opened->mdir.mid == file->mdir.mid) {
orphaned = false;
break;
}
}
if (orphaned) { // first try to push onto our grm queue
// this gets a bit tricky, since we're not able to write to the if (lfsr_grm_count(&lfs->grm) < 2) {
// filesystem if we're rdonly or desynced, fortunately we have lfsr_grm_pushrm(&lfs->grm, file->mdir.mid);
// a few tricks
// first try to push onto our grm queue // fallback to just marking the filesystem as orphaned
if (lfsr_grm_count(&lfs->grm) < 2) { } else {
lfsr_grm_pushrm(&lfs->grm, file->mdir.mid); lfs->hasorphans = true;
// fallback to just marking the filesystem as orphaned
} else {
lfs->hasorphans = true;
}
} }
} }