]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/append.c
Status cleanup and cancelling FD
[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, 2001, 2002 Kern Sibbald and John Walker
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 /* 
35  *  Append Data sent from File daemon   
36  *
37  */
38 int do_append_data(JCR *jcr) 
39 {
40    int32_t n;
41    long file_index, stream, last_file_index;
42    BSOCK *ds;
43    BSOCK *fd_sock = jcr->file_bsock;
44    int ok = TRUE;
45    DEVICE *dev = jcr->device->dev;
46    DEV_RECORD rec;
47    DEV_BLOCK  *block;
48    
49    Dmsg0(10, "Start append data.\n");
50
51    /* Tell File daemon to send data */
52    bnet_fsend(fd_sock, OK_data);
53
54    sm_check(__FILE__, __LINE__, False);
55
56    if (!jcr->no_attributes && jcr->spool_attributes) {
57       open_spool_file(jcr, jcr->dir_bsock);
58    }
59
60    ds = fd_sock;
61
62    if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
63       set_jcr_job_status(jcr, JS_ErrorTerminated);
64       Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
65       return 0;
66    }
67
68    Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
69
70    block = new_block(dev);
71
72    /* 
73     * Acquire output device for writing.  Note, after acquiring a
74     *   device, we MUST release it, which is done at the end of this
75     *   subroutine.
76     */
77    Dmsg0(100, "just before acquire_device\n");
78    if (!acquire_device_for_append(jcr, dev, block)) {
79       set_jcr_job_status(jcr, JS_ErrorTerminated);
80       free_block(block);
81       return 0;
82    }
83    sm_check(__FILE__, __LINE__, False);
84
85    Dmsg0(100, "Just after acquire_device_for_append\n");
86    /*
87     * Write Begin Session Record
88     */
89    if (!write_session_label(jcr, block, SOS_LABEL)) {
90       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
91          strerror_dev(dev));
92       set_jcr_job_status(jcr, JS_ErrorTerminated);
93       ok = FALSE;
94    }
95
96    sm_check(__FILE__, __LINE__, False);
97
98    memset(&rec, 0, sizeof(rec));
99
100    /* 
101     * Get Data from File daemon, write to device.  To clarify what is
102     *   going on here.  We expect:        
103     *     - A stream header
104     *     - Multiple records of data
105     *     - EOD record
106     *
107     *    The Stream header is just used to sychronize things, and
108     *    none of the stream header is written to tape.
109     *    The Multiple records of data, contain first the Attributes,
110     *    then after another stream header, the file data, then
111     *    after another stream header, the MD5 data if any.  
112     *
113     *   So we get the (stream header, data, EOD) three time for each
114     *   file. 1. for the Attributes, 2. for the file data if any, 
115     *   and 3. for the MD5 if any.
116     */
117    jcr->VolFirstFile = 0;
118    time(&jcr->run_time);              /* start counting time for rates */
119    for (last_file_index = 0; ok && !job_cancelled(jcr); ) {
120       char info[100];
121
122       /* Read Stream header from the File daemon.
123        *  The stream header consists of the following:
124        *    file_index (sequential Bacula file index)
125        *    stream     (arbitrary Bacula number to distinguish parts of data)
126        *    info       (Info for Storage daemon -- compressed, encryped, ...)
127        *       info is not currently used, so is read, but ignored!
128        */
129      if ((n=bget_msg(ds)) <= 0) {
130          if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
131             break;                    /* end of data */
132          }
133          Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
134             bnet_strerror(ds));
135          ok = FALSE;
136          break;
137       }
138       sm_check(__FILE__, __LINE__, False);
139       ds->msg[ds->msglen] = 0;
140       if (sscanf(ds->msg, "%ld %ld %100s", &file_index, &stream, info) != 3) {
141          Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
142          ok = FALSE;
143          break;
144       }
145       Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
146
147       if (!(file_index > 0 && (file_index == last_file_index ||
148           file_index == last_file_index + 1))) {
149          Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
150          ok = FALSE;
151          break;
152       }
153       if (file_index != last_file_index) {
154          jcr->JobFiles = file_index;
155          if (jcr->VolFirstFile == 0) {
156             jcr->VolFirstFile = file_index;
157          }
158          last_file_index = file_index;
159       }
160       
161       /* Read data stream from the File daemon.
162        *  The data stream is just raw bytes
163        */
164       sm_check(__FILE__, __LINE__, False);
165       while ((n=bget_msg(ds)) > 0 && !job_cancelled(jcr)) {
166
167          sm_check(__FILE__, __LINE__, False);
168          rec.VolSessionId = jcr->VolSessionId;
169          rec.VolSessionTime = jcr->VolSessionTime;
170          rec.FileIndex = file_index;
171          rec.Stream = stream;
172          rec.data_len = ds->msglen;
173          rec.data = ds->msg;            /* use message buffer */
174
175          Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
176             rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex), 
177             rec.data_len);
178           
179          while (!write_record_to_block(block, &rec)) {
180             Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
181                        rec.remainder);
182             if (!write_block_to_device(jcr, dev, block)) {
183                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
184                   dev_name(dev), strerror_dev(dev));
185                Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
186                      strerror_dev(dev));
187                ok = FALSE;
188                break;
189             }
190          }
191          sm_check(__FILE__, __LINE__, False);
192          if (!ok) {
193             Dmsg0(400, "Not OK\n");
194             break;
195          }
196          jcr->JobBytes += rec.data_len;   /* increment bytes this job */
197          Dmsg4(200, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
198             FI_to_ascii(rec.FileIndex), rec.VolSessionId, 
199             stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
200
201          /* Send attributes and MD5 to Director for Catalog */
202          if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE ||
203              stream == STREAM_WIN32_ATTRIBUTES) { 
204             if (!jcr->no_attributes) {
205                if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
206                   jcr->dir_bsock->spool = 1;
207                }
208                Dmsg0(200, "Send attributes.\n");
209                if (!dir_update_file_attributes(jcr, &rec)) {
210                   Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
211                      bnet_strerror(jcr->dir_bsock));
212                   ok = FALSE;
213                   jcr->dir_bsock->spool = 0;
214                   break;
215                }
216                jcr->dir_bsock->spool = 0;
217             }
218          }
219          sm_check(__FILE__, __LINE__, False);
220       }
221       if (is_bnet_error(ds)) {
222          Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
223             bnet_strerror(ds));
224          ok = FALSE;
225          break;
226       }
227    }
228    /* 
229     *   We probably need a new flag that says "Do not attempt
230     *   to write because there is no tape".
231     */
232    sm_check(__FILE__, __LINE__, False);
233    Dmsg0(90, "Write_end_session_label()\n");
234
235    /* Create Job status for end of session label */
236    set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
237
238    Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
239
240    if (!write_session_label(jcr, block, EOS_LABEL)) {
241       Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
242           strerror_dev(dev));
243       set_jcr_job_status(jcr, JS_ErrorTerminated);
244       ok = FALSE;
245    }
246    /* Write out final block of this session */
247    if (!write_block_to_device(jcr, dev, block)) {
248       Pmsg0(000, _("Set ok=FALSE after write_block_to_device.\n"));
249       set_jcr_job_status(jcr, JS_ErrorTerminated);
250       ok = FALSE;
251    }
252
253    Dmsg1(200, "release device JobStatus=%d\n", jcr->JobStatus);
254    /* Release the device */
255    if (!release_device(jcr, dev)) {
256       Pmsg0(000, _("Error in release_device\n"));
257       set_jcr_job_status(jcr, JS_ErrorTerminated);
258       ok = FALSE;
259    }
260
261    free_block(block);
262
263    if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
264       bnet_despool(jcr->dir_bsock);
265       close_spool_file(jcr, jcr->dir_bsock);
266    }
267
268    dir_send_job_status(jcr);          /* update director */
269
270    Dmsg0(90, "return from do_append_data()\n");
271    return ok ? 1 : 0;
272 }