temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame^] | 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. |
| 61 | 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 '/'. |
| 75 | 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 '/'. |
| 86 | 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 /. |
| 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. |
| 113 | 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. |
| 125 | bool FilePath::DirectoryExists() const { |
| 126 | bool result = false; |
| 127 | #ifdef _WIN32 |
| 128 | FilePath removed_sep(this->RemoveTrailingPathSeparator()); |
| 129 | struct _stat file_stat = {}; |
| 130 | result = _stat(removed_sep.c_str(), &file_stat) == 0 && |
| 131 | (_S_IFDIR & file_stat.st_mode) != 0; |
| 132 | #else |
| 133 | struct stat file_stat = {}; |
| 134 | result = stat(pathname_.c_str(), &file_stat) == 0 && |
| 135 | S_ISDIR(file_stat.st_mode); |
| 136 | #endif // _WIN32 |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | // Returns a pathname for a file that does not currently exist. The pathname |
| 141 | // will be directory/base_name.extension or |
| 142 | // directory/base_name_<number>.extension if directory/base_name.extension |
| 143 | // already exists. The number will be incremented until a pathname is found |
| 144 | // that does not already exist. |
| 145 | // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. |
| 146 | // There could be a race condition if two or more processes are calling this |
| 147 | // function at the same time -- they could both pick the same filename. |
| 148 | FilePath FilePath::GenerateUniqueFileName(const FilePath& directory, |
| 149 | const FilePath& base_name, |
| 150 | const char* extension) { |
| 151 | FilePath full_pathname; |
| 152 | int number = 0; |
| 153 | do { |
| 154 | full_pathname.Set(MakeFileName(directory, base_name, number++, extension)); |
| 155 | } while (full_pathname.FileOrDirectoryExists()); |
| 156 | return full_pathname; |
| 157 | } |
| 158 | |
| 159 | // Returns true if FilePath ends with a path separator, which indicates that |
| 160 | // it is intended to represent a directory. Returns false otherwise. |
| 161 | // This does NOT check that a directory (or file) actually exists. |
| 162 | bool FilePath::IsDirectory() const { |
| 163 | return pathname_.EndsWith(kPathSeparatorString); |
| 164 | } |
| 165 | |
| 166 | // Create directories so that path exists. Returns true if successful or if |
| 167 | // the directories already exist; returns false if unable to create directories |
| 168 | // for any reason. |
| 169 | bool FilePath::CreateDirectoriesRecursively() const { |
| 170 | if (!this->IsDirectory()) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (pathname_.GetLength() == 0 || this->DirectoryExists()) { |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName()); |
| 179 | return parent.CreateDirectoriesRecursively() && this->CreateFolder(); |
| 180 | } |
| 181 | |
| 182 | // Create the directory so that path exists. Returns true if successful or |
| 183 | // if the directory already exists; returns false if unable to create the |
| 184 | // directory for any reason, including if the parent directory does not |
| 185 | // exist. Not named "CreateDirectory" because that's a macro on Windows. |
| 186 | bool FilePath::CreateFolder() const { |
| 187 | #ifdef _WIN32 |
| 188 | int result = _mkdir(pathname_.c_str()); |
| 189 | #else |
| 190 | int result = mkdir(pathname_.c_str(), 0777); |
| 191 | #endif // _WIN32 |
| 192 | if (result == -1) { |
| 193 | return this->DirectoryExists(); // An error is OK if the directory exists. |
| 194 | } |
| 195 | return true; // No error. |
| 196 | } |
| 197 | |
| 198 | // If input name has a trailing separator character, remove it and return the |
| 199 | // name, otherwise return the name string unmodified. |
| 200 | // On Windows platform, uses \ as the separator, other platforms use /. |
| 201 | FilePath FilePath::RemoveTrailingPathSeparator() const { |
| 202 | return pathname_.EndsWith(kPathSeparatorString) |
| 203 | ? FilePath(String(pathname_.c_str(), pathname_.GetLength() - 1)) |
| 204 | : *this; |
| 205 | } |
| 206 | |
| 207 | } // namespace internal |
| 208 | } // namespace testing |