1 /* save-cwd.c -- Save and restore current working directory.
3 Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Jim Meyering <meyering@na-net.ornl.gov>. */
22 Bacula® - The Network Backup Solution
24 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
26 The main author of Bacula is Kern Sibbald, with contributions from
27 many others, a complete list can be found in the file AUTHORS.
28 This program is Free Software; you can redistribute it and/or
29 modify it under the terms of version two of the GNU General Public
30 License as published by the Free Software Foundation plus additions
31 that are listed in the file LICENSE.
33 This program is distributed in the hope that it will be useful, but
34 WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 General Public License for more details.
38 You should have received a copy of the GNU General Public License
39 along with this program; if not, write to the Free Software
40 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
43 Bacula® is a registered trademark of John Walker.
44 The licensor of Bacula is the Free Software Foundation Europe
45 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
46 Switzerland, email:ftf@fsfeurope.org.
55 /* Record the location of the current working directory in CWD so that
56 the program may change to other directories and later use restore_cwd
57 to return to the recorded location. This function may allocate
58 space using malloc (via xgetcwd) or leave a file descriptor open;
59 use free_cwd to perform the necessary free or close. Upon failure,
60 no memory is allocated, any locally opened file descriptors are
61 closed; return non-zero -- in that case, free_cwd need not be
62 called, but doing so is ok. Otherwise, return zero. */
65 save_cwd(struct saved_cwd *cwd)
67 static int have_working_fchdir = 1;
72 if (have_working_fchdir) {
74 cwd->desc = open(".", O_RDONLY);
77 Emsg1(M_ERROR, 0, _("Cannot open current directory: %s\n"), be.strerror());
82 /* On SunOS 4, fchdir returns EINVAL if accounting is enabled,
83 so we have to fall back to chdir. */
84 if (fchdir(cwd->desc)) {
85 if (errno == EINVAL) {
88 have_working_fchdir = 0;
91 Emsg1(M_ERROR, 0, _("Current directory: %s\n"), be.strerror());
97 # endif /* __sun__ || sun */
99 # define fchdir(x) (abort (), 0)
100 have_working_fchdir = 0;
104 if (!have_working_fchdir) {
106 POOLMEM *buf = get_memory(MAX_PATH);
108 POOLMEM *buf = get_memory(5000);
110 cwd->name = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
111 if (cwd->name == NULL) {
113 Emsg1(M_ERROR, 0, _("Cannot get current directory: %s\n"), be.strerror());
114 free_pool_memory(buf);
121 /* Change to recorded location, CWD, in directory hierarchy.
122 If "saved working directory", NULL))
126 restore_cwd(const struct saved_cwd *cwd, const char *dest, const char *from)
129 if (cwd->desc >= 0) {
130 if (fchdir(cwd->desc)) {
134 Emsg3(M_ERROR, 0, _("Cannot return to %s from %s: %s\n"),
135 dest, from, be.strerror());
138 Emsg2(M_ERROR, 0, _("Cannot return to saved working directory from %s: %s\n"),
139 from, be.strerror());
144 Emsg2(M_ERROR, 0, _("Cannot return to %s: %s\n"),
145 dest, be.strerror());
148 Emsg1(M_ERROR, 0, _("Cannot return to saved working directory: %s\n"),
154 } else if (chdir(cwd->name) < 0) {
156 Emsg2(M_ERROR, 0, "%s: %s\n", cwd->name, be.strerror());
163 free_cwd(struct saved_cwd *cwd)
165 if (cwd->desc >= 0) {
169 free_pool_memory(cwd->name);