]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/filed/plugins/file_node.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / filed / plugins / file_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 file_node_t::file_node_t(char *name, node_t *parent_node) : node_t(name, NODE_TYPE_FILE, parent_node)
35 {
36    backup_file_handle = INVALID_HANDLE_VALUE;
37    restore_file_handle = INVALID_HANDLE_VALUE;
38    restore_at_file_level = FALSE;
39 }
40
41 file_node_t::~file_node_t()
42 {
43    if (backup_file_handle != INVALID_HANDLE_VALUE)
44    {
45       //_DebugMessage(100, "closing file handle in destructor\n");
46       CloseHandle(backup_file_handle);
47    }
48    if (restore_file_handle != INVALID_HANDLE_VALUE)
49    {
50       //_DebugMessage(100, "closing file handle in destructor\n");
51       if (restore_at_file_level)
52       {
53          CloseHandle(restore_file_handle);
54       }
55       else
56       {
57          // maybe one day
58       }
59    }
60 }
61
62 bRC
63 file_node_t::startBackupFile(exchange_fd_context_t *context, struct save_pkt *sp)
64 {
65    time_t now = time(NULL);
66    _DebugMessage(100, "startBackupNode_FILE state = %d\n", state);
67
68    if (context->job_level == 'F' || parent->type == NODE_TYPE_STORAGE_GROUP) {
69       sp->fname = full_path;
70       sp->link = full_path;
71       _DebugMessage(100, "fname = %s\n", sp->fname);
72       sp->statp.st_mode = 0700 | S_IFREG;
73       sp->statp.st_ctime = now;
74       sp->statp.st_mtime = now;
75       sp->statp.st_atime = now;
76       sp->statp.st_size = (uint64_t)-1;
77       sp->type = FT_REG;
78       return bRC_OK;
79    } else {
80       bfuncs->setBaculaValue(context->bpContext, bVarFileSeen, (void *)full_path);
81       return bRC_Seen;
82    }
83 }
84
85 bRC
86 file_node_t::endBackupFile(exchange_fd_context_t *context)
87 {
88    _DebugMessage(100, "endBackupNode_FILE state = %d\n", state);
89
90    context->current_node = parent;
91
92    return bRC_OK;
93 }
94
95 bRC
96 file_node_t::createFile(exchange_fd_context_t *context, struct restore_pkt *rp)
97 {
98    //HrESERestoreOpenFile with name of log file
99
100    _DebugMessage(100, "createFile_FILE state = %d\n", state);
101    rp->create_status = CF_EXTRACT;
102    return bRC_OK;
103 }
104
105 bRC
106 file_node_t::endRestoreFile(exchange_fd_context_t *context)
107 {
108    _DebugMessage(100, "endRestoreFile_FILE state = %d\n", state);
109    context->current_node = parent;
110    return bRC_OK;
111 }
112
113 bRC
114 file_node_t::pluginIoOpen(exchange_fd_context_t *context, struct io_pkt *io)
115 {
116    HRESULT result;
117    HANDLE handle;
118    char *tmp = new char[wcslen(filename) + 1];
119    wcstombs(tmp, filename, wcslen(filename) + 1);
120
121    _DebugMessage(100, "pluginIoOpen_FILE - filename = %s\n", tmp);
122    io->status = 0;
123    io->io_errno = 0;
124    if (context->job_type == JOB_TYPE_BACKUP)
125    {
126       _DebugMessage(10, "Calling HrESEBackupOpenFile\n");
127       result = HrESEBackupOpenFile(hccx, filename, 65535, 1, &backup_file_handle, &section_size);
128       if (result)
129       {
130          _JobMessage(M_FATAL, "HrESEBackupOpenFile failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
131          backup_file_handle = INVALID_HANDLE_VALUE;
132          io->io_errno = 1;
133          return bRC_Error;
134       }
135    }
136    else
137    {
138       _DebugMessage(10, "Calling HrESERestoreOpenFile for '%s'\n", tmp);
139       result = HrESERestoreOpenFile(hccx, filename, 1, &restore_file_handle);
140       if (result == hrRestoreAtFileLevel)
141       {
142          restore_at_file_level = true;
143          _DebugMessage(100, "Calling CreateFileW for '%s'\n", tmp);
144          handle = CreateFileW(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
145          if (handle == INVALID_HANDLE_VALUE)
146          {
147             _JobMessage(M_FATAL, "CreateFile failed");
148             return bRC_Error;
149          }
150          restore_file_handle = (void *)handle;
151          return bRC_OK;
152       }
153       else if (result == 0)
154       {
155          _JobMessage(M_FATAL, "Exchange File IO API not yet supported for restore\n");
156          restore_at_file_level = false;
157          return bRC_Error;
158       }
159       else
160       {
161          _JobMessage(M_FATAL, "HrESERestoreOpenFile failed with error 0x%08x - %s\n", result, ESEErrorMessage(result));
162          return bRC_Error;
163       }
164    }
165    return bRC_OK;
166 }
167
168 bRC
169 file_node_t::pluginIoRead(exchange_fd_context_t *context, struct io_pkt *io)
170 {
171    HRESULT result;
172    uint32_t readLength;
173
174    io->status = 0;
175    io->io_errno = 0;
176    _DebugMessage(200, "Calling HrESEBackupReadFile\n");
177    result = HrESEBackupReadFile(hccx, backup_file_handle, io->buf, io->count, &readLength);
178    if (result)
179    {
180       io->io_errno = 1;
181       return bRC_Error;
182    }
183    io->status = readLength;
184    size += readLength;
185    return bRC_OK;
186 }
187
188 bRC
189 file_node_t::pluginIoWrite(exchange_fd_context_t *context, struct io_pkt *io)
190 {
191    DWORD bytes_written;
192
193    io->io_errno = 0;
194    if (!restore_at_file_level)
195       return bRC_Error;
196
197    if (!WriteFile(restore_file_handle, io->buf, io->count, &bytes_written, NULL))
198    {
199       _JobMessage(M_FATAL, "Write Error");
200       return bRC_Error;
201    }
202
203    if (bytes_written != (DWORD)io->count)
204    {
205       _JobMessage(M_FATAL, "Short write");
206       return bRC_Error;
207    }
208    io->status = bytes_written;
209    
210    return bRC_OK;
211 }
212
213 bRC
214 file_node_t::pluginIoClose(exchange_fd_context_t *context, struct io_pkt *io)
215 {
216    if (context->job_type == JOB_TYPE_BACKUP)
217    {
218       _DebugMessage(100, "Calling HrESEBackupCloseFile\n");
219       HrESEBackupCloseFile(hccx, backup_file_handle);
220       backup_file_handle = INVALID_HANDLE_VALUE;
221       return bRC_OK;
222    }
223    else
224    {
225       if (restore_at_file_level)
226       {
227          CloseHandle(restore_file_handle);
228          restore_file_handle = INVALID_HANDLE_VALUE;
229          return bRC_OK;
230       }
231       else
232       {
233          return bRC_OK;
234       }
235    }
236 }