Code pour fichiers verrouillées via VSS (Volume Shadow Copy)
Publié : ven. 14/févr./2014 11:24
Bonjour,
Je me remet un peu au Purebasic...
Je cherche à traduire ce code en Purebasic pour copier des fichiers verrouillées via VSS (Volume Shadow Copy)
J'ai du mal avec les Interfaces. Un peu d'aide serait le bienvenue.
Merci.
Je me remet un peu au Purebasic...
Je cherche à traduire ce code en Purebasic pour copier des fichiers verrouillées via VSS (Volume Shadow Copy)
J'ai du mal avec les Interfaces. Un peu d'aide serait le bienvenue.
Merci.
Code : Tout sélectionner
#include <stdio.h>
#include <tchar.h>
#include <shlwapi.h>
#include <vss.h>
#include <vswriter.h>
#include <vsbackup.h>
int _tmain(int argc, _TCHAR* argv[])
{
int returnValue = 1;
if (argc != 3)
{
printf("Usage: LockedFileCopy.exe sourcefile destinationfile\n");
return 1;
}
TCHAR sourceFile[_MAX_PATH] = {0};
LPTSTR destinationFile = argv[2];
//
// Create an absolute path if it is relative
//
_tcscpy(sourceFile, argv[1]);
if (PathIsRelative(sourceFile))
{
::PathFindFileName(sourceFile);
}
if (_taccess(sourceFile,0) != 0)
{
_tprintf(_T("\"%s\" does not exist!\n"), argv[1]);
return 1;
}
if (CoInitialize(NULL) != S_OK)
{
printf("CoInitialize failed!\n");
return 1;
}
//
// Create the IVssBackupComponents Interface
//
IVssBackupComponents *pBackup = NULL;
HRESULT result = CreateVssBackupComponents(&pBackup);
if (result == S_OK)
{
//
// Initialize for backup
//
result = pBackup->InitializeForBackup();</code>
if (result == S_OK)
{
//
// Prompts each writer to send the metadata they have collected
//
IVssAsync *pAsync = NULL;
result = pBackup->GatherWriterMetadata(&pAsync);
if (result == S_OK)
{
printf("Gathering metadata from writers...\n");
result = pAsync->Wait();
if (result == S_OK)
{
//
// Creates a new, empty shadow copy set
//
VSS_ID snapshotSetId;
result = pBackup->StartSnapshotSet(&snapshotSetId);
if (result == S_OK)
{
//
// Add Volume to the shadow copy set
//
WCHAR volumeName[4] = {sourceFile[0], _T(':'), _T("\\")};
wprintf(L"Adding %s volume to snapshot set...\n", volumeName);
result = pBackup->AddToSnapshotSet(volumeName, GUID_NULL, &snapshotSetId);
if (result == S_OK)
{
//
// Configure the backup operation for Copy with no backup history
//
result = pBackup->SetBackupState(false, false, VSS_BT_COPY);
if (result == S_OK)
{
//
// Make VSS generate a PrepareForBackup event
//
IVssAsync* pPrepare = NULL;
result = pBackup->PrepareForBackup(&pPrepare);
if (result == S_OK)
{
printf("Preparing for backup...\n");
result = pPrepare->Wait();
if (result == S_OK)
{
//
// Commit all snapshots in this set
//
IVssAsync* pDoShadowCopy = NULL;
result = pBackup->DoSnapshotSet(&pDoShadowCopy);
if (result == S_OK)
{
printf("Taking snapshots...\n");
result = pDoShadowCopy->Wait();
if (result == S_OK)
{
//
// Get the snapshot device object from the properties
//
VSS_SNAPSHOT_PROP snapshotProp = {0};
result = pBackup->GetSnapshotProperties(snapshotSetId, &snapshotProp);
if (result == S_OK)
{
//
// Create the source path by using the volume specified in
// m_pwszSnapshotDeviceObject and appending the path of
// the source file (minus the drive letter)
//
int sourceLength = wcslen(snapshotProp.m_pwszSnapshotDeviceObject) +
_tcslen(sourceFile);
LPTSTR sourceSnapshotFile = new TCHAR[sourceLength];
_tcscpy_s(sourceSnapshotFile, sourceLength,
snapshotProp.m_pwszSnapshotDeviceObject);
_tcscat_s(sourceSnapshotFile, sourceLength, &sourceFile[2]);</code>
//
// Copy the file from the snapshot device object
//
if (!::CopyFile(sourceSnapshotFile, destinationFile, FALSE))
{
DWORD lastError = GetLastError();
printf("Failed to copy file - Error: %d\n", lastError);
}
else
{
printf("Successfully copied file!\n");
returnValue = 0;
}
delete [] sourceSnapshotFile;</code>
//
// Cleanup properties
//
VssFreeSnapshotProperties(&snapshotProp);
}
}
pDoShadowCopy->Release();
}
}
pPrepare->Release();
}
}
}
//
// Delete the snapshot
//
LONG deletedSnapshots = 0;
VSS_ID nonDeletedSnapshotId;
result = pBackup->DeleteSnapshots(snapshotSetId, VSS_OBJECT_SNAPSHOT,
TRUE, &deletedSnapshots, &nonDeletedSnapshotId);
}
}
pAsync->Release();
}
}
pBackup->Release();
}
return returnValue;
}