]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
- Put Dmsg() on inside if() to avoid calling subroutine.
[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
13    version 2 as ammended with additional clauses defined in the
14    file LICENSE in the main source directory.
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 
19    the file LICENSE for additional details.
20
21  */
22
23 #include "bacula.h"
24 #include "stored.h"
25
26 /* Forward referenced subroutines */
27 static bool record_cb(DCR *dcr, DEV_RECORD *rec);
28
29
30 /* Responses sent to the File daemon */
31 static char OK_data[]    = "3000 OK data\n";
32 static char FD_error[]   = "3000 error\n";
33 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
34
35 /*
36  *  Read Data and send to File Daemon
37  *   Returns: false on failure
38  *            true  on success
39  */
40 bool do_read_data(JCR *jcr)
41 {
42    BSOCK *fd = jcr->file_bsock;
43    bool ok = true;
44    DCR *dcr = jcr->dcr;
45
46    Dmsg0(20, "Start read data.\n");
47
48    if (!bnet_set_buffer_size(fd, dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
49       return false;
50    }
51
52
53    create_vol_list(jcr);
54    if (jcr->NumVolumes == 0) {
55       Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
56       free_vol_list(jcr);
57       bnet_fsend(fd, FD_error);
58       return false;
59    }
60
61    Dmsg2(200, "Found %d volumes names to restore. First=%s\n", jcr->NumVolumes,
62       jcr->VolList->VolumeName);
63
64    /* Ready device for reading */
65    if (!acquire_device_for_read(dcr)) {
66       free_vol_list(jcr);
67       bnet_fsend(fd, FD_error);
68       return false;
69    }
70
71    /* Tell File daemon we will send data */
72    bnet_fsend(fd, OK_data);
73    ok = read_records(dcr, record_cb, mount_next_read_volume);
74
75    /* Send end of data to FD */
76    bnet_sig(fd, BNET_EOD);
77
78    if (!release_device(dcr)) {
79       ok = false;
80    }
81
82    free_vol_list(jcr);
83    Dmsg0(30, "Done reading.\n");
84    return ok;
85 }
86
87 /*
88  * Called here for each record from read_records()
89  *  Returns: true if OK
90  *           false if error
91  */
92 static bool record_cb(DCR *dcr, DEV_RECORD *rec)
93 {
94    JCR *jcr = dcr->jcr;
95    BSOCK *fd = jcr->file_bsock;
96    bool ok = true;
97    POOLMEM *save_msg;
98
99    if (rec->FileIndex < 0) {
100       return true;
101    }
102    Dmsg5(100, "Send to FD: SessId=%u SessTim=%u FI=%d Strm=%d, len=%d\n",
103       rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, rec->Stream,
104       rec->data_len);
105
106    /* Send record header to File daemon */
107    if (!bnet_fsend(fd, rec_header, rec->VolSessionId, rec->VolSessionTime,
108           rec->FileIndex, rec->Stream, rec->data_len)) {
109       Dmsg1(30, ">filed: Error Hdr=%s\n", fd->msg);
110       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
111          bnet_strerror(fd));
112       return false;
113    } else {
114       Dmsg1(31, ">filed: Hdr=%s\n", fd->msg);
115    }
116
117
118    /* Send data record to File daemon */
119    save_msg = fd->msg;          /* save fd message pointer */
120    fd->msg = rec->data;         /* pass data directly to bnet_send */
121    fd->msglen = rec->data_len;
122    Dmsg1(31, ">filed: send %d bytes data.\n", fd->msglen);
123    if (!bnet_send(fd)) {
124       Pmsg1(000, "Error sending to FD. ERR=%s\n", bnet_strerror(fd));
125       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
126          bnet_strerror(fd));
127
128       ok = false;
129    }
130    fd->msg = save_msg;                /* restore fd message pointer */
131    return ok;
132 }