mirror of
https://github.com/eledio-devices/thirdparty-littlefs.git
synced 2025-11-02 00:38:28 +01:00
Compare commits
2 Commits
fix-traili
...
fix-lookah
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f935fc0be6 | ||
|
|
6a89ecba39 |
101
lfs.c
101
lfs.c
@@ -305,6 +305,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check if we have looked at all blocks since last ack
|
// check if we have looked at all blocks since last ack
|
||||||
|
//if (lfs->free.i == lfs->free.ack - lfs->free.off) {
|
||||||
if (lfs->free.ack == 0) {
|
if (lfs->free.ack == 0) {
|
||||||
LFS_WARN("No more free space %d", lfs->free.i + lfs->free.off);
|
LFS_WARN("No more free space %d", lfs->free.i + lfs->free.off);
|
||||||
return LFS_ERR_NOSPC;
|
return LFS_ERR_NOSPC;
|
||||||
@@ -783,19 +784,26 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
|
|||||||
lfs_entry_t *entry, const char **path) {
|
lfs_entry_t *entry, const char **path) {
|
||||||
const char *pathname = *path;
|
const char *pathname = *path;
|
||||||
size_t pathlen;
|
size_t pathlen;
|
||||||
entry->d.type = LFS_TYPE_DIR;
|
|
||||||
entry->d.elen = sizeof(entry->d) - 4;
|
|
||||||
entry->d.alen = 0;
|
|
||||||
entry->d.nlen = 0;
|
|
||||||
entry->d.u.dir[0] = lfs->root[0];
|
|
||||||
entry->d.u.dir[1] = lfs->root[1];
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
nextname:
|
nextname:
|
||||||
// skip slashes
|
// skip slashes
|
||||||
pathname += strspn(pathname, "/");
|
pathname += strspn(pathname, "/");
|
||||||
pathlen = strcspn(pathname, "/");
|
pathlen = strcspn(pathname, "/");
|
||||||
|
|
||||||
|
// special case for root dir
|
||||||
|
if (pathname[0] == '\0') {
|
||||||
|
*entry = (lfs_entry_t){
|
||||||
|
.d.type = LFS_TYPE_DIR,
|
||||||
|
.d.elen = sizeof(entry->d) - 4,
|
||||||
|
.d.alen = 0,
|
||||||
|
.d.nlen = 0,
|
||||||
|
.d.u.dir[0] = lfs->root[0],
|
||||||
|
.d.u.dir[1] = lfs->root[1],
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// skip '.' and root '..'
|
// skip '.' and root '..'
|
||||||
if ((pathlen == 1 && memcmp(pathname, ".", 1) == 0) ||
|
if ((pathlen == 1 && memcmp(pathname, ".", 1) == 0) ||
|
||||||
(pathlen == 2 && memcmp(pathname, "..", 2) == 0)) {
|
(pathlen == 2 && memcmp(pathname, "..", 2) == 0)) {
|
||||||
@@ -827,25 +835,10 @@ nextname:
|
|||||||
suffix += sufflen;
|
suffix += sufflen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// found path
|
|
||||||
if (pathname[0] == '\0') {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// update what we've found
|
// update what we've found
|
||||||
*path = pathname;
|
*path = pathname;
|
||||||
|
|
||||||
// continue on if we hit a directory
|
// find path
|
||||||
if (entry->d.type != LFS_TYPE_DIR) {
|
|
||||||
return LFS_ERR_NOTDIR;
|
|
||||||
}
|
|
||||||
|
|
||||||
int err = lfs_dir_fetch(lfs, dir, entry->d.u.dir);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
// find entry matching name
|
|
||||||
while (true) {
|
while (true) {
|
||||||
int err = lfs_dir_next(lfs, dir, entry);
|
int err = lfs_dir_next(lfs, dir, entry);
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -881,8 +874,21 @@ nextname:
|
|||||||
entry->d.type &= ~0x80;
|
entry->d.type &= ~0x80;
|
||||||
}
|
}
|
||||||
|
|
||||||
// to next name
|
|
||||||
pathname += pathlen;
|
pathname += pathlen;
|
||||||
|
pathname += strspn(pathname, "/");
|
||||||
|
if (pathname[0] == '\0') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// continue on if we hit a directory
|
||||||
|
if (entry->d.type != LFS_TYPE_DIR) {
|
||||||
|
return LFS_ERR_NOTDIR;
|
||||||
|
}
|
||||||
|
|
||||||
|
int err = lfs_dir_fetch(lfs, dir, entry->d.u.dir);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -899,8 +905,13 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
|
|||||||
|
|
||||||
// fetch parent directory
|
// fetch parent directory
|
||||||
lfs_dir_t cwd;
|
lfs_dir_t cwd;
|
||||||
|
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t entry;
|
lfs_entry_t entry;
|
||||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||||
if (err != LFS_ERR_NOENT || strchr(path, '/') != NULL) {
|
if (err != LFS_ERR_NOENT || strchr(path, '/') != NULL) {
|
||||||
return err ? err : LFS_ERR_EXIST;
|
return err ? err : LFS_ERR_EXIST;
|
||||||
}
|
}
|
||||||
@@ -944,8 +955,13 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
|
|||||||
dir->pair[0] = lfs->root[0];
|
dir->pair[0] = lfs->root[0];
|
||||||
dir->pair[1] = lfs->root[1];
|
dir->pair[1] = lfs->root[1];
|
||||||
|
|
||||||
|
int err = lfs_dir_fetch(lfs, dir, dir->pair);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t entry;
|
lfs_entry_t entry;
|
||||||
int err = lfs_dir_find(lfs, dir, &entry, &path);
|
err = lfs_dir_find(lfs, dir, &entry, &path);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
} else if (entry.d.type != LFS_TYPE_DIR) {
|
} else if (entry.d.type != LFS_TYPE_DIR) {
|
||||||
@@ -1287,8 +1303,13 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
|||||||
|
|
||||||
// allocate entry for file if it doesn't exist
|
// allocate entry for file if it doesn't exist
|
||||||
lfs_dir_t cwd;
|
lfs_dir_t cwd;
|
||||||
|
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t entry;
|
lfs_entry_t entry;
|
||||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||||
if (err && (err != LFS_ERR_NOENT || strchr(path, '/') != NULL)) {
|
if (err && (err != LFS_ERR_NOENT || strchr(path, '/') != NULL)) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1794,8 +1815,13 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
|
|||||||
/// General fs operations ///
|
/// General fs operations ///
|
||||||
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
|
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
|
||||||
lfs_dir_t cwd;
|
lfs_dir_t cwd;
|
||||||
|
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t entry;
|
lfs_entry_t entry;
|
||||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1830,8 +1856,13 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lfs_dir_t cwd;
|
lfs_dir_t cwd;
|
||||||
|
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t entry;
|
lfs_entry_t entry;
|
||||||
int err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
err = lfs_dir_find(lfs, &cwd, &entry, &path);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1886,14 +1917,24 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
|
|
||||||
// find old entry
|
// find old entry
|
||||||
lfs_dir_t oldcwd;
|
lfs_dir_t oldcwd;
|
||||||
|
int err = lfs_dir_fetch(lfs, &oldcwd, lfs->root);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t oldentry;
|
lfs_entry_t oldentry;
|
||||||
int err = lfs_dir_find(lfs, &oldcwd, &oldentry, &oldpath);
|
err = lfs_dir_find(lfs, &oldcwd, &oldentry, &oldpath);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// allocate new entry
|
// allocate new entry
|
||||||
lfs_dir_t newcwd;
|
lfs_dir_t newcwd;
|
||||||
|
err = lfs_dir_fetch(lfs, &newcwd, lfs->root);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
lfs_entry_t preventry;
|
lfs_entry_t preventry;
|
||||||
err = lfs_dir_find(lfs, &newcwd, &preventry, &newpath);
|
err = lfs_dir_find(lfs, &newcwd, &preventry, &newpath);
|
||||||
if (err && (err != LFS_ERR_NOENT || strchr(newpath, '/') != NULL)) {
|
if (err && (err != LFS_ERR_NOENT || strchr(newpath, '/') != NULL)) {
|
||||||
|
|||||||
@@ -110,10 +110,35 @@ lfs_alloc_singleproc multiprocreuse
|
|||||||
lfs_verify multiprocreuse
|
lfs_verify multiprocreuse
|
||||||
lfs_verify singleprocreuse
|
lfs_verify singleprocreuse
|
||||||
|
|
||||||
echo "--- Cleanup ---"
|
|
||||||
lfs_remove multiprocreuse
|
lfs_remove multiprocreuse
|
||||||
lfs_remove singleprocreuse
|
lfs_remove singleprocreuse
|
||||||
|
|
||||||
|
echo "--- Lookahead overflow test ---"
|
||||||
|
lfs_mkdir overflow
|
||||||
|
for name in bacon eggs pancakes
|
||||||
|
do
|
||||||
|
tests/test.py << TEST
|
||||||
|
lfs_mount(&lfs, &cfg) => 0;
|
||||||
|
|
||||||
|
// // setup lookahead to almost overflow
|
||||||
|
// lfs.free.begin = ((lfs_size_t)-1) - 2*$SIZE;
|
||||||
|
// lfs.free.size = 0;
|
||||||
|
// lfs.free.off = 0;
|
||||||
|
|
||||||
|
lfs_file_open(&lfs, &file[0], "overflow/$name",
|
||||||
|
LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) => 0;
|
||||||
|
size = strlen("$name");
|
||||||
|
memcpy(buffer, "$name", size);
|
||||||
|
for (int i = 0; i < $SIZE; i++) {
|
||||||
|
lfs_file_write(&lfs, &file[0], buffer, size) => size;
|
||||||
|
}
|
||||||
|
lfs_file_close(&lfs, &file[0]) => 0;
|
||||||
|
lfs_unmount(&lfs) => 0;
|
||||||
|
TEST
|
||||||
|
done
|
||||||
|
lfs_verify overflow
|
||||||
|
lfs_remove overflow
|
||||||
|
|
||||||
echo "--- Exhaustion test ---"
|
echo "--- Exhaustion test ---"
|
||||||
tests/test.py << TEST
|
tests/test.py << TEST
|
||||||
lfs_mount(&lfs, &cfg) => 0;
|
lfs_mount(&lfs, &cfg) => 0;
|
||||||
@@ -368,7 +393,6 @@ echo "--- Outdated lookahead and split dir test ---"
|
|||||||
rm -rf blocks
|
rm -rf blocks
|
||||||
tests/test.py << TEST
|
tests/test.py << TEST
|
||||||
lfs_format(&lfs, &cfg) => 0;
|
lfs_format(&lfs, &cfg) => 0;
|
||||||
|
|
||||||
lfs_mount(&lfs, &cfg) => 0;
|
lfs_mount(&lfs, &cfg) => 0;
|
||||||
|
|
||||||
// fill completely with two files
|
// fill completely with two files
|
||||||
@@ -424,5 +448,8 @@ tests/test.py << TEST
|
|||||||
lfs_unmount(&lfs) => 0;
|
lfs_unmount(&lfs) => 0;
|
||||||
TEST
|
TEST
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "--- Results ---"
|
echo "--- Results ---"
|
||||||
tests/stats.py
|
tests/stats.py
|
||||||
|
|||||||
@@ -90,22 +90,6 @@ tests/test.py << TEST
|
|||||||
lfs_unmount(&lfs) => 0;
|
lfs_unmount(&lfs) => 0;
|
||||||
TEST
|
TEST
|
||||||
|
|
||||||
echo "--- Trailing dot path tests ---"
|
|
||||||
tests/test.py << TEST
|
|
||||||
lfs_mount(&lfs, &cfg) => 0;
|
|
||||||
lfs_stat(&lfs, "tea/hottea/", &info) => 0;
|
|
||||||
strcmp(info.name, "hottea") => 0;
|
|
||||||
lfs_stat(&lfs, "tea/hottea/.", &info) => 0;
|
|
||||||
strcmp(info.name, "hottea") => 0;
|
|
||||||
lfs_stat(&lfs, "tea/hottea/./.", &info) => 0;
|
|
||||||
strcmp(info.name, "hottea") => 0;
|
|
||||||
lfs_stat(&lfs, "tea/hottea/..", &info) => 0;
|
|
||||||
strcmp(info.name, "tea") => 0;
|
|
||||||
lfs_stat(&lfs, "tea/hottea/../.", &info) => 0;
|
|
||||||
strcmp(info.name, "tea") => 0;
|
|
||||||
lfs_unmount(&lfs) => 0;
|
|
||||||
TEST
|
|
||||||
|
|
||||||
echo "--- Root dot dot path tests ---"
|
echo "--- Root dot dot path tests ---"
|
||||||
tests/test.py << TEST
|
tests/test.py << TEST
|
||||||
lfs_mount(&lfs, &cfg) => 0;
|
lfs_mount(&lfs, &cfg) => 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user