@@ -26,8 +26,8 @@ Author: CM Wintersteiger
26
26
#include < unistd.h>
27
27
#endif
28
28
29
- #include " invariant.h"
30
29
#include " file_util.h"
30
+ #include " exception_utils.h"
31
31
32
32
std::string get_temporary_directory (const std::string &name_template)
33
33
{
@@ -38,22 +38,26 @@ std::string get_temporary_directory(const std::string &name_template)
38
38
char lpPathBuffer[MAX_PATH+1 ];
39
39
DWORD dwRetVal = GetTempPathA (dwBufSize, lpPathBuffer);
40
40
41
- if (dwRetVal > dwBufSize || (dwRetVal == 0 ))
42
- throw " GetTempPath failed" ; // NOLINT(readability/throw)
41
+ if (dwRetVal > dwBufSize || (dwRetVal == 0 )) {
42
+ throw system_exceptiont (" Couldn't get temporary path" );
43
+ }
43
44
44
45
// GetTempFileNameA produces <path>\<pre><uuuu>.TMP
45
46
// where <pre> = "TLO"
46
47
// Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
47
48
48
49
char t[MAX_PATH];
49
50
UINT uRetVal=GetTempFileNameA (lpPathBuffer, " TLO" , 0 , t);
50
- if (uRetVal == 0 )
51
- throw " GetTempFileName failed" ; // NOLINT(readability/throw)
51
+ if (uRetVal == 0 ) {
52
+ throw system_exceptiont (" Couldn't get new temporary file name in directory"
53
+ + lpPathBuffer);
54
+ }
52
55
53
56
unlink (t);
54
- if (_mkdir (t)!=0 )
55
- throw " _mkdir failed" ;
56
-
57
+ if (_mkdir (t)!=0 ) {
58
+ throw system_exceptiont (" Couldn't create temporary directory at "
59
+ + t);
60
+ }
57
61
result=std::string (t);
58
62
59
63
#else
@@ -69,7 +73,7 @@ std::string get_temporary_directory(const std::string &name_template)
69
73
t.push_back (' \0 ' ); // add the zero
70
74
const char *td = mkdtemp (t.data ());
71
75
if (!td)
72
- throw " mkdtemp failed " ;
76
+ throw system_exceptiont ( " Failed to create temporary directory " ) ;
73
77
result=std::string (td);
74
78
#endif
75
79
@@ -101,11 +105,13 @@ temp_working_dirt::temp_working_dirt(const std::string &name_template):
101
105
{
102
106
old_working_directory=get_current_working_directory ();
103
107
if (chdir (path.c_str ())!=0 )
104
- CHECK_RETURN ( false );
108
+ throw system_exceptiont ( " Couldn't change directory to " + path );
105
109
}
106
110
107
- temp_working_dirt::~temp_working_dirt ()
111
+ temp_working_dirt::~temp_working_dirt () noexcept ( false )
108
112
{
113
+ // FIXME throwing from destructors is generally a bad idea
114
+ // but I can't think of anything else that'd be reasonable here?
109
115
if (chdir (old_working_directory.c_str ())!=0 )
110
- CHECK_RETURN ( false );
116
+ throw system_exceptiont ( " Couldn't change directory to " + path );
111
117
}
0 commit comments