delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/02/05/00:20:37

X-Spam-Check-By: sourceware.org
Date: Sat, 4 Feb 2006 21:20:35 -0800
From: Yitzchak Scott-Thoennes <sthoenna AT efn DOT org>
To: cygwin AT cygwin DOT com
Subject: Re: readdir after rewinddir not working in 20060128 snapshot
Message-ID: <20060205052035.GA2768@efn.org>
References: <20060130082921 DOT GA2404 AT efn DOT org> <20060130103948 DOT GL15572 AT calimero DOT vinschen DOT de>
Mime-Version: 1.0
In-Reply-To: <20060130103948.GL15572@calimero.vinschen.de>
User-Agent: Mutt/1.4.2.1i
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

On Mon, Jan 30, 2006 at 11:39:48AM +0100, Corinna Vinschen wrote:
> On Jan 30 00:29, Yitzchak Scott-Thoennes wrote:
> > After a rewinddir, readdir seems to return as many empty entries as there
> > were actual entries left to read, followed by . and ..
> 
> Thanks for the testcase!  Since the underlying NT call NtQueryDirectoryInfo
> is able to return more than one directory entry in one call, I thought it
> might be a good idea to implement readdir caching.  Unfortunately I forgot
> to reset the cache in case of rewinddir.  I've applied a fix now.

Thanks, but..

With 20060202, that testcase now works, but if you read all the way to
the end, then rewind, you only get . and .. thereafter.

$ cat rewdir.c
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>

void readentries(DIR *dh, int limit) {
  struct dirent *de;
  int count;

  for (count = 0; count < limit; ++count) {
    errno = 0;
    if ((de = readdir(dh)) == NULL) {
      if (errno != 0) printf ("read error: %s\n", strerror (errno));
      break;
    }
    printf ("got: %s\n", de->d_name);
  }
}

int main(int argc, char **argv)
{
  DIR *dh;
  int i;

  if (argc != 2) return 1;

  if ( (dh = opendir (argv[1])) == NULL ) {
    fprintf (stderr, "couldn't open '%s': %s\n", argv[1], strerror (errno));
    return 1;
  }

  for (i = 0; i < 3; ++i) {
    readentries (dh, 9999);

    printf ("rewinding.\n");
    errno = 0;
    rewinddir (dh);
    if (errno != 0) printf ("unexpected errno: %d\n", errno);
  }

  return 0;
}

$ rm -rf foo; mkdir foo; touch foo/{bar,baz,quux,quuux,quuuux}

$ gcc -Wall rewdir.c -o rewdir && ./rewdir foo
got: .
got: ..
got: bar
got: baz
got: quuuux
got: quuux
got: quux
rewinding.
got: .
got: ..
rewinding.
got: .
got: ..
rewinding.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019