Skip to content

Commit 0f4e334

Browse files
committed
Squashed 'littlefs/' changes from 3f31c8c..78c79ec
78c79ec Added QUIET flag to tests so CI is readable f9f4f5c Fixed standard name mismatch LFS_ERR_EXISTS -> LFS_ERR_EXIST 843e3c6 Added sticky-bit for preventing file syncs after write errors 2612e1b Modified lfs_ctz_extend to be a little bit safer 6664723 Fixed issue with committing directories to bad-blocks that are stuck git-subtree-dir: littlefs git-subtree-split: 78c79ec
1 parent 3778759 commit 0f4e334

File tree

8 files changed

+78
-40
lines changed

8 files changed

+78
-40
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ script:
99
-include stdio.h -Werror' make all size
1010

1111
# run tests
12-
- make test
12+
- make test QUIET=1
1313

1414
# run tests with a few different configurations
15-
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test
16-
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test
17-
- CFLAGS="-DLFS_BLOCK_COUNT=1023" make test
18-
- CFLAGS="-DLFS_LOOKAHEAD=2048" make test
15+
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test QUIET=1
16+
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test QUIET=1
17+
- CFLAGS="-DLFS_BLOCK_COUNT=1023" make test QUIET=1
18+
- CFLAGS="-DLFS_LOOKAHEAD=2048" make test QUIET=1
1919

2020
# self-host with littlefs-fuse for fuzz test
2121
- make -C littlefs-fuse
@@ -28,7 +28,7 @@ script:
2828
- cp -r $(git ls-tree --name-only HEAD) mount/littlefs
2929
- cd mount/littlefs
3030
- ls
31-
- make -B test_dirs
31+
- make -B test_dirs QUIET=1
3232

3333
before_install:
3434
- fusermount -V

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ size: $(OBJ)
3434
test: test_format test_dirs test_files test_seek test_parallel \
3535
test_alloc test_paths test_orphan test_move test_corrupt
3636
test_%: tests/test_%.sh
37+
ifdef QUIET
38+
./$< | sed '/^[^-=]/d'
39+
else
3740
./$<
41+
endif
3842

3943
-include $(DEP)
4044

emubd/lfs_emubd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
138138
snprintf(emu->child, LFS_NAME_MAX, "%x", block);
139139

140140
FILE *f = fopen(emu->path, "r+b");
141-
if (!f && errno != ENOENT) {
142-
return -errno;
141+
if (!f) {
142+
return (errno == EACCES) ? 0 : -errno;
143143
}
144144

145145
// Check that file was erased
@@ -189,14 +189,14 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
189189
return -errno;
190190
}
191191

192-
if (!err && S_ISREG(st.st_mode)) {
192+
if (!err && S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode)) {
193193
int err = unlink(emu->path);
194194
if (err) {
195195
return -errno;
196196
}
197197
}
198198

