diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index fa457b788..ea66c3d8c 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -302,6 +302,7 @@ #include #include #include +#include #include // #include // Guarded by GTEST_IS_THREADSAFE below @@ -2112,6 +2113,24 @@ GTEST_DISABLE_MSC_DEPRECATED_PUSH_() !defined(GTEST_OS_XTENSA) && !defined(GTEST_OS_QURT) inline int ChDir(const char* dir) { return chdir(dir); } #endif +#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) +const int CREAT = _O_CREAT; +const int EXCL = _O_EXCL; +const int WRONLY = _O_WRONLY; +#else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW +const int CREAT = O_CREAT; +const int EXCL = O_EXCL; +const int WRONLY = O_WRONLY; +#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW +inline int Open(const char* path, int flags, int mode) { +#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) + std::wstring_convert converter; + std::wstring wide_path = converter.from_bytes(path); + return _wopen(wide_path.c_str(), flags, mode); +#else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW + return open(path, flags, mode); +#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW +} inline FILE* FOpen(const char* path, const char* mode) { #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) struct wchar_codecvt : public std::codecvt {}; diff --git a/googletest/src/gtest-filepath.cc b/googletest/src/gtest-filepath.cc index 902d8c7f6..cdb1c0375 100644 --- a/googletest/src/gtest-filepath.cc +++ b/googletest/src/gtest-filepath.cc @@ -312,12 +312,20 @@ bool FilePath::IsAbsolutePath() const { return CalculateRootLength() > 0; } FilePath FilePath::GenerateUniqueFileName(const FilePath& directory, const FilePath& base_name, const char* extension) { + // Make sure the directory does exist + directory.CreateDirectoriesRecursively(); + FilePath full_pathname; int number = 0; - do { + for (;;) { full_pathname.Set(MakeFileName(directory, base_name, number++, extension)); - } while (full_pathname.FileOrDirectoryExists()); - return full_pathname; + int fd = posix::Open(full_pathname.c_str(), + posix::CREAT | posix::EXCL | posix::WRONLY, 0644); + if (fd != -1) { + posix::Close(fd); + return full_pathname; + } + } } // Returns true if FilePath ends with a path separator, which indicates that