2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
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.
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.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
22 * Kern Sibbald, August MMVII
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.
35 static bool fchdir_failed = false; /* set if we get a fchdir failure */
37 static bool fchdir_failed = true; /* set if we get a fchdir failure */
41 * Save current working directory.
45 bool saveCWD::save(JCR *jcr)
47 release(); /* clean up */
49 m_fd = open(".", O_RDONLY);
52 Jmsg1(jcr, M_ERROR, 0, _("Cannot open current directory: ERR=%s\n"), be.bstrerror());
59 POOLMEM *buf = get_memory(5000);
60 m_cwd = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf));
63 Jmsg1(jcr, M_ERROR, 0, _("Cannot get current directory: ERR=%s\n"), be.bstrerror());
64 free_pool_memory(buf);
74 * Restore previous working directory.
78 bool saveCWD::restore(JCR *jcr)
85 if (fchdir(m_fd) != 0) {
87 Jmsg1(jcr, M_ERROR, 0, _("Cannot reset current directory: ERR=%s\n"), be.bstrerror());
91 chdir("/"); /* punt */
96 if (chdir(m_cwd) < 0) {
98 Jmsg1(jcr, M_ERROR, 0, _("Cannot reset current directory: ERR=%s\n"), be.bstrerror());
100 free_pool_memory(m_cwd);
107 void saveCWD::release()
118 free_pool_memory(m_cwd);