1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-19 10:22:27 +00:00
emacs/lib/fdopendir.c
Paul Eggert 8654f9d7d6 Use fdopendir, fstatat and readlinkat, for efficiency.
On my host, this speeds up directory-files-and-attributes by a
factor of 3, when applied to Emacs's src directory.
These functions are standardized by POSIX and are common these
days; fall back on a (slower) gnulib implementation if the host
is too old to supply them.
* .bzrignore: Add lib/dirent.h.
* lib/Makefile.am (libgnu_a_SOURCES): Add openat-die.c, save-cwd.c.
* lib/careadlinkat.c, lib/careadlinkat.h: Merge from gnulib,
incorporating: 2013-01-29 careadlinkat: do not provide careadlinkatcwd.
* lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate.
* lib/dirent.in.h, lib/fdopendir.c, lib/fstatat.c, lib/openat-priv.h:
* lib/openat-proc.c, lib/openat.h, m4/dirent_h.m4, m4/fdopendir.m4:
* m4/fstatat.m4: New files, from gnulib.
* lib/openat-die.c, lib/save-cwd.c, lib/save-cwd.h: New files.
These last three are specific to Emacs and are not copied from gnulib.
They are simpler than the gnulib versions and are tuned for Emacs.
* admin/merge-gnulib (GNULIB_MODULES): Add fdopendir, fstatat, readlinkat.
(GNULIB_TOOL_FLAGS): Do not avoid at-internal, openat-h.
Avoid dup, open, opendir.
* nt/inc/sys/stat.h (fstatat):
* nt/inc/unistd.h (readlinkat): New decls.
* src/conf_post.h (GNULIB_SUPPORT_ONLY_AT_FDCWD): Remove.
* src/dired.c: Include <fcntl.h>.
(open_directory): New function, which uses open and fdopendir
rather than opendir.  DOS_NT platforms still use opendir, though.
(directory_files_internal, file_name_completion): Use it.
(file_attributes): New function, with most of the old Ffile_attributes.
(directory_files_internal, Ffile_attributes): Use it.
(file_attributes, file_name_completion_stat): First arg is now fd,
not dir name.  All uses changed.  Use fstatat rather than lstat +
stat.
(file_attributes): Use emacs_readlinkat rather than Ffile_symlink_p.
* src/fileio.c: Include <allocator.h>, <careadlinkat.h>.
(emacs_readlinkat): New function, with much of the old
Ffile_symlink_p, but with an fd argument for speed.
It uses readlinkat rather than careadlinkatcwd, so that it
need not assume the working directory.
(Ffile_symlink_p): Use it.
* src/filelock.c (current_lock_owner): Use emacs_readlinkat
rather than emacs_readlink.
* src/lisp.h (emacs_readlinkat): New decl.
(READLINK_BUFSIZE, emacs_readlink): Remove.
* src/sysdep.c: Do not include <allocator.h>, <careadlinkat.h>.
(emacs_norealloc_allocator, emacs_readlink): Remove.
This stuff is moved to fileio.c.
* src/w32.c (fstatat, readlinkat): New functions.
(careadlinkat): Don't check that fd == AT_FDCWD.
(careadlinkatcwd): Remove; no longer needed.

Fixes: debbugs:13539
2013-01-31 22:30:51 -08:00

205 lines
5.6 KiB
C

