]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/save-cwd.c
Apply win32 fixes + add tapetest.c
[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           Emsg1(M_ERROR, 0, "Cannot open current directory: %s\n", strerror(errno));
48           return 1;
49         }
50
51 # if __sun__ || sun
52       /* On SunOS 4, fchdir returns EINVAL if accounting is enabled,
53          so we have to fall back to chdir.  */
54       if (fchdir(cwd->desc)) {
55           if (errno == EINVAL) {
56               close(cwd->desc);
57               cwd->desc = -1;
58               have_working_fchdir = 0;
59           } else {
60               Emsg1(M_ERROR, 0, "Current directory: %s\n", strerror(errno));
61               close(cwd->desc);
62               cwd->desc = -1;
63               return 1;
64           }
65       }
66 # endif /* __sun__ || sun */
67 #else
68 # define fchdir(x) (abort (), 0)
69       have_working_fchdir = 0;
70 #endif
71     }
72
73   if (!have_working_fchdir) {
74 #ifdef HAVE_WIN32
75       POOLMEM *buf = get_memory(MAX_PATH);
76 #else
77       POOLMEM *buf = get_memory(5000);
78 #endif
79       cwd->name = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
80       if (cwd->name == NULL) {
81           Emsg1(M_ERROR, 0, "Cannot get current directory: %s\n", strerror(errno));
82           free_pool_memory(buf);
83           return 1;
84       }
85   }
86   return 0;
87 }
88
89 /* Change to recorded location, CWD, in directory hierarchy.
90    If "saved working directory", NULL))
91    */
92
93 int
94 restore_cwd(const struct saved_cwd *cwd, const char *dest, const char *from)
95 {
96   int fail = 0;
97   if (cwd->desc >= 0) {
98       if (fchdir(cwd->desc)) {
99           Emsg4(M_ERROR, 0, "Cannot return to %s%s%s: %s\n", 
100                  (dest ? dest : "saved working directory"),
101                  (from ? " from " : ""),
102                  (from ? from : ""), strerror(errno));
103           fail = 1;
104       }
105   } else if (chdir(cwd->name) < 0) {
106       Emsg2(M_ERROR, 0, "%s: %s\n", cwd->name, strerror(errno));
107       fail = 1;
108   }
109   return fail;
110 }
111
112 void
113 free_cwd(struct saved_cwd *cwd)
114 {
115   if (cwd->desc >= 0) {
116      close(cwd->desc);
117   }
118   if (cwd->name) {
119      free_pool_memory(cwd->name);
120   }
121 }