nfsstat3 CNFS3Prog::ProcedureRMDIR(void) { char *path; wcc_data dir_wcc; nfsstat3 stat; PrintLog("RMDIR"); std::string dirName; std::string fileName; ReadDirectory(dirName, fileName); path = GetFullPath(dirName, fileName); stat = CheckFile((char*)dirName.c_str(), path); dir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.before.attributes); if (stat == NFS3_OK) { if (!RemoveFolder(path)) { stat = NFS3ERR_IO; } } dir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.after.attributes); Write(&stat); Write(&dir_wcc); return stat; }
void RemoveFolder(const TCHAR *src_folder) { TCHAR szFilesPath[MAX_PATH]; mir_sntprintf(szFilesPath, SIZEOF(szFilesPath), _T("%s\\*.*"), src_folder); TCHAR *p = _tcsrchr(szFilesPath, '\\') + 1; WIN32_FIND_DATA findData; HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData); if (hFileSearch != INVALID_HANDLE_VALUE) { do { if(findData.cFileName[0] != _T('.')) { _tcscpy(p, findData.cFileName); if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // recurse RemoveFolder(szFilesPath); else DeleteFile(szFilesPath); } } while(FindNextFile(hFileSearch, &findData)); FindClose(hFileSearch); } RemoveDirectory(src_folder); }
HRESULT CFileHelper::RemoveFolder(LPCTSTR path, BOOL removeChildren) { vector<WIN32_FIND_DATA> fileDatas; if(_ListChildren(path, fileDatas)) { for(int i=0,size = fileDatas.size() ; i<size;i++) { LPCTSTR path = fileDatas[i].cFileName; if(BIT_IS_TRUE( fileDatas[i].dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY) && removeChildren) { HRESULT hr = RemoveFolder(path, removeChildren); if(FAILED(hr)) return hr; } else { HRESULT hr = RemoveFile(path); if(FAILED(hr)) return hr; } } return S_OK; } return E_FAIL; }
TempFolderHelper::~TempFolderHelper(void) { try { if (m_sFolder.GetLength() > 0 && CGeneral::DirectoryExists(m_sFolder)) RemoveFolder(m_sFolder); } catch(...) { // Don't want to throw out of a destructor, do you? } }
TempFolderHelper::TempFolderHelper(void) : m_sFolder(_T("")) { CStdString sFolder; try { CreateTempFolder(sFolder); } catch(...) { if (sFolder.GetLength() > 0 && CGeneral::DirectoryExists(sFolder)) RemoveFolder(sFolder); throw; } m_sFolder = sFolder; }
BOOL CFolderRemoving::RemoveFolder(const TCHAR strFolderPath[]) { if (IsDirectory(strFolderPath) == FALSE) { return RemoveFile(strFolderPath); } size_t uiLength; uiLength = lstrlen(strFolderPath) + lstrlen(_T("\\*")) + 1; TCHAR *ptcFirstFilePath = new TCHAR[uiLength]; StringCchPrintf(ptcFirstFilePath, uiLength, _T("%s\\*"), strFolderPath); WIN32_FIND_DATA findFileData; HANDLE hFind = ::FindFirstFile(ptcFirstFilePath, &findFileData); delete ptcFirstFilePath; if (INVALID_HANDLE_VALUE == hFind) { return FALSE; } BOOL bFlag = TRUE; do { if (lstrcmpi(findFileData.cFileName, _T(".")) != 0 && lstrcmpi(findFileData.cFileName, _T("..")) != 0) { uiLength = lstrlen(strFolderPath) + lstrlen(_T("\\")) + lstrlen(findFileData.cFileName) + 1; TCHAR *ptcChildPath = new TCHAR[uiLength]; StringCchPrintf(ptcChildPath, uiLength, _T("%s\\%s"), strFolderPath, findFileData.cFileName); bFlag = RemoveFolder(ptcChildPath); delete ptcChildPath; } } while (::FindNextFile(hFind, &findFileData) != 0 && TRUE == bFlag); ::FindClose(hFind); if (FALSE == bFlag) { return FALSE; } return ::RemoveDirectory(strFolderPath); }
nfsstat3 CNFS3Prog::ProcedureREMOVE(void) { char *path; wcc_data dir_wcc; nfsstat3 stat; PrintLog("REMOVE"); std::string dirName; std::string fileName; ReadDirectory(dirName, fileName); path = GetFullPath(dirName, fileName); stat = CheckFile((char*)dirName.c_str(), path); dir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.before.attributes); if (stat == NFS3_OK) { DWORD fileAttr = GetFileAttributes(path); if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) && (fileAttr & FILE_ATTRIBUTE_REPARSE_POINT)) { if (RemoveFolder(path) == 0) { stat = NFS3ERR_IO; } } else { if (!RemoveFile(path)) { stat = NFS3ERR_IO; } } } dir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.after.attributes); Write(&stat); Write(&dir_wcc); return stat; }
// returns true if any downloaded dll is active bool DownloadUpdates(UpdateList &todo, FilenameMap *map, bool dlls_only) { bool dll_enabled_or_langpack = false; bool use_popup = options.popup_notify && ArePopupsEnabled(); // iterate through the updates we need to check if (use_popup) { ShowPopup(0, TranslateT("Downloading Updates"), _T(""), POPFLAG_SAVEHWND, -1); } else { CreateProgressWindow(); SendMessage(hwndProgress, WM_SETTEXT, 0, (LPARAM)TranslateT("Progress - Downloading updates...")); SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Downloading"), 0); } TCHAR msg[512]; TCHAR *temp_str; bool a_download_succeeded = false; for (int index = 0; index < todo.getCount(); index++) { // remember if the user has decided not to install this version char stored_setting[256]; mir_snprintf(stored_setting, SIZEOF(stored_setting), "DisabledVer%s", todo[index].update.szComponentName); DBVARIANT dbv; bool download = todo[index].update_options.enabled; if(!DBGetContactSettingString(0, "Updater", stored_setting, &dbv)) { if(dbv.pszVal && strcmp(dbv.pszVal, todo[index].newVersion) == 0) download = false; else DBDeleteContactSetting(0, "Updater", stored_setting); DBFreeVariant(&dbv); } if(download) { mir_sntprintf(msg, SIZEOF(msg), TranslateT("Downloading plugin: %s"), (temp_str = GetTString(todo[index].update.szComponentName))); mir_free(temp_str); } else { mir_sntprintf(msg, SIZEOF(msg), TranslateT("Skipping plugin: %s"), (temp_str = GetTString(todo[index].update.szComponentName))); mir_free(temp_str); } if (!use_popup) { SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)msg, 0); PostMessage(hwndProgress, WMU_SETPROGRESS, (WPARAM)(int)(index * 100.0 / todo.getCount()), 0); } //else if(hwndPop) // disabled - just annoying //ChangePopupText(hwndPop, msg); if (download) { bool got_file = false; if(todo[index].update_options.use_beta) { // download from i->update.szBetaUpdateURL to temp folder got_file = GetFile(todo[index].update.szBetaUpdateURL, options.temp_folder, todo[index].update.szComponentName, todo[index].newVersion, dlls_only); } else { got_file = GetFile(todo[index].update.szUpdateURL, options.temp_folder, todo[index].update.szComponentName, todo[index].newVersion, dlls_only); } if(got_file) { a_download_succeeded = true; if (todo[index].file_id != -1) { FileNameStruct* fns = map->find((FileNameStruct*)&todo[index].file_id); if (todo[index].cat == MC_PLUGINS || todo[index].cat == MC_UNKNOWN) dll_enabled_or_langpack |= RearrangeDlls(todo[index].shortName, fns->list); else if(todo[index].cat == MC_LOCALIZATION) { RearrangeLangpacks(todo[index].shortName, fns->list); dll_enabled_or_langpack = true; } } else { dll_enabled_or_langpack = true; } } } if (!use_popup && hwndProgress == 0) { RemoveFolder(options.temp_folder); break; // user closed progress window - cancel } } ProgressWindowDone(); // postmessage here causes a lockup on exit! bah popups!! //if(hwndPop) PostMessage(hwndPop, WMU_CLOSEPOP, 0, 0); if (hwndPop) SendMessage(hwndPop, WMU_CLOSEPOP, 0, 0); if(!a_download_succeeded) { for(int i = 0; i < todo.getCount(); ++i) free(todo[i].newVersion); todo.destroy(); } return dll_enabled_or_langpack; }
void CheckForUpdatesWorker(void *param) { if (checking) return; /* // this check doesn't work on some systems - not sure which or why if(!(GetSystemMetrics(SM_NETWORK) & 1)) { ShowError(TranslateT("No network - aborting update check")); NLog("worker thread aborting - no network"); return 1; // no network } */ NLog("CheckForUpdatesWorker thread starting"); checking = true; WriteLastCheckTime(); bool confirm = !(((DWORD)param & 1) == 1); bool restart = !(((DWORD)param & 2) == 2); // if restart is false, then we're doing an 'update and shutdown' if(hwndOptions) PostMessage(hwndOptions, WMU_CHECKING, 0, 0); bool use_popup = options.popup_notify && ArePopupsEnabled(); FilenameMap fn_map(5, CompareFileNameStruct); if (use_popup) { ShowPopup(0, TranslateT("Checking for Updates"), _T(""), POPFLAG_SAVEHWND, -1); } else { CreateProgressWindow(); SendMessage(hwndProgress, WM_SETTEXT, 0, (LPARAM)TranslateT("Progress - Checking for updates...")); if (options.use_xml_backend) SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Downloading XML data"), 0); else SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Checking for updates"), 0); } EnterCriticalSection(&list_cs); if (options.use_xml_backend) { if (UpdateXMLData(MC_PLUGINS)) // prevent double error messages (in some cases) { // iterate through the registered plugins if (!use_popup) SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Scanning plugins folder"), 0); ScanPlugins(&fn_map, &update_list); if (UpdateXMLData(MC_LOCALIZATION)) { if (!use_popup) SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Scanning language packs"), 0); ScanLangpacks(&fn_map, &update_list); } if(!use_popup) SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Updating component file listing ids"), 0); UpdateFLIDs(update_list); } if (!use_popup) SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)TranslateT("Checking for updates"), 0); } UpdateList update_list2(update_list); LeaveCriticalSection(&list_cs); int count = update_list2.getCount(), index = 0; TCHAR msg[512]; TCHAR *temp_str; UpdateList todo; for(index = 0; index < count; index++) { if(update_list2[index].update_options.enabled) { mir_sntprintf(msg, SIZEOF(msg), TranslateT("Checking plugin: %s"), (temp_str = GetTString(update_list2[index].update.szComponentName))); mir_free(temp_str); } else { mir_sntprintf(msg, SIZEOF(msg), TranslateT("Skipping plugin: %s"), (temp_str = GetTString(update_list2[index].update.szComponentName))); mir_free(temp_str); } if(!use_popup) { SendMessage(hwndProgress, WMU_SETMESSAGE, (WPARAM)msg, 0); SendMessage(hwndProgress, WMU_SETPROGRESS, (WPARAM)(int)(index * 100.0 / count), 0); } //else if(hwndPop) // disabled - just annoying //ChangePopupText(hwndPop, msg); if (update_list2[index].update_options.enabled) { char *nv; bool beta; if (nv = UpdateRequired(update_list2[index], &beta)) { todo.insert(new UpdateInternal(update_list2[index])); todo[todo.getCount()-1].newVersion = nv; todo[todo.getCount()-1].update_options.use_beta = beta; } } if (!use_popup && hwndProgress == NULL) { RemoveFolder(options.temp_folder); break; // user closed progress window - cancel } } ProgressWindowDone(); if (hwndPop) PostMessage(hwndPop, WMU_CLOSEPOP, 0 , 0); if(options.use_xml_backend) { FreeXMLData(MC_PLUGINS); FreeXMLData(MC_LOCALIZATION); } bool restore_status = true; if (todo.getCount()) { int cd_ret = CD_OK; if (confirm) { if (use_popup) { ShowPopup(0, TranslateT("Updates Available"), TranslateT("Updated Miranda components detected.\nClick here to install."), POPFLAG_SAVEHWND, -1); DWORD ret; while ((ret = WaitForSingleObject(hEventPop, 200)) == WAIT_TIMEOUT && !Miranda_Terminated()); if (!pop_cancelled && ret == WAIT_OBJECT_0 && !Miranda_Terminated()) { cd_ret = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_CONFIRMUPDATES), GetDesktopWindow(), DlgProcConfirm, (LPARAM)&todo); } else { if(hwndOptions) PostMessage(hwndOptions, WMU_DONECHECKING, 0, 0); RestoreStatus(); for(int i=0; i<todo.getCount(); ++i) free(todo[i].newVersion); checking = false; Netlib_CloseHandle(hNetlibHttp); hNetlibHttp = NULL; return; } } else cd_ret = DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_CONFIRMUPDATES), GetDesktopWindow(), DlgProcConfirm, (LPARAM)&todo); } if (!confirm || cd_ret == CD_CONFALL || cd_ret == CD_NOINSTALL || cd_ret == CD_OK) { bool conf_all = (cd_ret == CD_CONFALL), no_install = (cd_ret == CD_NOINSTALL); // ensure the backup folder exists (either create it or return non-zero signifying error) if (options.backup && !CreatePath(options.backup_folder)) { //MessageBox(0, Translate("Could not create backup folder"), Translate("Error"), MB_OK | MB_ICONERROR); ShowError(TranslateT("Could not create backup folder")); if (hwndOptions) PostMessage(hwndOptions, WMU_DONECHECKING, 0, 0); RestoreStatus(); for (int i=0; i<todo.getCount(); ++i) free(todo[i].newVersion); checking = false; Netlib_CloseHandle(hNetlibHttp); hNetlibHttp = NULL; return; } bool dll_enabled_or_langpack = DownloadUpdates(todo, &fn_map, confirm ? false : options.auto_dll_only); if (todo.getCount() && !no_install) { if (!conf_all || DialogBox(hInst, MAKEINTRESOURCE(IDD_CONFIRMCOMPONENTS), GetDesktopWindow(), DlgProcConfirmComponents) == IDOK) { if (!dll_enabled_or_langpack && restart) { // we're not doing an 'update and shutdown', and we're not updating any active dlls...so just install // get folders TCHAR root_folder[MAX_PATH], plugins_folder[MAX_PATH]; GetRootDir(root_folder); _tcscpy(plugins_folder, root_folder); _tcscat(plugins_folder, _T("\\Plugins")); // move files MoveFiles(0, options.temp_folder, plugins_folder, options.backup_folder, root_folder); // rescan to get correct version numbers ScanPlugins(0, 0); } else { if (ExternProcess(restart) == 0) // if restarting, don't restore status restore_status = false; } } } } for (int i=0; i<todo.getCount(); ++i) free(todo[i].newVersion); } else if (!restart) { PostMessage((HWND)CallService(MS_CLUI_GETHWND, 0, 0), WM_COMMAND, ID_ICQ_EXIT, 0); } if (restore_status && restart) // restore status if we're not doing an 'update and shutdown', and the update was unsuccessful (or there was nothing to update, or no need to shutdown) { checking = false; if (hwndOptions) PostMessage(hwndOptions, WMU_DONECHECKING, 0, 0); RestoreStatus(); } Netlib_CloseHandle(hNetlibHttp); hNetlibHttp = NULL; }
nfsstat3 CNFS3Prog::ProcedureRENAME(void) { char pathFrom[MAXPATHLEN], *pathTo; wcc_data fromdir_wcc, todir_wcc; nfsstat3 stat; PrintLog("RENAME"); std::string dirFromName; std::string fileFromName; ReadDirectory(dirFromName, fileFromName); strcpy_s(pathFrom, GetFullPath(dirFromName, fileFromName)); std::string dirToName; std::string fileToName; ReadDirectory(dirToName, fileToName); pathTo = GetFullPath(dirToName, fileToName); stat = CheckFile((char*)dirFromName.c_str(), pathFrom); fromdir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirFromName.c_str(), &fromdir_wcc.before.attributes); todir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirToName.c_str(), &todir_wcc.before.attributes); if (FileExists(pathTo)) { DWORD fileAttr = GetFileAttributes(pathTo); if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) && (fileAttr & FILE_ATTRIBUTE_REPARSE_POINT)) { if (RemoveFolder(pathTo) == 0) { stat = NFS3ERR_IO; } } else { if (!RemoveFile(pathTo)) { stat = NFS3ERR_IO; } } } if (stat == NFS3_OK) { errno_t errorNumber = RenameDirectory(pathFrom, pathTo); if (errorNumber != 0) { char buffer[BUFFER_SIZE]; strerror_s(buffer, BUFFER_SIZE, errorNumber); PrintLog(buffer); if (errorNumber == 13) { stat = NFS3ERR_ACCES; } else { stat = NFS3ERR_IO; } } } fromdir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirFromName.c_str(), &fromdir_wcc.after.attributes); todir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirToName.c_str(), &todir_wcc.after.attributes); Write(&stat); Write(&fromdir_wcc); Write(&todir_wcc); return stat; }
NOINLINE bln Files::RemoveFolder( const FilePath &path, CError *po_error ) // potentially recursive { uiw len; CWStr buf; WIN32_FIND_DATAW o_find; HANDLE h_find; CError o_error; bln funcResult = false; if( !Files::IsFolder( path, &o_error ) ) // will check the pnn { if( o_error == Error::Ok() ) { o_error = Error::InvalidArgument(); } goto toExit; } buf = path.PlatformPath(); len = buf.Size() + 1; buf += L"\\*"; h_find = ::FindFirstFileW( buf.CStr(), &o_find ); if( h_find == INVALID_HANDLE_VALUE ) { goto toExit; } do { if( !::wcscmp( o_find.cFileName, L"." ) || !::wcscmp( o_find.cFileName, L".." ) ) { continue; } buf.Resize( len ); buf += o_find.cFileName; if( o_find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { if( !RemoveFolder( buf.CStr(), po_error ) ) { ::FindClose( h_find ); goto toExit; } } else if( !RemoveFile( buf.CStr(), po_error ) ) { ::FindClose( h_find ); goto toExit; } } while( ::FindNextFileW( h_find, &o_find ) ); ::FindClose( h_find ); funcResult = ::RemoveDirectoryW( path.PlatformPath() ) != 0; if( !funcResult ) { o_error = Error::UnknownError(); } toExit: DSA( po_error, o_error ); return funcResult; }