CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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#include <filesystem>
12
13// clang-format off
14#ifndef _WIN32
15#if defined(__FreeBSD_kernel__) || \
16 defined(__CYGWIN__) || \
17 defined(__MACH__)
18#include <unistd.h>
19#endif
20
21#include <cstdlib>
22#include <cstring>
23#include <vector>
24#endif
25// clang-format on
26
27#include "exception_utils.h"
28
29std::string get_temporary_directory(const std::string &name_template)
30{
31 std::string result;
32
33#ifdef _WIN32
34 (void)name_template; // unused parameter
35 try
36 {
37 result = std::filesystem::temp_directory_path().string();
38 }
39 catch(const std::filesystem::filesystem_error &)
40 {
41 throw system_exceptiont("Failed to create temporary directory");
42 }
43#else
44 std::string prefixed_name_template = "/tmp/";
45 const char *TMPDIR_env = getenv("TMPDIR");
46 if(TMPDIR_env != nullptr)
48 if(*prefixed_name_template.rbegin() != '/')
51
52 std::vector<char> t(
54 t.push_back('\0'); // add the zero
55 const char *td = mkdtemp(t.data());
56 if(!td)
57 throw system_exceptiont("Failed to create temporary directory");
58
59 errno = 0;
60 char *wd = realpath(td, nullptr);
61
62 if(wd == nullptr)
64 std::string("realpath failed: ") + std::strerror(errno));
65
66 result = std::string(wd);
67 free(wd);
68#endif
69
70 return result;
71}
72
77
78std::string temp_dirt::operator()(const std::string &file)
79{
80 return std::filesystem::path(path).append(file).string();
81}
82
84{
85 std::filesystem::remove_all(path);
86}
87
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Thrown when some external system fails unexpectedly.
temp_dirt(const std::string &name_template)
Definition tempdir.cpp:73
std::string operator()(const std::string &file)
Definition tempdir.cpp:78
std::string path
Definition tempdir.h:36
void clear()
Definition tempdir.cpp:83
char * getenv(const char *name)
Definition stdlib.c:496
void free(void *ptr)
Definition stdlib.c:317
std::string get_temporary_directory(const std::string &name_template)
Definition tempdir.cpp:29
std::string get_temporary_directory(const std::string &name_template)
Definition tempdir.cpp:29