/* provide a replacement fdopendir function
Copyright (C) 2004-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* written by Jim Meyering */
#include <config.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#if !HAVE_FDOPENDIR
# include "openat.h"
# include "openat-priv.h"
# include "save-cwd.h"
# if GNULIB_DIRENT_SAFER
# include "dirent--.h"
# endif
# ifndef REPLACE_FCHDIR
# define REPLACE_FCHDIR 0
# endif
static DIR *fdopendir_with_dup (int, int, struct saved_cwd const *);
static DIR *fd_clone_opendir (int, struct saved_cwd const *);
/* Replacement for POSIX fdopendir.
First, try to simulate it via opendir ("/proc/self/fd/..."). Failing
that, simulate it by using fchdir metadata, or by doing
save_cwd/fchdir/opendir(".")/restore_cwd.
If either the save_cwd or the restore_cwd fails (relatively unlikely),
then give a diagnostic and exit nonzero.
If successful, the resulting stream is based on FD in
implementations where streams are based on file descriptors and in
applications where no other thread or signal handler allocates or
frees file descriptors. In other cases, consult dirfd on the result
to find out whether FD is still being used.
Otherwise, this function works just like POSIX fdopendir.
W A R N I N G:
Unlike other fd-related functions, this one places constraints on FD.
If this function returns successfully, FD is under control of the
dirent.h system, and the caller should not close or modify the state of
FD other than by the dirent.h functions. */
DIR *
fdopendir (int fd)
{
DIR *dir = fdopendir_with_dup (fd, -1, NULL);
if (! REPLACE_FCHDIR && ! dir)
{
int saved_errno = errno;
if (EXPECTED_ERRNO (saved_errno))
{
struct saved_cwd cwd;
if (save_cwd (&cwd) != 0)
openat_save_fail (errno);
dir = fdopendir_with_dup (fd, -1, &cwd);
saved_errno = errno;
free_cwd (&cwd);
errno = saved_errno;
}
}
return dir;
}
/* Like fdopendir, except that if OLDER_DUPFD is not -1, it is known
to be a dup of FD which is less than FD - 1 and which will be
closed by the caller and not otherwise used by the caller. This
function makes sure that FD is closed and all file descriptors less
than FD are open, and then calls fd_clone_opendir on a dup of FD.
That way, barring race conditions, fd_clone_opendir returns a
stream whose file descriptor is FD.
If REPLACE_CHDIR or CWD is null, use opendir ("/proc/self/fd/...",
falling back on fchdir metadata. Otherwise, CWD is a saved version
of the working directory; use fchdir/opendir(".")/restore_cwd(CWD). */
static DIR *
fdopendir_with_dup (int fd, int older_dupfd, struct saved_cwd const *cwd)
{
int dupfd = dup (fd);
if (dupfd < 0 && errno == EMFILE)
dupfd = older_dupfd;
if (dupfd < 0)
return NULL;
else
{
DIR *dir;
int saved_errno;
if (dupfd < fd - 1 && dupfd != older_dupfd)
{
dir = fdopendir_with_dup (fd, dupfd, cwd);
saved_errno = errno;
}
else
{
close (fd);
dir = fd_clone_opendir (dupfd, cwd);
saved_errno = errno;
if (! dir)
{
int fd1 = dup (dupfd);
if (fd1 != fd)
openat_save_fail (fd1 < 0 ? errno : EBADF);
}
}
if (dupfd != older_dupfd)
close (dupfd);
errno = saved_errno;
return dir;
}
}
/* Like fdopendir, except the result controls a clone of FD. It is
the caller's responsibility both to close FD and (if the result is
not null) to closedir the result. */
static DIR *
fd_clone_opendir (int fd, struct saved_cwd const *cwd)
{
if (REPLACE_FCHDIR || ! cwd)
{
DIR *dir = NULL;
int saved_errno = EOPNOTSUPP;
char buf[OPENAT_BUFFER_SIZE];
char *proc_file = openat_proc_name (buf, fd, ".");
if (proc_file)
{
dir = opendir (proc_file);
saved_errno = errno;
if (proc_file != buf)
free (proc_file);
}
# if REPLACE_FCHDIR
if (! dir && EXPECTED_ERRNO (saved_errno))
{
char const *name = _gl_directory_name (fd);
return (name ? opendir (name) : NULL);
}
# endif
errno = saved_errno;
return dir;
}
else
{
if (fchdir (fd) != 0)
return NULL;
else
{
DIR *dir = opendir (".");
int saved_errno = errno;
if (restore_cwd (cwd) != 0)
openat_restore_fail (errno);
errno = saved_errno;
return dir;
}
}
}
#else /* HAVE_FDOPENDIR */
# include <errno.h>
# include <sys/stat.h>
# undef fdopendir
/* Like fdopendir, but work around GNU/Hurd bug by validating FD. */
DIR *
rpl_fdopendir (int fd)
{
struct stat st;
if (fstat (fd, &st))
return NULL;
if (!S_ISDIR (st.st_mode))
{
errno = ENOTDIR;
return NULL;
}
return fdopendir (fd);
}
#endif /* HAVE_FDOPENDIR */