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 <windows.h>
13 #include <io.h>
14 #include <direct.h>
15 #endif
16 
17 #include <cassert>
18 #include <cstdlib>
19 #include <cstring>
20 
21 #if defined(__linux__) || \
22  defined(__FreeBSD_kernel__) || \
23  defined(__GNU__) || \
24  defined(__unix__) || \
25  defined(__CYGWIN__) || \
26  defined(__MACH__)
27 #include <unistd.h>
28 #endif
29 
30 #include "file_util.h"
31 
32 std::string get_temporary_directory(const std::string &name_template)
33 {
34  std::string result;
35 
36  #ifdef _WIN32
37  DWORD dwBufSize = MAX_PATH;
38  char lpPathBuffer[MAX_PATH];
39  DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
40 
41  if(dwRetVal > dwBufSize || (dwRetVal == 0))
42  throw "GetTempPath failed"; // NOLINT(readability/throw)
43 
44  char t[MAX_PATH];
45 
46  strncpy(t, name_template.c_str(), MAX_PATH);
47 
48  UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
49  if(uRetVal == 0)
50  throw "GetTempFileName failed"; // NOLINT(readability/throw)
51 
52  unlink(t);
53  if(_mkdir(t)!=0)
54  throw "_mkdir failed";
55 
56  result=std::string(t);
57 
58  #else
59  std::string prefixed_name_template="/tmp/";
60  const char *TMPDIR_env=getenv("TMPDIR");
61  if(TMPDIR_env!=nullptr)
62  prefixed_name_template=TMPDIR_env;
63  if(*prefixed_name_template.rbegin()!='/')
64  prefixed_name_template+='/';
65  prefixed_name_template+=name_template;
66 
67  char t[1000];
68  strncpy(t, prefixed_name_template.c_str(), 1000);
69  const char *td = mkdtemp(t);
70  if(!td)
71  throw "mkdtemp failed";
72  result=std::string(td);
73  #endif
74 
75  return result;
76 }
77 
78 temp_dirt::temp_dirt(const std::string &name_template)
79 {
80  path=get_temporary_directory(name_template);
81 }
82 
83 std::string temp_dirt::operator()(const std::string &file)
84 {
85  return concat_dir_file(path, file);
86 }
87 
89 {
91 }
92 
94 {
95  clear();
96 }
97 
98 temp_working_dirt::temp_working_dirt(const std::string &name_template):
99  temp_dirt(name_template)
100 {
102  if(chdir(path.c_str())!=0)
103  assert(false);
104 }
105 
107 {
108  if(chdir(old_working_directory.c_str())!=0)
109  assert(false);
110 }
std::string concat_dir_file(const std::string &directory, const std::string &file_name)
Definition: file_util.cpp:128
void clear()
Definition: tempdir.cpp:88
~temp_dirt()
Definition: tempdir.cpp:93
std::string get_current_working_directory()
Definition: file_util.cpp:44
temp_dirt(const std::string &name_template)
Definition: tempdir.cpp:78
std::string operator()(const std::string &file)
Definition: tempdir.cpp:83
void delete_directory(const std::string &path)
deletes all files in &#39;path&#39; and then the directory itself
Definition: file_util.cpp:95
temp_working_dirt(const std::string &name_template)
Definition: tempdir.cpp:98
std::string old_working_directory
Definition: tempdir.h:39
std::string get_temporary_directory(const std::string &name_template)
Definition: tempdir.cpp:32
std::string path
Definition: tempdir.h:22
Definition: kdev_t.h:19