1 // Copyright 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Authors: keith.ray@gmail.com (Keith Ray)
31
32 #include <gtest/internal/gtest-filepath.h>
33 #include <gtest/internal/gtest-port.h>
34
35 #ifdef _WIN32
36 #include <direct.h>
37 #include <io.h>
38 #endif // _WIN32
39
40 #include <sys/stat.h>
41
42 #include <gtest/internal/gtest-string.h>
43
44 namespace testing {
45 namespace internal {
46
47 #ifdef GTEST_OS_WINDOWS
48 const char kPathSeparator = '\\';
49 const char kPathSeparatorString[] = "\\";
50 const char kCurrentDirectoryString[] = ".\\";
51 #else
52 const char kPathSeparator = '/';
53 const char kPathSeparatorString[] = "/";
54 const char kCurrentDirectoryString[] = "./";
55 #endif // GTEST_OS_WINDOWS
56
57 // Returns a copy of the FilePath with the case-insensitive extension removed.
58 // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
59 // FilePath("dir/file"). If a case-insensitive extension is not
60 // found, returns a copy of the original FilePath.
RemoveExtension(const char * extension) const61 FilePath FilePath::RemoveExtension(const char* extension) const {
62 String dot_extension(String::Format(".%s", extension));
63 if (pathname_.EndsWithCaseInsensitive(dot_extension.c_str())) {
64 return FilePath(String(pathname_.c_str(), pathname_.GetLength() - 4));
65 }
66 return *this;
67 }
68
69 // Returns a copy of the FilePath with the directory part removed.
70 // Example: FilePath("path/to/file").RemoveDirectoryName() returns
71 // FilePath("file"). If there is no directory part ("just_a_file"), it returns
72 // the FilePath unmodified. If there is no file part ("just_a_dir/") it
73 // returns an empty FilePath ("").
74 // On Windows platform, '\' is the path separator, otherwise it is '/'.
RemoveDirectoryName() const75 FilePath FilePath::RemoveDirectoryName() const {
76 const char* const last_sep = strrchr(c_str(), kPathSeparator);
77 return last_sep ? FilePath(String(last_sep + 1)) : *this;
78 }
79
80 // RemoveFileName returns the directory path with the filename removed.
81 // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
82 // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
83 // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
84 // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
85 // On Windows platform, '\' is the path separator, otherwise it is '/'.
RemoveFileName() const86 FilePath FilePath::RemoveFileName() const {
87 const char* const last_sep = strrchr(c_str(), kPathSeparator);
88 return FilePath(last_sep ? String(c_str(), last_sep + 1 - c_str())
89 : String(kCurrentDirectoryString));
90 }
91
92 // Helper functions for naming files in a directory for xml output.
93
94 // Given directory = "dir", base_name = "test", number = 0,
95 // extension = "xml", returns "dir/test.xml". If number is greater
96 // than zero (e.g., 12), returns "dir/test_12.xml".
97 // On Windows platform, uses \ as the separator rather than /.
MakeFileName(const FilePath & directory,const FilePath & base_name,int number,const char * extension)98 FilePath FilePath::MakeFileName(const FilePath& directory,
99 const FilePath& base_name,
100 int number,
101 const char* extension) {
102 FilePath dir(directory.RemoveTrailingPathSeparator());
103 if (number == 0) {
104 return FilePath(String::Format("%s%c%s.%s", dir.c_str(), kPathSeparator,
105 base_name.c_str(), extension));
106 }
107 return FilePath(String::Format("%s%c%s_%d.%s", dir.c_str(), kPathSeparator,
108 base_name.c_str(), number, extension));
109 }
110
111 // Returns true if pathname describes something findable in the file-system,
112 // either a file, directory, or whatever.
FileOrDirectoryExists() const113 bool FilePath::FileOrDirectoryExists() const {
114 #ifdef GTEST_OS_WINDOWS
115 struct _stat file_stat;
116 return _stat(pathname_.c_str(), &file_stat) == 0;
117 #else
118 struct stat file_stat;
119 return stat(pathname_.c_str(), &file_stat) == 0;
120 #endif // GTEST_OS_WINDOWS
121 }
122
123 // Returns true if pathname describes a directory in the file-system
124 // that exists.
DirectoryExists() const125 bool FilePath::DirectoryExists() const {
126 bool result = false;
127 #ifdef _WIN32
128 FilePath removed_sep(this->RemoveTrailingPathSeparator());
129 struct _stat file_stat;
130 file_stat.st_mode = 0;
131 result = _stat(removed_sep.c_str(), &file_stat) == 0 &&
132 (_S_IFDIR & file_stat.st_mode) != 0;
133 #else
134 struct stat file_stat;
135 file_stat.st_mode = 0;
136 result = stat(pathname_.c_str(), &file_stat) == 0 &&
137 S_ISDIR(file_stat.st_mode);
138 #endif // _WIN32
139 return result;
140 }
141
142 // Returns a pathname for a file that does not currently exist. The pathname
143 // will be directory/base_name.extension or
144 // directory/base_name_<number>.extension if directory/base_name.extension
145 // already exists. The number will be incremented until a pathname is found
146 // that does not already exist.
147 // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
148 // There could be a race condition if two or more processes are calling this
149 // function at the same time -- they could both pick the same filename.
GenerateUniqueFileName(const FilePath & directory,const FilePath & base_name,const char * extension)150 FilePath FilePath::GenerateUniqueFileName(const FilePath& directory,
151 const FilePath& base_name,
152 const char* extension) {
153 FilePath full_pathname;
154 int number = 0;
155 do {
156 full_pathname.Set(MakeFileName(directory, base_name, number++, extension));
157 } while (full_pathname.FileOrDirectoryExists());
158 return full_pathname;
159 }
160
161 // Returns true if FilePath ends with a path separator, which indicates that
162 // it is intended to represent a directory. Returns false otherwise.
163 // This does NOT check that a directory (or file) actually exists.
IsDirectory() const164 bool FilePath::IsDirectory() const {
165 return pathname_.EndsWith(kPathSeparatorString);
166 }
167
168 // Create directories so that path exists. Returns true if successful or if
169 // the directories already exist; returns false if unable to create directories
170 // for any reason.
CreateDirectoriesRecursively() const171 bool FilePath::CreateDirectoriesRecursively() const {
172 if (!this->IsDirectory()) {
173 return false;
174 }
175
176 if (pathname_.GetLength() == 0 || this->DirectoryExists()) {
177 return true;
178 }
179
180 const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());
181 return parent.CreateDirectoriesRecursively() && this->CreateFolder();
182 }
183
184 // Create the directory so that path exists. Returns true if successful or
185 // if the directory already exists; returns false if unable to create the
186 // directory for any reason, including if the parent directory does not
187 // exist. Not named "CreateDirectory" because that's a macro on Windows.
CreateFolder() const188 bool FilePath::CreateFolder() const {
189 #ifdef _WIN32
190 int result = _mkdir(pathname_.c_str());
191 #else
192 int result = mkdir(pathname_.c_str(), 0777);
193 #endif // _WIN32
194 if (result == -1) {
195 return this->DirectoryExists(); // An error is OK if the directory exists.
196 }
197 return true; // No error.
198 }
199
200 // If input name has a trailing separator character, remove it and return the
201 // name, otherwise return the name string unmodified.
202 // On Windows platform, uses \ as the separator, other platforms use /.
RemoveTrailingPathSeparator() const203 FilePath FilePath::RemoveTrailingPathSeparator() const {
204 return pathname_.EndsWith(kPathSeparatorString)
205 ? FilePath(String(pathname_.c_str(), pathname_.GetLength() - 1))
206 : *this;
207 }
208
209 } // namespace internal
210 } // namespace testing
211