-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackupSocket.cpp
91 lines (74 loc) · 1.97 KB
/
BackupSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include "BackupSocket.h"
#include <process.h>
#include <thread>
BackupSocket::BackupSocket(void)
: VDISocket("9001")
{
}
BackupSocket::~BackupSocket(void)
{
}
bool BackupSocket::CanStartOperation(void)
{
return true;
}
void BackupSocket::PerformOperation(std::string instanceName, std::string databaseName)
{
std::thread o(&BackupSocket::OpenDevice, this);
std::thread r(&BackupSocket::RunBackup, this, instanceName, databaseName);
std::thread w(&BackupSocket::TransferData, this);
o.join();
r.join();
w.join();
}
void BackupSocket::TransferData(void)
{
VDC_Command* cmd;
DWORD bytes;
HRESULT r;
while(device == NULL)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
while(SUCCEEDED(r=device->GetCommand(INFINITE, &cmd)))
{
switch(cmd->commandCode)
{
case VDC_Write:
send(luge, (const char*)cmd->buffer, cmd->size, 0);
bytes = cmd->size;
device->CompleteCommand(cmd, ERROR_SUCCESS, bytes, 0);
printf("Transferred %u bytes\n", bytes);
break;
case VDC_Flush:
//we're done here.
device->CompleteCommand(cmd, ERROR_SUCCESS, bytes, 0);
break;
default:
device->CompleteCommand(cmd, ERROR_NOT_SUPPORTED, 0, 0);
}
}
// shutdown the connection since we're done
r = shutdown(luge, SD_SEND);
if (r == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(luge);
WSACleanup();
}
}
void BackupSocket::RunBackup(std::string instanceName, std::string databaseName)
{
char sqlCommand[1024];
sprintf_s(sqlCommand, 1024, "-Q \"BACKUP DATABASE pubs TO VIRTUAL_DEVICE='%ls'\"", deviceName);
int rc;
printf ("running osql to execute: %s\n", sqlCommand);
// this is probably going to be gross if the osql call fails...
rc = _spawnlp( _P_WAIT, "osql", "osql", "-E", "-b", sqlCommand, NULL);
if (rc == -1)
{
printf ("Spawn failed with error: %d\n", errno);
}
}