views:

43

answers:

1

Hello,

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

+1  A: 

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.

R..
http://opengroup.org/onlinepubs/007908775/xsh/readdir.html - If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified."
casablanca
+1 anyhow. Even though new entries *may* be retrieved, old entries should remain unchanged unless `rewinddir` is called.
casablanca
I think it should be so too. However, I`m unable to find what POSIX says about it.
konstantin
To be more precise, the question is not about whether new entries will appear in the directory stream or not, POSIX is clear on this topic. Instead, I`m interested in what POSIX says on directory positions correctness after folder modification. Sadly, noted readdir() manual page says nothing about this
konstantin
Indeed, it seems under-specified, perhaps intentionally so. See the rationale here: http://www.opengroup.org/onlinepubs/9699919799/functions/seekdir.html If you need really robust portability, you would probably do best to read the whole directory into memory and never perform `seekdir`.
R..