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 // Google Test filepath utilities 31 // 32 // This header file declares classes and functions used internally by 33 // Google Test. They are subject to change without notice. 34 // 35 // This file is #included in gtest/internal/gtest-internal.h. 36 // Do not include this header file separately! 37 38 // IWYU pragma: private, include "gtest/gtest.h" 39 // IWYU pragma: friend gtest/.* 40 // IWYU pragma: friend gmock/.* 41 42 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 43 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 44 45 #include <string> 46 #include <utility> 47 48 #include "gtest/internal/gtest-port.h" 49 #include "gtest/internal/gtest-string.h" 50 51 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 52 /* class A needs to have dll-interface to be used by clients of class B */) 53 54 #if GTEST_HAS_FILE_SYSTEM 55 56 namespace testing { 57 namespace internal { 58 59 // FilePath - a class for file and directory pathname manipulation which 60 // handles platform-specific conventions (like the pathname separator). 61 // Used for helper functions for naming files in a directory for xml output. 62 // Except for Set methods, all methods are const or static, which provides an 63 // "immutable value object" -- useful for peace of mind. 64 // A FilePath with a value ending in a path separator ("like/this/") represents 65 // a directory, otherwise it is assumed to represent a file. In either case, 66 // it may or may not represent an actual file or directory in the file system. 67 // Names are NOT checked for syntax correctness -- no checking for illegal 68 // characters, malformed paths, etc. 69 70 class GTEST_API_ FilePath { 71 public: FilePath()72 FilePath() : pathname_("") {} FilePath(const FilePath & rhs)73 FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {} FilePath(FilePath && rhs)74 FilePath(FilePath&& rhs) noexcept : pathname_(std::move(rhs.pathname_)) {} 75 FilePath(std::string pathname)76 explicit FilePath(std::string pathname) : pathname_(std::move(pathname)) { 77 Normalize(); 78 } 79 80 FilePath& operator=(const FilePath& rhs) { 81 Set(rhs); 82 return *this; 83 } 84 FilePath& operator=(FilePath&& rhs) noexcept { 85 pathname_ = std::move(rhs.pathname_); 86 return *this; 87 } 88 Set(const FilePath & rhs)89 void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; } 90 string()91 const std::string& string() const { return pathname_; } c_str()92 const char* c_str() const { return pathname_.c_str(); } 93 94 // Returns the current working directory, or "" if unsuccessful. 95 static FilePath GetCurrentDir(); 96 97 // Given directory = "dir", base_name = "test", number = 0, 98 // extension = "xml", returns "dir/test.xml". If number is greater 99 // than zero (e.g., 12), returns "dir/test_12.xml". 100 // On Windows platform, uses \ as the separator rather than /. 101 static FilePath MakeFileName(const FilePath& directory, 102 const FilePath& base_name, int number, 103 const char* extension); 104 105 // Given directory = "dir", relative_path = "test.xml", 106 // returns "dir/test.xml". 107 // On Windows, uses \ as the separator rather than /. 108 static FilePath ConcatPaths(const FilePath& directory, 109 const FilePath& relative_path); 110 111 // Returns a pathname for a file that does not currently exist. The pathname 112 // will be directory/base_name.extension or 113 // directory/base_name_<number>.extension if directory/base_name.extension 114 // already exists. The number will be incremented until a pathname is found 115 // that does not already exist. 116 // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. 117 // There could be a race condition if two or more processes are calling this 118 // function at the same time -- they could both pick the same filename. 119 static FilePath GenerateUniqueFileName(const FilePath& directory, 120 const FilePath& base_name, 121 const char* extension); 122 123 // Returns true if and only if the path is "". IsEmpty()124 bool IsEmpty() const { return pathname_.empty(); } 125 126 // If input name has a trailing separator character, removes it and returns 127 // the name, otherwise return the name string unmodified. 128 // On Windows platform, uses \ as the separator, other platforms use /. 129 FilePath RemoveTrailingPathSeparator() const; 130 131 // Returns a copy of the FilePath with the directory part removed. 132 // Example: FilePath("path/to/file").RemoveDirectoryName() returns 133 // FilePath("file"). If there is no directory part ("just_a_file"), it returns 134 // the FilePath unmodified. If there is no file part ("just_a_dir/") it 135 // returns an empty FilePath (""). 136 // On Windows platform, '\' is the path separator, otherwise it is '/'. 137 FilePath RemoveDirectoryName() const; 138 139 // RemoveFileName returns the directory path with the filename removed. 140 // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". 141 // If the FilePath is "a_file" or "/a_file", RemoveFileName returns 142 // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does 143 // not have a file, like "just/a/dir/", it returns the FilePath unmodified. 144 // On Windows platform, '\' is the path separator, otherwise it is '/'. 145 FilePath RemoveFileName() const; 146 147 // Returns a copy of the FilePath with the case-insensitive extension removed. 148 // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns 149 // FilePath("dir/file"). If a case-insensitive extension is not 150 // found, returns a copy of the original FilePath. 151 FilePath RemoveExtension(const char* extension) const; 152 153 // Creates directories so that path exists. Returns true if successful or if 154 // the directories already exist; returns false if unable to create 155 // directories for any reason. Will also return false if the FilePath does 156 // not represent a directory (that is, it doesn't end with a path separator). 157 bool CreateDirectoriesRecursively() const; 158 159 // Create the directory so that path exists. Returns true if successful or 160 // if the directory already exists; returns false if unable to create the 161 // directory for any reason, including if the parent directory does not 162 // exist. Not named "CreateDirectory" because that's a macro on Windows. 163 bool CreateFolder() const; 164 165 // Returns true if FilePath describes something in the file-system, 166 // either a file, directory, or whatever, and that something exists. 167 bool FileOrDirectoryExists() const; 168 169 // Returns true if pathname describes a directory in the file-system 170 // that exists. 171 bool DirectoryExists() const; 172 173 // Returns true if FilePath ends with a path separator, which indicates that 174 // it is intended to represent a directory. Returns false otherwise. 175 // This does NOT check that a directory (or file) actually exists. 176 bool IsDirectory() const; 177 178 // Returns true if pathname describes a root directory. (Windows has one 179 // root directory per disk drive.) 180 bool IsRootDirectory() const; 181 182 // Returns true if pathname describes an absolute path. 183 bool IsAbsolutePath() const; 184 185 private: 186 // Replaces multiple consecutive separators with a single separator. 187 // For example, "bar///foo" becomes "bar/foo". Does not eliminate other 188 // redundancies that might be in a pathname involving "." or "..". 189 // 190 // A pathname with multiple consecutive separators may occur either through 191 // user error or as a result of some scripts or APIs that generate a pathname 192 // with a trailing separator. On other platforms the same API or script 193 // may NOT generate a pathname with a trailing "/". Then elsewhere that 194 // pathname may have another "/" and pathname components added to it, 195 // without checking for the separator already being there. 196 // The script language and operating system may allow paths like "foo//bar" 197 // but some of the functions in FilePath will not handle that correctly. In 198 // particular, RemoveTrailingPathSeparator() only removes one separator, and 199 // it is called in CreateDirectoriesRecursively() assuming that it will change 200 // a pathname from directory syntax (trailing separator) to filename syntax. 201 // 202 // On Windows this method also replaces the alternate path separator '/' with 203 // the primary path separator '\\', so that for example "bar\\/\\foo" becomes 204 // "bar\\foo". 205 206 void Normalize(); 207 208 // Returns a pointer to the last occurrence of a valid path separator in 209 // the FilePath. On Windows, for example, both '/' and '\' are valid path 210 // separators. Returns NULL if no path separator was found. 211 const char* FindLastPathSeparator() const; 212 213 // Returns the length of the path root, including the directory separator at 214 // the end of the prefix. Returns zero by definition if the path is relative. 215 // Examples: 216 // - [Windows] "..\Sibling" => 0 217 // - [Windows] "\Windows" => 1 218 // - [Windows] "C:/Windows\Notepad.exe" => 3 219 // - [Windows] "\\Host\Share\C$/Windows" => 13 220 // - [UNIX] "/bin" => 1 221 size_t CalculateRootLength() const; 222 223 std::string pathname_; 224 }; // class FilePath 225 226 } // namespace internal 227 } // namespace testing 228 229 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 230 231 #endif // GTEST_HAS_FILE_SYSTEM 232 233 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ 234