Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change lfs_dir_read to return 0 on success #512

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

geky
Copy link
Member

@geky geky commented Dec 25, 2020

If #491 is merged, it means breaking changes to the API, and a major version bump of some form (but not disk-breaking). This means it's a good time to look at open issues we couldn't fix early without breaking the API.


Here's a requested change to lfs_dir_read discussed in #114

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

For some background:

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
Should fix #114

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
@geky geky added the needs major version breaking functionality only allowed in major versions label Dec 25, 2020
@geky geky marked this pull request as draft December 25, 2020 05:28
@geky geky added the postponed label Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs major version breaking functionality only allowed in major versions postponed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lfs_dir_read return 1 on success instead of LFS_ERR_OK
1 participant