]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/read.c
0e7dbeb8caf262036f85ffd107cade06d04a96f1
[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-2003 Kern Sibbald and John Walker
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 int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, 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: 0 on failure
43  *            1 on success
44  */
45 int do_read_data(JCR *jcr) 
46 {
47    BSOCK *fd = jcr->file_bsock;
48    int ok = TRUE;
49    DEVICE *dev;
50    DEV_BLOCK *block;
51    DCR *dcr;
52    
53    Dmsg0(20, "Start read data.\n");
54
55    dev = jcr->device->dev;
56
57    Dmsg1(10, "bstored>filed: %s\n", fd->msg);
58
59    if (!bnet_set_buffer_size(fd, jcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
60       return 0;
61    }
62
63
64    Dmsg1(20, "Begin read device=%s\n", dev_name(dev));
65
66    create_vol_list(jcr);
67    if (jcr->NumVolumes == 0) {
68       Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
69       free_vol_list(jcr);
70       bnet_fsend(fd, FD_error);
71       return 0;
72    }
73
74    Dmsg2(200, "Found %d volumes names to restore. First=%s\n", jcr->NumVolumes, 
75       jcr->VolList->VolumeName);
76
77    pm_strcpy(&jcr->VolumeName, jcr->VolList->VolumeName);
78
79    /* 
80     * Ready device for reading, and read records
81     */
82    if (!acquire_device_for_read(jcr)) {
83       free_vol_list(jcr);
84       return 0;
85    }
86
87    block = dcr->block;
88
89    /* Tell File daemon we will send data */
90    bnet_fsend(fd, OK_data);
91    ok = read_records(jcr, dev, record_cb, mount_next_read_volume);
92
93    /* Send end of data to FD */
94    bnet_sig(fd, BNET_EOD);
95
96    if (!release_device(jcr)) {
97       ok = FALSE;
98    }
99    
100    free_vol_list(jcr);
101    Dmsg0(30, "Done reading.\n");
102    return ok ? 1 : 0;
103 }
104
105 /*
106  * Called here for each record from read_records()
107  */
108 static int record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec)
109 {
110    BSOCK *fd = jcr->file_bsock;
111    int ok = TRUE;
112    POOLMEM *save_msg;
113
114    if (rec->FileIndex < 0) {
115       return 1;
116    }
117    Dmsg5(100, "Send to FD: SessId=%u SessTim=%u FI=%d Strm=%d, len=%d\n",
118       rec->VolSessionId, rec->VolSessionTime, rec->FileIndex, rec->Stream,
119       rec->data_len);
120
121    /* Send record header to File daemon */
122    if (!bnet_fsend(fd, rec_header, rec->VolSessionId, rec->VolSessionTime,
123           rec->FileIndex, rec->Stream, rec->data_len)) {
124       Dmsg1(30, ">filed: Error Hdr=%s\n", fd->msg);
125       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
126          bnet_strerror(fd));
127       return FALSE;
128    } else {
129       Dmsg1(30, ">filed: Hdr=%s\n", fd->msg);
130    }
131
132
133    /* Send data record to File daemon */
134    save_msg = fd->msg;          /* save fd message pointer */
135    fd->msg = rec->data;         /* pass data directly to bnet_send */
136    fd->msglen = rec->data_len;
137    Dmsg1(30, ">filed: send %d bytes data.\n", fd->msglen);
138    if (!bnet_send(fd)) {
139       Pmsg1(000, "Error sending to FD. ERR=%s\n", bnet_strerror(fd));
140       Jmsg1(jcr, M_FATAL, 0, _("Error sending to File daemon. ERR=%s\n"),
141          bnet_strerror(fd));
142
143       ok = FALSE;
144    }
145    fd->msg = save_msg;                /* restore fd message pointer */
146    return ok;
147 }