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