Example #1
0
// Lists the status for all writers
void VssClient::ListWriterStatus()
{
    FunctionTracer ft(DBG_INFO);

    ft.WriteLine(L"Listing writer status ..."); 
    
    // Gets the number of writers in the gathered status info
    // (WARNING: GatherWriterStatus must be called before)
    unsigned cWriters = 0;
    CHECK_COM(m_pVssObject->GetWriterStatusCount(&cWriters));
    ft.WriteLine(L"- Number of writers that responded: %u", cWriters);  

    // Enumerate each writer
    for(unsigned iWriter = 0; iWriter < cWriters; iWriter++)
    {
        VSS_ID idInstance = GUID_NULL;
        VSS_ID idWriter= GUID_NULL;
        VSS_WRITER_STATE eWriterStatus = VSS_WS_UNKNOWN;
        CComBSTR bstrWriterName;
        HRESULT hrWriterFailure = S_OK;

        // Get writer status
        CHECK_COM(m_pVssObject->GetWriterStatus(iWriter,
                             &idInstance,
                             &idWriter,
                             &bstrWriterName,
                             &eWriterStatus,
                             &hrWriterFailure));

        // Print writer status
        ft.WriteLine(L"\n"
            L"* WRITER \"%s\"\n"
            L"   - Status: %d (%s)\n" 
            L"   - Writer Failure code: 0x%08lx (%s)\n" 
            L"   - Writer ID: " WSTR_GUID_FMT L"\n"
            L"   - Instance ID: " WSTR_GUID_FMT L"\n",
            (PWCHAR)bstrWriterName,
            eWriterStatus, GetStringFromWriterStatus(eWriterStatus).c_str(), 
            hrWriterFailure, FunctionTracer::HResult2String(hrWriterFailure).c_str(),
            GUID_PRINTF_ARG(idWriter),
            GUID_PRINTF_ARG(idInstance)
            );
    }
}
// Check the status for all selected writers.
HRESULT GoogleVssClient::CheckSelectedWriterStatus() {
  // Gather writer status to detect potential errors.
  HRESULT hr = GatherWriterStatus();
  if (SUCCEEDED(hr)) {
    // Gets the number of writers in the gathered status info
    // (WARNING: GatherWriterStatus must be called before).
    unsigned writers = 0;
    hr = vss_object_->GetWriterStatusCount(&writers);
    if (FAILED(hr)) {
      LogDebugMessage(L"GetWriterStatusCount failed with error %x ", hr);
    } else {
      // Enumerate each writer.
      HRESULT hrWriterFailure = S_OK;
      for (unsigned writer = 0; writer < writers; writer++) {
        VSS_ID idInstance = GUID_NULL;
        VSS_ID idWriter = GUID_NULL;
        VSS_WRITER_STATE writerStatus = VSS_WS_UNKNOWN;
        CComBSTR bstrWriterName;
        // Get writer status.
        hr = vss_object_->GetWriterStatus(writer, &idInstance, &idWriter,
                                          &bstrWriterName, &writerStatus,
                                          &hrWriterFailure);
        // If the writer is not selected, just continue.
        if (!IsWriterSelected(idInstance)) {
          continue;
        }
        // If the writer is in non-stable state, break.
        switch (writerStatus) {
          case VSS_WS_FAILED_AT_IDENTIFY:
          case VSS_WS_FAILED_AT_PREPARE_BACKUP:
          case VSS_WS_FAILED_AT_PREPARE_SNAPSHOT:
          case VSS_WS_FAILED_AT_FREEZE:
          case VSS_WS_FAILED_AT_THAW:
          case VSS_WS_FAILED_AT_POST_SNAPSHOT:
          case VSS_WS_FAILED_AT_BACKUP_COMPLETE:
          case VSS_WS_FAILED_AT_PRE_RESTORE:
          case VSS_WS_FAILED_AT_POST_RESTORE:
            // Print writer status.
            LogDebugMessage(
                L"ERROR: Selected writer '%s' is in failed state."
                L" Status: %d (%s), Writer Failure code: 0x%08lx,"
                L" Writer ID: " WSTR_GUID_FMT L" Instance ID: " WSTR_GUID_FMT,
                BstrToWString(bstrWriterName).c_str(), writerStatus,
                GetStringFromWriterStatus(writerStatus).c_str(),
                hrWriterFailure,
                GUID_PRINTF_ARG(idWriter), GUID_PRINTF_ARG(idInstance));
            // Stop here.
            hr = E_UNEXPECTED;
            break;
          default:
            break;
        }
        if (FAILED(hr)) {
          break;
        }
      }
    }
  }
  LogDebugMessage(L"CheckSelectedWriterStatus returned with %x", hr);
  return hr;
}