]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
bacula-web: Changed header.tpl design and links
[bacula/bacula] / bacula / src / stored / read.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-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 and included
11    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  * Read code for Storage daemon
30  *
31  *     Kern Sibbald, November MM
32  *
33  */
34
35 #include "bacula.h"
36 #include "stored.h"
37
38 /* Forward referenced subroutines */
39 static bool record_cb(DCR *dcr, DEV_RECORD *rec);
40
41
42 /* Responses sent to the File daemon */
43 static char OK_data[]    = "3000 OK data\n";
44 static char FD_error[]   = "3000 error\n";
45 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
46
47 /*
48  *  Read Data and send to File Daemon
49  *   Returns: false on failure
50  *            true  on success
51  */
52 bool do_read_data(JCR *jcr)
53 {
54    BSOCK *fd = jcr->file_bsock;
55    bool ok = true;
56    DCR *dcr = jcr->read_dcr;
57
58    Dmsg0(20, "Start read data.\n");
59
60    if (!bnet_set_buffer_size(fd, dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
61       return false;
62    }
63
64    if (jcr->NumReadVolumes == 0) {
65       Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
66       fd->fsend(FD_error);
67       return false;
68    }
69
70    Dmsg2(200, "Found %d volumes names to restore. First=%s\n", jcr->NumReadVolumes,
71       jcr->VolList->VolumeName);
72
73    /* Ready device for reading */
74    if (!acquire_device_for_read(dcr)) {
75       fd->fsend(FD_error);
76       return false;
77    }
78
79    /* Tell File daemon we will send data */
80    fd->fsend(OK_data);
81    set_jcr_job_status(jcr, JS_Running);
82    dir_send_job_status(jcr);
83    ok = read_records(dcr, record_cb, mount_next_read_volume);
84
85    /* Send end of data to FD */
86    fd->signal(BNET_EOD);
87
88    if (!release_device(jcr->read_dcr)) {
89       ok = false;
90    }
91
92    Dmsg0(30, "Done reading.\n");
93    return ok;
94 }
95
96 /*
97  * Called here for each record from read_records()
98  *  Returns: true if OK
99  *           false if error
100  */
101 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
102 {
103    JCR *jcr = dcr->jcr;
104    BSOCK *fd = jcr->file_bsock;
105    bool ok = true;
106    POOLMEM *save_msg;
107    char ec1[50], ec2[50];
108
109    if (rec->FileIndex < 0) {
110       return true;
111    }
112    Dmsg5(400, "Send to FD: SessId=%u SessTim=%u FI=%s Strm=%s, len=%d\n",
113       rec->VolSessionId, rec->VolSessionTime, 
114       FI_to_ascii(ec1, rec->FileIndex),
115       stream_to_ascii(ec2, rec->Stream, rec->FileIndex),
116       rec->data_len);
117
118    /* Send record header to File daemon */
119    if (!fd->fsend(rec_header, rec->VolSessionId, rec->VolSessionTime,
120           rec->FileIndex, rec->Stream, rec->data_len)) {
121       Pmsg1(000, _(">filed: Error Hdr=%s\n"), fd->msg);
122       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
123          fd->bstrerror());
124       return false;
125    } else {
126       Dmsg1(400, ">filed: Hdr=%s\n", fd->msg);
127    }
128
129
130    /* Send data record to File daemon */
131    save_msg = fd->msg;          /* save fd message pointer */
132    fd->msg = rec->data;         /* pass data directly to the FD */
133    fd->msglen = rec->data_len;
134    Dmsg1(400, ">filed: send %d bytes data.\n", fd->msglen);
135    if (!fd->send()) {
136       Pmsg1(000, _("Error sending to FD. ERR=%s\n"), fd->bstrerror());
137       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
138          fd->bstrerror());
139
140       ok = false;
141    }
142    fd->msg = save_msg;                /* restore fd message pointer */
143    return ok;
144 }