Skip to content

Commit 0e1022a

Browse files
committed
Fixed missing erase during file relocation
This was an easy fix, but highlighted the fact that the current testing framework doesn't detect when a block is written to without an associated erase. Added a quick solution that creates an empty file during an erase.
1 parent a1138a4 commit 0e1022a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

emubd/lfs_emubd.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
127127
snprintf(emu->child, LFS_NAME_MAX, "%x", block);
128128

129129
FILE *f = fopen(emu->path, "r+b");
130-
if (!f && errno == ENOENT) {
131-
f = fopen(emu->path, "w+b");
132-
if (!f) {
133-
return -errno;
134-
}
130+
if (!f && errno != ENOENT) {
131+
return -errno;
135132
}
136133

134+
// Check that file was erased
135+
assert(f);
136+
137137
int err = fseek(f, off, SEEK_SET);
138138
if (err) {
139139
return -errno;
@@ -185,6 +185,18 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
185185
}
186186
}
187187

188+
if (err || S_ISREG(st.st_mode)) {
189+
FILE *f = fopen(emu->path, "w");
190+
if (!f) {
191+
return -errno;
192+
}
193+
194+
err = fclose(f);
195+
if (err) {
196+
return -errno;
197+
}
198+
}
199+
188200
emu->stats.erase_count += 1;
189201
return 0;
190202
}

lfs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,14 @@ static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
11961196
return err;
11971197
}
11981198

1199+
err = lfs_bd_erase(lfs, nblock);
1200+
if (err) {
1201+
if (err == LFS_ERR_CORRUPT) {
1202+
goto relocate;
1203+
}
1204+
return err;
1205+
}
1206+
11991207
// either read from dirty cache or disk
12001208
for (lfs_off_t i = 0; i < file->off; i++) {
12011209
uint8_t data;

0 commit comments

Comments
 (0)