2004-09-15 13:49:50 +08:00
|
|
|
//===- llvm/System/Linux/Path.cpp - Linux Path Implementation ---*- C++ -*-===//
|
|
|
|
//
|
2004-09-11 12:59:30 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2004-09-15 13:49:50 +08:00
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
2004-09-11 12:59:30 +08:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
2004-09-15 13:49:50 +08:00
|
|
|
//
|
|
|
|
// Modified by Henrik Bach to comply with at least MinGW.
|
2004-09-19 03:29:16 +08:00
|
|
|
// Ported to Win32 by Jeff Cohen.
|
2004-09-15 13:49:50 +08:00
|
|
|
//
|
2004-09-11 12:59:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of the Path class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-15 13:49:50 +08:00
|
|
|
//=== WARNING: Implementation here must contain only generic Win32 code that
|
|
|
|
//=== is guaranteed to work on *all* Win32 variants.
|
2004-09-11 12:59:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-19 03:29:16 +08:00
|
|
|
#include "Win32.h"
|
|
|
|
#include <malloc.h>
|
2004-09-11 12:59:30 +08:00
|
|
|
|
2004-12-24 10:38:34 +08:00
|
|
|
// We need to undo a macro defined in Windows.h, otherwise we won't compile:
|
|
|
|
#undef CopyFile
|
|
|
|
|
2004-09-29 08:01:17 +08:00
|
|
|
static void FlipBackSlashes(std::string& s) {
|
|
|
|
for (size_t i = 0; i < s.size(); i++)
|
|
|
|
if (s[i] == '\\')
|
|
|
|
s[i] = '/';
|
|
|
|
}
|
|
|
|
|
2004-09-11 12:59:30 +08:00
|
|
|
namespace llvm {
|
2004-09-15 13:49:50 +08:00
|
|
|
namespace sys {
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::isValid() const {
|
2004-09-15 13:49:50 +08:00
|
|
|
if (path.empty())
|
|
|
|
return false;
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2004-09-29 08:01:17 +08:00
|
|
|
// If there is a colon, it must be the second character, preceded by a letter
|
|
|
|
// and followed by something.
|
|
|
|
size_t len = path.size();
|
|
|
|
size_t pos = path.rfind(':',len);
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
if (pos != 1 || !isalpha(path[0]) || len < 3)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for illegal characters.
|
|
|
|
if (path.find_first_of("\\<>\"|\001\002\003\004\005\006\007\010\011\012"
|
|
|
|
"\013\014\015\016\017\020\021\022\023\024\025\026"
|
|
|
|
"\027\030\031\032\033\034\035\036\037")
|
|
|
|
!= std::string::npos)
|
|
|
|
return false;
|
|
|
|
|
2005-01-14 12:09:39 +08:00
|
|
|
// Check each component for legality.
|
|
|
|
for (pos = 0; pos < len; ++pos) {
|
|
|
|
// A component may not end in a space.
|
|
|
|
if (path[pos] == ' ') {
|
|
|
|
if (path[pos+1] == '/' || path[pos+1] == '\0')
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-29 08:01:17 +08:00
|
|
|
|
2005-01-14 12:09:39 +08:00
|
|
|
// A component may not end in a period.
|
|
|
|
if (path[pos] == '.') {
|
|
|
|
if (path[pos+1] == '/' || path[pos+1] == '\0') {
|
|
|
|
// Unless it is the pseudo-directory "."...
|
|
|
|
if (pos == 0 || path[pos-1] == '/' || path[pos-1] == ':')
|
|
|
|
return true;
|
|
|
|
// or "..".
|
|
|
|
if (pos > 0 && path[pos-1] == '.') {
|
|
|
|
if (pos == 1 || path[pos-2] == '/' || path[pos-2] == ':')
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2004-09-29 08:01:17 +08:00
|
|
|
return true;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2004-09-19 03:29:16 +08:00
|
|
|
static Path *TempDirectory = NULL;
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
Path
|
|
|
|
Path::GetTemporaryDirectory() {
|
2004-09-19 03:29:16 +08:00
|
|
|
if (TempDirectory)
|
|
|
|
return *TempDirectory;
|
|
|
|
|
|
|
|
char pathname[MAX_PATH];
|
|
|
|
if (!GetTempPath(MAX_PATH, pathname))
|
2004-09-29 08:01:17 +08:00
|
|
|
throw std::string("Can't determine temporary directory");
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
Path result;
|
2004-11-06 06:15:36 +08:00
|
|
|
result.setDirectory(pathname);
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
// Append a subdirectory passed on our process id so multiple LLVMs don't
|
|
|
|
// step on each other's toes.
|
|
|
|
sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
|
2004-11-06 06:15:36 +08:00
|
|
|
result.appendDirectory(pathname);
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
// If there's a directory left over from a previous LLVM execution that
|
|
|
|
// happened to have the same process id, get rid of it.
|
2004-11-06 06:15:36 +08:00
|
|
|
result.destroyDirectory(true);
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
// And finally (re-)create the empty directory.
|
2004-11-06 06:15:36 +08:00
|
|
|
result.createDirectory(false);
|
2004-09-19 03:29:16 +08:00
|
|
|
TempDirectory = new Path(result);
|
|
|
|
return *TempDirectory;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-13 14:57:15 +08:00
|
|
|
Path::Path(const std::string& unverified_path)
|
2004-09-15 13:49:50 +08:00
|
|
|
: path(unverified_path)
|
|
|
|
{
|
2004-09-29 08:01:17 +08:00
|
|
|
FlipBackSlashes(path);
|
2004-09-15 13:49:50 +08:00
|
|
|
if (unverified_path.empty())
|
|
|
|
return;
|
2004-11-06 06:15:36 +08:00
|
|
|
if (this->isValid())
|
2004-09-15 13:49:50 +08:00
|
|
|
return;
|
|
|
|
// oops, not valid.
|
|
|
|
path.clear();
|
2004-09-29 08:01:17 +08:00
|
|
|
throw std::string(unverified_path + ": path is not valid");
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2004-09-19 03:29:16 +08:00
|
|
|
// FIXME: the following set of functions don't map to Windows very well.
|
2004-09-15 13:49:50 +08:00
|
|
|
Path
|
|
|
|
Path::GetRootDirectory() {
|
|
|
|
Path result;
|
2004-11-06 06:15:36 +08:00
|
|
|
result.setDirectory("/");
|
2004-09-15 13:49:50 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-12-13 11:03:42 +08:00
|
|
|
static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
|
|
|
|
const char* at = path;
|
|
|
|
const char* delim = strchr(at, ';');
|
|
|
|
Path tmpPath;
|
|
|
|
while( delim != 0 ) {
|
|
|
|
std::string tmp(at, size_t(delim-at));
|
|
|
|
if (tmpPath.setDirectory(tmp))
|
|
|
|
if (tmpPath.readable())
|
|
|
|
Paths.push_back(tmpPath);
|
|
|
|
at = delim + 1;
|
|
|
|
delim = strchr(at, ';');
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
2004-12-13 11:03:42 +08:00
|
|
|
if (*at != 0)
|
|
|
|
if (tmpPath.setDirectory(std::string(at)))
|
|
|
|
if (tmpPath.readable())
|
|
|
|
Paths.push_back(tmpPath);
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-12-13 11:03:42 +08:00
|
|
|
void
|
|
|
|
Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
|
|
|
|
Paths.push_back(sys::Path("C:\\WINDOWS\\SYSTEM32\\"));
|
|
|
|
Paths.push_back(sys::Path("C:\\WINDOWS\\"));
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-13 11:03:42 +08:00
|
|
|
void
|
|
|
|
Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
|
|
|
|
char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
|
|
|
|
if (env_var != 0) {
|
|
|
|
getPathList(env_var,Paths);
|
|
|
|
}
|
|
|
|
#ifdef LLVM_LIBDIR
|
|
|
|
{
|
|
|
|
Path tmpPath;
|
|
|
|
if (tmpPath.setDirectory(LLVM_LIBDIR))
|
|
|
|
if (tmpPath.readable())
|
|
|
|
Paths.push_back(tmpPath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
GetSystemLibraryPaths(Paths);
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2004-09-11 12:59:30 +08:00
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
Path
|
|
|
|
Path::GetLLVMDefaultConfigDir() {
|
2004-12-31 12:39:07 +08:00
|
|
|
// TODO: this isn't going to fly on Windows
|
2004-09-15 13:49:50 +08:00
|
|
|
return Path("/etc/llvm/");
|
|
|
|
}
|
|
|
|
|
|
|
|
Path
|
|
|
|
Path::GetUserHomeDirectory() {
|
2004-12-31 12:39:07 +08:00
|
|
|
// TODO: Typical Windows setup doesn't define HOME.
|
2004-09-15 13:49:50 +08:00
|
|
|
const char* home = getenv("HOME");
|
|
|
|
if (home) {
|
|
|
|
Path result;
|
2004-11-06 06:15:36 +08:00
|
|
|
if (result.setDirectory(home))
|
2004-09-15 13:49:50 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return GetRootDirectory();
|
|
|
|
}
|
2004-09-19 03:29:16 +08:00
|
|
|
// FIXME: the above set of functions don't map to Windows very well.
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::isFile() const {
|
|
|
|
return (isValid() && path[path.length()-1] != '/');
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::isDirectory() const {
|
|
|
|
return (isValid() && path[path.length()-1] == '/');
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::getBasename() const {
|
2004-09-19 03:29:16 +08:00
|
|
|
// Find the last slash
|
|
|
|
size_t slash = path.rfind('/');
|
|
|
|
if (slash == std::string::npos)
|
|
|
|
slash = 0;
|
|
|
|
else
|
|
|
|
slash++;
|
|
|
|
|
|
|
|
return path.substr(slash, path.rfind('.'));
|
|
|
|
}
|
|
|
|
|
2004-11-06 06:15:36 +08:00
|
|
|
bool Path::hasMagicNumber(const std::string &Magic) const {
|
2005-01-01 03:01:08 +08:00
|
|
|
std::string actualMagic;
|
|
|
|
if (getMagicNumber(actualMagic, Magic.size()))
|
|
|
|
return Magic == actualMagic;
|
|
|
|
return false;
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::isBytecodeFile() const {
|
2005-01-01 03:01:08 +08:00
|
|
|
std::string actualMagic;
|
|
|
|
if (!getMagicNumber(actualMagic, 4))
|
|
|
|
return false;
|
|
|
|
return actualMagic == "llvc" || actualMagic == "llvm";
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
|
|
|
Path::exists() const {
|
2004-09-19 03:29:16 +08:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return attr != INVALID_FILE_ATTRIBUTES;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::readable() const {
|
2004-09-19 03:29:16 +08:00
|
|
|
// FIXME: take security attributes into account.
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return attr != INVALID_FILE_ATTRIBUTES;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::writable() const {
|
2004-09-19 03:29:16 +08:00
|
|
|
// FIXME: take security attributes into account.
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_READONLY);
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::executable() const {
|
2004-09-19 03:29:16 +08:00
|
|
|
// FIXME: take security attributes into account.
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return attr != INVALID_FILE_ATTRIBUTES;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2004-09-11 12:59:30 +08:00
|
|
|
|
|
|
|
std::string
|
2004-09-15 13:49:50 +08:00
|
|
|
Path::getLast() const {
|
|
|
|
// Find the last slash
|
|
|
|
size_t pos = path.rfind('/');
|
|
|
|
|
|
|
|
// Handle the corner cases
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
// If the last character is a slash
|
|
|
|
if (pos == path.length()-1) {
|
|
|
|
// Find the second to last slash
|
|
|
|
size_t pos2 = path.rfind('/', pos-1);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
return path.substr(0,pos);
|
|
|
|
else
|
|
|
|
return path.substr(pos2+1,pos-pos2-1);
|
|
|
|
}
|
|
|
|
// Return everything after the last slash
|
|
|
|
return path.substr(pos+1);
|
|
|
|
}
|
|
|
|
|
2004-12-14 13:26:43 +08:00
|
|
|
void
|
|
|
|
Path::getStatusInfo(StatusInfo& info) const {
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
|
|
|
|
ThrowError(std::string(path) + ": Can't get status: ");
|
|
|
|
|
|
|
|
info.fileSize = fi.nFileSizeHigh;
|
|
|
|
info.fileSize <<= 32;
|
|
|
|
info.fileSize += fi.nFileSizeLow;
|
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
info.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
|
2004-12-14 13:26:43 +08:00
|
|
|
info.user = 9999; // Not applicable to Windows, so...
|
|
|
|
info.group = 9999; // Not applicable to Windows, so...
|
|
|
|
|
|
|
|
__int64 ft = *reinterpret_cast<__int64*>(&fi.ftLastWriteTime);
|
|
|
|
info.modTime.fromWin32Time(ft);
|
|
|
|
|
|
|
|
info.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if (info.isDir && path[path.length() - 1] != '/')
|
|
|
|
path += '/';
|
|
|
|
else if (!info.isDir && path[path.length() - 1] == '/')
|
|
|
|
path.erase(path.length() - 1);
|
|
|
|
}
|
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
static bool AddPermissionBits(const std::string& Filename, int bits) {
|
|
|
|
DWORD attr = GetFileAttributes(Filename.c_str());
|
|
|
|
|
|
|
|
// If it doesn't exist, we're done.
|
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The best we can do to interpret Unix permission bits is to use
|
|
|
|
// the owner writable bit.
|
|
|
|
if ((attr & FILE_ATTRIBUTE_READONLY) && (bits & 0200)) {
|
|
|
|
if (!SetFileAttributes(Filename.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
|
2004-12-31 13:07:26 +08:00
|
|
|
ThrowError(Filename + ": SetFileAttributes: ");
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-14 03:59:50 +08:00
|
|
|
void Path::makeReadable() {
|
2004-12-14 13:26:43 +08:00
|
|
|
// All files are readable on Windows (ignoring security attributes).
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Path::makeWriteable() {
|
2004-12-14 13:26:43 +08:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
|
|
|
|
// If it doesn't exist, we're done.
|
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (attr & FILE_ATTRIBUTE_READONLY) {
|
|
|
|
if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY))
|
|
|
|
ThrowError(std::string(path) + ": Can't make file writable: ");
|
|
|
|
}
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Path::makeExecutable() {
|
2004-12-14 13:26:43 +08:00
|
|
|
// All files are executable on Windows (ignoring security attributes).
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
bool
|
|
|
|
Path::getDirectoryContents(std::set<Path>& result) const {
|
|
|
|
if (!isDirectory())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
result.clear();
|
|
|
|
WIN32_FIND_DATA fd;
|
|
|
|
HANDLE h = FindFirstFile(path.c_str(), &fd);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2004-12-31 13:07:26 +08:00
|
|
|
if (GetLastError() == ERROR_NO_MORE_FILES)
|
2004-12-31 12:39:07 +08:00
|
|
|
return true; // not really an error, now is it?
|
2004-12-31 13:07:26 +08:00
|
|
|
ThrowError(path + ": Can't read directory: ");
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2004-12-31 13:07:26 +08:00
|
|
|
if (fd.cFileName[0] == '.')
|
|
|
|
continue;
|
|
|
|
Path aPath(path + &fd.cFileName[0]);
|
|
|
|
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
aPath.path += "/";
|
2004-12-31 12:39:07 +08:00
|
|
|
result.insert(aPath);
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
|
2005-01-01 03:01:08 +08:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
FindClose(h);
|
|
|
|
if (err != ERROR_NO_MORE_FILES) {
|
|
|
|
SetLastError(err);
|
2004-12-31 13:07:26 +08:00
|
|
|
ThrowError(path + ": Can't read directory: ");
|
2005-01-01 03:01:08 +08:00
|
|
|
}
|
2004-12-31 12:39:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::setDirectory(const std::string& a_path) {
|
2004-09-15 13:49:50 +08:00
|
|
|
if (a_path.size() == 0)
|
|
|
|
return false;
|
|
|
|
Path save(*this);
|
|
|
|
path = a_path;
|
2004-09-29 08:01:17 +08:00
|
|
|
FlipBackSlashes(path);
|
2004-09-15 13:49:50 +08:00
|
|
|
size_t last = a_path.size() -1;
|
2004-12-13 15:51:52 +08:00
|
|
|
if (a_path[last] != '/')
|
2004-09-15 13:49:50 +08:00
|
|
|
path += '/';
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2004-09-15 13:49:50 +08:00
|
|
|
path = save.path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::setFile(const std::string& a_path) {
|
2004-09-15 13:49:50 +08:00
|
|
|
if (a_path.size() == 0)
|
|
|
|
return false;
|
|
|
|
Path save(*this);
|
|
|
|
path = a_path;
|
2004-09-29 08:01:17 +08:00
|
|
|
FlipBackSlashes(path);
|
2004-09-15 13:49:50 +08:00
|
|
|
size_t last = a_path.size() - 1;
|
|
|
|
while (last > 0 && a_path[last] == '/')
|
|
|
|
last--;
|
|
|
|
path.erase(last+1);
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2004-09-15 13:49:50 +08:00
|
|
|
path = save.path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::appendDirectory(const std::string& dir) {
|
|
|
|
if (isFile())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
Path save(*this);
|
|
|
|
path += dir;
|
|
|
|
path += "/";
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2004-09-15 13:49:50 +08:00
|
|
|
path = save.path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::elideDirectory() {
|
|
|
|
if (isFile())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
|
|
|
if (slashpos == 0 || slashpos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
if (slashpos == path.size() - 1)
|
|
|
|
slashpos = path.rfind('/',slashpos-1);
|
|
|
|
if (slashpos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
path.erase(slashpos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::appendFile(const std::string& file) {
|
|
|
|
if (!isDirectory())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
Path save(*this);
|
|
|
|
path += file;
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2004-09-15 13:49:50 +08:00
|
|
|
path = save.path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::elideFile() {
|
|
|
|
if (isDirectory())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
|
|
|
if (slashpos == std::string::npos)
|
|
|
|
return false;
|
|
|
|
path.erase(slashpos+1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::appendSuffix(const std::string& suffix) {
|
|
|
|
if (isDirectory())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
Path save(*this);
|
|
|
|
path.append(".");
|
|
|
|
path.append(suffix);
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2004-09-15 13:49:50 +08:00
|
|
|
path = save.path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::elideSuffix() {
|
|
|
|
if (isDirectory()) return false;
|
2004-09-15 13:49:50 +08:00
|
|
|
size_t dotpos = path.rfind('.',path.size());
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
|
|
|
if (slashpos != std::string::npos && dotpos != std::string::npos &&
|
|
|
|
dotpos > slashpos) {
|
|
|
|
path.erase(dotpos, path.size()-dotpos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::createDirectory( bool create_parents) {
|
2004-09-15 13:49:50 +08:00
|
|
|
// Make sure we're dealing with a directory
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isDirectory()) return false;
|
2004-09-15 13:49:50 +08:00
|
|
|
|
|
|
|
// Get a writeable copy of the path name
|
2004-09-19 03:29:16 +08:00
|
|
|
char *pathname = reinterpret_cast<char *>(_alloca(path.length()+1));
|
2004-09-29 08:01:17 +08:00
|
|
|
path.copy(pathname,path.length());
|
|
|
|
pathname[path.length()] = 0;
|
|
|
|
|
|
|
|
// Determine starting point for initial / search.
|
|
|
|
char *next = pathname;
|
|
|
|
if (pathname[0] == '/' && pathname[1] == '/') {
|
|
|
|
// Skip host name.
|
|
|
|
next = strchr(pathname+2, '/');
|
|
|
|
if (next == NULL)
|
|
|
|
throw std::string(pathname) + ": badly formed remote directory";
|
|
|
|
// Skip share name.
|
|
|
|
next = strchr(next+1, '/');
|
|
|
|
if (next == NULL)
|
|
|
|
throw std::string(pathname) + ": badly formed remote directory";
|
|
|
|
next++;
|
|
|
|
if (*next == 0)
|
|
|
|
throw std::string(pathname) + ": badly formed remote directory";
|
|
|
|
} else {
|
|
|
|
if (pathname[1] == ':')
|
|
|
|
next += 2; // skip drive letter
|
|
|
|
if (*next == '/')
|
|
|
|
next++; // skip root directory
|
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
|
|
|
|
// If we're supposed to create intermediate directories
|
2004-09-29 08:01:17 +08:00
|
|
|
if (create_parents) {
|
2004-09-15 13:49:50 +08:00
|
|
|
// Loop through the directory components until we're done
|
2004-09-29 08:01:17 +08:00
|
|
|
while (*next) {
|
|
|
|
next = strchr(next, '/');
|
2004-09-15 13:49:50 +08:00
|
|
|
*next = 0;
|
2004-09-19 03:29:16 +08:00
|
|
|
if (!CreateDirectory(pathname, NULL))
|
|
|
|
ThrowError(std::string(pathname) + ": Can't create directory: ");
|
2004-09-29 08:01:17 +08:00
|
|
|
*next++ = '/';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Drop trailing slash.
|
|
|
|
pathname[path.size()-1] = 0;
|
|
|
|
if (!CreateDirectory(pathname, NULL)) {
|
|
|
|
ThrowError(std::string(pathname) + ": Can't create directory: ");
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::createFile() {
|
2004-09-15 13:49:50 +08:00
|
|
|
// Make sure we're dealing with a file
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isFile()) return false;
|
2004-09-15 13:49:50 +08:00
|
|
|
|
|
|
|
// Create the file
|
2004-09-29 08:01:17 +08:00
|
|
|
HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
|
2004-09-19 03:29:16 +08:00
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2005-01-01 03:01:08 +08:00
|
|
|
ThrowError(path + ": Can't create file: ");
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2004-09-19 03:29:16 +08:00
|
|
|
CloseHandle(h);
|
2004-09-15 13:49:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-12-16 07:02:10 +08:00
|
|
|
Path::destroyDirectory(bool remove_contents) const {
|
2004-09-15 13:49:50 +08:00
|
|
|
// Make sure we're dealing with a directory
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isDirectory()) return false;
|
2004-09-15 13:49:50 +08:00
|
|
|
|
|
|
|
// If it doesn't exist, we're done.
|
|
|
|
if (!exists()) return true;
|
|
|
|
|
2005-01-23 00:28:33 +08:00
|
|
|
char *pathname = reinterpret_cast<char *>(_alloca(path.length()+2));
|
2004-09-19 03:29:16 +08:00
|
|
|
int lastchar = path.length() - 1 ;
|
2005-01-23 00:28:33 +08:00
|
|
|
path.copy(pathname,lastchar+2);
|
|
|
|
|
|
|
|
// Make path end with '/*'.
|
|
|
|
pathname[lastchar+1] = '*';
|
|
|
|
pathname[lastchar+2] = 0;
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
if (remove_contents) {
|
2005-01-01 03:01:08 +08:00
|
|
|
WIN32_FIND_DATA fd;
|
2005-01-23 00:28:33 +08:00
|
|
|
HANDLE h = FindFirstFile(pathname, &fd);
|
2005-01-01 03:01:08 +08:00
|
|
|
|
|
|
|
// 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<Path> 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: ");
|
|
|
|
}
|
|
|
|
|
2005-01-01 03:03:31 +08:00
|
|
|
for (std::vector<Path>::iterator I = list.begin(); I != list.end(); ++I) {
|
2005-01-01 03:01:08 +08:00
|
|
|
Path &aPath = *I;
|
|
|
|
if (aPath.isDirectory())
|
|
|
|
aPath.destroyDirectory(true);
|
|
|
|
else
|
|
|
|
aPath.destroyFile();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (GetLastError() != ERROR_NO_MORE_FILES)
|
|
|
|
ThrowError(path + ": Can't read directory: ");
|
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2005-01-01 03:01:08 +08:00
|
|
|
|
2005-01-23 00:28:33 +08:00
|
|
|
pathname[lastchar] = 0;
|
2005-01-01 03:01:08 +08:00
|
|
|
if (!RemoveDirectory(pathname))
|
|
|
|
ThrowError(std::string(pathname) + ": Can't destroy directory: ");
|
2004-09-15 13:49:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-12-16 07:02:10 +08:00
|
|
|
Path::destroyFile() const {
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isFile()) return false;
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
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))
|
2005-01-01 03:01:08 +08:00
|
|
|
ThrowError(path + ": Can't destroy file: ");
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DeleteFile(path.c_str()))
|
2005-01-01 03:01:08 +08:00
|
|
|
ThrowError(path + ": Can't destroy file: ");
|
2004-09-15 13:49:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-15 02:42:13 +08:00
|
|
|
bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
|
|
|
|
if (!isFile())
|
|
|
|
return false;
|
|
|
|
assert(len < 1024 && "Request for magic string too long");
|
|
|
|
char* buf = (char*) alloca(1 + len);
|
2005-01-01 03:01:08 +08:00
|
|
|
|
|
|
|
HANDLE h = CreateFile(path.c_str(),
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2004-12-15 02:42:13 +08:00
|
|
|
return false;
|
2005-01-01 03:01:08 +08:00
|
|
|
|
|
|
|
DWORD nRead = 0;
|
|
|
|
BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
|
|
|
|
CloseHandle(h);
|
|
|
|
|
|
|
|
if (!ret || nRead != len)
|
2004-12-15 02:42:13 +08:00
|
|
|
return false;
|
2005-01-01 03:01:08 +08:00
|
|
|
|
2004-12-15 02:42:13 +08:00
|
|
|
buf[len] = '\0';
|
|
|
|
Magic = buf;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
bool
|
|
|
|
Path::renameFile(const Path& newName) {
|
|
|
|
if (!isFile()) return false;
|
|
|
|
if (!MoveFile(path.c_str(), newName.c_str()))
|
|
|
|
ThrowError("Can't move '" + path +
|
2004-12-31 13:07:26 +08:00
|
|
|
"' to '" + newName.path + "': ");
|
2004-12-31 12:39:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Path::setStatusInfo(const StatusInfo& si) const {
|
|
|
|
if (!isFile()) return false;
|
|
|
|
|
|
|
|
HANDLE h = CreateFile(path.c_str(),
|
|
|
|
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
2004-12-31 13:07:26 +08:00
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
2004-12-31 12:39:07 +08:00
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BY_HANDLE_FILE_INFORMATION bhfi;
|
|
|
|
if (!GetFileInformationByHandle(h, &bhfi)) {
|
2005-01-01 03:01:08 +08:00
|
|
|
DWORD err = GetLastError();
|
2004-12-31 12:39:07 +08:00
|
|
|
CloseHandle(h);
|
2005-01-01 03:01:08 +08:00
|
|
|
SetLastError(err);
|
2004-12-31 13:07:26 +08:00
|
|
|
ThrowError(path + ": GetFileInformationByHandle: ");
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FILETIME ft;
|
|
|
|
(uint64_t&)ft = si.modTime.toWin32Time();
|
|
|
|
BOOL ret = SetFileTime(h, NULL, &ft, &ft);
|
2005-01-01 03:01:08 +08:00
|
|
|
DWORD err = GetLastError();
|
2004-12-31 12:39:07 +08:00
|
|
|
CloseHandle(h);
|
2005-01-01 03:01:08 +08:00
|
|
|
if (!ret) {
|
|
|
|
SetLastError(err);
|
2004-12-31 12:39:07 +08:00
|
|
|
ThrowError(path + ": SetFileTime: ");
|
2005-01-01 03:01:08 +08:00
|
|
|
}
|
2004-12-31 12:39:07 +08:00
|
|
|
|
|
|
|
// Best we can do with Unix permission bits is to interpret the owner
|
|
|
|
// writable bit.
|
|
|
|
if (si.mode & 0200) {
|
|
|
|
if (bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
|
|
|
|
if (!SetFileAttributes(path.c_str(),
|
2004-12-31 13:07:26 +08:00
|
|
|
bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
|
2004-12-31 12:39:07 +08:00
|
|
|
ThrowError(path + ": SetFileAttributes: ");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
if (!SetFileAttributes(path.c_str(),
|
2004-12-31 13:07:26 +08:00
|
|
|
bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
|
2004-12-31 12:39:07 +08:00
|
|
|
ThrowError(path + ": SetFileAttributes: ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-15 09:50:13 +08:00
|
|
|
void
|
2004-12-24 06:14:32 +08:00
|
|
|
sys::CopyFile(const sys::Path &Dest, const sys::Path &Src) {
|
2004-12-24 10:38:34 +08:00
|
|
|
// Can't use CopyFile macro defined in Windows.h because it would mess up the
|
|
|
|
// above line. We use the expansion it would have in a non-UNICODE build.
|
|
|
|
if (!::CopyFileA(Src.c_str(), Dest.c_str(), false))
|
2004-12-15 09:50:13 +08:00
|
|
|
ThrowError("Can't copy '" + Src.toString() +
|
2004-12-31 13:07:26 +08:00
|
|
|
"' to '" + Dest.toString() + "': ");
|
2004-12-15 09:50:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-12-24 10:38:34 +08:00
|
|
|
Path::makeUnique(bool reuse_current) {
|
2004-12-15 16:32:45 +08:00
|
|
|
if (reuse_current && !exists())
|
2004-12-15 09:50:13 +08:00
|
|
|
return; // File doesn't exist already, just use it!
|
|
|
|
|
|
|
|
Path dir (*this);
|
|
|
|
dir.elideFile();
|
|
|
|
std::string fname = this->getLast();
|
|
|
|
|
2004-12-15 12:08:15 +08:00
|
|
|
char newName[MAX_PATH + 1];
|
2004-12-15 09:50:13 +08:00
|
|
|
if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
|
2005-01-01 03:01:08 +08:00
|
|
|
ThrowError("Cannot make unique filename for '" + path + "': ");
|
2004-12-15 09:50:13 +08:00
|
|
|
|
|
|
|
path = newName;
|
|
|
|
}
|
|
|
|
|
2004-12-15 16:32:45 +08:00
|
|
|
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 );
|
2004-12-18 14:42:15 +08:00
|
|
|
return true;
|
2004-12-15 16:32:45 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
|
2004-09-15 13:49:50 +08:00
|
|
|
|