1 /* save-cwd.c -- Save and restore current working directory.
2 
3    Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2021 Free Software
4    Foundation, Inc.
5 
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
18 
19 /* Gnulib needs to save and restore the current working directory to
20    fully emulate functions like fstatat.  But Emacs doesn't care what
21    the current working directory is; it always uses absolute file
22    names.  This module replaces the Gnulib module by omitting the code
23    that Emacs does not need.  */
24 
25 #include <config.h>
26 
27 #include "save-cwd.h"
28 
29 #include <fcntl.h>
30 #include <unistd.h>
31 
32 /* Record the location of the current working directory in CWD so that
33    the program may change to other directories and later use restore_cwd
34    to return to the recorded location.  This function may allocate
35    space using malloc (via getcwd) or leave a file descriptor open;
36    use free_cwd to perform the necessary free or close.  Upon failure,
37    no memory is allocated, any locally opened file descriptors are
38    closed;  return non-zero -- in that case, free_cwd need not be
39    called, but doing so is ok.  Otherwise, return zero.
40 
41    The _raison d'etre_ for this interface is that the working directory
42    is sometimes inaccessible, and getcwd is not robust or as efficient.
43    So, we prefer to use the open/fchdir approach, but fall back on
44    getcwd if necessary.  This module works for most cases with just
45    the getcwd-lgpl module, but to be truly robust, use the getcwd module.
46 
47    Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
48    SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
49    doesn't work for partitions on which auditing is enabled.  If
50    you're still using an obsolete system with these problems, please
51    send email to the maintainer of this code.  */
52 
53 #if !defined HAVE_FCHDIR && !defined fchdir
54 # define fchdir(fd) (-1)
55 #endif
56 
57 int
save_cwd(struct saved_cwd * cwd)58 save_cwd (struct saved_cwd *cwd)
59 {
60   cwd->desc = open (".", O_SEARCH | O_CLOEXEC);
61   /* The 'name' member is present only to minimize differences from
62      gnulib.  Initialize it to zero, if only to simplify debugging.  */
63   cwd->name = 0;
64   return 0;
65 }
66 
67 /* Change to recorded location, CWD, in directory hierarchy.
68    Upon failure, return -1 (errno is set by chdir or fchdir).
69    Upon success, return zero.  */
70 
71 int
restore_cwd(const struct saved_cwd * cwd)72 restore_cwd (const struct saved_cwd *cwd)
73 {
74   /* Restore the previous directory if possible, to avoid tying down
75      the file system of the new directory (Bug#18232).
76      Don't worry if fchdir fails, as Emacs doesn't care what the
77      working directory is.  The fchdir call is inside an 'if' merely to
78      pacify compilers that complain if fchdir's return value is ignored.  */
79   if (fchdir (cwd->desc) == 0)
80     return 0;
81 
82   return 0;
83 }
84 
85 void
free_cwd(struct saved_cwd * cwd)86 free_cwd (struct saved_cwd *cwd)
87 {
88   close (cwd->desc);
89 }
90