2 * Append code for Storage daemon
8 Copyright (C) 2000, 2001, 2002 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,
30 extern int FiledDataChan; /* File daemon data channel (port) */
31 extern int BaculaTapeVersion; /* Version number */
32 extern char BaculaId[]; /* Id string */
34 /* Responses sent to the File daemon */
35 static char OK_data[] = "3000 OK data\n";
38 * Append Data sent from File daemon
41 int do_append_data(JCR *jcr)
44 long file_index, stream, last_file_index;
46 BSOCK *fd_sock = jcr->file_bsock;
48 DEVICE *dev = jcr->device->dev;
52 Dmsg0(10, "Start append data.\n");
54 /* Tell File daemon to send data */
55 bnet_fsend(fd_sock, OK_data);
57 sm_check(__FILE__, __LINE__, False);
59 if (!jcr->no_attributes && jcr->spool_attributes) {
60 open_spool_file(jcr, jcr->dir_bsock);
65 if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
66 jcr->JobStatus = JS_Cancelled;
67 Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
71 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
73 block = new_block(dev);
76 * Acquire output device for writing. Note, after acquiring a
77 * device, we MUST release it, which is done at the end of this
80 Dmsg0(100, "just before acquire_device\n");
81 if (!acquire_device_for_append(jcr, dev, block)) {
82 jcr->JobStatus = JS_Cancelled;
86 sm_check(__FILE__, __LINE__, False);
88 Dmsg0(100, "Just after acquire_device_for_append\n");
90 * Write Begin Session Record
92 if (!write_session_label(jcr, block, SOS_LABEL)) {
93 jcr->JobStatus = JS_Cancelled;
94 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
99 sm_check(__FILE__, __LINE__, False);
101 memset(&rec, 0, sizeof(rec));
104 * Get Data from File daemon, write to device
106 jcr->VolFirstFile = 0;
107 time(&jcr->run_time); /* start counting time for rates */
108 for (last_file_index = 0; ok && !job_cancelled(jcr); ) {
111 /* Read Stream header from the File daemon.
112 * The stream header consists of the following:
113 * file_index (sequential Bacula file index)
114 * stream (arbitrary Bacula number to distinguish parts of data)
115 * info (Info for Storage daemon -- compressed, encryped, ...)
117 if ((n=bget_msg(ds)) < 0) {
118 Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
123 if (n == 0 || job_cancelled(jcr)) {
124 break; /* all done */
126 sm_check(__FILE__, __LINE__, False);
127 ds->msg[ds->msglen] = 0;
128 if (sscanf(ds->msg, "%ld %ld %100s", &file_index, &stream, info) != 3) {
129 Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
133 Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
135 if (!(file_index > 0 && (file_index == last_file_index ||
136 file_index == last_file_index + 1))) {
137 Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
141 if (file_index != last_file_index) {
142 jcr->JobFiles = file_index;
143 if (jcr->VolFirstFile == 0) {
144 jcr->VolFirstFile = file_index;
146 last_file_index = file_index;
149 /* Read data stream from the File daemon.
150 * The data stream is just raw bytes
152 sm_check(__FILE__, __LINE__, False);
153 while ((n=bget_msg(ds)) > 0 && !job_cancelled(jcr)) {
155 sm_check(__FILE__, __LINE__, False);
156 rec.VolSessionId = jcr->VolSessionId;
157 rec.VolSessionTime = jcr->VolSessionTime;
158 rec.FileIndex = file_index;
160 rec.data_len = ds->msglen;
161 rec.data = ds->msg; /* use message buffer */
163 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
164 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream),
167 while (!write_record_to_block(block, &rec)) {
168 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
170 if (!write_block_to_device(jcr, dev, block)) {
171 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
172 dev_name(dev), strerror_dev(dev));
173 Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
179 sm_check(__FILE__, __LINE__, False);
181 Dmsg0(400, "Not OK\n");
184 jcr->JobBytes += rec.data_len; /* increment bytes this job */
185 Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
186 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
187 stream_to_ascii(rec.Stream), rec.data_len);
188 /* Send attributes and MD5 to Director for Catalog */
189 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE) {
190 if (!jcr->no_attributes) {
191 if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
192 jcr->dir_bsock->spool = 1;
194 if (!dir_update_file_attributes(jcr, &rec)) {
195 Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
196 bnet_strerror(jcr->dir_bsock));
198 jcr->dir_bsock->spool = 0;
201 jcr->dir_bsock->spool = 0;
204 sm_check(__FILE__, __LINE__, False);
207 Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
214 *******FIXME***** we should put the ok status in the End of
217 * We probably need a new flag that says "Do not attempt
218 * to write because there is no tape".
220 sm_check(__FILE__, __LINE__, False);
221 Dmsg0(90, "Write_end_session_label()\n");
222 if (!write_session_label(jcr, block, EOS_LABEL)) {
223 Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
227 /* Write out final block of this session */
228 if (!write_block_to_device(jcr, dev, block)) {
229 Pmsg0(000, "Set ok=FALSE after write_block_to_device.\n");
233 /* Release the device */
234 if (!release_device(jcr, dev)) {
235 Pmsg0(000, "Error in release_device\n");
241 if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
242 bnet_despool(jcr->dir_bsock);
243 close_spool_file(jcr, jcr->dir_bsock);
246 Dmsg0(90, "return from do_append_data()\n");