save-cwd.c 3.3 KB

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