]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/plugins/service_node.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / filed / plugins / service_node.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2008-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation, which is 
11    listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /* 
29  *  Written by James Harper, October 2008
30  */
31
32 #include "exchange-fd.h"
33
34 service_node_t::service_node_t(char *name, node_t *parent_node) : node_t(name, NODE_TYPE_SERVICE, parent_node)
35 {
36    current_ibi = 0;
37    hccx = NULL;
38    ibi = NULL;
39    ibi_count = 0;
40    first_storage_group_node = NULL;
41 }
42
43 service_node_t::~service_node_t()
44 {
45 }
46
47 bRC
48 service_node_t::startBackupFile(exchange_fd_context_t *context, struct save_pkt *sp)
49 {
50    HRESULT result;
51    char aname[256];
52
53    _DebugMessage(100, "startBackupNode_SERVICE state = %d\n", state);
54    switch(state)
55    {
56    case 0:
57       if (strcmp(PLUGIN_PATH_PREFIX_SERVICE, name) != 0)
58       {
59          _JobMessage(M_FATAL, "Invalid restore path specified, must start with /" PLUGIN_PATH_PREFIX_BASE "/" PLUGIN_PATH_PREFIX_SERVICE "/\n");
60          return bRC_Error;
61       }
62       // convert name to a wide string
63
64       _DebugMessage(100, "Calling HrESEBackupPrepare\n");
65       wcstombs(aname, context->computer_name, 256);
66       _JobMessage(M_INFO, "Preparing Exchange Backup for %s\n", aname);
67       result = HrESEBackupPrepare(context->computer_name, PLUGIN_PATH_PREFIX_SERVICE_W, &ibi_count, &ibi, &hccx);
68       if (result != 0)
69       {
70          _JobMessage(M_FATAL, "HrESEBackupPrepare failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
71          return bRC_Error;
72       }
73       state = 1;
74       // fall through
75    case 1:
76       if (context->path_bits[level + 1] == NULL)
77       {
78          _DebugMessage(100, "No specific storage group specified - backing them all up\n");
79          char *tmp = new char[wcslen(ibi[current_ibi].wszInstanceName) + 1];
80          wcstombs(tmp, ibi[current_ibi].wszInstanceName, wcslen(ibi[current_ibi].wszInstanceName) + 1);
81          first_storage_group_node = new storage_group_node_t(tmp, this);
82          delete tmp;
83          _DebugMessage(100, "storage group name = %s\n", first_storage_group_node->name);
84          first_storage_group_node->ibi = &ibi[current_ibi];
85          first_storage_group_node->hccx = hccx;
86          context->current_node = first_storage_group_node;
87       }
88       else
89       {
90          char *tmp = NULL;
91          for (current_ibi = 0; current_ibi < ibi_count; current_ibi++)
92          {
93             tmp = new char[wcslen(ibi[current_ibi].wszInstanceName) + 1];
94             wcstombs(tmp, ibi[current_ibi].wszInstanceName, wcslen(ibi[current_ibi].wszInstanceName) + 1);
95             if (stricmp(tmp, context->path_bits[level + 1]) == 0)
96                break;
97          }
98          first_storage_group_node = new storage_group_node_t(tmp, this);
99          delete tmp;
100          if (current_ibi == ibi_count)
101          {
102             _JobMessage(M_FATAL, "Invalid Storage Group '%s'\n", context->path_bits[level + 1]);
103             return bRC_Error;
104          }
105          _DebugMessage(100, "storage group name = %s\n", first_storage_group_node->name);
106          first_storage_group_node->ibi = &ibi[current_ibi];
107          first_storage_group_node->hccx = hccx;
108          context->current_node = first_storage_group_node;
109       }
110       break;
111    case 2:
112       time_t now = time(NULL);
113       sp->fname = full_path;
114       sp->link = full_path;
115       sp->statp.st_mode = 0700 | S_IFDIR;
116       sp->statp.st_ctime = now;
117       sp->statp.st_mtime = now;
118       sp->statp.st_atime = now;
119       sp->statp.st_size = 0;
120       sp->statp.st_nlink = 1;
121       //sp->statp.st_blocks = 0;
122       sp->type = FT_DIREND;
123       break;
124    }
125    _DebugMessage(100, "ending startBackupNode_SERVICE state = %d\n", state);
126    return bRC_OK;
127 }
128
129 bRC
130 service_node_t::endBackupFile(exchange_fd_context_t *context)
131 {
132    HRESULT result;
133    bRC retval = bRC_OK;
134
135    _DebugMessage(100, "endBackupNode_SERVICE state = %d\n", state);
136    switch(state)
137    {
138    case 0:
139       // should never happen
140       break;
141    case 1:
142       // free node->storage_group_node
143       if (context->path_bits[level + 1] == NULL)
144       {
145          current_ibi++;
146          if (current_ibi == ibi_count)
147             state = 2;
148       }
149       else
150          state = 2;
151       retval = bRC_More;
152       break;
153    case 2:
154       _DebugMessage(100, "calling HrESEBackupEnd\n");
155       result = HrESEBackupEnd(hccx);
156       if (result != 0)
157       {
158          _JobMessage(M_FATAL, "HrESEBackupEnd failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
159          return bRC_Error;
160       }
161
162       context->current_node = parent;
163       retval = bRC_OK;
164       break;
165    }
166    return retval;
167 }
168
169 bRC
170 service_node_t::createFile(exchange_fd_context_t *context, struct restore_pkt *rp)
171 {
172    storage_group_node_t *curr_sg, *prev_sg;
173
174    _DebugMessage(100, "createFile_SERVICE state = %d\n", state);
175    if (strcmp(name, "Microsoft Information Store") != 0)
176    {
177       _JobMessage(M_FATAL, "Invalid restore path specified, must start with '/" PLUGIN_PATH_PREFIX_BASE "/" PLUGIN_PATH_PREFIX_SERVICE "/'\n", state);
178       return bRC_Error;
179    }
180    for(;;)
181    {
182       switch (state)
183       {
184       case 0:
185          if (context->path_bits[level + 1] == NULL)
186          {
187             state = 1;
188             break;
189          }
190          for (prev_sg = NULL, curr_sg = first_storage_group_node; curr_sg != NULL; prev_sg = curr_sg, curr_sg = curr_sg->next)
191          {
192             if (strcmp(curr_sg->name, context->path_bits[level + 1]) == 0)
193             {
194                break;
195             }
196          }
197          if (curr_sg == NULL)
198          {
199             curr_sg = new storage_group_node_t(bstrdup(context->path_bits[level + 1]), this);
200             if (prev_sg == NULL)
201                first_storage_group_node = curr_sg;
202             else
203                prev_sg->next = curr_sg;
204          }
205          context->current_node = curr_sg;
206          return bRC_OK;
207       case 1:
208          rp->create_status = CF_CREATED;
209          return bRC_OK;
210       }
211    }
212    return bRC_Error;
213 }
214
215 bRC
216 service_node_t::endRestoreFile(exchange_fd_context_t *context)
217 {
218    _DebugMessage(100, "endRestoreFile_SERVICE state = %d\n", state);
219    switch(state)
220    {
221    case 0:
222       return bRC_Error;
223    case 1:
224       context->current_node = parent;
225       return bRC_OK;
226    }
227
228    return bRC_Error;
229 }