From 1cf2d04351e253e8e76ea7c12f9aee7da4a67829 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Thu, 7 Jul 2005 23:35:23 +0000 Subject: [PATCH] Changes to mimic those in Unix/Path.inc in support of PR495. This hasn't been compiled or tested. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22350 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/System/Win32/Path.inc | 249 ++++++++++++++------------------------ 1 file changed, 94 insertions(+), 155 deletions(-) diff --git a/lib/System/Win32/Path.inc b/lib/System/Win32/Path.inc index 6c80834ad73..fb6351695df 100644 --- a/lib/System/Win32/Path.inc +++ b/lib/System/Win32/Path.inc @@ -94,7 +94,7 @@ Path::GetTemporaryDirectory() { throw std::string("Can't determine temporary directory"); Path result; - result.setDirectory(pathname); + result.set(pathname); // Append a subdirectory passed on our process id so multiple LLVMs don't // step on each other's toes. @@ -128,7 +128,7 @@ Path::Path(const std::string& unverified_path) Path Path::GetRootDirectory() { Path result; - result.setDirectory("/"); + result.set("C:\\"); return result; } @@ -138,14 +138,14 @@ static void getPathList(const char*path, std::vector& Paths) { Path tmpPath; while( delim != 0 ) { std::string tmp(at, size_t(delim-at)); - if (tmpPath.setDirectory(tmp)) + if (tmpPath.set(tmp)) if (tmpPath.canRead()) Paths.push_back(tmpPath); at = delim + 1; delim = strchr(at, ';'); } if (*at != 0) - if (tmpPath.setDirectory(std::string(at))) + if (tmpPath.set(std::string(at))) if (tmpPath.canRead()) Paths.push_back(tmpPath); @@ -166,7 +166,7 @@ Path::GetBytecodeLibraryPaths(std::vector& Paths) { #ifdef LLVM_LIBDIR { Path tmpPath; - if (tmpPath.setDirectory(LLVM_LIBDIR)) + if (tmpPath.set(LLVM_LIBDIR)) if (tmpPath.canRead()) Paths.push_back(tmpPath); } @@ -186,7 +186,7 @@ Path::GetUserHomeDirectory() { const char* home = getenv("HOME"); if (home) { Path result; - if (result.setDirectory(home)) + if (result.set(home)) return result; } return GetRootDirectory(); @@ -380,58 +380,40 @@ Path::getDirectoryContents(std::set& result) const { } bool -Path::setDirectory(const std::string& a_path) { +Path::set(const std::string& a_path) { if (a_path.size() == 0) return false; - Path save(*this); + std::string save(path); path = a_path; FlipBackSlashes(path); size_t last = a_path.size() -1; - if (a_path[last] != '/') - path += '/'; if (!isValid()) { - path = save.path; + path = save; return false; } return true; } bool -Path::setFile(const std::string& a_path) { - if (a_path.size() == 0) - return false; - Path save(*this); - path = a_path; - FlipBackSlashes(path); - size_t last = a_path.size() - 1; - while (last > 0 && a_path[last] == '/') - last--; - path.erase(last+1); - if (!isValid()) { - path = save.path; +Path::appendComponent(const std::string& dir) { + if (name.empty()) return false; + std::string save(path); + if (!path.empty()) { + size_t last = path.size() - 1; + if (path[last] != '/') + path += '/'; } - return true; -} - -bool -Path::appendDirectory(const std::string& dir) { - if (isFile()) - return false; - Path save(*this); - path += dir; - path += "/"; + path += name; if (!isValid()) { - path = save.path; + path = save; return false; } return true; } bool -Path::elideDirectory() { - if (isFile()) - return false; +Path::eraseComponent() { size_t slashpos = path.rfind('/',path.size()); if (slashpos == 0 || slashpos == std::string::npos) return false; @@ -443,47 +425,20 @@ Path::elideDirectory() { return true; } -bool -Path::appendFile(const std::string& file) { - if (!isDirectory()) - return false; - Path save(*this); - path += file; - if (!isValid()) { - path = save.path; - return false; - } - return true; -} - -bool -Path::elideFile() { - if (isDirectory()) - return false; - size_t slashpos = path.rfind('/',path.size()); - if (slashpos == std::string::npos) - return false; - path.erase(slashpos+1); - return true; -} - bool Path::appendSuffix(const std::string& suffix) { - if (isDirectory()) - return false; - Path save(*this); + std::string save(path); path.append("."); path.append(suffix); if (!isValid()) { - path = save.path; + path = save; return false; } return true; } bool -Path::elideSuffix() { - if (isDirectory()) return false; +Path::eraseSuffix() { size_t dotpos = path.rfind('.',path.size()); size_t slashpos = path.rfind('/',path.size()); if (slashpos != std::string::npos && dotpos != std::string::npos && @@ -494,12 +449,8 @@ Path::elideSuffix() { return false; } - bool Path::createDirectory( bool create_parents) { - // Make sure we're dealing with a directory - if (!isDirectory()) return false; - // Get a writeable copy of the path name char *pathname = reinterpret_cast(_alloca(path.length()+1)); path.copy(pathname,path.length()); @@ -548,9 +499,6 @@ Path::createDirectory( bool create_parents) { bool Path::createFile() { - // Make sure we're dealing with a file - if (!isFile()) return false; - // Create the file HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); @@ -562,89 +510,83 @@ Path::createFile() { } bool -Path::destroyDirectory(bool remove_contents) const { - // Make sure we're dealing with a directory - if (!isDirectory()) return false; +Path::destroy(bool remove_contents) const { + if (isFile()) { + DWORD attr = GetFileAttributes(path.c_str()); + + // If it doesn't exist, we're done. + if (attr == INVALID_FILE_ATTRIBUTES) + return true; + + // Read-only files cannot be deleted on Windows. Must remove the read-only + // attribute first. + if (attr & FILE_ATTRIBUTE_READONLY) { + if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) + ThrowError(path + ": Can't destroy file: "); + } - // If it doesn't exist, we're done. - if (!exists()) return true; - - char *pathname = reinterpret_cast(_alloca(path.length()+2)); - int lastchar = path.length() - 1 ; - path.copy(pathname,lastchar+2); - - // Make path end with '/*'. - pathname[lastchar+1] = '*'; - pathname[lastchar+2] = 0; - - if (remove_contents) { - WIN32_FIND_DATA fd; - HANDLE h = FindFirstFile(pathname, &fd); - - // It's a bad idea to alter the contents of a directory while enumerating - // its contents. So build a list of its contents first, then destroy them. - - if (h != INVALID_HANDLE_VALUE) { - std::vector list; - - do { - if (strcmp(fd.cFileName, ".") == 0) - continue; - if (strcmp(fd.cFileName, "..") == 0) - continue; - - Path aPath(path + &fd.cFileName[0]); - if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - aPath.path += "/"; - list.push_back(aPath); - } while (FindNextFile(h, &fd)); - - DWORD err = GetLastError(); - FindClose(h); - if (err != ERROR_NO_MORE_FILES) { - SetLastError(err); - ThrowError(path + ": Can't read directory: "); - } + if (!DeleteFile(path.c_str())) + ThrowError(path + ": Can't destroy file: "); + return true; + } else if (isDirectory()) { + + // If it doesn't exist, we're done. + if (!exists()) + return true; + + char *pathname = reinterpret_cast(_alloca(path.length()+2)); + int lastchar = path.length() - 1 ; + path.copy(pathname,lastchar+2); + + // Make path end with '/*'. + pathname[lastchar+1] = '*'; + pathname[lastchar+2] = 0; + + if (remove_contents) { + WIN32_FIND_DATA fd; + HANDLE h = FindFirstFile(pathname, &fd); + + // It's a bad idea to alter the contents of a directory while enumerating + // its contents. So build a list of its contents first, then destroy them. + + if (h != INVALID_HANDLE_VALUE) { + std::vector list; + + do { + if (strcmp(fd.cFileName, ".") == 0) + continue; + if (strcmp(fd.cFileName, "..") == 0) + continue; + + Path aPath(path + &fd.cFileName[0]); + if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + aPath.path += "/"; + list.push_back(aPath); + } while (FindNextFile(h, &fd)); + + DWORD err = GetLastError(); + FindClose(h); + if (err != ERROR_NO_MORE_FILES) { + SetLastError(err); + ThrowError(path + ": Can't read directory: "); + } - for (std::vector::iterator I = list.begin(); I != list.end(); ++I) { - Path &aPath = *I; - if (aPath.isDirectory()) - aPath.destroyDirectory(true); - else - aPath.destroyFile(); + for (std::vector::iterator I = list.begin(); I != list.end(); + ++I) { + Path &aPath = *I; + aPath.destroy(true); + } + } else { + if (GetLastError() != ERROR_FILE_NOT_FOUND) + ThrowError(path + ": Can't read directory: "); } - } else { - if (GetLastError() != ERROR_FILE_NOT_FOUND) - ThrowError(path + ": Can't read directory: "); } - } - - pathname[lastchar] = 0; - if (!RemoveDirectory(pathname)) - ThrowError(std::string(pathname) + ": Can't destroy directory: "); - return true; -} -bool -Path::destroyFile() const { - if (!isFile()) return false; - - DWORD attr = GetFileAttributes(path.c_str()); - - // If it doesn't exist, we're done. - if (attr == INVALID_FILE_ATTRIBUTES) + pathname[lastchar] = 0; + if (!RemoveDirectory(pathname)) + ThrowError(std::string(pathname) + ": Can't destroy directory: "); return true; - - // Read-only files cannot be deleted on Windows. Must remove the read-only - // attribute first. - if (attr & FILE_ATTRIBUTE_READONLY) { - if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) - ThrowError(path + ": Can't destroy file: "); } - - if (!DeleteFile(path.c_str())) - ThrowError(path + ": Can't destroy file: "); - return true; } bool Path::getMagicNumber(std::string& Magic, unsigned len) const { @@ -676,7 +618,8 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const { } bool -Path::renameFile(const Path& newName) { +Path::rename(const Path& newName) { + // FIXME: This should rename a directory too. if (!isFile()) return false; if (!MoveFile(path.c_str(), newName.c_str())) ThrowError("Can't move '" + path + @@ -766,10 +709,6 @@ Path::makeUnique(bool reuse_current) { bool Path::createTemporaryFile(bool reuse_current) { - // Make sure we're dealing with a file - if (!isFile()) - return false; - // Make this into a unique file name makeUnique( reuse_current ); -- 2.34.1