Creates empty file at path. The path must be writable, we do not try to create directories in path because that's hard in C++14.
| 146 | // Creates empty file at path. The path must be writable, we do not |
| 147 | // try to create directories in path because that's hard in C++14. |
| 148 | void setUpGuardFile( std::string const& guardFilePath ) { |
| 149 | if ( !guardFilePath.empty() ) { |
| 150 | #if defined( _MSC_VER ) |
| 151 | std::FILE* file = nullptr; |
| 152 | if ( fopen_s( &file, guardFilePath.c_str(), "w" ) ) { |
| 153 | char msgBuffer[100]; |
| 154 | const auto err = errno; |
| 155 | std::string errMsg; |
| 156 | if ( !strerror_s( msgBuffer, err ) ) { |
| 157 | errMsg = msgBuffer; |
| 158 | } else { |
| 159 | errMsg = "Could not translate errno to a string"; |
| 160 | } |
| 161 | |
| 162 | #else |
| 163 | std::FILE* file = std::fopen( guardFilePath.c_str(), "w" ); |
| 164 | if ( !file ) { |
| 165 | const auto err = errno; |
| 166 | const char* errMsg = std::strerror( err ); |
| 167 | #endif |
| 168 | |
| 169 | CATCH_RUNTIME_ERROR( "Could not open the exit guard file '" |
| 170 | << guardFilePath << "' because '" |
| 171 | << errMsg << "' (" << err << ')' ); |
| 172 | } |
| 173 | const int ret = std::fclose( file ); |
| 174 | CATCH_ENFORCE( |
| 175 | ret == 0, |
| 176 | "Error when closing the exit guard file: " << ret ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Removes file at path. Assuming we created it in setUpGuardFile. |
| 181 | void tearDownGuardFile( std::string const& guardFilePath ) { |
| 182 | if ( !guardFilePath.empty() ) { |
| 183 | const int ret = std::remove( guardFilePath.c_str() ); |
| 184 | CATCH_ENFORCE( |
| 185 | ret == 0, |
| 186 | "Error when removing the exit guard file: " << ret ); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } // anon namespace |
| 191 | |
| 192 | Session::Session() { |
| 193 | static bool alreadyInstantiated = false; |