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