nix icon indicating copy to clipboard operation
nix copied to clipboard

readdir needs to call rewinddir

Open rbtcollins opened this issue 3 years ago • 3 comments
trafficstars

At least on BSD, and it looks like in some circumstances on Linux too. Quoting the NetBSD man page:

fdopendir(fd)
          The fdopendir() function associates a directory stream with the
          directory file descriptor fd.  The file offset associated with fd at
          the time of the call determines which entries are returned.

rbtcollins avatar Aug 07 '22 10:08 rbtcollins

Are you suggesting that Dir::from_fd should always rewind the dir, or that it should have an optional method for that? I don't think that always rewinding is necessarily the right option.

asomers avatar Aug 11 '22 03:08 asomers

So the scenario is this:

  1. open fd P, do something like openat() on P, which shifts the read pointer.
  2. opendirat(P) - gets a DIR* but the read pointer for P is not at 0
  3. readdir(P) - content is missed

rbtcollins avatar Aug 11 '22 14:08 rbtcollins

Yeah, in that above case I'd say that moving the read pointer is deliberate. We could have an optional method to rewinddir, but it shouldn't be mandatory. Basically, if the user gives us a raw file descriptor, we have to assume that they know what they're doing with it.

asomers avatar Sep 11 '22 22:09 asomers