199-
if (err || S_ISREG(st.st_mode)) {
199+
if (errno == ENOENT || (S_ISREG(st.st_mode) && (S_IWUSR & st.st_mode))) {
200200
FILE *f = fopen(emu->path, "w");
201201
if (!f) {
202202
return -errno;

lfs.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -531,18 +531,19 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_dir_t *dir,
531531
}
532532

533533
// successful commit, check checksum to make sure
534-
crc = 0xffffffff;
534+
uint32_t ncrc = 0xffffffff;
535535
err = lfs_bd_crc(lfs, dir->pair[0], 0,
536-
0x7fffffff & dir->d.size, &crc);
536+
(0x7fffffff & dir->d.size)-4, &ncrc);
537537
if (err) {
538538
return err;
539539
}
540540

541-
if (crc == 0) {
542-
break;
541+
if (ncrc != crc) {
542+
goto relocate;
543543
}
544544
}
545545

546+
break;
546547
relocate:
547548
//commit was corrupted
548549
LFS_DEBUG("Bad block at %d", dir->pair[0]);
@@ -818,7 +819,7 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
818819
lfs_entry_t entry;
819820
err = lfs_dir_find(lfs, &cwd, &entry, &path);
820821
if (err != LFS_ERR_NOENT || strchr(path, '/') != NULL) {
821-
return err ? err : LFS_ERR_EXISTS;
822+
return err ? err : LFS_ERR_EXIST;
822823
}
823824

824825
// build up new directory
@@ -1053,17 +1054,18 @@ static int lfs_ctz_find(lfs_t *lfs,
10531054
static int lfs_ctz_extend(lfs_t *lfs,
10541055
lfs_cache_t *rcache, lfs_cache_t *pcache,
10551056
lfs_block_t head, lfs_size_t size,
1056-
lfs_off_t *block, lfs_block_t *off) {
1057+
lfs_block_t *block, lfs_off_t *off) {
10571058
while (true) {
1058-
if (true) {
1059-
// go ahead and grab a block
1060-
int err = lfs_alloc(lfs, block);
1061-
if (err) {
1062-
return err;
1063-
}
1064-
assert(*block >= 2 && *block <= lfs->cfg->block_count);
1059+
// go ahead and grab a block
1060+
lfs_block_t nblock;
1061+
int err = lfs_alloc(lfs, &nblock);
1062+
if (err) {
1063+
return err;
1064+
}
1065+
assert(nblock >= 2 && nblock <= lfs->cfg->block_count);
10651066

1066-
err = lfs_bd_erase(lfs, *block);
1067+
if (true) {
1068+
err = lfs_bd_erase(lfs, nblock);
10671069
if (err) {
10681070
if (err == LFS_ERR_CORRUPT) {
10691071
goto relocate;
@@ -1072,6 +1074,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
10721074
}
10731075

10741076
if (size == 0) {
1077+
*block = nblock;
10751078
*off = 0;
10761079
return 0;
10771080
}
@@ -1091,7 +1094,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
10911094
}
10921095

10931096
err = lfs_cache_prog(lfs, pcache, rcache,
1094-
*block, i, &data, 1);
1097+
nblock, i, &data, 1);
10951098
if (err) {
10961099
if (err == LFS_ERR_CORRUPT) {
10971100
goto relocate;
@@ -1100,6 +1103,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
11001103
}
11011104
}
11021105

1106+
*block = nblock;
11031107
*off = size;
11041108
return 0;
11051109
}
@@ -1110,7 +1114,7 @@ static int lfs_ctz_extend(lfs_t *lfs,
11101114

11111115
for (lfs_off_t i = 0; i < skips; i++) {
11121116
int err = lfs_cache_prog(lfs, pcache, rcache,
1113-
*block, 4*i, &head, 4);
1117+
nblock, 4*i, &head, 4);
11141118
if (err) {
11151119
if (err == LFS_ERR_CORRUPT) {
11161120
goto relocate;
@@ -1129,12 +1133,13 @@ static int lfs_ctz_extend(lfs_t *lfs,
11291133
assert(head >= 2 && head <= lfs->cfg->block_count);
11301134
}
11311135

1136+
*block = nblock;
11321137
*off = 4*skips;
11331138
return 0;
11341139
}
11351140

11361141
relocate:
1137-
LFS_DEBUG("Bad block at %d", *block);
1142+
LFS_DEBUG("Bad block at %d", nblock);
11381143

11391144
// just clear cache and try a new block
11401145
pcache->block = 0xffffffff;
@@ -1214,7 +1219,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
12141219
} else if (entry.d.type == LFS_TYPE_DIR) {
12151220
return LFS_ERR_ISDIR;
12161221
} else if (flags & LFS_O_EXCL) {
1217-
return LFS_ERR_EXISTS;
1222+
return LFS_ERR_EXIST;
12181223
}
12191224

12201225
// setup file struct
@@ -1398,7 +1403,9 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
13981403
return err;
13991404
}
14001405

1401-
if ((file->flags & LFS_F_DIRTY) && !lfs_pairisnull(file->pair)) {
1406+
if ((file->flags & LFS_F_DIRTY) &&
1407+
!(file->flags & LFS_F_ERRED) &&
1408+
!lfs_pairisnull(file->pair)) {
14021409
// update dir entry
14031410
lfs_dir_t cwd;
14041411
int err = lfs_dir_fetch(lfs, &cwd, file->pair);
@@ -1532,6 +1539,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
15321539
file->head, file->size,
15331540
file->pos-1, &file->block, &file->off);
15341541
if (err) {
1542+
file->flags |= LFS_F_ERRED;
15351543
return err;
15361544
}
15371545

@@ -1545,6 +1553,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
15451553
file->block, file->pos,
15461554
&file->block, &file->off);
15471555
if (err) {
1556+
file->flags |= LFS_F_ERRED;
15481557
return err;
15491558
}
15501559

@@ -1560,13 +1569,15 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
15601569
if (err == LFS_ERR_CORRUPT) {
15611570
goto relocate;
15621571
}
1572+
file->flags |= LFS_F_ERRED;
15631573
return err;
15641574
}
15651575

15661576
break;
15671577
relocate:
15681578
err = lfs_file_relocate(lfs, file);
15691579
if (err) {
1580+
file->flags |= LFS_F_ERRED;
15701581
return err;
15711582
}
15721583
}
@@ -1579,6 +1590,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
15791590
lfs_alloc_ack(lfs);
15801591
}
15811592

1593+
file->flags &= ~LFS_F_ERRED;
15821594
return size;
15831595
}
15841596

