]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
kes Complete hopefully the last of the copyright transfer changes.
[bacula/bacula] / bacula / src / stored / read.c
1 /*
2  * Read code for Storage daemon
3  *
4  *     Kern Sibbald, November MM
5  *
6  *   Version $Id$
7  */
8 /*
9    Bacula® - The Network Backup Solution
10
11    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
12
13    The main author of Bacula is Kern Sibbald, with contributions from
14    many others, a complete list can be found in the file AUTHORS.
15    This program is Free Software; you can redistribute it and/or
16    modify it under the terms of version two of the GNU General Public
17    License as published by the Free Software Foundation plus additions
18    that are listed in the file LICENSE.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28    02110-1301, USA.
29
30    Bacula® is a registered trademark of John Walker.
31    The licensor of Bacula is the Free Software Foundation Europe
32    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
33    Switzerland, email:ftf@fsfeurope.org.
34 */
35
36 #include "bacula.h"
37 #include "stored.h"
38
39 /* Forward referenced subroutines */
40 static bool record_cb(DCR *dcr, DEV_RECORD *rec);
41
42
43 /* Responses sent to the File daemon */
44 static char OK_data[]    = "3000 OK data\n";
45 static char FD_error[]   = "3000 error\n";
46 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
47
48 /*
49  *  Read Data and send to File Daemon
50  *   Returns: false on failure
51  *            true  on success
52  */
53 bool do_read_data(JCR *jcr)
54 {
55    BSOCK *fd = jcr->file_bsock;
56    bool ok = true;
57    DCR *dcr = jcr->read_dcr;
58
59    Dmsg0(20, "Start read data.\n");
60
61    if (!bnet_set_buffer_size(fd, dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
62       return false;
63    }
64
65
66    create_restore_volume_list(jcr);
67    if (jcr->NumReadVolumes == 0) {
68       Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
69       free_restore_volume_list(jcr);
70       bnet_fsend(fd, FD_error);
71       return false;
72    }
73
74    Dmsg2(200, "Found %d volumes names to restore. First=%s\n", jcr->NumReadVolumes,
75       jcr->VolList->VolumeName);
76
77    /* Ready device for reading */
78    if (!acquire_device_for_read(dcr)) {
79       free_restore_volume_list(jcr);
80       bnet_fsend(fd, FD_error);
81       return false;
82    }
83
84    /* Tell File daemon we will send data */
85    bnet_fsend(fd, OK_data);
86    ok = read_records(dcr, record_cb, mount_next_read_volume);
87
88    /* Send end of data to FD */
89    bnet_sig(fd, BNET_EOD);
90
91    if (!release_device(jcr->read_dcr)) {
92       ok = false;
93    }
94
95    free_restore_volume_list(jcr);
96    Dmsg0(30, "Done reading.\n");
97    return ok;
98 }
99
100 /*
101  * Called here for each record from read_records()
102  *  Returns: true if OK
103  *           false if error
104  */
105 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
106 {
107    JCR *jcr = dcr->jcr;
108    BSOCK *fd = jcr->file_bsock;
109    bool ok = true;
110    POOLMEM *save_msg;
111
112    if (rec->FileIndex < 0) {
113       return true;
114    }
115    Dmsg5(400, "Send to FD: SessId=%u SessTim=%u FI=%d Strm=%d, len=%d\n",
116       rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, rec->Stream,
117       rec->data_len);
118
119    /* Send record header to File daemon */
120    if (!bnet_fsend(fd, rec_header, rec->VolSessionId, rec->VolSessionTime,
121           rec->FileIndex, rec->Stream, rec->data_len)) {
122       Pmsg1(000, _(">filed: Error Hdr=%s\n"), fd->msg);
123       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
124          bnet_strerror(fd));
125       return false;
126    } else {
127       Dmsg1(400, ">filed: Hdr=%s\n", fd->msg);
128    }
129
130
131    /* Send data record to File daemon */
132    save_msg = fd->msg;          /* save fd message pointer */
133    fd->msg = rec->data;         /* pass data directly to bnet_send */
134    fd->msglen = rec->data_len;
135    Dmsg1(400, ">filed: send %d bytes data.\n", fd->msglen);
136    if (!bnet_send(fd)) {
137       Pmsg1(000, _("Error sending to FD. ERR=%s\n"), bnet_strerror(fd));
138       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
139          bnet_strerror(fd));
140
141       ok = false;
142    }
143    fd->msg = save_msg;                /* restore fd message pointer */
144    return ok;
145 }