michael@0: // Copyright 2008, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Authors: keith.ray@gmail.com (Keith Ray) michael@0: michael@0: #include "gtest/internal/gtest-filepath.h" michael@0: #include "gtest/internal/gtest-port.h" michael@0: michael@0: #include michael@0: michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: # include michael@0: #elif GTEST_OS_WINDOWS michael@0: # include michael@0: # include michael@0: #elif GTEST_OS_SYMBIAN michael@0: // Symbian OpenC has PATH_MAX in sys/syslimits.h michael@0: # include michael@0: #else michael@0: # include michael@0: # include // Some Linux distributions define PATH_MAX here. michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: #if GTEST_OS_WINDOWS michael@0: # define GTEST_PATH_MAX_ _MAX_PATH michael@0: #elif defined(PATH_MAX) michael@0: # define GTEST_PATH_MAX_ PATH_MAX michael@0: #elif defined(_XOPEN_PATH_MAX) michael@0: # define GTEST_PATH_MAX_ _XOPEN_PATH_MAX michael@0: #else michael@0: # define GTEST_PATH_MAX_ _POSIX_PATH_MAX michael@0: #endif // GTEST_OS_WINDOWS michael@0: michael@0: #include "gtest/internal/gtest-string.h" michael@0: michael@0: namespace testing { michael@0: namespace internal { michael@0: michael@0: #if GTEST_OS_WINDOWS michael@0: // On Windows, '\\' is the standard path separator, but many tools and the michael@0: // Windows API also accept '/' as an alternate path separator. Unless otherwise michael@0: // noted, a file path can contain either kind of path separators, or a mixture michael@0: // of them. michael@0: const char kPathSeparator = '\\'; michael@0: const char kAlternatePathSeparator = '/'; michael@0: const char kPathSeparatorString[] = "\\"; michael@0: const char kAlternatePathSeparatorString[] = "/"; michael@0: # if GTEST_OS_WINDOWS_MOBILE michael@0: // Windows CE doesn't have a current directory. You should not use michael@0: // the current directory in tests on Windows CE, but this at least michael@0: // provides a reasonable fallback. michael@0: const char kCurrentDirectoryString[] = "\\"; michael@0: // Windows CE doesn't define INVALID_FILE_ATTRIBUTES michael@0: const DWORD kInvalidFileAttributes = 0xffffffff; michael@0: # else michael@0: const char kCurrentDirectoryString[] = ".\\"; michael@0: # endif // GTEST_OS_WINDOWS_MOBILE michael@0: #else michael@0: const char kPathSeparator = '/'; michael@0: const char kPathSeparatorString[] = "/"; michael@0: const char kCurrentDirectoryString[] = "./"; michael@0: #endif // GTEST_OS_WINDOWS michael@0: michael@0: // Returns whether the given character is a valid path separator. michael@0: static bool IsPathSeparator(char c) { michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: return (c == kPathSeparator) || (c == kAlternatePathSeparator); michael@0: #else michael@0: return c == kPathSeparator; michael@0: #endif michael@0: } michael@0: michael@0: // Returns the current working directory, or "" if unsuccessful. michael@0: FilePath FilePath::GetCurrentDir() { michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: // Windows CE doesn't have a current directory, so we just return michael@0: // something reasonable. michael@0: return FilePath(kCurrentDirectoryString); michael@0: #elif GTEST_OS_WINDOWS michael@0: char cwd[GTEST_PATH_MAX_ + 1] = { '\0' }; michael@0: return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd); michael@0: #else michael@0: char cwd[GTEST_PATH_MAX_ + 1] = { '\0' }; michael@0: return FilePath(getcwd(cwd, sizeof(cwd)) == NULL ? "" : cwd); michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: } michael@0: michael@0: // Returns a copy of the FilePath with the case-insensitive extension removed. michael@0: // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns michael@0: // FilePath("dir/file"). If a case-insensitive extension is not michael@0: // found, returns a copy of the original FilePath. michael@0: FilePath FilePath::RemoveExtension(const char* extension) const { michael@0: String dot_extension(String::Format(".%s", extension)); michael@0: if (pathname_.EndsWithCaseInsensitive(dot_extension.c_str())) { michael@0: return FilePath(String(pathname_.c_str(), pathname_.length() - 4)); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Returns a pointer to the last occurence of a valid path separator in michael@0: // the FilePath. On Windows, for example, both '/' and '\' are valid path michael@0: // separators. Returns NULL if no path separator was found. michael@0: const char* FilePath::FindLastPathSeparator() const { michael@0: const char* const last_sep = strrchr(c_str(), kPathSeparator); michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator); michael@0: // Comparing two pointers of which only one is NULL is undefined. michael@0: if (last_alt_sep != NULL && michael@0: (last_sep == NULL || last_alt_sep > last_sep)) { michael@0: return last_alt_sep; michael@0: } michael@0: #endif michael@0: return last_sep; michael@0: } michael@0: michael@0: // Returns a copy of the FilePath with the directory part removed. michael@0: // Example: FilePath("path/to/file").RemoveDirectoryName() returns michael@0: // FilePath("file"). If there is no directory part ("just_a_file"), it returns michael@0: // the FilePath unmodified. If there is no file part ("just_a_dir/") it michael@0: // returns an empty FilePath (""). michael@0: // On Windows platform, '\' is the path separator, otherwise it is '/'. michael@0: FilePath FilePath::RemoveDirectoryName() const { michael@0: const char* const last_sep = FindLastPathSeparator(); michael@0: return last_sep ? FilePath(String(last_sep + 1)) : *this; michael@0: } michael@0: michael@0: // RemoveFileName returns the directory path with the filename removed. michael@0: // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". michael@0: // If the FilePath is "a_file" or "/a_file", RemoveFileName returns michael@0: // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does michael@0: // not have a file, like "just/a/dir/", it returns the FilePath unmodified. michael@0: // On Windows platform, '\' is the path separator, otherwise it is '/'. michael@0: FilePath FilePath::RemoveFileName() const { michael@0: const char* const last_sep = FindLastPathSeparator(); michael@0: String dir; michael@0: if (last_sep) { michael@0: dir = String(c_str(), last_sep + 1 - c_str()); michael@0: } else { michael@0: dir = kCurrentDirectoryString; michael@0: } michael@0: return FilePath(dir); michael@0: } michael@0: michael@0: // Helper functions for naming files in a directory for xml output. michael@0: michael@0: // Given directory = "dir", base_name = "test", number = 0, michael@0: // extension = "xml", returns "dir/test.xml". If number is greater michael@0: // than zero (e.g., 12), returns "dir/test_12.xml". michael@0: // On Windows platform, uses \ as the separator rather than /. michael@0: FilePath FilePath::MakeFileName(const FilePath& directory, michael@0: const FilePath& base_name, michael@0: int number, michael@0: const char* extension) { michael@0: String file; michael@0: if (number == 0) { michael@0: file = String::Format("%s.%s", base_name.c_str(), extension); michael@0: } else { michael@0: file = String::Format("%s_%d.%s", base_name.c_str(), number, extension); michael@0: } michael@0: return ConcatPaths(directory, FilePath(file)); michael@0: } michael@0: michael@0: // Given directory = "dir", relative_path = "test.xml", returns "dir/test.xml". michael@0: // On Windows, uses \ as the separator rather than /. michael@0: FilePath FilePath::ConcatPaths(const FilePath& directory, michael@0: const FilePath& relative_path) { michael@0: if (directory.IsEmpty()) michael@0: return relative_path; michael@0: const FilePath dir(directory.RemoveTrailingPathSeparator()); michael@0: return FilePath(String::Format("%s%c%s", dir.c_str(), kPathSeparator, michael@0: relative_path.c_str())); michael@0: } michael@0: michael@0: // Returns true if pathname describes something findable in the file-system, michael@0: // either a file, directory, or whatever. michael@0: bool FilePath::FileOrDirectoryExists() const { michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str()); michael@0: const DWORD attributes = GetFileAttributes(unicode); michael@0: delete [] unicode; michael@0: return attributes != kInvalidFileAttributes; michael@0: #else michael@0: posix::StatStruct file_stat; michael@0: return posix::Stat(pathname_.c_str(), &file_stat) == 0; michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: } michael@0: michael@0: // Returns true if pathname describes a directory in the file-system michael@0: // that exists. michael@0: bool FilePath::DirectoryExists() const { michael@0: bool result = false; michael@0: #if GTEST_OS_WINDOWS michael@0: // Don't strip off trailing separator if path is a root directory on michael@0: // Windows (like "C:\\"). michael@0: const FilePath& path(IsRootDirectory() ? *this : michael@0: RemoveTrailingPathSeparator()); michael@0: #else michael@0: const FilePath& path(*this); michael@0: #endif michael@0: michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: LPCWSTR unicode = String::AnsiToUtf16(path.c_str()); michael@0: const DWORD attributes = GetFileAttributes(unicode); michael@0: delete [] unicode; michael@0: if ((attributes != kInvalidFileAttributes) && michael@0: (attributes & FILE_ATTRIBUTE_DIRECTORY)) { michael@0: result = true; michael@0: } michael@0: #else michael@0: posix::StatStruct file_stat; michael@0: result = posix::Stat(path.c_str(), &file_stat) == 0 && michael@0: posix::IsDir(file_stat); michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: return result; michael@0: } michael@0: michael@0: // Returns true if pathname describes a root directory. (Windows has one michael@0: // root directory per disk drive.) michael@0: bool FilePath::IsRootDirectory() const { michael@0: #if GTEST_OS_WINDOWS michael@0: // TODO(wan@google.com): on Windows a network share like michael@0: // \\server\share can be a root directory, although it cannot be the michael@0: // current directory. Handle this properly. michael@0: return pathname_.length() == 3 && IsAbsolutePath(); michael@0: #else michael@0: return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]); michael@0: #endif michael@0: } michael@0: michael@0: // Returns true if pathname describes an absolute path. michael@0: bool FilePath::IsAbsolutePath() const { michael@0: const char* const name = pathname_.c_str(); michael@0: #if GTEST_OS_WINDOWS michael@0: return pathname_.length() >= 3 && michael@0: ((name[0] >= 'a' && name[0] <= 'z') || michael@0: (name[0] >= 'A' && name[0] <= 'Z')) && michael@0: name[1] == ':' && michael@0: IsPathSeparator(name[2]); michael@0: #else michael@0: return IsPathSeparator(name[0]); michael@0: #endif michael@0: } michael@0: michael@0: // Returns a pathname for a file that does not currently exist. The pathname michael@0: // will be directory/base_name.extension or michael@0: // directory/base_name_.extension if directory/base_name.extension michael@0: // already exists. The number will be incremented until a pathname is found michael@0: // that does not already exist. michael@0: // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. michael@0: // There could be a race condition if two or more processes are calling this michael@0: // function at the same time -- they could both pick the same filename. michael@0: FilePath FilePath::GenerateUniqueFileName(const FilePath& directory, michael@0: const FilePath& base_name, michael@0: const char* extension) { michael@0: FilePath full_pathname; michael@0: int number = 0; michael@0: do { michael@0: full_pathname.Set(MakeFileName(directory, base_name, number++, extension)); michael@0: } while (full_pathname.FileOrDirectoryExists()); michael@0: return full_pathname; michael@0: } michael@0: michael@0: // Returns true if FilePath ends with a path separator, which indicates that michael@0: // it is intended to represent a directory. Returns false otherwise. michael@0: // This does NOT check that a directory (or file) actually exists. michael@0: bool FilePath::IsDirectory() const { michael@0: return !pathname_.empty() && michael@0: IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]); michael@0: } michael@0: michael@0: // Create directories so that path exists. Returns true if successful or if michael@0: // the directories already exist; returns false if unable to create directories michael@0: // for any reason. michael@0: bool FilePath::CreateDirectoriesRecursively() const { michael@0: if (!this->IsDirectory()) { michael@0: return false; michael@0: } michael@0: michael@0: if (pathname_.length() == 0 || this->DirectoryExists()) { michael@0: return true; michael@0: } michael@0: michael@0: const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName()); michael@0: return parent.CreateDirectoriesRecursively() && this->CreateFolder(); michael@0: } michael@0: michael@0: // Create the directory so that path exists. Returns true if successful or michael@0: // if the directory already exists; returns false if unable to create the michael@0: // directory for any reason, including if the parent directory does not michael@0: // exist. Not named "CreateDirectory" because that's a macro on Windows. michael@0: bool FilePath::CreateFolder() const { michael@0: #if GTEST_OS_WINDOWS_MOBILE michael@0: FilePath removed_sep(this->RemoveTrailingPathSeparator()); michael@0: LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str()); michael@0: int result = CreateDirectory(unicode, NULL) ? 0 : -1; michael@0: delete [] unicode; michael@0: #elif GTEST_OS_WINDOWS michael@0: int result = _mkdir(pathname_.c_str()); michael@0: #else michael@0: int result = mkdir(pathname_.c_str(), 0777); michael@0: #endif // GTEST_OS_WINDOWS_MOBILE michael@0: michael@0: if (result == -1) { michael@0: return this->DirectoryExists(); // An error is OK if the directory exists. michael@0: } michael@0: return true; // No error. michael@0: } michael@0: michael@0: // If input name has a trailing separator character, remove it and return the michael@0: // name, otherwise return the name string unmodified. michael@0: // On Windows platform, uses \ as the separator, other platforms use /. michael@0: FilePath FilePath::RemoveTrailingPathSeparator() const { michael@0: return IsDirectory() michael@0: ? FilePath(String(pathname_.c_str(), pathname_.length() - 1)) michael@0: : *this; michael@0: } michael@0: michael@0: // Removes any redundant separators that might be in the pathname. michael@0: // For example, "bar///foo" becomes "bar/foo". Does not eliminate other michael@0: // redundancies that might be in a pathname involving "." or "..". michael@0: // TODO(wan@google.com): handle Windows network shares (e.g. \\server\share). michael@0: void FilePath::Normalize() { michael@0: if (pathname_.c_str() == NULL) { michael@0: pathname_ = ""; michael@0: return; michael@0: } michael@0: const char* src = pathname_.c_str(); michael@0: char* const dest = new char[pathname_.length() + 1]; michael@0: char* dest_ptr = dest; michael@0: memset(dest_ptr, 0, pathname_.length() + 1); michael@0: michael@0: while (*src != '\0') { michael@0: *dest_ptr = *src; michael@0: if (!IsPathSeparator(*src)) { michael@0: src++; michael@0: } else { michael@0: #if GTEST_HAS_ALT_PATH_SEP_ michael@0: if (*dest_ptr == kAlternatePathSeparator) { michael@0: *dest_ptr = kPathSeparator; michael@0: } michael@0: #endif michael@0: while (IsPathSeparator(*src)) michael@0: src++; michael@0: } michael@0: dest_ptr++; michael@0: } michael@0: *dest_ptr = '\0'; michael@0: pathname_ = dest; michael@0: delete[] dest; michael@0: } michael@0: michael@0: } // namespace internal michael@0: } // namespace testing