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
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
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);
|
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] == ' ') {
|
|
|
|
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
|
2006-08-23 06:46:39 +08:00
|
|
|
Path::GetTemporaryDirectory(std::string* ErrMsg) {
|
2004-09-19 03:29:16 +08:00
|
|
|
if (TempDirectory)
|
|
|
|
return *TempDirectory;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Append a subdirectory passed on our process id so multiple LLVMs don't
|
|
|
|
// step on each other's toes.
|
2006-06-09 02:08:43 +08:00
|
|
|
sprintf(pathname, "LLVM_%u", unsigned(GetCurrentProcessId()));
|
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() {
|
|
|
|
Path result;
|
2005-07-10 02:42:49 +08:00
|
|
|
result.set("C:/");
|
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;
|
2005-07-08 12:50:08 +08:00
|
|
|
while (delim != 0) {
|
2004-12-13 11:03:42 +08:00
|
|
|
std::string tmp(at, size_t(delim-at));
|
2005-07-08 07:35:23 +08:00
|
|
|
if (tmpPath.set(tmp))
|
2005-07-08 02:21:42 +08:00
|
|
|
if (tmpPath.canRead())
|
2004-12-13 11:03:42 +08:00
|
|
|
Paths.push_back(tmpPath);
|
|
|
|
at = delim + 1;
|
|
|
|
delim = strchr(at, ';');
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
2005-07-08 13:02:13 +08:00
|
|
|
|
2004-12-13 11:03:42 +08:00
|
|
|
if (*at != 0)
|
2005-07-08 07:35:23 +08:00
|
|
|
if (tmpPath.set(std::string(at)))
|
2005-07-08 02:21:42 +08:00
|
|
|
if (tmpPath.canRead())
|
2004-12-13 11:03:42 +08:00
|
|
|
Paths.push_back(tmpPath);
|
2004-09-19 03:29:16 +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) {
|
2005-07-10 02:42:49 +08:00
|
|
|
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;
|
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::GetLLVMDefaultConfigDir() {
|
2004-12-31 12:39:07 +08:00
|
|
|
// TODO: this isn't going to fly on Windows
|
2005-07-08 12:50:08 +08:00
|
|
|
return Path("/etc/llvm");
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2005-07-08 07:35:23 +08:00
|
|
|
if (result.set(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.
|
|
|
|
|
2005-07-10 02:42:49 +08:00
|
|
|
|
|
|
|
bool
|
|
|
|
Path::isRootDirectory() const {
|
|
|
|
size_t len = path.size();
|
|
|
|
return len > 0 && path[len-1] == '/';
|
2005-07-08 11:08:58 +08:00
|
|
|
}
|
|
|
|
|
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++;
|
|
|
|
|
2005-07-10 02:42:49 +08:00
|
|
|
size_t dot = path.rfind('.');
|
|
|
|
if (dot == std::string::npos || dot < slash)
|
|
|
|
return path.substr(slash);
|
|
|
|
else
|
|
|
|
return path.substr(slash, dot - slash);
|
2004-09-19 03:29:16 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-07-08 13:02:13 +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
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
return path.substr(pos+1);
|
|
|
|
}
|
|
|
|
|
2006-07-29 06:32:09 +08:00
|
|
|
bool
|
2006-08-01 16:07:22 +08:00
|
|
|
Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
|
2004-12-14 13:26:43 +08:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fi;
|
|
|
|
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
|
|
|
|
": Can't get status: ");
|
2004-12-14 13:26:43 +08:00
|
|
|
|
|
|
|
info.fileSize = fi.nFileSizeHigh;
|
2006-06-09 01:00:08 +08:00
|
|
|
info.fileSize <<= sizeof(fi.nFileSizeHigh)*8;
|
2004-12-14 13:26:43 +08:00
|
|
|
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;
|
2006-07-29 06:32:09 +08:00
|
|
|
return false;
|
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-08-23 07:54:35 +08:00
|
|
|
void 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)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if (!isDirectory()) {
|
|
|
|
MakeErrMsg(ErrMsg, path + ": not a directory");
|
|
|
|
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
|
2005-07-08 07:35:23 +08:00
|
|
|
Path::set(const std::string& a_path) {
|
2004-09-15 13:49:50 +08:00
|
|
|
if (a_path.size() == 0)
|
|
|
|
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
|
2005-07-08 10:48:42 +08:00
|
|
|
Path::appendComponent(const std::string& 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
|
2004-11-06 06:15:36 +08:00
|
|
|
Path::appendSuffix(const std::string& suffix) {
|
2005-07-08 07:35:23 +08:00
|
|
|
std::string save(path);
|
2004-09-15 13:49:50 +08:00
|
|
|
path.append(".");
|
|
|
|
path.append(suffix);
|
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::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;
|
2004-09-19 03:29:16 +08:00
|
|
|
if (!CreateDirectory(pathname, NULL))
|
2006-08-25 02:58:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg,
|
|
|
|
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;
|
2004-09-29 08:01:17 +08:00
|
|
|
if (!CreateDirectory(pathname, NULL)) {
|
2006-08-25 02:58:37 +08:00
|
|
|
return MakeErrMsg(, 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 {
|
2006-08-02 01:51:09 +08:00
|
|
|
FileStatus Status;
|
|
|
|
if (getFileStatus(Status, ErrStr))
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2006-08-02 01:51:09 +08:00
|
|
|
|
|
|
|
if (Status.isFile) {
|
2005-07-08 07:35:23 +08:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
|
|
|
|
// If it doesn't exist, we're done.
|
|
|
|
if (attr == INVALID_FILE_ATTRIBUTES)
|
2006-08-25 02:58:37 +08:00
|
|
|
return false;
|
2005-07-08 07:35:23 +08:00
|
|
|
|
|
|
|
// 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))
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
|
2005-07-08 07:35:23 +08:00
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2005-07-08 07:35:23 +08:00
|
|
|
if (!DeleteFile(path.c_str()))
|
2006-08-25 02:58:37 +08:00
|
|
|
return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
|
2006-08-02 01:51:09 +08:00
|
|
|
return false;
|
|
|
|
} else if (Status.isDir) {
|
2005-07-08 07:35:23 +08:00
|
|
|
// If it doesn't exist, we're done.
|
2005-07-08 13:02:13 +08:00
|
|
|
if (!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))
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr,
|
|
|
|
std::string(pathname) + ": Can't destroy directory: ");
|
2006-07-29 06:29:50 +08:00
|
|
|
return false;
|
2006-08-25 02:58:37 +08:00
|
|
|
}
|
|
|
|
// It appears the path doesn't exist.
|
|
|
|
return true;
|
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");
|
|
|
|
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
|
2006-08-23 15:30:48 +08:00
|
|
|
Path::renamePathOnDisk(const Path& newName, std::string* ErrMsg) {
|
2006-05-07 10:51:51 +08:00
|
|
|
if (!MoveFileEx(path.c_str(), newName.c_str(), MOVEFILE_REPLACE_EXISTING))
|
2006-08-23 15:30:48 +08:00
|
|
|
return MakeErrMsg(ErrMsg, "Can't move '" + path + "' to '" + newName.path
|
|
|
|
+ "': ");
|
2004-12-31 12:39:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2006-07-29 06:36:17 +08:00
|
|
|
Path::setStatusInfoOnDisk(const FileStatus &si, std::string *ErrStr) const {
|
2005-07-10 02:42:49 +08:00
|
|
|
// FIXME: should work on directories also.
|
2006-07-29 06:36:17 +08:00
|
|
|
if (!isFile()) return true;
|
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-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, 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);
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(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-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, 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-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrStr, 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))
|
2006-08-25 02:58:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, "Can't copy '" + Src.toString() +
|
2004-12-31 13:07:26 +08:00
|
|
|
"' to '" + Dest.toString() + "': ");
|
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) {
|
2004-12-15 16:32:45 +08:00
|
|
|
if (reuse_current && !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.
|
2005-01-27 11:49:03 +08:00
|
|
|
static unsigned FCounter = 0;
|
|
|
|
do {
|
|
|
|
sprintf(FNBuffer+offset, "-%06u", FCounter);
|
|
|
|
if (++FCounter > 999999)
|
|
|
|
FCounter = 0;
|
|
|
|
path = FNBuffer;
|
|
|
|
} while (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
|
2005-07-10 02:42:49 +08:00
|
|
|
makeUnique(reuse_current);
|
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-08-25 02:58:37 +08:00
|
|
|
return MakeErrMsg(ErrMsg, path.toString() + ": 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
|
|
|
}
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|