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(__CYGWIN__) || \
32 defined(__MACH__)
33#include <unistd.h>
34#endif
35// clang-format on
36
37#include "exception_utils.h"
38#include "file_util.h"
39
40std::string get_temporary_directory(const std::string &name_template)
41{
42 std::string result;
43
44#ifdef _WIN32
45 (void)name_template; // unused parameter
47 char lpPathBuffer[MAX_PATH + 1];
49
50 if(dwRetVal > dwBufSize || (dwRetVal == 0))
51 {
52 throw system_exceptiont("Couldn't get temporary path");
53 }
54
55 // GetTempFileNameA produces <path><pre><uuuu>.TMP
56 // where <pre> = "TLO"
57 // Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
58
59 char t[MAX_PATH];
60 UINT uRetVal = GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
61 if(uRetVal == 0)
62 {
64 std::string("Couldn't get new temporary file name in directory") +
66 }
67
68 unlink(t);
69 if(_mkdir(t) != 0)
70 {
72 std::string("Couldn't create temporary directory at ") + t);
73 }
74 result = std::string(t);
75
76#else
77 std::string prefixed_name_template = "/tmp/";
78 const char *TMPDIR_env = getenv("TMPDIR");
79 if(TMPDIR_env != nullptr)
81 if(*prefixed_name_template.rbegin() != '/')
84
85 std::vector<char> t(
87 t.push_back('\0'); // add the zero
88 const char *td = mkdtemp(t.data());
89 if(!td)
90 throw system_exceptiont("Failed to create temporary directory");
91
92 errno = 0;
93 char *wd = realpath(td, nullptr);
94
95 if(wd == nullptr)
97 std::string("realpath failed: ") + std::strerror(errno));
98
99 result = std::string(wd);
100 free(wd);
101#endif
102
103 return result;
104}
105
110
111std::string temp_dirt::operator()(const std::string &file)
112{
113 return concat_dir_file(path, file);
114}
115
120
122{
123 clear();
124}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:563
Thrown when some external system fails unexpectedly.
temp_dirt(const std::string &name_template)
Definition tempdir.cpp:106
std::string operator()(const std::string &file)
Definition tempdir.cpp:111
std::string path
Definition tempdir.h:36
void clear()
Definition tempdir.cpp:116
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:40
std::string get_temporary_directory(const std::string &name_template)
Definition tempdir.cpp:40