summaryrefslogtreecommitdiff
path: root/shared/ShadowCopy/shadow.cpp
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:08:06 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:08:06 +0200
commitfbe76102e941b9f1edaf236788e42678f05fdf9a (patch)
treef5f538316019fa89be8dc478103490c3a826f3ac /shared/ShadowCopy/shadow.cpp
parent3.8 (diff)
downloadFreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.gz
FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.bz2
FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.zip
3.9
Diffstat (limited to 'shared/ShadowCopy/shadow.cpp')
-rw-r--r--shared/ShadowCopy/shadow.cpp139
1 files changed, 49 insertions, 90 deletions
diff --git a/shared/ShadowCopy/shadow.cpp b/shared/ShadowCopy/shadow.cpp
index 1fb78769..2e739146 100644
--- a/shared/ShadowCopy/shadow.cpp
+++ b/shared/ShadowCopy/shadow.cpp
@@ -8,6 +8,8 @@
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
+#include "../com_ptr.h"
+#include "../com_error.h"
#ifdef USE_SHADOW_XP
#include "xp/inc/vss.h"
@@ -15,7 +17,7 @@
#include "xp/inc/vsbackup.h"
#elif defined USE_SHADOW_2003
-#include "Server 2003/inc/vss.h"
+#include "Server 2003/inc/vss.h"
#include "Server 2003/inc/vswriter.h"
#include "Server 2003/inc/vsbackup.h"
#else
@@ -30,79 +32,37 @@ adapt!
//typedef GUID VSS_ID;
-
-//IShellItem resource management: better handled with boost::shared_ptr or CComPtr, but we avoid dependency with boost and ATL in this case
-template <class T>
-class ItemHolder
-{
-public:
- ItemHolder(T* item) : item_(item) {}
- ~ItemHolder()
- {
- if (item_)
- item_->Release();
- }
-
- T* release()
-{
-T* rv = item_;
-item_ = 0;
- return rv;
- }
-
- T* operator->() const
-{
- return item_;
- }
-private:
- ItemHolder(const ItemHolder&);
- ItemHolder& operator=(const ItemHolder&);
- T* item_;
-};
-
-
void writeString(const wchar_t* input, wchar_t* output, unsigned int outputBufferLen)
{
const size_t newSize = min(wcslen(input) + 1, outputBufferLen); //including null-termination
memcpy(output, input, newSize * sizeof(wchar_t));
- output[newSize-1] = 0; //if output buffer is too small...
-}
-
-
-std::wstring numberToHexString(const long number)
-{
- wchar_t result[100];
- swprintf(result, 100, L"0x%08x", number);
- return std::wstring(result);
+ output[newSize-1] = 0; //if output buffer is too small...
}
void writeErrorMsg(const wchar_t* input, HRESULT hr, wchar_t* output, unsigned int outputBufferLen)
{
- std::wstring formattedMsg(input);
- formattedMsg += L" (";
- formattedMsg += numberToHexString(hr);
- formattedMsg += L": ";
- formattedMsg += _com_error(hr).ErrorMessage();
- formattedMsg += L")";
-
- writeString(formattedMsg.c_str(), output, outputBufferLen);
+ writeString(Util::generateErrorMsg(input, hr).c_str(), output, outputBufferLen);
}
-bool Shadow::createShadowCopy(const wchar_t* volumeName,
+
+bool shadow::createShadowCopy(const wchar_t* volumeName,
wchar_t* shadowVolName,
unsigned int shadowBufferLen,
void** backupHandle,
wchar_t* errorMessage,
unsigned int errorBufferLen)
{
+ using Util::ComPtr;
+ using Util::generateErrorMsg;
+
//MessageBox(0, L"backup err", L"", 0); */
*backupHandle = NULL;
HRESULT hr = NULL;
- IVssBackupComponents* pBackupPtr = NULL;
- if (FAILED(hr = CreateVssBackupComponents(&pBackupPtr)))
+ ComPtr<IVssBackupComponents> backupComp;
+ if (FAILED(hr = CreateVssBackupComponents(backupComp.init())))
{
if (hr == E_ACCESSDENIED)
writeErrorMsg(L"The caller does not have sufficient backup privileges or is not an administrator.", hr, errorMessage, errorBufferLen);
@@ -111,97 +71,96 @@ bool Shadow::createShadowCopy(const wchar_t* volumeName,
return false;
}
- ItemHolder<IVssBackupComponents> pBackupComponents(pBackupPtr);
-
- if (FAILED(hr = pBackupComponents->InitializeForBackup()))
+ if (FAILED(hr = backupComp->InitializeForBackup()))
{
writeErrorMsg(L"Error calling \"InitializeForBackup\".", hr, errorMessage, errorBufferLen);
return false;
}
-
- if (FAILED(hr = pBackupComponents->SetBackupState(false, false, VSS_BT_FULL)))
+ if (FAILED(hr = backupComp->SetBackupState(false, false, VSS_BT_FULL)))
{
writeErrorMsg(L"Error calling \"SetBackupState\".", hr, errorMessage, errorBufferLen);
return false;
}
-
- IVssAsync* pWriteMetaData = NULL;
- if (FAILED(hr = pBackupComponents->GatherWriterMetadata( &pWriteMetaData )))
- { //this can happen if XP-version of VSS is used on Windows Vista (which needs at least VSS-Server2003 build)
+ ComPtr<IVssAsync> vssWriters;
+ if (FAILED(hr = backupComp->GatherWriterMetadata(vssWriters.init())))
+ {
+ //this can happen if XP-version of VSS is used on Windows Vista (which needs at least VSS-Server2003 build)
writeErrorMsg(L"Error calling \"GatherWriterMetadata\".", hr, errorMessage, errorBufferLen);
return false;
}
//wait for shadow copy writers to complete
- hr = pWriteMetaData->Wait();
- if (SUCCEEDED(hr))
- pWriteMetaData->QueryStatus(&hr, NULL); //check if the async operation succeeded...
-
- pWriteMetaData->Release();
- if (FAILED(hr))
+ if (FAILED(hr = vssWriters->Wait()))
{
- writeErrorMsg(L"Error calling \"ppWriteMetaData->Wait\".", hr, errorMessage, errorBufferLen);
+ writeErrorMsg(L"Error calling \"vssWriters->Wait\".", hr, errorMessage, errorBufferLen);
return false;
}
+ vssWriters->QueryStatus(&hr, NULL); //check if the async operation succeeded...
+ if (FAILED(hr))
+ {
+ writeErrorMsg(L"Error calling \"vssWriters->QueryStatus\".", hr, errorMessage, errorBufferLen);
+ return false;
+ }
VSS_ID snapshotSetId = {0};
- if (FAILED(hr = pBackupComponents->StartSnapshotSet( &snapshotSetId )))
+ if (FAILED(hr = backupComp->StartSnapshotSet(&snapshotSetId)))
{
writeErrorMsg(L"Error calling \"StartSnapshotSet\".", hr, errorMessage, errorBufferLen);
return false;
}
-
VSS_ID SnapShotId = {0};
- if (FAILED(hr = pBackupComponents->AddToSnapshotSet(const_cast<wchar_t*>(volumeName), GUID_NULL, &SnapShotId)))
+ if (FAILED(hr = backupComp->AddToSnapshotSet(const_cast<wchar_t*>(volumeName), GUID_NULL, &SnapShotId)))
{
writeErrorMsg(L"Error calling \"AddToSnapshotSet\".", hr, errorMessage, errorBufferLen);
return false;
}
-
- IVssAsync* pPrepare = NULL;
- if (FAILED(hr = pBackupComponents->PrepareForBackup( &pPrepare )))
+ ComPtr<IVssAsync> vssPrepare;
+ if (FAILED(hr = backupComp->PrepareForBackup(vssPrepare.init())))
{
writeErrorMsg(L"Error calling \"PrepareForBackup\".", hr, errorMessage, errorBufferLen);
return false;
}
- hr = pPrepare->Wait();
- if (SUCCEEDED(hr))
- pPrepare->QueryStatus(&hr, NULL); //check if the async operation succeeded...
+ if (FAILED(hr = vssPrepare->Wait()))
+ {
+ writeErrorMsg(L"Error calling \"vssPrepare->Wait\".", hr, errorMessage, errorBufferLen);
+ return false;
+ }
- pPrepare->Release();
+ vssPrepare->QueryStatus(&hr, NULL); //check if the async operation succeeded...
if (FAILED(hr))
{
- writeErrorMsg(L"Error calling \"pPrepare->Wait\".", hr, errorMessage, errorBufferLen);
+ writeErrorMsg(L"Error calling \"vssPrepare->QueryStatus\".", hr, errorMessage, errorBufferLen);
return false;
}
-
- IVssAsync* pDoShadowCopy = NULL;
- if (FAILED(hr = pBackupComponents->DoSnapshotSet(&pDoShadowCopy)))
+ ComPtr<IVssAsync> vssDoShadowCopy;
+ if (FAILED(hr = backupComp->DoSnapshotSet(vssDoShadowCopy.init())))
{
writeErrorMsg(L"Error calling \"DoSnapshotSet\".", hr, errorMessage, errorBufferLen);
return false;
}
- hr = pDoShadowCopy->Wait();
- if (SUCCEEDED(hr))
- pDoShadowCopy->QueryStatus(&hr, NULL); //check if the async operation succeeded...
+ if (FAILED(hr = vssDoShadowCopy->Wait()))
+ {
+ writeErrorMsg(L"Error calling \"vssDoShadowCopy->Wait\".", hr, errorMessage, errorBufferLen);
+ return false;
+ }
- pDoShadowCopy->Release();
+ vssDoShadowCopy->QueryStatus(&hr, NULL); //check if the async operation succeeded...
if (FAILED(hr))
{
- writeErrorMsg(L"Error calling \"pPrepare->Wait\".", hr, errorMessage, errorBufferLen);
+ writeErrorMsg(L"Error calling \"vssDoShadowCopy->QueryStatus\".", hr, errorMessage, errorBufferLen);
return false;
}
VSS_SNAPSHOT_PROP props;
- if (FAILED(hr = pBackupComponents->GetSnapshotProperties( SnapShotId, &props )))
+ if (FAILED(hr = backupComp->GetSnapshotProperties(SnapShotId, &props)))
{
writeErrorMsg(L"Error calling \"GetSnapshotProperties\".", hr, errorMessage, errorBufferLen);
return false;
@@ -212,13 +171,13 @@ bool Shadow::createShadowCopy(const wchar_t* volumeName,
VssFreeSnapshotProperties(&props);
- *backupHandle = pBackupComponents.release(); //release ownership
+ *backupHandle = backupComp.release(); //release ownership
return true;
}
-void Shadow::releaseShadowCopy(void* backupHandle)
+void shadow::releaseShadowCopy(void* backupHandle)
{
if (backupHandle != NULL)
static_cast<IVssBackupComponents*>(backupHandle)->Release();
bgstack15