From 5d0dff54982b174d382aca8da00393273bc7b3e5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 8 Sep 2020 13:21:50 +0300 Subject: DeleteDirectoryTreeW - new function that deletes a folder with all subfolders (recursively) --- plugins/BasicHistory/src/EventList.cpp | 3 +-- plugins/BasicHistory/src/RichHtmlExport.cpp | 29 +----------------------- plugins/BasicHistory/src/Scheduler.cpp | 15 ++++++------- plugins/PackUpdater/Src/Events.cpp | 16 ++------------ plugins/PluginUpdater/pu_stub/src/pu_stub.cpp | 32 +++++++++++++++++++++++++-- plugins/PluginUpdater/pu_stub/src/stdafx.h | 1 + plugins/PluginUpdater/src/Events.cpp | 16 +------------- plugins/Toaster/src/main.cpp | 14 +----------- 8 files changed, 44 insertions(+), 82 deletions(-) (limited to 'plugins') diff --git a/plugins/BasicHistory/src/EventList.cpp b/plugins/BasicHistory/src/EventList.cpp index f6d03409dd..b686dbc998 100644 --- a/plugins/BasicHistory/src/EventList.cpp +++ b/plugins/BasicHistory/src/EventList.cpp @@ -26,7 +26,6 @@ extern int iconsNum; static mir_cs csEventList; -bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true); std::wstring GetName(const std::wstring &path); HistoryEventList::HistoryEventList() : @@ -608,7 +607,7 @@ void HistoryEventList::Init() GetTempPath(MAX_PATH, temp); m_contactFileDir = temp; m_contactFileDir += L"BasicHistoryImportDir\\"; - DeleteDirectory(m_contactFileDir.c_str()); + DeleteDirectoryTreeW(m_contactFileDir.c_str()); CreateDirectory(m_contactFileDir.c_str(), nullptr); } diff --git a/plugins/BasicHistory/src/RichHtmlExport.cpp b/plugins/BasicHistory/src/RichHtmlExport.cpp index bc316393d8..dc712cc1f2 100644 --- a/plugins/BasicHistory/src/RichHtmlExport.cpp +++ b/plugins/BasicHistory/src/RichHtmlExport.cpp @@ -253,39 +253,12 @@ void IcoSave(const std::wstring &fileName, HICON hicon) if (ii.hbmMask) DeleteObject(ii.hbmMask); } -bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true) -{ - size_t len = mir_wstrlen(lpszDir); - wchar_t *pszFrom = new wchar_t[len + 2]; - wcscpy_s(pszFrom, len + 2, lpszDir); - pszFrom[len] = 0; - pszFrom[len + 1] = 0; - - SHFILEOPSTRUCT fileop; - fileop.hwnd = nullptr; // no status display - fileop.wFunc = FO_DELETE; // delete operation - fileop.pFrom = pszFrom; // source file name as double null terminated string - fileop.pTo = nullptr; // no destination needed - fileop.fFlags = FOF_NOCONFIRMATION | FOF_SILENT; // do not prompt the user - - if (!noRecycleBin) - fileop.fFlags |= FOF_ALLOWUNDO; - - fileop.fAnyOperationsAborted = FALSE; - fileop.lpszProgressTitle = nullptr; - fileop.hNameMappings = nullptr; - - int ret = SHFileOperation(&fileop); - delete[] pszFrom; - return (ret == 0); -} - void RichHtmlExport::WriteHeader(const std::wstring &fileName, const std::wstring &filterName, const std::wstring &myName, const std::wstring &myId, const std::wstring &name1, const std::wstring &proto1, const std::wstring &id1, const std::string& baseProto1, const std::wstring& encoding) { baseProto = baseProto1; folder = RemoveExt(fileName) + L"_files"; folderName = GetName(folder); - DeleteDirectory(folder.c_str()); + DeleteDirectoryTreeW(folder.c_str()); CreateDirectory(folder.c_str(), nullptr); std::wstring css = folder + L"\\history.css"; BOOL cssCopied = FALSE; diff --git a/plugins/BasicHistory/src/Scheduler.cpp b/plugins/BasicHistory/src/Scheduler.cpp index a080d18e0e..51cce458ec 100644 --- a/plugins/BasicHistory/src/Scheduler.cpp +++ b/plugins/BasicHistory/src/Scheduler.cpp @@ -26,7 +26,6 @@ bool IsValidTask(TaskOptions& to, std::list* top = nullptr, std::ws std::wstring GetFileName(const std::wstring &baseName, std::wstring contactName, std::map& existingContacts, bool replaceContact); std::wstring GetDirectoryName(const std::wstring &path); std::wstring GetName(const std::wstring &path); -bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true); void ListDirectory(const std::wstring &basePath, const std::wstring &path, std::list& files); std::wstring ReplaceStr(const std::wstring& str, wchar_t oldCh, wchar_t newCh); time_t GetNextExportTime(TaskOptions& to); @@ -286,7 +285,7 @@ bool DoTask(TaskOptions& to) if (pos < dir.length()) dir = dir.substr(0, pos); - DeleteDirectory(dir.c_str()); + DeleteDirectoryTreeW(dir.c_str()); CreateDirectory(dir.c_str(), nullptr); } @@ -416,7 +415,7 @@ bool DoTask(TaskOptions& to) } if (to.useFtp || to.compress) - DeleteDirectory(dir.c_str()); + DeleteDirectoryTreeW(dir.c_str()); } else { std::map existingContacts; @@ -440,7 +439,7 @@ bool DoTask(TaskOptions& to) if (pos < dir.length()) dir = dir.substr(0, pos); - DeleteDirectory(dir.c_str()); + DeleteDirectoryTreeW(dir.c_str()); CreateDirectory(dir.c_str(), nullptr); filePath = dir + L"\\" + filePath; } @@ -490,7 +489,7 @@ bool DoTask(TaskOptions& to) if (error) { if (to.compress && !to.useFtp) - DeleteDirectory(dir.c_str()); + DeleteDirectoryTreeW(dir.c_str()); } else if (to.compress) { std::wstring zipFilePath = to.filePath; @@ -508,7 +507,7 @@ bool DoTask(TaskOptions& to) zipDir = temp; zipDir += L"zip"; zipDir = GetFileName(zipDir, L"", existingContacts, true); - DeleteDirectory(zipDir.c_str()); + DeleteDirectoryTreeW(zipDir.c_str()); CreateDirectory(zipDir.c_str(), nullptr); zipFilePath = zipDir + L"\\" + zipFilePath; } @@ -533,7 +532,7 @@ bool DoTask(TaskOptions& to) } } - DeleteDirectory(dir.c_str()); + DeleteDirectoryTreeW(dir.c_str()); } } @@ -966,7 +965,7 @@ bool ZipFiles(const std::wstring &dir, std::wstring zipFilePath, const std::stri else error = true; } - DeleteDirectory(dir.c_str()); + DeleteDirectoryTreeW(dir.c_str()); return error; } diff --git a/plugins/PackUpdater/Src/Events.cpp b/plugins/PackUpdater/Src/Events.cpp index 3745f37732..0a59c5b14a 100644 --- a/plugins/PackUpdater/Src/Events.cpp +++ b/plugins/PackUpdater/Src/Events.cpp @@ -51,21 +51,9 @@ INT_PTR MenuCommand(WPARAM, LPARAM) return 0; } -INT_PTR EmptyFolder(WPARAM, LPARAM lParam) +INT_PTR EmptyFolder(WPARAM, LPARAM) { - SHFILEOPSTRUCT file_op = { - nullptr, - FO_DELETE, - tszRoot, - L"", - FOF_NOERRORUI | - FOF_SILENT, - false, - nullptr, - L"" }; - if (lParam) - file_op.fFlags |= FOF_NOCONFIRMATION; - SHFileOperation(&file_op); + DeleteDirectoryTreeW(tszRoot); return 0; } diff --git a/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp b/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp index 60c67f7c1a..c3cd302482 100644 --- a/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp +++ b/plugins/PluginUpdater/pu_stub/src/pu_stub.cpp @@ -21,8 +21,9 @@ void log(const wchar_t *tszFormat, ...) int CreateDirectoryTreeW(const wchar_t* szDir) { - wchar_t szTestDir[MAX_PATH]; - lstrcpynW(szTestDir, szDir, MAX_PATH); + wchar_t szTestDir[MAX_PATH+1]; + if (lstrcpynW(szTestDir, szDir, MAX_PATH) == nullptr) + szTestDir[MAX_PATH] = 0; DWORD dwAttributes = GetFileAttributesW(szTestDir); if (dwAttributes != INVALID_FILE_ATTRIBUTES && (dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) @@ -38,6 +39,28 @@ int CreateDirectoryTreeW(const wchar_t* szDir) return (CreateDirectoryW(szTestDir, nullptr) == 0) ? GetLastError() : 0; } +int DeleteDirectoryTreeW(const wchar_t *pwszDirName) +{ + // file name shall be double sero ended + wchar_t wszPath[MAX_PATH + 2]; + if (lstrcpynW(wszPath, pwszDirName, MAX_PATH) == nullptr) + wszPath[MAX_PATH] = 0; + wszPath[lstrlenW(wszPath) + 1] = 0; + + SHFILEOPSTRUCTW file_op = { + NULL, + FO_DELETE, + wszPath, + L"", + FOF_NOCONFIRMATION | + FOF_NOERRORUI | + FOF_SILENT, + false, + 0, + L"" }; + return SHFileOperationW(&file_op); +} + void CreatePathToFileW(wchar_t *wszFilePath) { wchar_t* pszLastBackslash = wcsrchr(wszFilePath, '\\'); @@ -132,6 +155,11 @@ int APIENTRY wWinMain(HINSTANCE /*hInstance*/, HINSTANCE, LPTSTR lpCmdLine, int) dwError = 0; break; + case 6: // delete folder recursively + DeleteDirectoryTreeW(ptszFile1); + dwError = 0; + break; + default: dwError = ERROR_UNKNOWN_FEATURE; } diff --git a/plugins/PluginUpdater/pu_stub/src/stdafx.h b/plugins/PluginUpdater/pu_stub/src/stdafx.h index b6f494b071..7ad5b19462 100644 --- a/plugins/PluginUpdater/pu_stub/src/stdafx.h +++ b/plugins/PluginUpdater/pu_stub/src/stdafx.h @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define WIN32_LEAN_AND_MEAN #include +#include // C RunTime Header Files #include diff --git a/plugins/PluginUpdater/src/Events.cpp b/plugins/PluginUpdater/src/Events.cpp index 4c808600dc..55a28341bd 100644 --- a/plugins/PluginUpdater/src/Events.cpp +++ b/plugins/PluginUpdater/src/Events.cpp @@ -30,20 +30,6 @@ int OnFoldersChanged(WPARAM, LPARAM) return 0; } -void EmptyFolder() -{ - SHFILEOPSTRUCT file_op = { - nullptr, - FO_DELETE, - g_wszRoot, - L"", - FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION, - false, - nullptr, - L"" }; - SHFileOperation(&file_op); -} - int ModulesLoaded(WPARAM, LPARAM) { if (hPluginUpdaterFolder = FoldersRegisterCustomPathW(MODULEA, LPGEN("Plugin Updater"), MIRANDA_PATHW L"\\" DEFAULT_UPDATES_FOLDER)) { @@ -59,7 +45,7 @@ int ModulesLoaded(WPARAM, LPARAM) if (iRestartCount > 0) g_plugin.setByte(DB_SETTING_RESTART_COUNT, iRestartCount - 1); else - EmptyFolder(); // silently + DeleteDirectoryTreeW(g_wszRoot); CheckUpdateOnStartup(); diff --git a/plugins/Toaster/src/main.cpp b/plugins/Toaster/src/main.cpp index 470399a83d..10dea8e278 100644 --- a/plugins/Toaster/src/main.cpp +++ b/plugins/Toaster/src/main.cpp @@ -31,19 +31,7 @@ extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = { MIID_POPUP, static int OnPreShutdown(WPARAM, LPARAM) { CleanupClasses(); - - SHFILEOPSTRUCT file_op = { - nullptr, - FO_DELETE, - wszTempDir, - L"", - FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION, - false, - nullptr, - L"" - }; - SHFileOperation(&file_op); - + DeleteDirectoryTreeW(wszTempDir); return 0; } -- cgit v1.2.3