2 ** NetXMS - Network Management System
4 ** Copyright (C) 2003-2017 Victor Kirhenshtein
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define _WIN32_WINNT 0x0600
31 #define DEBUG_TAG _T("logwatch.vss")
34 * Helper function to display error and return failure in FileSnapshot::create
36 inline FileSnapshot
*CreateFailure(HRESULT hr
, IVssBackupComponents
*bc
, const TCHAR
*format
)
39 nxlog_debug_tag(DEBUG_TAG
, 3, format
, err
.ErrorMessage(), hr
);
46 * Create file snapshot using VSS
48 FileSnapshot
*FileSnapshot
::create(const TCHAR
*path
)
50 IVssBackupComponents
*bc
;
51 HRESULT hr
= CreateVssBackupComponents(&bc
);
53 return CreateFailure(hr
, NULL
, _T("Call to CreateVssBackupComponents failed (%s) HRESULT=0x%08X"));
55 hr
= bc
->InitializeForBackup();
57 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::InitializeForBackup failed (%s) HRESULT=0x%08X"));
59 hr
= bc
->SetBackupState(false, false, VSS_BT_COPY
);
61 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::SetBackupState failed (%s) HRESULT=0x%08X"));
63 hr
= bc
->SetContext(VSS_CTX_FILE_SHARE_BACKUP
);
65 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::SetContext failed (%s) HRESULT=0x%08X"));
68 hr
= bc
->StartSnapshotSet(&snapshotSetId
);
70 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::StartSnapshotSet failed (%s) HRESULT=0x%08X"));
72 const TCHAR
*s
= _tcschr(path
, _T(':'));
74 return CreateFailure(S_OK
, bc
, _T("Unsupported file path format"));
77 size_t len
= s
- path
;
79 _tcslcpy(device
, path
, std
::min(static_cast<size_t>(64), len
+ 1));
80 _tcslcat(device
, _T("\\"), 64);
81 nxlog_debug_tag(DEBUG_TAG
, 7, _T("Adding device %s to VSS snapshot"), device
);
84 hr
= bc
->AddToSnapshotSet(device
, GUID_NULL
, &snapshotId
);
86 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::AddToSnapshotSet failed (%s) HRESULT=0x%08X"));
89 hr
= bc
->DoSnapshotSet(&async
);
91 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::DoSnapshotSet failed (%s) HRESULT=0x%08X"));
96 return CreateFailure(hr
, bc
, _T("Call to IVssAsync::Wait failed (%s) HRESULT=0x%08X"));
98 VSS_SNAPSHOT_PROP prop
;
99 hr
= bc
->GetSnapshotProperties(snapshotId
, &prop
);
101 return CreateFailure(hr
, bc
, _T("Call to IVssBackupComponents::GetSnapshotProperties failed (%s) HRESULT=0x%08X"));
103 nxlog_debug_tag(DEBUG_TAG
, 7, _T("Created VSS snapshot %s"), prop
.m_pwszSnapshotDeviceObject
);
104 String
sname(prop
.m_pwszSnapshotDeviceObject
);
107 FileSnapshot
*object
= new FileSnapshot();
108 object
->m_handle
= bc
;
109 object
->m_name
= _tcsdup(sname
);
114 * File snapshot object constructor
116 FileSnapshot
::FileSnapshot()
123 * File snapshot destructor
125 FileSnapshot
::~FileSnapshot()