2014-08-10 20:40:57 +00:00
|
|
|
/* save-cwd.c -- Save and restore current working directory.
|
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2024 Free Software
|
2014-08-10 20:40:57 +00:00
|
|
|
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
|
2017-09-13 22:52:52 +00:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2014-08-10 20:40:57 +00:00
|
|
|
|
|
|
|
/* Gnulib needs to save and restore the current working directory to
|
|
|
|
fully emulate functions like fstatat. But Emacs doesn't care what
|
|
|
|
the current working directory is; it always uses absolute file
|
|
|
|
names. This module replaces the Gnulib module by omitting the code
|
|
|
|
that Emacs does not need. */
|
|
|
|
|
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
|
|
|
#include <config.h>
|
2014-08-10 20:40:57 +00:00
|
|
|
|
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
|
|
|
#include "save-cwd.h"
|
2014-08-10 20:40:57 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* Record the location of the current working directory in CWD so that
|
|
|
|
the program may change to other directories and later use restore_cwd
|
|
|
|
to return to the recorded location. This function may allocate
|
|
|
|
space using malloc (via getcwd) or leave a file descriptor open;
|
|
|
|
use free_cwd to perform the necessary free or close. Upon failure,
|
|
|
|
no memory is allocated, any locally opened file descriptors are
|
|
|
|
closed; return non-zero -- in that case, free_cwd need not be
|
|
|
|
called, but doing so is ok. Otherwise, return zero.
|
|
|
|
|
|
|
|
The _raison d'etre_ for this interface is that the working directory
|
|
|
|
is sometimes inaccessible, and getcwd is not robust or as efficient.
|
|
|
|
So, we prefer to use the open/fchdir approach, but fall back on
|
|
|
|
getcwd if necessary. This module works for most cases with just
|
|
|
|
the getcwd-lgpl module, but to be truly robust, use the getcwd module.
|
|
|
|
|
|
|
|
Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
|
|
|
|
SCO Xenix. Also, SunOS 4 and Irix 5.3 provide the function, yet it
|
|
|
|
doesn't work for partitions on which auditing is enabled. If
|
|
|
|
you're still using an obsolete system with these problems, please
|
|
|
|
send email to the maintainer of this code. */
|
|
|
|
|
2014-08-10 21:06:07 +00:00
|
|
|
#if !defined HAVE_FCHDIR && !defined fchdir
|
|
|
|
# define fchdir(fd) (-1)
|
|
|
|
#endif
|
|
|
|
|
2014-08-10 20:40:57 +00:00
|
|
|
int
|
|
|
|
save_cwd (struct saved_cwd *cwd)
|
|
|
|
{
|
|
|
|
cwd->desc = open (".", O_SEARCH | O_CLOEXEC);
|
|
|
|
/* The 'name' member is present only to minimize differences from
|
|
|
|
gnulib. Initialize it to zero, if only to simplify debugging. */
|
|
|
|
cwd->name = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change to recorded location, CWD, in directory hierarchy.
|
|
|
|
Upon failure, return -1 (errno is set by chdir or fchdir).
|
|
|
|
Upon success, return zero. */
|
|
|
|
|
|
|
|
int
|
|
|
|
restore_cwd (const struct saved_cwd *cwd)
|
|
|
|
{
|
|
|
|
/* Restore the previous directory if possible, to avoid tying down
|
2014-08-10 21:06:07 +00:00
|
|
|
the file system of the new directory (Bug#18232).
|
|
|
|
Don't worry if fchdir fails, as Emacs doesn't care what the
|
|
|
|
working directory is. The fchdir call is inside an 'if' merely to
|
|
|
|
pacify compilers that complain if fchdir's return value is ignored. */
|
2014-08-10 20:40:57 +00:00
|
|
|
if (fchdir (cwd->desc) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_cwd (struct saved_cwd *cwd)
|
|
|
|
{
|
|
|
|
close (cwd->desc);
|
|
|
|
}
|