HRESULT GetCurrTime(FILETIME *pftCurrTime, DWORD dwSeconds) { FILETIME ftCurrTime; ULARGE_INTEGER uliTime; HRESULT hr = S_OK; TCHAR szTempFilePath[MAX_PATH+1]; HANDLE hFile = INVALID_HANDLE_VALUE; #define TEMP_FILE_LEN (15) memset(pftCurrTime, 0, sizeof(ULARGE_INTEGER)); DWORD dwLen = MAX_PATH; hr = GetPendingDeletePath( NULL, ASM_CACHE_DOWNLOAD, szTempFilePath, &dwLen); if (FAILED(hr)) { goto exit; } dwLen = MAX_PATH - dwLen; dwLen = TEMP_FILE_LEN; if((dwLen + lstrlenW(szTempFilePath) + 1)> MAX_PATH) { hr = E_FAIL; goto exit; } GetRandomFileName(szTempFilePath, dwLen); hFile = CreateFile(szTempFilePath, GENERIC_WRITE, 0 /* no sharing */, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL); if (hFile == INVALID_HANDLE_VALUE) { hr = HRESULT_FROM_WIN32(GetLastError()); } else if(!GetFileTime(hFile, &ftCurrTime, NULL, NULL)) { hr = HRESULT_FROM_WIN32(GetLastError()); } exit: if(FAILED(hr)) { GetSystemTimeAsFileTime(&ftCurrTime); hr = S_OK; } memcpy( &uliTime, &ftCurrTime, sizeof(ULARGE_INTEGER)); uliTime.QuadPart -= dwSeconds * 10000000; // 1 second = 10 ** 7 units in SystemTime. memcpy(pftCurrTime, &uliTime, sizeof(ULARGE_INTEGER)); if(hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); DeleteFile(szTempFilePath); } return hr; }
tstring PathX::GetRandomFileName(int nameSize) { tstring outRandomName; GetRandomFileName(nameSize, outRandomName); return outRandomName; }
// --------------------------------------------------------------------------- // CScavenger::DeleteAssembly // Deletes the given TransCache entry after deleting bits. // --------------------------------------------------------------------------- HRESULT CScavenger::DeleteAssembly( DWORD dwCacheFlags, LPCWSTR pszCustomPath, LPWSTR pszManFilePath, BOOL bForceDelete) { HRESULT hr = S_OK; LPTSTR pszTemp=NULL; TCHAR szPendDelDirPath[MAX_PATH+1]; TCHAR szAsmDirPath[MAX_PATH+1]; #define TEMP_PEND_DIR 10 DWORD dwLen = 0; LPWSTR pszManifestPath=pszManFilePath; ASSERT( pszManFilePath); if(bForceDelete) return DeleteAssemblyFiles( dwCacheFlags, pszCustomPath, pszManFilePath); dwLen = lstrlen(pszManifestPath); ASSERT(dwLen <= MAX_PATH); DWORD dwAttrib; if((dwAttrib = GetFileAttributes(pszManifestPath)) == (DWORD) -1) { if(bForceDelete) { return DeleteAssemblyFiles( dwCacheFlags, pszCustomPath, pszManFilePath); } hr = S_FALSE; goto exit; } StrCpy(szAsmDirPath, pszManifestPath); if(!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) { // looks manifestFilePath is passed in. knock-off the filename. pszTemp = PathFindFileName(szAsmDirPath); if(pszTemp > szAsmDirPath) { *(pszTemp-1) = L'\0'; } } dwLen = MAX_PATH; hr = GetPendingDeletePath( pszCustomPath, dwCacheFlags, szPendDelDirPath, &dwLen); if (FAILED(hr)) { goto exit; } if(lstrlen(szPendDelDirPath) + TEMP_PEND_DIR > MAX_PATH) { hr = HRESULT_FROM_WIN32(FUSION_E_INVALID_NAME); goto exit; } GetRandomFileName( szPendDelDirPath, TEMP_PEND_DIR); if(!MoveFile( szAsmDirPath, szPendDelDirPath)) { hr = FusionpHresultFromLastError(); /* #ifdef DEBUG WCHAR szMsgBuf[MAX_PATH*2 + 1]; wnsprintf( szMsgBuf, MAX_PATH*2, L" MoveFile((%s, %s)) FAILED in DeleteAssembly with <%x> \r\n", szAsmDirPath, szPendDelDirPath, hr ); WriteToLogFile(szMsgBuf); #endif // DEBUG */ if( (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ) || (hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) ) { hr = S_OK; goto exit; } } else { hr = RemoveDirectoryAndChildren(szPendDelDirPath); hr = S_OK; // don't worry about passing back error here. its already in pend-del dir. goto exit; } exit : if( (hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)) || (hr == HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION)) ) { // We cannot delete this as someone else has locked it... hr = HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION); } if(hr == S_OK) { pszTemp = PathFindFileName(szAsmDirPath); if(pszTemp > szAsmDirPath) { *(pszTemp-1) = L'\0'; RemoveDirectory(szAsmDirPath); } } return hr; }
HRESULT DeleteAssemblyFiles(DWORD dwCacheFlags, LPCWSTR pszCustomPath, LPWSTR pszManFilePath) { HRESULT hr = S_OK; LPTSTR pszTemp=NULL; LPWSTR pszManifestPath=pszManFilePath; TCHAR szPendDelDirPath[MAX_PATH+1]; TCHAR szAsmDirPath[MAX_PATH+1]; #define TEMP_PEND_DIR 10 DWORD dwLen = 0; if(!pszManifestPath) { hr = E_FAIL; goto exit; } dwLen = lstrlen(pszManifestPath); ASSERT(dwLen <= MAX_PATH); lstrcpy(szAsmDirPath, pszManifestPath); pszTemp = PathFindFileName(szAsmDirPath); if(pszTemp > szAsmDirPath) { *(pszTemp-1) = L'\0'; } dwLen = MAX_PATH; hr = GetPendingDeletePath( pszCustomPath, dwCacheFlags, szPendDelDirPath, &dwLen); if (FAILED(hr)) { goto exit; } if(lstrlen(szPendDelDirPath) + TEMP_PEND_DIR > MAX_PATH) { hr = HRESULT_FROM_WIN32(FUSION_E_INVALID_NAME); goto exit; } GetRandomFileName( szPendDelDirPath, TEMP_PEND_DIR); if(!MoveFile( szAsmDirPath, szPendDelDirPath)) { hr = FusionpHresultFromLastError(); #ifdef DEBUG WCHAR szMsgBuf[MAX_PATH*2 + 1]; wnsprintf( szMsgBuf, MAX_PATH*2, L" MoveFile((%s, %s)) FAILED in DeleteAssemblyFiles with <%x> \r\n", szAsmDirPath, szPendDelDirPath, hr ); WriteToLogFile(szMsgBuf); #endif // DEBUG if( (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ) || (hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)) ) { hr = S_OK; goto exit; } } else { hr = RemoveDirectoryAndChildren(szPendDelDirPath); hr = S_OK; // don't worry about passing back error here. its already in pend-del dir. goto exit; } // looks like there are some in-use files here. // move all the asm files to pend del dir. if(!CreateDirectory(szPendDelDirPath, NULL)) { hr = FusionpHresultFromLastError(); goto exit; } hr = MoveAllFilesFromDir(szAsmDirPath, szPendDelDirPath); if(hr == S_OK) { // assembly deleted successfully delete/pend all temp files. hr = RemoveDirectoryAndChildren(szPendDelDirPath); hr = S_OK; // don't worry about passing back error here. its already in pend-del dir. } else { // could not delete assembly; restore all files back to original state. #ifdef DEBUG HRESULT hrTemp = #endif MoveAllFilesFromDir(szPendDelDirPath, szAsmDirPath); #ifdef DEBUG WCHAR szMsgBuf[MAX_PATH*2 + 1]; wnsprintf( szMsgBuf, MAX_PATH*2, L" Restored all files back to <%s> hr = <%x> hrTemp = <%x> \r\n", szAsmDirPath, hr, hrTemp ); WriteToLogFile(szMsgBuf); #endif // DEBUG hr = HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION); } exit : if(hr == S_OK) { pszTemp = PathFindFileName(szAsmDirPath); if(pszTemp > szAsmDirPath) { *(pszTemp-1) = L'\0'; } // now that we have two levels of dirs...try to remove parent dir also // this succeeds only if it is empty, don't worry about return value. RemoveDirectory(szAsmDirPath); } return hr; }
// 获取当前系统的临时文件夹的路径下的唯一命名的临时文件名(全路径) tstring CPath::GetTempFileName(LPCTSTR lpszFileName) { return GetRandomFileName(GetTempPath().c_str(), lpszFileName); }