2010-11-30 03:44:50 +08:00
|
|
|
//===- llvm/Support/Win32/Path.cpp - Win32 Path Implementation ---*- C++ -*-===//
|
2004-09-15 13:49:50 +08:00
|
|
|
//
|
2004-09-11 12:59:30 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "Windows.h"
|
2004-09-19 03:29:16 +08:00
|
|
|
#include <malloc.h>
|
2009-04-01 10:03:38 +08:00
|
|
|
#include <cstdio>
|
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
|
2007-12-22 22:26:49 +08:00
|
|
|
#undef GetCurrentDirectory
|
2004-12-24 10:38:34 +08:00
|
|
|
|
2005-07-10 02:42:49 +08:00
|
|
|
// Windows happily accepts either forward or backward slashes, though any path
|
|
|
|
// returned by a Win32 API will have backward slashes. As LLVM code basically
|
|
|
|
// assumes forward slashes are used, backward slashs are converted where they
|
|
|
|
// can be introduced into a path.
|
|
|
|
//
|
|
|
|
// Another invariant is that a path ends with a slash if and only if the path
|
|
|
|
// is a root directory. Any other use of a trailing slash is stripped. Unlike
|
|
|
|
// in Unix, Windows has a rather complicated notion of a root path and this
|
|
|
|
// invariant helps simply the code.
|
|
|
|
|
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 {
|
2010-11-03 04:32:26 +08:00
|
|
|
|
2008-03-13 13:17:59 +08:00
|
|
|
const char PathSeparator = ';';
|
2008-02-27 14:17:10 +08:00
|
|
|
|
2010-11-03 04:32:26 +08:00
|
|
|
StringRef Path::GetEXESuffix() {
|
|
|
|
return "exe";
|
|
|
|
}
|
|
|
|
|
2009-12-19 03:59:48 +08:00
|
|
|
Path::Path(llvm::StringRef p)
|
2008-05-12 01:37:40 +08:00
|
|
|
: path(p) {
|
|
|
|
FlipBackSlashes(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Path::Path(const char *StrStart, unsigned StrLen)
|
|
|
|
: path(StrStart, StrLen) {
|
|
|
|
FlipBackSlashes(path);
|
|
|
|
}
|
|
|
|
|
2008-08-12 07:39:47 +08:00
|
|
|
Path&
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::operator=(StringRef that) {
|
|
|
|
path.assign(that.data(), that.size());
|
2008-08-12 07:39:47 +08:00
|
|
|
FlipBackSlashes(path);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
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
|
|
|
|
2011-10-24 11:27:19 +08:00
|
|
|
size_t len = path.size();
|
|
|
|
// If there is a null character, it and all its successors are ignored.
|
|
|
|
size_t pos = path.find_first_of('\0');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
len = pos;
|
|
|
|
|
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.
|
2011-10-24 11:27:19 +08:00
|
|
|
pos = path.rfind(':',len);
|
2005-07-08 12:50:08 +08:00
|
|
|
size_t rootslash = 0;
|
2004-09-29 08:01:17 +08:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
if (pos != 1 || !isalpha(path[0]) || len < 3)
|
|
|
|
return false;
|
2005-07-08 12:50:08 +08:00
|
|
|
rootslash = 2;
|
|
|
|
}
|
2005-07-08 13:02:13 +08:00
|
|
|
|
2005-07-08 12:50:08 +08:00
|
|
|
// Look for a UNC path, and if found adjust our notion of the root slash.
|
|
|
|
if (len > 3 && path[0] == '/' && path[1] == '/') {
|
|
|
|
rootslash = path.find('/', 2);
|
|
|
|
if (rootslash == std::string::npos)
|
|
|
|
rootslash = 0;
|
2004-09-29 08:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-08 13:02:13 +08:00
|
|
|
|
2005-07-08 12:50:08 +08:00
|
|
|
// Remove trailing slash, unless it's a root slash.
|
|
|
|
if (len > rootslash+1 && path[len-1] == '/')
|
|
|
|
path.erase(--len);
|
2004-09-29 08:01:17 +08:00
|
|
|
|
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] == ' ') {
|
2011-10-24 11:27:19 +08:00
|
|
|
if (pos+1 == len || path[pos+1] == '/' || path[pos+1] == '\0')
|
2005-01-14 12:09:39 +08:00
|
|
|
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] == '.') {
|
2011-10-24 11:27:19 +08:00
|
|
|
if (pos+1 == len || path[pos+1] == '/') {
|
2005-01-14 12:09:39 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2009-07-13 04:23:56 +08:00
|
|
|
void Path::makeAbsolute() {
|
2010-03-12 22:17:24 +08:00
|
|
|
TCHAR FullPath[MAX_PATH + 1] = {0};
|
2009-07-13 04:23:56 +08:00
|
|
|
LPTSTR FilePart = NULL;
|
|
|
|
|
|
|
|
DWORD RetLength = ::GetFullPathNameA(path.c_str(),
|
|
|
|
sizeof(FullPath)/sizeof(FullPath[0]),
|
|
|
|
FullPath, &FilePart);
|
|
|
|
|
|
|
|
if (0 == RetLength) {
|
|
|
|
// FIXME: Report the error GetLastError()
|
2009-07-27 05:16:42 +08:00
|
|
|
assert(0 && "Unable to make absolute path!");
|
2009-07-13 04:23:56 +08:00
|
|
|
} else if (RetLength > MAX_PATH) {
|
|
|
|
// FIXME: Report too small buffer (needed RetLength bytes).
|
2009-07-27 05:16:42 +08:00
|
|
|
assert(0 && "Unable to make absolute path!");
|
2009-07-13 04:23:56 +08:00
|
|
|
} else {
|
|
|
|
path = FullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 12:17:07 +08:00
|
|
|
bool
|
|
|
|
Path::isAbsolute(const char *NameStart, unsigned NameLen) {
|
|
|
|
assert(NameStart);
|
2009-07-13 04:23:56 +08:00
|
|
|
// FIXME: This does not handle correctly an absolute path starting from
|
|
|
|
// a drive letter or in UNC format.
|
2009-06-15 12:17:07 +08:00
|
|
|
switch (NameLen) {
|
|
|
|
case 0:
|
|
|
|
return false;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
return NameStart[0] == '/';
|
|
|
|
default:
|
2010-11-03 04:32:31 +08:00
|
|
|
return
|
|
|
|
(NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) ||
|
|
|
|
(NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\'));
|
2009-06-15 12:17:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 22:17:24 +08:00
|
|
|
bool
|
2007-03-30 00:43:20 +08:00
|
|
|
Path::isAbsolute() const {
|
2009-07-13 04:23:56 +08:00
|
|
|
// FIXME: This does not handle correctly an absolute path starting from
|
|
|
|
// a drive letter or in UNC format.
|
2007-03-30 01:27:38 +08:00
|
|
|
switch (path.length()) {
|
|
|
|
case 0:
|
|
|
|
return false;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
return path[0] == '/';
|
|
|
|
default:
|
|
|
|
return path[0] == '/' || (path[1] == ':' && path[2] == '/');
|
|
|
|
}
|
2010-03-12 22:17:24 +08:00
|
|
|
}
|
2007-03-30 00:43:20 +08:00
|
|
|
|
2010-03-12 22:17:24 +08:00
|
|
|
static Path *TempDirectory;
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
Path
|
2006-08-23 06:46:39 +08:00
|
|
|
Path::GetTemporaryDirectory(std::string* ErrMsg) {
|
2012-05-27 21:02:04 +08:00
|
|
|
if (TempDirectory) {
|
2012-06-09 21:59:29 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
// Visual Studio gets confused and emits a diagnostic about calling exists,
|
|
|
|
// even though this is the implementation for PathV1. Temporarily
|
|
|
|
// disable the deprecated warning message
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4996)
|
|
|
|
#endif
|
2012-05-27 21:02:04 +08:00
|
|
|
assert(TempDirectory->exists() && "Who has removed TempDirectory?");
|
2012-06-09 21:59:29 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2004-09-19 03:29:16 +08:00
|
|
|
return *TempDirectory;
|
2012-05-27 21:02:04 +08:00
|
|
|
}
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
char pathname[MAX_PATH];
|
2006-08-23 06:46:39 +08:00
|
|
|
if (!GetTempPath(MAX_PATH, pathname)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Can't determine temporary directory";
|
|
|
|
return Path();
|
|
|
|
}
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
Path result;
|
2005-07-08 07:35:23 +08:00
|
|
|
result.set(pathname);
|
2004-09-19 03:29:16 +08:00
|
|
|
|
2012-06-09 21:46:36 +08:00
|
|
|
// Append a subdirectory based on our process id so multiple LLVMs don't
|
2004-09-19 03:29:16 +08:00
|
|
|
// step on each other's toes.
|
2006-11-06 03:31:28 +08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
// Mingw's Win32 header files are broken.
|
2006-06-09 02:08:43 +08:00
|
|
|
sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
|
2006-11-06 03:31:28 +08:00
|
|
|
#else
|
|
|
|
sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
|
|
|
|
#endif
|
2005-07-08 10:48:42 +08:00
|
|
|
result.appendComponent(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.
|
2005-07-08 11:08:58 +08:00
|
|
|
result.eraseFromDisk(true);
|
2004-09-19 03:29:16 +08:00
|
|
|
|
|
|
|
// And finally (re-)create the empty directory.
|
2005-07-08 11:08:58 +08:00
|
|
|
result.createDirectoryOnDisk(false);
|
2004-09-19 03:29:16 +08:00
|
|
|
TempDirectory = new Path(result);
|
|
|
|
return *TempDirectory;
|
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() {
|
2010-11-09 23:10:45 +08:00
|
|
|
// This is the only notion that that Windows has of a root directory. Nothing
|
|
|
|
// is here except for drives.
|
|
|
|
return Path("file:///");
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2005-07-08 13:02:13 +08:00
|
|
|
void
|
2004-12-13 11:03:42 +08:00
|
|
|
Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
|
2010-11-09 23:11:19 +08:00
|
|
|
char buff[MAX_PATH];
|
|
|
|
// Generic form of C:\Windows\System32
|
|
|
|
HRESULT res = SHGetFolderPathA(NULL,
|
|
|
|
CSIDL_FLAG_CREATE | CSIDL_SYSTEM,
|
|
|
|
NULL,
|
|
|
|
SHGFP_TYPE_CURRENT,
|
|
|
|
buff);
|
|
|
|
if (res != S_OK) {
|
|
|
|
assert(0 && "Failed to get system directory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Paths.push_back(sys::Path(buff));
|
|
|
|
|
|
|
|
// Reset buff.
|
|
|
|
buff[0] = 0;
|
|
|
|
// Generic form of C:\Windows
|
|
|
|
res = SHGetFolderPathA(NULL,
|
|
|
|
CSIDL_FLAG_CREATE | CSIDL_WINDOWS,
|
|
|
|
NULL,
|
|
|
|
SHGFP_TYPE_CURRENT,
|
|
|
|
buff);
|
|
|
|
if (res != S_OK) {
|
|
|
|
assert(0 && "Failed to get windows directory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Paths.push_back(sys::Path(buff));
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-13 11:03:42 +08:00
|
|
|
void
|
2007-07-07 04:28:40 +08:00
|
|
|
Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
|
2004-12-13 11:03:42 +08:00
|
|
|
char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
|
|
|
|
if (env_var != 0) {
|
|
|
|
getPathList(env_var,Paths);
|
|
|
|
}
|
|
|
|
#ifdef LLVM_LIBDIR
|
|
|
|
{
|
|
|
|
Path tmpPath;
|
2005-07-08 07:35:23 +08:00
|
|
|
if (tmpPath.set(LLVM_LIBDIR))
|
2005-07-08 02:21:42 +08:00
|
|
|
if (tmpPath.canRead())
|
2004-12-13 11:03:42 +08:00
|
|
|
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::GetUserHomeDirectory() {
|
2010-11-09 23:11:31 +08:00
|
|
|
char buff[MAX_PATH];
|
2010-11-10 23:06:00 +08:00
|
|
|
HRESULT res = SHGetFolderPathA(NULL,
|
|
|
|
CSIDL_FLAG_CREATE | CSIDL_APPDATA,
|
|
|
|
NULL,
|
|
|
|
SHGFP_TYPE_CURRENT,
|
|
|
|
buff);
|
2010-11-09 23:11:31 +08:00
|
|
|
if (res != S_OK)
|
|
|
|
assert(0 && "Failed to get user home directory");
|
|
|
|
return Path(buff);
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2007-12-19 06:07:33 +08:00
|
|
|
|
|
|
|
Path
|
|
|
|
Path::GetCurrentDirectory() {
|
|
|
|
char pathname[MAX_PATH];
|
2007-12-22 22:26:49 +08:00
|
|
|
::GetCurrentDirectoryA(MAX_PATH,pathname);
|
2010-03-12 22:17:24 +08:00
|
|
|
return Path(pathname);
|
2007-12-19 06:07:33 +08:00
|
|
|
}
|
|
|
|
|
2008-03-03 10:55:43 +08:00
|
|
|
/// GetMainExecutable - Return the path to the main executable, given the
|
|
|
|
/// value of argv[0] from program startup.
|
|
|
|
Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
|
2009-06-15 13:38:04 +08:00
|
|
|
char pathname[MAX_PATH];
|
|
|
|
DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
|
|
|
|
return ret != MAX_PATH ? Path(pathname) : Path();
|
2008-03-03 10:55:43 +08:00
|
|
|
}
|
|
|
|
|
2007-12-19 06:07:33 +08:00
|
|
|
|
2004-09-19 03:29:16 +08:00
|
|
|
// FIXME: the above set of functions don't map to Windows very well.
|
|
|
|
|
2005-07-10 02:42:49 +08:00
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef Path::getDirname() const {
|
|
|
|
return getDirnameCharSep(path, "/");
|
2008-04-08 06:01:32 +08:00
|
|
|
}
|
2008-04-08 05:53:57 +08:00
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef
|
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++;
|
|
|
|
|
2005-07-10 02:42:49 +08:00
|
|
|
size_t dot = path.rfind('.');
|
|
|
|
if (dot == std::string::npos || dot < slash)
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(slash);
|
2005-07-10 02:42:49 +08:00
|
|
|
else
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(slash, dot - slash);
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef
|
2008-06-15 23:15:19 +08:00
|
|
|
Path::getSuffix() const {
|
|
|
|
// Find the last slash
|
|
|
|
size_t slash = path.rfind('/');
|
|
|
|
if (slash == std::string::npos)
|
|
|
|
slash = 0;
|
|
|
|
else
|
|
|
|
slash++;
|
|
|
|
|
|
|
|
size_t dot = path.rfind('.');
|
|
|
|
if (dot == std::string::npos || dot < slash)
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef("");
|
2008-06-15 23:15:19 +08:00
|
|
|
else
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(dot + 1);
|
2008-06-15 23:15:19 +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
|
|
|
}
|
|
|
|
|
2007-12-19 03:46:22 +08:00
|
|
|
bool
|
|
|
|
Path::isDirectory() const {
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return (attr != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
(attr & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
}
|
|
|
|
|
2010-11-07 12:36:50 +08:00
|
|
|
bool
|
|
|
|
Path::isSymLink() const {
|
2010-11-10 23:05:39 +08:00
|
|
|
DWORD attributes = GetFileAttributes(path.c_str());
|
|
|
|
|
|
|
|
if (attributes == INVALID_FILE_ATTRIBUTES)
|
|
|
|
// There's no sane way to report this :(.
|
|
|
|
assert(0 && "GetFileAttributes returned INVALID_FILE_ATTRIBUTES");
|
|
|
|
|
|
|
|
// This isn't exactly what defines a NTFS symlink, but it is only true for
|
|
|
|
// paths that act like a symlink.
|
|
|
|
return attributes & FILE_ATTRIBUTE_REPARSE_POINT;
|
2010-11-07 12:36:50 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
2005-07-08 02:21:42 +08:00
|
|
|
Path::canRead() 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
|
2005-07-08 02:21:42 +08:00
|
|
|
Path::canWrite() 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
|
2005-07-08 02:21:42 +08:00
|
|
|
Path::canExecute() 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
|
|
|
|
2009-11-24 23:19:10 +08:00
|
|
|
bool
|
2009-11-25 14:32:19 +08:00
|
|
|
Path::isRegularFile() const {
|
2011-01-11 09:21:55 +08:00
|
|
|
bool res;
|
|
|
|
if (fs::is_regular_file(path, res))
|
2009-11-25 14:32:19 +08:00
|
|
|
return false;
|
2011-01-11 09:21:55 +08:00
|
|
|
return res;
|
2009-11-24 23:19:10 +08:00
|
|
|
}
|
|
|
|
|
2009-12-18 05:02:39 +08:00
|
|
|
StringRef
|
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;
|
|
|
|
|
2005-07-08 12:50:08 +08:00
|
|
|
// If the last character is a slash, we have a root directory
|
|
|
|
if (pos == path.length()-1)
|
|
|
|
return path;
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
// Return everything after the last slash
|
2009-12-18 05:02:39 +08:00
|
|
|
return StringRef(path).substr(pos+1);
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2007-03-30 03:05:44 +08:00
|
|
|
const FileStatus *
|
2007-04-08 02:52:17 +08:00
|
|
|
PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
|
|
|
|
if (!fsIsValid || update) {
|
2007-03-30 00:43:20 +08:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
2007-03-30 03:05:44 +08:00
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
|
|
|
|
MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
|
2007-03-30 00:43:20 +08:00
|
|
|
": Can't get status: ");
|
2007-03-30 03:05:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2004-12-14 13:26:43 +08:00
|
|
|
|
2007-04-08 02:52:17 +08:00
|
|
|
status.fileSize = fi.nFileSizeHigh;
|
|
|
|
status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
|
|
|
|
status.fileSize += fi.nFileSizeLow;
|
2004-12-14 13:26:43 +08:00
|
|
|
|
2007-04-08 02:52:17 +08:00
|
|
|
status.mode = fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0555 : 0777;
|
|
|
|
status.user = 9999; // Not applicable to Windows, so...
|
|
|
|
status.group = 9999; // Not applicable to Windows, so...
|
2004-12-14 13:26:43 +08:00
|
|
|
|
2007-03-30 01:00:31 +08:00
|
|
|
// FIXME: this is only unique if the file is accessed by the same file path.
|
|
|
|
// How do we do this for C:\dir\file and ..\dir\file ? Unix has inode
|
|
|
|
// numbers, but the concept doesn't exist in Windows.
|
2007-04-08 02:52:17 +08:00
|
|
|
status.uniqueID = 0;
|
2007-03-30 01:00:31 +08:00
|
|
|
for (unsigned i = 0; i < path.length(); ++i)
|
2007-04-08 02:52:17 +08:00
|
|
|
status.uniqueID += path[i];
|
2007-03-30 01:00:31 +08:00
|
|
|
|
2010-08-29 00:39:32 +08:00
|
|
|
ULARGE_INTEGER ui;
|
|
|
|
ui.LowPart = fi.ftLastWriteTime.dwLowDateTime;
|
|
|
|
ui.HighPart = fi.ftLastWriteTime.dwHighDateTime;
|
|
|
|
status.modTime.fromWin32Time(ui.QuadPart);
|
2007-03-30 00:43:20 +08:00
|
|
|
|
2007-04-08 02:52:17 +08:00
|
|
|
status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
fsIsValid = true;
|
2007-03-30 00:43:20 +08:00
|
|
|
}
|
2007-04-08 02:52:17 +08:00
|
|
|
return &status;
|
2004-12-14 13:26:43 +08:00
|
|
|
}
|
|
|
|
|
2006-08-23 07:54:35 +08:00
|
|
|
bool Path::makeReadableOnDisk(std::string* ErrMsg) {
|
2004-12-14 13:26:43 +08:00
|
|
|
// All files are readable on Windows (ignoring security attributes).
|
2006-08-23 07:54:35 +08:00
|
|
|
return false;
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2006-09-02 04:35:17 +08:00
|
|
|
bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
|
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)
|
2006-09-02 04:35:17 +08:00
|
|
|
return false;
|
2004-12-14 13:26:43 +08:00
|
|
|
|
|
|
|
if (attr & FILE_ATTRIBUTE_READONLY) {
|
2006-08-23 07:54:35 +08:00
|
|
|
if (!SetFileAttributes(path.c_str(), attr & ~FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
MakeErrMsg(ErrMsg, std::string(path) + ": Can't make file writable: ");
|
|
|
|
return true;
|
|
|
|
}
|
2004-12-14 13:26:43 +08:00
|
|
|
}
|
2006-08-23 07:54:35 +08:00
|
|
|
return false;
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2006-08-23 07:54:35 +08:00
|
|
|
bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
|
2004-12-14 13:26:43 +08:00
|
|
|
// All files are executable on Windows (ignoring security attributes).
|
2006-08-23 07:54:35 +08:00
|
|
|
return false;
|
2004-12-14 03:59:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
bool
|
2006-08-23 14:56:27 +08:00
|
|
|
Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
|
2007-04-08 04:47:27 +08:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
|
|
|
|
MakeErrMsg(ErrMsg, path + ": can't get status of file");
|
2006-09-02 04:35:17 +08:00
|
|
|
return true;
|
2007-04-08 04:47:27 +08:00
|
|
|
}
|
2010-03-12 22:17:24 +08:00
|
|
|
|
2007-04-08 04:47:27 +08:00
|
|
|
if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = path + ": not a directory";
|
2006-08-23 14:56:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-31 12:39:07 +08:00
|
|
|
|
|
|
|
result.clear();
|
|
|
|
WIN32_FIND_DATA fd;
|
2005-07-08 12:50:08 +08:00
|
|
|
std::string searchpath = path;
|
|
|
|
if (path.size() == 0 || searchpath[path.size()-1] == '/')
|
2005-07-08 13:02:13 +08:00
|
|
|
searchpath += "*";
|
2005-07-08 12:50:08 +08:00
|
|
|
else
|
|
|
|
searchpath += "/*";
|
2005-07-08 13:02:13 +08:00
|
|
|
|
2005-01-27 11:49:03 +08:00
|
|
|
HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
|
2004-12-31 12:39:07 +08:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2005-01-27 11:49:03 +08:00
|
|
|
if (GetLastError() == ERROR_FILE_NOT_FOUND)
|
2004-12-31 12:39:07 +08:00
|
|
|
return true; // not really an error, now is it?
|
2006-08-23 14:56:27 +08:00
|
|
|
MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
|
|
|
|
return true;
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2004-12-31 13:07:26 +08:00
|
|
|
if (fd.cFileName[0] == '.')
|
|
|
|
continue;
|
2005-07-08 12:50:08 +08:00
|
|
|
Path aPath(path);
|
|
|
|
aPath.appendComponent(&fd.cFileName[0]);
|
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);
|
2006-08-23 14:56:27 +08:00
|
|
|
MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
|
|
|
|
return true;
|
2005-01-01 03:01:08 +08:00
|
|
|
}
|
2006-08-23 14:56:27 +08:00
|
|
|
return false;
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::set(StringRef a_path) {
|
2008-01-29 21:02:09 +08:00
|
|
|
if (a_path.empty())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
2005-07-08 07:35:23 +08:00
|
|
|
std::string save(path);
|
2004-09-15 13:49:50 +08:00
|
|
|
path = a_path;
|
2004-09-29 08:01:17 +08:00
|
|
|
FlipBackSlashes(path);
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2005-07-08 07:35:23 +08:00
|
|
|
path = save;
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
2009-12-18 05:02:39 +08:00
|
|
|
Path::appendComponent(StringRef name) {
|
2005-07-08 07:35:23 +08:00
|
|
|
if (name.empty())
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
2005-07-08 07:35:23 +08:00
|
|
|
std::string save(path);
|
|
|
|
if (!path.empty()) {
|
|
|
|
size_t last = path.size() - 1;
|
2005-07-08 13:02:13 +08:00
|
|
|
if (path[last] != '/')
|
2005-07-08 07:35:23 +08:00
|
|
|
path += '/';
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2005-07-08 07:35:23 +08:00
|
|
|
path += name;
|
2004-11-06 06:15:36 +08:00
|
|
|
if (!isValid()) {
|
2005-07-08 07:35:23 +08:00
|
|
|
path = save;
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-08 07:35:23 +08:00
|
|
|
Path::eraseComponent() {
|
2004-09-15 13:49:50 +08:00
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 12:50:08 +08:00
|
|
|
if (slashpos == path.size() - 1 || slashpos == std::string::npos)
|
2004-09-15 13:49:50 +08:00
|
|
|
return false;
|
2005-07-10 02:42:49 +08:00
|
|
|
std::string save(path);
|
2004-09-15 13:49:50 +08:00
|
|
|
path.erase(slashpos);
|
2005-07-10 02:42:49 +08:00
|
|
|
if (!isValid()) {
|
|
|
|
path = save;
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-08 07:35:23 +08:00
|
|
|
Path::eraseSuffix() {
|
2004-09-15 13:49:50 +08:00
|
|
|
size_t dotpos = path.rfind('.',path.size());
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 12:50:08 +08:00
|
|
|
if (dotpos != std::string::npos) {
|
2005-07-10 02:42:49 +08:00
|
|
|
if (slashpos == std::string::npos || dotpos > slashpos+1) {
|
|
|
|
std::string save(path);
|
2005-07-08 12:50:08 +08:00
|
|
|
path.erase(dotpos, path.size()-dotpos);
|
2005-07-10 02:42:49 +08:00
|
|
|
if (!isValid()) {
|
|
|
|
path = save;
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-08 13:02:13 +08:00
|
|
|
return true;
|
2005-07-08 12:50:08 +08:00
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-08-25 02:58:37 +08:00
|
|
|
inline bool PathMsg(std::string* ErrMsg, const char* pathname, const char*msg) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = std::string(pathname) + ": " + std::string(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
bool
|
2006-08-25 02:58:37 +08:00
|
|
|
Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
|
2004-09-15 13:49:50 +08:00
|
|
|
// Get a writeable copy of the path name
|
2005-07-08 12:50:08 +08:00
|
|
|
size_t len = path.length();
|
|
|
|
char *pathname = reinterpret_cast<char *>(_alloca(len+2));
|
|
|
|
path.copy(pathname, len);
|
|
|
|
pathname[len] = 0;
|
2005-07-08 13:02:13 +08:00
|
|
|
|
2005-07-08 12:50:08 +08:00
|
|
|
// Make sure it ends with a slash.
|
|
|
|
if (len == 0 || pathname[len - 1] != '/') {
|
|
|
|
pathname[len] = '/';
|
|
|
|
pathname[++len] = 0;
|
|
|
|
}
|
2004-09-29 08:01:17 +08:00
|
|
|
|
|
|
|
// Determine starting point for initial / search.
|
|
|
|
char *next = pathname;
|
|
|
|
if (pathname[0] == '/' && pathname[1] == '/') {
|
|
|
|
// Skip host name.
|
|
|
|
next = strchr(pathname+2, '/');
|
|
|
|
if (next == NULL)
|
2006-08-25 02:58:37 +08:00
|
|
|
return PathMsg(ErrMsg, pathname, "badly formed remote directory");
|
|
|
|
|
2004-09-29 08:01:17 +08:00
|
|
|
// Skip share name.
|
|
|
|
next = strchr(next+1, '/');
|
|
|
|
if (next == NULL)
|
2006-08-25 02:58:37 +08:00
|
|
|
return PathMsg(ErrMsg, pathname,"badly formed remote directory");
|
|
|
|
|
2004-09-29 08:01:17 +08:00
|
|
|
next++;
|
|
|
|
if (*next == 0)
|
2006-08-25 02:58:37 +08:00
|
|
|
return PathMsg(ErrMsg, pathname, "badly formed remote directory");
|
|
|
|
|
2004-09-29 08:01:17 +08:00
|
|
|
} 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;
|
2009-11-05 22:32:40 +08:00
|
|
|
if (!CreateDirectory(pathname, NULL) &&
|
|
|
|
GetLastError() != ERROR_ALREADY_EXISTS)
|
2010-03-12 22:17:24 +08:00
|
|
|
return MakeErrMsg(ErrMsg,
|
2006-08-25 02:58:37 +08:00
|
|
|
std::string(pathname) + ": Can't create directory: ");
|
2004-09-29 08:01:17 +08:00
|
|
|
*next++ = '/';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Drop trailing slash.
|
2005-07-08 12:50:08 +08:00
|
|
|
pathname[len-1] = 0;
|
2009-11-05 22:32:40 +08:00
|
|
|
if (!CreateDirectory(pathname, NULL) &&
|
|
|
|
GetLastError() != ERROR_ALREADY_EXISTS) {
|
2010-11-03 04:32:31 +08:00
|
|
|
return MakeErrMsg(ErrMsg, std::string(pathname) +
|
|
|
|
": Can't create directory: ");
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
}
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-08-25 02:58:37 +08:00
|
|
|
Path::createFileOnDisk(std::string* ErrMsg) {
|
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)
|
2006-08-25 02:58:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2004-09-19 03:29:16 +08:00
|
|
|
CloseHandle(h);
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-07-29 06:29:50 +08:00
|
|
|
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
|
2007-04-08 04:47:27 +08:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
|
|
|
|
return true;
|
2010-03-12 22:17:24 +08:00
|
|
|
|
2007-04-08 04:47:27 +08:00
|
|
|
if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
2005-07-08 07:35:23 +08:00
|
|
|
// If it doesn't exist, we're done.
|
2011-01-10 10:34:23 +08:00
|
|
|
bool Exists;
|
|
|
|
if (fs::exists(path, Exists) || !Exists)
|
2006-07-29 06:29:50 +08:00
|
|
|
return false;
|
2005-07-08 07:35:23 +08:00
|
|
|
|
2005-07-08 12:50:08 +08:00
|
|
|
char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
|
2005-07-08 07:35:23 +08:00
|
|
|
int lastchar = path.length() - 1 ;
|
2005-07-08 12:50:08 +08:00
|
|
|
path.copy(pathname, lastchar+1);
|
2005-07-08 07:35:23 +08:00
|
|
|
|
|
|
|
// Make path end with '/*'.
|
2005-07-08 12:50:08 +08:00
|
|
|
if (pathname[lastchar] != '/')
|
|
|
|
pathname[++lastchar] = '/';
|
2005-07-08 07:35:23 +08:00
|
|
|
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<Path> list;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (strcmp(fd.cFileName, ".") == 0)
|
|
|
|
continue;
|
|
|
|
if (strcmp(fd.cFileName, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
2005-07-08 13:02:13 +08:00
|
|
|
Path aPath(path);
|
|
|
|
aPath.appendComponent(&fd.cFileName[0]);
|
2005-07-08 07:35:23 +08:00
|
|
|
list.push_back(aPath);
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
FindClose(h);
|
|
|
|
if (err != ERROR_NO_MORE_FILES) {
|
|
|
|
SetLastError(err);
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
|
2005-07-08 07:35:23 +08:00
|
|
|
}
|
2005-01-01 03:01:08 +08:00
|
|
|
|
2005-07-08 13:02:13 +08:00
|
|
|
for (std::vector<Path>::iterator I = list.begin(); I != list.end();
|
2005-07-08 07:35:23 +08:00
|
|
|
++I) {
|
|
|
|
Path &aPath = *I;
|
2005-07-08 11:08:58 +08:00
|
|
|
aPath.eraseFromDisk(true);
|
2005-07-08 07:35:23 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (GetLastError() != ERROR_FILE_NOT_FOUND)
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
|
2005-01-01 03:01:08 +08:00
|
|
|
}
|
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2005-07-08 07:35:23 +08:00
|
|
|
pathname[lastchar] = 0;
|
|
|
|
if (!RemoveDirectory(pathname))
|
2010-03-12 22:17:24 +08:00
|
|
|
return MakeErrMsg(ErrStr,
|
2006-08-26 05:37:17 +08:00
|
|
|
std::string(pathname) + ": Can't destroy directory: ");
|
2006-07-29 06:29:50 +08:00
|
|
|
return false;
|
2007-04-08 04:47:27 +08:00
|
|
|
} else {
|
|
|
|
// Read-only files cannot be deleted on Windows. Must remove the read-only
|
|
|
|
// attribute first.
|
|
|
|
if (fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
|
|
|
|
if (!SetFileAttributes(path.c_str(),
|
|
|
|
fi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
|
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DeleteFile(path.c_str()))
|
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2004-12-15 02:42:13 +08:00
|
|
|
bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
|
|
|
|
assert(len < 1024 && "Request for magic string too long");
|
2010-08-31 14:36:33 +08:00
|
|
|
char* buf = reinterpret_cast<char*>(alloca(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
|
|
|
|
2010-08-31 14:36:33 +08:00
|
|
|
Magic = std::string(buf, len);
|
2004-12-15 02:42:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
bool
|
2006-08-23 15:30:48 +08:00
|
|
|
Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
|
2010-09-30 08:44:58 +08:00
|
|
|
if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
|
|
|
|
return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
|
|
|
|
+ "': ");
|
|
|
|
return false;
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-09-02 04:35:17 +08:00
|
|
|
Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
|
2005-07-10 02:42:49 +08:00
|
|
|
// FIXME: should work on directories also.
|
2006-09-02 04:35:17 +08:00
|
|
|
if (!si.isFile) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-12 22:17:24 +08:00
|
|
|
|
2004-12-31 12:39:07 +08:00
|
|
|
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)
|
2006-07-29 06:36:17 +08:00
|
|
|
return true;
|
2004-12-31 12:39:07 +08:00
|
|
|
|
|
|
|
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);
|
2006-09-02 04:35:17 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
2010-08-29 00:39:32 +08:00
|
|
|
ULARGE_INTEGER ui;
|
|
|
|
ui.QuadPart = si.modTime.toWin32Time();
|
2004-12-31 12:39:07 +08:00
|
|
|
FILETIME ft;
|
2010-08-29 00:39:32 +08:00
|
|
|
ft.dwLowDateTime = ui.LowPart;
|
|
|
|
ft.dwHighDateTime = ui.HighPart;
|
2004-12-31 12:39:07 +08:00
|
|
|
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);
|
2006-09-02 04:35:17 +08:00
|
|
|
return MakeErrMsg(ErrMsg, 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))
|
2006-09-02 04:35:17 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
} 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))
|
2006-09-02 04:35:17 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-29 06:36:17 +08:00
|
|
|
return false;
|
2004-12-31 12:39:07 +08:00
|
|
|
}
|
|
|
|
|
2006-08-25 02:58:37 +08:00
|
|
|
bool
|
|
|
|
CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
|
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))
|
2009-08-24 06:45:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() +
|
|
|
|
"' to '" + Dest.str() + "': ");
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2004-12-15 09:50:13 +08:00
|
|
|
}
|
|
|
|
|
2006-08-25 02:58:37 +08:00
|
|
|
bool
|
|
|
|
Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
|
2011-01-10 10:34:23 +08:00
|
|
|
bool Exists;
|
|
|
|
if (reuse_current && (fs::exists(path, Exists) || !Exists))
|
2006-08-25 02:58:37 +08:00
|
|
|
return false; // File doesn't exist already, just use it!
|
2004-12-15 09:50:13 +08:00
|
|
|
|
2005-01-27 11:49:03 +08:00
|
|
|
// Reserve space for -XXXXXX at the end.
|
|
|
|
char *FNBuffer = (char*) alloca(path.size()+8);
|
|
|
|
unsigned offset = path.size();
|
|
|
|
path.copy(FNBuffer, offset);
|
2004-12-15 09:50:13 +08:00
|
|
|
|
2005-07-10 02:42:49 +08:00
|
|
|
// Find a numeric suffix that isn't used by an existing file. Assume there
|
|
|
|
// won't be more than 1 million files with the same prefix. Probably a safe
|
|
|
|
// bet.
|
2011-03-16 10:53:24 +08:00
|
|
|
static int FCounter = -1;
|
|
|
|
if (FCounter < 0) {
|
|
|
|
// Give arbitrary initial seed.
|
|
|
|
// FIXME: We should use sys::fs::unique_file() in future.
|
|
|
|
LARGE_INTEGER cnt64;
|
|
|
|
DWORD x = GetCurrentProcessId();
|
|
|
|
x = (x << 16) | (x >> 16);
|
|
|
|
if (QueryPerformanceCounter(&cnt64)) // RDTSC
|
|
|
|
x ^= cnt64.HighPart ^ cnt64.LowPart;
|
|
|
|
FCounter = x % 1000000;
|
|
|
|
}
|
2005-01-27 11:49:03 +08:00
|
|
|
do {
|
|
|
|
sprintf(FNBuffer+offset, "-%06u", FCounter);
|
|
|
|
if (++FCounter > 999999)
|
|
|
|
FCounter = 0;
|
|
|
|
path = FNBuffer;
|
2011-01-10 10:34:23 +08:00
|
|
|
} while (!fs::exists(path, Exists) && Exists);
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2004-12-15 09:50:13 +08:00
|
|
|
}
|
|
|
|
|
2004-12-15 16:32:45 +08:00
|
|
|
bool
|
2006-08-25 02:58:37 +08:00
|
|
|
Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
|
2004-12-15 16:32:45 +08:00
|
|
|
// Make this into a unique file name
|
2006-09-02 04:35:17 +08:00
|
|
|
makeUnique(reuse_current, ErrMsg);
|
2005-01-27 11:49:03 +08:00
|
|
|
|
|
|
|
// Now go and create it
|
|
|
|
HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2006-09-02 04:35:17 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't create file");
|
2005-01-27 11:49:03 +08:00
|
|
|
|
|
|
|
CloseHandle(h);
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2004-12-15 16:32:45 +08:00
|
|
|
}
|
|
|
|
|
2008-04-01 14:00:12 +08:00
|
|
|
/// MapInFilePages - Not yet implemented on win32.
|
2011-03-11 02:30:48 +08:00
|
|
|
const char *Path::MapInFilePages(int FD, size_t FileSize, off_t Offset) {
|
2008-04-01 14:00:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MapInFilePages - Not yet implemented on win32.
|
2011-03-11 02:30:48 +08:00
|
|
|
void Path::UnMapFilePages(const char *Base, size_t FileSize) {
|
2008-04-01 14:00:12 +08:00
|
|
|
assert(0 && "NOT IMPLEMENTED");
|
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|