1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Authors: keith.ray@gmail.com (Keith Ray)
32 #include "gtest/gtest-message.h"
33 #include "gtest/internal/gtest-filepath.h"
34 #include "gtest/internal/gtest-port.h"
38 #if GTEST_OS_WINDOWS_MOBILE
40 #elif GTEST_OS_WINDOWS
43 #elif GTEST_OS_SYMBIAN
44 // Symbian OpenC has PATH_MAX in sys/syslimits.h
45 # include <sys/syslimits.h>
48 # include <climits> // Some Linux distributions define PATH_MAX here.
49 #endif // GTEST_OS_WINDOWS_MOBILE
52 # define GTEST_PATH_MAX_ _MAX_PATH
53 #elif defined(PATH_MAX)
54 # define GTEST_PATH_MAX_ PATH_MAX
55 #elif defined(_XOPEN_PATH_MAX)
56 # define GTEST_PATH_MAX_ _XOPEN_PATH_MAX
58 # define GTEST_PATH_MAX_ _POSIX_PATH_MAX
59 #endif // GTEST_OS_WINDOWS
61 #include "gtest/internal/gtest-string.h"
67 // On Windows, '\\' is the standard path separator, but many tools and the
68 // Windows API also accept '/' as an alternate path separator. Unless otherwise
69 // noted, a file path can contain either kind of path separators, or a mixture
71 const char kPathSeparator
= '\\';
72 const char kAlternatePathSeparator
= '/';
73 const char kPathSeparatorString
[] = "\\";
74 const char kAlternatePathSeparatorString
[] = "/";
75 # if GTEST_OS_WINDOWS_MOBILE
76 // Windows CE doesn't have a current directory. You should not use
77 // the current directory in tests on Windows CE, but this at least
78 // provides a reasonable fallback.
79 const char kCurrentDirectoryString
[] = "\\";
80 // Windows CE doesn't define INVALID_FILE_ATTRIBUTES
81 const DWORD kInvalidFileAttributes
= 0xffffffff;
83 const char kCurrentDirectoryString
[] = ".\\";
84 # endif // GTEST_OS_WINDOWS_MOBILE
86 const char kPathSeparator
= '/';
87 const char kPathSeparatorString
[] = "/";
88 const char kCurrentDirectoryString
[] = "./";
89 #endif // GTEST_OS_WINDOWS
91 // Returns whether the given character is a valid path separator.
92 static bool IsPathSeparator(char c
) {
93 #if GTEST_HAS_ALT_PATH_SEP_
94 return (c
== kPathSeparator
) || (c
== kAlternatePathSeparator
);
96 return c
== kPathSeparator
;
100 // Returns the current working directory, or "" if unsuccessful.
101 FilePath
FilePath::GetCurrentDir() {
102 #if GTEST_OS_WINDOWS_MOBILE
103 // Windows CE doesn't have a current directory, so we just return
104 // something reasonable.
105 return FilePath(kCurrentDirectoryString
);
106 #elif GTEST_OS_WINDOWS
107 char cwd
[GTEST_PATH_MAX_
+ 1] = { '\0' };
108 return FilePath(_getcwd(cwd
, sizeof(cwd
)) == NULL
? "" : cwd
);
110 char cwd
[GTEST_PATH_MAX_
+ 1] = { '\0' };
111 return FilePath(getcwd(cwd
, sizeof(cwd
)) == NULL
? "" : cwd
);
112 #endif // GTEST_OS_WINDOWS_MOBILE
115 // Returns a copy of the FilePath with the case-insensitive extension removed.
116 // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
117 // FilePath("dir/file"). If a case-insensitive extension is not
118 // found, returns a copy of the original FilePath.
119 FilePath
FilePath::RemoveExtension(const char* extension
) const {
120 const std::string dot_extension
= std::string(".") + extension
;
121 if (String::EndsWithCaseInsensitive(pathname_
, dot_extension
)) {
122 return FilePath(pathname_
.substr(
123 0, pathname_
.length() - dot_extension
.length()));
128 // Returns a pointer to the last occurence of a valid path separator in
129 // the FilePath. On Windows, for example, both '/' and '\' are valid path
130 // separators. Returns NULL if no path separator was found.
131 const char* FilePath::FindLastPathSeparator() const {
132 const char* const last_sep
= strrchr(c_str(), kPathSeparator
);
133 #if GTEST_HAS_ALT_PATH_SEP_
134 const char* const last_alt_sep
= strrchr(c_str(), kAlternatePathSeparator
);
135 // Comparing two pointers of which only one is NULL is undefined.
136 if (last_alt_sep
!= NULL
&&
137 (last_sep
== NULL
|| last_alt_sep
> last_sep
)) {
144 // Returns a copy of the FilePath with the directory part removed.
145 // Example: FilePath("path/to/file").RemoveDirectoryName() returns
146 // FilePath("file"). If there is no directory part ("just_a_file"), it returns
147 // the FilePath unmodified. If there is no file part ("just_a_dir/") it
148 // returns an empty FilePath ("").
149 // On Windows platform, '\' is the path separator, otherwise it is '/'.
150 FilePath
FilePath::RemoveDirectoryName() const {
151 const char* const last_sep
= FindLastPathSeparator();
152 return last_sep
? FilePath(last_sep
+ 1) : *this;
155 // RemoveFileName returns the directory path with the filename removed.
156 // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
157 // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
158 // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
159 // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
160 // On Windows platform, '\' is the path separator, otherwise it is '/'.
161 FilePath
FilePath::RemoveFileName() const {
162 const char* const last_sep
= FindLastPathSeparator();
165 dir
= std::string(c_str(), last_sep
+ 1 - c_str());
167 dir
= kCurrentDirectoryString
;
169 return FilePath(dir
);
172 // Helper functions for naming files in a directory for xml output.
174 // Given directory = "dir", base_name = "test", number = 0,
175 // extension = "xml", returns "dir/test.xml". If number is greater
176 // than zero (e.g., 12), returns "dir/test_12.xml".
177 // On Windows platform, uses \ as the separator rather than /.
178 FilePath
FilePath::MakeFileName(const FilePath
& directory
,
179 const FilePath
& base_name
,
181 const char* extension
) {
184 file
= base_name
.string() + "." + extension
;
186 file
= base_name
.string() + "_" + StreamableToString(number
)
189 return ConcatPaths(directory
, FilePath(file
));
192 // Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml".
193 // On Windows, uses \ as the separator rather than /.
194 FilePath
FilePath::ConcatPaths(const FilePath
& directory
,
195 const FilePath
& relative_path
) {
196 if (directory
.IsEmpty())
197 return relative_path
;
198 const FilePath
dir(directory
.RemoveTrailingPathSeparator());
199 return FilePath(dir
.string() + kPathSeparator
+ relative_path
.string());
202 // Returns true if pathname describes something findable in the file-system,
203 // either a file, directory, or whatever.
204 bool FilePath::FileOrDirectoryExists() const {
205 #if GTEST_OS_WINDOWS_MOBILE
206 LPCWSTR unicode
= String::AnsiToUtf16(pathname_
.c_str());
207 const DWORD attributes
= GetFileAttributes(unicode
);
209 return attributes
!= kInvalidFileAttributes
;
211 posix::StatStruct file_stat
;
212 return posix::Stat(pathname_
.c_str(), &file_stat
) == 0;
213 #endif // GTEST_OS_WINDOWS_MOBILE
216 // Returns true if pathname describes a directory in the file-system
218 bool FilePath::DirectoryExists() const {
221 // Don't strip off trailing separator if path is a root directory on
222 // Windows (like "C:\\").
223 const FilePath
& path(IsRootDirectory() ? *this :
224 RemoveTrailingPathSeparator());
226 const FilePath
& path(*this);
229 #if GTEST_OS_WINDOWS_MOBILE
230 LPCWSTR unicode
= String::AnsiToUtf16(path
.c_str());
231 const DWORD attributes
= GetFileAttributes(unicode
);
233 if ((attributes
!= kInvalidFileAttributes
) &&
234 (attributes
& FILE_ATTRIBUTE_DIRECTORY
)) {
238 posix::StatStruct file_stat
;
239 result
= posix::Stat(path
.c_str(), &file_stat
) == 0 &&
240 posix::IsDir(file_stat
);
241 #endif // GTEST_OS_WINDOWS_MOBILE
246 // Returns true if pathname describes a root directory. (Windows has one
247 // root directory per disk drive.)
248 bool FilePath::IsRootDirectory() const {
250 // TODO(wan@google.com): on Windows a network share like
251 // \\server\share can be a root directory, although it cannot be the
252 // current directory. Handle this properly.
253 return pathname_
.length() == 3 && IsAbsolutePath();
255 return pathname_
.length() == 1 && IsPathSeparator(pathname_
.c_str()[0]);
259 // Returns true if pathname describes an absolute path.
260 bool FilePath::IsAbsolutePath() const {
261 const char* const name
= pathname_
.c_str();
263 return pathname_
.length() >= 3 &&
264 ((name
[0] >= 'a' && name
[0] <= 'z') ||
265 (name
[0] >= 'A' && name
[0] <= 'Z')) &&
267 IsPathSeparator(name
[2]);
269 return IsPathSeparator(name
[0]);
273 // Returns a pathname for a file that does not currently exist. The pathname
274 // will be directory/base_name.extension or
275 // directory/base_name_<number>.extension if directory/base_name.extension
276 // already exists. The number will be incremented until a pathname is found
277 // that does not already exist.
278 // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
279 // There could be a race condition if two or more processes are calling this
280 // function at the same time -- they could both pick the same filename.
281 FilePath
FilePath::GenerateUniqueFileName(const FilePath
& directory
,
282 const FilePath
& base_name
,
283 const char* extension
) {
284 FilePath full_pathname
;
287 full_pathname
.Set(MakeFileName(directory
, base_name
, number
++, extension
));
288 } while (full_pathname
.FileOrDirectoryExists());
289 return full_pathname
;
292 // Returns true if FilePath ends with a path separator, which indicates that
293 // it is intended to represent a directory. Returns false otherwise.
294 // This does NOT check that a directory (or file) actually exists.
295 bool FilePath::IsDirectory() const {
296 return !pathname_
.empty() &&
297 IsPathSeparator(pathname_
.c_str()[pathname_
.length() - 1]);
300 // Create directories so that path exists. Returns true if successful or if
301 // the directories already exist; returns false if unable to create directories
303 bool FilePath::CreateDirectoriesRecursively() const {
304 if (!this->IsDirectory()) {
308 if (pathname_
.length() == 0 || this->DirectoryExists()) {
312 const FilePath
parent(this->RemoveTrailingPathSeparator().RemoveFileName());
313 return parent
.CreateDirectoriesRecursively() && this->CreateFolder();
316 // Create the directory so that path exists. Returns true if successful or
317 // if the directory already exists; returns false if unable to create the
318 // directory for any reason, including if the parent directory does not
319 // exist. Not named "CreateDirectory" because that's a macro on Windows.
320 bool FilePath::CreateFolder() const {
321 #if GTEST_OS_WINDOWS_MOBILE
322 FilePath
removed_sep(this->RemoveTrailingPathSeparator());
323 LPCWSTR unicode
= String::AnsiToUtf16(removed_sep
.c_str());
324 int result
= CreateDirectory(unicode
, NULL
) ? 0 : -1;
326 #elif GTEST_OS_WINDOWS
327 int result
= _mkdir(pathname_
.c_str());
329 int result
= mkdir(pathname_
.c_str(), 0777);
330 #endif // GTEST_OS_WINDOWS_MOBILE
333 return this->DirectoryExists(); // An error is OK if the directory exists.
335 return true; // No error.
338 // If input name has a trailing separator character, remove it and return the
339 // name, otherwise return the name string unmodified.
340 // On Windows platform, uses \ as the separator, other platforms use /.
341 FilePath
FilePath::RemoveTrailingPathSeparator() const {
343 ? FilePath(pathname_
.substr(0, pathname_
.length() - 1))
347 // Removes any redundant separators that might be in the pathname.
348 // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
349 // redundancies that might be in a pathname involving "." or "..".
350 // TODO(wan@google.com): handle Windows network shares (e.g. \\server\share).
351 void FilePath::Normalize() {
352 if (pathname_
.c_str() == NULL
) {
356 const char* src
= pathname_
.c_str();
357 char* const dest
= new char[pathname_
.length() + 1];
358 char* dest_ptr
= dest
;
359 memset(dest_ptr
, 0, pathname_
.length() + 1);
361 while (*src
!= '\0') {
363 if (!IsPathSeparator(*src
)) {
366 #if GTEST_HAS_ALT_PATH_SEP_
367 if (*dest_ptr
== kAlternatePathSeparator
) {
368 *dest_ptr
= kPathSeparator
;
371 while (IsPathSeparator(*src
))
381 } // namespace internal
382 } // namespace testing