c – POSIX seekdir() and telldir() behaviour after target folder modification – Education Career Blog

consider the following task :

1) read a target directory contents, pass each found dirent structure to some filter function and remember filtered elements somehow for the later processing

2) some time later, iterate through the filtered elements and process them (do some I/O)

The most obvious way is to save names of sub-directories.
However, I want to keep memory usage to the minimum and to avoid additional I/O.

According to POSIX manuals, I can save position of each directory entry using telldir() and restore them later using seekdir(). To keep these positions valid, I have to keep target directory opened and to not use rewinddir() call.
Keeping a directory stream open and storing a list of dir positions(long int`s) seems to be an appropriate solution.
However, it is unclear whether stored positions remain valid after folder modification. I didn`t found any comments on these conditions in the POSIX standard.

  • 1) Whether stored positions remain valid when only new directory entries are added/removed ?
  • 2) Whether stored positions of unmodified directory entries remain valid in case of some of the filtered directory entries were removed ?
  • 3) Is it possible for the stored position to point to another directory entry after folder modification ?

It is easy to test and find out the answer on these questions for the particular system, but I would like to know what standards say on this topic

Thank you

,

Until you call rewinddir or close and reopen the directory, your view of the directory contents should not change. Sorry I don’t have the reference handy. I’ll find it later if you need it.

Leave a Comment