lfs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ enum lfs_error {
4545
LFS_ERR_IO = -5, // Error during device operation
4646
LFS_ERR_CORRUPT = -52, // Corrupted
4747
LFS_ERR_NOENT = -2, // No directory entry
48-
LFS_ERR_EXISTS = -17, // Entry already exists
48+
LFS_ERR_EXIST = -17, // Entry already exists
4949
LFS_ERR_NOTDIR = -20, // Entry is not a dir
5050
LFS_ERR_ISDIR = -21, // Entry is a dir
5151
LFS_ERR_INVAL = -22, // Invalid parameter
@@ -75,6 +75,7 @@ enum lfs_open_flags {
7575
LFS_F_DIRTY = 0x10000, // File does not match storage
7676
LFS_F_WRITING = 0x20000, // File has been written since last flush
7777
LFS_F_READING = 0x40000, // File has been read since last flush
78+
LFS_F_ERRED = 0x80000, // An error occured during write
7879
};
7980

8081
// File seek flags

tests/test_alloc.sh

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ tests/test.py << TEST
121121
size = strlen("exhaustion");
122122
memcpy(buffer, "exhaustion", size);
123123
lfs_file_write(&lfs, &file[0], buffer, size) => size;
124+
lfs_file_sync(&lfs, &file[0]) => 0;
124125
125126
size = strlen("blahblahblahblah");
126127
memcpy(buffer, "blahblahblahblah", size);
@@ -142,6 +143,7 @@ tests/test.py << TEST
142143
lfs_mount(&lfs, &cfg) => 0;
143144
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_RDONLY);
144145
size = strlen("exhaustion");
146+
lfs_file_size(&lfs, &file[0]) => size;
145147
lfs_file_read(&lfs, &file[0], buffer, size) => size;
146148
memcmp(buffer, "exhaustion", size) => 0;
147149
lfs_file_close(&lfs, &file[0]) => 0;
@@ -166,6 +168,7 @@ tests/test.py << TEST
166168
size = strlen("exhaustion");
167169
memcpy(buffer, "exhaustion", size);
168170
lfs_file_write(&lfs, &file[0], buffer, size) => size;
171+
lfs_file_sync(&lfs, &file[0]) => 0;
169172
170173
size = strlen("blahblahblahblah");
171174
memcpy(buffer, "blahblahblahblah", size);
@@ -187,6 +190,7 @@ tests/test.py << TEST
187190
lfs_mount(&lfs, &cfg) => 0;
188191
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_RDONLY);
189192
size = strlen("exhaustion");
193+
lfs_file_size(&lfs, &file[0]) => size;
190194
lfs_file_read(&lfs, &file[0], buffer, size) => size;
191195
memcmp(buffer, "exhaustion", size) => 0;
192196
lfs_file_close(&lfs, &file[0]) => 0;
@@ -196,14 +200,14 @@ TEST
196200
echo "--- Dir exhaustion test ---"
197201
tests/test.py << TEST
198202
lfs_mount(&lfs, &cfg) => 0;
199-
lfs_stat(&lfs, "exhaustion", &info) => 0;
200-
lfs_size_t fullsize = info.size;
201203
lfs_remove(&lfs, "exhaustion") => 0;
202204
203205
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
204206
size = strlen("blahblahblahblah");
205207
memcpy(buffer, "blahblahblahblah", size);
206-
for (lfs_size_t i = 0; i < fullsize - 2*512; i += size) {
208+
for (lfs_size_t i = 0;
209+
i < (cfg.block_count-6)*(cfg.block_size-8);
210+
i += size) {
207211
lfs_file_write(&lfs, &file[0], buffer, size) => size;
208212
}
209213
lfs_file_close(&lfs, &file[0]) => 0;
@@ -214,7 +218,11 @@ tests/test.py << TEST
214218
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_WRONLY | LFS_O_APPEND);
215219
size = strlen("blahblahblahblah");
216220
memcpy(buffer, "blahblahblahblah", size);
217-
lfs_file_write(&lfs, &file[0], buffer, size) => size;
221+
for (lfs_size_t i = 0;
222+
i < (cfg.block_size-8);
223+
i += size) {
224+
lfs_file_write(&lfs, &file[0], buffer, size) => size;
225+
}
218226
lfs_file_close(&lfs, &file[0]) => 0;
219227
220228
lfs_mkdir(&lfs, "exhaustiondir") => LFS_ERR_NOSPC;
@@ -224,14 +232,14 @@ TEST
224232
echo "--- Chained dir exhaustion test ---"
225233
tests/test.py << TEST
226234
lfs_mount(&lfs, &cfg) => 0;
227-
lfs_stat(&lfs, "exhaustion", &info) => 0;
228-
lfs_size_t fullsize = info.size;
229-
230235
lfs_remove(&lfs, "exhaustion") => 0;
236+
231237
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
232238
size = strlen("blahblahblahblah");
233239
memcpy(buffer, "blahblahblahblah", size);
234-
for (lfs_size_t i = 0; i < fullsize - 19*512; i += size) {
240+
for (lfs_size_t i = 0;
241+
i < (cfg.block_count-24)*(cfg.block_size-8);
242+
i += size) {
235243
lfs_file_write(&lfs, &file[0], buffer, size) => size;
236244
}
237245
lfs_file_close(&lfs, &file[0]) => 0;
@@ -247,7 +255,9 @@ tests/test.py << TEST
247255
lfs_file_open(&lfs, &file[0], "exhaustion", LFS_O_WRONLY | LFS_O_CREAT);
248256
size = strlen("blahblahblahblah");
249257
memcpy(buffer, "blahblahblahblah", size);
250-
for (lfs_size_t i = 0; i < fullsize - 20*512; i += size) {
258+
for (lfs_size_t i = 0;
259+
i < (cfg.block_count-26)*(cfg.block_size-8);
260+
i += size) {
251261
lfs_file_write(&lfs, &file[0], buffer, size) => size;
252262
}
253263
lfs_file_close(&lfs, &file[0]) => 0;

tests/test_corrupt.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ do
8282
lfs_chktree
8383
done
8484

85+
echo "--- Block persistance ---"
86+
for i in {0..33}
87+
do
88+
rm -rf blocks
89+
mkdir blocks
90+
lfs_mktree
91+
chmod a-w blocks/$(printf '%x' $i)
92+
lfs_mktree
93+
lfs_chktree
94+
done
95+
8596
echo "--- Big region corruption ---"
8697
rm -rf blocks
8798
mkdir blocks

tests/test_dirs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TEST
5656
echo "--- Directory failures ---"
5757
tests/test.py << TEST
5858
lfs_mount(&lfs, &cfg) => 0;
59-
lfs_mkdir(&lfs, "potato") => LFS_ERR_EXISTS;
59+
lfs_mkdir(&lfs, "potato") => LFS_ERR_EXIST;
6060
lfs_dir_open(&lfs, &dir[0], "tomato") => LFS_ERR_NOENT;
6161
lfs_dir_open(&lfs, &dir[0], "burito") => LFS_ERR_NOTDIR;
6262
lfs_file_open(&lfs, &file[0], "tomato", LFS_O_RDONLY) => LFS_ERR_NOENT;

0 commit comments

Comments
 (0)