| 708 | } |
| 709 | |
| 710 | bool Path_WriteStringToTextFileAtomic( const std::string &strFilename, const char *pchData ) |
| 711 | { |
| 712 | std::string strTmpFilename = strFilename + ".tmp"; |
| 713 | |
| 714 | if ( !Path_WriteStringToTextFile( strTmpFilename, pchData ) ) |
| 715 | return false; |
| 716 | |
| 717 | // Platform specific atomic file replacement |
| 718 | #if defined( _WIN32 ) |
| 719 | std::wstring wsFilename = UTF8to16( strFilename.c_str() ); |
| 720 | std::wstring wsTmpFilename = UTF8to16( strTmpFilename.c_str() ); |
| 721 | if ( !::ReplaceFileW( wsFilename.c_str(), wsTmpFilename.c_str(), nullptr, 0, 0, 0 ) ) |
| 722 | { |
| 723 | // if we couldn't ReplaceFile, try a non-atomic write as a fallback |
| 724 | if ( !Path_WriteStringToTextFile( strFilename, pchData ) ) |
| 725 | return false; |
| 726 | } |
| 727 | #elif defined( POSIX ) |
| 728 | if ( rename( strTmpFilename.c_str(), strFilename.c_str() ) == -1 ) |
| 729 | return false; |
| 730 | #else |
| 731 | #error Do not know how to write atomic file |
| 732 | #endif |
| 733 | |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | #if defined(WIN32) |
nothing calls this directly
no test coverage detected