cprover
Loading...
Searching...
No Matches
tempdir.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: 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
42std::string get_temporary_directory(const std::string &name_template)
43{
44 std::string result;
45
46#ifdef _WIN32
47 (void)name_template; // unused parameter
49 char lpPathBuffer[MAX_PATH + 1];
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 {
66 std::string("Couldn't get new temporary file name in directory") +
68 }
69
70 unlink(t);
71 if(_mkdir(t) != 0)
72 {
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)
83 if(*prefixed_name_template.rbegin() != '/')
86
87 std::vector<char> t(
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)
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
112
113std::string temp_dirt::operator()(const std::string &file)
114{
115 return concat_dir_file(path, file);
116}
117
122
124{
125 clear();
126}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:564
Thrown when some external system fails unexpectedly.
temp_dirt(const std::string &name_template)
Definition tempdir.cpp:108
std::string operator()(const std::string &file)
Definition tempdir.cpp:113
std::string path
Definition tempdir.h:36
void clear()
Definition tempdir.cpp:118
void delete_directory(const std::string &path)
deletes all files in 'path' and then the directory itself
std::string concat_dir_file(const std::string &directory, const std::string &file_name)
Definition kdev_t.h:19
std::string get_temporary_directory(const std::string &name_template)
Definition tempdir.cpp:42
std::string get_temporary_directory(const std::string &name_template)
Definition tempdir.cpp:42