]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/plugins/exch_service_node.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / filed / plugins / exch_service_node.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2018 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /* 
20  *  Written by James Harper, July 2010
21  *  
22  *  Used only in "old Exchange plugin" now deprecated.
23  */
24
25 #include "exchange-fd.h"
26
27 service_node_t::service_node_t(char *name, node_t *parent_node) : node_t(name, NODE_TYPE_SERVICE, parent_node)
28 {
29    current_ibi = 0;
30    hccx = NULL;
31    ibi = NULL;
32    ibi_count = 0;
33    first_storage_group_node = NULL;
34 }
35
36 service_node_t::~service_node_t()
37 {
38 }
39
40 bRC
41 service_node_t::startBackupFile(exchange_fd_context_t *context, struct save_pkt *sp)
42 {
43    HRESULT result;
44    char aname[256];
45
46    _DebugMessage(100, "startBackupNode_SERVICE state = %d\n", state);
47    switch(state)
48    {
49    case 0:
50       if (strcmp(PLUGIN_PATH_PREFIX_SERVICE, name) != 0)
51       {
52          _JobMessage(M_FATAL, "Invalid restore path specified, must start with /" PLUGIN_PATH_PREFIX_BASE "/" PLUGIN_PATH_PREFIX_SERVICE "/\n");
53          return bRC_Error;
54       }
55       // convert name to a wide string
56
57       _DebugMessage(100, "Calling HrESEBackupPrepare\n");
58       wcstombs(aname, context->computer_name, 256);
59       _JobMessage(M_INFO, "Preparing Exchange Backup for %s\n", aname);
60       result = HrESEBackupPrepare(context->computer_name, 
61           (WCHAR *)PLUGIN_PATH_PREFIX_SERVICE_W, &ibi_count, &ibi, &hccx);
62       if (result != 0)
63       {
64          _JobMessage(M_FATAL, "HrESEBackupPrepare failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
65          return bRC_Error;
66       }
67       state = 1;
68       // fall through
69    case 1:
70       if (context->path_bits[level + 1] == NULL)
71       {
72          _DebugMessage(100, "No specific storage group specified - backing them all up\n");
73          char *tmp = new char[wcslen(ibi[current_ibi].wszInstanceName) + 1];
74          wcstombs(tmp, ibi[current_ibi].wszInstanceName, wcslen(ibi[current_ibi].wszInstanceName) + 1);
75          first_storage_group_node = new storage_group_node_t(tmp, this);
76          delete tmp;
77          _DebugMessage(100, "storage group name = %s\n", first_storage_group_node->name);
78          first_storage_group_node->ibi = &ibi[current_ibi];
79          first_storage_group_node->hccx = hccx;
80          context->current_node = first_storage_group_node;
81       }
82       else
83       {
84          char *tmp = NULL;
85          for (current_ibi = 0; current_ibi < ibi_count; current_ibi++)
86          {
87             tmp = new char[wcslen(ibi[current_ibi].wszInstanceName) + 1];
88             wcstombs(tmp, ibi[current_ibi].wszInstanceName, wcslen(ibi[current_ibi].wszInstanceName) + 1);
89             if (stricmp(tmp, context->path_bits[level + 1]) == 0)
90                break;
91          }
92          first_storage_group_node = new storage_group_node_t(tmp, this);
93          delete tmp;
94          if (current_ibi == ibi_count)
95          {
96             _JobMessage(M_FATAL, "Invalid Storage Group '%s'\n", context->path_bits[level + 1]);
97             return bRC_Error;
98          }
99          _DebugMessage(100, "storage group name = %s\n", first_storage_group_node->name);
100          first_storage_group_node->ibi = &ibi[current_ibi];
101          first_storage_group_node->hccx = hccx;
102          context->current_node = first_storage_group_node;
103       }
104       break;
105    case 2:
106       time_t now = time(NULL);
107       sp->fname = full_path;
108       sp->link = full_path;
109       sp->statp.st_mode = 0700 | S_IFDIR;
110       sp->statp.st_ctime = now;
111       sp->statp.st_mtime = now;
112       sp->statp.st_atime = now;
113       sp->statp.st_size = 0;
114       sp->statp.st_nlink = 1;
115       //sp->statp.st_blocks = 0;
116       sp->type = FT_DIREND;
117       break;
118    }
119    _DebugMessage(100, "ending startBackupNode_SERVICE state = %d\n", state);
120    return bRC_OK;
121 }
122
123 bRC
124 service_node_t::endBackupFile(exchange_fd_context_t *context)
125 {
126    HRESULT result;
127    bRC retval = bRC_OK;
128
129    _DebugMessage(100, "endBackupNode_SERVICE state = %d\n", state);
130    switch(state)
131    {
132    case 0:
133       // should never happen
134       break;
135    case 1:
136       // free node->storage_group_node
137       if (context->path_bits[level + 1] == NULL)
138       {
139          current_ibi++;
140          if (current_ibi == ibi_count)
141             state = 2;
142       }
143       else
144          state = 2;
145       retval = bRC_More;
146       break;
147    case 2:
148       _DebugMessage(100, "calling HrESEBackupEnd\n");
149       result = HrESEBackupEnd(hccx);
150       if (result != 0)
151       {
152          _JobMessage(M_FATAL, "HrESEBackupEnd failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
153          return bRC_Error;
154       }
155
156       context->current_node = parent;
157       retval = bRC_OK;
158       break;
159    }
160    return retval;
161 }
162
163 bRC
164 service_node_t::createFile(exchange_fd_context_t *context, struct restore_pkt *rp)
165 {
166    storage_group_node_t *curr_sg, *prev_sg;
167
168    _DebugMessage(0, "createFile_SERVICE state = %d\n", state);
169    if (strcmp(name, "Microsoft Information Store") != 0)
170    {
171       _JobMessage(M_FATAL, "Invalid restore path specified, must start with '/" PLUGIN_PATH_PREFIX_BASE "/" PLUGIN_PATH_PREFIX_SERVICE "/'\n", state);
172       return bRC_Error;
173    }
174    for(;;)
175    {
176       switch (state)
177       {
178       case 0:
179          if (context->path_bits[level + 1] == NULL)
180          {
181             state = 1;
182             break;
183          }
184          for (prev_sg = NULL, curr_sg = first_storage_group_node; curr_sg != NULL; prev_sg = curr_sg, curr_sg = curr_sg->next)
185          {
186             if (strcmp(curr_sg->name, context->path_bits[level + 1]) == 0)
187             {
188                break;
189             }
190          }
191          if (curr_sg == NULL)
192          {
193             curr_sg = new storage_group_node_t(bstrdup(context->path_bits[level + 1]), this);
194             if (prev_sg == NULL)
195                first_storage_group_node = curr_sg;
196             else
197                prev_sg->next = curr_sg;
198          }
199          context->current_node = curr_sg;
200          return bRC_OK;
201       case 1:
202          rp->create_status = CF_CREATED;
203          return bRC_OK;
204       }
205    }
206    return bRC_Error;
207 }
208
209 bRC
210 service_node_t::endRestoreFile(exchange_fd_context_t *context)
211 {
212    _DebugMessage(0, "endRestoreFile_SERVICE state = %d\n", state);
213    switch(state)
214    {
215    case 0:
216       return bRC_Error;
217    case 1:
218       context->current_node = parent;
219       return bRC_OK;
220    }
221
222    return bRC_Error;
223 }