xref: /llvm-project/third-party/unittest/googletest/src/gtest-filepath.cc (revision a866ce789eb99da4d7a486eeb60a53be6c75f4fd)
1a11cd0d9STom Stellard // Copyright 2008, Google Inc.
2a11cd0d9STom Stellard // All rights reserved.
3a11cd0d9STom Stellard //
4a11cd0d9STom Stellard // Redistribution and use in source and binary forms, with or without
5a11cd0d9STom Stellard // modification, are permitted provided that the following conditions are
6a11cd0d9STom Stellard // met:
7a11cd0d9STom Stellard //
8a11cd0d9STom Stellard //     * Redistributions of source code must retain the above copyright
9a11cd0d9STom Stellard // notice, this list of conditions and the following disclaimer.
10a11cd0d9STom Stellard //     * Redistributions in binary form must reproduce the above
11a11cd0d9STom Stellard // copyright notice, this list of conditions and the following disclaimer
12a11cd0d9STom Stellard // in the documentation and/or other materials provided with the
13a11cd0d9STom Stellard // distribution.
14a11cd0d9STom Stellard //     * Neither the name of Google Inc. nor the names of its
15a11cd0d9STom Stellard // contributors may be used to endorse or promote products derived from
16a11cd0d9STom Stellard // this software without specific prior written permission.
17a11cd0d9STom Stellard //
18a11cd0d9STom Stellard // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a11cd0d9STom Stellard // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a11cd0d9STom Stellard // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a11cd0d9STom Stellard // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a11cd0d9STom Stellard // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a11cd0d9STom Stellard // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a11cd0d9STom Stellard // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a11cd0d9STom Stellard // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a11cd0d9STom Stellard // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a11cd0d9STom Stellard // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a11cd0d9STom Stellard // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a11cd0d9STom Stellard 
30a11cd0d9STom Stellard #include "gtest/internal/gtest-filepath.h"
31a11cd0d9STom Stellard 
32a11cd0d9STom Stellard #include <stdlib.h>
3354c1a9b2SZero Omega 
34*a866ce78SHaowei Wu #include <iterator>
35*a866ce78SHaowei Wu #include <string>
36*a866ce78SHaowei Wu 
37*a866ce78SHaowei Wu #include "gtest/gtest-message.h"
38*a866ce78SHaowei Wu #include "gtest/internal/gtest-port.h"
39*a866ce78SHaowei Wu 
40*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS_MOBILE
41a11cd0d9STom Stellard #include <windows.h>
42*a866ce78SHaowei Wu #elif defined(GTEST_OS_WINDOWS)
43a11cd0d9STom Stellard #include <direct.h>
44a11cd0d9STom Stellard #include <io.h>
45a11cd0d9STom Stellard #else
46a11cd0d9STom Stellard #include <limits.h>
47*a866ce78SHaowei Wu 
48a11cd0d9STom Stellard #include <climits>  // Some Linux distributions define PATH_MAX here.
49a11cd0d9STom Stellard #endif              // GTEST_OS_WINDOWS_MOBILE
50a11cd0d9STom Stellard 
51a11cd0d9STom Stellard #include "gtest/internal/gtest-string.h"
52a11cd0d9STom Stellard 
53*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS
54a11cd0d9STom Stellard #define GTEST_PATH_MAX_ _MAX_PATH
55a11cd0d9STom Stellard #elif defined(PATH_MAX)
56a11cd0d9STom Stellard #define GTEST_PATH_MAX_ PATH_MAX
57a11cd0d9STom Stellard #elif defined(_XOPEN_PATH_MAX)
58a11cd0d9STom Stellard #define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
59a11cd0d9STom Stellard #else
60a11cd0d9STom Stellard #define GTEST_PATH_MAX_ _POSIX_PATH_MAX
61a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS
62a11cd0d9STom Stellard 
63*a866ce78SHaowei Wu #if GTEST_HAS_FILE_SYSTEM
64*a866ce78SHaowei Wu 
65a11cd0d9STom Stellard namespace testing {
66a11cd0d9STom Stellard namespace internal {
67a11cd0d9STom Stellard 
68*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS
69a11cd0d9STom Stellard // On Windows, '\\' is the standard path separator, but many tools and the
70a11cd0d9STom Stellard // Windows API also accept '/' as an alternate path separator. Unless otherwise
71a11cd0d9STom Stellard // noted, a file path can contain either kind of path separators, or a mixture
72a11cd0d9STom Stellard // of them.
73a11cd0d9STom Stellard const char kPathSeparator = '\\';
74a11cd0d9STom Stellard const char kAlternatePathSeparator = '/';
75a11cd0d9STom Stellard const char kAlternatePathSeparatorString[] = "/";
76*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS_MOBILE
77a11cd0d9STom Stellard // Windows CE doesn't have a current directory. You should not use
78a11cd0d9STom Stellard // the current directory in tests on Windows CE, but this at least
79a11cd0d9STom Stellard // provides a reasonable fallback.
80a11cd0d9STom Stellard const char kCurrentDirectoryString[] = "\\";
81a11cd0d9STom Stellard // Windows CE doesn't define INVALID_FILE_ATTRIBUTES
82a11cd0d9STom Stellard const DWORD kInvalidFileAttributes = 0xffffffff;
83a11cd0d9STom Stellard #else
84a11cd0d9STom Stellard const char kCurrentDirectoryString[] = ".\\";
85a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS_MOBILE
86a11cd0d9STom Stellard #else
87a11cd0d9STom Stellard const char kPathSeparator = '/';
88a11cd0d9STom Stellard const char kCurrentDirectoryString[] = "./";
89a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS
90a11cd0d9STom Stellard 
91a11cd0d9STom Stellard // Returns whether the given character is a valid path separator.
IsPathSeparator(char c)92a11cd0d9STom Stellard static bool IsPathSeparator(char c) {
93a11cd0d9STom Stellard #if GTEST_HAS_ALT_PATH_SEP_
94a11cd0d9STom Stellard   return (c == kPathSeparator) || (c == kAlternatePathSeparator);
95a11cd0d9STom Stellard #else
96a11cd0d9STom Stellard   return c == kPathSeparator;
97a11cd0d9STom Stellard #endif
98a11cd0d9STom Stellard }
99a11cd0d9STom Stellard 
100a11cd0d9STom Stellard // Returns the current working directory, or "" if unsuccessful.
GetCurrentDir()101a11cd0d9STom Stellard FilePath FilePath::GetCurrentDir() {
102*a866ce78SHaowei Wu #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
103*a866ce78SHaowei Wu     defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_ESP8266) ||           \
104*a866ce78SHaowei Wu     defined(GTEST_OS_ESP32) || defined(GTEST_OS_XTENSA) ||                 \
105*a866ce78SHaowei Wu     defined(GTEST_OS_QURT) || defined(GTEST_OS_NXP_QN9090) ||              \
106*a866ce78SHaowei Wu     defined(GTEST_OS_NRF52)
107a11cd0d9STom Stellard   // These platforms do not have a current directory, so we just return
108a11cd0d9STom Stellard   // something reasonable.
109a11cd0d9STom Stellard   return FilePath(kCurrentDirectoryString);
110*a866ce78SHaowei Wu #elif defined(GTEST_OS_WINDOWS)
111a11cd0d9STom Stellard   char cwd[GTEST_PATH_MAX_ + 1] = {'\0'};
112a11cd0d9STom Stellard   return FilePath(_getcwd(cwd, sizeof(cwd)) == nullptr ? "" : cwd);
113a11cd0d9STom Stellard #else
114a11cd0d9STom Stellard   char cwd[GTEST_PATH_MAX_ + 1] = {'\0'};
115a11cd0d9STom Stellard   char* result = getcwd(cwd, sizeof(cwd));
116*a866ce78SHaowei Wu #ifdef GTEST_OS_NACL
117a11cd0d9STom Stellard   // getcwd will likely fail in NaCl due to the sandbox, so return something
118a11cd0d9STom Stellard   // reasonable. The user may have provided a shim implementation for getcwd,
119a11cd0d9STom Stellard   // however, so fallback only when failure is detected.
120a11cd0d9STom Stellard   return FilePath(result == nullptr ? kCurrentDirectoryString : cwd);
121a11cd0d9STom Stellard #endif  // GTEST_OS_NACL
122a11cd0d9STom Stellard   return FilePath(result == nullptr ? "" : cwd);
123a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS_MOBILE
124a11cd0d9STom Stellard }
125a11cd0d9STom Stellard 
126a11cd0d9STom Stellard // Returns a copy of the FilePath with the case-insensitive extension removed.
127a11cd0d9STom Stellard // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
128a11cd0d9STom Stellard // FilePath("dir/file"). If a case-insensitive extension is not
129a11cd0d9STom Stellard // found, returns a copy of the original FilePath.
RemoveExtension(const char * extension) const130a11cd0d9STom Stellard FilePath FilePath::RemoveExtension(const char* extension) const {
131a11cd0d9STom Stellard   const std::string dot_extension = std::string(".") + extension;
132a11cd0d9STom Stellard   if (String::EndsWithCaseInsensitive(pathname_, dot_extension)) {
133*a866ce78SHaowei Wu     return FilePath(
134*a866ce78SHaowei Wu         pathname_.substr(0, pathname_.length() - dot_extension.length()));
135a11cd0d9STom Stellard   }
136a11cd0d9STom Stellard   return *this;
137a11cd0d9STom Stellard }
138a11cd0d9STom Stellard 
139a11cd0d9STom Stellard // Returns a pointer to the last occurrence of a valid path separator in
140a11cd0d9STom Stellard // the FilePath. On Windows, for example, both '/' and '\' are valid path
141a11cd0d9STom Stellard // separators. Returns NULL if no path separator was found.
FindLastPathSeparator() const142a11cd0d9STom Stellard const char* FilePath::FindLastPathSeparator() const {
143a11cd0d9STom Stellard   const char* const last_sep = strrchr(c_str(), kPathSeparator);
144a11cd0d9STom Stellard #if GTEST_HAS_ALT_PATH_SEP_
145a11cd0d9STom Stellard   const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);
146a11cd0d9STom Stellard   // Comparing two pointers of which only one is NULL is undefined.
147a11cd0d9STom Stellard   if (last_alt_sep != nullptr &&
148a11cd0d9STom Stellard       (last_sep == nullptr || last_alt_sep > last_sep)) {
149a11cd0d9STom Stellard     return last_alt_sep;
150a11cd0d9STom Stellard   }
151a11cd0d9STom Stellard #endif
152a11cd0d9STom Stellard   return last_sep;
153a11cd0d9STom Stellard }
154a11cd0d9STom Stellard 
CalculateRootLength() const155*a866ce78SHaowei Wu size_t FilePath::CalculateRootLength() const {
156*a866ce78SHaowei Wu   const auto& path = pathname_;
157*a866ce78SHaowei Wu   auto s = path.begin();
158*a866ce78SHaowei Wu   auto end = path.end();
159*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS
160*a866ce78SHaowei Wu   if (end - s >= 2 && s[1] == ':' && (end - s == 2 || IsPathSeparator(s[2])) &&
161*a866ce78SHaowei Wu       (('A' <= s[0] && s[0] <= 'Z') || ('a' <= s[0] && s[0] <= 'z'))) {
162*a866ce78SHaowei Wu     // A typical absolute path like "C:\Windows" or "D:"
163*a866ce78SHaowei Wu     s += 2;
164*a866ce78SHaowei Wu     if (s != end) {
165*a866ce78SHaowei Wu       ++s;
166*a866ce78SHaowei Wu     }
167*a866ce78SHaowei Wu   } else if (end - s >= 3 && IsPathSeparator(*s) && IsPathSeparator(*(s + 1)) &&
168*a866ce78SHaowei Wu              !IsPathSeparator(*(s + 2))) {
169*a866ce78SHaowei Wu     // Move past the "\\" prefix in a UNC path like "\\Server\Share\Folder"
170*a866ce78SHaowei Wu     s += 2;
171*a866ce78SHaowei Wu     // Skip 2 components and their following separators ("Server\" and "Share\")
172*a866ce78SHaowei Wu     for (int i = 0; i < 2; ++i) {
173*a866ce78SHaowei Wu       while (s != end) {
174*a866ce78SHaowei Wu         bool stop = IsPathSeparator(*s);
175*a866ce78SHaowei Wu         ++s;
176*a866ce78SHaowei Wu         if (stop) {
177*a866ce78SHaowei Wu           break;
178*a866ce78SHaowei Wu         }
179*a866ce78SHaowei Wu       }
180*a866ce78SHaowei Wu     }
181*a866ce78SHaowei Wu   } else if (s != end && IsPathSeparator(*s)) {
182*a866ce78SHaowei Wu     // A drive-rooted path like "\Windows"
183*a866ce78SHaowei Wu     ++s;
184*a866ce78SHaowei Wu   }
185*a866ce78SHaowei Wu #else
186*a866ce78SHaowei Wu   if (s != end && IsPathSeparator(*s)) {
187*a866ce78SHaowei Wu     ++s;
188*a866ce78SHaowei Wu   }
189*a866ce78SHaowei Wu #endif
190*a866ce78SHaowei Wu   return static_cast<size_t>(s - path.begin());
191*a866ce78SHaowei Wu }
192*a866ce78SHaowei Wu 
193a11cd0d9STom Stellard // Returns a copy of the FilePath with the directory part removed.
194a11cd0d9STom Stellard // Example: FilePath("path/to/file").RemoveDirectoryName() returns
195a11cd0d9STom Stellard // FilePath("file"). If there is no directory part ("just_a_file"), it returns
196a11cd0d9STom Stellard // the FilePath unmodified. If there is no file part ("just_a_dir/") it
197a11cd0d9STom Stellard // returns an empty FilePath ("").
198a11cd0d9STom Stellard // On Windows platform, '\' is the path separator, otherwise it is '/'.
RemoveDirectoryName() const199a11cd0d9STom Stellard FilePath FilePath::RemoveDirectoryName() const {
200a11cd0d9STom Stellard   const char* const last_sep = FindLastPathSeparator();
201a11cd0d9STom Stellard   return last_sep ? FilePath(last_sep + 1) : *this;
202a11cd0d9STom Stellard }
203a11cd0d9STom Stellard 
204a11cd0d9STom Stellard // RemoveFileName returns the directory path with the filename removed.
205a11cd0d9STom Stellard // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
206a11cd0d9STom Stellard // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
207a11cd0d9STom Stellard // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
208a11cd0d9STom Stellard // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
209a11cd0d9STom Stellard // On Windows platform, '\' is the path separator, otherwise it is '/'.
RemoveFileName() const210a11cd0d9STom Stellard FilePath FilePath::RemoveFileName() const {
211a11cd0d9STom Stellard   const char* const last_sep = FindLastPathSeparator();
212a11cd0d9STom Stellard   std::string dir;
213a11cd0d9STom Stellard   if (last_sep) {
214a11cd0d9STom Stellard     dir = std::string(c_str(), static_cast<size_t>(last_sep + 1 - c_str()));
215a11cd0d9STom Stellard   } else {
216a11cd0d9STom Stellard     dir = kCurrentDirectoryString;
217a11cd0d9STom Stellard   }
218a11cd0d9STom Stellard   return FilePath(dir);
219a11cd0d9STom Stellard }
220a11cd0d9STom Stellard 
221a11cd0d9STom Stellard // Helper functions for naming files in a directory for xml output.
222a11cd0d9STom Stellard 
223a11cd0d9STom Stellard // Given directory = "dir", base_name = "test", number = 0,
224a11cd0d9STom Stellard // extension = "xml", returns "dir/test.xml". If number is greater
225a11cd0d9STom Stellard // than zero (e.g., 12), returns "dir/test_12.xml".
226a11cd0d9STom Stellard // On Windows platform, uses \ as the separator rather than /.
MakeFileName(const FilePath & directory,const FilePath & base_name,int number,const char * extension)227a11cd0d9STom Stellard FilePath FilePath::MakeFileName(const FilePath& directory,
228*a866ce78SHaowei Wu                                 const FilePath& base_name, int number,
229a11cd0d9STom Stellard                                 const char* extension) {
230a11cd0d9STom Stellard   std::string file;
231a11cd0d9STom Stellard   if (number == 0) {
232a11cd0d9STom Stellard     file = base_name.string() + "." + extension;
233a11cd0d9STom Stellard   } else {
234*a866ce78SHaowei Wu     file =
235*a866ce78SHaowei Wu         base_name.string() + "_" + StreamableToString(number) + "." + extension;
236a11cd0d9STom Stellard   }
237a11cd0d9STom Stellard   return ConcatPaths(directory, FilePath(file));
238a11cd0d9STom Stellard }
239a11cd0d9STom Stellard 
240a11cd0d9STom Stellard // Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
241a11cd0d9STom Stellard // On Windows, uses \ as the separator rather than /.
ConcatPaths(const FilePath & directory,const FilePath & relative_path)242a11cd0d9STom Stellard FilePath FilePath::ConcatPaths(const FilePath& directory,
243a11cd0d9STom Stellard                                const FilePath& relative_path) {
244*a866ce78SHaowei Wu   if (directory.IsEmpty()) return relative_path;
245a11cd0d9STom Stellard   const FilePath dir(directory.RemoveTrailingPathSeparator());
246a11cd0d9STom Stellard   return FilePath(dir.string() + kPathSeparator + relative_path.string());
247a11cd0d9STom Stellard }
248a11cd0d9STom Stellard 
249a11cd0d9STom Stellard // Returns true if pathname describes something findable in the file-system,
250a11cd0d9STom Stellard // either a file, directory, or whatever.
FileOrDirectoryExists() const251a11cd0d9STom Stellard bool FilePath::FileOrDirectoryExists() const {
252*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS_MOBILE
253a11cd0d9STom Stellard   LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());
254a11cd0d9STom Stellard   const DWORD attributes = GetFileAttributes(unicode);
255a11cd0d9STom Stellard   delete[] unicode;
256a11cd0d9STom Stellard   return attributes != kInvalidFileAttributes;
257a11cd0d9STom Stellard #else
258*a866ce78SHaowei Wu   posix::StatStruct file_stat{};
259a11cd0d9STom Stellard   return posix::Stat(pathname_.c_str(), &file_stat) == 0;
260a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS_MOBILE
261a11cd0d9STom Stellard }
262a11cd0d9STom Stellard 
263a11cd0d9STom Stellard // Returns true if pathname describes a directory in the file-system
264a11cd0d9STom Stellard // that exists.
DirectoryExists() const265a11cd0d9STom Stellard bool FilePath::DirectoryExists() const {
266a11cd0d9STom Stellard   bool result = false;
267*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS
268a11cd0d9STom Stellard   // Don't strip off trailing separator if path is a root directory on
269a11cd0d9STom Stellard   // Windows (like "C:\\").
270*a866ce78SHaowei Wu   const FilePath& path(IsRootDirectory() ? *this
271*a866ce78SHaowei Wu                                          : RemoveTrailingPathSeparator());
272a11cd0d9STom Stellard #else
273a11cd0d9STom Stellard   const FilePath& path(*this);
274a11cd0d9STom Stellard #endif
275a11cd0d9STom Stellard 
276*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS_MOBILE
277a11cd0d9STom Stellard   LPCWSTR unicode = String::AnsiToUtf16(path.c_str());
278a11cd0d9STom Stellard   const DWORD attributes = GetFileAttributes(unicode);
279a11cd0d9STom Stellard   delete[] unicode;
280a11cd0d9STom Stellard   if ((attributes != kInvalidFileAttributes) &&
281a11cd0d9STom Stellard       (attributes & FILE_ATTRIBUTE_DIRECTORY)) {
282a11cd0d9STom Stellard     result = true;
283a11cd0d9STom Stellard   }
284a11cd0d9STom Stellard #else
285*a866ce78SHaowei Wu   posix::StatStruct file_stat{};
286*a866ce78SHaowei Wu   result =
287*a866ce78SHaowei Wu       posix::Stat(path.c_str(), &file_stat) == 0 && posix::IsDir(file_stat);
288a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS_MOBILE
289a11cd0d9STom Stellard 
290a11cd0d9STom Stellard   return result;
291a11cd0d9STom Stellard }
292a11cd0d9STom Stellard 
293a11cd0d9STom Stellard // Returns true if pathname describes a root directory. (Windows has one
294*a866ce78SHaowei Wu // root directory per disk drive. UNC share roots are also included.)
IsRootDirectory() const295a11cd0d9STom Stellard bool FilePath::IsRootDirectory() const {
296*a866ce78SHaowei Wu   size_t root_length = CalculateRootLength();
297*a866ce78SHaowei Wu   return root_length > 0 && root_length == pathname_.size() &&
298*a866ce78SHaowei Wu          IsPathSeparator(pathname_[root_length - 1]);
299a11cd0d9STom Stellard }
300a11cd0d9STom Stellard 
301a11cd0d9STom Stellard // Returns true if pathname describes an absolute path.
IsAbsolutePath() const302*a866ce78SHaowei Wu bool FilePath::IsAbsolutePath() const { return CalculateRootLength() > 0; }
303a11cd0d9STom Stellard 
304a11cd0d9STom Stellard // Returns a pathname for a file that does not currently exist. The pathname
305a11cd0d9STom Stellard // will be directory/base_name.extension or
306a11cd0d9STom Stellard // directory/base_name_<number>.extension if directory/base_name.extension
307a11cd0d9STom Stellard // already exists. The number will be incremented until a pathname is found
308a11cd0d9STom Stellard // that does not already exist.
309a11cd0d9STom Stellard // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
310a11cd0d9STom Stellard // There could be a race condition if two or more processes are calling this
311a11cd0d9STom Stellard // function at the same time -- they could both pick the same filename.
GenerateUniqueFileName(const FilePath & directory,const FilePath & base_name,const char * extension)312a11cd0d9STom Stellard FilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
313a11cd0d9STom Stellard                                           const FilePath& base_name,
314a11cd0d9STom Stellard                                           const char* extension) {
315a11cd0d9STom Stellard   FilePath full_pathname;
316a11cd0d9STom Stellard   int number = 0;
317a11cd0d9STom Stellard   do {
318a11cd0d9STom Stellard     full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
319a11cd0d9STom Stellard   } while (full_pathname.FileOrDirectoryExists());
320a11cd0d9STom Stellard   return full_pathname;
321a11cd0d9STom Stellard }
322a11cd0d9STom Stellard 
323a11cd0d9STom Stellard // Returns true if FilePath ends with a path separator, which indicates that
324a11cd0d9STom Stellard // it is intended to represent a directory. Returns false otherwise.
325a11cd0d9STom Stellard // This does NOT check that a directory (or file) actually exists.
IsDirectory() const326a11cd0d9STom Stellard bool FilePath::IsDirectory() const {
327a11cd0d9STom Stellard   return !pathname_.empty() &&
328a11cd0d9STom Stellard          IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]);
329a11cd0d9STom Stellard }
330a11cd0d9STom Stellard 
331a11cd0d9STom Stellard // Create directories so that path exists. Returns true if successful or if
332a11cd0d9STom Stellard // the directories already exist; returns false if unable to create directories
333a11cd0d9STom Stellard // for any reason.
CreateDirectoriesRecursively() const334a11cd0d9STom Stellard bool FilePath::CreateDirectoriesRecursively() const {
335a11cd0d9STom Stellard   if (!this->IsDirectory()) {
336a11cd0d9STom Stellard     return false;
337a11cd0d9STom Stellard   }
338a11cd0d9STom Stellard 
339a11cd0d9STom Stellard   if (pathname_.length() == 0 || this->DirectoryExists()) {
340a11cd0d9STom Stellard     return true;
341a11cd0d9STom Stellard   }
342a11cd0d9STom Stellard 
343a11cd0d9STom Stellard   const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
344a11cd0d9STom Stellard   return parent.CreateDirectoriesRecursively() && this->CreateFolder();
345a11cd0d9STom Stellard }
346a11cd0d9STom Stellard 
347a11cd0d9STom Stellard // Create the directory so that path exists. Returns true if successful or
348a11cd0d9STom Stellard // if the directory already exists; returns false if unable to create the
349a11cd0d9STom Stellard // directory for any reason, including if the parent directory does not
350a11cd0d9STom Stellard // exist. Not named "CreateDirectory" because that's a macro on Windows.
CreateFolder() const351a11cd0d9STom Stellard bool FilePath::CreateFolder() const {
352*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS_MOBILE
353a11cd0d9STom Stellard   FilePath removed_sep(this->RemoveTrailingPathSeparator());
354a11cd0d9STom Stellard   LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());
355a11cd0d9STom Stellard   int result = CreateDirectory(unicode, nullptr) ? 0 : -1;
356a11cd0d9STom Stellard   delete[] unicode;
357*a866ce78SHaowei Wu #elif defined(GTEST_OS_WINDOWS)
358a11cd0d9STom Stellard   int result = _mkdir(pathname_.c_str());
359*a866ce78SHaowei Wu #elif defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
360*a866ce78SHaowei Wu     defined(GTEST_OS_QURT) || defined(GTEST_OS_NXP_QN9090) ||  \
361*a866ce78SHaowei Wu     defined(GTEST_OS_NRF52)
362*a866ce78SHaowei Wu   // do nothing
363*a866ce78SHaowei Wu   int result = 0;
364a11cd0d9STom Stellard #else
365a11cd0d9STom Stellard   int result = mkdir(pathname_.c_str(), 0777);
366a11cd0d9STom Stellard #endif  // GTEST_OS_WINDOWS_MOBILE
367a11cd0d9STom Stellard 
368a11cd0d9STom Stellard   if (result == -1) {
369a11cd0d9STom Stellard     return this->DirectoryExists();  // An error is OK if the directory exists.
370a11cd0d9STom Stellard   }
371a11cd0d9STom Stellard   return true;  // No error.
372a11cd0d9STom Stellard }
373a11cd0d9STom Stellard 
374a11cd0d9STom Stellard // If input name has a trailing separator character, remove it and return the
375a11cd0d9STom Stellard // name, otherwise return the name string unmodified.
376a11cd0d9STom Stellard // On Windows platform, uses \ as the separator, other platforms use /.
RemoveTrailingPathSeparator() const377a11cd0d9STom Stellard FilePath FilePath::RemoveTrailingPathSeparator() const {
378*a866ce78SHaowei Wu   return IsDirectory() ? FilePath(pathname_.substr(0, pathname_.length() - 1))
379a11cd0d9STom Stellard                        : *this;
380a11cd0d9STom Stellard }
381a11cd0d9STom Stellard 
382a11cd0d9STom Stellard // Removes any redundant separators that might be in the pathname.
383a11cd0d9STom Stellard // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
384a11cd0d9STom Stellard // redundancies that might be in a pathname involving "." or "..".
385*a866ce78SHaowei Wu // Note that "\\Host\Share" does not contain a redundancy on Windows!
Normalize()386a11cd0d9STom Stellard void FilePath::Normalize() {
387*a866ce78SHaowei Wu   auto out = pathname_.begin();
388a11cd0d9STom Stellard 
389*a866ce78SHaowei Wu   auto i = pathname_.cbegin();
390*a866ce78SHaowei Wu #ifdef GTEST_OS_WINDOWS
391*a866ce78SHaowei Wu   // UNC paths are treated specially
392*a866ce78SHaowei Wu   if (pathname_.end() - i >= 3 && IsPathSeparator(*i) &&
393*a866ce78SHaowei Wu       IsPathSeparator(*(i + 1)) && !IsPathSeparator(*(i + 2))) {
394*a866ce78SHaowei Wu     *(out++) = kPathSeparator;
395*a866ce78SHaowei Wu     *(out++) = kPathSeparator;
396a11cd0d9STom Stellard   }
397a11cd0d9STom Stellard #endif
398*a866ce78SHaowei Wu   while (i != pathname_.end()) {
399*a866ce78SHaowei Wu     const char character = *i;
400*a866ce78SHaowei Wu     if (!IsPathSeparator(character)) {
401*a866ce78SHaowei Wu       *(out++) = character;
402*a866ce78SHaowei Wu     } else if (out == pathname_.begin() || *std::prev(out) != kPathSeparator) {
403*a866ce78SHaowei Wu       *(out++) = kPathSeparator;
404a11cd0d9STom Stellard     }
405*a866ce78SHaowei Wu     ++i;
406a11cd0d9STom Stellard   }
407*a866ce78SHaowei Wu 
408*a866ce78SHaowei Wu   pathname_.erase(out, pathname_.end());
409a11cd0d9STom Stellard }
410a11cd0d9STom Stellard 
411a11cd0d9STom Stellard }  // namespace internal
412a11cd0d9STom Stellard }  // namespace testing
413*a866ce78SHaowei Wu 
414*a866ce78SHaowei Wu #endif  // GTEST_HAS_FILE_SYSTEM
415