asdlei00 / crashrpt

Automatically exported from code.google.com/p/crashrpt
0 stars 0 forks source link

When copying a log file into the crash archive, access it with FILE_SHARE_WRITE #124

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It may be simpler to integrate CrashRpt into an existing application, if it 
accesses a log file as read only and FILE_SHARE_WRITE, so the application does 
not need to close the log file before it gets copied into the crash archive. 
See the following code snippet, changes marked with //Vojtech

in BOOL CErrorReportSender::CollectCrashFiles():

    std::map<CString, ERIFileItem>::iterator it;
    for(it=g_CrashInfo.GetReport(m_nCurReport).m_FileItems.begin(); it!=g_CrashInfo.GetReport(m_nCurReport).m_FileItems.end(); it++)
    {
        if(m_Assync.IsCancelled())
            goto cleanup;

        if(it->second.m_bMakeCopy)
        {
            str.Format(_T("Copying file %s."), it->second.m_sSrcFile);
            m_Assync.SetProgress(str, 0, false);

            //Vojtech: Share source file with the producer
            hSrcFile = CreateFile(it->second.m_sSrcFile, GENERIC_READ, 
                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
            if(hSrcFile==INVALID_HANDLE_VALUE)
            {
                it->second.m_sErrorStatus = Utility::FormatErrorMsg(GetLastError());
                str.Format(_T("Error opening file %s."), it->second.m_sSrcFile);
                m_Assync.SetProgress(str, 0, false);
            }

            bGetSize = GetFileSizeEx(hSrcFile, &lFileSize);
            if(!bGetSize)
            {
                it->second.m_sErrorStatus = Utility::FormatErrorMsg(GetLastError());
                str.Format(_T("Couldn't get file size of %s"), it->second.m_sSrcFile);
                m_Assync.SetProgress(str, 0, false);
                CloseHandle(hSrcFile);
                hSrcFile = INVALID_HANDLE_VALUE;
                continue;
            }

            sDestFile = sErrorReportDir + _T("\\") + it->second.m_sDestFile;

            hDestFile = CreateFile(sDestFile, GENERIC_WRITE, 
                FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
            if(hDestFile==INVALID_HANDLE_VALUE)
            {
                it->second.m_sErrorStatus = Utility::FormatErrorMsg(GetLastError());
                str.Format(_T("Error creating file %s."), sDestFile);
                m_Assync.SetProgress(str, 0, false);
                CloseHandle(hSrcFile);
                hSrcFile = INVALID_HANDLE_VALUE;
                continue;
            }

            lTotalWritten.QuadPart = 0;

            for(;;)
            {        
                if(m_Assync.IsCancelled())
                    goto cleanup;

                bRead = ReadFile(hSrcFile, buffer, 1024, &dwBytesRead, NULL);
                if(!bRead || dwBytesRead==0)
                    break;

                bWrite = WriteFile(hDestFile, buffer, dwBytesRead, &dwBytesWritten, NULL);
                if(!bWrite || dwBytesRead!=dwBytesWritten)
                    break;

                lTotalWritten.QuadPart += dwBytesWritten;

                int nProgress = (int)(100.0f*lTotalWritten.QuadPart/lFileSize.QuadPart);

                m_Assync.SetProgress(nProgress, false);
            }

            CloseHandle(hSrcFile);
            hSrcFile = INVALID_HANDLE_VALUE;
            CloseHandle(hDestFile);
            hDestFile = INVALID_HANDLE_VALUE;

            //Vojtech: Use the copy for display and zipping.
            it->second.m_sSrcFile = sDestFile;
        }
    }

Original issue reported on code.google.com by bubn...@gmail.com on 3 Jan 2012 at 11:49

GoogleCodeExporter commented 9 years ago

Original comment by zexspect...@gmail.com on 5 Jan 2012 at 8:27

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1338.

Original comment by zexspect...@gmail.com on 25 Aug 2012 at 6:47