// 将pc中的文件夹从一个目录拷贝到另外的一个目录 BOOL MoveDirectory(CString strSrcPath, CString strDesPath) { if( strSrcPath.IsEmpty() ) { return FALSE; } if ( !PathIsDirectory(strDesPath) ) { if ( !CreateDirectory(strDesPath,NULL)) return FALSE; } if ( strSrcPath.GetAt(strSrcPath.GetLength()-1) != '\\' ) strSrcPath += '\\'; if ( strDesPath.GetAt(strDesPath.GetLength()-1) != '\\' ) strDesPath += '\\'; BOOL bRet = FALSE; // 因为源目录不可能为空,所以该值一定会被修改 CFileFind ff; BOOL bFound = ff.FindFile(strSrcPath+_T("*"), 0); CString strFile; BOOL bSpecialFile=FALSE; while(bFound) // 递归拷贝 { bFound = ff.FindNextFile(); bSpecialFile=FALSE; if( ff.IsDots() ) continue; CString strSubSrcPath = ff.GetFilePath(); CString strSubDespath = strSubSrcPath; strSubDespath.Replace(strSrcPath, strDesPath); if( ff.IsDirectory() ) bRet = MoveDirectory(strSubSrcPath, strSubDespath); // 递归拷贝文件夹 else { strFile=PathFindFileName(strSubSrcPath); strFile.MakeUpper(); for (int i=0;i<nSpecialFileCount;i++) { //找到特殊文件 if(_tcscmp(strFile.GetString(),sSpecialFile[i])==0) { bSpecialFile=TRUE; break; } } if(bSpecialFile) bRet=MoveFileEx( strSubSrcPath,strSubDespath,MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); else bRet = MoveFileEx(strSubSrcPath, strSubDespath,MOVEFILE_REPLACE_EXISTING); // 移动文件 } if ( !bRet ) break; } ff.Close(); return bRet; }
bool GalleryUtil::Move(const QFileInfo &src, QFileInfo &dst) { if (src.isDir()) return MoveDirectory(src, dst); dst = MakeUnique(dst); if (!FileMove(src, dst)) return false; MSqlQuery query(MSqlQuery::InitCon()); query.prepare("UPDATE gallerymetadata " "SET image = :IMAGENEW " "WHERE image = :IMAGEOLD"); query.bindValue(":IMAGENEW", dst.absoluteFilePath()); query.bindValue(":IMAGEOLD", src.absoluteFilePath()); if (query.exec()) return true; // try to undo move on DB failure FileMove(dst, src); return false; }
//更新功能函数 UINT DoPatch( LPVOID lp1 ) { //进程是否存在 BOOL bIsExistApp=TRUE; BOOL bIsRunSvc=TRUE; int nTmp=0; bool bTmp=TRUE; //杀死和停止服务重试次数 int nRetryKillCount=0; int nRetryKillMaxCount=5; CString str; TCHAR tz1[MAX_PATH]={0}; CString sTmp; BOOL bFind=FALSE; BOOL bCheck=FALSE; int nHintCount=10; UINT ret=S_OK; CPatchDlg *pDlg=NULL; pDlg=(CPatchDlg*)theApp.GetMainWnd(); //检测新版本 pDlg->CheckVersion(&bFind); if(bFind) { Log(_T("发现新版本.进行文件检验!\r\n")); pDlg->GetCrcInfo(&bCheck,tz1,MAX_PATH); if(bCheck) { Log(_T("检验通过!\r\n")); //检查是否有更新 //检查要更新进程是否退出 nTmp=0; Log(_T("正在检测需更新的程序是否已经退出!\r\n")); for (int i=0;i<nAppCount;i++) { nTmp=GetProcessIdByName(sPatchAppName[i][0].GetString()); if(nTmp==0) { //没有启动的,按默认。 continue; } else { //有在启动的 改为T, sPatchAppName[i][1]=_T("T"); } nRetryKillCount=0; Sleep(1000); do { Sleep(1000); KillProcess(nTmp); nTmp=GetProcessIdByName(sPatchAppName[i][0].GetString()); nRetryKillCount++; } while (nTmp!=0 && nRetryKillCount<nRetryKillMaxCount); if(nRetryKillCount>=nRetryKillMaxCount) { Log(_T("尝试杀死进程%s失败.已经重试%d次.更新程序不能继续.\r\n"),sPatchAppName[i][0].GetString(),nRetryKillCount); ret=S_FALSE; goto Done; } } Log(_T("需更新的程序已经退出!\r\n")); //停止服务 Log(_T("正在检测需更新的服务是否已经停止!\r\n")); for(int i=0;i<nSvcCount;i++) { nRetryKillCount=0; while(QueryServiceStatus_dan(sPatchSvcName[i][0].GetString(),SERVICE_RUNNING)|| nRetryKillCount>=nRetryKillMaxCount) { str.Empty(); str.AppendFormat(_T("%s,%s"),sPatchSvcName[i][0],sPatchSvcName[i][1]); StopService_dan((LPVOID)str.GetString()); nRetryKillCount++; Sleep(1000); //暂停一下。 if(QueryServiceStatus_dan(sPatchSvcName[i][0].GetString(),SERVICE_RUNNING) || QueryServiceStatus_dan(sPatchSvcName[i][0].GetString(),SERVICE_START_PENDING)|| QueryServiceStatus_dan(sPatchSvcName[i][0].GetString(),SERVICE_STOP_PENDING)) { nTmp=GetProcessIdByName(sPatchSvcName[i][1].GetString()); if(nTmp>0) KillProcess(nTmp); } } if(nRetryKillCount>=nRetryKillMaxCount) { str.Empty(); str.AppendFormat(_T("尝试停止服务%s失败.已经重试%d次.更新程序不能继续.\r\n"),sPatchSvcName[i][0].GetString(),nRetryKillCount); Log(str.GetBuffer()); ret= S_FALSE; goto Done; } } Log(_T("需更新的服务已经停止!\r\n")); //移动文件 Log(_T("进行更新文件操作.\r\n")); GetAppPath(tz1); sTmp.AppendFormat(_T("%s\\%s"),tz1,sUpdateDir); if(MoveDirectory(sTmp.GetString(),tz1)) { Log(_T("更新文件完毕.\r\n")); //重新启动被更新程序 for (int i=0;i<nAppCount;i++) { str.Empty(); str.AppendFormat(_T("%s\\%s"),sAppPath,sPatchAppName[i][0].GetString()); //F不启动 if(sPatchAppName[i][1]==_T("F")) continue; if(Execute(str,NULL,FALSE,FALSE)) { Log(_T("程序%s启动成功.\r\n"),sPatchAppName[i][0].GetString()); } } //启动服务 for(int i=0;i<nSvcCount;i++) { if(StartService_dan((LPVOID)sPatchSvcName[i][0].GetString())) { Log(_T("服务%s启动成功.\r\n"),sPatchSvcName[i][0].GetString()); } } ret=S_OK; } else { Log(_T("更新文件失败.更新程序不能继续.\r\n")); ret= S_FALSE; } } else { Log(_T("检验不通过!\r\n%s"),tz1); } } else { Log(_T("正在检查服务状态!\r\n")); CheckSyncSvc(); } Done: for(int i=nHintCount;i>0;i--) { Log(_T("%d 秒后关闭。\r\n"),i); Sleep(1000); } pDlg->isCanClose=TRUE; pDlg->SendMessage(WM_CLOSE); return ret; }