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