diff options
Diffstat (limited to 'Source/kwsys/SystemTools.cxx')
-rw-r--r-- | Source/kwsys/SystemTools.cxx | 209 |
1 files changed, 54 insertions, 155 deletions
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index ae7a18a..ce4d6ef 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -192,15 +192,15 @@ static inline char* realpath(const char* path, char* resolved_path) { const size_t maxlen = KWSYS_SYSTEMTOOLS_MAXPATH; snprintf(resolved_path, maxlen, "%s", path); - BPath normalized(resolved_path, NULL, true); + BPath normalized(resolved_path, nullptr, true); const char* resolved = normalized.Path(); - if (resolved != NULL) // NULL == No such file. + if (resolved != nullptr) // nullptr == No such file. { if (snprintf(resolved_path, maxlen, "%s", resolved) < maxlen) { return resolved_path; } } - return NULL; // something went wrong. + return nullptr; // something went wrong. } #endif @@ -273,12 +273,12 @@ inline void Realpath(const std::string& path, std::string& resolved_path, if (bufferLen) { *errorMessage = "Destination path buffer size too small."; } else if (unsigned int errorId = GetLastError()) { - LPSTR message = NULL; + LPSTR message = nullptr; DWORD size = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&message, 0, NULL); + nullptr, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&message, 0, nullptr); *errorMessage = std::string(message, size); LocalFree(message); } else { @@ -313,7 +313,7 @@ inline int Chdir(const std::string& dir) return chdir(dir.c_str()); } inline void Realpath(const std::string& path, std::string& resolved_path, - std::string* errorMessage = KWSYS_NULLPTR) + std::string* errorMessage = nullptr) { char resolved_name[KWSYS_SYSTEMTOOLS_MAXPATH]; @@ -359,7 +359,7 @@ double SystemTools::GetTime(void) 11644473600.0); #else struct timeval t; - gettimeofday(&t, KWSYS_NULLPTR); + gettimeofday(&t, nullptr); return 1.0 * double(t.tv_sec) + 0.000001 * double(t.tv_usec); #endif } @@ -389,8 +389,8 @@ struct kwsysEnvCompare #else const char* leq = strchr(l, '='); const char* req = strchr(r, '='); - size_t llen = leq ? (leq - l) : strlen(l); - size_t rlen = req ? (req - r) : strlen(r); + size_t llen = leq ? static_cast<size_t>(leq - l) : strlen(l); + size_t rlen = req ? static_cast<size_t>(req - r) : strlen(r); if (llen == rlen) { return strncmp(l, r, llen) < 0; } else { @@ -420,7 +420,7 @@ public: const envchar* Release(const envchar* env) { - const envchar* old = KWSYS_NULLPTR; + const envchar* old = nullptr; iterator i = this->find(env); if (i != this->end()) { old = *i; @@ -630,7 +630,7 @@ const char* SystemToolsStatic::GetEnvBuffered(const char* key) } return menv.c_str(); } - return KWSYS_NULLPTR; + return nullptr; } #endif @@ -684,7 +684,7 @@ bool SystemTools::HasEnv(const char* key) #else const char* v = getenv(key); #endif - return v != KWSYS_NULLPTR; + return v != nullptr; } bool SystemTools::HasEnv(const std::string& key) @@ -915,7 +915,7 @@ bool SystemTools::MakeDirectory(const std::string& path, const mode_t* mode) while ((pos = dir.find('/', pos)) != std::string::npos) { topdir = dir.substr(0, pos); - if (Mkdir(topdir) == 0 && mode != KWSYS_NULLPTR) { + if (Mkdir(topdir) == 0 && mode != nullptr) { SystemTools::SetPermissions(topdir, *mode); } @@ -934,7 +934,7 @@ bool SystemTools::MakeDirectory(const std::string& path, const mode_t* mode) ) { return false; } - } else if (mode != KWSYS_NULLPTR) { + } else if (mode != nullptr) { SystemTools::SetPermissions(topdir, *mode); } @@ -1055,7 +1055,7 @@ static DWORD SystemToolsMakeRegistryMode(DWORD mode, // only add the modes when on a system that supports Wow64. static FARPROC wow64p = GetProcAddress(GetModuleHandleW(L"kernel32"), "IsWow64Process"); - if (wow64p == NULL) { + if (wow64p == nullptr) { return mode; } @@ -1136,7 +1136,7 @@ bool SystemTools::ReadRegistryValue(const std::string& key, std::string& value, DWORD dwType, dwSize; dwSize = 1023; wchar_t data[1024]; - if (RegQueryValueExW(hKey, Encoding::ToWide(valuename).c_str(), NULL, + if (RegQueryValueExW(hKey, Encoding::ToWide(valuename).c_str(), nullptr, &dwType, (BYTE*)data, &dwSize) == ERROR_SUCCESS) { if (dwType == REG_SZ) { value = Encoding::ToNarrow(data); @@ -1186,7 +1186,7 @@ bool SystemTools::WriteRegistryValue(const std::string& key, wchar_t lpClass[] = L""; if (RegCreateKeyExW(primaryKey, Encoding::ToWide(second).c_str(), 0, lpClass, REG_OPTION_NON_VOLATILE, - SystemToolsMakeRegistryMode(KEY_WRITE, view), NULL, + SystemToolsMakeRegistryMode(KEY_WRITE, view), nullptr, &hKey, &dwDummy) != ERROR_SUCCESS) { return false; } @@ -1252,10 +1252,10 @@ bool SystemTools::SameFile(const std::string& file1, const std::string& file2) hFile1 = CreateFileW(Encoding::ToWide(file1).c_str(), GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); hFile2 = CreateFileW(Encoding::ToWide(file2).c_str(), GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); if (hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE) { if (hFile1 != INVALID_HANDLE_VALUE) { CloseHandle(hFile1); @@ -1347,7 +1347,7 @@ bool SystemTools::FileExists(const std::string& filename) // even if we do not have permission to read the file itself HANDLE handle = CreateFileW(Encoding::ToWindowsExtendedPath(filename).c_str(), 0, 0, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); if (handle == INVALID_HANDLE_VALUE) { return false; @@ -1493,12 +1493,12 @@ bool SystemTools::Touch(const std::string& filename, bool create) CloseHandle(h); #elif KWSYS_CXX_HAS_UTIMENSAT // utimensat is only available on newer Unixes and macOS 10.13+ - if (utimensat(AT_FDCWD, filename.c_str(), NULL, 0) < 0) { + if (utimensat(AT_FDCWD, filename.c_str(), nullptr, 0) < 0) { return false; } #else // fall back to utimes - if (utimes(filename.c_str(), NULL) < 0) { + if (utimes(filename.c_str(), nullptr) < 0) { return false; } #endif @@ -1653,7 +1653,7 @@ char* SystemTools::AppendStrings(const char* str1, const char* str2) size_t len1 = strlen(str1); char* newstr = new char[len1 + strlen(str2) + 1]; if (!newstr) { - return KWSYS_NULLPTR; + return nullptr; } strcpy(newstr, str1); strcat(newstr + len1, str2); @@ -1676,7 +1676,7 @@ char* SystemTools::AppendStrings(const char* str1, const char* str2, size_t len1 = strlen(str1), len2 = strlen(str2); char* newstr = new char[len1 + len2 + strlen(str3) + 1]; if (!newstr) { - return KWSYS_NULLPTR; + return nullptr; } strcpy(newstr, str1); strcat(newstr + len1, str2); @@ -1726,7 +1726,7 @@ size_t SystemTools::CountChar(const char* str, char c) char* SystemTools::RemoveChars(const char* str, const char* toremove) { if (!str) { - return KWSYS_NULLPTR; + return nullptr; } char* clean_str = new char[strlen(str) + 1]; char* ptr = clean_str; @@ -1748,7 +1748,7 @@ char* SystemTools::RemoveChars(const char* str, const char* toremove) char* SystemTools::RemoveCharsButUpperHex(const char* str) { if (!str) { - return KWSYS_NULLPTR; + return nullptr; } char* clean_str = new char[strlen(str) + 1]; char* ptr = clean_str; @@ -1829,7 +1829,7 @@ bool SystemTools::StringEndsWith(const std::string& str1, const char* str2) const char* SystemTools::FindLastString(const char* str1, const char* str2) { if (!str1 || !str2) { - return KWSYS_NULLPTR; + return nullptr; } size_t len1 = strlen(str1), len2 = strlen(str2); @@ -1842,7 +1842,7 @@ const char* SystemTools::FindLastString(const char* str1, const char* str2) } while (ptr-- != str1); } - return KWSYS_NULLPTR; + return nullptr; } // Duplicate string @@ -1852,7 +1852,7 @@ char* SystemTools::DuplicateString(const char* str) char* newstr = new char[strlen(str) + 1]; return strcpy(newstr, str); } - return KWSYS_NULLPTR; + return nullptr; } // Return a cropped string @@ -2169,24 +2169,24 @@ std::string SystemTools::ConvertToWindowsOutputPath(const std::string& path) return ret; } +/** + * Append the filename from the path source to the directory name dir. + */ +static std::string FileInDir(const std::string& source, const std::string& dir) +{ + std::string new_destination = dir; + SystemTools::ConvertToUnixSlashes(new_destination); + return new_destination + '/' + SystemTools::GetFilenameName(source); +} + bool SystemTools::CopyFileIfDifferent(const std::string& source, const std::string& destination) { // special check for a destination that is a directory // FilesDiffer does not handle file to directory compare if (SystemTools::FileIsDirectory(destination)) { - std::string new_destination = destination; - SystemTools::ConvertToUnixSlashes(new_destination); - new_destination += '/'; - std::string source_name = source; - new_destination += SystemTools::GetFilenameName(source_name); - if (SystemTools::FilesDiffer(source, new_destination)) { - return SystemTools::CopyFileAlways(source, destination); - } else { - // the files are the same so the copy is done return - // true - return true; - } + const std::string new_destination = FileInDir(source, destination); + return SystemTools::CopyFileIfDifferent(source, new_destination); } // source and destination are files so do a copy if they // are different @@ -2612,101 +2612,6 @@ std::string SystemTools::GetLastSystemError() return strerror(e); } -#ifdef _WIN32 - -static bool IsJunction(const std::wstring& source) -{ -# ifdef FSCTL_GET_REPARSE_POINT - const DWORD JUNCTION_ATTRS = - FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT; - DWORD attrs = GetFileAttributesW(source.c_str()); - if (attrs == INVALID_FILE_ATTRIBUTES) { - return false; - } - if ((attrs & JUNCTION_ATTRS) != JUNCTION_ATTRS) { - return false; - } - - // Adjust privileges so that we can succefully open junction points. - HANDLE token; - TOKEN_PRIVILEGES privs; - OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token); - LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &privs.Privileges[0].Luid); - privs.PrivilegeCount = 1; - privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; - AdjustTokenPrivileges(token, FALSE, &privs, sizeof(TOKEN_PRIVILEGES), NULL, - NULL); - CloseHandle(token); - - HANDLE dir = CreateFileW( - source.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (dir == INVALID_HANDLE_VALUE) { - return false; - } - - // Query whether this is a reparse point or not. - BYTE buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; - REPARSE_GUID_DATA_BUFFER* reparse_buffer = (REPARSE_GUID_DATA_BUFFER*)buffer; - DWORD sentinel; - - BOOL success = - DeviceIoControl(dir, FSCTL_GET_REPARSE_POINT, NULL, 0, reparse_buffer, - MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &sentinel, NULL); - - CloseHandle(dir); - - return (success && - (reparse_buffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)); -# else - return false; -# endif -} - -static bool DeleteJunction(const std::wstring& source) -{ -# ifdef FSCTL_DELETE_REPARSE_POINT - // Adjust privileges so that we can succefully open junction points as - // read/write. - HANDLE token; - TOKEN_PRIVILEGES privs; - OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token); - LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &privs.Privileges[0].Luid); - privs.PrivilegeCount = 1; - privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; - AdjustTokenPrivileges(token, FALSE, &privs, sizeof(TOKEN_PRIVILEGES), NULL, - NULL); - CloseHandle(token); - - HANDLE dir = CreateFileW( - source.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (dir == INVALID_HANDLE_VALUE) { - return false; - } - - // Set up the structure so that we can delete the junction. - std::vector<BYTE> buffer(REPARSE_GUID_DATA_BUFFER_HEADER_SIZE, 0); - REPARSE_GUID_DATA_BUFFER* reparse_buffer = - (REPARSE_GUID_DATA_BUFFER*)&buffer[0]; - DWORD sentinel; - - reparse_buffer->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; - - BOOL success = DeviceIoControl( - dir, FSCTL_DELETE_REPARSE_POINT, reparse_buffer, - REPARSE_GUID_DATA_BUFFER_HEADER_SIZE, NULL, 0, &sentinel, NULL); - - CloseHandle(dir); - - return !!success; -# else - return false; -# endif -} - -#endif - bool SystemTools::RemoveFile(const std::string& source) { #ifdef _WIN32 @@ -2728,9 +2633,7 @@ bool SystemTools::RemoveFile(const std::string& source) SetLastError(err); return false; } - if (IsJunction(ws) && DeleteJunction(ws)) { - return true; - } + const DWORD DIRECTORY_SOFT_LINK_ATTRS = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT; DWORD attrs = GetFileAttributesW(ws.c_str()); @@ -3115,16 +3018,16 @@ bool SystemTools::FileIsSymlink(const std::string& name) // * a file or directory that has an associated reparse point, or // * a file that is a symbolic link. HANDLE hFile = CreateFileW( - path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); + path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr); if (hFile == INVALID_HANDLE_VALUE) { return false; } byte buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; DWORD bytesReturned = 0; - if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, + if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, nullptr, 0, buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bytesReturned, - NULL)) { + nullptr)) { CloseHandle(hFile); // Since FILE_ATTRIBUTE_REPARSE_POINT is set this file must be // a symbolic link if it is not a reparse point. @@ -3155,7 +3058,7 @@ bool SystemTools::FileIsFIFO(const std::string& name) #if defined(_WIN32) HANDLE hFile = CreateFileW(Encoding::ToWide(name).c_str(), GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); if (hFile == INVALID_HANDLE_VALUE) { return false; } @@ -3316,7 +3219,7 @@ bool SystemTools::FindProgramPath(const char* argv0, std::string& pathOut, std::string SystemTools::CollapseFullPath(const std::string& in_relative) { - return SystemTools::CollapseFullPath(in_relative, KWSYS_NULLPTR); + return SystemTools::CollapseFullPath(in_relative, nullptr); } #if KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP @@ -3403,11 +3306,7 @@ static void SystemToolsAppendComponents( out_components.emplace_back(std::move(*i)); } } else if (!i->empty() && *i != cur) { -#if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L) - out_components.push_back(std::move(*i)); -#else - out_components.push_back(*i); -#endif + out_components.emplace_back(std::move(*i)); } } } @@ -4114,7 +4013,7 @@ bool SystemTools::GetShortPath(const std::string& path, std::string& shortPath) } std::wstring wtempPath = Encoding::ToWide(tempPath); - DWORD ret = GetShortPathNameW(wtempPath.c_str(), NULL, 0); + DWORD ret = GetShortPathNameW(wtempPath.c_str(), nullptr, 0); std::vector<wchar_t> buffer(ret); if (ret != 0) { ret = GetShortPathNameW(wtempPath.c_str(), &buffer[0], @@ -4522,7 +4421,7 @@ std::string SystemTools::GetOperatingSystemNameAndVersion() return 0; } - lRet = RegQueryValueExW(hKey, L"ProductType", NULL, NULL, + lRet = RegQueryValueExW(hKey, L"ProductType", nullptr, nullptr, (LPBYTE)szProductType, &dwBufLen); if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE)) { @@ -4743,7 +4642,7 @@ void SystemTools::ClassInitialize() // Test progressively shorter logical-to-physical mappings. std::string cwd_str = cwd; std::string pwd_path; - Realpath(pwd_str.c_str(), pwd_path); + Realpath(pwd_str, pwd_path); while (cwd_str == pwd_path && cwd_str != pwd_str) { // The current pair of paths is a working logical mapping. cwd_changed = cwd_str; @@ -4753,7 +4652,7 @@ void SystemTools::ClassInitialize() // mapping still works. pwd_str = SystemTools::GetFilenamePath(pwd_str); cwd_str = SystemTools::GetFilenamePath(cwd_str); - Realpath(pwd_str.c_str(), pwd_path); + Realpath(pwd_str, pwd_path); } // Add the translation to keep the logical path name. |