BOOL KFunction::_CopyFolder(LPCTSTR szSrcPath, LPCTSTR szDstPath, BOOL& bReboot) { BOOL bReturn = FALSE; BOOL bRetCode = FALSE; WIN32_FIND_DATA find_data; HANDLE hFind = INVALID_HANDLE_VALUE; CString strSrcPath(szSrcPath); CString strDstPath(szDstPath); hFind = ::FindFirstFile(strSrcPath + _T("*"), &find_data); if (hFind == INVALID_HANDLE_VALUE) { bReturn = TRUE; goto Exit0; } do{ if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (lstrcmp(find_data.cFileName, _T(".")) != 0 && lstrcmp(find_data.cFileName, _T("..")) != 0) { bRetCode = ::CreateDirectory(strDstPath + find_data.cFileName, NULL); if (!bRetCode && ::GetLastError() != ERROR_ALREADY_EXISTS) goto Exit0; bRetCode = KFunction::_CopyFolder(strSrcPath + find_data.cFileName + _T("\\"), strDstPath + find_data.cFileName + _T("\\"), bReboot); if (!bRetCode) goto Exit0; } } else { bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, strDstPath + find_data.cFileName, FALSE); if (!bRetCode) { bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, strDstPath + find_data.cFileName + _T("_up"), FALSE); if (!bRetCode) goto Exit0; bRetCode = ::MoveFileEx(strDstPath + find_data.cFileName + _T("_up"), strDstPath + find_data.cFileName, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING); if (!bRetCode) goto Exit0; bReboot = TRUE; } } bRetCode = ::FindNextFile(hFind, &find_data); } while(bRetCode); bReturn = TRUE; Exit0: if (hFind) FindClose(hFind); return bReturn; }
int main(int argc, char *argv[]) { if (argc > 1) { if ( 0 == strcmp (argv[1] , "-g") && argc > 2 ) { GenerateList(argv[2]); } else if (0 == strcmp (argv[1] , "-c")) { if (argc < 5) { std::cerr << "Too few parameters for copy mode" << std::endl << std::endl; PrintHelp(); return 1; } std::string strFileList (argv[2]); std::string strSrcPath (argv[3]); std::string strDstPath (argv[4]); CopyList(strFileList, strSrcPath, strDstPath); } else if (0 == strcmp (argv[1] , "-e")) { if (argc < 4) { std::cerr << "Too few parameters for exclude mode" << std::endl << std::endl; PrintHelp(); return 1; } std::string strBaseList (argv[2]); std::string strDiffList (argv[3]); ExcludeList(strBaseList, strDiffList); } else { PrintHelp(); } } else { PrintHelp(); } return 0; }
BOOL KFunction::_IsEnableCopy(LPCTSTR szSrcPath, LPCTSTR szDstPath) { BOOL bReturn = FALSE; BOOL bRetCode = FALSE; WIN32_FIND_DATA find_data; HANDLE hFind = INVALID_HANDLE_VALUE; CString strSrcPath(szSrcPath); CString strDstPath(szDstPath); hFind = ::FindFirstFile(strSrcPath + _T("*"), &find_data); if (hFind == INVALID_HANDLE_VALUE) { bReturn = TRUE; goto Exit0; } do{ if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (lstrcmp(find_data.cFileName, _T(".")) != 0 && lstrcmp(find_data.cFileName, _T("..")) != 0) { bRetCode = KFunction::_IsEnableCopy(strSrcPath + find_data.cFileName + _T("\\"), strDstPath + find_data.cFileName + _T("\\")); if (!bRetCode) goto Exit0; } } else { if (KFunction::IsFileInUse(strDstPath + find_data.cFileName)) { log_w(L"KFunction::_IsEnableCopy IsFileInUse return:1, path:%s\n", (LPCTSTR)(strDstPath + find_data.cFileName)); goto Exit0; } } bRetCode = ::FindNextFile(hFind, &find_data); } while(bRetCode); bReturn = TRUE; Exit0: if (hFind) FindClose(hFind); return bReturn; }
BOOL KFunction::IsEnableCopy(LPCTSTR szSrcPath, LPCTSTR szDstPath) { BOOL bReturn = FALSE; BOOL bRetCode = FALSE; CString strSrcPath(szSrcPath); CString strDstPath(szDstPath); KFunction::PathAddBackslash(strSrcPath); KFunction::PathAddBackslash(strDstPath); bRetCode = _IsEnableCopy(strSrcPath, strDstPath); if (!bRetCode) goto Exit0; bReturn = TRUE; Exit0: log_w(L"KFunction::IsEnableCopy return:%d, src:%s, dst:%s\n", bReturn, szSrcPath, szDstPath); return bReturn; }
BOOL KFunction::CopyFolder_Force(LPCTSTR szSrcPath, LPCTSTR szDstPath) { BOOL bReturn = FALSE; BOOL bRetCode = FALSE; CString strSrcPath(szSrcPath); CString strDstPath(szDstPath); KFunction::PathAddBackslash(strSrcPath); KFunction::PathAddBackslash(strDstPath); bRetCode = CreateDeepDirectory(strDstPath); if (!bRetCode) goto Exit0; bRetCode = _CopyFolder_Force(strSrcPath, strDstPath); if (!bRetCode) goto Exit0; bReturn = TRUE; Exit0: log_w(L"KFunction::CopyFolder_Force return:%d, src:%s, dst:%s\n", bReturn, szSrcPath, szDstPath); return bReturn; }
BOOL KFunction::_CopyFolder_Force(LPCTSTR szSrcPath, LPCTSTR szDstPath) { BOOL bReturn = FALSE; BOOL bRetCode = FALSE; WIN32_FIND_DATA find_data; HANDLE hFind = INVALID_HANDLE_VALUE; CString strSrcPath(szSrcPath); CString strDstPath(szDstPath); hFind = ::FindFirstFile(strSrcPath + _T("*"), &find_data); if (hFind == INVALID_HANDLE_VALUE) { bReturn = TRUE; goto Exit0; } do{ if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (lstrcmp(find_data.cFileName, _T(".")) != 0 && lstrcmp(find_data.cFileName, _T("..")) != 0) { bRetCode = ::CreateDirectory(strDstPath + find_data.cFileName, NULL); if (!bRetCode && ::GetLastError() != ERROR_ALREADY_EXISTS) goto Exit0; bRetCode = KFunction::_CopyFolder_Force(strSrcPath + find_data.cFileName + _T("\\"), strDstPath + find_data.cFileName + _T("\\")); if (!bRetCode) goto Exit0; } } else { bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, strDstPath + find_data.cFileName, FALSE); if (!bRetCode) { bRetCode = ::MoveFile(strDstPath + find_data.cFileName, strDstPath + find_data.cFileName + _T("_del")); if (bRetCode) { ::MoveFileEx(strDstPath + find_data.cFileName + _T("_del"), NULL, MOVEFILE_DELAY_UNTIL_REBOOT); bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, strDstPath + find_data.cFileName, FALSE); log_w(L"CopyFolder_Force CopyFile fail, try rename return:%d, filename:%s\n", bRetCode, strDstPath + find_data.cFileName); if (!bRetCode) { bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, strDstPath + find_data.cFileName + _T("_up"), FALSE); if (bRetCode) ::MoveFileEx(strDstPath + find_data.cFileName + _T("_up"), strDstPath + find_data.cFileName, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING); } } else { log_w(L"CopyFolder_Force rename fail, try MoveFileEx filename:%s\n", strDstPath + find_data.cFileName); bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, strDstPath + find_data.cFileName + _T("_up"), FALSE); if (bRetCode) ::MoveFileEx(strDstPath + find_data.cFileName + _T("_up"), strDstPath + find_data.cFileName, MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING); } } if (KFunction::IsDriverFile(find_data.cFileName)) { if (_tcsicmp(find_data.cFileName, _T("kisknl64.sys")) == 0) { if (KFunction::IsWow64()) { KFunction::EnableWow64FsRedirection(FALSE); bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, KFunction::GetDriversPath() + _T("kisknl.sys"), FALSE); log_w(L"Copy driver file return:%d, src:%ws, dst:%ws\n", bRetCode, strSrcPath + find_data.cFileName, KFunction::GetDriversPath() + _T("kisknl.sys")); KFunction::EnableWow64FsRedirection(TRUE); } } else if (_tcsicmp(find_data.cFileName, _T("kisknl.sys")) == 0) { if (!KFunction::IsWow64()) { bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, KFunction::GetDriversPath() + _T("kisknl.sys"), FALSE); log_w(L"Copy driver file return:%d, src:%ws, dst:%ws\n", bRetCode, strSrcPath + find_data.cFileName, KFunction::GetDriversPath() + _T("kisknl.sys")); } } else { bRetCode = ::CopyFile(strSrcPath + find_data.cFileName, KFunction::GetDriversPath() + find_data.cFileName, FALSE); log_w(L"Copy driver file return:%d, src:%ws, dst:%ws\n", bRetCode, strSrcPath + find_data.cFileName, KFunction::GetDriversPath() + find_data.cFileName); } } } bRetCode = ::FindNextFile(hFind, &find_data); } while(bRetCode); bReturn = TRUE; Exit0: if (hFind) FindClose(hFind); return bReturn; }