]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/append.c
83f95674df37c075d9f865bf2190ce1fadf75b53
[bacula/bacula] / bacula / src / stored / append.c
1 /*
2  * Append code for Storage daemon
3  *  Kern Sibbald, May MM
4  */
5 /*
6    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 #include "bacula.h"
26 #include "stored.h"
27
28 extern int  FiledDataChan;            /* File daemon data channel (port) */
29 extern int BaculaTapeVersion;         /* Version number */
30 extern char BaculaId[];               /* Id string */
31
32 /* Responses sent to the File daemon */
33 static char OK_data[]    = "3000 OK data\n";
34
35 /* 
36  *  Append Data sent from File daemon   
37  *
38  */
39 int do_append_data(JCR *jcr) 
40 {
41    int32_t n;
42    long file_index, stream, last_file_index;
43    BSOCK *ds;
44    BSOCK *fd_sock = jcr->file_bsock;
45    int ok = TRUE;
46    DEVICE *dev = jcr->device->dev;
47    DEV_RECORD rec;
48    DEV_BLOCK  *block;
49    
50    Dmsg0(10, "Start append data.\n");
51
52    /* Tell File daemon to send data */
53    bnet_fsend(fd_sock, OK_data);
54
55    sm_check(__FILE__, __LINE__, False);
56
57    ds = fd_sock;
58
59    if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
60       jcr->JobStatus = JS_Cancelled;
61       Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
62       return 0;
63    }
64
65    Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
66
67    block = new_block(dev);
68
69    /* 
70     * Acquire output device for writing.  Note, after acquiring a
71     *   device, we MUST release it, which is done at the end of this
72     *   subroutine.
73     */
74    Dmsg0(100, "just before acquire_device\n");
75    if (!acquire_device_for_append(jcr, dev, block)) {
76       jcr->JobStatus = JS_Cancelled;
77       free_block(block);
78       return 0;
79    }
80    sm_check(__FILE__, __LINE__, False);
81
82    Dmsg0(100, "Just after acquire_device_for_append\n");
83    /*
84     * Write Begin Session Record
85     */
86    if (!write_session_label(jcr, block, SOS_LABEL)) {
87       jcr->JobStatus = JS_Cancelled;
88       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
89          strerror_dev(dev));
90       ok = FALSE;
91    }
92
93    sm_check(__FILE__, __LINE__, False);
94
95    memset(&rec, 0, sizeof(rec));
96
97    /* 
98     * Get Data from File daemon, write to device   
99     */
100    jcr->VolFirstFile = 0;
101    time(&jcr->run_time);              /* start counting time for rates */
102    for (last_file_index = 0; ok && !job_cancelled(jcr); ) {
103       char info[100];
104
105       /* Read Stream header from the File daemon.
106        *  The stream header consists of the
107        *
108        *   file_index (sequential Bacula file index)
109        *   stream     (arbitrary Bacula number to distinguish parts of data)
110        *   info       (Info for Storage daemon -- compressed, encryped, ...)
111        */
112       if ((n=bget_msg(ds)) < 0) { 
113          Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
114             bnet_strerror(ds));
115          ok = FALSE;
116          break;
117       }
118       if (n == 0) {                   /* End of conversation */
119          break;                       /* all done */
120       }
121       sm_check(__FILE__, __LINE__, False);
122       ds->msg[ds->msglen] = 0;
123       if (sscanf(ds->msg, "%ld %ld %100s", &file_index, &stream, info) != 3) {
124          Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
125          ok = FALSE;
126          break;
127       }
128       Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
129
130       if (!(file_index > 0 && (file_index == last_file_index ||
131           file_index == last_file_index + 1))) {
132          Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
133          ok = FALSE;
134          break;
135       }
136       if (file_index != last_file_index) {
137          jcr->JobFiles = file_index;
138          if (jcr->VolFirstFile == 0) {
139             jcr->VolFirstFile = file_index;
140          }
141          last_file_index = file_index;
142       }
143       
144       /* Read data stream from the File daemon.
145        *  The data stream is just raw bytes
146        */
147       sm_check(__FILE__, __LINE__, False);
148       while ((n=bget_msg(ds)) > 0 && !job_cancelled(jcr)) {
149          char *dsmsg = ds->msg;
150
151          sm_check(__FILE__, __LINE__, False);
152          rec.VolSessionId = jcr->VolSessionId;
153          rec.VolSessionTime = jcr->VolSessionTime;
154          rec.FileIndex = file_index;
155          rec.Stream = stream;
156          rec.data_len = ds->msglen;
157          rec.data = ds->msg;            /* use message buffer */
158
159          Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
160             rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream), 
161             rec.data_len);
162           
163          while (!write_record_to_block(block, &rec)) {
164             Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
165                        rec.remainder);
166             if (!write_block_to_device(jcr, dev, block)) {
167                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
168                   dev_name(dev), strerror_dev(dev));
169                Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
170                      strerror_dev(dev));
171                ok = FALSE;
172                break;
173             }
174          }
175          sm_check(__FILE__, __LINE__, False);
176          if (!ok) {
177             break;
178          }
179          jcr->JobBytes += rec.data_len;   /* increment bytes this job */
180          Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
181             FI_to_ascii(rec.FileIndex), rec.VolSessionId, 
182             stream_to_ascii(rec.Stream), rec.data_len);
183          /* Send attributes and MD5 to Director for Catalog */
184          if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE) {
185             if (!dir_update_file_attributes(jcr, &rec)) {
186                ok = FALSE;
187                break;
188             }
189          }
190          ASSERT(dsmsg == ds->msg);
191          ASSERT(dsmsg == rec.data);
192          sm_check(__FILE__, __LINE__, False);
193       }
194       if (n < 0) {
195          Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
196             bnet_strerror(ds));
197          ok = FALSE;
198          break;
199       }
200    }
201    /* 
202     *******FIXME***** we should put the ok status in the End of
203     * session label 
204     *
205     *   We probably need a new flag that says "Do not attempt
206     *   to write because there is no tape".
207     */
208    sm_check(__FILE__, __LINE__, False);
209    Dmsg0(90, "Write_end_session_label()\n");
210    if (!write_session_label(jcr, block, EOS_LABEL)) {
211       Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
212           strerror_dev(dev));
213       ok = FALSE;
214    }
215    /* Write out final block of this session */
216    if (!write_block_to_device(jcr, dev, block)) {
217       Dmsg0(0, "Set ok=FALSE after write_block_to_device.\n");
218       ok = FALSE;
219    }
220
221    /* Release the device */
222    if (!release_device(jcr, dev, block)) {
223       Dmsg0(0, "Error in release_device\n");
224       ok = FALSE;
225    }
226
227    free_block(block);
228    Dmsg0(90, "return from do_append_data()\n");
229    return ok ? 1 : 0;
230 }