| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#ifdef __osf__ |
| 4 |
# define _OSF_SOURCE |
| 5 |
# define _POSIX_C_SOURCE 199506L |
| 6 |
# define _XOPEN_SOURCE_EXTENDED |
| 7 |
#endif |
| 8 |
|
| 9 |
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__MINGW32__)) |
| 10 |
# define KWSYS_WINDOWS_DIRS |
| 11 |
#else |
| 12 |
# if defined(__SUNPRO_CC) |
| 13 |
# include <fcntl.h> |
| 14 |
# endif |
| 15 |
#endif |
| 16 |
|
| 17 |
#if defined(_WIN32) && !defined(_WIN32_WINNT) |
| 18 |
# define _WIN32_WINNT _WIN32_WINNT_VISTA |
| 19 |
#endif |
| 20 |
|
| 21 |
#include "kwsysPrivate.h" |
| 22 |
#include KWSYS_HEADER(RegularExpression.hxx) |
| 23 |
#include KWSYS_HEADER(SystemTools.hxx) |
| 24 |
#include KWSYS_HEADER(Directory.hxx) |
| 25 |
#include KWSYS_HEADER(FStream.hxx) |
| 26 |
#include KWSYS_HEADER(Encoding.h) |
| 27 |
#include KWSYS_HEADER(Encoding.hxx) |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <fstream> |
| 31 |
#include <iostream> |
| 32 |
#include <set> |
| 33 |
#include <sstream> |
| 34 |
#include <utility> |
| 35 |
#include <vector> |
| 36 |
|
| 37 |
#ifdef _WIN32 |
| 38 |
# include <cwchar> |
| 39 |
# include <unordered_map> |
| 40 |
#endif |
| 41 |
|
| 42 |
// Work-around CMake dependency scanning limitation. This must |
| 43 |
// duplicate the above list of headers. |
| 44 |
#if 0 |
| 45 |
# include "Directory.hxx.in" |
| 46 |
# include "Encoding.hxx.in" |
| 47 |
# include "FStream.hxx.in" |
| 48 |
# include "RegularExpression.hxx.in" |
| 49 |
# include "SystemTools.hxx.in" |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifdef _MSC_VER |
| 53 |
# pragma warning(disable : 4786) |
| 54 |
#endif |
| 55 |
|
| 56 |
#if defined(__sgi) && !defined(__GNUC__) |
| 57 |
# pragma set woff 1375 /* base class destructor not virtual */ |
| 58 |
#endif |
| 59 |
|
| 60 |
#include <cctype> |
| 61 |
#include <cerrno> |
| 62 |
#ifdef __QNX__ |
| 63 |
# include <malloc.h> /* for malloc/free on QNX */ |
| 64 |
#endif |
| 65 |
#include <cstdio> |
| 66 |
#include <cstdlib> |
| 67 |
#include <cstring> |
| 68 |
#include <ctime> |
| 69 |
|
| 70 |
#if defined(_WIN32) && !defined(_MSC_VER) && defined(__GNUC__) |
| 71 |
# include <strings.h> /* for strcasecmp */ |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef _MSC_VER |
| 75 |
# define umask _umask |
| 76 |
#endif |
| 77 |
|
| 78 |
// support for realpath call |
| 79 |
#ifndef _WIN32 |
| 80 |
# include <climits> |
| 81 |
# include <pwd.h> |
| 82 |
# include <sys/ioctl.h> |
| 83 |
# include <sys/time.h> |
| 84 |
# include <sys/wait.h> |
| 85 |
# include <unistd.h> |
| 86 |
# include <utime.h> |
| 87 |
# ifndef __VMS |
| 88 |
# include <sys/param.h> |
| 89 |
# include <termios.h> |
| 90 |
# endif |
| 91 |
# include <csignal> /* sigprocmask */ |
| 92 |
#endif |
| 93 |
|
| 94 |
#ifdef __linux |
| 95 |
# include <linux/fs.h> |
| 96 |
#endif |
| 97 |
|
| 98 |
#if defined(__APPLE__) && \ |
| 99 |
(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0 >= 101200) |
| 100 |
# define KWSYS_SYSTEMTOOLS_HAVE_MACOS_COPYFILE_CLONE |
| 101 |
# include <copyfile.h> |
| 102 |
# include <sys/stat.h> |
| 103 |
#endif |
| 104 |
|
| 105 |
// Windows API. |
| 106 |
#if defined(_WIN32) |
| 107 |
# include <windows.h> |
| 108 |
# include <winioctl.h> |
| 109 |
# ifndef INVALID_FILE_ATTRIBUTES |
| 110 |
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| 111 |
# endif |
| 112 |
# ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE |
| 113 |
# define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE (0x2) |
| 114 |
# endif |
| 115 |
# if defined(_MSC_VER) && _MSC_VER >= 1800 |
| 116 |
# define KWSYS_WINDOWS_DEPRECATED_GetVersionEx |
| 117 |
# endif |
| 118 |
# ifndef IO_REPARSE_TAG_APPEXECLINK |
| 119 |
# define IO_REPARSE_TAG_APPEXECLINK (0x8000001BL) |
| 120 |
# endif |
| 121 |
// from ntifs.h, which can only be used by drivers |
| 122 |
typedef struct _REPARSE_DATA_BUFFER |
| 123 |
{ |
| 124 |
ULONG ReparseTag; |
| 125 |
USHORT ReparseDataLength; |
| 126 |
USHORT Reserved; |
| 127 |
union |
| 128 |
{ |
| 129 |
struct |
| 130 |
{ |
| 131 |
USHORT SubstituteNameOffset; |
| 132 |
USHORT SubstituteNameLength; |
| 133 |
USHORT PrintNameOffset; |
| 134 |
USHORT PrintNameLength; |
| 135 |
ULONG Flags; |
| 136 |
WCHAR PathBuffer[1]; |
| 137 |
} SymbolicLinkReparseBuffer; |
| 138 |
struct |
| 139 |
{ |
| 140 |
USHORT SubstituteNameOffset; |
| 141 |
USHORT SubstituteNameLength; |
| 142 |
USHORT PrintNameOffset; |
| 143 |
USHORT PrintNameLength; |
| 144 |
WCHAR PathBuffer[1]; |
| 145 |
} MountPointReparseBuffer; |
| 146 |
struct |
| 147 |
{ |
| 148 |
UCHAR DataBuffer[1]; |
| 149 |
} GenericReparseBuffer; |
| 150 |
struct |
| 151 |
{ |
| 152 |
ULONG Version; |
| 153 |
WCHAR StringList[1]; |
| 154 |
// In version 3, there are 4 NUL-terminated strings: |
| 155 |
// * Package ID |
| 156 |
// * Entry Point |
| 157 |
// * Executable Path |
| 158 |
// * Application Type |
| 159 |
} AppExecLinkReparseBuffer; |
| 160 |
} DUMMYUNIONNAME; |
| 161 |
} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 162 |
|
| 163 |
namespace { |
| 164 |
WCHAR* GetAppExecLink(PREPARSE_DATA_BUFFER data, size_t& len) |
| 165 |
{ |
| 166 |
// We only know the layout of version 3. |
| 167 |
if (data->AppExecLinkReparseBuffer.Version != 3) { |
| 168 |
return nullptr; |
| 169 |
} |
| 170 |
|
| 171 |
WCHAR* pstr = data->AppExecLinkReparseBuffer.StringList; |
| 172 |
|
| 173 |
// Skip the package id and entry point strings. |
| 174 |
for (int i = 0; i < 2; ++i) { |
| 175 |
len = std::wcslen(pstr); |
| 176 |
if (len == 0) { |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
pstr += len + 1; |
| 180 |
} |
| 181 |
|
| 182 |
// The third string is the executable path. |
| 183 |
len = std::wcslen(pstr); |
| 184 |
if (len == 0) { |
| 185 |
return nullptr; |
| 186 |
} |
| 187 |
return pstr; |
| 188 |
} |
| 189 |
} |
| 190 |
#endif |
| 191 |
|
| 192 |
#if !KWSYS_CXX_HAS_ENVIRON_IN_STDLIB_H |
| 193 |
extern char** environ; |
| 194 |
#endif |
| 195 |
|
| 196 |
// getpwnam doesn't exist on Windows and Cray Xt3/Catamount |
| 197 |
// same for TIOCGWINSZ |
| 198 |
#if defined(_WIN32) || defined(__LIBCATAMOUNT__) || \ |
| 199 |
(defined(HAVE_GETPWNAM) && HAVE_GETPWNAM == 0) |
| 200 |
# undef HAVE_GETPWNAM |
| 201 |
# undef HAVE_TTY_INFO |
| 202 |
#else |
| 203 |
# define HAVE_GETPWNAM 1 |
| 204 |
# define HAVE_TTY_INFO 1 |
| 205 |
#endif |
| 206 |
|
| 207 |
#define VTK_URL_PROTOCOL_REGEX "([a-zA-Z0-9]*)://(.*)" |
| 208 |
#define VTK_URL_REGEX \ |
| 209 |
"([a-zA-Z0-9]*)://(([A-Za-z0-9]+)(:([^:@]+))?@)?([^:@/]*)(:([0-9]+))?/" \ |
| 210 |
"(.+)?" |
| 211 |
#define VTK_URL_BYTE_REGEX "%[0-9a-fA-F][0-9a-fA-F]" |
| 212 |
#ifdef _MSC_VER |
| 213 |
# include <sys/utime.h> |
| 214 |
#else |
| 215 |
# include <utime.h> |
| 216 |
#endif |
| 217 |
|
| 218 |
// This is a hack to prevent warnings about these functions being |
| 219 |
// declared but not referenced. |
| 220 |
#if defined(__sgi) && !defined(__GNUC__) |
| 221 |
# include <sys/termios.h> |
| 222 |
namespace KWSYS_NAMESPACE { |
| 223 |
class SystemToolsHack |
| 224 |
{ |
| 225 |
public: |
| 226 |
enum |
| 227 |
{ |
| 228 |
Ref1 = sizeof(cfgetospeed(0)), |
| 229 |
Ref2 = sizeof(cfgetispeed(0)), |
| 230 |
Ref3 = sizeof(tcgetattr(0, 0)), |
| 231 |
Ref4 = sizeof(tcsetattr(0, 0, 0)), |
| 232 |
Ref5 = sizeof(cfsetospeed(0, 0)), |
| 233 |
Ref6 = sizeof(cfsetispeed(0, 0)) |
| 234 |
}; |
| 235 |
}; |
| 236 |
} |
| 237 |
#endif |
| 238 |
|
| 239 |
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__MINGW32__)) |
| 240 |
# include <direct.h> |
| 241 |
# include <io.h> |
| 242 |
# define _unlink unlink |
| 243 |
#endif |
| 244 |
|
| 245 |
/* The maximum length of a file name. */ |
| 246 |
#if defined(PATH_MAX) |
| 247 |
# define KWSYS_SYSTEMTOOLS_MAXPATH PATH_MAX |
| 248 |
#elif defined(MAXPATHLEN) |
| 249 |
# define KWSYS_SYSTEMTOOLS_MAXPATH MAXPATHLEN |
| 250 |
#else |
| 251 |
# define KWSYS_SYSTEMTOOLS_MAXPATH 16384 |
| 252 |
#endif |
| 253 |
|
| 254 |
#if defined(__BEOS__) && !defined(__ZETA__) |
| 255 |
# include <be/kernel/OS.h> |
| 256 |
# include <be/storage/Path.h> |
| 257 |
|
| 258 |
// BeOS 5 doesn't have usleep(), but it has snooze(), which is identical. |
| 259 |
static inline void usleep(unsigned int msec) |
| 260 |
{ |
| 261 |
::snooze(msec); |
| 262 |
} |
| 263 |
|
| 264 |
// BeOS 5 also doesn't have realpath(), but its C++ API offers something close. |
| 265 |
static inline char* realpath(const char* path, char* resolved_path) |
| 266 |
{ |
| 267 |
const size_t maxlen = KWSYS_SYSTEMTOOLS_MAXPATH; |
| 268 |
snprintf(resolved_path, maxlen, "%s", path); |
| 269 |
BPath normalized(resolved_path, nullptr, true); |
| 270 |
const char* resolved = normalized.Path(); |
| 271 |
if (resolved != nullptr) // nullptr == No such file. |
| 272 |
{ |
| 273 |
if (snprintf(resolved_path, maxlen, "%s", resolved) < maxlen) { |
| 274 |
return resolved_path; |
| 275 |
} |
| 276 |
} |
| 277 |
return nullptr; // something went wrong. |
| 278 |
} |
| 279 |
#endif |
| 280 |
|
| 281 |
#ifdef _WIN32 |
| 282 |
static time_t windows_filetime_to_posix_time(const FILETIME& ft) |
| 283 |
{ |
| 284 |
LARGE_INTEGER date; |
| 285 |
date.HighPart = ft.dwHighDateTime; |
| 286 |
date.LowPart = ft.dwLowDateTime; |
| 287 |
|
| 288 |
// removes the diff between 1970 and 1601 |
| 289 |
date.QuadPart -= ((LONGLONG)(369 * 365 + 89) * 24 * 3600 * 10000000); |
| 290 |
|
| 291 |
// converts back from 100-nanoseconds to seconds |
| 292 |
return date.QuadPart / 10000000; |
| 293 |
} |
| 294 |
#endif |
| 295 |
|
| 296 |
#ifdef KWSYS_WINDOWS_DIRS |
| 297 |
# include <wctype.h> |
| 298 |
# ifdef _MSC_VER |
| 299 |
typedef KWSYS_NAMESPACE::SystemTools::mode_t mode_t; |
| 300 |
# endif |
| 301 |
|
| 302 |
inline int Mkdir(const std::string& dir, const mode_t* mode) |
| 303 |
{ |
| 304 |
int ret = |
| 305 |
_wmkdir(KWSYS_NAMESPACE::Encoding::ToWindowsExtendedPath(dir).c_str()); |
| 306 |
if (ret == 0 && mode) |
| 307 |
KWSYS_NAMESPACE::SystemTools::SetPermissions(dir, *mode); |
| 308 |
return ret; |
| 309 |
} |
| 310 |
inline int Rmdir(const std::string& dir) |
| 311 |
{ |
| 312 |
return _wrmdir( |
| 313 |
KWSYS_NAMESPACE::Encoding::ToWindowsExtendedPath(dir).c_str()); |
| 314 |
} |
| 315 |
inline const char* Getcwd(char* buf, unsigned int len) |
| 316 |
{ |
| 317 |
std::vector<wchar_t> w_buf(len); |
| 318 |
if (_wgetcwd(&w_buf[0], len)) { |
| 319 |
size_t nlen = kwsysEncoding_wcstombs(buf, &w_buf[0], len); |
| 320 |
if (nlen == static_cast<size_t>(-1)) { |
| 321 |
return 0; |
| 322 |
} |
| 323 |
if (nlen < len) { |
| 324 |
// make sure the drive letter is capital |
| 325 |
if (nlen > 1 && buf[1] == ':') { |
| 326 |
buf[0] = toupper(buf[0]); |
| 327 |
} |
| 328 |
return buf; |
| 329 |
} |
| 330 |
} |
| 331 |
return 0; |
| 332 |
} |
| 333 |
inline int Chdir(const std::string& dir) |
| 334 |
{ |
| 335 |
return _wchdir(KWSYS_NAMESPACE::Encoding::ToWide(dir).c_str()); |
| 336 |
} |
| 337 |
inline void Realpath(const std::string& path, std::string& resolved_path, |
| 338 |
std::string* errorMessage = nullptr) |
| 339 |
{ |
| 340 |
std::wstring tmp = KWSYS_NAMESPACE::Encoding::ToWide(path); |
| 341 |
wchar_t* ptemp; |
| 342 |
wchar_t fullpath[MAX_PATH]; |
| 343 |
DWORD bufferLen = GetFullPathNameW( |
| 344 |
tmp.c_str(), sizeof(fullpath) / sizeof(fullpath[0]), fullpath, &ptemp); |
| 345 |
if (bufferLen < sizeof(fullpath) / sizeof(fullpath[0])) { |
| 346 |
resolved_path = KWSYS_NAMESPACE::Encoding::ToNarrow(fullpath); |
| 347 |
KWSYS_NAMESPACE::SystemTools::ConvertToUnixSlashes(resolved_path); |
| 348 |
} else if (errorMessage) { |
| 349 |
if (bufferLen) { |
| 350 |
*errorMessage = "Destination path buffer size too small."; |
| 351 |
} else if (unsigned int errorId = GetLastError()) { |
| 352 |
LPSTR message = nullptr; |
| 353 |
DWORD size = FormatMessageA( |
| 354 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 355 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 356 |
nullptr, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 357 |
(LPSTR)&message, 0, nullptr); |
| 358 |
*errorMessage = std::string(message, size); |
| 359 |
LocalFree(message); |
| 360 |
} else { |
| 361 |
*errorMessage = "Unknown error."; |
| 362 |
} |
| 363 |
|
| 364 |
resolved_path = ""; |
| 365 |
} else { |
| 366 |
resolved_path = path; |
| 367 |
} |
| 368 |
} |
| 369 |
#else |
| 370 |
# include <sys/types.h> |
| 371 |
|
| 372 |
# include <fcntl.h> |
| 373 |
# include <unistd.h> |
| 374 |
inline int Mkdir(const std::string& dir, const mode_t* mode) |
| 375 |
{ |
| 376 |
return mkdir(dir.c_str(), mode ? *mode : 00777); |
| 377 |
} |
| 378 |
inline int Rmdir(const std::string& dir) |
| 379 |
{ |
| 380 |
return rmdir(dir.c_str()); |
| 381 |
} |
| 382 |
inline const char* Getcwd(char* buf, unsigned int len) |
| 383 |
{ |
| 384 |
return getcwd(buf, len); |
| 385 |
} |
| 386 |
|
| 387 |
inline int Chdir(const std::string& dir) |
| 388 |
{ |
| 389 |
return chdir(dir.c_str()); |
| 390 |
} |
| 391 |
inline void Realpath(const std::string& path, std::string& resolved_path, |
| 392 |
std::string* errorMessage = nullptr) |
| 393 |
{ |
| 394 |
char resolved_name[KWSYS_SYSTEMTOOLS_MAXPATH]; |
| 395 |
|
| 396 |
errno = 0; |
| 397 |
char* ret = realpath(path.c_str(), resolved_name); |
| 398 |
if (ret) { |
| 399 |
resolved_path = ret; |
| 400 |
} else if (errorMessage) { |
| 401 |
if (errno) { |
| 402 |
*errorMessage = strerror(errno); |
| 403 |
} else { |
| 404 |
*errorMessage = "Unknown error."; |
| 405 |
} |
| 406 |
|
| 407 |
resolved_path = ""; |
| 408 |
} else { |
| 409 |
// if path resolution fails, return what was passed in |
| 410 |
resolved_path = path; |
| 411 |
} |
| 412 |
} |
| 413 |
#endif |
| 414 |
|
| 415 |
namespace KWSYS_NAMESPACE { |
| 416 |
|
| 417 |
double SystemTools::GetTime() |
| 418 |
{ |
| 419 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 420 |
FILETIME ft; |
| 421 |
GetSystemTimeAsFileTime(&ft); |
| 422 |
return (429.4967296 * ft.dwHighDateTime + 0.0000001 * ft.dwLowDateTime - |
| 423 |
11644473600.0); |
| 424 |
#else |
| 425 |
struct timeval t; |
| 426 |
gettimeofday(&t, nullptr); |
| 427 |
return 1.0 * double(t.tv_sec) + 0.000001 * double(t.tv_usec); |
| 428 |
#endif |
| 429 |
} |
| 430 |
|
| 431 |
/* Type of character storing the environment. */ |
| 432 |
#if defined(_WIN32) |
| 433 |
typedef wchar_t envchar; |
| 434 |
#else |
| 435 |
using envchar = char; |
| 436 |
#endif |
| 437 |
|
| 438 |
/* Order by environment key only (VAR from VAR=VALUE). */ |
| 439 |
struct kwsysEnvCompare |
| 440 |
{ |
| 441 |
bool operator()(const envchar* l, const envchar* r) const |
| 442 |
{ |
| 443 |
#if defined(_WIN32) |
| 444 |
const wchar_t* leq = wcschr(l, L'='); |
| 445 |
const wchar_t* req = wcschr(r, L'='); |
| 446 |
size_t llen = leq ? (leq - l) : wcslen(l); |
| 447 |
size_t rlen = req ? (req - r) : wcslen(r); |
| 448 |
if (llen == rlen) { |
| 449 |
return wcsncmp(l, r, llen) < 0; |
| 450 |
} else { |
| 451 |
return wcscmp(l, r) < 0; |
| 452 |
} |
| 453 |
#else |
| 454 |
const char* leq = strchr(l, '='); |
| 455 |
const char* req = strchr(r, '='); |
| 456 |
size_t llen = leq ? static_cast<size_t>(leq - l) : strlen(l); |
| 457 |
size_t rlen = req ? static_cast<size_t>(req - r) : strlen(r); |
| 458 |
if (llen == rlen) { |
| 459 |
return strncmp(l, r, llen) < 0; |
| 460 |
} else { |
| 461 |
return strcmp(l, r) < 0; |
| 462 |
} |
| 463 |
#endif |
| 464 |
} |
| 465 |
}; |
| 466 |
|
| 467 |
class kwsysEnvSet : public std::set<const envchar*, kwsysEnvCompare> |
| 468 |
{ |
| 469 |
public: |
| 470 |
class Free |
| 471 |
{ |
| 472 |
const envchar* Env; |
| 473 |
|
| 474 |
public: |
| 475 |
Free(const envchar* env) |
| 476 |
: Env(env) |
| 477 |
{ |
| 478 |
} |
| 479 |
~Free() { free(const_cast<envchar*>(this->Env)); } |
| 480 |
|
| 481 |
Free(const Free&) = delete; |
| 482 |
Free& operator=(const Free&) = delete; |
| 483 |
}; |
| 484 |
|
| 485 |
const envchar* Release(const envchar* env) |
| 486 |
{ |
| 487 |
const envchar* old = nullptr; |
| 488 |
auto i = this->find(env); |
| 489 |
if (i != this->end()) { |
| 490 |
old = *i; |
| 491 |
this->erase(i); |
| 492 |
} |
| 493 |
return old; |
| 494 |
} |
| 495 |
}; |
| 496 |
|
| 497 |
#ifdef _WIN32 |
| 498 |
# if defined(_WIN64) |
| 499 |
static constexpr size_t FNV_OFFSET_BASIS = 14695981039346656037ULL; |
| 500 |
static constexpr size_t FNV_PRIME = 1099511628211ULL; |
| 501 |
# else |
| 502 |
static constexpr size_t FNV_OFFSET_BASIS = 2166136261U; |
| 503 |
static constexpr size_t FNV_PRIME = 16777619U; |
| 504 |
# endif |
| 505 |
|
| 506 |
// Case insensitive Fnv1a hash |
| 507 |
struct SystemToolsPathCaseHash |
| 508 |
{ |
| 509 |
size_t operator()(std::string const& path) const |
| 510 |
{ |
| 511 |
size_t hash = FNV_OFFSET_BASIS; |
| 512 |
for (auto c : path) { |
| 513 |
hash ^= static_cast<size_t>(std::tolower(c)); |
| 514 |
hash *= FNV_PRIME; |
| 515 |
} |
| 516 |
|
| 517 |
return hash; |
| 518 |
} |
| 519 |
}; |
| 520 |
|
| 521 |
struct SystemToolsPathCaseEqual |
| 522 |
{ |
| 523 |
bool operator()(std::string const& l, std::string const& r) const |
| 524 |
{ |
| 525 |
# ifdef _MSC_VER |
| 526 |
return _stricmp(l.c_str(), r.c_str()) == 0; |
| 527 |
# elif defined(__GNUC__) |
| 528 |
return strcasecmp(l.c_str(), r.c_str()) == 0; |
| 529 |
# else |
| 530 |
return SystemTools::Strucmp(l.c_str(), r.c_str()) == 0; |
| 531 |
# endif |
| 532 |
} |
| 533 |
}; |
| 534 |
#endif |
| 535 |
|
| 536 |
/** |
| 537 |
* SystemTools static variables singleton class. |
| 538 |
*/ |
| 539 |
class SystemToolsStatic |
| 540 |
{ |
| 541 |
public: |
| 542 |
using StringMap = std::map<std::string, std::string>; |
| 543 |
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP |
| 544 |
/** |
| 545 |
* Path translation table from dir to refdir |
| 546 |
* Each time 'dir' will be found it will be replace by 'refdir' |
| 547 |
*/ |
| 548 |
StringMap TranslationMap; |
| 549 |
#endif |
| 550 |
#ifdef _WIN32 |
| 551 |
static std::string GetCasePathName(std::string const& pathIn, |
| 552 |
bool const cache); |
| 553 |
static std::string GetActualCaseForPathCached(std::string const& path); |
| 554 |
static const char* GetEnvBuffered(const char* key); |
| 555 |
std::unordered_map<std::string, std::string, SystemToolsPathCaseHash, |
| 556 |
SystemToolsPathCaseEqual> |
| 557 |
FindFileMap; |
| 558 |
std::unordered_map<std::string, std::string, SystemToolsPathCaseHash, |
| 559 |
SystemToolsPathCaseEqual> |
| 560 |
PathCaseMap; |
| 561 |
std::map<std::string, std::string> EnvMap; |
| 562 |
#endif |
| 563 |
#ifdef __CYGWIN__ |
| 564 |
StringMap Cyg2Win32Map; |
| 565 |
#endif |
| 566 |
|
| 567 |
/** |
| 568 |
* Actual implementation of ReplaceString. |
| 569 |
*/ |
| 570 |
static void ReplaceString(std::string& source, const char* replace, |
| 571 |
size_t replaceSize, const std::string& with); |
| 572 |
|
| 573 |
/** |
| 574 |
* Actual implementation of FileIsFullPath. |
| 575 |
*/ |
| 576 |
static bool FileIsFullPath(const char*, size_t); |
| 577 |
|
| 578 |
/** |
| 579 |
* Find a filename (file or directory) in the system PATH, with |
| 580 |
* optional extra paths. |
| 581 |
*/ |
| 582 |
static std::string FindName( |
| 583 |
const std::string& name, |
| 584 |
const std::vector<std::string>& userPaths = std::vector<std::string>(), |
| 585 |
bool no_system_path = false); |
| 586 |
}; |
| 587 |
|
| 588 |
// Do NOT initialize. Default initialization to zero is necessary. |
| 589 |
static SystemToolsStatic* SystemToolsStatics; |
| 590 |
|
| 591 |
#ifdef _WIN32 |
| 592 |
std::string SystemToolsStatic::GetCasePathName(std::string const& pathIn, |
| 593 |
bool const cache) |
| 594 |
{ |
| 595 |
std::string casePath; |
| 596 |
|
| 597 |
// First check if the file is relative. We don't fix relative paths since the |
| 598 |
// real case depends on the root directory and the given path fragment may |
| 599 |
// have meaning elsewhere in the project. |
| 600 |
if (!SystemTools::FileIsFullPath(pathIn)) { |
| 601 |
// This looks unnecessary, but it allows for the return value optimization |
| 602 |
// since all return paths return the same local variable. |
| 603 |
casePath = pathIn; |
| 604 |
return casePath; |
| 605 |
} |
| 606 |
|
| 607 |
std::vector<std::string> path_components; |
| 608 |
SystemTools::SplitPath(pathIn, path_components); |
| 609 |
|
| 610 |
// Start with root component. |
| 611 |
std::vector<std::string>::size_type idx = 0; |
| 612 |
casePath = path_components[idx++]; |
| 613 |
// make sure drive letter is always upper case |
| 614 |
if (casePath.size() > 1 && casePath[1] == ':') { |
| 615 |
casePath[0] = toupper(casePath[0]); |
| 616 |
} |
| 617 |
const char* sep = ""; |
| 618 |
|
| 619 |
// If network path, fill casePath with server/share so FindFirstFile |
| 620 |
// will work after that. Maybe someday call other APIs to get |
| 621 |
// actual case of servers and shares. |
| 622 |
if (path_components.size() > 2 && path_components[0] == "//") { |
| 623 |
casePath += path_components[idx++]; |
| 624 |
casePath += "/"; |
| 625 |
casePath += path_components[idx++]; |
| 626 |
sep = "/"; |
| 627 |
} |
| 628 |
|
| 629 |
// Convert case of all components that exist. |
| 630 |
bool converting = true; |
| 631 |
for (; idx < path_components.size(); idx++) { |
| 632 |
casePath += sep; |
| 633 |
sep = "/"; |
| 634 |
|
| 635 |
if (converting) { |
| 636 |
// If path component contains wildcards, we skip matching |
| 637 |
// because these filenames are not allowed on windows, |
| 638 |
// and we do not want to match a different file. |
| 639 |
if (path_components[idx].find('*') != std::string::npos || |
| 640 |
path_components[idx].find('?') != std::string::npos) { |
| 641 |
converting = false; |
| 642 |
} else { |
| 643 |
std::string test_str = casePath; |
| 644 |
test_str += path_components[idx]; |
| 645 |
|
| 646 |
bool found_in_cache = false; |
| 647 |
if (cache) { |
| 648 |
auto const it = SystemToolsStatics->FindFileMap.find(test_str); |
| 649 |
if (it != SystemToolsStatics->FindFileMap.end()) { |
| 650 |
path_components[idx] = it->second; |
| 651 |
found_in_cache = true; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
if (!found_in_cache) { |
| 656 |
WIN32_FIND_DATAW findData; |
| 657 |
HANDLE hFind = |
| 658 |
::FindFirstFileW(Encoding::ToWide(test_str).c_str(), &findData); |
| 659 |
if (INVALID_HANDLE_VALUE != hFind) { |
| 660 |
auto case_file_name = Encoding::ToNarrow(findData.cFileName); |
| 661 |
if (cache) { |
| 662 |
SystemToolsStatics->FindFileMap.emplace(test_str, |
| 663 |
case_file_name); |
| 664 |
} |
| 665 |
path_components[idx] = std::move(case_file_name); |
| 666 |
::FindClose(hFind); |
| 667 |
} else { |
| 668 |
converting = false; |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
casePath += path_components[idx]; |
| 675 |
} |
| 676 |
return casePath; |
| 677 |
} |
| 678 |
|
| 679 |
std::string SystemToolsStatic::GetActualCaseForPathCached(std::string const& p) |
| 680 |
{ |
| 681 |
std::string casePath; |
| 682 |
|
| 683 |
auto it = SystemToolsStatics->PathCaseMap.find(p); |
| 684 |
if (it != SystemToolsStatics->PathCaseMap.end()) { |
| 685 |
casePath = it->second; |
| 686 |
} else { |
| 687 |
casePath = SystemToolsStatic::GetCasePathName(p, true); |
| 688 |
SystemToolsStatics->PathCaseMap.emplace(p, casePath); |
| 689 |
} |
| 690 |
|
| 691 |
return casePath; |
| 692 |
} |
| 693 |
#endif |
| 694 |
|
| 695 |
// adds the elements of the env variable path to the arg passed in |
| 696 |
void SystemTools::GetPath(std::vector<std::string>& path, const char* env) |
| 697 |
{ |
| 698 |
size_t const old_size = path.size(); |
| 699 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 700 |
const char pathSep = ';'; |
| 701 |
#else |
| 702 |
const char pathSep = ':'; |
| 703 |
#endif |
| 704 |
if (!env) { |
| 705 |
env = "PATH"; |
| 706 |
} |
| 707 |
std::string pathEnv; |
| 708 |
if (!SystemTools::GetEnv(env, pathEnv)) { |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
// A hack to make the below algorithm work. |
| 713 |
if (!pathEnv.empty() && pathEnv.back() != pathSep) { |
| 714 |
pathEnv += pathSep; |
| 715 |
} |
| 716 |
std::string::size_type start = 0; |
| 717 |
bool done = false; |
| 718 |
while (!done) { |
| 719 |
std::string::size_type endpos = pathEnv.find(pathSep, start); |
| 720 |
if (endpos != std::string::npos) { |
| 721 |
path.push_back(pathEnv.substr(start, endpos - start)); |
| 722 |
start = endpos + 1; |
| 723 |
} else { |
| 724 |
done = true; |
| 725 |
} |
| 726 |
} |
| 727 |
for (auto i = path.begin() + old_size; i != path.end(); ++i) { |
| 728 |
SystemTools::ConvertToUnixSlashes(*i); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
#if defined(_WIN32) |
| 733 |
const char* SystemToolsStatic::GetEnvBuffered(const char* key) |
| 734 |
{ |
| 735 |
std::string env; |
| 736 |
if (SystemTools::GetEnv(key, env)) { |
| 737 |
std::string& menv = SystemToolsStatics->EnvMap[key]; |
| 738 |
if (menv != env) { |
| 739 |
menv = std::move(env); |
| 740 |
} |
| 741 |
return menv.c_str(); |
| 742 |
} |
| 743 |
return nullptr; |
| 744 |
} |
| 745 |
#endif |
| 746 |
|
| 747 |
const char* SystemTools::GetEnv(const char* key) |
| 748 |
{ |
| 749 |
#if defined(_WIN32) |
| 750 |
return SystemToolsStatic::GetEnvBuffered(key); |
| 751 |
#else |
| 752 |
return getenv(key); |
| 753 |
#endif |
| 754 |
} |
| 755 |
|
| 756 |
const char* SystemTools::GetEnv(const std::string& key) |
| 757 |
{ |
| 758 |
#if defined(_WIN32) |
| 759 |
return SystemToolsStatic::GetEnvBuffered(key.c_str()); |
| 760 |
#else |
| 761 |
return getenv(key.c_str()); |
| 762 |
#endif |
| 763 |
} |
| 764 |
|
| 765 |
bool SystemTools::GetEnv(const char* key, std::string& result) |
| 766 |
{ |
| 767 |
#if defined(_WIN32) |
| 768 |
auto wide_key = Encoding::ToWide(key); |
| 769 |
auto result_size = GetEnvironmentVariableW(wide_key.data(), nullptr, 0); |
| 770 |
if (result_size <= 0) { |
| 771 |
return false; |
| 772 |
} |
| 773 |
std::wstring wide_result; |
| 774 |
wide_result.resize(result_size - 1); |
| 775 |
GetEnvironmentVariableW(wide_key.data(), &wide_result[0], result_size); |
| 776 |
result = Encoding::ToNarrow(wide_result); |
| 777 |
return true; |
| 778 |
#else |
| 779 |
const char* v = getenv(key); |
| 780 |
if (v) { |
| 781 |
result = v; |
| 782 |
return true; |
| 783 |
} |
| 784 |
#endif |
| 785 |
return false; |
| 786 |
} |
| 787 |
|
| 788 |
bool SystemTools::GetEnv(const std::string& key, std::string& result) |
| 789 |
{ |
| 790 |
return SystemTools::GetEnv(key.c_str(), result); |
| 791 |
} |
| 792 |
|
| 793 |
bool SystemTools::HasEnv(const char* key) |
| 794 |
{ |
| 795 |
#if defined(_WIN32) |
| 796 |
const std::wstring wkey = Encoding::ToWide(key); |
| 797 |
const wchar_t* v = _wgetenv(wkey.c_str()); |
| 798 |
#else |
| 799 |
const char* v = getenv(key); |
| 800 |
#endif |
| 801 |
return v != nullptr; |
| 802 |
} |
| 803 |
|
| 804 |
bool SystemTools::HasEnv(const std::string& key) |
| 805 |
{ |
| 806 |
return SystemTools::HasEnv(key.c_str()); |
| 807 |
} |
| 808 |
|
| 809 |
#if KWSYS_CXX_HAS_UNSETENV |
| 810 |
/* unsetenv("A") removes A from the environment. |
| 811 |
On older platforms it returns void instead of int. */ |
| 812 |
static int kwsysUnPutEnv(const std::string& env) |
| 813 |
{ |
| 814 |
size_t pos = env.find('='); |
| 815 |
if (pos != std::string::npos) { |
| 816 |
std::string name = env.substr(0, pos); |
| 817 |
unsetenv(name.c_str()); |
| 818 |
} else { |
| 819 |
unsetenv(env.c_str()); |
| 820 |
} |
| 821 |
return 0; |
| 822 |
} |
| 823 |
|
| 824 |
#elif defined(__CYGWIN__) || defined(__GLIBC__) |
| 825 |
/* putenv("A") removes A from the environment. It must not put the |
| 826 |
memory in the environment because it does not have any "=" syntax. */ |
| 827 |
static int kwsysUnPutEnv(const std::string& env) |
| 828 |
{ |
| 829 |
int err = 0; |
| 830 |
size_t pos = env.find('='); |
| 831 |
size_t const len = pos == std::string::npos ? env.size() : pos; |
| 832 |
size_t const sz = len + 1; |
| 833 |
char local_buf[256]; |
| 834 |
char* buf = sz > sizeof(local_buf) ? (char*)malloc(sz) : local_buf; |
| 835 |
if (!buf) { |
| 836 |
return -1; |
| 837 |
} |
| 838 |
strncpy(buf, env.c_str(), len); |
| 839 |
buf[len] = 0; |
| 840 |
if (putenv(buf) < 0 && errno != EINVAL) { |
| 841 |
err = errno; |
| 842 |
} |
| 843 |
if (buf != local_buf) { |
| 844 |
free(buf); |
| 845 |
} |
| 846 |
if (err) { |
| 847 |
errno = err; |
| 848 |
return -1; |
| 849 |
} |
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
#elif defined(_WIN32) |
| 854 |
/* putenv("A=") places "A=" in the environment, which is as close to |
| 855 |
removal as we can get with the putenv API. We have to leak the |
| 856 |
most recent value placed in the environment for each variable name |
| 857 |
on program exit in case exit routines access it. */ |
| 858 |
|
| 859 |
static kwsysEnvSet kwsysUnPutEnvSet; |
| 860 |
|
| 861 |
static int kwsysUnPutEnv(std::string const& env) |
| 862 |
{ |
| 863 |
std::wstring wEnv = Encoding::ToWide(env); |
| 864 |
size_t const pos = wEnv.find('='); |
| 865 |
size_t const len = pos == std::string::npos ? wEnv.size() : pos; |
| 866 |
wEnv.resize(len + 1, L'='); |
| 867 |
wchar_t* newEnv = _wcsdup(wEnv.c_str()); |
| 868 |
if (!newEnv) { |
| 869 |
return -1; |
| 870 |
} |
| 871 |
kwsysEnvSet::Free oldEnv(kwsysUnPutEnvSet.Release(newEnv)); |
| 872 |
kwsysUnPutEnvSet.insert(newEnv); |
| 873 |
return _wputenv(newEnv); |
| 874 |
} |
| 875 |
|
| 876 |
#else |
| 877 |
/* Manipulate the "environ" global directly. */ |
| 878 |
static int kwsysUnPutEnv(const std::string& env) |
| 879 |
{ |
| 880 |
size_t pos = env.find('='); |
| 881 |
size_t const len = pos == std::string::npos ? env.size() : pos; |
| 882 |
int in = 0; |
| 883 |
int out = 0; |
| 884 |
while (environ[in]) { |
| 885 |
if (strlen(environ[in]) > len && environ[in][len] == '=' && |
| 886 |
strncmp(env.c_str(), environ[in], len) == 0) { |
| 887 |
++in; |
| 888 |
} else { |
| 889 |
environ[out++] = environ[in++]; |
| 890 |
} |
| 891 |
} |
| 892 |
while (out < in) { |
| 893 |
environ[out++] = 0; |
| 894 |
} |
| 895 |
return 0; |
| 896 |
} |
| 897 |
#endif |
| 898 |
|
| 899 |
#if KWSYS_CXX_HAS_SETENV |
| 900 |
|
| 901 |
/* setenv("A", "B", 1) will set A=B in the environment and makes its |
| 902 |
own copies of the strings. */ |
| 903 |
bool SystemTools::PutEnv(const std::string& env) |
| 904 |
{ |
| 905 |
size_t pos = env.find('='); |
| 906 |
if (pos != std::string::npos) { |
| 907 |
std::string name = env.substr(0, pos); |
| 908 |
return setenv(name.c_str(), env.c_str() + pos + 1, 1) == 0; |
| 909 |
} else { |
| 910 |
return kwsysUnPutEnv(env) == 0; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
bool SystemTools::UnPutEnv(const std::string& env) |
| 915 |
{ |
| 916 |
return kwsysUnPutEnv(env) == 0; |
| 917 |
} |
| 918 |
|
| 919 |
#else |
| 920 |
|
| 921 |
/* putenv("A=B") will set A=B in the environment. Most putenv implementations |
| 922 |
put their argument directly in the environment. They never free the memory |
| 923 |
on program exit. Keep an active set of pointers to memory we allocate and |
| 924 |
pass to putenv, one per environment key. At program exit remove any |
| 925 |
environment values that may still reference memory we allocated. Then free |
| 926 |
the memory. This will not affect any environment values we never set. */ |
| 927 |
|
| 928 |
# ifdef __INTEL_COMPILER |
| 929 |
# pragma warning disable 444 /* base has non-virtual destructor */ |
| 930 |
# endif |
| 931 |
|
| 932 |
class kwsysEnv : public kwsysEnvSet |
| 933 |
{ |
| 934 |
public: |
| 935 |
~kwsysEnv() |
| 936 |
{ |
| 937 |
for (iterator i = this->begin(); i != this->end(); ++i) { |
| 938 |
# if defined(_WIN32) |
| 939 |
const std::string s = Encoding::ToNarrow(*i); |
| 940 |
kwsysUnPutEnv(s); |
| 941 |
# else |
| 942 |
kwsysUnPutEnv(*i); |
| 943 |
# endif |
| 944 |
free(const_cast<envchar*>(*i)); |
| 945 |
} |
| 946 |
} |
| 947 |
bool Put(const char* env) |
| 948 |
{ |
| 949 |
# if defined(_WIN32) |
| 950 |
const std::wstring wEnv = Encoding::ToWide(env); |
| 951 |
wchar_t* newEnv = _wcsdup(wEnv.c_str()); |
| 952 |
# else |
| 953 |
char* newEnv = strdup(env); |
| 954 |
# endif |
| 955 |
Free oldEnv(this->Release(newEnv)); |
| 956 |
this->insert(newEnv); |
| 957 |
# if defined(_WIN32) |
| 958 |
return _wputenv(newEnv) == 0; |
| 959 |
# else |
| 960 |
return putenv(newEnv) == 0; |
| 961 |
# endif |
| 962 |
} |
| 963 |
bool UnPut(const char* env) |
| 964 |
{ |
| 965 |
# if defined(_WIN32) |
| 966 |
const std::wstring wEnv = Encoding::ToWide(env); |
| 967 |
Free oldEnv(this->Release(wEnv.c_str())); |
| 968 |
# else |
| 969 |
Free oldEnv(this->Release(env)); |
| 970 |
# endif |
| 971 |
return kwsysUnPutEnv(env) == 0; |
| 972 |
} |
| 973 |
}; |
| 974 |
|
| 975 |
static kwsysEnv kwsysEnvInstance; |
| 976 |
|
| 977 |
bool SystemTools::PutEnv(const std::string& env) |
| 978 |
{ |
| 979 |
return kwsysEnvInstance.Put(env.c_str()); |
| 980 |
} |
| 981 |
|
| 982 |
bool SystemTools::UnPutEnv(const std::string& env) |
| 983 |
{ |
| 984 |
return kwsysEnvInstance.UnPut(env.c_str()); |
| 985 |
} |
| 986 |
|
| 987 |
#endif |
| 988 |
|
| 989 |
const char* SystemTools::GetExecutableExtension() |
| 990 |
{ |
| 991 |
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__VMS) |
| 992 |
return ".exe"; |
| 993 |
#else |
| 994 |
return ""; |
| 995 |
#endif |
| 996 |
} |
| 997 |
|
| 998 |
FILE* SystemTools::Fopen(const std::string& file, const char* mode) |
| 999 |
{ |
| 1000 |
#ifdef _WIN32 |
| 1001 |
// Remove any 'e', which is supported on UNIX, but not Windows. |
| 1002 |
std::wstring trimmedMode = Encoding::ToWide(mode); |
| 1003 |
trimmedMode.erase(std::remove(trimmedMode.begin(), trimmedMode.end(), L'e'), |
| 1004 |
trimmedMode.end()); |
| 1005 |
return _wfopen(Encoding::ToWindowsExtendedPath(file).c_str(), |
| 1006 |
trimmedMode.c_str()); |
| 1007 |
#else |
| 1008 |
return fopen(file.c_str(), mode); |
| 1009 |
#endif |
| 1010 |
} |
| 1011 |
|
| 1012 |
Status SystemTools::MakeDirectory(const char* path, const mode_t* mode) |
| 1013 |
{ |
| 1014 |
if (!path) { |
| 1015 |
return Status::POSIX(EINVAL); |
| 1016 |
} |
| 1017 |
return SystemTools::MakeDirectory(std::string(path), mode); |
| 1018 |
} |
| 1019 |
|
| 1020 |
Status SystemTools::MakeDirectory(std::string const& path, const mode_t* mode) |
| 1021 |
{ |
| 1022 |
if (path.empty()) { |
| 1023 |
return Status::POSIX(EINVAL); |
| 1024 |
} |
| 1025 |
if (SystemTools::PathExists(path)) { |
| 1026 |
if (SystemTools::FileIsDirectory(path)) { |
| 1027 |
return Status::Success(); |
| 1028 |
} |
| 1029 |
return Status::POSIX(EEXIST); |
| 1030 |
} |
| 1031 |
std::string dir = path; |
| 1032 |
SystemTools::ConvertToUnixSlashes(dir); |
| 1033 |
|
| 1034 |
std::string::size_type pos = 0; |
| 1035 |
std::string topdir; |
| 1036 |
while ((pos = dir.find('/', pos)) != std::string::npos) { |
| 1037 |
// all underlying functions use C strings, so temporarily |
| 1038 |
// end the string here |
| 1039 |
dir[pos] = '\0'; |
| 1040 |
|
| 1041 |
Mkdir(dir, mode); |
| 1042 |
dir[pos] = '/'; |
| 1043 |
|
| 1044 |
++pos; |
| 1045 |
} |
| 1046 |
topdir = dir; |
| 1047 |
if (Mkdir(topdir, mode) != 0 && errno != EEXIST) { |
| 1048 |
return Status::POSIX_errno(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
return Status::Success(); |
| 1052 |
} |
| 1053 |
|
| 1054 |
// replace replace with with as many times as it shows up in source. |
| 1055 |
// write the result into source. |
| 1056 |
void SystemTools::ReplaceString(std::string& source, |
| 1057 |
const std::string& replace, |
| 1058 |
const std::string& with) |
| 1059 |
{ |
| 1060 |
// do while hangs if replaceSize is 0 |
| 1061 |
if (replace.empty()) { |
| 1062 |
return; |
| 1063 |
} |
| 1064 |
|
| 1065 |
SystemToolsStatic::ReplaceString(source, replace.c_str(), replace.size(), |
| 1066 |
with); |
| 1067 |
} |
| 1068 |
|
| 1069 |
void SystemTools::ReplaceString(std::string& source, const char* replace, |
| 1070 |
const char* with) |
| 1071 |
{ |
| 1072 |
// do while hangs if replaceSize is 0 |
| 1073 |
if (!*replace) { |
| 1074 |
return; |
| 1075 |
} |
| 1076 |
|
| 1077 |
SystemToolsStatic::ReplaceString(source, replace, strlen(replace), |
| 1078 |
with ? with : ""); |
| 1079 |
} |
| 1080 |
|
| 1081 |
void SystemToolsStatic::ReplaceString(std::string& source, const char* replace, |
| 1082 |
size_t replaceSize, |
| 1083 |
const std::string& with) |
| 1084 |
{ |
| 1085 |
const char* src = source.c_str(); |
| 1086 |
char* searchPos = const_cast<char*>(strstr(src, replace)); |
| 1087 |
|
| 1088 |
// get out quick if string is not found |
| 1089 |
if (!searchPos) { |
| 1090 |
return; |
| 1091 |
} |
| 1092 |
|
| 1093 |
// perform replacements until done |
| 1094 |
char* orig = strdup(src); |
| 1095 |
char* currentPos = orig; |
| 1096 |
searchPos = searchPos - src + orig; |
| 1097 |
|
| 1098 |
// initialize the result |
| 1099 |
source.erase(source.begin(), source.end()); |
| 1100 |
do { |
| 1101 |
*searchPos = '\0'; |
| 1102 |
source += currentPos; |
| 1103 |
currentPos = searchPos + replaceSize; |
| 1104 |
// replace |
| 1105 |
source += with; |
| 1106 |
searchPos = strstr(currentPos, replace); |
| 1107 |
} while (searchPos); |
| 1108 |
|
| 1109 |
// copy any trailing text |
| 1110 |
source += currentPos; |
| 1111 |
free(orig); |
| 1112 |
} |
| 1113 |
|
| 1114 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1115 |
|
| 1116 |
# if defined(KEY_WOW64_32KEY) && defined(KEY_WOW64_64KEY) |
| 1117 |
# define KWSYS_ST_KEY_WOW64_32KEY KEY_WOW64_32KEY |
| 1118 |
# define KWSYS_ST_KEY_WOW64_64KEY KEY_WOW64_64KEY |
| 1119 |
# else |
| 1120 |
# define KWSYS_ST_KEY_WOW64_32KEY 0x0200 |
| 1121 |
# define KWSYS_ST_KEY_WOW64_64KEY 0x0100 |
| 1122 |
# endif |
| 1123 |
|
| 1124 |
static bool hasPrefix(const std::string& s, const char* pattern, |
| 1125 |
std::string::size_type spos) |
| 1126 |
{ |
| 1127 |
size_t plen = strlen(pattern); |
| 1128 |
if (spos != plen) |
| 1129 |
return false; |
| 1130 |
return s.compare(0, plen, pattern) == 0; |
| 1131 |
} |
| 1132 |
|
| 1133 |
static bool SystemToolsParseRegistryKey(const std::string& key, |
| 1134 |
HKEY& primaryKey, std::wstring& second, |
| 1135 |
std::string* valuename) |
| 1136 |
{ |
| 1137 |
size_t start = key.find('\\'); |
| 1138 |
if (start == std::string::npos) { |
| 1139 |
return false; |
| 1140 |
} |
| 1141 |
|
| 1142 |
size_t valuenamepos = key.find(';'); |
| 1143 |
if (valuenamepos != std::string::npos && valuename) { |
| 1144 |
*valuename = key.substr(valuenamepos + 1); |
| 1145 |
} |
| 1146 |
|
| 1147 |
second = Encoding::ToWide(key.substr(start + 1, valuenamepos - start - 1)); |
| 1148 |
|
| 1149 |
if (hasPrefix(key, "HKEY_CURRENT_USER", start)) { |
| 1150 |
primaryKey = HKEY_CURRENT_USER; |
| 1151 |
} else if (hasPrefix(key, "HKEY_CURRENT_CONFIG", start)) { |
| 1152 |
primaryKey = HKEY_CURRENT_CONFIG; |
| 1153 |
} else if (hasPrefix(key, "HKEY_CLASSES_ROOT", start)) { |
| 1154 |
primaryKey = HKEY_CLASSES_ROOT; |
| 1155 |
} else if (hasPrefix(key, "HKEY_LOCAL_MACHINE", start)) { |
| 1156 |
primaryKey = HKEY_LOCAL_MACHINE; |
| 1157 |
} else if (hasPrefix(key, "HKEY_USERS", start)) { |
| 1158 |
primaryKey = HKEY_USERS; |
| 1159 |
} |
| 1160 |
|
| 1161 |
return true; |
| 1162 |
} |
| 1163 |
|
| 1164 |
static DWORD SystemToolsMakeRegistryMode(DWORD mode, |
| 1165 |
SystemTools::KeyWOW64 view) |
| 1166 |
{ |
| 1167 |
// only add the modes when on a system that supports Wow64. |
| 1168 |
static FARPROC wow64p = |
| 1169 |
GetProcAddress(GetModuleHandleW(L"kernel32"), "IsWow64Process"); |
| 1170 |
if (wow64p == nullptr) { |
| 1171 |
return mode; |
| 1172 |
} |
| 1173 |
|
| 1174 |
if (view == SystemTools::KeyWOW64_32) { |
| 1175 |
return mode | KWSYS_ST_KEY_WOW64_32KEY; |
| 1176 |
} else if (view == SystemTools::KeyWOW64_64) { |
| 1177 |
return mode | KWSYS_ST_KEY_WOW64_64KEY; |
| 1178 |
} |
| 1179 |
return mode; |
| 1180 |
} |
| 1181 |
#endif |
| 1182 |
|
| 1183 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1184 |
bool SystemTools::GetRegistrySubKeys(const std::string& key, |
| 1185 |
std::vector<std::string>& subkeys, |
| 1186 |
KeyWOW64 view) |
| 1187 |
{ |
| 1188 |
HKEY primaryKey = HKEY_CURRENT_USER; |
| 1189 |
std::wstring second; |
| 1190 |
if (!SystemToolsParseRegistryKey(key, primaryKey, second, nullptr)) { |
| 1191 |
return false; |
| 1192 |
} |
| 1193 |
|
| 1194 |
HKEY hKey; |
| 1195 |
if (RegOpenKeyExW(primaryKey, second.c_str(), 0, |
| 1196 |
SystemToolsMakeRegistryMode(KEY_READ, view), |
| 1197 |
&hKey) != ERROR_SUCCESS) { |
| 1198 |
return false; |
| 1199 |
} else { |
| 1200 |
wchar_t name[1024]; |
| 1201 |
DWORD dwNameSize = sizeof(name) / sizeof(name[0]); |
| 1202 |
|
| 1203 |
DWORD i = 0; |
| 1204 |
while (RegEnumKeyW(hKey, i, name, dwNameSize) == ERROR_SUCCESS) { |
| 1205 |
subkeys.push_back(Encoding::ToNarrow(name)); |
| 1206 |
++i; |
| 1207 |
} |
| 1208 |
|
| 1209 |
RegCloseKey(hKey); |
| 1210 |
} |
| 1211 |
|
| 1212 |
return true; |
| 1213 |
} |
| 1214 |
#else |
| 1215 |
bool SystemTools::GetRegistrySubKeys(const std::string&, |
| 1216 |
std::vector<std::string>&, KeyWOW64) |
| 1217 |
{ |
| 1218 |
return false; |
| 1219 |
} |
| 1220 |
#endif |
| 1221 |
|
| 1222 |
// Read a registry value. |
| 1223 |
// Example : |
| 1224 |
// HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath |
| 1225 |
// => will return the data of the "default" value of the key |
| 1226 |
// HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root |
| 1227 |
// => will return the data of the "Root" value of the key |
| 1228 |
|
| 1229 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1230 |
bool SystemTools::ReadRegistryValue(const std::string& key, std::string& value, |
| 1231 |
KeyWOW64 view) |
| 1232 |
{ |
| 1233 |
bool valueset = false; |
| 1234 |
HKEY primaryKey = HKEY_CURRENT_USER; |
| 1235 |
std::wstring second; |
| 1236 |
std::string valuename; |
| 1237 |
if (!SystemToolsParseRegistryKey(key, primaryKey, second, &valuename)) { |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
|
| 1241 |
HKEY hKey; |
| 1242 |
if (RegOpenKeyExW(primaryKey, second.c_str(), 0, |
| 1243 |
SystemToolsMakeRegistryMode(KEY_READ, view), |
| 1244 |
&hKey) != ERROR_SUCCESS) { |
| 1245 |
return false; |
| 1246 |
} else { |
| 1247 |
DWORD dwType, dwSize; |
| 1248 |
dwSize = 1023; |
| 1249 |
wchar_t data[1024]; |
| 1250 |
if (RegQueryValueExW(hKey, Encoding::ToWide(valuename).c_str(), nullptr, |
| 1251 |
&dwType, (BYTE*)data, &dwSize) == ERROR_SUCCESS) { |
| 1252 |
if (dwType == REG_SZ) { |
| 1253 |
value = Encoding::ToNarrow(data); |
| 1254 |
valueset = true; |
| 1255 |
} else if (dwType == REG_EXPAND_SZ) { |
| 1256 |
wchar_t expanded[1024]; |
| 1257 |
DWORD dwExpandedSize = sizeof(expanded) / sizeof(expanded[0]); |
| 1258 |
if (ExpandEnvironmentStringsW(data, expanded, dwExpandedSize)) { |
| 1259 |
value = Encoding::ToNarrow(expanded); |
| 1260 |
valueset = true; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
RegCloseKey(hKey); |
| 1266 |
} |
| 1267 |
|
| 1268 |
return valueset; |
| 1269 |
} |
| 1270 |
#else |
| 1271 |
bool SystemTools::ReadRegistryValue(const std::string&, std::string&, KeyWOW64) |
| 1272 |
{ |
| 1273 |
return false; |
| 1274 |
} |
| 1275 |
#endif |
| 1276 |
|
| 1277 |
// Write a registry value. |
| 1278 |
// Example : |
| 1279 |
// HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath |
| 1280 |
// => will set the data of the "default" value of the key |
| 1281 |
// HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root |
| 1282 |
// => will set the data of the "Root" value of the key |
| 1283 |
|
| 1284 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1285 |
bool SystemTools::WriteRegistryValue(const std::string& key, |
| 1286 |
const std::string& value, KeyWOW64 view) |
| 1287 |
{ |
| 1288 |
HKEY primaryKey = HKEY_CURRENT_USER; |
| 1289 |
std::wstring second; |
| 1290 |
std::string valuename; |
| 1291 |
if (!SystemToolsParseRegistryKey(key, primaryKey, second, &valuename)) { |
| 1292 |
return false; |
| 1293 |
} |
| 1294 |
|
| 1295 |
HKEY hKey; |
| 1296 |
DWORD dwDummy; |
| 1297 |
wchar_t lpClass[] = L""; |
| 1298 |
if (RegCreateKeyExW(primaryKey, second.c_str(), 0, lpClass, |
| 1299 |
REG_OPTION_NON_VOLATILE, |
| 1300 |
SystemToolsMakeRegistryMode(KEY_WRITE, view), nullptr, |
| 1301 |
&hKey, &dwDummy) != ERROR_SUCCESS) { |
| 1302 |
return false; |
| 1303 |
} |
| 1304 |
|
| 1305 |
std::wstring wvalue = Encoding::ToWide(value); |
| 1306 |
if (RegSetValueExW(hKey, Encoding::ToWide(valuename).c_str(), 0, REG_SZ, |
| 1307 |
(CONST BYTE*)wvalue.c_str(), |
| 1308 |
(DWORD)(sizeof(wchar_t) * (wvalue.size() + 1))) == |
| 1309 |
ERROR_SUCCESS) { |
| 1310 |
return true; |
| 1311 |
} |
| 1312 |
return false; |
| 1313 |
} |
| 1314 |
#else |
| 1315 |
bool SystemTools::WriteRegistryValue(const std::string&, const std::string&, |
| 1316 |
KeyWOW64) |
| 1317 |
{ |
| 1318 |
return false; |
| 1319 |
} |
| 1320 |
#endif |
| 1321 |
|
| 1322 |
// Delete a registry value. |
| 1323 |
// Example : |
| 1324 |
// HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath |
| 1325 |
// => will delete the data of the "default" value of the key |
| 1326 |
// HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root |
| 1327 |
// => will delete the data of the "Root" value of the key |
| 1328 |
|
| 1329 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1330 |
bool SystemTools::DeleteRegistryValue(const std::string& key, KeyWOW64 view) |
| 1331 |
{ |
| 1332 |
HKEY primaryKey = HKEY_CURRENT_USER; |
| 1333 |
std::wstring second; |
| 1334 |
std::string valuename; |
| 1335 |
if (!SystemToolsParseRegistryKey(key, primaryKey, second, &valuename)) { |
| 1336 |
return false; |
| 1337 |
} |
| 1338 |
|
| 1339 |
HKEY hKey; |
| 1340 |
if (RegOpenKeyExW(primaryKey, second.c_str(), 0, |
| 1341 |
SystemToolsMakeRegistryMode(KEY_WRITE, view), |
| 1342 |
&hKey) != ERROR_SUCCESS) { |
| 1343 |
return false; |
| 1344 |
} else { |
| 1345 |
if (RegDeleteValue(hKey, (LPTSTR)valuename.c_str()) == ERROR_SUCCESS) { |
| 1346 |
RegCloseKey(hKey); |
| 1347 |
return true; |
| 1348 |
} |
| 1349 |
} |
| 1350 |
return false; |
| 1351 |
} |
| 1352 |
#else |
| 1353 |
bool SystemTools::DeleteRegistryValue(const std::string&, KeyWOW64) |
| 1354 |
{ |
| 1355 |
return false; |
| 1356 |
} |
| 1357 |
#endif |
| 1358 |
|
| 1359 |
bool SystemTools::SameFile(const std::string& file1, const std::string& file2) |
| 1360 |
{ |
| 1361 |
#ifdef _WIN32 |
| 1362 |
HANDLE hFile1, hFile2; |
| 1363 |
|
| 1364 |
hFile1 = |
| 1365 |
CreateFileW(Encoding::ToWide(file1).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 1366 |
nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 1367 |
hFile2 = |
| 1368 |
CreateFileW(Encoding::ToWide(file2).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 1369 |
nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 1370 |
if (hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE) { |
| 1371 |
if (hFile1 != INVALID_HANDLE_VALUE) { |
| 1372 |
CloseHandle(hFile1); |
| 1373 |
} |
| 1374 |
if (hFile2 != INVALID_HANDLE_VALUE) { |
| 1375 |
CloseHandle(hFile2); |
| 1376 |
} |
| 1377 |
return false; |
| 1378 |
} |
| 1379 |
|
| 1380 |
BY_HANDLE_FILE_INFORMATION fiBuf1; |
| 1381 |
BY_HANDLE_FILE_INFORMATION fiBuf2; |
| 1382 |
GetFileInformationByHandle(hFile1, &fiBuf1); |
| 1383 |
GetFileInformationByHandle(hFile2, &fiBuf2); |
| 1384 |
CloseHandle(hFile1); |
| 1385 |
CloseHandle(hFile2); |
| 1386 |
return (fiBuf1.dwVolumeSerialNumber == fiBuf2.dwVolumeSerialNumber && |
| 1387 |
fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh && |
| 1388 |
fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow); |
| 1389 |
#else |
| 1390 |
struct stat fileStat1, fileStat2; |
| 1391 |
if (stat(file1.c_str(), &fileStat1) == 0 && |
| 1392 |
stat(file2.c_str(), &fileStat2) == 0) { |
| 1393 |
// see if the files are the same file |
| 1394 |
// check the device inode and size |
| 1395 |
if (memcmp(&fileStat2.st_dev, &fileStat1.st_dev, |
| 1396 |
sizeof(fileStat1.st_dev)) == 0 && |
| 1397 |
memcmp(&fileStat2.st_ino, &fileStat1.st_ino, |
| 1398 |
sizeof(fileStat1.st_ino)) == 0 && |
| 1399 |
fileStat2.st_size == fileStat1.st_size) { |
| 1400 |
return true; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
return false; |
| 1404 |
#endif |
| 1405 |
} |
| 1406 |
|
| 1407 |
bool SystemTools::PathExists(const std::string& path) |
| 1408 |
{ |
| 1409 |
if (path.empty()) { |
| 1410 |
return false; |
| 1411 |
} |
| 1412 |
#if defined(_WIN32) |
| 1413 |
return (GetFileAttributesW(Encoding::ToWindowsExtendedPath(path).c_str()) != |
| 1414 |
INVALID_FILE_ATTRIBUTES); |
| 1415 |
#else |
| 1416 |
struct stat st; |
| 1417 |
return lstat(path.c_str(), &st) == 0; |
| 1418 |
#endif |
| 1419 |
} |
| 1420 |
|
| 1421 |
bool SystemTools::FileExists(const char* filename) |
| 1422 |
{ |
| 1423 |
if (!filename) { |
| 1424 |
return false; |
| 1425 |
} |
| 1426 |
return SystemTools::FileExists(std::string(filename)); |
| 1427 |
} |
| 1428 |
|
| 1429 |
bool SystemTools::FileExists(const std::string& filename) |
| 1430 |
{ |
| 1431 |
if (filename.empty()) { |
| 1432 |
return false; |
| 1433 |
} |
| 1434 |
#if defined(_WIN32) |
| 1435 |
const std::wstring path = Encoding::ToWindowsExtendedPath(filename); |
| 1436 |
DWORD attr = GetFileAttributesW(path.c_str()); |
| 1437 |
if (attr == INVALID_FILE_ATTRIBUTES) { |
| 1438 |
return false; |
| 1439 |
} |
| 1440 |
|
| 1441 |
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 1442 |
// Using 0 instead of GENERIC_READ as it allows reading of file attributes |
| 1443 |
// even if we do not have permission to read the file itself |
| 1444 |
HANDLE handle = CreateFileW(path.c_str(), 0, 0, nullptr, OPEN_EXISTING, |
| 1445 |
FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 1446 |
|
| 1447 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1448 |
// A reparse point may be an execution alias (Windows Store app), which |
| 1449 |
// is similar to a symlink but it cannot be opened as a regular file. |
| 1450 |
// We must look at the reparse point data explicitly. |
| 1451 |
handle = CreateFileW( |
| 1452 |
path.c_str(), 0, 0, nullptr, OPEN_EXISTING, |
| 1453 |
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 1454 |
|
| 1455 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1456 |
return false; |
| 1457 |
} |
| 1458 |
|
| 1459 |
byte buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 1460 |
DWORD bytesReturned = 0; |
| 1461 |
|
| 1462 |
if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, nullptr, 0, buffer, |
| 1463 |
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bytesReturned, |
| 1464 |
nullptr)) { |
| 1465 |
CloseHandle(handle); |
| 1466 |
return false; |
| 1467 |
} |
| 1468 |
|
| 1469 |
CloseHandle(handle); |
| 1470 |
|
| 1471 |
PREPARSE_DATA_BUFFER data = |
| 1472 |
reinterpret_cast<PREPARSE_DATA_BUFFER>(&buffer[0]); |
| 1473 |
|
| 1474 |
// Assume that file exists if it is an execution alias. |
| 1475 |
return data->ReparseTag == IO_REPARSE_TAG_APPEXECLINK; |
| 1476 |
} |
| 1477 |
|
| 1478 |
CloseHandle(handle); |
| 1479 |
} |
| 1480 |
|
| 1481 |
return true; |
| 1482 |
#else |
| 1483 |
// SCO OpenServer 5.0.7/3.2's command has 711 permission. |
| 1484 |
# if defined(_SCO_DS) |
| 1485 |
return access(filename.c_str(), F_OK) == 0; |
| 1486 |
# else |
| 1487 |
return access(filename.c_str(), R_OK) == 0; |
| 1488 |
# endif |
| 1489 |
#endif |
| 1490 |
} |
| 1491 |
|
| 1492 |
bool SystemTools::FileExists(const char* filename, bool isFile) |
| 1493 |
{ |
| 1494 |
if (!filename) { |
| 1495 |
return false; |
| 1496 |
} |
| 1497 |
return SystemTools::FileExists(std::string(filename), isFile); |
| 1498 |
} |
| 1499 |
|
| 1500 |
bool SystemTools::FileExists(const std::string& filename, bool isFile) |
| 1501 |
{ |
| 1502 |
if (SystemTools::FileExists(filename)) { |
| 1503 |
// If isFile is set return not FileIsDirectory, |
| 1504 |
// so this will only be true if it is a file |
| 1505 |
return !isFile || !SystemTools::FileIsDirectory(filename); |
| 1506 |
} |
| 1507 |
return false; |
| 1508 |
} |
| 1509 |
|
| 1510 |
bool SystemTools::TestFileAccess(const char* filename, |
| 1511 |
TestFilePermissions permissions) |
| 1512 |
{ |
| 1513 |
if (!filename) { |
| 1514 |
return false; |
| 1515 |
} |
| 1516 |
return SystemTools::TestFileAccess(std::string(filename), permissions); |
| 1517 |
} |
| 1518 |
|
| 1519 |
bool SystemTools::TestFileAccess(const std::string& filename, |
| 1520 |
TestFilePermissions permissions) |
| 1521 |
{ |
| 1522 |
if (filename.empty()) { |
| 1523 |
return false; |
| 1524 |
} |
| 1525 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1526 |
// If execute set, change to read permission (all files on Windows |
| 1527 |
// are executable if they are readable). The CRT will always fail |
| 1528 |
// if you pass an execute bit. |
| 1529 |
if (permissions & TEST_FILE_EXECUTE) { |
| 1530 |
permissions &= ~TEST_FILE_EXECUTE; |
| 1531 |
permissions |= TEST_FILE_READ; |
| 1532 |
} |
| 1533 |
return _waccess(Encoding::ToWindowsExtendedPath(filename).c_str(), |
| 1534 |
permissions) == 0; |
| 1535 |
#else |
| 1536 |
return access(filename.c_str(), permissions) == 0; |
| 1537 |
#endif |
| 1538 |
} |
| 1539 |
|
| 1540 |
int SystemTools::Stat(const char* path, SystemTools::Stat_t* buf) |
| 1541 |
{ |
| 1542 |
if (!path) { |
| 1543 |
errno = EFAULT; |
| 1544 |
return -1; |
| 1545 |
} |
| 1546 |
return SystemTools::Stat(std::string(path), buf); |
| 1547 |
} |
| 1548 |
|
| 1549 |
int SystemTools::Stat(const std::string& path, SystemTools::Stat_t* buf) |
| 1550 |
{ |
| 1551 |
if (path.empty()) { |
| 1552 |
errno = ENOENT; |
| 1553 |
return -1; |
| 1554 |
} |
| 1555 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1556 |
// Ideally we should use Encoding::ToWindowsExtendedPath to support |
| 1557 |
// long paths, but _wstat64 rejects paths with '?' in them, thinking |
| 1558 |
// they are wildcards. |
| 1559 |
std::wstring const& wpath = Encoding::ToWide(path); |
| 1560 |
return _wstat64(wpath.c_str(), buf); |
| 1561 |
#else |
| 1562 |
return stat(path.c_str(), buf); |
| 1563 |
#endif |
| 1564 |
} |
| 1565 |
|
| 1566 |
Status SystemTools::Touch(std::string const& filename, bool create) |
| 1567 |
{ |
| 1568 |
if (!SystemTools::FileExists(filename)) { |
| 1569 |
if (create) { |
| 1570 |
FILE* file = Fopen(filename, "a+b"); |
| 1571 |
if (file) { |
| 1572 |
fclose(file); |
| 1573 |
return Status::Success(); |
| 1574 |
} |
| 1575 |
return Status::POSIX_errno(); |
| 1576 |
} else { |
| 1577 |
return Status::Success(); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 1581 |
HANDLE h = CreateFileW(Encoding::ToWindowsExtendedPath(filename).c_str(), |
| 1582 |
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, |
| 1583 |
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); |
| 1584 |
if (!h) { |
| 1585 |
return Status::Windows_GetLastError(); |
| 1586 |
} |
| 1587 |
FILETIME mtime; |
| 1588 |
GetSystemTimeAsFileTime(&mtime); |
| 1589 |
if (!SetFileTime(h, 0, 0, &mtime)) { |
| 1590 |
Status status = Status::Windows_GetLastError(); |
| 1591 |
CloseHandle(h); |
| 1592 |
return status; |
| 1593 |
} |
| 1594 |
CloseHandle(h); |
| 1595 |
#elif KWSYS_CXX_HAS_UTIMENSAT |
| 1596 |
// utimensat is only available on newer Unixes and macOS 10.13+ |
| 1597 |
if (utimensat(AT_FDCWD, filename.c_str(), nullptr, 0) < 0) { |
| 1598 |
return Status::POSIX_errno(); |
| 1599 |
} |
| 1600 |
#else |
| 1601 |
// fall back to utimes |
| 1602 |
if (utimes(filename.c_str(), nullptr) < 0) { |
| 1603 |
return Status::POSIX_errno(); |
| 1604 |
} |
| 1605 |
#endif |
| 1606 |
return Status::Success(); |
| 1607 |
} |
| 1608 |
|
| 1609 |
Status SystemTools::FileTimeCompare(std::string const& f1, |
| 1610 |
std::string const& f2, int* result) |
| 1611 |
{ |
| 1612 |
// Default to same time. |
| 1613 |
*result = 0; |
| 1614 |
#if !defined(_WIN32) || defined(__CYGWIN__) |
| 1615 |
// POSIX version. Use stat function to get file modification time. |
| 1616 |
struct stat s1; |
| 1617 |
if (stat(f1.c_str(), &s1) != 0) { |
| 1618 |
return Status::POSIX_errno(); |
| 1619 |
} |
| 1620 |
struct stat s2; |
| 1621 |
if (stat(f2.c_str(), &s2) != 0) { |
| 1622 |
return Status::POSIX_errno(); |
| 1623 |
} |
| 1624 |
# if KWSYS_CXX_STAT_HAS_ST_MTIM |
| 1625 |
// Compare using nanosecond resolution. |
| 1626 |
if (s1.st_mtim.tv_sec < s2.st_mtim.tv_sec) { |
| 1627 |
*result = -1; |
| 1628 |
} else if (s1.st_mtim.tv_sec > s2.st_mtim.tv_sec) { |
| 1629 |
*result = 1; |
| 1630 |
} else if (s1.st_mtim.tv_nsec < s2.st_mtim.tv_nsec) { |
| 1631 |
*result = -1; |
| 1632 |
} else if (s1.st_mtim.tv_nsec > s2.st_mtim.tv_nsec) { |
| 1633 |
*result = 1; |
| 1634 |
} |
| 1635 |
# elif KWSYS_CXX_STAT_HAS_ST_MTIMESPEC |
| 1636 |
// Compare using nanosecond resolution. |
| 1637 |
if (s1.st_mtimespec.tv_sec < s2.st_mtimespec.tv_sec) { |
| 1638 |
*result = -1; |
| 1639 |
} else if (s1.st_mtimespec.tv_sec > s2.st_mtimespec.tv_sec) { |
| 1640 |
*result = 1; |
| 1641 |
} else if (s1.st_mtimespec.tv_nsec < s2.st_mtimespec.tv_nsec) { |
| 1642 |
*result = -1; |
| 1643 |
} else if (s1.st_mtimespec.tv_nsec > s2.st_mtimespec.tv_nsec) { |
| 1644 |
*result = 1; |
| 1645 |
} |
| 1646 |
# else |
| 1647 |
// Compare using 1 second resolution. |
| 1648 |
if (s1.st_mtime < s2.st_mtime) { |
| 1649 |
*result = -1; |
| 1650 |
} else if (s1.st_mtime > s2.st_mtime) { |
| 1651 |
*result = 1; |
| 1652 |
} |
| 1653 |
# endif |
| 1654 |
#else |
| 1655 |
// Windows version. Get the modification time from extended file attributes. |
| 1656 |
WIN32_FILE_ATTRIBUTE_DATA f1d; |
| 1657 |
WIN32_FILE_ATTRIBUTE_DATA f2d; |
| 1658 |
if (!GetFileAttributesExW(Encoding::ToWindowsExtendedPath(f1).c_str(), |
| 1659 |
GetFileExInfoStandard, &f1d)) { |
| 1660 |
return Status::Windows_GetLastError(); |
| 1661 |
} |
| 1662 |
if (!GetFileAttributesExW(Encoding::ToWindowsExtendedPath(f2).c_str(), |
| 1663 |
GetFileExInfoStandard, &f2d)) { |
| 1664 |
return Status::Windows_GetLastError(); |
| 1665 |
} |
| 1666 |
|
| 1667 |
// Compare the file times using resolution provided by system call. |
| 1668 |
*result = (int)CompareFileTime(&f1d.ftLastWriteTime, &f2d.ftLastWriteTime); |
| 1669 |
#endif |
| 1670 |
return Status::Success(); |
| 1671 |
} |
| 1672 |
|
| 1673 |
// Return a capitalized string (i.e the first letter is uppercased, all other |
| 1674 |
// are lowercased) |
| 1675 |
std::string SystemTools::Capitalized(const std::string& s) |
| 1676 |
{ |
| 1677 |
std::string n; |
| 1678 |
if (s.empty()) { |
| 1679 |
return n; |
| 1680 |
} |
| 1681 |
n.resize(s.size()); |
| 1682 |
n[0] = static_cast<std::string::value_type>(toupper(s[0])); |
| 1683 |
for (size_t i = 1; i < s.size(); i++) { |
| 1684 |
n[i] = static_cast<std::string::value_type>(tolower(s[i])); |
| 1685 |
} |
| 1686 |
return n; |
| 1687 |
} |
| 1688 |
|
| 1689 |
// Return capitalized words |
| 1690 |
std::string SystemTools::CapitalizedWords(const std::string& s) |
| 1691 |
{ |
| 1692 |
std::string n(s); |
| 1693 |
for (size_t i = 0; i < s.size(); i++) { |
| 1694 |
#if defined(_MSC_VER) && defined(_MT) && defined(_DEBUG) |
| 1695 |
// MS has an assert that will fail if s[i] < 0; setting |
| 1696 |
// LC_CTYPE using setlocale() does *not* help. Painful. |
| 1697 |
if ((int)s[i] >= 0 && isalpha(s[i]) && |
| 1698 |
(i == 0 || ((int)s[i - 1] >= 0 && isspace(s[i - 1])))) |
| 1699 |
#else |
| 1700 |
if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1]))) |
| 1701 |
#endif |
| 1702 |
{ |
| 1703 |
n[i] = static_cast<std::string::value_type>(toupper(s[i])); |
| 1704 |
} |
| 1705 |
} |
| 1706 |
return n; |
| 1707 |
} |
| 1708 |
|
| 1709 |
// Return uncapitalized words |
| 1710 |
std::string SystemTools::UnCapitalizedWords(const std::string& s) |
| 1711 |
{ |
| 1712 |
std::string n(s); |
| 1713 |
for (size_t i = 0; i < s.size(); i++) { |
| 1714 |
#if defined(_MSC_VER) && defined(_MT) && defined(_DEBUG) |
| 1715 |
// MS has an assert that will fail if s[i] < 0; setting |
| 1716 |
// LC_CTYPE using setlocale() does *not* help. Painful. |
| 1717 |
if ((int)s[i] >= 0 && isalpha(s[i]) && |
| 1718 |
(i == 0 || ((int)s[i - 1] >= 0 && isspace(s[i - 1])))) |
| 1719 |
#else |
| 1720 |
if (isalpha(s[i]) && (i == 0 || isspace(s[i - 1]))) |
| 1721 |
#endif |
| 1722 |
{ |
| 1723 |
n[i] = static_cast<std::string::value_type>(tolower(s[i])); |
| 1724 |
} |
| 1725 |
} |
| 1726 |
return n; |
| 1727 |
} |
| 1728 |
|
| 1729 |
// only works for words with at least two letters |
| 1730 |
std::string SystemTools::AddSpaceBetweenCapitalizedWords(const std::string& s) |
| 1731 |
{ |
| 1732 |
std::string n; |
| 1733 |
if (!s.empty()) { |
| 1734 |
n.reserve(s.size()); |
| 1735 |
n += s[0]; |
| 1736 |
for (size_t i = 1; i < s.size(); i++) { |
| 1737 |
if (isupper(s[i]) && !isspace(s[i - 1]) && !isupper(s[i - 1])) { |
| 1738 |
n += ' '; |
| 1739 |
} |
| 1740 |
n += s[i]; |
| 1741 |
} |
| 1742 |
} |
| 1743 |
return n; |
| 1744 |
} |
| 1745 |
|
| 1746 |
char* SystemTools::AppendStrings(const char* str1, const char* str2) |
| 1747 |
{ |
| 1748 |
if (!str1) { |
| 1749 |
return SystemTools::DuplicateString(str2); |
| 1750 |
} |
| 1751 |
if (!str2) { |
| 1752 |
return SystemTools::DuplicateString(str1); |
| 1753 |
} |
| 1754 |
size_t len1 = strlen(str1); |
| 1755 |
char* newstr = new char[len1 + strlen(str2) + 1]; |
| 1756 |
if (!newstr) { |
| 1757 |
return nullptr; |
| 1758 |
} |
| 1759 |
strcpy(newstr, str1); |
| 1760 |
strcat(newstr + len1, str2); |
| 1761 |
return newstr; |
| 1762 |
} |
| 1763 |
|
| 1764 |
char* SystemTools::AppendStrings(const char* str1, const char* str2, |
| 1765 |
const char* str3) |
| 1766 |
{ |
| 1767 |
if (!str1) { |
| 1768 |
return SystemTools::AppendStrings(str2, str3); |
| 1769 |
} |
| 1770 |
if (!str2) { |
| 1771 |
return SystemTools::AppendStrings(str1, str3); |
| 1772 |
} |
| 1773 |
if (!str3) { |
| 1774 |
return SystemTools::AppendStrings(str1, str2); |
| 1775 |
} |
| 1776 |
|
| 1777 |
size_t len1 = strlen(str1), len2 = strlen(str2); |
| 1778 |
char* newstr = new char[len1 + len2 + strlen(str3) + 1]; |
| 1779 |
if (!newstr) { |
| 1780 |
return nullptr; |
| 1781 |
} |
| 1782 |
strcpy(newstr, str1); |
| 1783 |
strcat(newstr + len1, str2); |
| 1784 |
strcat(newstr + len1 + len2, str3); |
| 1785 |
return newstr; |
| 1786 |
} |
| 1787 |
|
| 1788 |
// Return a lower case string |
| 1789 |
std::string SystemTools::LowerCase(const std::string& s) |
| 1790 |
{ |
| 1791 |
std::string n; |
| 1792 |
n.resize(s.size()); |
| 1793 |
for (size_t i = 0; i < s.size(); i++) { |
| 1794 |
n[i] = static_cast<std::string::value_type>(tolower(s[i])); |
| 1795 |
} |
| 1796 |
return n; |
| 1797 |
} |
| 1798 |
|
| 1799 |
// Return a lower case string |
| 1800 |
std::string SystemTools::UpperCase(const std::string& s) |
| 1801 |
{ |
| 1802 |
std::string n; |
| 1803 |
n.resize(s.size()); |
| 1804 |
for (size_t i = 0; i < s.size(); i++) { |
| 1805 |
n[i] = static_cast<std::string::value_type>(toupper(s[i])); |
| 1806 |
} |
| 1807 |
return n; |
| 1808 |
} |
| 1809 |
|
| 1810 |
// Count char in string |
| 1811 |
size_t SystemTools::CountChar(const char* str, char c) |
| 1812 |
{ |
| 1813 |
size_t count = 0; |
| 1814 |
|
| 1815 |
if (str) { |
| 1816 |
while (*str) { |
| 1817 |
if (*str == c) { |
| 1818 |
++count; |
| 1819 |
} |
| 1820 |
++str; |
| 1821 |
} |
| 1822 |
} |
| 1823 |
return count; |
| 1824 |
} |
| 1825 |
|
| 1826 |
// Remove chars in string |
| 1827 |
char* SystemTools::RemoveChars(const char* str, const char* toremove) |
| 1828 |
{ |
| 1829 |
if (!str) { |
| 1830 |
return nullptr; |
| 1831 |
} |
| 1832 |
char* clean_str = new char[strlen(str) + 1]; |
| 1833 |
char* ptr = clean_str; |
| 1834 |
while (*str) { |
| 1835 |
const char* str2 = toremove; |
| 1836 |
while (*str2 && *str != *str2) { |
| 1837 |
++str2; |
| 1838 |
} |
| 1839 |
if (!*str2) { |
| 1840 |
*ptr++ = *str; |
| 1841 |
} |
| 1842 |
++str; |
| 1843 |
} |
| 1844 |
*ptr = '\0'; |
| 1845 |
return clean_str; |
| 1846 |
} |
| 1847 |
|
| 1848 |
// Remove chars in string |
| 1849 |
char* SystemTools::RemoveCharsButUpperHex(const char* str) |
| 1850 |
{ |
| 1851 |
if (!str) { |
| 1852 |
return nullptr; |
| 1853 |
} |
| 1854 |
char* clean_str = new char[strlen(str) + 1]; |
| 1855 |
char* ptr = clean_str; |
| 1856 |
while (*str) { |
| 1857 |
if ((*str >= '0' && *str <= '9') || (*str >= 'A' && *str <= 'F')) { |
| 1858 |
*ptr++ = *str; |
| 1859 |
} |
| 1860 |
++str; |
| 1861 |
} |
| 1862 |
*ptr = '\0'; |
| 1863 |
return clean_str; |
| 1864 |
} |
| 1865 |
|
| 1866 |
// Replace chars in string |
| 1867 |
char* SystemTools::ReplaceChars(char* str, const char* toreplace, |
| 1868 |
char replacement) |
| 1869 |
{ |
| 1870 |
if (str) { |
| 1871 |
char* ptr = str; |
| 1872 |
while (*ptr) { |
| 1873 |
const char* ptr2 = toreplace; |
| 1874 |
while (*ptr2) { |
| 1875 |
if (*ptr == *ptr2) { |
| 1876 |
*ptr = replacement; |
| 1877 |
} |
| 1878 |
++ptr2; |
| 1879 |
} |
| 1880 |
++ptr; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
return str; |
| 1884 |
} |
| 1885 |
|
| 1886 |
// Returns if string starts with another string |
| 1887 |
bool SystemTools::StringStartsWith(const char* str1, const char* str2) |
| 1888 |
{ |
| 1889 |
if (!str1 || !str2) { |
| 1890 |
return false; |
| 1891 |
} |
| 1892 |
size_t len1 = strlen(str1), len2 = strlen(str2); |
| 1893 |
return len1 >= len2 && !strncmp(str1, str2, len2) ? true : false; |
| 1894 |
} |
| 1895 |
|
| 1896 |
// Returns if string starts with another string |
| 1897 |
bool SystemTools::StringStartsWith(const std::string& str1, const char* str2) |
| 1898 |
{ |
| 1899 |
if (!str2) { |
| 1900 |
return false; |
| 1901 |
} |
| 1902 |
size_t len1 = str1.size(), len2 = strlen(str2); |
| 1903 |
return len1 >= len2 && !strncmp(str1.c_str(), str2, len2) ? true : false; |
| 1904 |
} |
| 1905 |
|
| 1906 |
// Returns if string ends with another string |
| 1907 |
bool SystemTools::StringEndsWith(const char* str1, const char* str2) |
| 1908 |
{ |
| 1909 |
if (!str1 || !str2) { |
| 1910 |
return false; |
| 1911 |
} |
| 1912 |
size_t len1 = strlen(str1), len2 = strlen(str2); |
| 1913 |
return len1 >= len2 && !strncmp(str1 + (len1 - len2), str2, len2) ? true |
| 1914 |
: false; |
| 1915 |
} |
| 1916 |
|
| 1917 |
// Returns if string ends with another string |
| 1918 |
bool SystemTools::StringEndsWith(const std::string& str1, const char* str2) |
| 1919 |
{ |
| 1920 |
if (!str2) { |
| 1921 |
return false; |
| 1922 |
} |
| 1923 |
size_t len1 = str1.size(), len2 = strlen(str2); |
| 1924 |
return len1 >= len2 && !strncmp(str1.c_str() + (len1 - len2), str2, len2) |
| 1925 |
? true |
| 1926 |
: false; |
| 1927 |
} |
| 1928 |
|
| 1929 |
// Returns a pointer to the last occurrence of str2 in str1 |
| 1930 |
const char* SystemTools::FindLastString(const char* str1, const char* str2) |
| 1931 |
{ |
| 1932 |
if (!str1 || !str2) { |
| 1933 |
return nullptr; |
| 1934 |
} |
| 1935 |
|
| 1936 |
size_t len1 = strlen(str1), len2 = strlen(str2); |
| 1937 |
if (len1 >= len2) { |
| 1938 |
const char* ptr = str1 + len1 - len2; |
| 1939 |
do { |
| 1940 |
if (!strncmp(ptr, str2, len2)) { |
| 1941 |
return ptr; |
| 1942 |
} |
| 1943 |
} while (ptr-- != str1); |
| 1944 |
} |
| 1945 |
|
| 1946 |
return nullptr; |
| 1947 |
} |
| 1948 |
|
| 1949 |
// Duplicate string |
| 1950 |
char* SystemTools::DuplicateString(const char* str) |
| 1951 |
{ |
| 1952 |
if (str) { |
| 1953 |
char* newstr = new char[strlen(str) + 1]; |
| 1954 |
return strcpy(newstr, str); |
| 1955 |
} |
| 1956 |
return nullptr; |
| 1957 |
} |
| 1958 |
|
| 1959 |
// Return a cropped string |
| 1960 |
std::string SystemTools::CropString(const std::string& s, size_t max_len) |
| 1961 |
{ |
| 1962 |
if (s.empty() || max_len == 0 || max_len >= s.size()) { |
| 1963 |
return s; |
| 1964 |
} |
| 1965 |
|
| 1966 |
std::string n; |
| 1967 |
n.reserve(max_len); |
| 1968 |
|
| 1969 |
size_t middle = max_len / 2; |
| 1970 |
|
| 1971 |
n.assign(s, 0, middle); |
| 1972 |
n += s.substr(s.size() - (max_len - middle)); |
| 1973 |
|
| 1974 |
if (max_len > 2) { |
| 1975 |
n[middle] = '.'; |
| 1976 |
if (max_len > 3) { |
| 1977 |
n[middle - 1] = '.'; |
| 1978 |
if (max_len > 4) { |
| 1979 |
n[middle + 1] = '.'; |
| 1980 |
} |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
return n; |
| 1985 |
} |
| 1986 |
|
| 1987 |
std::vector<std::string> SystemTools::SplitString(const std::string& p, |
| 1988 |
char sep, bool isPath) |
| 1989 |
{ |
| 1990 |
std::string path = p; |
| 1991 |
std::vector<std::string> paths; |
| 1992 |
if (path.empty()) { |
| 1993 |
return paths; |
| 1994 |
} |
| 1995 |
if (isPath && path[0] == '/') { |
| 1996 |
path.erase(path.begin()); |
| 1997 |
paths.emplace_back("/"); |
| 1998 |
} |
| 1999 |
std::string::size_type pos1 = 0; |
| 2000 |
std::string::size_type pos2 = path.find(sep, pos1); |
| 2001 |
while (pos2 != std::string::npos) { |
| 2002 |
paths.push_back(path.substr(pos1, pos2 - pos1)); |
| 2003 |
pos1 = pos2 + 1; |
| 2004 |
pos2 = path.find(sep, pos1 + 1); |
| 2005 |
} |
| 2006 |
paths.push_back(path.substr(pos1, pos2 - pos1)); |
| 2007 |
|
| 2008 |
return paths; |
| 2009 |
} |
| 2010 |
|
| 2011 |
int SystemTools::EstimateFormatLength(const char* format, va_list ap) |
| 2012 |
{ |
| 2013 |
if (!format) { |
| 2014 |
return 0; |
| 2015 |
} |
| 2016 |
|
| 2017 |
// Quick-hack attempt at estimating the length of the string. |
| 2018 |
// Should never under-estimate. |
| 2019 |
|
| 2020 |
// Start with the length of the format string itself. |
| 2021 |
|
| 2022 |
size_t length = strlen(format); |
| 2023 |
|
| 2024 |
// Increase the length for every argument in the format. |
| 2025 |
|
| 2026 |
const char* cur = format; |
| 2027 |
while (*cur) { |
| 2028 |
if (*cur++ == '%') { |
| 2029 |
// Skip "%%" since it doesn't correspond to a va_arg. |
| 2030 |
if (*cur != '%') { |
| 2031 |
while (!int(isalpha(*cur))) { |
| 2032 |
++cur; |
| 2033 |
} |
| 2034 |
switch (*cur) { |
| 2035 |
case 's': { |
| 2036 |
// Check the length of the string. |
| 2037 |
char* s = va_arg(ap, char*); |
| 2038 |
if (s) { |
| 2039 |
length += strlen(s); |
| 2040 |
} |
| 2041 |
} break; |
| 2042 |
case 'e': |
| 2043 |
case 'f': |
| 2044 |
case 'g': { |
| 2045 |
// Assume the argument contributes no more than 64 characters. |
| 2046 |
length += 64; |
| 2047 |
|
| 2048 |
// Eat the argument. |
| 2049 |
static_cast<void>(va_arg(ap, double)); |
| 2050 |
} break; |
| 2051 |
default: { |
| 2052 |
// Assume the argument contributes no more than 64 characters. |
| 2053 |
length += 64; |
| 2054 |
|
| 2055 |
// Eat the argument. |
| 2056 |
static_cast<void>(va_arg(ap, int)); |
| 2057 |
} break; |
| 2058 |
} |
| 2059 |
} |
| 2060 |
|
| 2061 |
// Move past the characters just tested. |
| 2062 |
++cur; |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
return static_cast<int>(length); |
| 2067 |
} |
| 2068 |
|
| 2069 |
std::string SystemTools::EscapeChars(const char* str, |
| 2070 |
const char* chars_to_escape, |
| 2071 |
char escape_char) |
| 2072 |
{ |
| 2073 |
std::string n; |
| 2074 |
if (str) { |
| 2075 |
if (!chars_to_escape || !*chars_to_escape) { |
| 2076 |
n.append(str); |
| 2077 |
} else { |
| 2078 |
n.reserve(strlen(str)); |
| 2079 |
while (*str) { |
| 2080 |
const char* ptr = chars_to_escape; |
| 2081 |
while (*ptr) { |
| 2082 |
if (*str == *ptr) { |
| 2083 |
n += escape_char; |
| 2084 |
break; |
| 2085 |
} |
| 2086 |
++ptr; |
| 2087 |
} |
| 2088 |
n += *str; |
| 2089 |
++str; |
| 2090 |
} |
| 2091 |
} |
| 2092 |
} |
| 2093 |
return n; |
| 2094 |
} |
| 2095 |
|
| 2096 |
#ifdef __VMS |
| 2097 |
static void ConvertVMSToUnix(std::string& path) |
| 2098 |
{ |
| 2099 |
std::string::size_type rootEnd = path.find(":["); |
| 2100 |
std::string::size_type pathEnd = path.find("]"); |
| 2101 |
if (rootEnd != std::string::npos) { |
| 2102 |
std::string root = path.substr(0, rootEnd); |
| 2103 |
std::string pathPart = path.substr(rootEnd + 2, pathEnd - rootEnd - 2); |
| 2104 |
const char* pathCString = pathPart.c_str(); |
| 2105 |
const char* pos0 = pathCString; |
| 2106 |
for (std::string::size_type pos = 0; *pos0; ++pos) { |
| 2107 |
if (*pos0 == '.') { |
| 2108 |
pathPart[pos] = '/'; |
| 2109 |
} |
| 2110 |
pos0++; |
| 2111 |
} |
| 2112 |
path = "/" + root + "/" + pathPart; |
| 2113 |
} |
| 2114 |
} |
| 2115 |
#endif |
| 2116 |
|
| 2117 |
// convert windows slashes to unix slashes |
| 2118 |
void SystemTools::ConvertToUnixSlashes(std::string& path) |
| 2119 |
{ |
| 2120 |
if (path.empty()) { |
| 2121 |
return; |
| 2122 |
} |
| 2123 |
|
| 2124 |
const char* pathCString = path.c_str(); |
| 2125 |
bool hasDoubleSlash = false; |
| 2126 |
#ifdef __VMS |
| 2127 |
ConvertVMSToUnix(path); |
| 2128 |
#else |
| 2129 |
const char* pos0 = pathCString; |
| 2130 |
for (std::string::size_type pos = 0; *pos0; ++pos) { |
| 2131 |
if (*pos0 == '\\') { |
| 2132 |
path[pos] = '/'; |
| 2133 |
} |
| 2134 |
|
| 2135 |
// Also, reuse the loop to check for slash followed by another slash |
| 2136 |
if (!hasDoubleSlash && *(pos0 + 1) == '/' && *(pos0 + 2) == '/') { |
| 2137 |
# ifdef _WIN32 |
| 2138 |
// However, on windows if the first characters are both slashes, |
| 2139 |
// then keep them that way, so that network paths can be handled. |
| 2140 |
if (pos > 0) { |
| 2141 |
hasDoubleSlash = true; |
| 2142 |
} |
| 2143 |
# else |
| 2144 |
hasDoubleSlash = true; |
| 2145 |
# endif |
| 2146 |
} |
| 2147 |
|
| 2148 |
pos0++; |
| 2149 |
} |
| 2150 |
|
| 2151 |
if (hasDoubleSlash) { |
| 2152 |
SystemTools::ReplaceString(path, "//", "/"); |
| 2153 |
} |
| 2154 |
#endif |
| 2155 |
|
| 2156 |
// remove any trailing slash |
| 2157 |
// if there is a tilda ~ then replace it with HOME |
| 2158 |
pathCString = path.c_str(); |
| 2159 |
if (pathCString[0] == '~' && |
| 2160 |
(pathCString[1] == '/' || pathCString[1] == '\0')) { |
| 2161 |
std::string homeEnv; |
| 2162 |
if (SystemTools::GetEnv("HOME", homeEnv)) { |
| 2163 |
path.replace(0, 1, homeEnv); |
| 2164 |
} |
| 2165 |
} |
| 2166 |
#ifdef HAVE_GETPWNAM |
| 2167 |
else if (pathCString[0] == '~') { |
| 2168 |
std::string::size_type idx = path.find_first_of("/\0"); |
| 2169 |
char oldch = path[idx]; |
| 2170 |
path[idx] = '\0'; |
| 2171 |
passwd* pw = getpwnam(path.c_str() + 1); |
| 2172 |
path[idx] = oldch; |
| 2173 |
if (pw) { |
| 2174 |
path.replace(0, idx, pw->pw_dir); |
| 2175 |
} |
| 2176 |
} |
| 2177 |
#endif |
| 2178 |
// remove trailing slash if the path is more than |
| 2179 |
// a single / |
| 2180 |
pathCString = path.c_str(); |
| 2181 |
size_t size = path.size(); |
| 2182 |
if (size > 1 && path.back() == '/') { |
| 2183 |
// if it is c:/ then do not remove the trailing slash |
| 2184 |
if (!((size == 3 && pathCString[1] == ':'))) { |
| 2185 |
path.resize(size - 1); |
| 2186 |
} |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
#ifdef _WIN32 |
| 2191 |
std::wstring SystemTools::ConvertToWindowsExtendedPath( |
| 2192 |
const std::string& source) |
| 2193 |
{ |
| 2194 |
return Encoding::ToWindowsExtendedPath(source); |
| 2195 |
} |
| 2196 |
#endif |
| 2197 |
|
| 2198 |
// change // to /, and escape any spaces in the path |
| 2199 |
std::string SystemTools::ConvertToUnixOutputPath(const std::string& path) |
| 2200 |
{ |
| 2201 |
std::string ret = path; |
| 2202 |
|
| 2203 |
// remove // except at the beginning might be a cygwin drive |
| 2204 |
std::string::size_type pos = 1; |
| 2205 |
while ((pos = ret.find("//", pos)) != std::string::npos) { |
| 2206 |
ret.erase(pos, 1); |
| 2207 |
} |
| 2208 |
// escape spaces and () in the path |
| 2209 |
if (ret.find_first_of(' ') != std::string::npos) { |
| 2210 |
std::string result; |
| 2211 |
char lastch = 1; |
| 2212 |
for (const char* ch = ret.c_str(); *ch != '\0'; ++ch) { |
| 2213 |
// if it is already escaped then don't try to escape it again |
| 2214 |
if ((*ch == ' ') && lastch != '\\') { |
| 2215 |
result += '\\'; |
| 2216 |
} |
| 2217 |
result += *ch; |
| 2218 |
lastch = *ch; |
| 2219 |
} |
| 2220 |
ret = result; |
| 2221 |
} |
| 2222 |
return ret; |
| 2223 |
} |
| 2224 |
|
| 2225 |
std::string SystemTools::ConvertToOutputPath(const std::string& path) |
| 2226 |
{ |
| 2227 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 2228 |
return SystemTools::ConvertToWindowsOutputPath(path); |
| 2229 |
#else |
| 2230 |
return SystemTools::ConvertToUnixOutputPath(path); |
| 2231 |
#endif |
| 2232 |
} |
| 2233 |
|
| 2234 |
// remove double slashes not at the start |
| 2235 |
std::string SystemTools::ConvertToWindowsOutputPath(const std::string& path) |
| 2236 |
{ |
| 2237 |
std::string ret; |
| 2238 |
// make it big enough for all of path and double quotes |
| 2239 |
ret.reserve(path.size() + 3); |
| 2240 |
// put path into the string |
| 2241 |
ret = path; |
| 2242 |
std::string::size_type pos = 0; |
| 2243 |
// first convert all of the slashes |
| 2244 |
while ((pos = ret.find('/', pos)) != std::string::npos) { |
| 2245 |
ret[pos] = '\\'; |
| 2246 |
pos++; |
| 2247 |
} |
| 2248 |
// check for really small paths |
| 2249 |
if (ret.size() < 2) { |
| 2250 |
return ret; |
| 2251 |
} |
| 2252 |
// now clean up a bit and remove double slashes |
| 2253 |
// Only if it is not the first position in the path which is a network |
| 2254 |
// path on windows |
| 2255 |
pos = 1; // start at position 1 |
| 2256 |
if (ret[0] == '\"') { |
| 2257 |
pos = 2; // if the string is already quoted then start at 2 |
| 2258 |
if (ret.size() < 3) { |
| 2259 |
return ret; |
| 2260 |
} |
| 2261 |
} |
| 2262 |
while ((pos = ret.find("\\\\", pos)) != std::string::npos) { |
| 2263 |
ret.erase(pos, 1); |
| 2264 |
} |
| 2265 |
// now double quote the path if it has spaces in it |
| 2266 |
// and is not already double quoted |
| 2267 |
if (ret.find(' ') != std::string::npos && ret[0] != '\"') { |
| 2268 |
ret.insert(static_cast<std::string::size_type>(0), |
| 2269 |
static_cast<std::string::size_type>(1), '\"'); |
| 2270 |
ret.append(1, '\"'); |
| 2271 |
} |
| 2272 |
return ret; |
| 2273 |
} |
| 2274 |
|
| 2275 |
/** |
| 2276 |
* Append the filename from the path source to the directory name dir. |
| 2277 |
*/ |
| 2278 |
static std::string FileInDir(const std::string& source, const std::string& dir) |
| 2279 |
{ |
| 2280 |
std::string new_destination = dir; |
| 2281 |
SystemTools::ConvertToUnixSlashes(new_destination); |
| 2282 |
return new_destination + '/' + SystemTools::GetFilenameName(source); |
| 2283 |
} |
| 2284 |
|
| 2285 |
SystemTools::CopyStatus SystemTools::CopyFileIfDifferent( |
| 2286 |
std::string const& source, std::string const& destination) |
| 2287 |
{ |
| 2288 |
// special check for a destination that is a directory |
| 2289 |
// FilesDiffer does not handle file to directory compare |
| 2290 |
if (SystemTools::FileIsDirectory(destination)) { |
| 2291 |
const std::string new_destination = FileInDir(source, destination); |
| 2292 |
if (!SystemTools::ComparePath(new_destination, destination)) { |
| 2293 |
return SystemTools::CopyFileIfDifferent(source, new_destination); |
| 2294 |
} |
| 2295 |
} else { |
| 2296 |
// source and destination are files so do a copy if they |
| 2297 |
// are different |
| 2298 |
if (SystemTools::FilesDiffer(source, destination)) { |
| 2299 |
return SystemTools::CopyFileAlways(source, destination); |
| 2300 |
} |
| 2301 |
} |
| 2302 |
// at this point the files must be the same so return true |
| 2303 |
return CopyStatus{ Status::Success(), CopyStatus::NoPath }; |
| 2304 |
} |
| 2305 |
|
| 2306 |
#define KWSYS_ST_BUFFER 4096 |
| 2307 |
|
| 2308 |
bool SystemTools::FilesDiffer(const std::string& source, |
| 2309 |
const std::string& destination) |
| 2310 |
{ |
| 2311 |
|
| 2312 |
#if defined(_WIN32) |
| 2313 |
WIN32_FILE_ATTRIBUTE_DATA statSource; |
| 2314 |
if (GetFileAttributesExW(Encoding::ToWindowsExtendedPath(source).c_str(), |
| 2315 |
GetFileExInfoStandard, &statSource) == 0) { |
| 2316 |
return true; |
| 2317 |
} |
| 2318 |
|
| 2319 |
WIN32_FILE_ATTRIBUTE_DATA statDestination; |
| 2320 |
if (GetFileAttributesExW( |
| 2321 |
Encoding::ToWindowsExtendedPath(destination).c_str(), |
| 2322 |
GetFileExInfoStandard, &statDestination) == 0) { |
| 2323 |
return true; |
| 2324 |
} |
| 2325 |
|
| 2326 |
if (statSource.nFileSizeHigh != statDestination.nFileSizeHigh || |
| 2327 |
statSource.nFileSizeLow != statDestination.nFileSizeLow) { |
| 2328 |
return true; |
| 2329 |
} |
| 2330 |
|
| 2331 |
if (statSource.nFileSizeHigh == 0 && statSource.nFileSizeLow == 0) { |
| 2332 |
return false; |
| 2333 |
} |
| 2334 |
auto nleft = |
| 2335 |
((__int64)statSource.nFileSizeHigh << 32) + statSource.nFileSizeLow; |
| 2336 |
|
| 2337 |
#else |
| 2338 |
|
| 2339 |
struct stat statSource; |
| 2340 |
if (stat(source.c_str(), &statSource) != 0) { |
| 2341 |
return true; |
| 2342 |
} |
| 2343 |
|
| 2344 |
struct stat statDestination; |
| 2345 |
if (stat(destination.c_str(), &statDestination) != 0) { |
| 2346 |
return true; |
| 2347 |
} |
| 2348 |
|
| 2349 |
if (statSource.st_size != statDestination.st_size) { |
| 2350 |
return true; |
| 2351 |
} |
| 2352 |
|
| 2353 |
if (statSource.st_size == 0) { |
| 2354 |
return false; |
| 2355 |
} |
| 2356 |
off_t nleft = statSource.st_size; |
| 2357 |
#endif |
| 2358 |
|
| 2359 |
#if defined(_WIN32) |
| 2360 |
kwsys::ifstream finSource(source.c_str(), (std::ios::binary | std::ios::in)); |
| 2361 |
kwsys::ifstream finDestination(destination.c_str(), |
| 2362 |
(std::ios::binary | std::ios::in)); |
| 2363 |
#else |
| 2364 |
kwsys::ifstream finSource(source.c_str()); |
| 2365 |
kwsys::ifstream finDestination(destination.c_str()); |
| 2366 |
#endif |
| 2367 |
if (!finSource || !finDestination) { |
| 2368 |
return true; |
| 2369 |
} |
| 2370 |
|
| 2371 |
// Compare the files a block at a time. |
| 2372 |
char source_buf[KWSYS_ST_BUFFER]; |
| 2373 |
char dest_buf[KWSYS_ST_BUFFER]; |
| 2374 |
while (nleft > 0) { |
| 2375 |
// Read a block from each file. |
| 2376 |
std::streamsize nnext = (nleft > KWSYS_ST_BUFFER) |
| 2377 |
? KWSYS_ST_BUFFER |
| 2378 |
: static_cast<std::streamsize>(nleft); |
| 2379 |
finSource.read(source_buf, nnext); |
| 2380 |
finDestination.read(dest_buf, nnext); |
| 2381 |
|
| 2382 |
// If either failed to read assume they are different. |
| 2383 |
if (static_cast<std::streamsize>(finSource.gcount()) != nnext || |
| 2384 |
static_cast<std::streamsize>(finDestination.gcount()) != nnext) { |
| 2385 |
return true; |
| 2386 |
} |
| 2387 |
|
| 2388 |
// If this block differs the file differs. |
| 2389 |
if (memcmp(static_cast<const void*>(source_buf), |
| 2390 |
static_cast<const void*>(dest_buf), |
| 2391 |
static_cast<size_t>(nnext)) != 0) { |
| 2392 |
return true; |
| 2393 |
} |
| 2394 |
|
| 2395 |
// Update the byte count remaining. |
| 2396 |
nleft -= nnext; |
| 2397 |
} |
| 2398 |
|
| 2399 |
// No differences found. |
| 2400 |
return false; |
| 2401 |
} |
| 2402 |
|
| 2403 |
bool SystemTools::TextFilesDiffer(const std::string& path1, |
| 2404 |
const std::string& path2) |
| 2405 |
{ |
| 2406 |
kwsys::ifstream if1(path1.c_str()); |
| 2407 |
kwsys::ifstream if2(path2.c_str()); |
| 2408 |
if (!if1 || !if2) { |
| 2409 |
return true; |
| 2410 |
} |
| 2411 |
|
| 2412 |
for (;;) { |
| 2413 |
std::string line1, line2; |
| 2414 |
bool hasData1 = GetLineFromStream(if1, line1); |
| 2415 |
bool hasData2 = GetLineFromStream(if2, line2); |
| 2416 |
if (hasData1 != hasData2) { |
| 2417 |
return true; |
| 2418 |
} |
| 2419 |
if (!hasData1) { |
| 2420 |
break; |
| 2421 |
} |
| 2422 |
if (line1 != line2) { |
| 2423 |
return true; |
| 2424 |
} |
| 2425 |
} |
| 2426 |
return false; |
| 2427 |
} |
| 2428 |
|
| 2429 |
SystemTools::CopyStatus SystemTools::CopyFileContentBlockwise( |
| 2430 |
std::string const& source, std::string const& destination) |
| 2431 |
{ |
| 2432 |
// Open files |
| 2433 |
kwsys::ifstream fin(source.c_str(), std::ios::in | std::ios::binary); |
| 2434 |
if (!fin) { |
| 2435 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::SourcePath }; |
| 2436 |
} |
| 2437 |
|
| 2438 |
// try and remove the destination file so that read only destination files |
| 2439 |
// can be written to. |
| 2440 |
// If the remove fails continue so that files in read only directories |
| 2441 |
// that do not allow file removal can be modified. |
| 2442 |
SystemTools::RemoveFile(destination); |
| 2443 |
|
| 2444 |
kwsys::ofstream fout(destination.c_str(), |
| 2445 |
std::ios::out | std::ios::trunc | std::ios::binary); |
| 2446 |
if (!fout) { |
| 2447 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::DestPath }; |
| 2448 |
} |
| 2449 |
|
| 2450 |
// This copy loop is very sensitive on certain platforms with |
| 2451 |
// slightly broken stream libraries (like HPUX). Normally, it is |
| 2452 |
// incorrect to not check the error condition on the fin.read() |
| 2453 |
// before using the data, but the fin.gcount() will be zero if an |
| 2454 |
// error occurred. Therefore, the loop should be safe everywhere. |
| 2455 |
while (fin) { |
| 2456 |
const int bufferSize = 4096; |
| 2457 |
char buffer[bufferSize]; |
| 2458 |
|
| 2459 |
fin.read(buffer, bufferSize); |
| 2460 |
if (fin.gcount()) { |
| 2461 |
fout.write(buffer, fin.gcount()); |
| 2462 |
} else { |
| 2463 |
break; |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
// Make sure the operating system has finished writing the file |
| 2468 |
// before closing it. This will ensure the file is finished before |
| 2469 |
// the check below. |
| 2470 |
fout.flush(); |
| 2471 |
|
| 2472 |
fin.close(); |
| 2473 |
fout.close(); |
| 2474 |
|
| 2475 |
if (!fout) { |
| 2476 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::DestPath }; |
| 2477 |
} |
| 2478 |
|
| 2479 |
return CopyStatus{ Status::Success(), CopyStatus::NoPath }; |
| 2480 |
} |
| 2481 |
|
| 2482 |
/** |
| 2483 |
* Clone the source file to the destination file |
| 2484 |
* |
| 2485 |
* If available, the Linux FICLONE ioctl is used to create a check |
| 2486 |
* copy-on-write clone of the source file. |
| 2487 |
* |
| 2488 |
* The method returns false for the following cases: |
| 2489 |
* - The code has not been compiled on Linux or the ioctl was unknown |
| 2490 |
* - The source and destination is on different file systems |
| 2491 |
* - The underlying filesystem does not support file cloning |
| 2492 |
* - An unspecified error occurred |
| 2493 |
*/ |
| 2494 |
SystemTools::CopyStatus SystemTools::CloneFileContent( |
| 2495 |
std::string const& source, std::string const& destination) |
| 2496 |
{ |
| 2497 |
#if defined(__linux) && defined(FICLONE) |
| 2498 |
int in = open(source.c_str(), O_RDONLY); |
| 2499 |
if (in < 0) { |
| 2500 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::SourcePath }; |
| 2501 |
} |
| 2502 |
|
| 2503 |
SystemTools::RemoveFile(destination); |
| 2504 |
|
| 2505 |
int out = |
| 2506 |
open(destination.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
| 2507 |
if (out < 0) { |
| 2508 |
CopyStatus status{ Status::POSIX_errno(), CopyStatus::DestPath }; |
| 2509 |
close(in); |
| 2510 |
return status; |
| 2511 |
} |
| 2512 |
|
| 2513 |
CopyStatus status{ Status::Success(), CopyStatus::NoPath }; |
| 2514 |
if (ioctl(out, FICLONE, in) < 0) { |
| 2515 |
status = CopyStatus{ Status::POSIX_errno(), CopyStatus::NoPath }; |
| 2516 |
} |
| 2517 |
close(in); |
| 2518 |
close(out); |
| 2519 |
|
| 2520 |
return status; |
| 2521 |
#elif defined(__APPLE__) && \ |
| 2522 |
defined(KWSYS_SYSTEMTOOLS_HAVE_MACOS_COPYFILE_CLONE) |
| 2523 |
// When running as root, copyfile() copies more metadata than we |
| 2524 |
// want, such as ownership. Pretend it is not available. |
| 2525 |
if (getuid() == 0) { |
| 2526 |
return CopyStatus{ Status::POSIX(ENOSYS), CopyStatus::NoPath }; |
| 2527 |
} |
| 2528 |
|
| 2529 |
// NOTE: we cannot use `clonefile` as the {a,c,m}time for the file needs to |
| 2530 |
// be updated by `copy_file_if_different` and `copy_file`. |
| 2531 |
if (copyfile(source.c_str(), destination.c_str(), nullptr, |
| 2532 |
COPYFILE_METADATA | COPYFILE_CLONE) < 0) { |
| 2533 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::NoPath }; |
| 2534 |
} |
| 2535 |
# if KWSYS_CXX_HAS_UTIMENSAT |
| 2536 |
// utimensat is only available on newer Unixes and macOS 10.13+ |
| 2537 |
if (utimensat(AT_FDCWD, destination.c_str(), nullptr, 0) < 0) { |
| 2538 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::DestPath }; |
| 2539 |
} |
| 2540 |
# else |
| 2541 |
// fall back to utimes |
| 2542 |
if (utimes(destination.c_str(), nullptr) < 0) { |
| 2543 |
return CopyStatus{ Status::POSIX_errno(), CopyStatus::DestPath }; |
| 2544 |
} |
| 2545 |
# endif |
| 2546 |
return CopyStatus{ Status::Success(), CopyStatus::NoPath }; |
| 2547 |
#else |
| 2548 |
(void)source; |
| 2549 |
(void)destination; |
| 2550 |
return CopyStatus{ Status::POSIX(ENOSYS), CopyStatus::NoPath }; |
| 2551 |
#endif |
| 2552 |
} |
| 2553 |
|
| 2554 |
/** |
| 2555 |
* Copy a file named by "source" to the file named by "destination". |
| 2556 |
*/ |
| 2557 |
SystemTools::CopyStatus SystemTools::CopyFileAlways( |
| 2558 |
std::string const& source, std::string const& destination) |
| 2559 |
{ |
| 2560 |
CopyStatus status; |
| 2561 |
mode_t perm = 0; |
| 2562 |
Status perms = SystemTools::GetPermissions(source, perm); |
| 2563 |
std::string real_destination = destination; |
| 2564 |
|
| 2565 |
if (SystemTools::FileIsDirectory(source)) { |
| 2566 |
status = CopyStatus{ SystemTools::MakeDirectory(destination), |
| 2567 |
CopyStatus::DestPath }; |
| 2568 |
if (!status.IsSuccess()) { |
| 2569 |
return status; |
| 2570 |
} |
| 2571 |
} else { |
| 2572 |
// If destination is a directory, try to create a file with the same |
| 2573 |
// name as the source in that directory. |
| 2574 |
|
| 2575 |
std::string destination_dir; |
| 2576 |
if (SystemTools::FileIsDirectory(destination)) { |
| 2577 |
destination_dir = real_destination; |
| 2578 |
SystemTools::ConvertToUnixSlashes(real_destination); |
| 2579 |
real_destination += '/'; |
| 2580 |
std::string source_name = source; |
| 2581 |
real_destination += SystemTools::GetFilenameName(source_name); |
| 2582 |
} else { |
| 2583 |
destination_dir = SystemTools::GetFilenamePath(destination); |
| 2584 |
} |
| 2585 |
// If files are the same do not copy |
| 2586 |
if (SystemTools::SameFile(source, real_destination)) { |
| 2587 |
return status; |
| 2588 |
} |
| 2589 |
|
| 2590 |
// Create destination directory |
| 2591 |
if (!destination_dir.empty()) { |
| 2592 |
status = CopyStatus{ SystemTools::MakeDirectory(destination_dir), |
| 2593 |
CopyStatus::DestPath }; |
| 2594 |
if (!status.IsSuccess()) { |
| 2595 |
return status; |
| 2596 |
} |
| 2597 |
} |
| 2598 |
|
| 2599 |
status = SystemTools::CloneFileContent(source, real_destination); |
| 2600 |
// if cloning did not succeed, fall back to blockwise copy |
| 2601 |
if (!status.IsSuccess()) { |
| 2602 |
status = SystemTools::CopyFileContentBlockwise(source, real_destination); |
| 2603 |
} |
| 2604 |
if (!status.IsSuccess()) { |
| 2605 |
return status; |
| 2606 |
} |
| 2607 |
} |
| 2608 |
if (perms) { |
| 2609 |
status = CopyStatus{ SystemTools::SetPermissions(real_destination, perm), |
| 2610 |
CopyStatus::DestPath }; |
| 2611 |
} |
| 2612 |
return status; |
| 2613 |
} |
| 2614 |
|
| 2615 |
SystemTools::CopyStatus SystemTools::CopyAFile(std::string const& source, |
| 2616 |
std::string const& destination, |
| 2617 |
bool always) |
| 2618 |
{ |
| 2619 |
if (always) { |
| 2620 |
return SystemTools::CopyFileAlways(source, destination); |
| 2621 |
} else { |
| 2622 |
return SystemTools::CopyFileIfDifferent(source, destination); |
| 2623 |
} |
| 2624 |
} |
| 2625 |
|
| 2626 |
/** |
| 2627 |
* Copy a directory content from "source" directory to the directory named by |
| 2628 |
* "destination". |
| 2629 |
*/ |
| 2630 |
Status SystemTools::CopyADirectory(std::string const& source, |
| 2631 |
std::string const& destination, bool always) |
| 2632 |
{ |
| 2633 |
Status status; |
| 2634 |
Directory dir; |
| 2635 |
status = dir.Load(source); |
| 2636 |
if (!status.IsSuccess()) { |
| 2637 |
return status; |
| 2638 |
} |
| 2639 |
status = SystemTools::MakeDirectory(destination); |
| 2640 |
if (!status.IsSuccess()) { |
| 2641 |
return status; |
| 2642 |
} |
| 2643 |
|
| 2644 |
for (size_t fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum) { |
| 2645 |
if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)), ".") != 0 && |
| 2646 |
strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)), "..") != 0) { |
| 2647 |
std::string fullPath = source; |
| 2648 |
fullPath += "/"; |
| 2649 |
fullPath += dir.GetFile(static_cast<unsigned long>(fileNum)); |
| 2650 |
if (SystemTools::FileIsDirectory(fullPath)) { |
| 2651 |
std::string fullDestPath = destination; |
| 2652 |
fullDestPath += "/"; |
| 2653 |
fullDestPath += dir.GetFile(static_cast<unsigned long>(fileNum)); |
| 2654 |
status = SystemTools::CopyADirectory(fullPath, fullDestPath, always); |
| 2655 |
if (!status.IsSuccess()) { |
| 2656 |
return status; |
| 2657 |
} |
| 2658 |
} else { |
| 2659 |
status = SystemTools::CopyAFile(fullPath, destination, always); |
| 2660 |
if (!status.IsSuccess()) { |
| 2661 |
return status; |
| 2662 |
} |
| 2663 |
} |
| 2664 |
} |
| 2665 |
} |
| 2666 |
|
| 2667 |
return status; |
| 2668 |
} |
| 2669 |
|
| 2670 |
// return size of file; also returns zero if no file exists |
| 2671 |
unsigned long SystemTools::FileLength(const std::string& filename) |
| 2672 |
{ |
| 2673 |
unsigned long length = 0; |
| 2674 |
#ifdef _WIN32 |
| 2675 |
WIN32_FILE_ATTRIBUTE_DATA fs; |
| 2676 |
if (GetFileAttributesExW(Encoding::ToWindowsExtendedPath(filename).c_str(), |
| 2677 |
GetFileExInfoStandard, &fs) != 0) { |
| 2678 |
/* To support the full 64-bit file size, use fs.nFileSizeHigh |
| 2679 |
* and fs.nFileSizeLow to construct the 64 bit size |
| 2680 |
|
| 2681 |
length = ((__int64)fs.nFileSizeHigh << 32) + fs.nFileSizeLow; |
| 2682 |
*/ |
| 2683 |
length = static_cast<unsigned long>(fs.nFileSizeLow); |
| 2684 |
} |
| 2685 |
#else |
| 2686 |
struct stat fs; |
| 2687 |
if (stat(filename.c_str(), &fs) == 0) { |
| 2688 |
length = static_cast<unsigned long>(fs.st_size); |
| 2689 |
} |
| 2690 |
#endif |
| 2691 |
return length; |
| 2692 |
} |
| 2693 |
|
| 2694 |
int SystemTools::Strucmp(const char* l, const char* r) |
| 2695 |
{ |
| 2696 |
int lc; |
| 2697 |
int rc; |
| 2698 |
do { |
| 2699 |
lc = tolower(*l++); |
| 2700 |
rc = tolower(*r++); |
| 2701 |
} while (lc == rc && lc); |
| 2702 |
return lc - rc; |
| 2703 |
} |
| 2704 |
|
| 2705 |
// return file's modified time |
| 2706 |
long int SystemTools::ModifiedTime(const std::string& filename) |
| 2707 |
{ |
| 2708 |
long int mt = 0; |
| 2709 |
#ifdef _WIN32 |
| 2710 |
WIN32_FILE_ATTRIBUTE_DATA fs; |
| 2711 |
if (GetFileAttributesExW(Encoding::ToWindowsExtendedPath(filename).c_str(), |
| 2712 |
GetFileExInfoStandard, &fs) != 0) { |
| 2713 |
mt = windows_filetime_to_posix_time(fs.ftLastWriteTime); |
| 2714 |
} |
| 2715 |
#else |
| 2716 |
struct stat fs; |
| 2717 |
if (stat(filename.c_str(), &fs) == 0) { |
| 2718 |
mt = static_cast<long int>(fs.st_mtime); |
| 2719 |
} |
| 2720 |
#endif |
| 2721 |
return mt; |
| 2722 |
} |
| 2723 |
|
| 2724 |
// return file's creation time |
| 2725 |
long int SystemTools::CreationTime(const std::string& filename) |
| 2726 |
{ |
| 2727 |
long int ct = 0; |
| 2728 |
#ifdef _WIN32 |
| 2729 |
WIN32_FILE_ATTRIBUTE_DATA fs; |
| 2730 |
if (GetFileAttributesExW(Encoding::ToWindowsExtendedPath(filename).c_str(), |
| 2731 |
GetFileExInfoStandard, &fs) != 0) { |
| 2732 |
ct = windows_filetime_to_posix_time(fs.ftCreationTime); |
| 2733 |
} |
| 2734 |
#else |
| 2735 |
struct stat fs; |
| 2736 |
if (stat(filename.c_str(), &fs) == 0) { |
| 2737 |
ct = fs.st_ctime >= 0 ? static_cast<long int>(fs.st_ctime) : 0; |
| 2738 |
} |
| 2739 |
#endif |
| 2740 |
return ct; |
| 2741 |
} |
| 2742 |
|
| 2743 |
std::string SystemTools::GetLastSystemError() |
| 2744 |
{ |
| 2745 |
int e = errno; |
| 2746 |
return strerror(e); |
| 2747 |
} |
| 2748 |
|
| 2749 |
Status SystemTools::RemoveFile(std::string const& source) |
| 2750 |
{ |
| 2751 |
#ifdef _WIN32 |
| 2752 |
std::wstring const& ws = Encoding::ToWindowsExtendedPath(source); |
| 2753 |
if (DeleteFileW(ws.c_str())) { |
| 2754 |
return Status::Success(); |
| 2755 |
} |
| 2756 |
DWORD err = GetLastError(); |
| 2757 |
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) { |
| 2758 |
return Status::Success(); |
| 2759 |
} |
| 2760 |
if (err != ERROR_ACCESS_DENIED) { |
| 2761 |
return Status::Windows(err); |
| 2762 |
} |
| 2763 |
/* The file may be read-only. Try adding write permission. */ |
| 2764 |
mode_t mode; |
| 2765 |
if (!SystemTools::GetPermissions(source, mode) || |
| 2766 |
!SystemTools::SetPermissions(source, S_IWRITE)) { |
| 2767 |
SetLastError(err); |
| 2768 |
return Status::Windows(err); |
| 2769 |
} |
| 2770 |
|
| 2771 |
const DWORD DIRECTORY_SOFT_LINK_ATTRS = |
| 2772 |
FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT; |
| 2773 |
DWORD attrs = GetFileAttributesW(ws.c_str()); |
| 2774 |
if (attrs != INVALID_FILE_ATTRIBUTES && |
| 2775 |
(attrs & DIRECTORY_SOFT_LINK_ATTRS) == DIRECTORY_SOFT_LINK_ATTRS && |
| 2776 |
RemoveDirectoryW(ws.c_str())) { |
| 2777 |
return Status::Success(); |
| 2778 |
} |
| 2779 |
if (DeleteFileW(ws.c_str()) || GetLastError() == ERROR_FILE_NOT_FOUND || |
| 2780 |
GetLastError() == ERROR_PATH_NOT_FOUND) { |
| 2781 |
return Status::Success(); |
| 2782 |
} |
| 2783 |
/* Try to restore the original permissions. */ |
| 2784 |
SystemTools::SetPermissions(source, mode); |
| 2785 |
SetLastError(err); |
| 2786 |
return Status::Windows(err); |
| 2787 |
#else |
| 2788 |
if (unlink(source.c_str()) != 0 && errno != ENOENT) { |
| 2789 |
return Status::POSIX_errno(); |
| 2790 |
} |
| 2791 |
return Status::Success(); |
| 2792 |
#endif |
| 2793 |
} |
| 2794 |
|
| 2795 |
Status SystemTools::RemoveADirectory(std::string const& source) |
| 2796 |
{ |
| 2797 |
// Add read and write permission to the directory so we can read |
| 2798 |
// and modify its content to remove files and directories from it. |
| 2799 |
mode_t mode = 0; |
| 2800 |
if (SystemTools::GetPermissions(source, mode)) { |
| 2801 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 2802 |
mode |= S_IREAD | S_IWRITE; |
| 2803 |
#else |
| 2804 |
mode |= S_IRUSR | S_IWUSR; |
| 2805 |
#endif |
| 2806 |
SystemTools::SetPermissions(source, mode); |
| 2807 |
} |
| 2808 |
|
| 2809 |
Status status; |
| 2810 |
Directory dir; |
| 2811 |
status = dir.Load(source); |
| 2812 |
if (!status.IsSuccess()) { |
| 2813 |
return status; |
| 2814 |
} |
| 2815 |
|
| 2816 |
size_t fileNum; |
| 2817 |
for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum) { |
| 2818 |
if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)), ".") != 0 && |
| 2819 |
strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)), "..") != 0) { |
| 2820 |
std::string fullPath = source; |
| 2821 |
fullPath += "/"; |
| 2822 |
fullPath += dir.GetFile(static_cast<unsigned long>(fileNum)); |
| 2823 |
if (SystemTools::FileIsDirectory(fullPath) && |
| 2824 |
!SystemTools::FileIsSymlink(fullPath)) { |
| 2825 |
status = SystemTools::RemoveADirectory(fullPath); |
| 2826 |
if (!status.IsSuccess()) { |
| 2827 |
return status; |
| 2828 |
} |
| 2829 |
} else { |
| 2830 |
status = SystemTools::RemoveFile(fullPath); |
| 2831 |
if (!status.IsSuccess()) { |
| 2832 |
return status; |
| 2833 |
} |
| 2834 |
} |
| 2835 |
} |
| 2836 |
} |
| 2837 |
|
| 2838 |
if (Rmdir(source) != 0) { |
| 2839 |
status = Status::POSIX_errno(); |
| 2840 |
} |
| 2841 |
return status; |
| 2842 |
} |
| 2843 |
|
| 2844 |
/** |
| 2845 |
*/ |
| 2846 |
size_t SystemTools::GetMaximumFilePathLength() |
| 2847 |
{ |
| 2848 |
return KWSYS_SYSTEMTOOLS_MAXPATH; |
| 2849 |
} |
| 2850 |
|
| 2851 |
/** |
| 2852 |
* Find the file the given name. Searches the given path and then |
| 2853 |
* the system search path. Returns the full path to the file if it is |
| 2854 |
* found. Otherwise, the empty string is returned. |
| 2855 |
*/ |
| 2856 |
std::string SystemToolsStatic::FindName( |
| 2857 |
const std::string& name, const std::vector<std::string>& userPaths, |
| 2858 |
bool no_system_path) |
| 2859 |
{ |
| 2860 |
// Add the system search path to our path first |
| 2861 |
std::vector<std::string> path; |
| 2862 |
if (!no_system_path) { |
| 2863 |
SystemTools::GetPath(path, "CMAKE_FILE_PATH"); |
| 2864 |
SystemTools::GetPath(path); |
| 2865 |
} |
| 2866 |
// now add the additional paths |
| 2867 |
path.reserve(path.size() + userPaths.size()); |
| 2868 |
path.insert(path.end(), userPaths.begin(), userPaths.end()); |
| 2869 |
// now look for the file |
| 2870 |
std::string tryPath; |
| 2871 |
for (std::string const& p : path) { |
| 2872 |
tryPath = p; |
| 2873 |
if (tryPath.empty() || tryPath.back() != '/') { |
| 2874 |
tryPath += '/'; |
| 2875 |
} |
| 2876 |
tryPath += name; |
| 2877 |
if (SystemTools::FileExists(tryPath)) { |
| 2878 |
return tryPath; |
| 2879 |
} |
| 2880 |
} |
| 2881 |
// Couldn't find the file. |
| 2882 |
return ""; |
| 2883 |
} |
| 2884 |
|
| 2885 |
/** |
| 2886 |
* Find the file the given name. Searches the given path and then |
| 2887 |
* the system search path. Returns the full path to the file if it is |
| 2888 |
* found. Otherwise, the empty string is returned. |
| 2889 |
*/ |
| 2890 |
std::string SystemTools::FindFile(const std::string& name, |
| 2891 |
const std::vector<std::string>& userPaths, |
| 2892 |
bool no_system_path) |
| 2893 |
{ |
| 2894 |
std::string tryPath = |
| 2895 |
SystemToolsStatic::FindName(name, userPaths, no_system_path); |
| 2896 |
if (!tryPath.empty() && !SystemTools::FileIsDirectory(tryPath)) { |
| 2897 |
return SystemTools::CollapseFullPath(tryPath); |
| 2898 |
} |
| 2899 |
// Couldn't find the file. |
| 2900 |
return ""; |
| 2901 |
} |
| 2902 |
|
| 2903 |
/** |
| 2904 |
* Find the directory the given name. Searches the given path and then |
| 2905 |
* the system search path. Returns the full path to the directory if it is |
| 2906 |
* found. Otherwise, the empty string is returned. |
| 2907 |
*/ |
| 2908 |
std::string SystemTools::FindDirectory( |
| 2909 |
const std::string& name, const std::vector<std::string>& userPaths, |
| 2910 |
bool no_system_path) |
| 2911 |
{ |
| 2912 |
std::string tryPath = |
| 2913 |
SystemToolsStatic::FindName(name, userPaths, no_system_path); |
| 2914 |
if (!tryPath.empty() && SystemTools::FileIsDirectory(tryPath)) { |
| 2915 |
return SystemTools::CollapseFullPath(tryPath); |
| 2916 |
} |
| 2917 |
// Couldn't find the file. |
| 2918 |
return ""; |
| 2919 |
} |
| 2920 |
|
| 2921 |
/** |
| 2922 |
* Find the executable with the given name. Searches the given path and then |
| 2923 |
* the system search path. Returns the full path to the executable if it is |
| 2924 |
* found. Otherwise, the empty string is returned. |
| 2925 |
*/ |
| 2926 |
std::string SystemTools::FindProgram(const char* nameIn, |
| 2927 |
const std::vector<std::string>& userPaths, |
| 2928 |
bool no_system_path) |
| 2929 |
{ |
| 2930 |
if (!nameIn || !*nameIn) { |
| 2931 |
return ""; |
| 2932 |
} |
| 2933 |
return SystemTools::FindProgram(std::string(nameIn), userPaths, |
| 2934 |
no_system_path); |
| 2935 |
} |
| 2936 |
|
| 2937 |
std::string SystemTools::FindProgram(const std::string& name, |
| 2938 |
const std::vector<std::string>& userPaths, |
| 2939 |
bool no_system_path) |
| 2940 |
{ |
| 2941 |
std::string tryPath; |
| 2942 |
|
| 2943 |
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) |
| 2944 |
std::vector<std::string> extensions; |
| 2945 |
// check to see if the name already has a .xxx at |
| 2946 |
// the end of it |
| 2947 |
// on windows try .com then .exe |
| 2948 |
if (name.size() <= 3 || name[name.size() - 4] != '.') { |
| 2949 |
extensions.emplace_back(".com"); |
| 2950 |
extensions.emplace_back(".exe"); |
| 2951 |
|
| 2952 |
// first try with extensions if the os supports them |
| 2953 |
for (std::string const& ext : extensions) { |
| 2954 |
tryPath = name; |
| 2955 |
tryPath += ext; |
| 2956 |
if (SystemTools::FileIsExecutable(tryPath)) { |
| 2957 |
return SystemTools::CollapseFullPath(tryPath); |
| 2958 |
} |
| 2959 |
} |
| 2960 |
} |
| 2961 |
#endif |
| 2962 |
|
| 2963 |
// now try just the name |
| 2964 |
if (SystemTools::FileIsExecutable(name)) { |
| 2965 |
return SystemTools::CollapseFullPath(name); |
| 2966 |
} |
| 2967 |
// now construct the path |
| 2968 |
std::vector<std::string> path; |
| 2969 |
// Add the system search path to our path. |
| 2970 |
if (!no_system_path) { |
| 2971 |
SystemTools::GetPath(path); |
| 2972 |
} |
| 2973 |
// now add the additional paths |
| 2974 |
path.reserve(path.size() + userPaths.size()); |
| 2975 |
path.insert(path.end(), userPaths.begin(), userPaths.end()); |
| 2976 |
// Add a trailing slash to all paths to aid the search process. |
| 2977 |
for (std::string& p : path) { |
| 2978 |
if (p.empty() || p.back() != '/') { |
| 2979 |
p += '/'; |
| 2980 |
} |
| 2981 |
} |
| 2982 |
// Try each path |
| 2983 |
for (std::string& p : path) { |
| 2984 |
#ifdef _WIN32 |
| 2985 |
// Remove double quotes from the path on windows |
| 2986 |
SystemTools::ReplaceString(p, "\"", ""); |
| 2987 |
#endif |
| 2988 |
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) |
| 2989 |
// first try with extensions |
| 2990 |
for (std::string const& ext : extensions) { |
| 2991 |
tryPath = p; |
| 2992 |
tryPath += name; |
| 2993 |
tryPath += ext; |
| 2994 |
if (SystemTools::FileIsExecutable(tryPath)) { |
| 2995 |
return SystemTools::CollapseFullPath(tryPath); |
| 2996 |
} |
| 2997 |
} |
| 2998 |
#endif |
| 2999 |
// now try it without them |
| 3000 |
tryPath = p; |
| 3001 |
tryPath += name; |
| 3002 |
if (SystemTools::FileIsExecutable(tryPath)) { |
| 3003 |
return SystemTools::CollapseFullPath(tryPath); |
| 3004 |
} |
| 3005 |
} |
| 3006 |
// Couldn't find the program. |
| 3007 |
return ""; |
| 3008 |
} |
| 3009 |
|
| 3010 |
std::string SystemTools::FindProgram(const std::vector<std::string>& names, |
| 3011 |
const std::vector<std::string>& path, |
| 3012 |
bool noSystemPath) |
| 3013 |
{ |
| 3014 |
for (std::string const& name : names) { |
| 3015 |
// Try to find the program. |
| 3016 |
std::string result = SystemTools::FindProgram(name, path, noSystemPath); |
| 3017 |
if (!result.empty()) { |
| 3018 |
return result; |
| 3019 |
} |
| 3020 |
} |
| 3021 |
return ""; |
| 3022 |
} |
| 3023 |
|
| 3024 |
/** |
| 3025 |
* Find the library with the given name. Searches the given path and then |
| 3026 |
* the system search path. Returns the full path to the library if it is |
| 3027 |
* found. Otherwise, the empty string is returned. |
| 3028 |
*/ |
| 3029 |
std::string SystemTools::FindLibrary(const std::string& name, |
| 3030 |
const std::vector<std::string>& userPaths) |
| 3031 |
{ |
| 3032 |
// See if the executable exists as written. |
| 3033 |
if (SystemTools::FileExists(name, true)) { |
| 3034 |
return SystemTools::CollapseFullPath(name); |
| 3035 |
} |
| 3036 |
|
| 3037 |
// Add the system search path to our path. |
| 3038 |
std::vector<std::string> path; |
| 3039 |
SystemTools::GetPath(path); |
| 3040 |
// now add the additional paths |
| 3041 |
path.reserve(path.size() + userPaths.size()); |
| 3042 |
path.insert(path.end(), userPaths.begin(), userPaths.end()); |
| 3043 |
// Add a trailing slash to all paths to aid the search process. |
| 3044 |
for (std::string& p : path) { |
| 3045 |
if (p.empty() || p.back() != '/') { |
| 3046 |
p += '/'; |
| 3047 |
} |
| 3048 |
} |
| 3049 |
std::string tryPath; |
| 3050 |
for (std::string const& p : path) { |
| 3051 |
#if defined(__APPLE__) |
| 3052 |
tryPath = p; |
| 3053 |
tryPath += name; |
| 3054 |
tryPath += ".framework"; |
| 3055 |
if (SystemTools::FileIsDirectory(tryPath)) { |
| 3056 |
return SystemTools::CollapseFullPath(tryPath); |
| 3057 |
} |
| 3058 |
#endif |
| 3059 |
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__) |
| 3060 |
tryPath = p; |
| 3061 |
tryPath += name; |
| 3062 |
tryPath += ".lib"; |
| 3063 |
if (SystemTools::FileExists(tryPath, true)) { |
| 3064 |
return SystemTools::CollapseFullPath(tryPath); |
| 3065 |
} |
| 3066 |
#else |
| 3067 |
tryPath = p; |
| 3068 |
tryPath += "lib"; |
| 3069 |
tryPath += name; |
| 3070 |
tryPath += ".so"; |
| 3071 |
if (SystemTools::FileExists(tryPath, true)) { |
| 3072 |
return SystemTools::CollapseFullPath(tryPath); |
| 3073 |
} |
| 3074 |
tryPath = p; |
| 3075 |
tryPath += "lib"; |
| 3076 |
tryPath += name; |
| 3077 |
tryPath += ".a"; |
| 3078 |
if (SystemTools::FileExists(tryPath, true)) { |
| 3079 |
return SystemTools::CollapseFullPath(tryPath); |
| 3080 |
} |
| 3081 |
tryPath = p; |
| 3082 |
tryPath += "lib"; |
| 3083 |
tryPath += name; |
| 3084 |
tryPath += ".sl"; |
| 3085 |
if (SystemTools::FileExists(tryPath, true)) { |
| 3086 |
return SystemTools::CollapseFullPath(tryPath); |
| 3087 |
} |
| 3088 |
tryPath = p; |
| 3089 |
tryPath += "lib"; |
| 3090 |
tryPath += name; |
| 3091 |
tryPath += ".dylib"; |
| 3092 |
if (SystemTools::FileExists(tryPath, true)) { |
| 3093 |
return SystemTools::CollapseFullPath(tryPath); |
| 3094 |
} |
| 3095 |
tryPath = p; |
| 3096 |
tryPath += "lib"; |
| 3097 |
tryPath += name; |
| 3098 |
tryPath += ".dll"; |
| 3099 |
if (SystemTools::FileExists(tryPath, true)) { |
| 3100 |
return SystemTools::CollapseFullPath(tryPath); |
| 3101 |
} |
| 3102 |
#endif |
| 3103 |
} |
| 3104 |
|
| 3105 |
// Couldn't find the library. |
| 3106 |
return ""; |
| 3107 |
} |
| 3108 |
|
| 3109 |
std::string SystemTools::GetRealPath(const std::string& path, |
| 3110 |
std::string* errorMessage) |
| 3111 |
{ |
| 3112 |
std::string ret; |
| 3113 |
Realpath(path, ret, errorMessage); |
| 3114 |
return ret; |
| 3115 |
} |
| 3116 |
|
| 3117 |
// Remove any trailing slash from the name except in a root component. |
| 3118 |
static const char* RemoveTrailingSlashes( |
| 3119 |
const std::string& inName, char (&local_buffer)[KWSYS_SYSTEMTOOLS_MAXPATH], |
| 3120 |
std::string& string_buffer) |
| 3121 |
{ |
| 3122 |
size_t length = inName.size(); |
| 3123 |
const char* name = inName.c_str(); |
| 3124 |
|
| 3125 |
size_t last = length - 1; |
| 3126 |
if (last > 0 && (name[last] == '/' || name[last] == '\\') && |
| 3127 |
strcmp(name, "/") != 0 && name[last - 1] != ':') { |
| 3128 |
if (last < sizeof(local_buffer)) { |
| 3129 |
memcpy(local_buffer, name, last); |
| 3130 |
local_buffer[last] = '\0'; |
| 3131 |
name = local_buffer; |
| 3132 |
} else { |
| 3133 |
string_buffer.append(name, last); |
| 3134 |
name = string_buffer.c_str(); |
| 3135 |
} |
| 3136 |
} |
| 3137 |
|
| 3138 |
return name; |
| 3139 |
} |
| 3140 |
|
| 3141 |
bool SystemTools::FileIsDirectory(const std::string& inName) |
| 3142 |
{ |
| 3143 |
if (inName.empty()) { |
| 3144 |
return false; |
| 3145 |
} |
| 3146 |
|
| 3147 |
char local_buffer[KWSYS_SYSTEMTOOLS_MAXPATH]; |
| 3148 |
std::string string_buffer; |
| 3149 |
const auto name = RemoveTrailingSlashes(inName, local_buffer, string_buffer); |
| 3150 |
|
| 3151 |
// Now check the file node type. |
| 3152 |
#if defined(_WIN32) |
| 3153 |
DWORD attr = |
| 3154 |
GetFileAttributesW(Encoding::ToWindowsExtendedPath(name).c_str()); |
| 3155 |
if (attr != INVALID_FILE_ATTRIBUTES) { |
| 3156 |
return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 3157 |
#else |
| 3158 |
struct stat fs; |
| 3159 |
if (stat(name, &fs) == 0) { |
| 3160 |
return S_ISDIR(fs.st_mode); |
| 3161 |
#endif |
| 3162 |
} else { |
| 3163 |
return false; |
| 3164 |
} |
| 3165 |
} |
| 3166 |
|
| 3167 |
bool SystemTools::FileIsExecutable(const std::string& inName) |
| 3168 |
{ |
| 3169 |
#ifdef _WIN32 |
| 3170 |
char local_buffer[KWSYS_SYSTEMTOOLS_MAXPATH]; |
| 3171 |
std::string string_buffer; |
| 3172 |
const auto name = RemoveTrailingSlashes(inName, local_buffer, string_buffer); |
| 3173 |
const auto attr = |
| 3174 |
GetFileAttributesW(Encoding::ToWindowsExtendedPath(name).c_str()); |
| 3175 |
|
| 3176 |
// On Windows any file that exists and is not a directory is considered |
| 3177 |
// readable and therefore also executable: |
| 3178 |
return attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY); |
| 3179 |
#else |
| 3180 |
return !FileIsDirectory(inName) && TestFileAccess(inName, TEST_FILE_EXECUTE); |
| 3181 |
#endif |
| 3182 |
} |
| 3183 |
|
| 3184 |
#if defined(_WIN32) |
| 3185 |
bool SystemTools::FileIsSymlinkWithAttr(const std::wstring& path, |
| 3186 |
unsigned long attr) |
| 3187 |
{ |
| 3188 |
if (attr != INVALID_FILE_ATTRIBUTES) { |
| 3189 |
if ((attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { |
| 3190 |
// FILE_ATTRIBUTE_REPARSE_POINT means: |
| 3191 |
// * a file or directory that has an associated reparse point, or |
| 3192 |
// * a file that is a symbolic link. |
| 3193 |
HANDLE hFile = CreateFileW( |
| 3194 |
path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 3195 |
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 3196 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 3197 |
return false; |
| 3198 |
} |
| 3199 |
byte buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 3200 |
DWORD bytesReturned = 0; |
| 3201 |
if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, nullptr, 0, buffer, |
| 3202 |
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bytesReturned, |
| 3203 |
nullptr)) { |
| 3204 |
CloseHandle(hFile); |
| 3205 |
// Since FILE_ATTRIBUTE_REPARSE_POINT is set this file must be |
| 3206 |
// a symbolic link if it is not a reparse point. |
| 3207 |
return GetLastError() == ERROR_NOT_A_REPARSE_POINT; |
| 3208 |
} |
| 3209 |
CloseHandle(hFile); |
| 3210 |
ULONG reparseTag = |
| 3211 |
reinterpret_cast<PREPARSE_DATA_BUFFER>(&buffer[0])->ReparseTag; |
| 3212 |
return (reparseTag == IO_REPARSE_TAG_SYMLINK) || |
| 3213 |
(reparseTag == IO_REPARSE_TAG_MOUNT_POINT); |
| 3214 |
} |
| 3215 |
return false; |
| 3216 |
} |
| 3217 |
|
| 3218 |
return false; |
| 3219 |
} |
| 3220 |
#endif |
| 3221 |
|
| 3222 |
bool SystemTools::FileIsSymlink(const std::string& name) |
| 3223 |
{ |
| 3224 |
#if defined(_WIN32) |
| 3225 |
std::wstring path = Encoding::ToWindowsExtendedPath(name); |
| 3226 |
return FileIsSymlinkWithAttr(path, GetFileAttributesW(path.c_str())); |
| 3227 |
#else |
| 3228 |
struct stat fs; |
| 3229 |
if (lstat(name.c_str(), &fs) == 0) { |
| 3230 |
return S_ISLNK(fs.st_mode); |
| 3231 |
} else { |
| 3232 |
return false; |
| 3233 |
} |
| 3234 |
#endif |
| 3235 |
} |
| 3236 |
|
| 3237 |
bool SystemTools::FileIsFIFO(const std::string& name) |
| 3238 |
{ |
| 3239 |
#if defined(_WIN32) |
| 3240 |
HANDLE hFile = |
| 3241 |
CreateFileW(Encoding::ToWide(name).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 3242 |
nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 3243 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 3244 |
return false; |
| 3245 |
} |
| 3246 |
const DWORD type = GetFileType(hFile); |
| 3247 |
CloseHandle(hFile); |
| 3248 |
return type == FILE_TYPE_PIPE; |
| 3249 |
#else |
| 3250 |
struct stat fs; |
| 3251 |
if (lstat(name.c_str(), &fs) == 0) { |
| 3252 |
return S_ISFIFO(fs.st_mode); |
| 3253 |
} else { |
| 3254 |
return false; |
| 3255 |
} |
| 3256 |
#endif |
| 3257 |
} |
| 3258 |
|
| 3259 |
Status SystemTools::CreateSymlink(std::string const& origName, |
| 3260 |
std::string const& newName) |
| 3261 |
{ |
| 3262 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 3263 |
DWORD flags; |
| 3264 |
if (FileIsDirectory(origName)) { |
| 3265 |
flags = SYMBOLIC_LINK_FLAG_DIRECTORY; |
| 3266 |
} else { |
| 3267 |
flags = 0; |
| 3268 |
} |
| 3269 |
|
| 3270 |
std::wstring origPath = Encoding::ToWindowsExtendedPath(origName); |
| 3271 |
std::wstring newPath = Encoding::ToWindowsExtendedPath(newName); |
| 3272 |
|
| 3273 |
Status status; |
| 3274 |
if (!CreateSymbolicLinkW(newPath.c_str(), origPath.c_str(), |
| 3275 |
flags | |
| 3276 |
SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) { |
| 3277 |
status = Status::Windows_GetLastError(); |
| 3278 |
} |
| 3279 |
// Older Windows versions do not understand |
| 3280 |
// SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE |
| 3281 |
if (status.GetWindows() == ERROR_INVALID_PARAMETER) { |
| 3282 |
status = Status::Success(); |
| 3283 |
if (!CreateSymbolicLinkW(newPath.c_str(), origPath.c_str(), flags)) { |
| 3284 |
status = Status::Windows_GetLastError(); |
| 3285 |
} |
| 3286 |
} |
| 3287 |
|
| 3288 |
return status; |
| 3289 |
#else |
| 3290 |
if (symlink(origName.c_str(), newName.c_str()) < 0) { |
| 3291 |
return Status::POSIX_errno(); |
| 3292 |
} |
| 3293 |
return Status::Success(); |
| 3294 |
#endif |
| 3295 |
} |
| 3296 |
|
| 3297 |
Status SystemTools::ReadSymlink(std::string const& newName, |
| 3298 |
std::string& origName) |
| 3299 |
{ |
| 3300 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 3301 |
std::wstring newPath = Encoding::ToWindowsExtendedPath(newName); |
| 3302 |
// FILE_ATTRIBUTE_REPARSE_POINT means: |
| 3303 |
// * a file or directory that has an associated reparse point, or |
| 3304 |
// * a file that is a symbolic link. |
| 3305 |
HANDLE hFile = CreateFileW( |
| 3306 |
newPath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 3307 |
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 3308 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 3309 |
return Status::Windows_GetLastError(); |
| 3310 |
} |
| 3311 |
byte buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 3312 |
DWORD bytesReturned = 0; |
| 3313 |
Status status; |
| 3314 |
if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, nullptr, 0, buffer, |
| 3315 |
MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bytesReturned, |
| 3316 |
nullptr)) { |
| 3317 |
status = Status::Windows_GetLastError(); |
| 3318 |
} |
| 3319 |
CloseHandle(hFile); |
| 3320 |
if (!status.IsSuccess()) { |
| 3321 |
return status; |
| 3322 |
} |
| 3323 |
PREPARSE_DATA_BUFFER data = |
| 3324 |
reinterpret_cast<PREPARSE_DATA_BUFFER>(&buffer[0]); |
| 3325 |
USHORT substituteNameLength; |
| 3326 |
PCWSTR substituteNameData; |
| 3327 |
if (data->ReparseTag == IO_REPARSE_TAG_SYMLINK) { |
| 3328 |
substituteNameLength = |
| 3329 |
data->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(WCHAR); |
| 3330 |
substituteNameData = data->SymbolicLinkReparseBuffer.PathBuffer + |
| 3331 |
data->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR); |
| 3332 |
} else if (data->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) { |
| 3333 |
substituteNameLength = |
| 3334 |
data->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR); |
| 3335 |
substituteNameData = data->MountPointReparseBuffer.PathBuffer + |
| 3336 |
data->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR); |
| 3337 |
} else if (data->ReparseTag == IO_REPARSE_TAG_APPEXECLINK) { |
| 3338 |
// The reparse buffer is a list of 0-terminated non-empty strings, |
| 3339 |
// terminated by an empty string (0-0). We need the third string. |
| 3340 |
size_t destLen; |
| 3341 |
substituteNameData = GetAppExecLink(data, destLen); |
| 3342 |
if (substituteNameData == nullptr || destLen == 0) { |
| 3343 |
return Status::Windows(ERROR_SYMLINK_NOT_SUPPORTED); |
| 3344 |
} |
| 3345 |
substituteNameLength = static_cast<USHORT>(destLen); |
| 3346 |
} else { |
| 3347 |
return Status::Windows(ERROR_REPARSE_TAG_MISMATCH); |
| 3348 |
} |
| 3349 |
std::wstring substituteName(substituteNameData, substituteNameLength); |
| 3350 |
origName = Encoding::ToNarrow(substituteName); |
| 3351 |
#else |
| 3352 |
char buf[KWSYS_SYSTEMTOOLS_MAXPATH + 1]; |
| 3353 |
int count = static_cast<int>( |
| 3354 |
readlink(newName.c_str(), buf, KWSYS_SYSTEMTOOLS_MAXPATH)); |
| 3355 |
if (count < 0) { |
| 3356 |
return Status::POSIX_errno(); |
| 3357 |
} |
| 3358 |
// Add null-terminator. |
| 3359 |
buf[count] = 0; |
| 3360 |
origName = buf; |
| 3361 |
#endif |
| 3362 |
return Status::Success(); |
| 3363 |
} |
| 3364 |
|
| 3365 |
Status SystemTools::ChangeDirectory(std::string const& dir) |
| 3366 |
{ |
| 3367 |
if (Chdir(dir) < 0) { |
| 3368 |
return Status::POSIX_errno(); |
| 3369 |
} |
| 3370 |
return Status::Success(); |
| 3371 |
} |
| 3372 |
|
| 3373 |
std::string SystemTools::GetCurrentWorkingDirectory() |
| 3374 |
{ |
| 3375 |
char buf[2048]; |
| 3376 |
const char* cwd = Getcwd(buf, 2048); |
| 3377 |
std::string path; |
| 3378 |
if (cwd) { |
| 3379 |
path = cwd; |
| 3380 |
SystemTools::ConvertToUnixSlashes(path); |
| 3381 |
} |
| 3382 |
return path; |
| 3383 |
} |
| 3384 |
|
| 3385 |
std::string SystemTools::GetProgramPath(const std::string& in_name) |
| 3386 |
{ |
| 3387 |
std::string dir, file; |
| 3388 |
SystemTools::SplitProgramPath(in_name, dir, file); |
| 3389 |
return dir; |
| 3390 |
} |
| 3391 |
|
| 3392 |
bool SystemTools::SplitProgramPath(const std::string& in_name, |
| 3393 |
std::string& dir, std::string& file, bool) |
| 3394 |
{ |
| 3395 |
dir = in_name; |
| 3396 |
file.clear(); |
| 3397 |
SystemTools::ConvertToUnixSlashes(dir); |
| 3398 |
|
| 3399 |
if (!SystemTools::FileIsDirectory(dir)) { |
| 3400 |
std::string::size_type slashPos = dir.rfind('/'); |
| 3401 |
if (slashPos != std::string::npos) { |
| 3402 |
file = dir.substr(slashPos + 1); |
| 3403 |
dir.resize(slashPos); |
| 3404 |
} else { |
| 3405 |
file = dir; |
| 3406 |
dir.clear(); |
| 3407 |
} |
| 3408 |
} |
| 3409 |
if (!(dir.empty()) && !SystemTools::FileIsDirectory(dir)) { |
| 3410 |
std::string oldDir = in_name; |
| 3411 |
SystemTools::ConvertToUnixSlashes(oldDir); |
| 3412 |
dir = in_name; |
| 3413 |
return false; |
| 3414 |
} |
| 3415 |
return true; |
| 3416 |
} |
| 3417 |
|
| 3418 |
bool SystemTools::FindProgramPath(const char* argv0, std::string& pathOut, |
| 3419 |
std::string& errorMsg) |
| 3420 |
{ |
| 3421 |
std::vector<std::string> failures; |
| 3422 |
std::string self = argv0 ? argv0 : ""; |
| 3423 |
failures.push_back(self); |
| 3424 |
SystemTools::ConvertToUnixSlashes(self); |
| 3425 |
self = SystemTools::FindProgram(self); |
| 3426 |
if (!SystemTools::FileIsExecutable(self)) { |
| 3427 |
failures.push_back(self); |
| 3428 |
std::ostringstream msg; |
| 3429 |
msg << "Can not find the command line program "; |
| 3430 |
msg << "\n"; |
| 3431 |
if (argv0) { |
| 3432 |
msg << " argv[0] = \"" << argv0 << "\"\n"; |
| 3433 |
} |
| 3434 |
msg << " Attempted paths:\n"; |
| 3435 |
for (std::string const& ff : failures) { |
| 3436 |
msg << " \"" << ff << "\"\n"; |
| 3437 |
} |
| 3438 |
errorMsg = msg.str(); |
| 3439 |
return false; |
| 3440 |
} |
| 3441 |
pathOut = self; |
| 3442 |
return true; |
| 3443 |
} |
| 3444 |
|
| 3445 |
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP |
| 3446 |
void SystemTools::AddTranslationPath(const std::string& a, |
| 3447 |
const std::string& b) |
| 3448 |
{ |
| 3449 |
std::string path_a = a; |
| 3450 |
std::string path_b = b; |
| 3451 |
SystemTools::ConvertToUnixSlashes(path_a); |
| 3452 |
SystemTools::ConvertToUnixSlashes(path_b); |
| 3453 |
// First check this is a directory path, since we don't want the table to |
| 3454 |
// grow too fat |
| 3455 |
if (SystemTools::FileIsDirectory(path_a)) { |
| 3456 |
// Make sure the path is a full path and does not contain no '..' |
| 3457 |
// Ken--the following code is incorrect. .. can be in a valid path |
| 3458 |
// for example /home/martink/MyHubba...Hubba/Src |
| 3459 |
if (SystemTools::FileIsFullPath(path_b) && |
| 3460 |
path_b.find("..") == std::string::npos) { |
| 3461 |
// Before inserting make sure path ends with '/' |
| 3462 |
if (!path_a.empty() && path_a.back() != '/') { |
| 3463 |
path_a += '/'; |
| 3464 |
} |
| 3465 |
if (!path_b.empty() && path_b.back() != '/') { |
| 3466 |
path_b += '/'; |
| 3467 |
} |
| 3468 |
if (!(path_a == path_b)) { |
| 3469 |
SystemToolsStatics->TranslationMap.insert( |
| 3470 |
SystemToolsStatic::StringMap::value_type(std::move(path_a), |
| 3471 |
std::move(path_b))); |
| 3472 |
} |
| 3473 |
} |
| 3474 |
} |
| 3475 |
} |
| 3476 |
|
| 3477 |
void SystemTools::AddKeepPath(const std::string& dir) |
| 3478 |
{ |
| 3479 |
std::string cdir; |
| 3480 |
Realpath(SystemTools::CollapseFullPath(dir), cdir); |
| 3481 |
SystemTools::AddTranslationPath(cdir, dir); |
| 3482 |
} |
| 3483 |
|
| 3484 |
void SystemTools::CheckTranslationPath(std::string& path) |
| 3485 |
{ |
| 3486 |
// Do not translate paths that are too short to have meaningful |
| 3487 |
// translations. |
| 3488 |
if (path.size() < 2) { |
| 3489 |
return; |
| 3490 |
} |
| 3491 |
|
| 3492 |
// Always add a trailing slash before translation. It does not |
| 3493 |
// matter if this adds an extra slash, but we do not want to |
| 3494 |
// translate part of a directory (like the foo part of foo-dir). |
| 3495 |
path += '/'; |
| 3496 |
|
| 3497 |
// In case a file was specified we still have to go through this: |
| 3498 |
// Now convert any path found in the table back to the one desired: |
| 3499 |
for (auto const& pair : SystemToolsStatics->TranslationMap) { |
| 3500 |
// We need to check of the path is a substring of the other path |
| 3501 |
if (path.compare(0, pair.first.size(), pair.first) == 0) { |
| 3502 |
path = path.replace(0, pair.first.size(), pair.second); |
| 3503 |
} |
| 3504 |
} |
| 3505 |
|
| 3506 |
// Remove the trailing slash we added before. |
| 3507 |
path.pop_back(); |
| 3508 |
} |
| 3509 |
#endif |
| 3510 |
|
| 3511 |
static void SystemToolsAppendComponents( |
| 3512 |
std::vector<std::string>& out_components, |
| 3513 |
std::vector<std::string>::iterator first, |
| 3514 |
std::vector<std::string>::iterator last) |
| 3515 |
{ |
| 3516 |
static const std::string up = ".."; |
| 3517 |
static const std::string cur = "."; |
| 3518 |
for (std::vector<std::string>::const_iterator i = first; i != last; ++i) { |
| 3519 |
if (*i == up) { |
| 3520 |
// Remove the previous component if possible. Ignore ../ components |
| 3521 |
// that try to go above the root. Keep ../ components if they are |
| 3522 |
// at the beginning of a relative path (base path is relative). |
| 3523 |
if (out_components.size() > 1 && out_components.back() != up) { |
| 3524 |
out_components.resize(out_components.size() - 1); |
| 3525 |
} else if (!out_components.empty() && out_components[0].empty()) { |
| 3526 |
out_components.emplace_back(std::move(*i)); |
| 3527 |
} |
| 3528 |
} else if (!i->empty() && *i != cur) { |
| 3529 |
out_components.emplace_back(std::move(*i)); |
| 3530 |
} |
| 3531 |
} |
| 3532 |
} |
| 3533 |
|
| 3534 |
namespace { |
| 3535 |
|
| 3536 |
std::string CollapseFullPathImpl(std::string const& in_path, |
| 3537 |
std::string const* in_base) |
| 3538 |
{ |
| 3539 |
// Collect the output path components. |
| 3540 |
std::vector<std::string> out_components; |
| 3541 |
|
| 3542 |
// Split the input path components. |
| 3543 |
std::vector<std::string> path_components; |
| 3544 |
SystemTools::SplitPath(in_path, path_components); |
| 3545 |
out_components.reserve(path_components.size()); |
| 3546 |
|
| 3547 |
// If the input path is relative, start with a base path. |
| 3548 |
if (path_components[0].empty()) { |
| 3549 |
std::vector<std::string> base_components; |
| 3550 |
|
| 3551 |
if (in_base) { |
| 3552 |
// Use the given base path. |
| 3553 |
SystemTools::SplitPath(*in_base, base_components); |
| 3554 |
} else { |
| 3555 |
// Use the current working directory as a base path. |
| 3556 |
std::string cwd = SystemTools::GetCurrentWorkingDirectory(); |
| 3557 |
SystemTools::SplitPath(cwd, base_components); |
| 3558 |
} |
| 3559 |
|
| 3560 |
// Append base path components to the output path. |
| 3561 |
out_components.push_back(base_components[0]); |
| 3562 |
SystemToolsAppendComponents(out_components, base_components.begin() + 1, |
| 3563 |
base_components.end()); |
| 3564 |
} |
| 3565 |
|
| 3566 |
// Append input path components to the output path. |
| 3567 |
SystemToolsAppendComponents(out_components, path_components.begin(), |
| 3568 |
path_components.end()); |
| 3569 |
|
| 3570 |
// Transform the path back to a string. |
| 3571 |
std::string newPath = SystemTools::JoinPath(out_components); |
| 3572 |
|
| 3573 |
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP |
| 3574 |
// Update the translation table with this potentially new path. I am not |
| 3575 |
// sure why this line is here, it seems really questionable, but yet I |
| 3576 |
// would put good money that if I remove it something will break, basically |
| 3577 |
// from what I can see it created a mapping from the collapsed path, to be |
| 3578 |
// replaced by the input path, which almost completely does the opposite of |
| 3579 |
// this function, the only thing preventing this from happening a lot is |
| 3580 |
// that if the in_path has a .. in it, then it is not added to the |
| 3581 |
// translation table. So for most calls this either does nothing due to the |
| 3582 |
// .. or it adds a translation between identical paths as nothing was |
| 3583 |
// collapsed, so I am going to try to comment it out, and see what hits the |
| 3584 |
// fan, hopefully quickly. |
| 3585 |
// Commented out line below: |
| 3586 |
// SystemTools::AddTranslationPath(newPath, in_path); |
| 3587 |
|
| 3588 |
SystemTools::CheckTranslationPath(newPath); |
| 3589 |
#endif |
| 3590 |
#ifdef _WIN32 |
| 3591 |
newPath = SystemToolsStatics->GetActualCaseForPathCached(newPath); |
| 3592 |
SystemTools::ConvertToUnixSlashes(newPath); |
| 3593 |
#endif |
| 3594 |
// Return the reconstructed path. |
| 3595 |
return newPath; |
| 3596 |
} |
| 3597 |
} |
| 3598 |
|
| 3599 |
std::string SystemTools::CollapseFullPath(std::string const& in_path) |
| 3600 |
{ |
| 3601 |
return CollapseFullPathImpl(in_path, nullptr); |
| 3602 |
} |
| 3603 |
|
| 3604 |
std::string SystemTools::CollapseFullPath(std::string const& in_path, |
| 3605 |
const char* in_base) |
| 3606 |
{ |
| 3607 |
if (!in_base) { |
| 3608 |
return CollapseFullPathImpl(in_path, nullptr); |
| 3609 |
} |
| 3610 |
std::string tmp_base = in_base; |
| 3611 |
return CollapseFullPathImpl(in_path, &tmp_base); |
| 3612 |
} |
| 3613 |
|
| 3614 |
std::string SystemTools::CollapseFullPath(std::string const& in_path, |
| 3615 |
std::string const& in_base) |
| 3616 |
{ |
| 3617 |
return CollapseFullPathImpl(in_path, &in_base); |
| 3618 |
} |
| 3619 |
|
| 3620 |
// compute the relative path from here to there |
| 3621 |
std::string SystemTools::RelativePath(const std::string& local, |
| 3622 |
const std::string& remote) |
| 3623 |
{ |
| 3624 |
if (!SystemTools::FileIsFullPath(local)) { |
| 3625 |
return ""; |
| 3626 |
} |
| 3627 |
if (!SystemTools::FileIsFullPath(remote)) { |
| 3628 |
return ""; |
| 3629 |
} |
| 3630 |
|
| 3631 |
std::string l = SystemTools::CollapseFullPath(local); |
| 3632 |
std::string r = SystemTools::CollapseFullPath(remote); |
| 3633 |
|
| 3634 |
// split up both paths into arrays of strings using / as a separator |
| 3635 |
std::vector<std::string> localSplit = SystemTools::SplitString(l, '/', true); |
| 3636 |
std::vector<std::string> remoteSplit = |
| 3637 |
SystemTools::SplitString(r, '/', true); |
| 3638 |
std::vector<std::string> |
| 3639 |
commonPath; // store shared parts of path in this array |
| 3640 |
std::vector<std::string> finalPath; // store the final relative path here |
| 3641 |
// count up how many matching directory names there are from the start |
| 3642 |
unsigned int sameCount = 0; |
| 3643 |
while (((sameCount <= (localSplit.size() - 1)) && |
| 3644 |
(sameCount <= (remoteSplit.size() - 1))) && |
| 3645 |
// for Windows and Apple do a case insensitive string compare |
| 3646 |
#if defined(_WIN32) || defined(__APPLE__) |
| 3647 |
SystemTools::Strucmp(localSplit[sameCount].c_str(), |
| 3648 |
remoteSplit[sameCount].c_str()) == 0 |
| 3649 |
#else |
| 3650 |
localSplit[sameCount] == remoteSplit[sameCount] |
| 3651 |
#endif |
| 3652 |
) { |
| 3653 |
// put the common parts of the path into the commonPath array |
| 3654 |
commonPath.push_back(localSplit[sameCount]); |
| 3655 |
// erase the common parts of the path from the original path arrays |
| 3656 |
localSplit[sameCount] = ""; |
| 3657 |
remoteSplit[sameCount] = ""; |
| 3658 |
sameCount++; |
| 3659 |
} |
| 3660 |
|
| 3661 |
// If there is nothing in common at all then just return the full |
| 3662 |
// path. This is the case only on windows when the paths have |
| 3663 |
// different drive letters. On unix two full paths always at least |
| 3664 |
// have the root "/" in common so we will return a relative path |
| 3665 |
// that passes through the root directory. |
| 3666 |
if (sameCount == 0) { |
| 3667 |
return remote; |
| 3668 |
} |
| 3669 |
|
| 3670 |
// for each entry that is not common in the local path |
| 3671 |
// add a ../ to the finalpath array, this gets us out of the local |
| 3672 |
// path into the remote dir |
| 3673 |
for (std::string const& lp : localSplit) { |
| 3674 |
if (!lp.empty()) { |
| 3675 |
finalPath.emplace_back("../"); |
| 3676 |
} |
| 3677 |
} |
| 3678 |
// for each entry that is not common in the remote path add it |
| 3679 |
// to the final path. |
| 3680 |
for (std::string const& rp : remoteSplit) { |
| 3681 |
if (!rp.empty()) { |
| 3682 |
finalPath.push_back(rp); |
| 3683 |
} |
| 3684 |
} |
| 3685 |
std::string relativePath; // result string |
| 3686 |
// now turn the array of directories into a unix path by puttint / |
| 3687 |
// between each entry that does not already have one |
| 3688 |
for (std::string const& fp : finalPath) { |
| 3689 |
if (!relativePath.empty() && relativePath.back() != '/') { |
| 3690 |
relativePath += '/'; |
| 3691 |
} |
| 3692 |
relativePath += fp; |
| 3693 |
} |
| 3694 |
return relativePath; |
| 3695 |
} |
| 3696 |
|
| 3697 |
std::string SystemTools::GetActualCaseForPath(const std::string& p) |
| 3698 |
{ |
| 3699 |
#ifdef _WIN32 |
| 3700 |
return SystemToolsStatic::GetCasePathName(p, false); |
| 3701 |
#else |
| 3702 |
return p; |
| 3703 |
#endif |
| 3704 |
} |
| 3705 |
|
| 3706 |
const char* SystemTools::SplitPathRootComponent(const std::string& p, |
| 3707 |
std::string* root) |
| 3708 |
{ |
| 3709 |
// Identify the root component. |
| 3710 |
const char* c = p.c_str(); |
| 3711 |
if ((c[0] == '/' && c[1] == '/') || (c[0] == '\\' && c[1] == '\\')) { |
| 3712 |
// Network path. |
| 3713 |
if (root) { |
| 3714 |
*root = "//"; |
| 3715 |
} |
| 3716 |
c += 2; |
| 3717 |
} else if (c[0] == '/' || c[0] == '\\') { |
| 3718 |
// Unix path (or Windows path w/out drive letter). |
| 3719 |
if (root) { |
| 3720 |
*root = "/"; |
| 3721 |
} |
| 3722 |
c += 1; |
| 3723 |
} else if (c[0] && c[1] == ':' && (c[2] == '/' || c[2] == '\\')) { |
| 3724 |
// Windows path. |
| 3725 |
if (root) { |
| 3726 |
(*root) = "_:/"; |
| 3727 |
(*root)[0] = c[0]; |
| 3728 |
} |
| 3729 |
c += 3; |
| 3730 |
} else if (c[0] && c[1] == ':') { |
| 3731 |
// Path relative to a windows drive working directory. |
| 3732 |
if (root) { |
| 3733 |
(*root) = "_:"; |
| 3734 |
(*root)[0] = c[0]; |
| 3735 |
} |
| 3736 |
c += 2; |
| 3737 |
} else if (c[0] == '~') { |
| 3738 |
// Home directory. The returned root should always have a |
| 3739 |
// trailing slash so that appending components as |
| 3740 |
// c[0]c[1]/c[2]/... works. The remaining path returned should |
| 3741 |
// skip the first slash if it exists: |
| 3742 |
// |
| 3743 |
// "~" : root = "~/" , return "" |
| 3744 |
// "~/ : root = "~/" , return "" |
| 3745 |
// "~/x : root = "~/" , return "x" |
| 3746 |
// "~u" : root = "~u/", return "" |
| 3747 |
// "~u/" : root = "~u/", return "" |
| 3748 |
// "~u/x" : root = "~u/", return "x" |
| 3749 |
size_t n = 1; |
| 3750 |
while (c[n] && c[n] != '/') { |
| 3751 |
++n; |
| 3752 |
} |
| 3753 |
if (root) { |
| 3754 |
root->assign(c, n); |
| 3755 |
*root += '/'; |
| 3756 |
} |
| 3757 |
if (c[n] == '/') { |
| 3758 |
++n; |
| 3759 |
} |
| 3760 |
c += n; |
| 3761 |
} else { |
| 3762 |
// Relative path. |
| 3763 |
if (root) { |
| 3764 |
*root = ""; |
| 3765 |
} |
| 3766 |
} |
| 3767 |
|
| 3768 |
// Return the remaining path. |
| 3769 |
return c; |
| 3770 |
} |
| 3771 |
|
| 3772 |
void SystemTools::SplitPath(const std::string& p, |
| 3773 |
std::vector<std::string>& components, |
| 3774 |
bool expand_home_dir) |
| 3775 |
{ |
| 3776 |
const char* c; |
| 3777 |
components.clear(); |
| 3778 |
|
| 3779 |
// Identify the root component. |
| 3780 |
{ |
| 3781 |
std::string root; |
| 3782 |
c = SystemTools::SplitPathRootComponent(p, &root); |
| 3783 |
|
| 3784 |
// Expand home directory references if requested. |
| 3785 |
if (expand_home_dir && !root.empty() && root[0] == '~') { |
| 3786 |
std::string homedir; |
| 3787 |
root.resize(root.size() - 1); |
| 3788 |
if (root.size() == 1) { |
| 3789 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 3790 |
if (!SystemTools::GetEnv("USERPROFILE", homedir)) |
| 3791 |
#endif |
| 3792 |
SystemTools::GetEnv("HOME", homedir); |
| 3793 |
} |
| 3794 |
#ifdef HAVE_GETPWNAM |
| 3795 |
else if (passwd* pw = getpwnam(root.c_str() + 1)) { |
| 3796 |
if (pw->pw_dir) { |
| 3797 |
homedir = pw->pw_dir; |
| 3798 |
} |
| 3799 |
} |
| 3800 |
#endif |
| 3801 |
if (!homedir.empty() && |
| 3802 |
(homedir.back() == '/' || homedir.back() == '\\')) { |
| 3803 |
homedir.resize(homedir.size() - 1); |
| 3804 |
} |
| 3805 |
SystemTools::SplitPath(homedir, components); |
| 3806 |
} else { |
| 3807 |
components.push_back(root); |
| 3808 |
} |
| 3809 |
} |
| 3810 |
|
| 3811 |
// Parse the remaining components. |
| 3812 |
const char* first = c; |
| 3813 |
const char* last = first; |
| 3814 |
for (; *last; ++last) { |
| 3815 |
if (*last == '/' || *last == '\\') { |
| 3816 |
// End of a component. Save it. |
| 3817 |
components.emplace_back(first, last); |
| 3818 |
first = last + 1; |
| 3819 |
} |
| 3820 |
} |
| 3821 |
|
| 3822 |
// Save the last component unless there were no components. |
| 3823 |
if (last != c) { |
| 3824 |
components.emplace_back(first, last); |
| 3825 |
} |
| 3826 |
} |
| 3827 |
|
| 3828 |
std::string SystemTools::JoinPath(const std::vector<std::string>& components) |
| 3829 |
{ |
| 3830 |
return SystemTools::JoinPath(components.begin(), components.end()); |
| 3831 |
} |
| 3832 |
|
| 3833 |
std::string SystemTools::JoinPath( |
| 3834 |
std::vector<std::string>::const_iterator first, |
| 3835 |
std::vector<std::string>::const_iterator last) |
| 3836 |
{ |
| 3837 |
// Construct result in a single string. |
| 3838 |
std::string result; |
| 3839 |
size_t len = 0; |
| 3840 |
for (auto i = first; i != last; ++i) { |
| 3841 |
len += 1 + i->size(); |
| 3842 |
} |
| 3843 |
result.reserve(len); |
| 3844 |
|
| 3845 |
// The first two components do not add a slash. |
| 3846 |
if (first != last) { |
| 3847 |
result.append(*first++); |
| 3848 |
} |
| 3849 |
if (first != last) { |
| 3850 |
result.append(*first++); |
| 3851 |
} |
| 3852 |
|
| 3853 |
// All remaining components are always separated with a slash. |
| 3854 |
while (first != last) { |
| 3855 |
result.push_back('/'); |
| 3856 |
result.append((*first++)); |
| 3857 |
} |
| 3858 |
|
| 3859 |
// Return the concatenated result. |
| 3860 |
return result; |
| 3861 |
} |
| 3862 |
|
| 3863 |
bool SystemTools::ComparePath(const std::string& c1, const std::string& c2) |
| 3864 |
{ |
| 3865 |
#if defined(_WIN32) || defined(__APPLE__) |
| 3866 |
# ifdef _MSC_VER |
| 3867 |
return _stricmp(c1.c_str(), c2.c_str()) == 0; |
| 3868 |
# elif defined(__APPLE__) || defined(__GNUC__) |
| 3869 |
return strcasecmp(c1.c_str(), c2.c_str()) == 0; |
| 3870 |
# else |
| 3871 |
return SystemTools::Strucmp(c1.c_str(), c2.c_str()) == 0; |
| 3872 |
# endif |
| 3873 |
#else |
| 3874 |
return c1 == c2; |
| 3875 |
#endif |
| 3876 |
} |
| 3877 |
|
| 3878 |
bool SystemTools::Split(const std::string& str, |
| 3879 |
std::vector<std::string>& lines, char separator) |
| 3880 |
{ |
| 3881 |
std::string data(str); |
| 3882 |
std::string::size_type lpos = 0; |
| 3883 |
while (lpos < data.length()) { |
| 3884 |
std::string::size_type rpos = data.find_first_of(separator, lpos); |
| 3885 |
if (rpos == std::string::npos) { |
| 3886 |
// String ends at end of string without a separator. |
| 3887 |
lines.push_back(data.substr(lpos)); |
| 3888 |
return false; |
| 3889 |
} else { |
| 3890 |
// String ends in a separator, remove the character. |
| 3891 |
lines.push_back(data.substr(lpos, rpos - lpos)); |
| 3892 |
} |
| 3893 |
lpos = rpos + 1; |
| 3894 |
} |
| 3895 |
return true; |
| 3896 |
} |
| 3897 |
|
| 3898 |
bool SystemTools::Split(const std::string& str, |
| 3899 |
std::vector<std::string>& lines) |
| 3900 |
{ |
| 3901 |
std::string data(str); |
| 3902 |
std::string::size_type lpos = 0; |
| 3903 |
while (lpos < data.length()) { |
| 3904 |
std::string::size_type rpos = data.find_first_of('\n', lpos); |
| 3905 |
if (rpos == std::string::npos) { |
| 3906 |
// Line ends at end of string without a newline. |
| 3907 |
lines.push_back(data.substr(lpos)); |
| 3908 |
return false; |
| 3909 |
} |
| 3910 |
if ((rpos > lpos) && (data[rpos - 1] == '\r')) { |
| 3911 |
// Line ends in a "\r\n" pair, remove both characters. |
| 3912 |
lines.push_back(data.substr(lpos, (rpos - 1) - lpos)); |
| 3913 |
} else { |
| 3914 |
// Line ends in a "\n", remove the character. |
| 3915 |
lines.push_back(data.substr(lpos, rpos - lpos)); |
| 3916 |
} |
| 3917 |
lpos = rpos + 1; |
| 3918 |
} |
| 3919 |
return true; |
| 3920 |
} |
| 3921 |
|
| 3922 |
std::string SystemTools::Join(const std::vector<std::string>& list, |
| 3923 |
const std::string& separator) |
| 3924 |
{ |
| 3925 |
std::string result; |
| 3926 |
if (list.empty()) { |
| 3927 |
return result; |
| 3928 |
} |
| 3929 |
|
| 3930 |
size_t total_size = separator.size() * (list.size() - 1); |
| 3931 |
for (const std::string& string : list) { |
| 3932 |
total_size += string.size(); |
| 3933 |
} |
| 3934 |
|
| 3935 |
result.reserve(total_size); |
| 3936 |
bool needs_separator = false; |
| 3937 |
for (const std::string& string : list) { |
| 3938 |
if (needs_separator) { |
| 3939 |
result += separator; |
| 3940 |
} |
| 3941 |
result += string; |
| 3942 |
needs_separator = true; |
| 3943 |
} |
| 3944 |
|
| 3945 |
return result; |
| 3946 |
} |
| 3947 |
|
| 3948 |
/** |
| 3949 |
* Return path of a full filename (no trailing slashes). |
| 3950 |
* Warning: returned path is converted to Unix slashes format. |
| 3951 |
*/ |
| 3952 |
std::string SystemTools::GetFilenamePath(const std::string& filename) |
| 3953 |
{ |
| 3954 |
std::string fn = filename; |
| 3955 |
SystemTools::ConvertToUnixSlashes(fn); |
| 3956 |
|
| 3957 |
std::string::size_type slash_pos = fn.rfind('/'); |
| 3958 |
if (slash_pos == 0) { |
| 3959 |
return "/"; |
| 3960 |
} |
| 3961 |
if (slash_pos == 2 && fn[1] == ':') { |
| 3962 |
// keep the / after a drive letter |
| 3963 |
fn.resize(3); |
| 3964 |
return fn; |
| 3965 |
} |
| 3966 |
if (slash_pos == std::string::npos) { |
| 3967 |
return ""; |
| 3968 |
} |
| 3969 |
fn.resize(slash_pos); |
| 3970 |
return fn; |
| 3971 |
} |
| 3972 |
|
| 3973 |
/** |
| 3974 |
* Return file name of a full filename (i.e. file name without path). |
| 3975 |
*/ |
| 3976 |
std::string SystemTools::GetFilenameName(const std::string& filename) |
| 3977 |
{ |
| 3978 |
#if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES) |
| 3979 |
const char* separators = "/\\"; |
| 3980 |
#else |
| 3981 |
char separators = '/'; |
| 3982 |
#endif |
| 3983 |
std::string::size_type slash_pos = filename.find_last_of(separators); |
| 3984 |
if (slash_pos != std::string::npos) { |
| 3985 |
return filename.substr(slash_pos + 1); |
| 3986 |
} else { |
| 3987 |
return filename; |
| 3988 |
} |
| 3989 |
} |
| 3990 |
|
| 3991 |
/** |
| 3992 |
* Return file extension of a full filename (dot included). |
| 3993 |
* Warning: this is the longest extension (for example: .tar.gz) |
| 3994 |
*/ |
| 3995 |
std::string SystemTools::GetFilenameExtension(const std::string& filename) |
| 3996 |
{ |
| 3997 |
std::string name = SystemTools::GetFilenameName(filename); |
| 3998 |
std::string::size_type dot_pos = name.find('.'); |
| 3999 |
if (dot_pos != std::string::npos) { |
| 4000 |
name.erase(0, dot_pos); |
| 4001 |
return name; |
| 4002 |
} else { |
| 4003 |
return ""; |
| 4004 |
} |
| 4005 |
} |
| 4006 |
|
| 4007 |
/** |
| 4008 |
* Return file extension of a full filename (dot included). |
| 4009 |
* Warning: this is the shortest extension (for example: .gz of .tar.gz) |
| 4010 |
*/ |
| 4011 |
std::string SystemTools::GetFilenameLastExtension(const std::string& filename) |
| 4012 |
{ |
| 4013 |
std::string name = SystemTools::GetFilenameName(filename); |
| 4014 |
std::string::size_type dot_pos = name.rfind('.'); |
| 4015 |
if (dot_pos != std::string::npos) { |
| 4016 |
name.erase(0, dot_pos); |
| 4017 |
return name; |
| 4018 |
} else { |
| 4019 |
return ""; |
| 4020 |
} |
| 4021 |
} |
| 4022 |
|
| 4023 |
/** |
| 4024 |
* Return file name without extension of a full filename (i.e. without path). |
| 4025 |
* Warning: it considers the longest extension (for example: .tar.gz) |
| 4026 |
*/ |
| 4027 |
std::string SystemTools::GetFilenameWithoutExtension( |
| 4028 |
const std::string& filename) |
| 4029 |
{ |
| 4030 |
std::string name = SystemTools::GetFilenameName(filename); |
| 4031 |
std::string::size_type dot_pos = name.find('.'); |
| 4032 |
if (dot_pos != std::string::npos) { |
| 4033 |
name.resize(dot_pos); |
| 4034 |
} |
| 4035 |
return name; |
| 4036 |
} |
| 4037 |
|
| 4038 |
/** |
| 4039 |
* Return file name without extension of a full filename (i.e. without path). |
| 4040 |
* Warning: it considers the last extension (for example: removes .gz |
| 4041 |
* from .tar.gz) |
| 4042 |
*/ |
| 4043 |
std::string SystemTools::GetFilenameWithoutLastExtension( |
| 4044 |
const std::string& filename) |
| 4045 |
{ |
| 4046 |
std::string name = SystemTools::GetFilenameName(filename); |
| 4047 |
std::string::size_type dot_pos = name.rfind('.'); |
| 4048 |
if (dot_pos != std::string::npos) { |
| 4049 |
name.resize(dot_pos); |
| 4050 |
} |
| 4051 |
return name; |
| 4052 |
} |
| 4053 |
|
| 4054 |
bool SystemTools::FileHasSignature(const char* filename, const char* signature, |
| 4055 |
long offset) |
| 4056 |
{ |
| 4057 |
if (!filename || !signature) { |
| 4058 |
return false; |
| 4059 |
} |
| 4060 |
|
| 4061 |
FILE* fp = Fopen(filename, "rb"); |
| 4062 |
if (!fp) { |
| 4063 |
return false; |
| 4064 |
} |
| 4065 |
|
| 4066 |
fseek(fp, offset, SEEK_SET); |
| 4067 |
|
| 4068 |
bool res = false; |
| 4069 |
size_t signature_len = strlen(signature); |
| 4070 |
char* buffer = new char[signature_len]; |
| 4071 |
|
| 4072 |
if (fread(buffer, 1, signature_len, fp) == signature_len) { |
| 4073 |
res = (!strncmp(buffer, signature, signature_len) ? true : false); |
| 4074 |
} |
| 4075 |
|
| 4076 |
delete[] buffer; |
| 4077 |
|
| 4078 |
fclose(fp); |
| 4079 |
return res; |
| 4080 |
} |
| 4081 |
|
| 4082 |
SystemTools::FileTypeEnum SystemTools::DetectFileType(const char* filename, |
| 4083 |
unsigned long length, |
| 4084 |
double percent_bin) |
| 4085 |
{ |
| 4086 |
if (!filename || percent_bin < 0) { |
| 4087 |
return SystemTools::FileTypeUnknown; |
| 4088 |
} |
| 4089 |
|
| 4090 |
if (SystemTools::FileIsDirectory(filename)) { |
| 4091 |
return SystemTools::FileTypeUnknown; |
| 4092 |
} |
| 4093 |
|
| 4094 |
FILE* fp = Fopen(filename, "rb"); |
| 4095 |
if (!fp) { |
| 4096 |
return SystemTools::FileTypeUnknown; |
| 4097 |
} |
| 4098 |
|
| 4099 |
// Allocate buffer and read bytes |
| 4100 |
|
| 4101 |
auto* buffer = new unsigned char[length]; |
| 4102 |
size_t read_length = fread(buffer, 1, length, fp); |
| 4103 |
fclose(fp); |
| 4104 |
if (read_length == 0) { |
| 4105 |
delete[] buffer; |
| 4106 |
return SystemTools::FileTypeUnknown; |
| 4107 |
} |
| 4108 |
|
| 4109 |
// Loop over contents and count |
| 4110 |
|
| 4111 |
size_t text_count = 0; |
| 4112 |
|
| 4113 |
const unsigned char* ptr = buffer; |
| 4114 |
const unsigned char* buffer_end = buffer + read_length; |
| 4115 |
|
| 4116 |
while (ptr != buffer_end) { |
| 4117 |
if ((*ptr >= 0x20 && *ptr <= 0x7F) || *ptr == '\n' || *ptr == '\r' || |
| 4118 |
*ptr == '\t') { |
| 4119 |
text_count++; |
| 4120 |
} |
| 4121 |
ptr++; |
| 4122 |
} |
| 4123 |
|
| 4124 |
delete[] buffer; |
| 4125 |
|
| 4126 |
double current_percent_bin = (static_cast<double>(read_length - text_count) / |
| 4127 |
static_cast<double>(read_length)); |
| 4128 |
|
| 4129 |
if (current_percent_bin >= percent_bin) { |
| 4130 |
return SystemTools::FileTypeBinary; |
| 4131 |
} |
| 4132 |
|
| 4133 |
return SystemTools::FileTypeText; |
| 4134 |
} |
| 4135 |
|
| 4136 |
bool SystemTools::LocateFileInDir(const char* filename, const char* dir, |
| 4137 |
std::string& filename_found, |
| 4138 |
int try_filename_dirs) |
| 4139 |
{ |
| 4140 |
if (!filename || !dir) { |
| 4141 |
return false; |
| 4142 |
} |
| 4143 |
|
| 4144 |
// Get the basename of 'filename' |
| 4145 |
|
| 4146 |
std::string filename_base = SystemTools::GetFilenameName(filename); |
| 4147 |
|
| 4148 |
// Check if 'dir' is really a directory |
| 4149 |
// If win32 and matches something like C:, accept it as a dir |
| 4150 |
|
| 4151 |
std::string real_dir; |
| 4152 |
if (!SystemTools::FileIsDirectory(dir)) { |
| 4153 |
#if defined(_WIN32) |
| 4154 |
size_t dir_len = strlen(dir); |
| 4155 |
if (dir_len < 2 || dir[dir_len - 1] != ':') { |
| 4156 |
#endif |
| 4157 |
real_dir = SystemTools::GetFilenamePath(dir); |
| 4158 |
dir = real_dir.c_str(); |
| 4159 |
#if defined(_WIN32) |
| 4160 |
} |
| 4161 |
#endif |
| 4162 |
} |
| 4163 |
|
| 4164 |
// Try to find the file in 'dir' |
| 4165 |
|
| 4166 |
bool res = false; |
| 4167 |
if (!filename_base.empty() && dir) { |
| 4168 |
size_t dir_len = strlen(dir); |
| 4169 |
int need_slash = |
| 4170 |
(dir_len && dir[dir_len - 1] != '/' && dir[dir_len - 1] != '\\'); |
| 4171 |
|
| 4172 |
std::string temp = dir; |
| 4173 |
if (need_slash) { |
| 4174 |
temp += "/"; |
| 4175 |
} |
| 4176 |
temp += filename_base; |
| 4177 |
|
| 4178 |
if (SystemTools::FileExists(temp)) { |
| 4179 |
res = true; |
| 4180 |
filename_found = temp; |
| 4181 |
} |
| 4182 |
|
| 4183 |
// If not found, we can try harder by appending part of the file to |
| 4184 |
// to the directory to look inside. |
| 4185 |
// Example: if we were looking for /foo/bar/yo.txt in /d1/d2, then |
| 4186 |
// try to find yo.txt in /d1/d2/bar, then /d1/d2/foo/bar, etc. |
| 4187 |
|
| 4188 |
else if (try_filename_dirs) { |
| 4189 |
std::string filename_dir(filename); |
| 4190 |
std::string filename_dir_base; |
| 4191 |
std::string filename_dir_bases; |
| 4192 |
do { |
| 4193 |
filename_dir = SystemTools::GetFilenamePath(filename_dir); |
| 4194 |
filename_dir_base = SystemTools::GetFilenameName(filename_dir); |
| 4195 |
#if defined(_WIN32) |
| 4196 |
if (filename_dir_base.empty() || filename_dir_base.back() == ':') |
| 4197 |
#else |
| 4198 |
if (filename_dir_base.empty()) |
| 4199 |
#endif |
| 4200 |
{ |
| 4201 |
break; |
| 4202 |
} |
| 4203 |
|
| 4204 |
filename_dir_bases = filename_dir_base + "/" + filename_dir_bases; |
| 4205 |
|
| 4206 |
temp = dir; |
| 4207 |
if (need_slash) { |
| 4208 |
temp += "/"; |
| 4209 |
} |
| 4210 |
temp += filename_dir_bases; |
| 4211 |
|
| 4212 |
res = SystemTools::LocateFileInDir(filename_base.c_str(), temp.c_str(), |
| 4213 |
filename_found, 0); |
| 4214 |
|
| 4215 |
} while (!res && !filename_dir_base.empty()); |
| 4216 |
} |
| 4217 |
} |
| 4218 |
|
| 4219 |
return res; |
| 4220 |
} |
| 4221 |
|
| 4222 |
bool SystemTools::FileIsFullPath(const std::string& in_name) |
| 4223 |
{ |
| 4224 |
return SystemToolsStatic::FileIsFullPath(in_name.c_str(), in_name.size()); |
| 4225 |
} |
| 4226 |
|
| 4227 |
bool SystemTools::FileIsFullPath(const char* in_name) |
| 4228 |
{ |
| 4229 |
return SystemToolsStatic::FileIsFullPath( |
| 4230 |
in_name, in_name[0] ? (in_name[1] ? 2 : 1) : 0); |
| 4231 |
} |
| 4232 |
|
| 4233 |
bool SystemToolsStatic::FileIsFullPath(const char* in_name, size_t len) |
| 4234 |
{ |
| 4235 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 4236 |
// On Windows, the name must be at least two characters long. |
| 4237 |
if (len < 2) { |
| 4238 |
return false; |
| 4239 |
} |
| 4240 |
if (in_name[1] == ':') { |
| 4241 |
return true; |
| 4242 |
} |
| 4243 |
if (in_name[0] == '\\') { |
| 4244 |
return true; |
| 4245 |
} |
| 4246 |
#else |
| 4247 |
// On UNIX, the name must be at least one character long. |
| 4248 |
if (len < 1) { |
| 4249 |
return false; |
| 4250 |
} |
| 4251 |
#endif |
| 4252 |
#if !defined(_WIN32) |
| 4253 |
if (in_name[0] == '~') { |
| 4254 |
return true; |
| 4255 |
} |
| 4256 |
#endif |
| 4257 |
// On UNIX, the name must begin in a '/'. |
| 4258 |
// On Windows, if the name begins in a '/', then it is a full |
| 4259 |
// network path. |
| 4260 |
if (in_name[0] == '/') { |
| 4261 |
return true; |
| 4262 |
} |
| 4263 |
return false; |
| 4264 |
} |
| 4265 |
|
| 4266 |
Status SystemTools::GetShortPath(std::string const& path, |
| 4267 |
std::string& shortPath) |
| 4268 |
{ |
| 4269 |
#if defined(_WIN32) && !defined(__CYGWIN__) |
| 4270 |
std::string tempPath = path; // create a buffer |
| 4271 |
|
| 4272 |
// if the path passed in has quotes around it, first remove the quotes |
| 4273 |
if (!path.empty() && path[0] == '"' && path.back() == '"') { |
| 4274 |
tempPath.resize(path.length() - 1); |
| 4275 |
tempPath.erase(0, 1); |
| 4276 |
} |
| 4277 |
|
| 4278 |
std::wstring wtempPath = Encoding::ToWide(tempPath); |
| 4279 |
DWORD ret = GetShortPathNameW(wtempPath.c_str(), nullptr, 0); |
| 4280 |
std::vector<wchar_t> buffer(ret); |
| 4281 |
if (ret != 0) { |
| 4282 |
ret = GetShortPathNameW(wtempPath.c_str(), &buffer[0], |
| 4283 |
static_cast<DWORD>(buffer.size())); |
| 4284 |
} |
| 4285 |
|
| 4286 |
if (ret == 0) { |
| 4287 |
return Status::Windows_GetLastError(); |
| 4288 |
} else { |
| 4289 |
shortPath = Encoding::ToNarrow(&buffer[0]); |
| 4290 |
return Status::Success(); |
| 4291 |
} |
| 4292 |
#else |
| 4293 |
shortPath = path; |
| 4294 |
return Status::Success(); |
| 4295 |
#endif |
| 4296 |
} |
| 4297 |
|
| 4298 |
std::string SystemTools::GetCurrentDateTime(const char* format) |
| 4299 |
{ |
| 4300 |
char buf[1024]; |
| 4301 |
time_t t; |
| 4302 |
time(&t); |
| 4303 |
strftime(buf, sizeof(buf), format, localtime(&t)); |
| 4304 |
return std::string(buf); |
| 4305 |
} |
| 4306 |
|
| 4307 |
std::string SystemTools::MakeCidentifier(const std::string& s) |
| 4308 |
{ |
| 4309 |
std::string str(s); |
| 4310 |
if (str.find_first_of("0123456789") == 0) { |
| 4311 |
str = "_" + str; |
| 4312 |
} |
| 4313 |
|
| 4314 |
std::string permited_chars("_" |
| 4315 |
"abcdefghijklmnopqrstuvwxyz" |
| 4316 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4317 |
"0123456789"); |
| 4318 |
std::string::size_type pos = 0; |
| 4319 |
while ((pos = str.find_first_not_of(permited_chars, pos)) != |
| 4320 |
std::string::npos) { |
| 4321 |
str[pos] = '_'; |
| 4322 |
} |
| 4323 |
return str; |
| 4324 |
} |
| 4325 |
|
| 4326 |
// Convenience function around std::getline which removes a trailing carriage |
| 4327 |
// return and can truncate the buffer as needed. Returns true |
| 4328 |
// if any data were read before the end-of-file was reached. |
| 4329 |
bool SystemTools::GetLineFromStream( |
| 4330 |
std::istream& is, std::string& line, bool* has_newline /* = 0 */, |
| 4331 |
std::string::size_type sizeLimit /* = std::string::npos */) |
| 4332 |
{ |
| 4333 |
// Start with an empty line. |
| 4334 |
line = ""; |
| 4335 |
|
| 4336 |
// Early short circuit return if stream is no good. Just return |
| 4337 |
// false and the empty line. (Probably means caller tried to |
| 4338 |
// create a file stream with a non-existent file name...) |
| 4339 |
// |
| 4340 |
if (!is) { |
| 4341 |
if (has_newline) { |
| 4342 |
*has_newline = false; |
| 4343 |
} |
| 4344 |
return false; |
| 4345 |
} |
| 4346 |
|
| 4347 |
std::getline(is, line); |
| 4348 |
bool haveData = !line.empty() || !is.eof(); |
| 4349 |
if (!line.empty()) { |
| 4350 |
// Avoid storing a carriage return character. |
| 4351 |
if (line.back() == '\r') { |
| 4352 |
line.resize(line.size() - 1); |
| 4353 |
} |
| 4354 |
|
| 4355 |
// if we read too much then truncate the buffer |
| 4356 |
if (sizeLimit != std::string::npos && line.size() > sizeLimit) { |
| 4357 |
line.resize(sizeLimit); |
| 4358 |
} |
| 4359 |
} |
| 4360 |
|
| 4361 |
// Return the results. |
| 4362 |
if (has_newline) { |
| 4363 |
*has_newline = !is.eof(); |
| 4364 |
} |
| 4365 |
return haveData; |
| 4366 |
} |
| 4367 |
|
| 4368 |
int SystemTools::GetTerminalWidth() |
| 4369 |
{ |
| 4370 |
int width = -1; |
| 4371 |
#ifdef HAVE_TTY_INFO |
| 4372 |
struct winsize ws; |
| 4373 |
std::string columns; /* Unix98 environment variable */ |
| 4374 |
if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0 && ws.ws_row > 0) { |
| 4375 |
width = ws.ws_col; |
| 4376 |
} |
| 4377 |
if (!isatty(STDOUT_FILENO)) { |
| 4378 |
width = -1; |
| 4379 |
} |
| 4380 |
if (SystemTools::GetEnv("COLUMNS", columns) && !columns.empty()) { |
| 4381 |
long t; |
| 4382 |
char* endptr; |
| 4383 |
t = strtol(columns.c_str(), &endptr, 0); |
| 4384 |
if (endptr && !*endptr && (t > 0) && (t < 1000)) { |
| 4385 |
width = static_cast<int>(t); |
| 4386 |
} |
| 4387 |
} |
| 4388 |
if (width < 9) { |
| 4389 |
width = -1; |
| 4390 |
} |
| 4391 |
#endif |
| 4392 |
return width; |
| 4393 |
} |
| 4394 |
|
| 4395 |
Status SystemTools::GetPermissions(const char* file, mode_t& mode) |
| 4396 |
{ |
| 4397 |
if (!file) { |
| 4398 |
return Status::POSIX(EINVAL); |
| 4399 |
} |
| 4400 |
return SystemTools::GetPermissions(std::string(file), mode); |
| 4401 |
} |
| 4402 |
|
| 4403 |
Status SystemTools::GetPermissions(std::string const& file, mode_t& mode) |
| 4404 |
{ |
| 4405 |
#if defined(_WIN32) |
| 4406 |
DWORD attr = |
| 4407 |
GetFileAttributesW(Encoding::ToWindowsExtendedPath(file).c_str()); |
| 4408 |
if (attr == INVALID_FILE_ATTRIBUTES) { |
| 4409 |
return Status::Windows_GetLastError(); |
| 4410 |
} |
| 4411 |
if ((attr & FILE_ATTRIBUTE_READONLY) != 0) { |
| 4412 |
mode = (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6)); |
| 4413 |
} else { |
| 4414 |
mode = (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6)) | |
| 4415 |
(_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6)); |
| 4416 |
} |
| 4417 |
if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 4418 |
mode |= S_IFDIR | (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6)); |
| 4419 |
} else { |
| 4420 |
mode |= S_IFREG; |
| 4421 |
} |
| 4422 |
size_t dotPos = file.rfind('.'); |
| 4423 |
const char* ext = dotPos == std::string::npos ? 0 : (file.c_str() + dotPos); |
| 4424 |
if (ext && |
| 4425 |
(Strucmp(ext, ".exe") == 0 || Strucmp(ext, ".com") == 0 || |
| 4426 |
Strucmp(ext, ".cmd") == 0 || Strucmp(ext, ".bat") == 0)) { |
| 4427 |
mode |= (_S_IEXEC | (_S_IEXEC >> 3) | (_S_IEXEC >> 6)); |
| 4428 |
} |
| 4429 |
#else |
| 4430 |
struct stat st; |
| 4431 |
if (stat(file.c_str(), &st) < 0) { |
| 4432 |
return Status::POSIX_errno(); |
| 4433 |
} |
| 4434 |
mode = st.st_mode; |
| 4435 |
#endif |
| 4436 |
return Status::Success(); |
| 4437 |
} |
| 4438 |
|
| 4439 |
Status SystemTools::SetPermissions(const char* file, mode_t mode, |
| 4440 |
bool honor_umask) |
| 4441 |
{ |
| 4442 |
if (!file) { |
| 4443 |
return Status::POSIX(EINVAL); |
| 4444 |
} |
| 4445 |
return SystemTools::SetPermissions(std::string(file), mode, honor_umask); |
| 4446 |
} |
| 4447 |
|
| 4448 |
Status SystemTools::SetPermissions(std::string const& file, mode_t mode, |
| 4449 |
bool honor_umask) |
| 4450 |
{ |
| 4451 |
if (!SystemTools::PathExists(file)) { |
| 4452 |
return Status::POSIX(ENOENT); |
| 4453 |
} |
| 4454 |
if (honor_umask) { |
| 4455 |
mode_t currentMask = umask(0); |
| 4456 |
umask(currentMask); |
| 4457 |
mode &= ~currentMask; |
| 4458 |
} |
| 4459 |
#ifdef _WIN32 |
| 4460 |
if (_wchmod(Encoding::ToWindowsExtendedPath(file).c_str(), mode) < 0) |
| 4461 |
#else |
| 4462 |
if (chmod(file.c_str(), mode) < 0) |
| 4463 |
#endif |
| 4464 |
{ |
| 4465 |
return Status::POSIX_errno(); |
| 4466 |
} |
| 4467 |
|
| 4468 |
return Status::Success(); |
| 4469 |
} |
| 4470 |
|
| 4471 |
std::string SystemTools::GetParentDirectory(const std::string& fileOrDir) |
| 4472 |
{ |
| 4473 |
return SystemTools::GetFilenamePath(fileOrDir); |
| 4474 |
} |
| 4475 |
|
| 4476 |
bool SystemTools::IsSubDirectory(const std::string& cSubdir, |
| 4477 |
const std::string& cDir) |
| 4478 |
{ |
| 4479 |
if (cDir.empty()) { |
| 4480 |
return false; |
| 4481 |
} |
| 4482 |
std::string subdir = cSubdir; |
| 4483 |
std::string dir = cDir; |
| 4484 |
SystemTools::ConvertToUnixSlashes(subdir); |
| 4485 |
SystemTools::ConvertToUnixSlashes(dir); |
| 4486 |
if (subdir.size() <= dir.size() || dir.empty()) { |
| 4487 |
return false; |
| 4488 |
} |
| 4489 |
bool isRootPath = dir.back() == '/'; // like "/" or "C:/" |
| 4490 |
size_t expectedSlashPosition = isRootPath ? dir.size() - 1u : dir.size(); |
| 4491 |
if (subdir[expectedSlashPosition] != '/') { |
| 4492 |
return false; |
| 4493 |
} |
| 4494 |
subdir.resize(dir.size()); |
| 4495 |
return SystemTools::ComparePath(subdir, dir); |
| 4496 |
} |
| 4497 |
|
| 4498 |
void SystemTools::Delay(unsigned int msec) |
| 4499 |
{ |
| 4500 |
#ifdef _WIN32 |
| 4501 |
Sleep(msec); |
| 4502 |
#else |
| 4503 |
// The sleep function gives 1 second resolution and the usleep |
| 4504 |
// function gives 1e-6 second resolution but on some platforms has a |
| 4505 |
// maximum sleep time of 1 second. This could be re-implemented to |
| 4506 |
// use select with masked signals or pselect to mask signals |
| 4507 |
// atomically. If select is given empty sets and zero as the max |
| 4508 |
// file descriptor but a non-zero timeout it can be used to block |
| 4509 |
// for a precise amount of time. |
| 4510 |
if (msec >= 1000) { |
| 4511 |
sleep(msec / 1000); |
| 4512 |
usleep((msec % 1000) * 1000); |
| 4513 |
} else { |
| 4514 |
usleep(msec * 1000); |
| 4515 |
} |
| 4516 |
#endif |
| 4517 |
} |
| 4518 |
|
| 4519 |
std::string SystemTools::GetOperatingSystemNameAndVersion() |
| 4520 |
{ |
| 4521 |
std::string res; |
| 4522 |
|
| 4523 |
#ifdef _WIN32 |
| 4524 |
char buffer[256]; |
| 4525 |
|
| 4526 |
OSVERSIONINFOEXA osvi; |
| 4527 |
BOOL bOsVersionInfoEx; |
| 4528 |
|
| 4529 |
ZeroMemory(&osvi, sizeof(osvi)); |
| 4530 |
osvi.dwOSVersionInfoSize = sizeof(osvi); |
| 4531 |
|
| 4532 |
# ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx |
| 4533 |
# pragma warning(push) |
| 4534 |
# ifdef __INTEL_COMPILER |
| 4535 |
# pragma warning(disable : 1478) |
| 4536 |
# elif defined __clang__ |
| 4537 |
# pragma clang diagnostic push |
| 4538 |
# pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 4539 |
# else |
| 4540 |
# pragma warning(disable : 4996) |
| 4541 |
# endif |
| 4542 |
# endif |
| 4543 |
bOsVersionInfoEx = GetVersionExA((OSVERSIONINFOA*)&osvi); |
| 4544 |
if (!bOsVersionInfoEx) { |
| 4545 |
return ""; |
| 4546 |
} |
| 4547 |
# ifdef KWSYS_WINDOWS_DEPRECATED_GetVersionEx |
| 4548 |
# ifdef __clang__ |
| 4549 |
# pragma clang diagnostic pop |
| 4550 |
# else |
| 4551 |
# pragma warning(pop) |
| 4552 |
# endif |
| 4553 |
# endif |
| 4554 |
|
| 4555 |
switch (osvi.dwPlatformId) { |
| 4556 |
// Test for the Windows NT product family. |
| 4557 |
|
| 4558 |
case VER_PLATFORM_WIN32_NT: |
| 4559 |
|
| 4560 |
// Test for the specific product family. |
| 4561 |
if (osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0) { |
| 4562 |
if (osvi.wProductType == VER_NT_WORKSTATION) { |
| 4563 |
res += "Microsoft Windows 10"; |
| 4564 |
} else { |
| 4565 |
res += "Microsoft Windows Server 2016 family"; |
| 4566 |
} |
| 4567 |
} |
| 4568 |
|
| 4569 |
if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 3) { |
| 4570 |
if (osvi.wProductType == VER_NT_WORKSTATION) { |
| 4571 |
res += "Microsoft Windows 8.1"; |
| 4572 |
} else { |
| 4573 |
res += "Microsoft Windows Server 2012 R2 family"; |
| 4574 |
} |
| 4575 |
} |
| 4576 |
|
| 4577 |
if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2) { |
| 4578 |
if (osvi.wProductType == VER_NT_WORKSTATION) { |
| 4579 |
res += "Microsoft Windows 8"; |
| 4580 |
} else { |
| 4581 |
res += "Microsoft Windows Server 2012 family"; |
| 4582 |
} |
| 4583 |
} |
| 4584 |
|
| 4585 |
if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1) { |
| 4586 |
if (osvi.wProductType == VER_NT_WORKSTATION) { |
| 4587 |
res += "Microsoft Windows 7"; |
| 4588 |
} else { |
| 4589 |
res += "Microsoft Windows Server 2008 R2 family"; |
| 4590 |
} |
| 4591 |
} |
| 4592 |
|
| 4593 |
if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0) { |
| 4594 |
if (osvi.wProductType == VER_NT_WORKSTATION) { |
| 4595 |
res += "Microsoft Windows Vista"; |
| 4596 |
} else { |
| 4597 |
res += "Microsoft Windows Server 2008 family"; |
| 4598 |
} |
| 4599 |
} |
| 4600 |
|
| 4601 |
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2) { |
| 4602 |
res += "Microsoft Windows Server 2003 family"; |
| 4603 |
} |
| 4604 |
|
| 4605 |
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) { |
| 4606 |
res += "Microsoft Windows XP"; |
| 4607 |
} |
| 4608 |
|
| 4609 |
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) { |
| 4610 |
res += "Microsoft Windows 2000"; |
| 4611 |
} |
| 4612 |
|
| 4613 |
if (osvi.dwMajorVersion <= 4) { |
| 4614 |
res += "Microsoft Windows NT"; |
| 4615 |
} |
| 4616 |
|
| 4617 |
// Test for specific product on Windows NT 4.0 SP6 and later. |
| 4618 |
|
| 4619 |
if (bOsVersionInfoEx) { |
| 4620 |
// Test for the workstation type. |
| 4621 |
|
| 4622 |
if (osvi.wProductType == VER_NT_WORKSTATION) { |
| 4623 |
if (osvi.dwMajorVersion == 4) { |
| 4624 |
res += " Workstation 4.0"; |
| 4625 |
} else if (osvi.dwMajorVersion == 5) { |
| 4626 |
if (osvi.wSuiteMask & VER_SUITE_PERSONAL) { |
| 4627 |
res += " Home Edition"; |
| 4628 |
} else { |
| 4629 |
res += " Professional"; |
| 4630 |
} |
| 4631 |
} |
| 4632 |
} |
| 4633 |
|
| 4634 |
// Test for the server type. |
| 4635 |
|
| 4636 |
else if (osvi.wProductType == VER_NT_SERVER) { |
| 4637 |
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2) { |
| 4638 |
if (osvi.wSuiteMask & VER_SUITE_DATACENTER) { |
| 4639 |
res += " Datacenter Edition"; |
| 4640 |
} else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { |
| 4641 |
res += " Enterprise Edition"; |
| 4642 |
} else if (osvi.wSuiteMask == VER_SUITE_BLADE) { |
| 4643 |
res += " Web Edition"; |
| 4644 |
} else { |
| 4645 |
res += " Standard Edition"; |
| 4646 |
} |
| 4647 |
} |
| 4648 |
|
| 4649 |
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) { |
| 4650 |
if (osvi.wSuiteMask & VER_SUITE_DATACENTER) { |
| 4651 |
res += " Datacenter Server"; |
| 4652 |
} else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { |
| 4653 |
res += " Advanced Server"; |
| 4654 |
} else { |
| 4655 |
res += " Server"; |
| 4656 |
} |
| 4657 |
} |
| 4658 |
|
| 4659 |
else if (osvi.dwMajorVersion <= 4) // Windows NT 4.0 |
| 4660 |
{ |
| 4661 |
if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { |
| 4662 |
res += " Server 4.0, Enterprise Edition"; |
| 4663 |
} else { |
| 4664 |
res += " Server 4.0"; |
| 4665 |
} |
| 4666 |
} |
| 4667 |
} |
| 4668 |
} |
| 4669 |
|
| 4670 |
// Test for specific product on Windows NT 4.0 SP5 and earlier |
| 4671 |
|
| 4672 |
else { |
| 4673 |
HKEY hKey; |
| 4674 |
# define BUFSIZE 80 |
| 4675 |
wchar_t szProductType[BUFSIZE]; |
| 4676 |
DWORD dwBufLen = BUFSIZE; |
| 4677 |
LONG lRet; |
| 4678 |
|
| 4679 |
lRet = |
| 4680 |
RegOpenKeyExW(HKEY_LOCAL_MACHINE, |
| 4681 |
L"SYSTEM\\CurrentControlSet\\Control\\ProductOptions", |
| 4682 |
0, KEY_QUERY_VALUE, &hKey); |
| 4683 |
if (lRet != ERROR_SUCCESS) { |
| 4684 |
return ""; |
| 4685 |
} |
| 4686 |
|
| 4687 |
lRet = RegQueryValueExW(hKey, L"ProductType", nullptr, nullptr, |
| 4688 |
(LPBYTE)szProductType, &dwBufLen); |
| 4689 |
|
| 4690 |
if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE)) { |
| 4691 |
return ""; |
| 4692 |
} |
| 4693 |
|
| 4694 |
RegCloseKey(hKey); |
| 4695 |
|
| 4696 |
if (lstrcmpiW(L"WINNT", szProductType) == 0) { |
| 4697 |
res += " Workstation"; |
| 4698 |
} |
| 4699 |
if (lstrcmpiW(L"LANMANNT", szProductType) == 0) { |
| 4700 |
res += " Server"; |
| 4701 |
} |
| 4702 |
if (lstrcmpiW(L"SERVERNT", szProductType) == 0) { |
| 4703 |
res += " Advanced Server"; |
| 4704 |
} |
| 4705 |
|
| 4706 |
res += " "; |
| 4707 |
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwMajorVersion); |
| 4708 |
res += buffer; |
| 4709 |
res += "."; |
| 4710 |
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwMinorVersion); |
| 4711 |
res += buffer; |
| 4712 |
} |
| 4713 |
|
| 4714 |
// Display service pack (if any) and build number. |
| 4715 |
|
| 4716 |
if (osvi.dwMajorVersion == 4 && |
| 4717 |
lstrcmpiA(osvi.szCSDVersion, "Service Pack 6") == 0) { |
| 4718 |
HKEY hKey; |
| 4719 |
LONG lRet; |
| 4720 |
|
| 4721 |
// Test for SP6 versus SP6a. |
| 4722 |
|
| 4723 |
lRet = RegOpenKeyExW( |
| 4724 |
HKEY_LOCAL_MACHINE, |
| 4725 |
L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009", |
| 4726 |
0, KEY_QUERY_VALUE, &hKey); |
| 4727 |
|
| 4728 |
if (lRet == ERROR_SUCCESS) { |
| 4729 |
res += " Service Pack 6a (Build "; |
| 4730 |
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwBuildNumber & 0xFFFF); |
| 4731 |
res += buffer; |
| 4732 |
res += ")"; |
| 4733 |
} else // Windows NT 4.0 prior to SP6a |
| 4734 |
{ |
| 4735 |
res += " "; |
| 4736 |
res += osvi.szCSDVersion; |
| 4737 |
res += " (Build "; |
| 4738 |
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwBuildNumber & 0xFFFF); |
| 4739 |
res += buffer; |
| 4740 |
res += ")"; |
| 4741 |
} |
| 4742 |
|
| 4743 |
RegCloseKey(hKey); |
| 4744 |
} else // Windows NT 3.51 and earlier or Windows 2000 and later |
| 4745 |
{ |
| 4746 |
res += " "; |
| 4747 |
res += osvi.szCSDVersion; |
| 4748 |
res += " (Build "; |
| 4749 |
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwBuildNumber & 0xFFFF); |
| 4750 |
res += buffer; |
| 4751 |
res += ")"; |
| 4752 |
} |
| 4753 |
|
| 4754 |
break; |
| 4755 |
|
| 4756 |
// Test for the Windows 95 product family. |
| 4757 |
|
| 4758 |
case VER_PLATFORM_WIN32_WINDOWS: |
| 4759 |
|
| 4760 |
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { |
| 4761 |
res += "Microsoft Windows 95"; |
| 4762 |
if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B') { |
| 4763 |
res += " OSR2"; |
| 4764 |
} |
| 4765 |
} |
| 4766 |
|
| 4767 |
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) { |
| 4768 |
res += "Microsoft Windows 98"; |
| 4769 |
if (osvi.szCSDVersion[1] == 'A') { |
| 4770 |
res += " SE"; |
| 4771 |
} |
| 4772 |
} |
| 4773 |
|
| 4774 |
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { |
| 4775 |
res += "Microsoft Windows Millennium Edition"; |
| 4776 |
} |
| 4777 |
break; |
| 4778 |
|
| 4779 |
case VER_PLATFORM_WIN32s: |
| 4780 |
|
| 4781 |
res += "Microsoft Win32s"; |
| 4782 |
break; |
| 4783 |
} |
| 4784 |
#endif |
| 4785 |
|
| 4786 |
return res; |
| 4787 |
} |
| 4788 |
|
| 4789 |
bool SystemTools::ParseURLProtocol(const std::string& URL, |
| 4790 |
std::string& protocol, |
| 4791 |
std::string& dataglom, bool decode) |
| 4792 |
{ |
| 4793 |
// match 0 entire url |
| 4794 |
// match 1 protocol |
| 4795 |
// match 2 dataglom following protocol:// |
| 4796 |
kwsys::RegularExpression urlRe(VTK_URL_PROTOCOL_REGEX); |
| 4797 |
|
| 4798 |
if (!urlRe.find(URL)) |
| 4799 |
return false; |
| 4800 |
|
| 4801 |
protocol = urlRe.match(1); |
| 4802 |
dataglom = urlRe.match(2); |
| 4803 |
|
| 4804 |
if (decode) { |
| 4805 |
dataglom = DecodeURL(dataglom); |
| 4806 |
} |
| 4807 |
|
| 4808 |
return true; |
| 4809 |
} |
| 4810 |
|
| 4811 |
bool SystemTools::ParseURL(const std::string& URL, std::string& protocol, |
| 4812 |
std::string& username, std::string& password, |
| 4813 |
std::string& hostname, std::string& dataport, |
| 4814 |
std::string& database, bool decode) |
| 4815 |
{ |
| 4816 |
kwsys::RegularExpression urlRe(VTK_URL_REGEX); |
| 4817 |
if (!urlRe.find(URL)) |
| 4818 |
return false; |
| 4819 |
|
| 4820 |
// match 0 URL |
| 4821 |
// match 1 protocol |
| 4822 |
// match 2 mangled user |
| 4823 |
// match 3 username |
| 4824 |
// match 4 mangled password |
| 4825 |
// match 5 password |
| 4826 |
// match 6 hostname |
| 4827 |
// match 7 mangled port |
| 4828 |
// match 8 dataport |
| 4829 |
// match 9 database name |
| 4830 |
|
| 4831 |
protocol = urlRe.match(1); |
| 4832 |
username = urlRe.match(3); |
| 4833 |
password = urlRe.match(5); |
| 4834 |
hostname = urlRe.match(6); |
| 4835 |
dataport = urlRe.match(8); |
| 4836 |
database = urlRe.match(9); |
| 4837 |
|
| 4838 |
if (decode) { |
| 4839 |
username = DecodeURL(username); |
| 4840 |
password = DecodeURL(password); |
| 4841 |
hostname = DecodeURL(hostname); |
| 4842 |
dataport = DecodeURL(dataport); |
| 4843 |
database = DecodeURL(database); |
| 4844 |
} |
| 4845 |
|
| 4846 |
return true; |
| 4847 |
} |
| 4848 |
|
| 4849 |
// ---------------------------------------------------------------------- |
| 4850 |
std::string SystemTools::DecodeURL(const std::string& url) |
| 4851 |
{ |
| 4852 |
kwsys::RegularExpression urlByteRe(VTK_URL_BYTE_REGEX); |
| 4853 |
std::string ret; |
| 4854 |
for (size_t i = 0; i < url.length(); i++) { |
| 4855 |
if (urlByteRe.find(url.substr(i, 3))) { |
| 4856 |
char bytes[] = { url[i + 1], url[i + 2], '\0' }; |
| 4857 |
ret += static_cast<char>(strtoul(bytes, nullptr, 16)); |
| 4858 |
i += 2; |
| 4859 |
} else { |
| 4860 |
ret += url[i]; |
| 4861 |
} |
| 4862 |
} |
| 4863 |
return ret; |
| 4864 |
} |
| 4865 |
|
| 4866 |
// ---------------------------------------------------------------------- |
| 4867 |
// Do NOT initialize. Default initialization to zero is necessary. |
| 4868 |
static unsigned int SystemToolsManagerCount; |
| 4869 |
|
| 4870 |
// SystemToolsManager manages the SystemTools singleton. |
| 4871 |
// SystemToolsManager should be included in any translation unit |
| 4872 |
// that will use SystemTools or that implements the singleton |
| 4873 |
// pattern. It makes sure that the SystemTools singleton is created |
| 4874 |
// before and destroyed after all other singletons in CMake. |
| 4875 |
|
| 4876 |
SystemToolsManager::SystemToolsManager() |
| 4877 |
{ |
| 4878 |
if (++SystemToolsManagerCount == 1) { |
| 4879 |
SystemTools::ClassInitialize(); |
| 4880 |
} |
| 4881 |
} |
| 4882 |
|
| 4883 |
SystemToolsManager::~SystemToolsManager() |
| 4884 |
{ |
| 4885 |
if (--SystemToolsManagerCount == 0) { |
| 4886 |
SystemTools::ClassFinalize(); |
| 4887 |
} |
| 4888 |
} |
| 4889 |
|
| 4890 |
#if defined(__VMS) |
| 4891 |
// On VMS we configure the run time C library to be more UNIX like. |
| 4892 |
// http://h71000.www7.hp.com/doc/732final/5763/5763pro_004.html |
| 4893 |
extern "C" int decc$feature_get_index(char* name); |
| 4894 |
extern "C" int decc$feature_set_value(int index, int mode, int value); |
| 4895 |
static int SetVMSFeature(char* name, int value) |
| 4896 |
{ |
| 4897 |
int i; |
| 4898 |
errno = 0; |
| 4899 |
i = decc$feature_get_index(name); |
| 4900 |
return i >= 0 && (decc$feature_set_value(i, 1, value) >= 0 || errno == 0); |
| 4901 |
} |
| 4902 |
#endif |
| 4903 |
|
| 4904 |
void SystemTools::ClassInitialize() |
| 4905 |
{ |
| 4906 |
#ifdef __VMS |
| 4907 |
SetVMSFeature("DECC$FILENAME_UNIX_ONLY", 1); |
| 4908 |
#endif |
| 4909 |
|
| 4910 |
// Create statics singleton instance |
| 4911 |
SystemToolsStatics = new SystemToolsStatic; |
| 4912 |
|
| 4913 |
#if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP |
| 4914 |
// Add some special translation paths for unix. These are not added |
| 4915 |
// for windows because drive letters need to be maintained. Also, |
| 4916 |
// there are not sym-links and mount points on windows anyway. |
| 4917 |
# if !defined(_WIN32) || defined(__CYGWIN__) |
| 4918 |
// The tmp path is frequently a logical path so always keep it: |
| 4919 |
SystemTools::AddKeepPath("/tmp/"); |
| 4920 |
|
| 4921 |
// If the current working directory is a logical path then keep the |
| 4922 |
// logical name. |
| 4923 |
std::string pwd_str; |
| 4924 |
if (SystemTools::GetEnv("PWD", pwd_str)) { |
| 4925 |
char buf[2048]; |
| 4926 |
if (const char* cwd = Getcwd(buf, 2048)) { |
| 4927 |
// The current working directory may be a logical path. Find |
| 4928 |
// the shortest logical path that still produces the correct |
| 4929 |
// physical path. |
| 4930 |
std::string cwd_changed; |
| 4931 |
std::string pwd_changed; |
| 4932 |
|
| 4933 |
// Test progressively shorter logical-to-physical mappings. |
| 4934 |
std::string cwd_str = cwd; |
| 4935 |
std::string pwd_path; |
| 4936 |
Realpath(pwd_str, pwd_path); |
| 4937 |
while (cwd_str == pwd_path && cwd_str != pwd_str) { |
| 4938 |
// The current pair of paths is a working logical mapping. |
| 4939 |
cwd_changed = cwd_str; |
| 4940 |
pwd_changed = pwd_str; |
| 4941 |
|
| 4942 |
// Strip off one directory level and see if the logical |
| 4943 |
// mapping still works. |
| 4944 |
pwd_str = SystemTools::GetFilenamePath(pwd_str); |
| 4945 |
cwd_str = SystemTools::GetFilenamePath(cwd_str); |
| 4946 |
Realpath(pwd_str, pwd_path); |
| 4947 |
} |
| 4948 |
|
| 4949 |
// Add the translation to keep the logical path name. |
| 4950 |
if (!cwd_changed.empty() && !pwd_changed.empty()) { |
| 4951 |
SystemTools::AddTranslationPath(cwd_changed, pwd_changed); |
| 4952 |
} |
| 4953 |
} |
| 4954 |
} |
| 4955 |
# endif |
| 4956 |
#endif |
| 4957 |
} |
| 4958 |
|
| 4959 |
void SystemTools::ClassFinalize() |
| 4960 |
{ |
| 4961 |
delete SystemToolsStatics; |
| 4962 |
} |
| 4963 |
|
| 4964 |
} // namespace KWSYS_NAMESPACE |
| 4965 |
|
| 4966 |
#if defined(_MSC_VER) && defined(_DEBUG) |
| 4967 |
# include <crtdbg.h> |
| 4968 |
# include <stdio.h> |
| 4969 |
# include <stdlib.h> |
| 4970 |
namespace KWSYS_NAMESPACE { |
| 4971 |
|
| 4972 |
static int SystemToolsDebugReport(int, char* message, int* ret) |
| 4973 |
{ |
| 4974 |
if (ret) { |
| 4975 |
// Pretend user clicked on Retry button in popup. |
| 4976 |
*ret = 1; |
| 4977 |
} |
| 4978 |
fprintf(stderr, "%s", message); |
| 4979 |
fflush(stderr); |
| 4980 |
return 1; // no further reporting required |
| 4981 |
} |
| 4982 |
|
| 4983 |
void SystemTools::EnableMSVCDebugHook() |
| 4984 |
{ |
| 4985 |
if (SystemTools::HasEnv("DART_TEST_FROM_DART") || |
| 4986 |
SystemTools::HasEnv("DASHBOARD_TEST_FROM_CTEST")) { |
| 4987 |
_CrtSetReportHook(SystemToolsDebugReport); |
| 4988 |
} |
| 4989 |
} |
| 4990 |
|
| 4991 |
} // namespace KWSYS_NAMESPACE |
| 4992 |
#else |
| 4993 |
namespace KWSYS_NAMESPACE { |
| 4994 |
void SystemTools::EnableMSVCDebugHook() |
| 4995 |
{ |
| 4996 |
} |
| 4997 |
} // namespace KWSYS_NAMESPACE |
| 4998 |
#endif |