1 /*
2    Copyright 2009 Sun Microsystems, Inc.
3 
4    Use is subject to license terms.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License, version 2.0,
8    as published by the Free Software Foundation.
9 
10    This program is also distributed with certain software (including
11    but not limited to OpenSSL) that is licensed under separate terms,
12    as designated in a particular file or component or in included license
13    documentation.  The authors of MySQL hereby grant you an additional
14    permission to link the program and your derivative works with the
15    separately licensed software that they have included with MySQL.
16 
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License, version 2.0, for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
25  */
26 
27 #ifndef NDBT_WORKINGDIR_HPP
28 #define NDBT_WORKINGDIR_HPP
29 
30 #include <NdbDir.hpp>
31 #include <BaseString.hpp>
32 
33 class NDBT_Workingdir
34 {
35   NdbDir::Temp m_temp;
36   BaseString m_wd;
37 public:
38 
NDBT_Workingdir(const char * dirname)39   NDBT_Workingdir(const char* dirname)
40   {
41     const char* tmp_path = m_temp.path();
42     char* ndbt_tmp = getenv("NDBT_TMP_DIR");
43     if (ndbt_tmp)
44       tmp_path = ndbt_tmp;
45     require(tmp_path);
46 
47     m_wd.assfmt("%s%s%s%d", tmp_path, DIR_SEPARATOR, dirname,
48                 (int)NdbProcess::getpid());
49     if (access(m_wd.c_str(), F_OK) == 0)
50       NdbDir::remove_recursive(m_wd.c_str());
51     if (!NdbDir::create(m_wd.c_str()))
52       abort();
53   }
54 
~NDBT_Workingdir()55   ~NDBT_Workingdir()
56   {
57     if (access(m_wd.c_str(), F_OK) == 0)
58       NdbDir::remove_recursive(m_wd.c_str());
59   }
60 
path(void) const61   const char* path(void) const
62   {
63     return m_wd.c_str();
64   }
65 
66 };
67 
68 #endif
69