From fbe76102e941b9f1edaf236788e42678f05fdf9a Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:08:06 +0200 Subject: 3.9 --- shared/ShadowCopy/Shadow_2003.vcxproj | 2 +- shared/ShadowCopy/Shadow_XP.vcxproj | 2 +- shared/ShadowCopy/dll_main.cpp | 25 ++++++ shared/ShadowCopy/dllmain.cpp | 27 ------- shared/ShadowCopy/shadow.cpp | 139 ++++++++++++---------------------- shared/ShadowCopy/shadow.h | 2 +- 6 files changed, 77 insertions(+), 120 deletions(-) create mode 100644 shared/ShadowCopy/dll_main.cpp delete mode 100644 shared/ShadowCopy/dllmain.cpp (limited to 'shared/ShadowCopy') diff --git a/shared/ShadowCopy/Shadow_2003.vcxproj b/shared/ShadowCopy/Shadow_2003.vcxproj index b7d31ae4..5351cc1d 100644 --- a/shared/ShadowCopy/Shadow_2003.vcxproj +++ b/shared/ShadowCopy/Shadow_2003.vcxproj @@ -210,7 +210,7 @@ - + false diff --git a/shared/ShadowCopy/Shadow_XP.vcxproj b/shared/ShadowCopy/Shadow_XP.vcxproj index 5c531a27..d096297a 100644 --- a/shared/ShadowCopy/Shadow_XP.vcxproj +++ b/shared/ShadowCopy/Shadow_XP.vcxproj @@ -211,7 +211,7 @@ - + false diff --git a/shared/ShadowCopy/dll_main.cpp b/shared/ShadowCopy/dll_main.cpp new file mode 100644 index 00000000..142e26a2 --- /dev/null +++ b/shared/ShadowCopy/dll_main.cpp @@ -0,0 +1,25 @@ +// ************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl.html * +// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) * +// ************************************************************************** +// + +#define WIN32_LEAN_AND_MEAN +#include + +//optional: add init/teardown logic here +BOOL APIENTRY DllMain(HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpvReserved) +{ + switch (fdwReason) + { + case DLL_PROCESS_ATTACH: + case DLL_PROCESS_DETACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + break; + } + return TRUE; +} diff --git a/shared/ShadowCopy/dllmain.cpp b/shared/ShadowCopy/dllmain.cpp deleted file mode 100644 index 8dae897a..00000000 --- a/shared/ShadowCopy/dllmain.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// ************************************************************************** -// * This file is part of the FreeFileSync project. It is distributed under * -// * GNU General Public License: http://www.gnu.org/licenses/gpl.html * -// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) * -// ************************************************************************** -// - -#define WIN32_LEAN_AND_MEAN -#include - - -BOOL APIENTRY DllMain( HMODULE hModule, - DWORD ul_reason_for_call, - LPVOID lpReserved - ) -{ - switch (ul_reason_for_call) - { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - break; - } - return TRUE; -} - 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 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 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 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 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(volumeName), GUID_NULL, &SnapShotId))) + if (FAILED(hr = backupComp->AddToSnapshotSet(const_cast(volumeName), GUID_NULL, &SnapShotId))) { writeErrorMsg(L"Error calling \"AddToSnapshotSet\".", hr, errorMessage, errorBufferLen); return false; } - - IVssAsync* pPrepare = NULL; - if (FAILED(hr = pBackupComponents->PrepareForBackup( &pPrepare ))) + ComPtr 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 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(backupHandle)->Release(); diff --git a/shared/ShadowCopy/shadow.h b/shared/ShadowCopy/shadow.h index 61d6ae7e..a9120e8a 100644 --- a/shared/ShadowCopy/shadow.h +++ b/shared/ShadowCopy/shadow.h @@ -14,7 +14,7 @@ #endif -namespace Shadow +namespace shadow { //COM needs to be initialized before calling any of these functions! CoInitializeEx/CoUninitialize -- cgit