Austin Group Bug Tracker via austin-group-l at The Open Group wrote in <fa4fa389e50a3b2d84fe9dfb203c7...@austingroupbugs.net>: ... |https://austingroupbugs.net/view.php?id=1798 ... | (0006709) corinna_vinschen (reporter) - 2024-03-07 15:00 | https://austingroupbugs.net/view.php?id=1798#c6709 ... |You're missing the fact that the underlying OS does *not* maintain a file |position on directory descriptors. The function returning the file |position |always returns 0 on a directory, independent of the actually read directory |entries. Also, there's no way to lseek on a directory. The only operation |available is a "restart" flag to the directory read operation, which |allows |to specify to start at position 0. | |So, to be able to implement telldir/seekdir, the DIR struct has to |maintain |a read counter. telldir() simply returns the number of directory entries |read so far. Seekdir() is implemented as a "restart" and then reading |directory entries |in a loop until the counter matches the one given as argument. | |Having said that, as soon as you fork() a directory descriptor with |posix_getdent operation, you not only generate a copy of the underlying OS |descriptor, you also duplicate the DIR struct into the new process. Now |the |DIR structs are independent from each other. If you call posix_getdents |on |one of them, the DIR strucxt in the other process is obviously *not* |updated |accordingly. Thus, any lseek() on the directory descriptor in one process |is lost on the one in the other directory. | |I used fork() as an example, but the same goes for dup(), unless you share |the same DIR structure for all the directory descriptors in shared memory. | |Does that clear things up?
How about a very expensive temporary file storing a binary dirent dump of the entire directory content (in case of a fork)? Then the child can pick up the read pointer to that file. If it is not a real directory FD anyway. Or a file / xy with only the counter. --steffen | |Der Kragenbaer, The moon bear, |der holt sich munter he cheerfully and one by one |einen nach dem anderen runter wa.ks himself off |(By Robert Gernhardt)