robust chdir and openat (works with names longer than PATH_MAX)

From:

Jim Meyering

Subject:

robust chdir and openat (works with names longer than PATH_MAX)

Date:

Sun, 28 Nov 2004 22:18:29 +0100

I've written a chdir wrapper that works even for directory
names longer than PATH_MAX. Who cares, you ask?
Any application that uses save-cwd.c's save_cwd/restore_cwd
function and for which open ("."... and then later, fchdir, may fail,
in which case restore_cwd uses (chdir's to) the string returned
by xgetcwd. With Paul's recent changes, on Solaris, getcwd can
return arbitrarily long names. Once getcwd uses replacement
openat/fstatat functions (and drops the O(n^2) code in the #else branch),
that will be true for all systems.
There are still a couple of FIXME comments. Suggestions or patches
welcome. It relies on a Solaris-style openat function (a kludgey
replacement version of which I coded up, too -- it has more FIXMEs).
openat documentation is here:
<http://www.google.com/search?q=openat+site:docs.sun.com>
For now, I've checked these into the coreutils repository.
Once they've matured a little, I'll update gnulib.
2004-11-28 Jim Meyering <address@hidden>
* save-cwd.c: Include "chdir.h", so that if save_cwd/getcwd
saves a name longer than PATH_MAX, restore_cwd's chdir can use it.
* Makefile.am (libfetish_a_SOURCES): Add chdir.h and openat.h.
This is a robust version of chdir, in that it can handle directory
names longer than PATH_MAX.
* chdir.c, chdir.h: New file.
* openat.c, openat.h: New files.
2004-11-28 Jim Meyering <address@hidden>
* chdir.m4: New file.
* openat.m4: New file.
* jm-macros.m4 (gl_MACROS): Require gl_FUNC_CHDIR.