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-02-01 06:30:51 +00:00
|
|
|
/* Create /proc/self/fd-related names for subfiles of open directories.
|
|
|
|
|
2015-01-01 19:05:24 +00:00
|
|
|
Copyright (C) 2006, 2009-2015 Free Software Foundation, Inc.
|
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-02-01 06:30:51 +00:00
|
|
|
|
|
|
|
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 Paul Eggert. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "openat-priv.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "intprops.h"
|
|
|
|
|
|
|
|
#define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/%s"
|
|
|
|
|
|
|
|
#define PROC_SELF_FD_NAME_SIZE_BOUND(len) \
|
|
|
|
(sizeof PROC_SELF_FD_FORMAT - sizeof "%d%s" \
|
|
|
|
+ INT_STRLEN_BOUND (int) + (len) + 1)
|
|
|
|
|
|
|
|
|
|
|
|
/* Set BUF to the expansion of PROC_SELF_FD_FORMAT, using FD and FILE
|
|
|
|
respectively for %d and %s. If successful, return BUF if the
|
|
|
|
result fits in BUF, dynamically allocated memory otherwise. But
|
|
|
|
return NULL if /proc is not reliable, either because the operating
|
|
|
|
system support is lacking or because memory is low. */
|
|
|
|
char *
|
|
|
|
openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
|
|
|
|
{
|
|
|
|
static int proc_status = 0;
|
|
|
|
|
|
|
|
/* Make sure the caller gets ENOENT when appropriate. */
|
|
|
|
if (!*file)
|
|
|
|
{
|
|
|
|
buf[0] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! proc_status)
|
|
|
|
{
|
|
|
|
/* Set PROC_STATUS to a positive value if /proc/self/fd is
|
|
|
|
reliable, and a negative value otherwise. Solaris 10
|
|
|
|
/proc/self/fd mishandles "..", and any file name might expand
|
|
|
|
to ".." after symbolic link expansion, so avoid /proc/self/fd
|
|
|
|
if it mishandles "..". Solaris 10 has openat, but this
|
|
|
|
problem is exhibited on code that built on Solaris 8 and
|
|
|
|
running on Solaris 10. */
|
|
|
|
|
|
|
|
int proc_self_fd = open ("/proc/self/fd",
|
|
|
|
O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
|
|
|
|
if (proc_self_fd < 0)
|
|
|
|
proc_status = -1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Detect whether /proc/self/fd/%i/../fd exists, where %i is the
|
|
|
|
number of a file descriptor open on /proc/self/fd. On Linux,
|
|
|
|
that name resolves to /proc/self/fd, which was opened above.
|
|
|
|
However, on Solaris, it may resolve to /proc/self/fd/fd, which
|
|
|
|
cannot exist, since all names in /proc/self/fd are numeric. */
|
|
|
|
char dotdot_buf[PROC_SELF_FD_NAME_SIZE_BOUND (sizeof "../fd" - 1)];
|
|
|
|
sprintf (dotdot_buf, PROC_SELF_FD_FORMAT, proc_self_fd, "../fd");
|
|
|
|
proc_status = access (dotdot_buf, F_OK) ? -1 : 1;
|
|
|
|
close (proc_self_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proc_status < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t bufsize = PROC_SELF_FD_NAME_SIZE_BOUND (strlen (file));
|
|
|
|
char *result = buf;
|
|
|
|
if (OPENAT_BUFFER_SIZE < bufsize)
|
|
|
|
{
|
|
|
|
result = malloc (bufsize);
|
|
|
|
if (! result)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sprintf (result, PROC_SELF_FD_FORMAT, fd, file);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|