]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/save-cwd.c
Convert to pure GPL v2 license.
[bacula/bacula] / bacula / src / findlib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2
3    Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.
4
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)
8    any later version.
9
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.
14
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.  */
18
19 /* Written by Jim Meyering <meyering@na-net.ornl.gov>.  */
20
21 /*
22    Bacula® - The Network Backup Solution
23
24    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
25
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 and included
31    in the file LICENSE.
32
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.
37
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
41    02110-1301, USA.
42
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.
47 */
48
49
50 #include "bacula.h"
51 #include "save-cwd.h"
52
53
54
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.  */
63
64 int
65 save_cwd(struct saved_cwd *cwd)
66 {
67   static int have_working_fchdir = 1;
68
69   cwd->desc = -1;
70   cwd->name = NULL;
71
72   if (have_working_fchdir) {
73 #if HAVE_FCHDIR
74       cwd->desc = open(".", O_RDONLY);
75       if (cwd->desc < 0) {
76          berrno be;
77          Emsg1(M_ERROR, 0, _("Cannot open current directory: %s\n"), be.bstrerror());
78          return 1;
79       }
80
81 # if __sun__ || sun
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) {
86               close(cwd->desc);
87               cwd->desc = -1;
88               have_working_fchdir = 0;
89           } else {
90               berrno be;
91               Emsg1(M_ERROR, 0, _("Current directory: %s\n"), be.bstrerror());
92               close(cwd->desc);
93               cwd->desc = -1;
94               return 1;
95           }
96       }
97 # endif /* __sun__ || sun */
98 #else
99 # define fchdir(x) (abort (), 0)
100       have_working_fchdir = 0;
101 #endif
102     }
103
104   if (!have_working_fchdir) {
105 #ifdef HAVE_WIN32
106       POOLMEM *buf = get_memory(MAX_PATH);
107 #else
108       POOLMEM *buf = get_memory(5000);
109 #endif
110       cwd->name = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
111       if (cwd->name == NULL) {
112          berrno be;
113          Emsg1(M_ERROR, 0, _("Cannot get current directory: %s\n"), be.bstrerror());
114          free_pool_memory(buf);
115          return 1;
116       }
117   }
118   return 0;
119 }
120
121 /* Change to recorded location, CWD, in directory hierarchy.
122    If "saved working directory", NULL))
123    */
124
125 int
126 restore_cwd(const struct saved_cwd *cwd, const char *dest, const char *from)
127 {
128   int fail = 0;
129   if (cwd->desc >= 0) {
130       if (fchdir(cwd->desc)) {
131          berrno be;
132          if (from) {
133             if (dest) {
134                Emsg3(M_ERROR, 0, _("Cannot return to %s from %s: %s\n"),
135                   dest, from, be.bstrerror());
136             }
137             else {
138                Emsg2(M_ERROR, 0, _("Cannot return to saved working directory from %s: %s\n"),
139                   from, be.bstrerror());
140             }
141          }
142          else {
143             if (dest) {
144                Emsg2(M_ERROR, 0, _("Cannot return to %s: %s\n"),
145                   dest, be.bstrerror());
146             }
147             else {
148                Emsg1(M_ERROR, 0, _("Cannot return to saved working directory: %s\n"),
149                   be.bstrerror());
150             }
151          }
152          fail = 1;
153       }
154   } else if (chdir(cwd->name) < 0) {
155       berrno be;
156       Emsg2(M_ERROR, 0, "%s: %s\n", cwd->name, be.bstrerror());
157       fail = 1;
158   }
159   return fail;
160 }
161
162 void
163 free_cwd(struct saved_cwd *cwd)
164 {
165   if (cwd->desc >= 0) {
166      close(cwd->desc);
167   }
168   if (cwd->name) {
169      free_pool_memory(cwd->name);
170   }
171 }