bug-glibc
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ftw/nftw don't diagnose failure to restore initial working directory


From: Jim Meyering
Subject: ftw/nftw don't diagnose failure to restore initial working directory
Date: Sun, 02 Mar 2003 14:41:25 +0100

With FTW_CHDIR, nftw changes the current working directory.
However, there are two problems:

  * ftw.c uses getcwd, which doesn't work if the absolute path to that
    starting directory is longer than PATH_MAX: the function won't be
    able to restore the initial working directory.

  * even when getcwd does work, if ftw/nftw is unable to chdir back
    to that initial directory, it doesn't report that failure to the caller.

The documentation *does* say that the current directory is restored:

    `FTW_CHDIR'
          If this flag is given the current working directory is
          changed to the directory of the reported object before the
          callback function is called.  When `ntfw' finally returns the
          current directory is restored to its original value.

To solve the first problem, how about using the combination
of `open (".", O_RDONLY | O_DIRECTORY)' and fchdir/close, rather
than the existing `getcwd + chdir'?  If you like the idea,
let me know and I'll prepare a patch.

Solving the latter is trickier, since it probably involves changing
the interface.  Currently the documentation (man page, not texinfo)
says that an EACCES error is ignored.  But the final chdir (or, if
changed, fchdir) may fail with errno == EACCES.  Returning to the
initial directory may well be very important to some applications.
The failure must be reported.

As a user of nftw, I think I'd want to know if it returned early
(because the user-supplied function returned non-zero) and in the process
nftw was unable to restore the initial working directory.  That's two
pieces of information that currently would get lumped (somehow) into
nftw's return value.

Ideally, we'd just add a new parameter or two, and return through it/them
an indication of how ftw/nftw finished:

  * out of memory
  * unable to return to initial working directory
  * the caller-supplied function returned nonzero
  * changing to some other directory failed
  * opendir failed for errno != EACCES
  * stat or lstat failed for (errno != EACCES && errno != ENOENT)
  * ok

If nftw's interface is frozen and there's no other way around this
problem, then perhaps we need a new function altogether...

Jim

Attachment: pgpDZbTT8JAM9.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]