Skip to content

Commit

Permalink
Using the new optimized toUncWcharArray() everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
VioletGiraffe committed Sep 11, 2024
1 parent ce04a40 commit a7673e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
7 changes: 4 additions & 3 deletions file-commander-core/src/cfilemanipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,16 @@ bool CFileManipulator::makeWritable(bool writable)
assert_and_return_message_r(_srcObject.isFile(), "This method only works for files", false);

#ifdef _WIN32
const QString UNCPath = toUncPath(_srcObject.fullAbsolutePath());
const DWORD attributes = GetFileAttributesW(reinterpret_cast<LPCWSTR>(UNCPath.utf16()));
WCHAR UNCPath[32768];
toUncWcharArray(_srcObject.fullAbsolutePath(), UNCPath);
const DWORD attributes = ::GetFileAttributesW(UNCPath);
if (attributes == INVALID_FILE_ATTRIBUTES)
{
_lastErrorMessage = QString::fromStdString(ErrorStringFromLastError());
return false;
}

if (SetFileAttributesW(reinterpret_cast<LPCWSTR>(UNCPath.utf16()), writable ? (attributes & (~(uint32_t)FILE_ATTRIBUTE_READONLY)) : (attributes | FILE_ATTRIBUTE_READONLY)) != TRUE)
if (::SetFileAttributesW(UNCPath, writable ? (attributes & (~(uint32_t)FILE_ATTRIBUTE_READONLY)) : (attributes | FILE_ATTRIBUTE_READONLY)) != TRUE)
{
_lastErrorMessage = QString::fromStdString(ErrorStringFromLastError());
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ bool CFileSystemWatcherWindows::setPathToWatch(const QString& path) noexcept
return true;

WCHAR wPath[32768];
const auto pathLen = toUncPath(path).remove(R"(\\?\)").toWCharArray(wPath);
const auto pathLen = toUncWcharArray(path, wPath);
if (pathLen <= 0)
return false;

assert_debug_only(!path.contains('\\'));
wPath[pathLen] = 0;
if (const auto trailingChar = wPath[pathLen - 1]; trailingChar == L'\\')
wPath[pathLen - 1] = 0; // FindFirstChangeNotificationW fails if the trailing slash is present

Expand All @@ -50,6 +49,7 @@ bool CFileSystemWatcherWindows::setPathToWatch(const QString& path) noexcept
_handle = ::FindFirstChangeNotificationW(wPath, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE);
if (_handle == INVALID_HANDLE_VALUE)
{
assert_debug_only(_handle);
_handle = nullptr;
return false;
}
Expand Down
8 changes: 5 additions & 3 deletions file-commander-core/src/shell/cshell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@ bool OsShell::runExecutable(const QString& command, const QString& arguments, co

bool OsShell::runExe(const QString& command, const QString& arguments, const QString& workingDir, bool asAdmin)
{
const QString commandPathUnc = toUncPath(command);
const QString workingDirNative = toNativeSeparators(workingDir);

SHELLEXECUTEINFOW shExecInfo;
::memset(&shExecInfo, 0, sizeof(shExecInfo));

WCHAR commandPathUnc[32768];
toUncWcharArray(command, commandPathUnc);

shExecInfo.cbSize = sizeof(SHELLEXECUTEINFOW);
shExecInfo.fMask = SEE_MASK_FLAG_NO_UI;
shExecInfo.hwnd = nullptr;
shExecInfo.lpVerb = asAdmin ? L"runas" : L"open";
shExecInfo.lpFile = reinterpret_cast<const WCHAR*>(commandPathUnc.utf16());
shExecInfo.lpFile = commandPathUnc;
shExecInfo.lpParameters = arguments.isEmpty() ? nullptr : reinterpret_cast<const WCHAR*>(arguments.utf16());
shExecInfo.lpDirectory = reinterpret_cast<const WCHAR*>(workingDirNative.utf16());
shExecInfo.nShow = SW_SHOWNORMAL;
Expand All @@ -126,7 +128,7 @@ bool OsShell::runExe(const QString& command, const QString& arguments, const QSt
if (GetLastError() != ERROR_CANCELLED) // Operation canceled by the user
{
const QString errorString = QString::fromStdString(ErrorStringFromLastError());
qInfo() << "ShellExecuteExW failed when trying to run" << commandPathUnc << "in" << workingDirNative;
qInfo() << "ShellExecuteExW failed when trying to run" << QString::fromWCharArray(commandPathUnc) << "in" << workingDirNative;
qInfo() << errorString;

return false;
Expand Down

0 comments on commit a7673e8

Please sign in to comment.