cprover
tempdir.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: CM Wintersteiger
6 
7 \*******************************************************************/
8 
9 #include "tempdir.h"
10 
11 #ifdef _WIN32
12 #include <windows.h>
13 #include <io.h>
14 #include <direct.h>
15 #endif
16 
17 #include <cstdlib>
18 #include <vector>
19 
20 #if defined(__linux__) || \
21  defined(__FreeBSD_kernel__) || \
22  defined(__GNU__) || \
23  defined(__unix__) || \
24  defined(__CYGWIN__) || \
25  defined(__MACH__)
26 #include <unistd.h>
27 #endif
28 
29 #include "invariant.h"
30 #include "file_util.h"
31 
32 std::string get_temporary_directory(const std::string &name_template)
33 {
34  std::string result;
35 
36  #ifdef _WIN32
37  DWORD dwBufSize = MAX_PATH+1;
38  char lpPathBuffer[MAX_PATH+1];
39  DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
40 
41  if(dwRetVal > dwBufSize || (dwRetVal == 0))
42  throw "GetTempPath failed"; // NOLINT(readability/throw)
43 
44  // GetTempFileNameA produces <path><pre><uuuu>.TMP
45  // where <pre> = "TLO"
46  // Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
47 
48  char t[MAX_PATH];
49  UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
50  if(uRetVal == 0)
51  throw "GetTempFileName failed"; // NOLINT(readability/throw)
52 
53  unlink(t);
54  if(_mkdir(t)!=0)
55  throw "_mkdir failed";
56 
57  result=std::string(t);
58 
59  #else
60  std::string prefixed_name_template="/tmp/";
61  const char *TMPDIR_env=getenv("TMPDIR");
62  if(TMPDIR_env!=nullptr)
63  prefixed_name_template=TMPDIR_env;
64  if(*prefixed_name_template.rbegin()!='/')
65  prefixed_name_template+='/';
66  prefixed_name_template+=name_template;
67 
68  std::vector<char> t(prefixed_name_template.begin(), prefixed_name_template.end());
69  t.push_back('\0'); // add the zero
70  const char *td = mkdtemp(t.data());
71  if(!td)
72  throw "mkdtemp failed";
73  result=std::string(td);
74  #endif
75 
76  return result;
77 }
78 
79 temp_dirt::temp_dirt(const std::string &name_template)
80 {
81  path=get_temporary_directory(name_template);
82 }
83 
84 std::string temp_dirt::operator()(const std::string &file)
85 {
86  return concat_dir_file(path, file);
87 }
88 
90 {
92 }
93 
95 {
96  clear();
97 }
98 
99 temp_working_dirt::temp_working_dirt(const std::string &name_template):
100  temp_dirt(name_template)
101 {
103  if(chdir(path.c_str())!=0)
104  CHECK_RETURN(false);
105 }
106 
108 {
109  if(chdir(old_working_directory.c_str())!=0)
110  CHECK_RETURN(false);
111 }
std::string concat_dir_file(const std::string &directory, const std::string &file_name)
Definition: file_util.cpp:134
void clear()
Definition: tempdir.cpp:89
~temp_dirt()
Definition: tempdir.cpp:94
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:266
std::string get_current_working_directory()
Definition: file_util.cpp:45
temp_dirt(const std::string &name_template)
Definition: tempdir.cpp:79
std::string operator()(const std::string &file)
Definition: tempdir.cpp:84
void delete_directory(const std::string &path)
deletes all files in &#39;path&#39; and then the directory itself
Definition: file_util.cpp:95
temp_working_dirt(const std::string &name_template)
Definition: tempdir.cpp:99
std::string old_working_directory
Definition: tempdir.h:39
std::string get_temporary_directory(const std::string &name_template)
Definition: tempdir.cpp:32
std::string path
Definition: tempdir.h:22
Definition: kdev_t.h:19