2 * Append code for Storage daemon
8 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
31 /* Responses sent to the File daemon */
32 static char OK_data[] = "3000 OK data\n";
35 * Append Data sent from File daemon
38 int do_append_data(JCR *jcr)
41 long file_index, stream, last_file_index;
43 BSOCK *fd_sock = jcr->file_bsock;
45 DEVICE *dev = jcr->device->dev;
49 Dmsg0(10, "Start append data.\n");
51 /* Tell File daemon to send data */
52 bnet_fsend(fd_sock, OK_data);
54 if (!jcr->no_attributes && jcr->spool_attributes) {
55 open_spool_file(jcr, jcr->dir_bsock);
60 if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
61 set_jcr_job_status(jcr, JS_ErrorTerminated);
62 Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
66 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
68 block = new_block(dev);
71 * Acquire output device for writing. Note, after acquiring a
72 * device, we MUST release it, which is done at the end of this
75 Dmsg0(100, "just before acquire_device\n");
76 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
77 set_jcr_job_status(jcr, JS_ErrorTerminated);
81 Dmsg0(100, "Just after acquire_device_for_append\n");
83 * Write Begin Session Record
85 if (!write_session_label(jcr, block, SOS_LABEL)) {
86 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
88 set_jcr_job_status(jcr, JS_ErrorTerminated);
92 memset(&rec, 0, sizeof(rec));
95 * Get Data from File daemon, write to device. To clarify what is
96 * going on here. We expect:
98 * - Multiple records of data
101 * The Stream header is just used to sychronize things, and
102 * none of the stream header is written to tape.
103 * The Multiple records of data, contain first the Attributes,
104 * then after another stream header, the file data, then
105 * after another stream header, the MD5 data if any.
107 * So we get the (stream header, data, EOD) three time for each
108 * file. 1. for the Attributes, 2. for the file data if any,
109 * and 3. for the MD5 if any.
111 jcr->VolFirstFile = 0;
112 time(&jcr->run_time); /* start counting time for rates */
113 for (last_file_index = 0; ok && !job_canceled(jcr); ) {
116 /* Read Stream header from the File daemon.
117 * The stream header consists of the following:
118 * file_index (sequential Bacula file index)
119 * stream (arbitrary Bacula number to distinguish parts of data)
120 * info (Info for Storage daemon -- compressed, encryped, ...)
121 * info is not currently used, so is read, but ignored!
123 if ((n=bget_msg(ds)) <= 0) {
124 if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
125 break; /* end of data */
127 Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
132 ds->msg[ds->msglen] = 0;
133 if (sscanf(ds->msg, "%ld %ld %100s", &file_index, &stream, info) != 3) {
134 Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
138 Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
140 if (!(file_index > 0 && (file_index == last_file_index ||
141 file_index == last_file_index + 1))) {
142 Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
146 if (file_index != last_file_index) {
147 jcr->JobFiles = file_index;
148 if (jcr->VolFirstFile == 0) {
149 jcr->VolFirstFile = file_index;
151 last_file_index = file_index;
154 /* Read data stream from the File daemon.
155 * The data stream is just raw bytes
157 while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
159 rec.VolSessionId = jcr->VolSessionId;
160 rec.VolSessionTime = jcr->VolSessionTime;
161 rec.FileIndex = file_index;
163 rec.data_len = ds->msglen;
164 rec.data = ds->msg; /* use message buffer */
166 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
167 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex),
170 while (!write_record_to_block(block, &rec)) {
171 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
173 if (!write_block_to_device(jcr, dev, block)) {
174 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
175 dev_name(dev), strerror_dev(dev));
176 Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
183 Dmsg0(400, "Not OK\n");
186 jcr->JobBytes += rec.data_len; /* increment bytes this job */
187 Dmsg4(200, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
188 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
189 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
191 /* Send attributes and MD5 to Director for Catalog */
192 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE ||
193 stream == STREAM_WIN32_ATTRIBUTES || stream == STREAM_SHA1_SIGNATURE) {
194 if (!jcr->no_attributes) {
195 if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
196 jcr->dir_bsock->spool = 1;
198 Dmsg0(200, "Send attributes.\n");
199 if (!dir_update_file_attributes(jcr, &rec)) {
200 Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
201 bnet_strerror(jcr->dir_bsock));
203 jcr->dir_bsock->spool = 0;
206 jcr->dir_bsock->spool = 0;
210 if (is_bnet_error(ds)) {
211 Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
218 * We probably need a new flag that says "Do not attempt
219 * to write because there is no tape".
221 Dmsg0(90, "Write_end_session_label()\n");
223 /* Create Job status for end of session label */
224 set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
226 Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
228 if (!write_session_label(jcr, block, EOS_LABEL)) {
229 Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
231 set_jcr_job_status(jcr, JS_ErrorTerminated);
234 /* Write out final block of this session */
235 if (!write_block_to_device(jcr, dev, block)) {
236 Pmsg0(000, _("Set ok=FALSE after write_block_to_device.\n"));
237 set_jcr_job_status(jcr, JS_ErrorTerminated);
241 Dmsg1(200, "release device JobStatus=%d\n", jcr->JobStatus);
242 /* Release the device */
243 if (!release_device(jcr, dev)) {
244 Pmsg0(000, _("Error in release_device\n"));
245 set_jcr_job_status(jcr, JS_ErrorTerminated);
251 if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
252 bnet_despool(jcr->dir_bsock);
253 close_spool_file(jcr, jcr->dir_bsock);
256 dir_send_job_status(jcr); /* update director */
258 Dmsg0(90, "return from do_append_data()\n");