]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/savecwd.c
2c64cc708628781329ead7b0ea2d61604dcc8c63
[bacula/bacula] / bacula / src / findlib / savecwd.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20
21 /*
22  *  Kern Sibbald, August MMVII
23  *
24  */
25
26 #include "bacula.h"
27 #include "savecwd.h"
28
29 /*
30  * Attempt to save the current working directory by various means so that
31  *  we can optimize code by doing a cwd and then restore the cwd.
32  */
33
34 #ifdef HAVE_FCHDIR
35 static bool fchdir_failed = false;          /* set if we get a fchdir failure */
36 #else
37 static bool fchdir_failed = true;           /* set if we get a fchdir failure */
38 #endif
39
40 /*
41  * Save current working directory.
42  * Returns: true if OK
43  *          false if failed
44  */
45 bool saveCWD::save(JCR *jcr)
46 {
47    release();                                /* clean up */
48    if (!fchdir_failed) {
49       m_fd = open(".", O_RDONLY);
50       if (m_fd < 0) {
51          berrno be;
52          Jmsg1(jcr, M_ERROR, 0, _("Cannot open current directory: ERR=%s\n"), be.bstrerror());
53          m_saved = false;
54          return false;
55       }
56    }
57
58    if (fchdir_failed) {
59       POOLMEM *buf = get_memory(5000);
60       m_cwd = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
61       if (m_cwd == NULL) {
62          berrno be;
63          Jmsg1(jcr, M_ERROR, 0, _("Cannot get current directory: ERR=%s\n"), be.bstrerror());
64          free_pool_memory(buf);
65          m_saved = false;
66          return false;
67       }
68    }
69    m_saved = true;
70    return true;
71 }
72
73 /*
74  * Restore previous working directory.
75  * Returns: true if OK
76  *          false if failed
77  */
78 bool saveCWD::restore(JCR *jcr)
79 {
80    if (!m_saved) {
81       return true;
82    }
83    m_saved = false;
84    if (m_fd >= 0) {
85       if (fchdir(m_fd) != 0) {
86          berrno be;
87          Jmsg1(jcr, M_ERROR, 0, _("Cannot reset current directory: ERR=%s\n"), be.bstrerror());
88          close(m_fd);
89          m_fd = -1;
90          fchdir_failed = true;
91          chdir("/");                  /* punt */
92          return false;
93       }
94       return true;
95    }
96    if (chdir(m_cwd) < 0) {
97       berrno be;
98       Jmsg1(jcr, M_ERROR, 0, _("Cannot reset current directory: ERR=%s\n"), be.bstrerror());
99       chdir("/");
100       free_pool_memory(m_cwd);
101       m_cwd = NULL;
102       return false;
103    }
104    return true;
105 }
106
107 void saveCWD::release()
108 {
109    if (!m_saved) {
110       return;
111    }
112    m_saved = false;
113    if (m_fd >= 0) {
114       close(m_fd);
115       m_fd = -1;
116    }
117    if (m_cwd) {
118       free_pool_memory(m_cwd);
119       m_cwd = NULL;
120    }
121 }