Skip to content

Commit de7aa5a

Browse files
xomxdonho
authored andcommitted
Add NPP_UAC_CREATEEMPTYFILE
+ minor fixes for FileManager::deleteFile Follow notepad-plus-plus@4b0fc8d Fix notepad-plus-plus#16933 (comment)
1 parent b3884c1 commit de7aa5a

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

PowerEditor/src/MISC/Common/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,5 @@ class ControlInfoTip final
336336
#define NPP_UAC_SAVE_SIGN L"#UAC-SAVE#"
337337
#define NPP_UAC_SETFILEATTRIBUTES_SIGN L"#UAC-SETFILEATTRIBUTES#"
338338
#define NPP_UAC_MOVEFILE_SIGN L"#UAC-MOVEFILE#"
339+
#define NPP_UAC_CREATEEMPTYFILE_SIGN L"#UAC-CREATEEMPTYFILE#"
339340
DWORD invokeNppUacOp(std::wstring& strCmdLineParams);

PowerEditor/src/ScintillaComponent/Buffer.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,21 +1079,23 @@ bool FileManager::reloadBufferDeferred(BufferID id)
10791079

10801080
bool FileManager::deleteFile(BufferID id)
10811081
{
1082+
if (id == BUFFER_INVALID)
1083+
return false;
1084+
10821085
const Buffer* buf = getBufferByID(id);
10831086
wstring fileNamePath = buf->getFullPathName();
1087+
if (!doesFileExist(fileNamePath.c_str()))
1088+
return false;
10841089

10851090
// Make sure to form a string with double '\0' terminator.
10861091
fileNamePath.append(1, '\0');
10871092

1088-
if (!doesFileExist(fileNamePath.c_str()))
1089-
return false;
1090-
10911093
SHFILEOPSTRUCT fileOpStruct = {};
10921094
fileOpStruct.hwnd = NULL;
10931095
fileOpStruct.pFrom = fileNamePath.c_str();
10941096
fileOpStruct.pTo = NULL;
10951097
fileOpStruct.wFunc = FO_DELETE;
1096-
fileOpStruct.fFlags = FOF_ALLOWUNDO;
1098+
fileOpStruct.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION; // FOF_NOCONFIRMATION - prevent possible redundant shell-dlg (Notepad++ uses its own delete-confirmation dlg)
10971099
fileOpStruct.fAnyOperationsAborted = false;
10981100
fileOpStruct.hNameMappings = NULL;
10991101
fileOpStruct.lpszProgressTitle = NULL;
@@ -2079,10 +2081,26 @@ BufferID FileManager::getBufferFromDocument(Document doc)
20792081
}
20802082

20812083

2082-
bool FileManager::createEmptyFile(const wchar_t * path)
2084+
bool FileManager::createEmptyFile(const wchar_t* path)
20832085
{
20842086
Win32_IO_File file(path);
2085-
return file.isOpened();
2087+
if (!file.isOpened())
2088+
{
2089+
if (file.getLastErrorCode() != ERROR_ACCESS_DENIED)
2090+
return false;
2091+
2092+
// ERROR_ACCESS_DENIED, try the same but elevated
2093+
// (notepad++.exe #UAC-CREATEEMPTYFILE# new_empty_file_path)
2094+
wstring strCmdLineParams = NPP_UAC_CREATEEMPTYFILE_SIGN;
2095+
strCmdLineParams += L" \"";
2096+
strCmdLineParams += path;
2097+
strCmdLineParams += L"\"";
2098+
DWORD dwNppUacOpError = invokeNppUacOp(strCmdLineParams);
2099+
if (dwNppUacOpError != NO_ERROR)
2100+
return false;
2101+
}
2102+
2103+
return true;
20862104
}
20872105

20882106

PowerEditor/src/winmain.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,20 @@ DWORD nppUacMoveFile(const wchar_t* wszOriginalFilePath, const wchar_t* wszNewFi
489489
return ERROR_SUCCESS;
490490
}
491491

492+
DWORD nppUacCreateEmptyFile(const wchar_t* wszNewEmptyFilePath)
493+
{
494+
if (lstrlenW(wszNewEmptyFilePath) == 0) // safe check (lstrlen returns 0 for possible nullptr)
495+
return ERROR_INVALID_PARAMETER;
496+
if (doesFileExist(wszNewEmptyFilePath))
497+
return ERROR_FILE_EXISTS;
498+
499+
Win32_IO_File file(wszNewEmptyFilePath);
500+
if (!file.isOpened())
501+
return file.getLastErrorCode();
502+
503+
return ERROR_SUCCESS;
504+
}
505+
492506
} // namespace
493507

494508

@@ -529,6 +543,12 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE /*hPrevInstance
529543
// __wargv[x]: 2 ... originalFilePath, 3 ... newFilePath
530544
return static_cast<int>(nppUacMoveFile(__wargv[2], __wargv[3]));
531545
}
546+
547+
if ((__argc == 3) && (wcscmp(wszNppUacOpSign, NPP_UAC_CREATEEMPTYFILE_SIGN) == 0))
548+
{
549+
// __wargv[x]: 2 ... newEmptyFilePath
550+
return static_cast<int>(nppUacCreateEmptyFile(__wargv[2]));
551+
}
532552
}
533553
} // Notepad++ UAC OPS////////////////////////////////////////////////////////////////////////////////////////////
534554

0 commit comments

Comments
 (0)