2010-11-29 19:44:50 +00:00
|
|
|
//===- llvm/Support/Win32/Path.cpp - Win32 Path Implementation ---*- C++ -*-===//
|
2004-09-15 05:49:50 +00:00
|
|
|
//
|
2004-09-11 04:59:30 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-15 05:49:50 +00:00
|
|
|
//
|
2004-09-11 04:59:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of the Path class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-15 05:49:50 +00:00
|
|
|
//=== WARNING: Implementation here must contain only generic Win32 code that
|
|
|
|
//=== is guaranteed to work on *all* Win32 variants.
|
2004-09-11 04:59:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "Windows.h"
|
2004-09-18 19:29:16 +00:00
|
|
|
#include <malloc.h>
|
2009-04-01 02:03:38 +00:00
|
|
|
#include <cstdio>
|
2004-09-11 04:59:30 +00:00
|
|
|
|
2004-12-24 02:38:34 +00:00
|
|
|
// We need to undo a macro defined in Windows.h, otherwise we won't compile:
|
|
|
|
#undef CopyFile
|
2007-12-22 14:26:49 +00:00
|
|
|
#undef GetCurrentDirectory
|
2004-12-24 02:38:34 +00:00
|
|
|
|
2005-07-09 18:42:49 +00: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 00:01:17 +00:00
|
|
|
static void FlipBackSlashes(std::string& s) {
|
|
|
|
for (size_t i = 0; i < s.size(); i++)
|
|
|
|
if (s[i] == '\\')
|
|
|
|
s[i] = '/';
|
|
|
|
}
|
|
|
|
|
2004-09-11 04:59:30 +00:00
|
|
|
namespace llvm {
|
2004-09-15 05:49:50 +00:00
|
|
|
namespace sys {
|
2010-11-02 20:32:26 +00:00
|
|
|
|
2008-03-13 05:17:59 +00:00
|
|
|
const char PathSeparator = ';';
|
2008-02-27 06:17:10 +00:00
|
|
|
|
2010-11-02 20:32:26 +00:00
|
|
|
StringRef Path::GetEXESuffix() {
|
|
|
|
return "exe";
|
|
|
|
}
|
|
|
|
|
2009-12-18 19:59:48 +00:00
|
|
|
Path::Path(llvm::StringRef p)
|
2008-05-11 17:37:40 +00:00
|
|
|
: path(p) {
|
|
|
|
FlipBackSlashes(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Path::Path(const char *StrStart, unsigned StrLen)
|
|
|
|
: path(StrStart, StrLen) {
|
|
|
|
FlipBackSlashes(path);
|
|
|
|
}
|
|
|
|
|
2008-08-11 23:39:47 +00:00
|
|
|
Path&
|
2009-12-17 21:02:39 +00:00
|
|
|
Path::operator=(StringRef that) {
|
|
|
|
path.assign(that.data(), that.size());
|
2008-08-11 23:39:47 +00:00
|
|
|
FlipBackSlashes(path);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-10-20 15:23:58 +00:00
|
|
|
// push_back 0 on create, and pop_back on delete.
|
|
|
|
struct ScopedNullTerminator {
|
|
|
|
std::string &str;
|
|
|
|
ScopedNullTerminator(std::string &s) : str(s) { str.push_back(0); }
|
2010-10-20 16:00:45 +00:00
|
|
|
~ScopedNullTerminator() {
|
|
|
|
// str.pop_back(); But wait, C++03 doesn't have this...
|
|
|
|
assert(!str.empty() && str[str.size() - 1] == 0
|
|
|
|
&& "Null char not present!");
|
|
|
|
str.resize(str.size() - 1);
|
|
|
|
}
|
2010-10-20 15:23:58 +00:00
|
|
|
};
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
bool
|
2004-11-05 22:15:36 +00:00
|
|
|
Path::isValid() const {
|
2004-09-15 05:49:50 +00:00
|
|
|
if (path.empty())
|
|
|
|
return false;
|
2004-09-18 19:29:16 +00:00
|
|
|
|
2004-09-29 00:01:17 +00: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();
|
2010-10-20 15:23:58 +00:00
|
|
|
// This code assumes that path is null terminated, so make sure it is.
|
|
|
|
ScopedNullTerminator snt(path);
|
2004-09-29 00:01:17 +00:00
|
|
|
size_t pos = path.rfind(':',len);
|
2005-07-08 04:50:08 +00:00
|
|
|
size_t rootslash = 0;
|
2004-09-29 00:01:17 +00:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
if (pos != 1 || !isalpha(path[0]) || len < 3)
|
|
|
|
return false;
|
2005-07-08 04:50:08 +00:00
|
|
|
rootslash = 2;
|
|
|
|
}
|
2005-07-08 05:02:13 +00:00
|
|
|
|
2005-07-08 04:50:08 +00: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 00:01:17 +00: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 05:02:13 +00:00
|
|
|
|
2005-07-08 04:50:08 +00:00
|
|
|
// Remove trailing slash, unless it's a root slash.
|
|
|
|
if (len > rootslash+1 && path[len-1] == '/')
|
|
|
|
path.erase(--len);
|
2004-09-29 00:01:17 +00:00
|
|
|
|
2005-01-14 04:09:39 +00: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 00:01:17 +00:00
|
|
|
|
2005-01-14 04:09:39 +00: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-18 19:29:16 +00:00
|
|
|
|
2004-09-29 00:01:17 +00:00
|
|
|
return true;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2009-07-12 20:23:56 +00:00
|
|
|
void Path::makeAbsolute() {
|
2010-03-12 14:17:24 +00:00
|
|
|
TCHAR FullPath[MAX_PATH + 1] = {0};
|
2009-07-12 20:23:56 +00: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-26 21:16:42 +00:00
|
|
|
assert(0 && "Unable to make absolute path!");
|
2009-07-12 20:23:56 +00:00
|
|
|
} else if (RetLength > MAX_PATH) {
|
|
|
|
// FIXME: Report too small buffer (needed RetLength bytes).
|
2009-07-26 21:16:42 +00:00
|
|
|
assert(0 && "Unable to make absolute path!");
|
2009-07-12 20:23:56 +00:00
|
|
|
} else {
|
|
|
|
path = FullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 04:17:07 +00:00
|
|
|
bool
|
|
|
|
Path::isAbsolute(const char *NameStart, unsigned NameLen) {
|
|
|
|
assert(NameStart);
|
2009-07-12 20:23:56 +00:00
|
|
|
// FIXME: This does not handle correctly an absolute path starting from
|
|
|
|
// a drive letter or in UNC format.
|
2009-06-15 04:17:07 +00:00
|
|
|
switch (NameLen) {
|
|
|
|
case 0:
|
|
|
|
return false;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
return NameStart[0] == '/';
|
|
|
|
default:
|
2010-11-02 20:32:31 +00:00
|
|
|
return
|
|
|
|
(NameStart[0] == '/' || (NameStart[1] == ':' && NameStart[2] == '/')) ||
|
|
|
|
(NameStart[0] == '\\' || (NameStart[1] == ':' && NameStart[2] == '\\'));
|
2009-06-15 04:17:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-12 14:17:24 +00:00
|
|
|
bool
|
2007-03-29 16:43:20 +00:00
|
|
|
Path::isAbsolute() const {
|
2009-07-12 20:23:56 +00:00
|
|
|
// FIXME: This does not handle correctly an absolute path starting from
|
|
|
|
// a drive letter or in UNC format.
|
2007-03-29 17:27:38 +00: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 14:17:24 +00:00
|
|
|
}
|
2007-03-29 16:43:20 +00:00
|
|
|
|
2010-03-12 14:17:24 +00:00
|
|
|
static Path *TempDirectory;
|
2004-09-18 19:29:16 +00:00
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
Path
|
2006-08-22 22:46:39 +00:00
|
|
|
Path::GetTemporaryDirectory(std::string* ErrMsg) {
|
2004-09-18 19:29:16 +00:00
|
|
|
if (TempDirectory)
|
|
|
|
return *TempDirectory;
|
|
|
|
|
|
|
|
char pathname[MAX_PATH];
|
2006-08-22 22:46:39 +00:00
|
|
|
if (!GetTempPath(MAX_PATH, pathname)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Can't determine temporary directory";
|
|
|
|
return Path();
|
|
|
|
}
|
2004-09-18 19:29:16 +00:00
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
Path result;
|
2005-07-07 23:35:23 +00:00
|
|
|
result.set(pathname);
|
2004-09-18 19:29:16 +00:00
|
|
|
|
|
|
|
// Append a subdirectory passed on our process id so multiple LLVMs don't
|
|
|
|
// step on each other's toes.
|
2006-11-05 19:31:28 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
// Mingw's Win32 header files are broken.
|
2006-06-08 18:08:43 +00:00
|
|
|
sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
|
2006-11-05 19:31:28 +00:00
|
|
|
#else
|
|
|
|
sprintf(pathname, "LLVM_%u", GetCurrentProcessId());
|
|
|
|
#endif
|
2005-07-08 02:48:42 +00:00
|
|
|
result.appendComponent(pathname);
|
2004-09-18 19:29:16 +00: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 03:08:58 +00:00
|
|
|
result.eraseFromDisk(true);
|
2004-09-18 19:29:16 +00:00
|
|
|
|
|
|
|
// And finally (re-)create the empty directory.
|
2005-07-08 03:08:58 +00:00
|
|
|
result.createDirectoryOnDisk(false);
|
2004-09-18 19:29:16 +00:00
|
|
|
TempDirectory = new Path(result);
|
|
|
|
return *TempDirectory;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2004-09-18 19:29:16 +00:00
|
|
|
// FIXME: the following set of functions don't map to Windows very well.
|
2004-09-15 05:49:50 +00:00
|
|
|
Path
|
|
|
|
Path::GetRootDirectory() {
|
2010-11-09 15:10:45 +00: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 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2005-07-08 05:02:13 +00:00
|
|
|
void
|
2004-12-13 03:03:42 +00:00
|
|
|
Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
|
2010-11-09 15:11:19 +00: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 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2004-12-13 03:03:42 +00:00
|
|
|
void
|
2007-07-06 20:28:40 +00:00
|
|
|
Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
|
2004-12-13 03:03:42 +00:00
|
|
|
char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
|
|
|
|
if (env_var != 0) {
|
|
|
|
getPathList(env_var,Paths);
|
|
|
|
}
|
|
|
|
#ifdef LLVM_LIBDIR
|
|
|
|
{
|
|
|
|
Path tmpPath;
|
2005-07-07 23:35:23 +00:00
|
|
|
if (tmpPath.set(LLVM_LIBDIR))
|
2005-07-07 18:21:42 +00:00
|
|
|
if (tmpPath.canRead())
|
2004-12-13 03:03:42 +00:00
|
|
|
Paths.push_back(tmpPath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
GetSystemLibraryPaths(Paths);
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
2004-09-11 04:59:30 +00:00
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
Path
|
|
|
|
Path::GetLLVMDefaultConfigDir() {
|
2010-11-09 15:11:42 +00:00
|
|
|
Path ret = GetUserHomeDirectory();
|
2010-11-10 15:06:00 +00:00
|
|
|
if (!ret.appendComponent(".llvm"))
|
2010-11-09 15:11:42 +00:00
|
|
|
assert(0 && "Failed to append .llvm");
|
|
|
|
return ret;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Path
|
|
|
|
Path::GetUserHomeDirectory() {
|
2010-11-09 15:11:31 +00:00
|
|
|
char buff[MAX_PATH];
|
2010-11-10 15:06:00 +00:00
|
|
|
HRESULT res = SHGetFolderPathA(NULL,
|
|
|
|
CSIDL_FLAG_CREATE | CSIDL_APPDATA,
|
|
|
|
NULL,
|
|
|
|
SHGFP_TYPE_CURRENT,
|
|
|
|
buff);
|
2010-11-09 15:11:31 +00:00
|
|
|
if (res != S_OK)
|
|
|
|
assert(0 && "Failed to get user home directory");
|
|
|
|
return Path(buff);
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
2007-12-18 22:07:33 +00:00
|
|
|
|
|
|
|
Path
|
|
|
|
Path::GetCurrentDirectory() {
|
|
|
|
char pathname[MAX_PATH];
|
2007-12-22 14:26:49 +00:00
|
|
|
::GetCurrentDirectoryA(MAX_PATH,pathname);
|
2010-03-12 14:17:24 +00:00
|
|
|
return Path(pathname);
|
2007-12-18 22:07:33 +00:00
|
|
|
}
|
|
|
|
|
2008-03-03 02:55:43 +00: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 05:38:04 +00:00
|
|
|
char pathname[MAX_PATH];
|
|
|
|
DWORD ret = ::GetModuleFileNameA(NULL, pathname, MAX_PATH);
|
|
|
|
return ret != MAX_PATH ? Path(pathname) : Path();
|
2008-03-03 02:55:43 +00:00
|
|
|
}
|
|
|
|
|
2007-12-18 22:07:33 +00:00
|
|
|
|
2004-09-18 19:29:16 +00:00
|
|
|
// FIXME: the above set of functions don't map to Windows very well.
|
|
|
|
|
2005-07-09 18:42:49 +00:00
|
|
|
|
2009-12-17 21:02:39 +00:00
|
|
|
StringRef Path::getDirname() const {
|
|
|
|
return getDirnameCharSep(path, "/");
|
2008-04-07 22:01:32 +00:00
|
|
|
}
|
2008-04-07 21:53:57 +00:00
|
|
|
|
2009-12-17 21:02:39 +00:00
|
|
|
StringRef
|
2004-11-05 22:15:36 +00:00
|
|
|
Path::getBasename() const {
|
2004-09-18 19:29:16 +00:00
|
|
|
// Find the last slash
|
|
|
|
size_t slash = path.rfind('/');
|
|
|
|
if (slash == std::string::npos)
|
|
|
|
slash = 0;
|
|
|
|
else
|
|
|
|
slash++;
|
|
|
|
|
2005-07-09 18:42:49 +00:00
|
|
|
size_t dot = path.rfind('.');
|
|
|
|
if (dot == std::string::npos || dot < slash)
|
2009-12-17 21:02:39 +00:00
|
|
|
return StringRef(path).substr(slash);
|
2005-07-09 18:42:49 +00:00
|
|
|
else
|
2009-12-17 21:02:39 +00:00
|
|
|
return StringRef(path).substr(slash, dot - slash);
|
2004-09-18 19:29:16 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 21:02:39 +00:00
|
|
|
StringRef
|
2008-06-15 15:15:19 +00: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-17 21:02:39 +00:00
|
|
|
return StringRef("");
|
2008-06-15 15:15:19 +00:00
|
|
|
else
|
2009-12-17 21:02:39 +00:00
|
|
|
return StringRef(path).substr(dot + 1);
|
2008-06-15 15:15:19 +00:00
|
|
|
}
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
bool
|
|
|
|
Path::exists() const {
|
2004-09-18 19:29:16 +00:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return attr != INVALID_FILE_ATTRIBUTES;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2007-12-18 19:46:22 +00:00
|
|
|
bool
|
|
|
|
Path::isDirectory() const {
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return (attr != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
(attr & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
}
|
|
|
|
|
2010-11-07 04:36:50 +00:00
|
|
|
bool
|
|
|
|
Path::isSymLink() const {
|
2010-11-10 15:05:39 +00: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 04:36:50 +00:00
|
|
|
}
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
bool
|
2005-07-07 18:21:42 +00:00
|
|
|
Path::canRead() const {
|
2004-09-18 19:29:16 +00:00
|
|
|
// FIXME: take security attributes into account.
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return attr != INVALID_FILE_ATTRIBUTES;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-07 18:21:42 +00:00
|
|
|
Path::canWrite() const {
|
2004-09-18 19:29:16 +00: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 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-07 18:21:42 +00:00
|
|
|
Path::canExecute() const {
|
2004-09-18 19:29:16 +00:00
|
|
|
// FIXME: take security attributes into account.
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return attr != INVALID_FILE_ATTRIBUTES;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
2004-09-11 04:59:30 +00:00
|
|
|
|
2009-11-24 15:19:10 +00:00
|
|
|
bool
|
2009-11-25 06:32:19 +00:00
|
|
|
Path::isRegularFile() const {
|
2011-01-11 01:21:55 +00:00
|
|
|
bool res;
|
|
|
|
if (fs::is_regular_file(path, res))
|
2009-11-25 06:32:19 +00:00
|
|
|
return false;
|
2011-01-11 01:21:55 +00:00
|
|
|
return res;
|
2009-11-24 15:19:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 21:02:39 +00:00
|
|
|
StringRef
|
2004-09-15 05:49:50 +00: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 04:50:08 +00:00
|
|
|
// If the last character is a slash, we have a root directory
|
|
|
|
if (pos == path.length()-1)
|
|
|
|
return path;
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
// Return everything after the last slash
|
2009-12-17 21:02:39 +00:00
|
|
|
return StringRef(path).substr(pos+1);
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 19:05:44 +00:00
|
|
|
const FileStatus *
|
2007-04-07 18:52:17 +00:00
|
|
|
PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
|
|
|
|
if (!fsIsValid || update) {
|
2007-03-29 16:43:20 +00:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
2007-03-29 19:05:44 +00:00
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
|
|
|
|
MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
|
2007-03-29 16:43:20 +00:00
|
|
|
": Can't get status: ");
|
2007-03-29 19:05:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-12-14 05:26:43 +00:00
|
|
|
|
2007-04-07 18:52:17 +00:00
|
|
|
status.fileSize = fi.nFileSizeHigh;
|
|
|
|
status.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
|
|
|
|
status.fileSize += fi.nFileSizeLow;
|
2004-12-14 05:26:43 +00:00
|
|
|
|
2007-04-07 18:52:17 +00: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 05:26:43 +00:00
|
|
|
|
2007-03-29 17:00:31 +00: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-07 18:52:17 +00:00
|
|
|
status.uniqueID = 0;
|
2007-03-29 17:00:31 +00:00
|
|
|
for (unsigned i = 0; i < path.length(); ++i)
|
2007-04-07 18:52:17 +00:00
|
|
|
status.uniqueID += path[i];
|
2007-03-29 17:00:31 +00:00
|
|
|
|
2010-08-28 16:39:32 +00:00
|
|
|
ULARGE_INTEGER ui;
|
|
|
|
ui.LowPart = fi.ftLastWriteTime.dwLowDateTime;
|
|
|
|
ui.HighPart = fi.ftLastWriteTime.dwHighDateTime;
|
|
|
|
status.modTime.fromWin32Time(ui.QuadPart);
|
2007-03-29 16:43:20 +00:00
|
|
|
|
2007-04-07 18:52:17 +00:00
|
|
|
status.isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
fsIsValid = true;
|
2007-03-29 16:43:20 +00:00
|
|
|
}
|
2007-04-07 18:52:17 +00:00
|
|
|
return &status;
|
2004-12-14 05:26:43 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 23:54:35 +00:00
|
|
|
bool Path::makeReadableOnDisk(std::string* ErrMsg) {
|
2004-12-14 05:26:43 +00:00
|
|
|
// All files are readable on Windows (ignoring security attributes).
|
2006-08-22 23:54:35 +00:00
|
|
|
return false;
|
2004-12-13 19:59:50 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 20:35:17 +00:00
|
|
|
bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
|
2004-12-14 05:26:43 +00:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
|
|
|
|
// If it doesn't exist, we're done.
|
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES)
|
2006-09-01 20:35:17 +00:00
|
|
|
return false;
|
2004-12-14 05:26:43 +00:00
|
|
|
|
|
|
|
if (attr & FILE_ATTRIBUTE_READONLY) {
|
2006-08-22 23:54:35 +00: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 05:26:43 +00:00
|
|
|
}
|
2006-08-22 23:54:35 +00:00
|
|
|
return false;
|
2004-12-13 19:59:50 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 23:54:35 +00:00
|
|
|
bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
|
2004-12-14 05:26:43 +00:00
|
|
|
// All files are executable on Windows (ignoring security attributes).
|
2006-08-22 23:54:35 +00:00
|
|
|
return false;
|
2004-12-13 19:59:50 +00:00
|
|
|
}
|
|
|
|
|
2004-12-31 04:39:07 +00:00
|
|
|
bool
|
2006-08-23 06:56:27 +00:00
|
|
|
Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
|
2007-04-07 20:47:27 +00:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
|
|
|
|
MakeErrMsg(ErrMsg, path + ": can't get status of file");
|
2006-09-01 20:35:17 +00:00
|
|
|
return true;
|
2007-04-07 20:47:27 +00:00
|
|
|
}
|
2010-03-12 14:17:24 +00:00
|
|
|
|
2007-04-07 20:47:27 +00:00
|
|
|
if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = path + ": not a directory";
|
2006-08-23 06:56:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-31 04:39:07 +00:00
|
|
|
|
|
|
|
result.clear();
|
|
|
|
WIN32_FIND_DATA fd;
|
2005-07-08 04:50:08 +00:00
|
|
|
std::string searchpath = path;
|
|
|
|
if (path.size() == 0 || searchpath[path.size()-1] == '/')
|
2005-07-08 05:02:13 +00:00
|
|
|
searchpath += "*";
|
2005-07-08 04:50:08 +00:00
|
|
|
else
|
|
|
|
searchpath += "/*";
|
2005-07-08 05:02:13 +00:00
|
|
|
|
2005-01-27 03:49:03 +00:00
|
|
|
HANDLE h = FindFirstFile(searchpath.c_str(), &fd);
|
2004-12-31 04:39:07 +00:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
2005-01-27 03:49:03 +00:00
|
|
|
if (GetLastError() == ERROR_FILE_NOT_FOUND)
|
2004-12-31 04:39:07 +00:00
|
|
|
return true; // not really an error, now is it?
|
2006-08-23 06:56:27 +00:00
|
|
|
MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
|
|
|
|
return true;
|
2004-12-31 04:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2004-12-31 05:07:26 +00:00
|
|
|
if (fd.cFileName[0] == '.')
|
|
|
|
continue;
|
2005-07-08 04:50:08 +00:00
|
|
|
Path aPath(path);
|
|
|
|
aPath.appendComponent(&fd.cFileName[0]);
|
2004-12-31 04:39:07 +00:00
|
|
|
result.insert(aPath);
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
|
2004-12-31 19:01:08 +00:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
FindClose(h);
|
|
|
|
if (err != ERROR_NO_MORE_FILES) {
|
|
|
|
SetLastError(err);
|
2006-08-23 06:56:27 +00:00
|
|
|
MakeErrMsg(ErrMsg, path + ": Can't read directory: ");
|
|
|
|
return true;
|
2004-12-31 19:01:08 +00:00
|
|
|
}
|
2006-08-23 06:56:27 +00:00
|
|
|
return false;
|
2004-12-31 04:39:07 +00:00
|
|
|
}
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
bool
|
2009-12-17 21:02:39 +00:00
|
|
|
Path::set(StringRef a_path) {
|
2008-01-29 13:02:09 +00:00
|
|
|
if (a_path.empty())
|
2004-09-15 05:49:50 +00:00
|
|
|
return false;
|
2005-07-07 23:35:23 +00:00
|
|
|
std::string save(path);
|
2004-09-15 05:49:50 +00:00
|
|
|
path = a_path;
|
2004-09-29 00:01:17 +00:00
|
|
|
FlipBackSlashes(path);
|
2004-11-05 22:15:36 +00:00
|
|
|
if (!isValid()) {
|
2005-07-07 23:35:23 +00:00
|
|
|
path = save;
|
2004-09-15 05:49:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2004-09-11 04:59:30 +00:00
|
|
|
}
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
bool
|
2009-12-17 21:02:39 +00:00
|
|
|
Path::appendComponent(StringRef name) {
|
2005-07-07 23:35:23 +00:00
|
|
|
if (name.empty())
|
2004-09-15 05:49:50 +00:00
|
|
|
return false;
|
2005-07-07 23:35:23 +00:00
|
|
|
std::string save(path);
|
|
|
|
if (!path.empty()) {
|
|
|
|
size_t last = path.size() - 1;
|
2005-07-08 05:02:13 +00:00
|
|
|
if (path[last] != '/')
|
2005-07-07 23:35:23 +00:00
|
|
|
path += '/';
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
2005-07-07 23:35:23 +00:00
|
|
|
path += name;
|
2004-11-05 22:15:36 +00:00
|
|
|
if (!isValid()) {
|
2005-07-07 23:35:23 +00:00
|
|
|
path = save;
|
2004-09-15 05:49:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-07 23:35:23 +00:00
|
|
|
Path::eraseComponent() {
|
2004-09-15 05:49:50 +00:00
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 04:50:08 +00:00
|
|
|
if (slashpos == path.size() - 1 || slashpos == std::string::npos)
|
2004-09-15 05:49:50 +00:00
|
|
|
return false;
|
2005-07-09 18:42:49 +00:00
|
|
|
std::string save(path);
|
2004-09-15 05:49:50 +00:00
|
|
|
path.erase(slashpos);
|
2005-07-09 18:42:49 +00:00
|
|
|
if (!isValid()) {
|
|
|
|
path = save;
|
|
|
|
return false;
|
|
|
|
}
|
2004-09-15 05:49:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-07-07 23:35:23 +00:00
|
|
|
Path::eraseSuffix() {
|
2004-09-15 05:49:50 +00:00
|
|
|
size_t dotpos = path.rfind('.',path.size());
|
|
|
|
size_t slashpos = path.rfind('/',path.size());
|
2005-07-08 04:50:08 +00:00
|
|
|
if (dotpos != std::string::npos) {
|
2005-07-09 18:42:49 +00:00
|
|
|
if (slashpos == std::string::npos || dotpos > slashpos+1) {
|
|
|
|
std::string save(path);
|
2005-07-08 04:50:08 +00:00
|
|
|
path.erase(dotpos, path.size()-dotpos);
|
2005-07-09 18:42:49 +00:00
|
|
|
if (!isValid()) {
|
|
|
|
path = save;
|
|
|
|
return false;
|
|
|
|
}
|
2005-07-08 05:02:13 +00:00
|
|
|
return true;
|
2005-07-08 04:50:08 +00:00
|
|
|
}
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-08-24 18:58:37 +00: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 05:49:50 +00:00
|
|
|
bool
|
2006-08-24 18:58:37 +00:00
|
|
|
Path::createDirectoryOnDisk(bool create_parents, std::string* ErrMsg) {
|
2004-09-15 05:49:50 +00:00
|
|
|
// Get a writeable copy of the path name
|
2005-07-08 04:50:08 +00:00
|
|
|
size_t len = path.length();
|
|
|
|
char *pathname = reinterpret_cast<char *>(_alloca(len+2));
|
|
|
|
path.copy(pathname, len);
|
|
|
|
pathname[len] = 0;
|
2005-07-08 05:02:13 +00:00
|
|
|
|
2005-07-08 04:50:08 +00:00
|
|
|
// Make sure it ends with a slash.
|
|
|
|
if (len == 0 || pathname[len - 1] != '/') {
|
|
|
|
pathname[len] = '/';
|
|
|
|
pathname[++len] = 0;
|
|
|
|
}
|
2004-09-29 00:01:17 +00: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-24 18:58:37 +00:00
|
|
|
return PathMsg(ErrMsg, pathname, "badly formed remote directory");
|
|
|
|
|
2004-09-29 00:01:17 +00:00
|
|
|
// Skip share name.
|
|
|
|
next = strchr(next+1, '/');
|
|
|
|
if (next == NULL)
|
2006-08-24 18:58:37 +00:00
|
|
|
return PathMsg(ErrMsg, pathname,"badly formed remote directory");
|
|
|
|
|
2004-09-29 00:01:17 +00:00
|
|
|
next++;
|
|
|
|
if (*next == 0)
|
2006-08-24 18:58:37 +00:00
|
|
|
return PathMsg(ErrMsg, pathname, "badly formed remote directory");
|
|
|
|
|
2004-09-29 00:01:17 +00:00
|
|
|
} else {
|
|
|
|
if (pathname[1] == ':')
|
|
|
|
next += 2; // skip drive letter
|
|
|
|
if (*next == '/')
|
|
|
|
next++; // skip root directory
|
|
|
|
}
|
2004-09-15 05:49:50 +00:00
|
|
|
|
|
|
|
// If we're supposed to create intermediate directories
|
2004-09-29 00:01:17 +00:00
|
|
|
if (create_parents) {
|
2004-09-15 05:49:50 +00:00
|
|
|
// Loop through the directory components until we're done
|
2004-09-29 00:01:17 +00:00
|
|
|
while (*next) {
|
|
|
|
next = strchr(next, '/');
|
2004-09-15 05:49:50 +00:00
|
|
|
*next = 0;
|
2009-11-05 14:32:40 +00:00
|
|
|
if (!CreateDirectory(pathname, NULL) &&
|
|
|
|
GetLastError() != ERROR_ALREADY_EXISTS)
|
2010-03-12 14:17:24 +00:00
|
|
|
return MakeErrMsg(ErrMsg,
|
2006-08-24 18:58:37 +00:00
|
|
|
std::string(pathname) + ": Can't create directory: ");
|
2004-09-29 00:01:17 +00:00
|
|
|
*next++ = '/';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Drop trailing slash.
|
2005-07-08 04:50:08 +00:00
|
|
|
pathname[len-1] = 0;
|
2009-11-05 14:32:40 +00:00
|
|
|
if (!CreateDirectory(pathname, NULL) &&
|
|
|
|
GetLastError() != ERROR_ALREADY_EXISTS) {
|
2010-11-02 20:32:31 +00:00
|
|
|
return MakeErrMsg(ErrMsg, std::string(pathname) +
|
|
|
|
": Can't create directory: ");
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-24 18:58:37 +00:00
|
|
|
return false;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-08-24 18:58:37 +00:00
|
|
|
Path::createFileOnDisk(std::string* ErrMsg) {
|
2004-09-15 05:49:50 +00:00
|
|
|
// Create the file
|
2004-09-29 00:01:17 +00:00
|
|
|
HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
|
2004-09-18 19:29:16 +00:00
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2006-08-24 18:58:37 +00:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": Can't create file: ");
|
2004-09-15 05:49:50 +00:00
|
|
|
|
2004-09-18 19:29:16 +00:00
|
|
|
CloseHandle(h);
|
2006-08-24 18:58:37 +00:00
|
|
|
return false;
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-07-28 22:29:50 +00:00
|
|
|
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
|
2007-04-07 20:47:27 +00:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
|
|
|
|
return true;
|
2010-03-12 14:17:24 +00:00
|
|
|
|
2007-04-07 20:47:27 +00:00
|
|
|
if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
2005-07-07 23:35:23 +00:00
|
|
|
// If it doesn't exist, we're done.
|
2011-01-10 02:34:23 +00:00
|
|
|
bool Exists;
|
|
|
|
if (fs::exists(path, Exists) || !Exists)
|
2006-07-28 22:29:50 +00:00
|
|
|
return false;
|
2005-07-07 23:35:23 +00:00
|
|
|
|
2005-07-08 04:50:08 +00:00
|
|
|
char *pathname = reinterpret_cast<char *>(_alloca(path.length()+3));
|
2005-07-07 23:35:23 +00:00
|
|
|
int lastchar = path.length() - 1 ;
|
2005-07-08 04:50:08 +00:00
|
|
|
path.copy(pathname, lastchar+1);
|
2005-07-07 23:35:23 +00:00
|
|
|
|
|
|
|
// Make path end with '/*'.
|
2005-07-08 04:50:08 +00:00
|
|
|
if (pathname[lastchar] != '/')
|
|
|
|
pathname[++lastchar] = '/';
|
2005-07-07 23:35:23 +00: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 05:02:13 +00:00
|
|
|
Path aPath(path);
|
|
|
|
aPath.appendComponent(&fd.cFileName[0]);
|
2005-07-07 23:35:23 +00:00
|
|
|
list.push_back(aPath);
|
|
|
|
} while (FindNextFile(h, &fd));
|
|
|
|
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
FindClose(h);
|
|
|
|
if (err != ERROR_NO_MORE_FILES) {
|
|
|
|
SetLastError(err);
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
|
2005-07-07 23:35:23 +00:00
|
|
|
}
|
2004-12-31 19:01:08 +00:00
|
|
|
|
2005-07-08 05:02:13 +00:00
|
|
|
for (std::vector<Path>::iterator I = list.begin(); I != list.end();
|
2005-07-07 23:35:23 +00:00
|
|
|
++I) {
|
|
|
|
Path &aPath = *I;
|
2005-07-08 03:08:58 +00:00
|
|
|
aPath.eraseFromDisk(true);
|
2005-07-07 23:35:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (GetLastError() != ERROR_FILE_NOT_FOUND)
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't read directory: ");
|
2004-12-31 19:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-15 05:49:50 +00:00
|
|
|
|
2005-07-07 23:35:23 +00:00
|
|
|
pathname[lastchar] = 0;
|
|
|
|
if (!RemoveDirectory(pathname))
|
2010-03-12 14:17:24 +00:00
|
|
|
return MakeErrMsg(ErrStr,
|
2006-08-25 21:37:17 +00:00
|
|
|
std::string(pathname) + ": Can't destroy directory: ");
|
2006-07-28 22:29:50 +00:00
|
|
|
return false;
|
2007-04-07 20:47:27 +00: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 05:49:50 +00:00
|
|
|
}
|
|
|
|
|
2004-12-14 18:42:13 +00:00
|
|
|
bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
|
|
|
|
assert(len < 1024 && "Request for magic string too long");
|
2010-08-31 06:36:33 +00:00
|
|
|
char* buf = reinterpret_cast<char*>(alloca(len));
|
2004-12-31 19:01:08 +00: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-14 18:42:13 +00:00
|
|
|
return false;
|
2004-12-31 19:01:08 +00:00
|
|
|
|
|
|
|
DWORD nRead = 0;
|
|
|
|
BOOL ret = ReadFile(h, buf, len, &nRead, NULL);
|
|
|
|
CloseHandle(h);
|
|
|
|
|
|
|
|
if (!ret || nRead != len)
|
2004-12-14 18:42:13 +00:00
|
|
|
return false;
|
2004-12-31 19:01:08 +00:00
|
|
|
|
2010-08-31 06:36:33 +00:00
|
|
|
Magic = std::string(buf, len);
|
2004-12-14 18:42:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-31 04:39:07 +00:00
|
|
|
bool
|
2006-08-23 07:30:48 +00:00
|
|
|
Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
|
2010-09-30 00:44:58 +00: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 04:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-09-01 20:35:17 +00:00
|
|
|
Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrMsg) const {
|
2005-07-09 18:42:49 +00:00
|
|
|
// FIXME: should work on directories also.
|
2006-09-01 20:35:17 +00:00
|
|
|
if (!si.isFile) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-12 14:17:24 +00:00
|
|
|
|
2004-12-31 04:39:07 +00: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 05:07:26 +00:00
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
2004-12-31 04:39:07 +00:00
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2006-07-28 22:36:17 +00:00
|
|
|
return true;
|
2004-12-31 04:39:07 +00:00
|
|
|
|
|
|
|
BY_HANDLE_FILE_INFORMATION bhfi;
|
|
|
|
if (!GetFileInformationByHandle(h, &bhfi)) {
|
2004-12-31 19:01:08 +00:00
|
|
|
DWORD err = GetLastError();
|
2004-12-31 04:39:07 +00:00
|
|
|
CloseHandle(h);
|
2004-12-31 19:01:08 +00:00
|
|
|
SetLastError(err);
|
2006-09-01 20:35:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": GetFileInformationByHandle: ");
|
2004-12-31 04:39:07 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 16:39:32 +00:00
|
|
|
ULARGE_INTEGER ui;
|
|
|
|
ui.QuadPart = si.modTime.toWin32Time();
|
2004-12-31 04:39:07 +00:00
|
|
|
FILETIME ft;
|
2010-08-28 16:39:32 +00:00
|
|
|
ft.dwLowDateTime = ui.LowPart;
|
|
|
|
ft.dwHighDateTime = ui.HighPart;
|
2004-12-31 04:39:07 +00:00
|
|
|
BOOL ret = SetFileTime(h, NULL, &ft, &ft);
|
2004-12-31 19:01:08 +00:00
|
|
|
DWORD err = GetLastError();
|
2004-12-31 04:39:07 +00:00
|
|
|
CloseHandle(h);
|
2004-12-31 19:01:08 +00:00
|
|
|
if (!ret) {
|
|
|
|
SetLastError(err);
|
2006-09-01 20:35:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": SetFileTime: ");
|
2004-12-31 19:01:08 +00:00
|
|
|
}
|
2004-12-31 04:39:07 +00: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 05:07:26 +00:00
|
|
|
bhfi.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
|
2006-09-01 20:35:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
|
2004-12-31 04:39:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(bhfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
if (!SetFileAttributes(path.c_str(),
|
2004-12-31 05:07:26 +00:00
|
|
|
bhfi.dwFileAttributes | FILE_ATTRIBUTE_READONLY))
|
2006-09-01 20:35:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": SetFileAttributes: ");
|
2004-12-31 04:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-28 22:36:17 +00:00
|
|
|
return false;
|
2004-12-31 04:39:07 +00:00
|
|
|
}
|
|
|
|
|
2006-08-24 18:58:37 +00:00
|
|
|
bool
|
|
|
|
CopyFile(const sys::Path &Dest, const sys::Path &Src, std::string* ErrMsg) {
|
2004-12-24 02:38:34 +00: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-23 22:45:37 +00:00
|
|
|
return MakeErrMsg(ErrMsg, "Can't copy '" + Src.str() +
|
|
|
|
"' to '" + Dest.str() + "': ");
|
2006-08-24 18:58:37 +00:00
|
|
|
return false;
|
2004-12-15 01:50:13 +00:00
|
|
|
}
|
|
|
|
|
2006-08-24 18:58:37 +00:00
|
|
|
bool
|
|
|
|
Path::makeUnique(bool reuse_current, std::string* ErrMsg) {
|
2011-01-10 02:34:23 +00:00
|
|
|
bool Exists;
|
|
|
|
if (reuse_current && (fs::exists(path, Exists) || !Exists))
|
2006-08-24 18:58:37 +00:00
|
|
|
return false; // File doesn't exist already, just use it!
|
2004-12-15 01:50:13 +00:00
|
|
|
|
2005-01-27 03:49:03 +00: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 01:50:13 +00:00
|
|
|
|
2005-07-09 18:42:49 +00: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.
|
2005-01-27 03:49:03 +00:00
|
|
|
static unsigned FCounter = 0;
|
|
|
|
do {
|
|
|
|
sprintf(FNBuffer+offset, "-%06u", FCounter);
|
|
|
|
if (++FCounter > 999999)
|
|
|
|
FCounter = 0;
|
|
|
|
path = FNBuffer;
|
2011-01-10 02:34:23 +00:00
|
|
|
} while (!fs::exists(path, Exists) && Exists);
|
2006-08-24 18:58:37 +00:00
|
|
|
return false;
|
2004-12-15 01:50:13 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 08:32:45 +00:00
|
|
|
bool
|
2006-08-24 18:58:37 +00:00
|
|
|
Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
|
2004-12-15 08:32:45 +00:00
|
|
|
// Make this into a unique file name
|
2006-09-01 20:35:17 +00:00
|
|
|
makeUnique(reuse_current, ErrMsg);
|
2005-01-27 03:49:03 +00: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-01 20:35:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg, path + ": can't create file");
|
2005-01-27 03:49:03 +00:00
|
|
|
|
|
|
|
CloseHandle(h);
|
2006-08-24 18:58:37 +00:00
|
|
|
return false;
|
2004-12-15 08:32:45 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 06:00:12 +00:00
|
|
|
/// MapInFilePages - Not yet implemented on win32.
|
|
|
|
const char *Path::MapInFilePages(int FD, uint64_t FileSize) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MapInFilePages - Not yet implemented on win32.
|
|
|
|
void Path::UnMapFilePages(const char *Base, uint64_t FileSize) {
|
|
|
|
assert(0 && "NOT IMPLEMENTED");
|
|
|
|
}
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
}
|
2004-09-11 04:59:30 +00:00
|
|
|
}
|