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 <util/pragma_push.def>
13 #ifdef _MSC_VER
14 #pragma warning(disable:4668)
15  // using #if/#elif on undefined macro
16 #endif
17 #include <windows.h>
18 #include <io.h>
19 #include <direct.h>
20 #include <util/pragma_pop.def>
21 #endif
22 
23 #include <cstdlib>
24 #include <vector>
25 
26 #if defined(__linux__) || \
27  defined(__FreeBSD_kernel__) || \
28  defined(__GNU__) || \
29  defined(__unix__) || \
30  defined(__CYGWIN__) || \
31  defined(__MACH__)
32 #include <unistd.h>
33 #endif
34 
35 #include "exception_utils.h"
36 #include "file_util.h"
37 
38 std::string get_temporary_directory(const std::string &name_template)
39 {
40  std::string result;
41 
42 #ifdef _WIN32
43  (void)name_template; // unused parameter
44  DWORD dwBufSize = MAX_PATH + 1;
45  char lpPathBuffer[MAX_PATH + 1];
46  DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
47 
48  if(dwRetVal > dwBufSize || (dwRetVal == 0))
49  {
50  throw system_exceptiont("Couldn't get temporary path");
51  }
52 
53  // GetTempFileNameA produces <path><pre><uuuu>.TMP
54  // where <pre> = "TLO"
55  // Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
56 
57  char t[MAX_PATH];
58  UINT uRetVal = GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
59  if(uRetVal == 0)
60  {
61  throw system_exceptiont(
62  std::string("Couldn't get new temporary file name in directory") +
63  lpPathBuffer);
64  }
65 
66  unlink(t);
67  if(_mkdir(t) != 0)
68  {
69  throw system_exceptiont(
70  std::string("Couldn't create temporary directory at ") + t);
71  }
72  result = std::string(t);
73 
74 #else
75  std::string prefixed_name_template = "/tmp/";
76  const char *TMPDIR_env = getenv("TMPDIR");
77  if(TMPDIR_env != nullptr)
78  prefixed_name_template = TMPDIR_env;
79  if(*prefixed_name_template.rbegin() != '/')
80  prefixed_name_template += '/';
81  prefixed_name_template += name_template;
82 
83  std::vector<char> t(
84  prefixed_name_template.begin(), prefixed_name_template.end());
85  t.push_back('\0'); // add the zero
86  const char *td = mkdtemp(t.data());
87  if(!td)
88  throw system_exceptiont("Failed to create temporary directory");
89  result = std::string(td);
90 #endif
91 
92  return result;
93 }
94 
95 temp_dirt::temp_dirt(const std::string &name_template)
96 {
97  path=get_temporary_directory(name_template);
98 }
99 
100 std::string temp_dirt::operator()(const std::string &file)
101 {
102  return concat_dir_file(path, file);
103 }
104 
106 {
108 }
109 
111 {
112  clear();
113 }
std::string concat_dir_file(const std::string &directory, const std::string &file_name)
Definition: file_util.cpp:141
void clear()
Definition: tempdir.cpp:105
~temp_dirt()
Definition: tempdir.cpp:110
temp_dirt(const std::string &name_template)
Definition: tempdir.cpp:95
std::string operator()(const std::string &file)
Definition: tempdir.cpp:100
void delete_directory(const std::string &path)
deletes all files in 'path' and then the directory itself
Definition: file_util.cpp:100
Thrown when some external system fails unexpectedly.
std::string get_temporary_directory(const std::string &name_template)
Definition: tempdir.cpp:38
std::string path
Definition: tempdir.h:22
Definition: kdev_t.h:19