]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
Vacation work -- see tech log
[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    Copyright (C) 2000-2005 Kern Sibbald
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "stored.h"
30
31 /* Forward referenced subroutines */
32 static bool record_cb(DCR *dcr, DEV_RECORD *rec);
33
34
35 /* Responses sent to the File daemon */
36 static char OK_data[]    = "3000 OK data\n";
37 static char FD_error[]   = "3000 error\n";
38 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
39
40 /*
41  *  Read Data and send to File Daemon
42  *   Returns: false on failure
43  *            true  on success
44  */
45 bool do_read_data(JCR *jcr)
46 {
47    BSOCK *fd = jcr->file_bsock;
48    bool ok = true;
49    DCR *dcr = jcr->dcr;
50
51    Dmsg0(20, "Start read data.\n");
52
53    if (!bnet_set_buffer_size(fd, dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
54       return false;
55    }
56
57
58    create_vol_list(jcr);
59    if (jcr->NumVolumes == 0) {
60       Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
61       free_vol_list(jcr);
62       bnet_fsend(fd, FD_error);
63       return false;
64    }
65
66    Dmsg2(200, "Found %d volumes names to restore. First=%s\n", jcr->NumVolumes,
67       jcr->VolList->VolumeName);
68
69    /* Ready device for reading */
70    if (!acquire_device_for_read(jcr, dcr->dev)) {
71       free_vol_list(jcr);
72       bnet_fsend(fd, FD_error);
73       return false;
74    }
75
76    /* Tell File daemon we will send data */
77    bnet_fsend(fd, OK_data);
78    ok = read_records(dcr, record_cb, mount_next_read_volume);
79
80    /* Send end of data to FD */
81    bnet_sig(fd, BNET_EOD);
82
83    if (!release_device(jcr)) {
84       ok = false;
85    }
86
87    free_vol_list(jcr);
88    Dmsg0(30, "Done reading.\n");
89    return ok;
90 }
91
92 /*
93  * Called here for each record from read_records()
94  *  Returns: true if OK
95  *           false if error
96  */
97 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
98 {
99    JCR *jcr = dcr->jcr;
100    BSOCK *fd = jcr->file_bsock;
101    bool ok = true;
102    POOLMEM *save_msg;
103
104    if (rec->FileIndex < 0) {
105       return true;
106    }
107    Dmsg5(100, "Send to FD: SessId=%u SessTim=%u FI=%d Strm=%d, len=%d\n",
108       rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, rec->Stream,
109       rec->data_len);
110
111    /* Send record header to File daemon */
112    if (!bnet_fsend(fd, rec_header, rec->VolSessionId, rec->VolSessionTime,
113           rec->FileIndex, rec->Stream, rec->data_len)) {
114       Dmsg1(30, ">filed: Error Hdr=%s\n", fd->msg);
115       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
116          bnet_strerror(fd));
117       return false;
118    } else {
119       Dmsg1(31, ">filed: Hdr=%s\n", fd->msg);
120    }
121
122
123    /* Send data record to File daemon */
124    save_msg = fd->msg;          /* save fd message pointer */
125    fd->msg = rec->data;         /* pass data directly to bnet_send */
126    fd->msglen = rec->data_len;
127    Dmsg1(31, ">filed: send %d bytes data.\n", fd->msglen);
128    if (!bnet_send(fd)) {
129       Pmsg1(000, "Error sending to FD. ERR=%s\n", bnet_strerror(fd));
130       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
131          bnet_strerror(fd));
132
133       ok = false;
134    }
135    fd->msg = save_msg;                /* restore fd message pointer */
136    return ok;
137 }