Import upstream version 1.28
[debian/tar] / gnu / save-cwd.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* save-cwd.c -- Save and restore current working directory.
4
5    Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2014 Free Software
6    Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* Written by Jim Meyering.  */
22
23 #include <config.h>
24
25 #include "save-cwd.h"
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "chdir-long.h"
34 #include "unistd--.h"
35 #include "cloexec.h"
36
37 #if GNULIB_FCNTL_SAFER
38 # include "fcntl--.h"
39 #else
40 # define GNULIB_FCNTL_SAFER 0
41 #endif
42
43 /* Record the location of the current working directory in CWD so that
44    the program may change to other directories and later use restore_cwd
45    to return to the recorded location.  This function may allocate
46    space using malloc (via getcwd) or leave a file descriptor open;
47    use free_cwd to perform the necessary free or close.  Upon failure,
48    no memory is allocated, any locally opened file descriptors are
49    closed;  return non-zero -- in that case, free_cwd need not be
50    called, but doing so is ok.  Otherwise, return zero.
51
52    The _raison d'etre_ for this interface is that the working directory
53    is sometimes inaccessible, and getcwd is not robust or as efficient.
54    So, we prefer to use the open/fchdir approach, but fall back on
55    getcwd if necessary.  This module works for most cases with just
56    the getcwd-lgpl module, but to be truly robust, use the getcwd module.
57
58    Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
59    SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
60    doesn't work for partitions on which auditing is enabled.  If
61    you're still using an obsolete system with these problems, please
62    send email to the maintainer of this code.  */
63
64 int
65 save_cwd (struct saved_cwd *cwd)
66 {
67   cwd->name = NULL;
68
69   cwd->desc = open (".", O_SEARCH);
70   if (!GNULIB_FCNTL_SAFER)
71     cwd->desc = fd_safer (cwd->desc);
72   if (cwd->desc < 0)
73     {
74       cwd->name = getcwd (NULL, 0);
75       return cwd->name ? 0 : -1;
76     }
77
78   set_cloexec_flag (cwd->desc, true);
79   return 0;
80 }
81
82 /* Change to recorded location, CWD, in directory hierarchy.
83    Upon failure, return -1 (errno is set by chdir or fchdir).
84    Upon success, return zero.  */
85
86 int
87 restore_cwd (const struct saved_cwd *cwd)
88 {
89   if (0 <= cwd->desc)
90     return fchdir (cwd->desc);
91   else
92     return chdir_long (cwd->name);
93 }
94
95 void
96 free_cwd (struct saved_cwd *cwd)
97 {
98   if (cwd->desc >= 0)
99     close (cwd->desc);
100   free (cwd->name);
101 }