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

Use rewinddir to count number of files #13

Open
cxong opened this issue May 13, 2014 · 2 comments
Open

Use rewinddir to count number of files #13

cxong opened this issue May 13, 2014 · 2 comments

Comments

@cxong
Copy link
Owner

cxong commented May 13, 2014

In tinydir_open_sorted, a first pass is used to count the number of files, then a second pass actually reads the file info. In between the two passes, tinydir closes and reopens the dir.

This is unnecessary with dirent; there's the rewinddir function which allows going back to the first file. This would also eliminate a race condition where the directory may have changed between the two passes.

Not sure if there's an equivalent for Windows.

@lautis0503
Copy link
Collaborator

lautis0503 commented Jul 9, 2016

What about malloc() a small block of memory (maybe size * 12, I guess), and realloc() another block when needed? Will need only one-pass. Might be slower, but safer about race condition.
There could be a balance between safety and efficiency.

@cxong
Copy link
Owner Author

cxong commented Jul 21, 2016

I tried a doubling realloc implementation before but counting in two passes was faster on a folder with 10000 items. Anyway rewinddir should be both fast and safe. See #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants