]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/append.c
Use the command line utility dropdb instead of the psql command
[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-2003 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 /* Forward referenced functions */
35 static bool is_spooled(JCR *jcr);
36 static int begin_attribute_spool(JCR *jcr);
37 static int discard_attribute_spool(JCR *jcr);
38 static int commit_attribute_spool(JCR *jcr);
39
40 /* 
41  *  Append Data sent from File daemon   
42  *
43  */
44 int do_append_data(JCR *jcr) 
45 {
46    int32_t n;
47    int32_t file_index, stream, last_file_index;
48    BSOCK *ds;
49    BSOCK *fd_sock = jcr->file_bsock;
50    bool ok = true;
51    DEVICE *dev = jcr->device->dev;
52    DEV_RECORD rec;
53    DEV_BLOCK  *block;
54    
55    Dmsg0(10, "Start append data.\n");
56
57    /* Tell File daemon to send data */
58    bnet_fsend(fd_sock, OK_data);
59
60    begin_attribute_spool(jcr);
61
62    ds = fd_sock;
63
64    if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
65       set_jcr_job_status(jcr, JS_ErrorTerminated);
66       Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
67       discard_attribute_spool(jcr);
68       return 0;
69    }
70
71    Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
72
73    block = new_block(dev);
74    memset(&rec, 0, sizeof(rec));
75
76    /* 
77     * Acquire output device for writing.  Note, after acquiring a
78     *   device, we MUST release it, which is done at the end of this
79     *   subroutine.
80     */
81    Dmsg0(100, "just before acquire_device\n");
82    if (!(dev=acquire_device_for_append(jcr, dev, block))) {
83       set_jcr_job_status(jcr, JS_ErrorTerminated);
84       free_block(block);
85       discard_attribute_spool(jcr);
86       return 0;
87    }
88    Dmsg0(100, "Just after acquire_device_for_append\n");
89    /*
90     * Write Begin Session Record
91     */
92    if (!write_session_label(jcr, block, SOS_LABEL)) {
93       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
94          strerror_dev(dev));
95       set_jcr_job_status(jcr, JS_ErrorTerminated);
96       ok = false;
97    }
98
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->VolFirstIndex = jcr->VolLastIndex = 0;
118    jcr->run_time = time(NULL);              /* start counting time for rates */
119    for (last_file_index = 0; ok && !job_canceled(jcr); ) {
120
121       /* Read Stream header from the File daemon.
122        *  The stream header consists of the following:
123        *    file_index (sequential Bacula file index, base 1)
124        *    stream     (Bacula number to distinguish parts of data)
125        *    info       (Info for Storage daemon -- compressed, encryped, ...)
126        *       info is not currently used, so is read, but ignored!
127        */
128      if ((n=bget_msg(ds)) <= 0) {
129          if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
130             break;                    /* end of data */
131          }
132          Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
133             bnet_strerror(ds));
134          ok = false;
135          break;
136       }
137         
138       /* 
139        * This hand scanning is a bit more complicated than a simple
140        *   sscanf, but it allows us to handle any size integer up to
141        *   int64_t without worrying about whether %d, %ld, %lld, or %q 
142        *   is the correct format for each different architecture.
143        * It is a real pity that sscanf() is not portable.
144        */
145       char *p = ds->msg;
146       while (B_ISSPACE(*p)) {
147          p++;
148       }
149       file_index = (int32_t)str_to_int64(p);
150       while (B_ISDIGIT(*p)) {
151          p++;
152       }
153       if (!B_ISSPACE(*p) || !B_ISDIGIT(*(p+1))) {
154          Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
155          ok = false;
156          break;
157       }
158       stream = (int32_t)str_to_int64(p);
159
160       Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
161
162       if (!(file_index > 0 && (file_index == last_file_index ||
163           file_index == last_file_index + 1))) {
164          Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
165          ok = false;
166          break;
167       }
168       if (file_index != last_file_index) {
169          jcr->JobFiles = file_index;
170          last_file_index = file_index;
171       }
172       
173       /* Read data stream from the File daemon.
174        *  The data stream is just raw bytes
175        */
176       while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
177
178          rec.VolSessionId = jcr->VolSessionId;
179          rec.VolSessionTime = jcr->VolSessionTime;
180          rec.FileIndex = file_index;
181          rec.Stream = stream;
182          rec.data_len = ds->msglen;
183          rec.data = ds->msg;            /* use message buffer */
184
185          Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
186             rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex), 
187             rec.data_len);
188           
189          while (!write_record_to_block(block, &rec)) {
190             Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
191                        rec.remainder);
192             if (!write_block_to_device(jcr, dev, block)) {
193                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
194                   dev_name(dev), strerror_dev(dev));
195                Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
196                      strerror_dev(dev));
197                ok = false;
198                break;
199             }
200          }
201          if (!ok) {
202             Dmsg0(400, "Not OK\n");
203             break;
204          }
205          jcr->JobBytes += rec.data_len;   /* increment bytes this job */
206          Dmsg4(200, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
207             FI_to_ascii(rec.FileIndex), rec.VolSessionId, 
208             stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
209
210          /* Send attributes and MD5 to Director for Catalog */
211          if (stream == STREAM_UNIX_ATTRIBUTES    || stream == STREAM_MD5_SIGNATURE ||
212              stream == STREAM_UNIX_ATTRIBUTES_EX || stream == STREAM_SHA1_SIGNATURE) { 
213             if (!jcr->no_attributes) {
214                if (is_spooled(jcr)) {
215                   jcr->dir_bsock->spool = 1;
216                }
217                Dmsg0(200, "Send attributes.\n");
218                if (!dir_update_file_attributes(jcr, &rec)) {
219                   Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
220                      bnet_strerror(jcr->dir_bsock));
221                   ok = false;
222                   jcr->dir_bsock->spool = 0;
223                   break;
224                }
225                jcr->dir_bsock->spool = 0;
226             }
227          }
228       }
229       if (is_bnet_error(ds)) {
230          Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
231             bnet_strerror(ds));
232          ok = false;
233          break;
234       }
235    }
236
237    /* Create Job status for end of session label */
238    set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
239
240    Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
241
242    /*
243     * If !OK, check if we can still write. This may not be the case
244     *  if we are at the end of the tape or we got a fatal I/O error.
245     */
246    if (ok || dev_can_write(dev)) {
247       if (!write_session_label(jcr, block, EOS_LABEL)) {
248          Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
249              strerror_dev(dev));
250          set_jcr_job_status(jcr, JS_ErrorTerminated);
251          ok = false;
252       }
253       Dmsg0(90, "back from write_end_session_label()\n");
254       /* Flush out final partial block of this session */
255       if (!write_block_to_device(jcr, dev, block)) {
256          Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
257          set_jcr_job_status(jcr, JS_ErrorTerminated);
258          ok = false;
259       }
260    }
261
262    Dmsg1(200, "calling release device JobStatus=%d\n", jcr->JobStatus);
263    /* Release the device */
264    if (!release_device(jcr, dev)) {
265       Pmsg0(000, _("Error in release_device\n"));
266       set_jcr_job_status(jcr, JS_ErrorTerminated);
267       ok = false;
268    }
269
270    free_block(block);
271
272    commit_attribute_spool(jcr);
273
274    dir_send_job_status(jcr);          /* update director */
275
276    Dmsg1(100, "return from do_append_data() stat=%d\n", ok);
277    return ok ? 1 : 0;
278 }
279
280 static bool is_spooled(JCR *jcr)
281 {
282    return jcr->spool_attributes && jcr->dir_bsock->spool_fd;
283 }
284
285 static int begin_attribute_spool(JCR *jcr)
286 {
287    if (!jcr->no_attributes && jcr->spool_attributes) {
288       return open_spool_file(jcr, jcr->dir_bsock);
289    }
290    return 1;
291 }
292
293 static int discard_attribute_spool(JCR *jcr)
294 {
295    if (is_spooled(jcr)) {
296       return close_spool_file(jcr, jcr->dir_bsock);
297    }
298    return 1;
299 }
300
301 static int commit_attribute_spool(JCR *jcr)
302 {
303    if (is_spooled(jcr)) {
304       bnet_despool(jcr->dir_bsock);
305       return close_spool_file(jcr, jcr->dir_bsock);
306    }
307    return 1;
308 }