]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/append.c
- Convert more atoi to str_to_int64() for DB.
[bacula/bacula] / bacula / src / stored / append.c
1 /*
2  * Append code for Storage daemon
3  *  Kern Sibbald, May MM
4  *
5  *  Version $Id$
6  */
7 /*
8    Copyright (C) 2000-2005 Kern Sibbald
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30
31 /* Responses sent to the File daemon */
32 static char OK_data[]    = "3000 OK data\n";
33
34 /* Forward referenced functions */
35
36 /*
37  *  Append Data sent from File daemon
38  *
39  */
40 bool do_append_data(JCR *jcr)
41 {
42    int32_t n;
43    int32_t file_index, stream, last_file_index;
44    BSOCK *ds;
45    BSOCK *fd_sock = jcr->file_bsock;
46    bool ok = true;
47    DEV_RECORD rec;
48    DCR *dcr = jcr->dcr;
49    DEVICE *dev = dcr->dev;
50
51
52    Dmsg0(10, "Start append data.\n");
53
54    memset(&rec, 0, sizeof(rec));
55
56    ds = fd_sock;
57
58    if (!bnet_set_buffer_size(ds, dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
59       set_jcr_job_status(jcr, JS_ErrorTerminated);
60       Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
61       return false;
62    }
63
64    if (!acquire_device_for_append(dcr)) {
65       set_jcr_job_status(jcr, JS_ErrorTerminated);
66       return false;
67    }
68
69    set_jcr_job_status(jcr, JS_Running);
70    dir_send_job_status(jcr);
71
72    if (dev->VolCatInfo.VolCatName[0] == 0) {
73       Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
74    }
75    Dmsg1(20, "Begin append device=%s\n", dev->print_name());
76
77    begin_data_spool(dcr);
78    begin_attribute_spool(jcr);
79
80    Dmsg0(100, "Just after acquire_device_for_append\n");
81    if (dev->VolCatInfo.VolCatName[0] == 0) {
82       Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
83    }
84    /*
85     * Write Begin Session Record
86     */
87    if (!write_session_label(dcr, SOS_LABEL)) {
88       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
89          strerror_dev(dev));
90       set_jcr_job_status(jcr, JS_ErrorTerminated);
91       ok = false;
92    }
93    if (dev->VolCatInfo.VolCatName[0] == 0) {
94       Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
95    }
96
97    /* Tell File daemon to send data */
98    if (!bnet_fsend(fd_sock, OK_data)) {
99       berrno be;
100       Jmsg1(jcr, M_FATAL, 0, _("Network send error to FD. ERR=%s\n"),
101             be.strerror(fd_sock->b_errno));
102       ok = false;
103    }
104
105    /*
106     * Get Data from File daemon, write to device.  To clarify what is
107     *   going on here.  We expect:
108     *     - A stream header
109     *     - Multiple records of data
110     *     - EOD record
111     *
112     *    The Stream header is just used to sychronize things, and
113     *    none of the stream header is written to tape.
114     *    The Multiple records of data, contain first the Attributes,
115     *    then after another stream header, the file data, then
116     *    after another stream header, the MD5 data if any.
117     *
118     *   So we get the (stream header, data, EOD) three time for each
119     *   file. 1. for the Attributes, 2. for the file data if any,
120     *   and 3. for the MD5 if any.
121     */
122    dcr->VolFirstIndex = dcr->VolLastIndex = 0;
123    jcr->run_time = time(NULL);              /* start counting time for rates */
124    for (last_file_index = 0; ok && !job_canceled(jcr); ) {
125
126       /* Read Stream header from the File daemon.
127        *  The stream header consists of the following:
128        *    file_index (sequential Bacula file index, base 1)
129        *    stream     (Bacula number to distinguish parts of data)
130        *    info       (Info for Storage daemon -- compressed, encryped, ...)
131        *       info is not currently used, so is read, but ignored!
132        */
133      if ((n=bget_msg(ds)) <= 0) {
134          if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
135             break;                    /* end of data */
136          }
137          Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
138                bnet_strerror(ds));
139          ok = false;
140          break;
141       }
142
143       /*
144        * This hand scanning is a bit more complicated than a simple
145        *   sscanf, but it allows us to handle any size integer up to
146        *   int64_t without worrying about whether %d, %ld, %lld, or %q
147        *   is the correct format for each different architecture.
148        * It is a real pity that sscanf() is not portable.
149        */
150       char *p = ds->msg;
151       while (B_ISSPACE(*p)) {
152          p++;
153       }
154       file_index = (int32_t)str_to_int64(p);
155       while (B_ISDIGIT(*p)) {
156          p++;
157       }
158       if (!B_ISSPACE(*p) || !B_ISDIGIT(*(p+1))) {
159          Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
160          ok = false;
161          break;
162       }
163       stream = (int32_t)str_to_int64(p);
164
165       Dmsg2(890, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
166
167       if (!(file_index > 0 && (file_index == last_file_index ||
168           file_index == last_file_index + 1))) {
169          Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
170          ok = false;
171          break;
172       }
173       if (file_index != last_file_index) {
174          jcr->JobFiles = file_index;
175          last_file_index = file_index;
176       }
177
178       /* Read data stream from the File daemon.
179        *  The data stream is just raw bytes
180        */
181       while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
182          rec.VolSessionId = jcr->VolSessionId;
183          rec.VolSessionTime = jcr->VolSessionTime;
184          rec.FileIndex = file_index;
185          rec.Stream = stream;
186          rec.data_len = ds->msglen;
187          rec.data = ds->msg;            /* use message buffer */
188
189          Dmsg4(850, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
190             rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex),
191             rec.data_len);
192
193          while (!write_record_to_block(dcr->block, &rec)) {
194             Dmsg2(850, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
195                        rec.remainder);
196             if (!write_block_to_device(dcr)) {
197                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
198                   dev->print_name(), strerror_dev(dev));
199                Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
200                      dev->print_name(), strerror_dev(dev));
201                ok = false;
202                break;
203             }
204          }
205          if (!ok) {
206             Dmsg0(400, "Not OK\n");
207             break;
208          }
209          jcr->JobBytes += rec.data_len;   /* increment bytes this job */
210          Dmsg4(850, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
211             FI_to_ascii(rec.FileIndex), rec.VolSessionId,
212             stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
213
214          /* Send attributes and MD5 to Director for Catalog */
215          if (stream == STREAM_UNIX_ATTRIBUTES    || stream == STREAM_MD5_SIGNATURE ||
216              stream == STREAM_UNIX_ATTRIBUTES_EX || stream == STREAM_SHA1_SIGNATURE) {
217             if (!jcr->no_attributes) {
218                if (are_attributes_spooled(jcr)) {
219                   jcr->dir_bsock->spool = true;
220                }
221                Dmsg0(850, "Send attributes to dir.\n");
222                if (!dir_update_file_attributes(dcr, &rec)) {
223                   jcr->dir_bsock->spool = false;
224                   Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
225                      bnet_strerror(jcr->dir_bsock));
226                   ok = false;
227                   break;
228                }
229                jcr->dir_bsock->spool = false;
230             }
231          }
232          Dmsg0(350, "Enter bnet_get\n");
233       }
234       Dmsg1(350, "End read loop with FD. Stat=%d\n", n);
235       if (is_bnet_error(ds)) {
236          Dmsg1(350, "Network read error from FD. ERR=%s\n", bnet_strerror(ds));
237          Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
238                bnet_strerror(ds));
239          ok = false;
240          break;
241       }
242    }
243
244    /* Create Job status for end of session label */
245    set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
246
247    Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
248    if (dev->VolCatInfo.VolCatName[0] == 0) {
249       Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
250    }
251
252    /*
253     * If !OK, check if we can still write. This may not be the case
254     *  if we are at the end of the tape or we got a fatal I/O error.
255     */
256    if (ok || dev_can_write(dev)) {
257       if (!write_session_label(dcr, EOS_LABEL)) {
258          Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
259                strerror_dev(dev));
260          set_jcr_job_status(jcr, JS_ErrorTerminated);
261          ok = false;
262       }
263       if (dev->VolCatInfo.VolCatName[0] == 0) {
264          Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
265       }
266       Dmsg0(90, "back from write_end_session_label()\n");
267       /* Flush out final partial block of this session */
268       if (!write_block_to_device(dcr)) {
269          Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
270                dev->print_name(), strerror_dev(dev));
271          Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
272          ok = false;
273       }
274    }
275    if (dev->VolCatInfo.VolCatName[0] == 0) {
276       Pmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
277    }
278
279    if (!ok) {
280       discard_data_spool(dcr);
281    } else {
282       commit_data_spool(dcr);
283    }
284
285    if (ok) {
286       ok = dvd_close_job(dcr);  /* do DVD cleanup if any */
287    }
288    
289    /* Release the device -- and send final Vol info to DIR */
290    release_device(dcr);
291
292    if (!ok || job_canceled(jcr)) {
293       discard_attribute_spool(jcr);
294    } else {
295       commit_attribute_spool(jcr);
296    }
297
298    dir_send_job_status(jcr);          /* update director */
299
300    Dmsg1(100, "return from do_append_data() stat=%d\n", ok);
301    return ok;
302 }