2 * Bacula File Daemon backup.c send file attributes and data
3 * to the Storage daemon.
5 * Kern Sibbald, March MM
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 static int save_file(FF_PKT *ff_pkt, void *pkt);
34 * Find all the requested files and send them
35 * to the Storage daemon.
38 int blast_data_to_storage_daemon(JCR *jcr, char *addr, int port)
43 sd = jcr->store_bsock;
45 jcr->JobStatus = JS_Running;
47 Dmsg1(10, "bfiled: opened data connection %d to stored\n", sd->fd);
49 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
52 jcr->buf_size = sd->msglen;
54 jcr->compress_buf = (char *) bmalloc(jcr->buf_size);
56 Dmsg1(100, "set_find_options ff=%p\n", jcr->ff);
57 set_find_options(jcr->ff, jcr->incremental, jcr->mtime);
58 Dmsg0(10, "start find files\n");
60 /* Subroutine save_file() is called for each file */
61 /* ***FIXME**** add FSM code */
62 if (!find_files(jcr->ff, save_file, (void *)jcr)) {
66 bnet_sig(sd, BNET_EOF); /* terminate data connection */
72 if (jcr->compress_buf) {
73 free(jcr->compress_buf);
74 jcr->compress_buf = NULL;
80 * Called here by find() for each file included.
82 * *****FIXME***** add FSMs File System Modules
84 * Send the file and its data to the Storage daemon.
86 static int save_file(FF_PKT *ff_pkt, void *ijcr)
88 char attribs[MAXSTRING];
89 int fid, stat, stream;
91 struct MD5Context md5c;
93 unsigned char signature[16];
95 JCR *jcr = (JCR *)ijcr;
98 sd = jcr->store_bsock;
100 jcr->num_files_examined++; /* bump total file count */
102 switch (ff_pkt->type) {
103 case FT_LNKSAVED: /* Hard linked, file already saved */
106 Dmsg1(30, "FT_REGE saving: %s\n", ff_pkt->fname);
109 Dmsg1(30, "FT_REG saving: %s\n", ff_pkt->fname);
112 Dmsg2(30, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
115 Dmsg1(30, "FT_DIR saving: %s\n", ff_pkt->link);
118 Dmsg1(30, "FT_SPEC saving: %s\n", ff_pkt->fname);
121 Jmsg(jcr, M_NOTSAVED, -1, _(" Could not access %s: ERR=%s"), ff_pkt->fname,
122 strerror(ff_pkt->ff_errno));
125 Jmsg(jcr, M_NOTSAVED, -1, _(" Could not follow link %s: ERR=%s\n"), ff_pkt->fname,
126 strerror(ff_pkt->ff_errno));
129 Jmsg(jcr, M_NOTSAVED, -1, _(" Could not stat %s: ERR=%s\n"), ff_pkt->fname,
130 strerror(ff_pkt->ff_errno));
134 Jmsg(jcr, M_SKIPPED, -1, _(" Unchanged file skipped: %s\n"), ff_pkt->fname);
137 Jmsg(jcr, M_NOTSAVED, -1, _(" Archive file not saved: %s\n"), ff_pkt->fname);
140 Jmsg(jcr, M_SKIPPED, -1, _(" Recursion turned off. Directory skipped: %s\n"),
144 Jmsg(jcr, M_SKIPPED, -1, _(" File system change prohibited. Directory skipped. %s\n"),
148 Jmsg(jcr, M_NOTSAVED, -1, _(" Could not open directory %s: ERR=%s\n"), ff_pkt->fname,
149 strerror(ff_pkt->ff_errno));
152 Jmsg(jcr, M_ERROR, 0, _("Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname);
156 if (ff_pkt->type != FT_LNKSAVED && S_ISREG(ff_pkt->statp.st_mode) &&
157 ff_pkt->statp.st_size > 0) {
158 if ((fid = open(ff_pkt->fname, O_RDONLY | O_BINARY)) < 0) {
159 ff_pkt->ff_errno = errno;
160 Jmsg(jcr, M_NOTSAVED, -1, _("Cannot open %s: ERR=%s.\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno));
167 Dmsg1(30, "bfiled: sending %s to stored\n", ff_pkt->fname);
168 encode_stat(attribs, &ff_pkt->statp);
170 jcr->JobFiles++; /* increment number of files sent */
171 jcr->last_fname = (char *) check_pool_memory_size(jcr->last_fname, strlen(ff_pkt->fname) + 1);
172 strcpy(jcr->last_fname, ff_pkt->fname);
175 * Send Attributes header to Storage daemon
176 * <file-index> <stream> <info>
178 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_UNIX_ATTRIBUTES)) {
184 Dmsg1(10, ">stored: attrhdr %s\n", sd->msg);
187 * Send file attributes to Storage daemon
190 * Filename (full path)
192 * Link name (if type==FT_LNK)
193 * For a directory, link is the same as fname, but with trailing
194 * slash. For a linked file, link is the link.
196 if (ff_pkt->type == FT_LNK) {
197 stat = bnet_fsend(sd, "%ld %d %s%c%s%c%s%c", jcr->JobFiles,
198 ff_pkt->type, ff_pkt->fname, 0, attribs, 0, ff_pkt->link, 0);
199 } else if (ff_pkt->type == FT_DIR) {
200 stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c", jcr->JobFiles,
201 ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0);
203 stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c", jcr->JobFiles,
204 ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0);
207 Dmsg2(20, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
214 /* send data termination sentinel */
215 bnet_sig(sd, BNET_EOD);
218 * If the file has data, read it and send to the Storage daemon
223 Dmsg1(60, "Saving data, type=%d\n", ff_pkt->type);
225 * Send Data header to Storage daemon
226 * <file-index> <stream> <info>
228 if (ff_pkt->flags & FO_GZIP) {
229 stream = STREAM_GZIP_DATA;
230 /* Adjust for compression so that output buffer is
231 * 12 bytes + 0.1% larger than input buffer
233 read_size = jcr->buf_size - 12 - (jcr->buf_size / 1000) - 1;
235 stream = STREAM_FILE_DATA;
236 read_size = jcr->buf_size;
238 if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream)) {
242 Dmsg1(10, ">stored: datahdr %s\n", sd->msg);
244 if (ff_pkt->flags & FO_MD5) {
249 while ((sd->msglen=read(fid, sd->msg, read_size)) > 0) {
254 if (ff_pkt->flags & FO_MD5) {
255 MD5Update(&md5c, (unsigned char *) (sd->msg), sd->msglen);
258 /* ***FIXME*** add compression level options */
260 if (ff_pkt->flags & FO_GZIP) {
261 if (compress((Bytef *)jcr->compress_buf, &compress_len,
262 (const Bytef *)sd->msg, (uLong)sd->msglen) != Z_OK) {
263 Jmsg(jcr, M_ERROR, 0, _("Compression error\n"));
269 sd->msg = jcr->compress_buf;
270 sd->msglen = compress_len;
273 if (!bnet_send(sd)) {
279 Dmsg1(30, "Send data to FD len=%d\n", sd->msglen);
280 jcr->JobBytes += sd->msglen;
282 if (sd->msglen < 0) {
283 Jmsg(jcr, M_ERROR, 0, _("Error during save reading ERR=%s\n"), ff_pkt->fname,
284 strerror(ff_pkt->ff_errno));
288 /* Send data termination poll signal to Storage daemon.
289 * NOTE possibly put this poll on a counter as specified
290 * by the user to improve efficiency (i.e. poll every
291 * other file, every third file, ...
293 bnet_sig(sd, BNET_EOD_POLL);
294 Dmsg0(30, "Send EndData_Poll\n");
295 /* ***FIXME**** change to use bget_msg() */
296 if (bnet_recv(sd) <= 0) {
300 if (strcmp(sd->msg, "3000 OK\n") != 0) {
301 Jmsg1(jcr, M_ERROR, 0, _("Job aborted by Storage daemon: %s\n"), sd->msg);
306 close(fid); /* close file */
310 /* Terminate any MD5 signature and send it to Storage daemon and the Director */
311 if (gotMD5 && ff_pkt->flags & FO_MD5) {
312 MD5Final(signature, &md5c);
313 bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_MD5_SIGNATURE);
314 Dmsg1(10, "bfiled>stored:header %s\n", sd->msg);
315 memcpy(sd->msg, signature, 16);
318 bnet_sig(sd, BNET_EOD); /* end of MD5 */
322 if (ff_pkt->type == FT_DIR) {
323 Jmsg(jcr, M_SAVED, -1, _(" Directory saved normally: %s\n"), ff_pkt->link);
325 Jmsg(jcr, M_SAVED, -1, _(" File saved normally: %s\n"), ff_pkt->fname);