]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/savecwd.c
First cut cd to dir during save and restore
[bacula/bacula] / bacula / src / findlib / savecwd.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    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, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28
29 /*
30  *  Kern Sibbald, August MMVII
31  *
32  */
33
34 #include "bacula.h"
35 #include "savecwd.h"
36
37 /* 
38  * Attempt to save the current working directory by various means so that
39  *  we can optimize code by doing a cwd and then restore the cwd.
40  */
41
42 #ifdef HAVE_FCHDIR
43 static bool fchdir_failed = false;          /* set if we get a fchdir failure */
44 #else
45 static bool fchdir_failed = true;           /* set if we get a fchdir failure */
46 #endif
47
48 /* 
49  * Save current working directory.
50  * Returns: true if OK
51  *          false if failed
52  */
53 bool saveCWD::save(JCR *jcr)
54 {
55    release();                                /* clean up */
56    if (!fchdir_failed) {
57       m_fd = open(".", O_RDONLY);
58       if (m_fd < 0) {
59          berrno be;
60          Jmsg1(jcr, M_ERROR, 0, _("Cannot open current directory: ERR=%s\n"), be.bstrerror());
61          m_saved = false;
62          return false;
63       }
64    }
65
66    if (fchdir_failed) {
67       POOLMEM *buf = get_memory(5000);
68       m_cwd = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
69       if (m_cwd == NULL) {
70          berrno be;
71          Jmsg1(jcr, M_ERROR, 0, _("Cannot get current directory: ERR=%s\n"), be.bstrerror());
72          free_pool_memory(buf);
73          m_saved = false;
74          return false;
75       }
76    }
77    m_saved = true;
78    return true;
79 }
80
81 /*
82  * Restore previous working directory.
83  * Returns: true if OK
84  *          false if failed
85  */
86 bool saveCWD::restore(JCR *jcr)
87 {
88    if (!m_saved) {
89       return true;
90    }
91    m_saved = false;
92    if (m_fd >= 0) {
93       if (fchdir(m_fd) != 0) {
94          berrno be;
95          Jmsg1(jcr, M_ERROR, 0, _("Cannot reset current directory: ERR=%s\n"), be.bstrerror());
96          close(m_fd);
97          m_fd = -1;
98          fchdir_failed = true;
99          chdir("/");                  /* punt */
100          return false;
101       }
102       return true;
103    }
104    if (chdir(m_cwd) < 0) {
105       berrno be;
106       Jmsg1(jcr, M_ERROR, 0, _("Cannot reset current directory: ERR=%s\n"), be.bstrerror());
107       chdir("/");
108       free_pool_memory(m_cwd);
109       m_cwd = NULL;
110       return false;
111    }
112    return true;
113 }       
114
115 void saveCWD::release()
116 {
117    if (!m_saved) {
118       return;
119    }
120    m_saved = false;
121    if (m_fd >= 0) {
122       close(m_fd);
123       m_fd = -1;
124    }
125    if (m_cwd) {
126       free_pool_memory(m_cwd);
127       m_cwd = NULL;
128    }
129 }