]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/save-cwd.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[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 #include "bacula.h"
22 #include "save-cwd.h"
23
24
25
26 /* Record the location of the current working directory in CWD so that
27    the program may change to other directories and later use restore_cwd
28    to return to the recorded location.  This function may allocate
29    space using malloc (via xgetcwd) or leave a file descriptor open;
30    use free_cwd to perform the necessary free or close.  Upon failure,
31    no memory is allocated, any locally opened file descriptors are
32    closed;  return non-zero -- in that case, free_cwd need not be
33    called, but doing so is ok.  Otherwise, return zero.  */
34
35 int
36 save_cwd(struct saved_cwd *cwd)
37 {
38   static int have_working_fchdir = 1;
39
40   cwd->desc = -1;
41   cwd->name = NULL;
42
43   if (have_working_fchdir) {
44 #if HAVE_FCHDIR
45       cwd->desc = open(".", O_RDONLY);
46       if (cwd->desc < 0) {
47          berrno be;
48          Emsg1(M_ERROR, 0, "Cannot open current directory: %s\n", be.strerror());
49          return 1;
50       }
51
52 # if __sun__ || sun
53       /* On SunOS 4, fchdir returns EINVAL if accounting is enabled,
54          so we have to fall back to chdir.  */
55       if (fchdir(cwd->desc)) {
56           if (errno == EINVAL) {
57               close(cwd->desc);
58               cwd->desc = -1;
59               have_working_fchdir = 0;
60           } else {
61               berrno be;
62               Emsg1(M_ERROR, 0, "Current directory: %s\n", be.strerror());
63               close(cwd->desc);
64               cwd->desc = -1;
65               return 1;
66           }
67       }
68 # endif /* __sun__ || sun */
69 #else
70 # define fchdir(x) (abort (), 0)
71       have_working_fchdir = 0;
72 #endif
73     }
74
75   if (!have_working_fchdir) {
76 #ifdef HAVE_WIN32
77       POOLMEM *buf = get_memory(MAX_PATH);
78 #else
79       POOLMEM *buf = get_memory(5000);
80 #endif
81       cwd->name = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
82       if (cwd->name == NULL) {
83          berrno be;
84          Emsg1(M_ERROR, 0, "Cannot get current directory: %s\n", be.strerror());
85          free_pool_memory(buf);
86          return 1;
87       }
88   }
89   return 0;
90 }
91
92 /* Change to recorded location, CWD, in directory hierarchy.
93    If "saved working directory", NULL))
94    */
95
96 int
97 restore_cwd(const struct saved_cwd *cwd, const char *dest, const char *from)
98 {
99   int fail = 0;
100   if (cwd->desc >= 0) {
101       if (fchdir(cwd->desc)) {
102          berrno be;
103          Emsg4(M_ERROR, 0, "Cannot return to %s%s%s: %s\n",
104                  (dest ? dest : "saved working directory"),
105                  (from ? " from " : ""),
106                  (from ? from : ""), be.strerror());
107          fail = 1;
108       }
109   } else if (chdir(cwd->name) < 0) {
110       berrno be;
111       Emsg2(M_ERROR, 0, "%s: %s\n", cwd->name, be.strerror());
112       fail = 1;
113   }
114   return fail;
115 }
116
117 void
118 free_cwd(struct saved_cwd *cwd)
119 {
120   if (cwd->desc >= 0) {
121      close(cwd->desc);
122   }
123   if (cwd->name) {
124      free_pool_memory(cwd->name);
125   }
126 }