Changed lfs_dir_read to return 0 on success

Before:
If an entry is found, lfs_dir_read returns 1
If at end of directory, lfs_dir_read returns 0

After:
If an entry is found, lfs_dir_read returns 0
If at end of directory, lfs_dir_read returns LFS_ERR_NOENT

---

lfs_dir_read is a bit of an odd function. It's supposed to match
lfs_file_read, but instead of reading bytes, it reads directory
entries.

Its POSIX equivalent, readdir, indicates whether or not we reached
the end of the directory by returning a NULL pointer:

struct dirent *readdir(DIR *dirp);

But this API needed to change for littlefs, since we don't use errno,
instead returning errors as signed integers through all API functions,
including lfs_dir_read.  So, in an effort to match lfs_file_read,
lfs_dir_read returned the "number" of directory entries read, limited
to either 0 or 1.

Perhaps unsurprisingly, this turned out to be confusing to users,
and a better API was proposed hathach to instead return either 0
or LFS_ERR_NOENT, an error which can't occur for other reasons in
lfs_dir_read.

Suggested by hathach
This commit is contained in:
Christopher Haster
2020-12-20 18:36:38 -06:00
parent 1a59954ec6
commit e50a0677c0
6 changed files with 366 additions and 364 deletions

9
lfs.c
View File

@@ -2160,18 +2160,19 @@ static int lfs_dir_rawread(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
info->type = LFS_TYPE_DIR;
strcpy(info->name, ".");
dir->pos += 1;
return true;
return 0;
} else if (dir->pos == 1) {
info->type = LFS_TYPE_DIR;
strcpy(info->name, "..");
dir->pos += 1;
return true;
return 0;
}
while (true) {
if (dir->id == dir->m.count) {
if (!dir->m.split) {
return false;
// reached end of traversal
return LFS_ERR_NOENT;
}
int err = lfs_dir_fetch(lfs, &dir->m, dir->m.tail);
@@ -2194,7 +2195,7 @@ static int lfs_dir_rawread(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
}
dir->pos += 1;
return true;
return 0;
}
static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {