]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/plugins/store_node.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / filed / plugins / store_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 store_node_t::store_node_t(char *name, node_t *parent_node) : node_t(name, NODE_TYPE_STORE, parent_node)
35 {
36    dbi = NULL;
37    hccx = NULL;
38    dbi_node = NULL;
39    file_node = NULL;
40 }
41
42 store_node_t::~store_node_t()
43 {
44    if (dbi_node != NULL)
45       delete dbi_node;
46
47    if (file_node != NULL)
48       delete file_node;
49 }
50
51 bRC
52 store_node_t::startBackupFile(exchange_fd_context_t *context, struct save_pkt *sp)
53 {
54    char *tmp;
55
56    _DebugMessage(100, "startBackupNode_STORE state = %d\n", state);
57
58    switch(state)
59    {
60    case 0:
61       stream_ptr = dbi->wszDatabaseStreams;
62       state = 1;
63       // fall through
64    case 1:
65       dbi_node = new dbi_node_t("DatabaseBackupInfo", this);
66       dbi_node->dbi = dbi;
67       context->current_node = dbi_node;
68       break;
69    case 2:
70       tmp = new char[wcslen(stream_ptr) + 1];
71       wcstombs(tmp, stream_ptr, wcslen(stream_ptr) + 1);
72       file_node = new file_node_t(tmp, this);
73       file_node->hccx = hccx;
74       file_node->filename = stream_ptr;
75       context->current_node = file_node;
76       break;
77    case 3:
78       if (context->job_level == 'F')
79       {
80          time_t now = time(NULL);
81          sp->fname = full_path;
82          sp->link = full_path;
83          sp->statp.st_mode = 0700 | S_IFDIR;
84          sp->statp.st_ctime = now;
85          sp->statp.st_mtime = now;
86          sp->statp.st_atime = now;
87          sp->statp.st_size = 0;
88          sp->type = FT_DIREND;
89       }
90       else
91       {
92          bfuncs->setBaculaValue(context->bpContext, bVarFileSeen, (void *)full_path);
93          return bRC_Seen;
94       }
95       break;
96    }
97
98    return bRC_OK;
99 }
100
101 bRC
102 store_node_t::endBackupFile(exchange_fd_context_t *context)
103 {
104    _DebugMessage(100, "endBackupNode_STORE state = %d\n", state);
105    bRC retval = bRC_OK;
106
107    switch(state)
108    {
109    case 0:
110       // should never happen
111       break;
112    case 1:
113       state = 2;
114       retval = bRC_More;
115       break;
116    case 2:
117       delete file_node;
118       stream_ptr += wcslen(stream_ptr) + 1;
119       if (*stream_ptr == 0)
120          state = 3;
121       retval = bRC_More;
122       break;
123    case 3:
124       //delete dbi_node;
125       context->current_node = parent;
126       break;
127    }
128    return retval;
129 }
130
131 bRC
132 store_node_t::createFile(exchange_fd_context_t *context, struct restore_pkt *rp)
133 {
134    _DebugMessage(100, "createFile_STORE state = %d\n", state);
135
136    if (strcmp(context->path_bits[level - 1], parent->name) != 0)
137    {
138       _DebugMessage(100, "Different storage group - switching back to parent\n", state);
139       context->current_node = parent;
140       return bRC_OK;
141    }
142    for (;;)
143    {
144       switch (state)
145       {
146       case 0:
147          if (strcmp("DatabaseBackupInfo", context->path_bits[level + 1]) != 0)
148          {
149             _JobMessage(M_FATAL, "DatabaseBackupInfo file must exist and must be first in directory\n");
150             state = 999;
151             break;
152          }
153          dbi_node = new dbi_node_t(bstrdup(context->path_bits[level + 1]), this);
154          context->current_node = dbi_node;
155          return bRC_OK;
156       case 1:
157          if (strcmp(context->path_bits[level - 1], parent->name) != 0)
158          {
159             _JobMessage(M_ERROR, "Unexpected Storage Group Change\n");
160             state = 999;
161             break;
162          }
163
164          if (*stream_ptr != 0)
165          {
166             // verify that stream_ptr == context->path_bits[level + 1];
167             _DebugMessage(150, "stream_ptr = %S\n", stream_ptr);
168             _DebugMessage(150, "out_stream_ptr = %S\n", out_stream_ptr);
169             file_node = new file_node_t(bstrdup(context->path_bits[level + 1]), this);
170             file_node->hccx = hccx;
171             file_node->filename = out_stream_ptr;
172             context->current_node = file_node;
173             return bRC_OK;
174          }
175          else
176          {
177             _JobMessage(M_ERROR, "Extra file found '%s'\n", full_path);
178             state = 999;
179             break;
180          }
181       case 2:
182          if (rp->type != FT_DIREND)
183          {
184             _JobMessage(M_ERROR, "Unexpected file '%s'\n", full_path);
185             state = 999;
186             break;
187          }
188          rp->create_status = CF_CREATED;
189          return bRC_OK;
190       case 999:
191          if (strcmp(context->path_bits[level], name) != 0)
192          {
193             _DebugMessage(100, "End of Store when in error state - switching back to parent\n", state);
194             context->current_node = parent;
195             return bRC_OK;
196          }
197          rp->create_status = CF_CREATED;
198          return bRC_OK;
199       }
200    }
201 }
202
203 bRC
204 store_node_t::endRestoreFile(exchange_fd_context_t *context)
205 {
206    HRESULT result;
207
208    _DebugMessage(100, "endRestoreFile_STORE state = %d\n", state);
209    for (;;)
210    {
211       switch (state)
212       {
213       case 0:
214          state = 1;
215          _DebugMessage(100, "Calling HrESERestoreAddDatabase\n");
216          result = HrESERestoreAddDatabase(hccx, dbi_node->restore_display_name, dbi_node->restore_guid, dbi_node->restore_input_streams, &dbi_node->restore_output_streams);
217          if (result != 0)
218          {
219             _JobMessage(M_FATAL, "HrESERestoreAddDatabase failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
220             state = 999;
221             break;
222          }
223          stream_ptr = dbi_node->restore_input_streams;
224          out_stream_ptr = dbi_node->restore_output_streams;
225          return bRC_OK;
226       case 1:
227          if (*stream_ptr != 0)
228          {
229             delete file_node;
230             file_node = NULL;
231             stream_ptr += wcslen(stream_ptr) + 1;
232             out_stream_ptr += wcslen(out_stream_ptr) + 1;
233             if (*stream_ptr == 0)
234                state = 2;
235             return bRC_OK;
236          }
237          else
238          {
239             state = 999;
240             break;
241          }
242       case 2:
243          context->current_node = parent;
244          return bRC_OK;
245       case 999:
246          return bRC_OK;
247       }
248    }
249 }