Debian Codegraph @

application-src/ambsdtar.c

raw
1 /*
2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
4 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
5 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of U.M. not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. U.M. makes no representations about the
15 * suitability of this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
17 *
18 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
20 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * Authors: the Amanda Development Team. Its members are listed in a
26 * file named AUTHORS, in the root directory of this distribution.
27 */
28
29 /* PROPERTY:
30 *
31 * BSDTAR-PATH (default /usr/bin/tar))
32 * STATE-DIR (default $AMSTATDIR/bsdtar)
33 * TARGET (DIRECTORY) (no default, if set, the backup will be from that
34 * directory instead of from the --device)
35 * ONE-FILE-SYSTEM (default YES)
36 * SPARSE (default YES) -S on restore
37 * INCLUDE-FILE
38 * INCLUDE-LIST
39 * INCLUDE-OPTIONAL
40 * EXCLUDE-FILE
41 * EXCLUDE-LIST
42 * EXCLUDE-OPTIONAL
43 * NORMAL
44 * IGNORE
45 * STRANGE
46 * EXIT-HANDLING (1=GOOD 2=BAD)
47 * TAR-BLOCKSIZE (default does not add --block-size option,
48 * using tar's default)
49 * VERBOSE
50 */
51
52 #include "amanda.h"
53 #include "match.h"
54 #include "pipespawn.h"
55 #include "amfeatures.h"
56 #include "clock.h"
57 #include "amutil.h"
58 #include "getfsent.h"
59 #include "client_util.h"
60 #include "conffile.h"
61 #include "getopt.h"
62 #include "event.h"
63 #include "security-file.h"
64 #include "ammessage.h"
65
66 int debug_application = 1;
67 #define application_debug(i, ...) do { \
68 if ((i) <= debug_application) { \
69 g_debug(__VA_ARGS__); \
70 } \
71 } while (0)
72
73 static amregex_t init_re_table[] = {
74 /* tar prints the size in bytes */
75 AM_SIZE_RE("^ *Total bytes written: [0-9][0-9]*", 1, 1),
76 AM_NORMAL_RE("^could not open conf file"),
77 AM_NORMAL_RE("^Elapsed time:"),
78 AM_NORMAL_RE("^Throughput"),
79 AM_IGNORE_RE(": Directory is new$"),
80 AM_IGNORE_RE(": Directory has been renamed"),
81
82 /* GNU tar 1.13.17 will print this warning when (not) backing up a
83 Unix named socket. */
84 AM_NORMAL_RE(": socket ignored$"),
85
86 /* GNUTAR produces a few error messages when files are modified or
87 removed while it is running. They may cause data to be lost, but
88 then they may not. We shouldn't consider them NORMAL until
89 further investigation. */
90 AM_NORMAL_RE(": File .* shrunk by [0-9][0-9]* bytes, padding with zeros"),
91 AM_NORMAL_RE(": Cannot add file .*: No such file or directory$"),
92 AM_NORMAL_RE(": Error exit delayed from previous errors"),
93
94 AM_ERROR_RE("ambsdtar: error"),
95
96 /* catch-all: DMP_STRANGE is returned for all other lines */
97 AM_STRANGE_RE(NULL)
98 };
99 static amregex_t *re_table;
100
101 /* local functions */
102 int main(int argc, char **argv);
103
104 typedef struct application_argument_s {
105 char *config;
106 char *host;
107 int message;
108 int collection;
109 int calcsize;
110 char *tar_blocksize;
111 GSList *level;
112 GSList *command_options;
113 dle_t dle;
114 int argc;
115 char **argv;
116 int verbose;
117 } application_argument_t;
118
119 enum { CMD_ESTIMATE, CMD_BACKUP };
120
121 static void ambsdtar_support(application_argument_t *argument);
122 static void ambsdtar_selfcheck(application_argument_t *argument);
123 static void ambsdtar_estimate(application_argument_t *argument);
124 static void ambsdtar_backup(application_argument_t *argument);
125 static void ambsdtar_restore(application_argument_t *argument);
126 static void ambsdtar_validate(application_argument_t *argument);
127 static void ambsdtar_index(application_argument_t *argument);
128 static void ambsdtar_build_exinclude(dle_t *dle,
129 int *nb_exclude, char **file_exclude,
130 int *nb_include, char **file_include,
131 char *dirname, messagelist_t *mlist);
132 static char *ambsdtar_get_timestamps(application_argument_t *argument,
133 int level,
134 FILE *mesgstream, int command);
135 static void ambsdtar_set_timestamps(application_argument_t *argument,
136 int level,
137 char *timestamps,
138 FILE *mesgstream);
139 static GPtrArray *ambsdtar_build_argv(char *bsdtar_realpath,
140 application_argument_t *argument,
141 char *timestamps,
142 char **file_exclude, char **file_include,
143 int command, messagelist_t *mlist);
144 static char *command = NULL;
145 static char *bsdtar_path;
146 static char *state_dir;
147 static char *bsdtar_target;
148 static int bsdtar_onefilesystem;
149 static int bsdtar_sparse;
150 static GSList *normal_message = NULL;
151 static GSList *ignore_message = NULL;
152 static GSList *strange_message = NULL;
153 static char *exit_handling;
154 static int exit_value[256];
155 static off_t gblocksize = 0;
156 static int ambsdtar_exit_value = 0;
157 static FILE *mesgstream;
158 static gboolean restore_ok = TRUE;
159
160 static struct option long_options[] = {
161 {"config" , 1, NULL, 1},
162 {"host" , 1, NULL, 2},
163 {"disk" , 1, NULL, 3},
164 {"device" , 1, NULL, 4},
165 {"level" , 1, NULL, 5},
166 {"index" , 1, NULL, 6},
167 {"message" , 1, NULL, 7},
168 {"collection" , 0, NULL, 8},
169 {"record" , 0, NULL, 9},
170 {"bsdtar-path" , 1, NULL, 10},
171 {"state-dir" , 1, NULL, 11},
172 {"one-file-system" , 1, NULL, 12},
173 {"include-file" , 1, NULL, 16},
174 {"include-list" , 1, NULL, 17},
175 {"include-optional", 1, NULL, 18},
176 {"exclude-file" , 1, NULL, 19},
177 {"exclude-list" , 1, NULL, 20},
178 {"exclude-optional", 1, NULL, 21},
179 {"directory" , 1, NULL, 22},
180 {"normal" , 1, NULL, 23},
181 {"ignore" , 1, NULL, 24},
182 {"strange" , 1, NULL, 25},
183 {"exit-handling" , 1, NULL, 26},
184 {"calcsize" , 0, NULL, 27},
185 {"tar-blocksize" , 1, NULL, 28},
186 {"no-unquote" , 1, NULL, 29},
187 {"command-options" , 1, NULL, 33},
188 {"verbose" , 1, NULL, 36},
189 {"sparse" , 1, NULL, 37},
190 {"target" , 1, NULL, 38},
191 {NULL, 0, NULL, 0}
192 };
193
194 static message_t *
195 ambsdtar_print_message(
196 message_t *message)
197 {
198 if (strcasecmp(command, "selfcheck") == 0) {
199 return print_message(message);
200 }
201 if (message_get_severity(message) <= MSG_INFO) {
202 if (g_str_equal(command, "estimate")) {
203 fprintf(stdout, "OK %s\n", get_message(message));
204 } else if (g_str_equal(command, "backup")) {
205 fprintf(mesgstream, "| %s\n", get_message(message));
206 }
207 } else {
208 ambsdtar_exit_value = 1;
209 if (g_str_equal(command, "estimate")) {
210 fprintf(stdout, "ERROR %s\n", get_message(message));
211 } else if (g_str_equal(command, "backup")) {
212 fprintf(mesgstream, "sendbackup: error [%s]\n", get_message(message));
213 }
214 }
215 return message;
216 }
217
218
219 static char *
220 escape_tar_glob(
221 char *str,
222 int *in_argv)
223 {
224 char *result = malloc(4*strlen(str)+1);
225 char *r = result;
226 char *s;
227
228 *in_argv = 0;
229 for (s = str; *s != '\0'; s++) {
230 if (*s == '\\') {
231 char c = *(s+1);
232 if (c == '\\') {
233 *r++ = '\\';
234 *r++ = '\\';
235 *r++ = '\\';
236 s++;
237 } else if (c == '?') {
238 *r++ = 127;
239 s++;
240 continue;
241 } else if (c == 'a') {
242 *r++ = 7;
243 s++;
244 continue;
245 } else if (c == 'b') {
246 *r++ = 8;
247 s++;
248 continue;
249 } else if (c == 'f') {
250 *r++ = 12;
251 s++;
252 continue;
253 } else if (c == 'n') {
254 *r++ = 10;
255 s++;
256 *in_argv = 1;
257 continue;
258 } else if (c == 'r') {
259 *r++ = 13;
260 s++;
261 *in_argv = 1;
262 continue;
263 } else if (c == 't') {
264 *r++ = 9;
265 s++;
266 continue;
267 } else if (c == 'v') {
268 *r++ = 11;
269 s++;
270 continue;
271 } else if (c >= '0' && c <= '9') {
272 char d = c-'0';
273 s++;
274 c = *(s+1);
275 if (c >= '0' && c <= '9') {
276 d = (d*8)+(c-'0');
277 s++;
278 c = *(s+1);
279 if (c >= '0' && c <= '9') {
280 d = (d*8)+(c-'0');
281 s++;
282 }
283 }
284 *r++ = d;
285 continue;
286 } else {
287 *r++ = '\\';
288 }
289 } else if (*s == '?') {
290 *r++ = '\\';
291 *r++ = '\\';
292 } else if (*s == '*' || *s == '[') {
293 *r++ = '\\';
294 }
295 *r++ = *s;
296 }
297 *r = '\0';
298
299 return result;
300 }
301
302
303 int
304 main(
305 int argc,
306 char ** argv)
307 {
308 int c;
309 application_argument_t argument;
310 int i;
311 char *bsdtar_onefilesystem_value = NULL;
312
313 #ifdef BSDTAR
314 bsdtar_path = g_strdup(BSDTAR);
315 #else
316 bsdtar_path = NULL;
317 #endif
318 state_dir = NULL;
319 bsdtar_target = NULL;
320 bsdtar_onefilesystem = 1;
321 bsdtar_sparse = 1;
322 exit_handling = NULL;
323
324 glib_init();
325
326 /* initialize */
327
328 /*
329 * Configure program for internationalization:
330 * 1) Only set the message locale for now.
331 * 2) Set textdomain for all amanda related programs to "amanda"
332 * We don't want to be forced to support dozens of message catalogs.
333 */
334 setlocale(LC_MESSAGES, "C");
335 textdomain("amanda");
336
337 if (argc < 2) {
338 printf("ERROR no command given to ambsdtar\n");
339 error(_("No command given to ambsdtar"));
340 }
341
342 /* drop root privileges */
343 if (!set_root_privs(0)) {
344 if (g_str_equal(argv[1], "selfcheck")) {
345 printf("ERROR ambsdtar must be run setuid root\n");
346 }
347 error(_("ambsdtar must be run setuid root"));
348 }
349
350 safe_fd(3, 2);
351
352 set_pname("ambsdtar");
353 set_pcomponent("application");
354 set_pmodule("ambsdtar");
355
356 /* Don't die when child closes pipe */
357 signal(SIGPIPE, SIG_IGN);
358
359 #if defined(USE_DBMALLOC)
360 malloc_size_1 = malloc_inuse(&malloc_hist_1);
361 #endif
362
363 add_amanda_log_handler(amanda_log_stderr);
364 add_amanda_log_handler(amanda_log_syslog);
365 dbopen(DBG_SUBDIR_CLIENT);
366 startclock();
367 g_debug(_("version %s"), VERSION);
368
369 config_init(CONFIG_INIT_CLIENT|CONFIG_INIT_GLOBAL, NULL);
370
371 //check_running_as(RUNNING_AS_DUMPUSER_PREFERRED);
372 //root for amrecover
373 //RUNNING_AS_CLIENT_LOGIN from selfcheck, sendsize, sendbackup
374
375 /* parse argument */
376 command = argv[1];
377
378 if (strcasecmp(command,"selfcheck") == 0) {
379 fprintf(stdout, "MESSAGE JSON\n");
380 }
381
382 argument.config = NULL;
383 argument.host = NULL;
384 argument.message = 0;
385 argument.collection = 0;
386 argument.calcsize = 0;
387 argument.tar_blocksize = NULL;
388 argument.level = NULL;
389 argument.command_options = NULL;
390 argument.verbose = 0;
391 init_dle(&argument.dle);
392 argument.dle.record = 0;
393
394 while (1) {
395 int option_index = 0;
396 c = getopt_long (argc, argv, "", long_options, &option_index);
397 if (c == -1) {
398 break;
399 }
400 switch (c) {
401 case 1: amfree(argument.config);
402 argument.config = g_strdup(optarg);
403 break;
404 case 2: amfree(argument.host);
405 argument.host = g_strdup(optarg);
406 break;
407 case 3: amfree(argument.dle.disk);
408 argument.dle.disk = g_strdup(optarg);
409 break;
410 case 4: amfree(argument.dle.device);
411 argument.dle.device = g_strdup(optarg);
412 break;
413 case 5: argument.level = g_slist_append(argument.level,
414 GINT_TO_POINTER(atoi(optarg)));
415 break;
416 case 6: argument.dle.create_index = 1;
417 break;
418 case 7: argument.message = 1;
419 break;
420 case 8: argument.collection = 1;
421 break;
422 case 9: argument.dle.record = 1;
423 break;
424 case 10: amfree(bsdtar_path);
425 bsdtar_path = g_strdup(optarg);
426 break;
427 case 11: amfree(state_dir);
428 state_dir = g_strdup(optarg);
429 break;
430 case 12: amfree(bsdtar_onefilesystem_value);
431 bsdtar_onefilesystem_value = g_strdup(optarg);
432 break;
433 case 16: argument.dle.include_file =
434 append_sl(argument.dle.include_file, optarg);
435 break;
436 case 17: argument.dle.include_list =
437 append_sl(argument.dle.include_list, optarg);
438 break;
439 case 18: argument.dle.include_optional = 1;
440 break;
441 case 19: argument.dle.exclude_file =
442 append_sl(argument.dle.exclude_file, optarg);
443 break;
444 case 20: argument.dle.exclude_list =
445 append_sl(argument.dle.exclude_list, optarg);
446 break;
447 case 21: argument.dle.exclude_optional = 1;
448 break;
449 case 22: amfree(bsdtar_target);
450 bsdtar_target = g_strdup(optarg);
451 break;
452 case 23: normal_message =
453 g_slist_append(normal_message, optarg);
454 break;
455 case 24: ignore_message =
456 g_slist_append(ignore_message, optarg);
457 break;
458 case 25: strange_message =
459 g_slist_append(strange_message, optarg);
460 break;
461 case 26: amfree(exit_handling);
462 exit_handling = g_strdup(optarg);
463 break;
464 case 27: argument.calcsize = 1;
465 break;
466 case 28: amfree(argument.tar_blocksize);
467 argument.tar_blocksize = g_strdup(optarg);
468 gblocksize = atoi(argument.tar_blocksize);
469 break;
470 case 33: argument.command_options =
471 g_slist_append(argument.command_options,
472 g_strdup(optarg));
473 break;
474 case 36: if (strcasecmp(optarg, "YES") == 0)
475 argument.verbose = 1;
476 break;
477 case 37: if (strcasecmp(optarg, "NO") == 0)
478 bsdtar_sparse = 0;
479 break;
480 case 38: amfree(bsdtar_target);
481 bsdtar_target = g_strdup(optarg);
482 break;
483 case ':':
484 case '?':
485 break;
486 }
487 }
488
489 if (g_str_equal(command, "backup")) {
490 mesgstream = fdopen(3, "w");
491 if (!mesgstream) {
492 error(_("error mesgstream(%d): %s\n"), 3, strerror(errno));
493 }
494 }
495
496 if (!argument.dle.disk && argument.dle.device)
497 argument.dle.disk = g_strdup(argument.dle.device);
498 if (!argument.dle.device && argument.dle.disk)
499 argument.dle.device = g_strdup(argument.dle.disk);
500
501 if (bsdtar_onefilesystem_value) {
502 if (strcasecmp(bsdtar_onefilesystem_value, "NO") == 0) {
503 bsdtar_onefilesystem = 0;
504 } else if (strcasecmp(bsdtar_onefilesystem_value, "YES") == 0) {
505 bsdtar_onefilesystem = 1;
506 } else {
507 delete_message(ambsdtar_print_message(build_message(
508 AMANDA_FILE, __LINE__, 3702007, MSG_ERROR, 4,
509 "value", bsdtar_onefilesystem_value,
510 "disk", argument.dle.disk,
511 "device", argument.dle.device,
512 "hostname", argument.host)));
513 }
514 }
515
516 argument.argc = argc - optind;
517 argument.argv = argv + optind;
518
519 if (argument.config) {
520 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
521 argument.config);
522 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
523 }
524
525 if (config_errors(NULL) >= CFGERR_ERRORS) {
526 g_critical(_("errors processing config file"));
527 }
528
529 if (state_dir && strlen(state_dir) == 0)
530 amfree(state_dir);
531 if (!state_dir) {
532 state_dir = g_strdup_printf("%s/%s", amdatadir, "bsdtar");
533 }
534
535 re_table = build_re_table(init_re_table, normal_message, ignore_message,
536 strange_message);
537
538 for(i=0;i<256;i++)
539 exit_value[i] = 1; /* BAD */
540 exit_value[0] = 0; /* GOOD */
541 if (exit_handling) {
542 char *s = exit_handling;
543 while (s) {
544 char *r = strchr(s, '=');
545 if (r) {
546 int j = atoi(s);
547 if (j >= 0 && j < 256) {
548 r++;
549 if (strncasecmp(r, "GOOD", 4) == 0) {
550 exit_value[j] = 0;
551 }
552 }
553 }
554 s = strchr(s+1, ' ');
555 }
556 }
557
558 if (bsdtar_path) {
559 g_debug("BSDTAR-PATH %s", bsdtar_path);
560 } else {
561 g_debug("BSDTAR-PATH is not set");
562 }
563 if (state_dir) {
564 g_debug("STATE-DIR %s", state_dir);
565 } else {
566 g_debug("STATE-DIR is not set");
567 }
568 if (bsdtar_target) {
569 g_debug("TARGET %s", bsdtar_target);
570 }
571 g_debug("ONE-FILE-SYSTEM %s", bsdtar_onefilesystem? "yes":"no");
572 {
573 amregex_t *rp;
574 for (rp = re_table; rp->regex != NULL; rp++) {
575 switch (rp->typ) {
576 case DMP_NORMAL : g_debug("NORMAL %s", rp->regex); break;
577 case DMP_IGNORE : g_debug("IGNORE %s", rp->regex); break;
578 case DMP_STRANGE: g_debug("STRANGE %s", rp->regex); break;
579 case DMP_SIZE : g_debug("SIZE %s", rp->regex); break;
580 case DMP_ERROR : g_debug("ERROR %s", rp->regex); break;
581 }
582 }
583 }
584
585 if (g_str_equal(command, "support")) {
586 ambsdtar_support(&argument);
587 } else if (g_str_equal(command, "selfcheck")) {
588 ambsdtar_selfcheck(&argument);
589 } else if (g_str_equal(command, "estimate")) {
590 ambsdtar_estimate(&argument);
591 } else if (g_str_equal(command, "backup")) {
592 ambsdtar_backup(&argument);
593 } else if (g_str_equal(command, "restore")) {
594 ambsdtar_restore(&argument);
595 } else if (g_str_equal(command, "validate")) {
596 ambsdtar_validate(&argument);
597 } else if (g_str_equal(command, "index")) {
598 ambsdtar_index(&argument);
599 } else {
600 g_debug("Unknown command `%s'.", command);
601 fprintf(stderr, "Unknown command `%s'.\n", command);
602 exit (1);
603 }
604
605 g_free(argument.config);
606 g_free(argument.host);
607 g_free(argument.dle.disk);
608 g_free(argument.dle.device);
609 g_free(argument.tar_blocksize);
610 g_slist_free(argument.level);
611
612 dbclose();
613
614 return ambsdtar_exit_value;
615 }
616
617 static char *validate_command_options(
618 application_argument_t *argument)
619 {
620 GSList *copt;
621
622 for (copt = argument->command_options; copt != NULL; copt = copt->next) {
623 char *opt = (char *)copt->data;
624
625 if (g_str_has_prefix(opt, "--use-compress-program")) {
626 return opt;
627 }
628 }
629
630 return NULL;
631 }
632
633 static void
634 ambsdtar_support(
635 application_argument_t *argument)
636 {
637 (void)argument;
638 fprintf(stdout, "CONFIG YES\n");
639 fprintf(stdout, "HOST YES\n");
640 fprintf(stdout, "DISK YES\n");
641 fprintf(stdout, "MAX-LEVEL 399\n");
642 fprintf(stdout, "INDEX-LINE YES\n");
643 fprintf(stdout, "INDEX-XML NO\n");
644 fprintf(stdout, "MESSAGE-LINE YES\n");
645 fprintf(stdout, "MESSAGE-SELFCHECK-JSON YES\n");
646 fprintf(stdout, "MESSAGE-XML NO\n");
647 fprintf(stdout, "RECORD YES\n");
648 fprintf(stdout, "INCLUDE-FILE YES\n");
649 fprintf(stdout, "INCLUDE-LIST YES\n");
650 fprintf(stdout, "INCLUDE-OPTIONAL YES\n");
651 fprintf(stdout, "EXCLUDE-FILE YES\n");
652 fprintf(stdout, "EXCLUDE-LIST YES\n");
653 fprintf(stdout, "EXCLUDE-OPTIONAL YES\n");
654 fprintf(stdout, "COLLECTION NO\n");
655 fprintf(stdout, "MULTI-ESTIMATE YES\n");
656 fprintf(stdout, "CALCSIZE YES\n");
657 fprintf(stdout, "CLIENT-ESTIMATE YES\n");
658 }
659
660 static void
661 ambsdtar_selfcheck(
662 application_argument_t *argument)
663 {
664 char *option;
665 messagelist_t mlist = NULL;
666 messagelist_t mesglist = NULL;
667 char *dirname;
668
669 if (argument->dle.disk) {
670 delete_message(ambsdtar_print_message(build_message(
671 AMANDA_FILE, __LINE__, 3702000, MSG_INFO, 3,
672 "disk", argument->dle.disk,
673 "device", argument->dle.device,
674 "hostname", argument->host)));
675 }
676
677 delete_message(ambsdtar_print_message(build_message(
678 AMANDA_FILE, __LINE__, 3702001, MSG_INFO, 4,
679 "version", VERSION,
680 "disk", argument->dle.disk,
681 "device", argument->dle.device,
682 "hostname", argument->host)));
683
684 if (bsdtar_target) {
685 dirname = bsdtar_target;
686 } else {
687 dirname = argument->dle.device;
688 }
689
690 ambsdtar_build_exinclude(&argument->dle, NULL, NULL, NULL, NULL,
691 dirname, &mlist);
692 for (mesglist = mlist; mesglist != NULL; mesglist = mesglist->next){
693 message_t *message = mesglist->data;
694 if (message_get_severity(message) > MSG_INFO)
695 ambsdtar_print_message(message);
696 delete_message(message);
697 }
698 g_slist_free(mlist);
699
700 delete_message(ambsdtar_print_message(build_message(
701 AMANDA_FILE, __LINE__, 3702004, MSG_INFO, 3,
702 "disk", argument->dle.disk,
703 "device", argument->dle.device,
704 "hostname", argument->host)));
705
706 if ((option = validate_command_options(argument))) {
707 delete_message(ambsdtar_print_message(build_message(
708 AMANDA_FILE, __LINE__, 3702014, MSG_ERROR, 4,
709 "disk", argument->dle.disk,
710 "device", argument->dle.device,
711 "hostname", argument->host,
712 "command-options", option)));
713 }
714
715 if (bsdtar_path) {
716 char *bsdtar_realpath;
717 message_t *message;
718 if ((message = check_exec_for_suid_message("BSDTAR_PATH", bsdtar_path, &bsdtar_realpath))) {
719 delete_message(ambsdtar_print_message(message));
720 } else {
721 message = ambsdtar_print_message(check_file_message(bsdtar_path, X_OK));
722 if (message && message_get_severity(message) <= MSG_INFO) {
723 } else {
724 char *bsdtar_version;
725 GPtrArray *argv_ptr = g_ptr_array_new();
726
727 g_ptr_array_add(argv_ptr, bsdtar_realpath);
728 g_ptr_array_add(argv_ptr, "--version");
729 g_ptr_array_add(argv_ptr, NULL);
730
731 bsdtar_version = get_first_line(argv_ptr);
732 if (bsdtar_version) {
733 char *tv, *bv;
734 for (tv = bsdtar_version; *tv && !g_ascii_isdigit(*tv); tv++);
735 for (bv = tv; *bv && *bv != ' '; bv++);
736 if (*bv) *bv = '\0';
737 delete_message(ambsdtar_print_message(build_message(
738 AMANDA_FILE, __LINE__, 3702002, MSG_INFO, 4,
739 "bsdtar-version", bv,
740 "disk", argument->dle.disk,
741 "device", argument->dle.device,
742 "hostname", argument->host)));
743 } else {
744 delete_message(ambsdtar_print_message(build_message(
745 AMANDA_FILE, __LINE__, 3702003, MSG_ERROR, 4,
746 "bsdtar-path", bsdtar_path,
747 "disk", argument->dle.disk,
748 "device", argument->dle.device,
749 "hostname", argument->host)));
750 }
751
752 g_ptr_array_free(argv_ptr, TRUE);
753 amfree(bsdtar_version);
754 }
755 if (message)
756 delete_message(message);
757 }
758 amfree(bsdtar_realpath);
759 } else {
760 delete_message(ambsdtar_print_message(build_message(
761 AMANDA_FILE, __LINE__, 3702005, MSG_ERROR, 3,
762 "disk", argument->dle.disk,
763 "device", argument->dle.device,
764 "hostname", argument->host)));
765 }
766
767 set_root_privs(1);
768 if (state_dir && strlen(state_dir) == 0)
769 state_dir = NULL;
770 if (state_dir) {
771 delete_message(ambsdtar_print_message(check_dir_message(state_dir, R_OK|W_OK)));
772 } else {
773 delete_message(ambsdtar_print_message(build_message(
774 AMANDA_FILE, __LINE__, 3702020, MSG_ERROR, 3,
775 "disk", argument->dle.disk,
776 "device", argument->dle.device,
777 "hostname", argument->host)));
778 }
779
780 if (bsdtar_target) {
781 delete_message(ambsdtar_print_message(check_dir_message(bsdtar_target, R_OK)));
782 } else if (argument->dle.device) {
783 delete_message(ambsdtar_print_message(check_dir_message(argument->dle.device, R_OK)));
784 }
785
786 if (argument->calcsize) {
787 char *calcsize = g_strjoin(NULL, amlibexecdir, "/", "calcsize", NULL);
788 delete_message(ambsdtar_print_message(check_file_message(calcsize, X_OK)));
789 delete_message(ambsdtar_print_message(check_suid_message(calcsize)));
790 amfree(calcsize);
791 }
792 set_root_privs(0);
793 }
794
795 static void
796 ambsdtar_estimate(
797 application_argument_t *argument)
798 {
799 char *incrname = NULL;
800 GPtrArray *argv_ptr;
801 int nullfd = -1;
802 int pipefd = -1;
803 FILE *dumpout = NULL;
804 off_t size = -1;
805 char line[32768];
806 char *errmsg = NULL;
807 char *qerrmsg = NULL;
808 char *qdisk = NULL;
809 amwait_t wait_status;
810 int tarpid;
811 amregex_t *rp;
812 times_t start_time;
813 int level;
814 GSList *levels;
815 char *file_exclude = NULL;
816 char *file_include = NULL;
817 GString *strbuf;
818 char *option;
819 char *bsdtar_realpath;
820 message_t *message;
821
822 if (!argument->level) {
823 fprintf(stderr, "ERROR No level argument\n");
824 error(_("No level argument"));
825 }
826 if (!argument->dle.disk) {
827 fprintf(stderr, "ERROR No disk argument\n");
828 error(_("No disk argument"));
829 }
830 if (!argument->dle.device) {
831 fprintf(stderr, "ERROR No device argument\n");
832 error(_("No device argument"));
833 }
834
835 if ((option = validate_command_options(argument))) {
836 fprintf(stdout, "ERROR Invalid '%s' COMMAND-OPTIONS\n", option);
837 error("Invalid '%s' COMMAND-OPTIONS", option);
838 }
839
840 if (state_dir && strlen(state_dir) == 0)
841 state_dir = NULL;
842 if (state_dir) {
843 message = check_dir_message(state_dir, R_OK|W_OK);
844 if (message) {
845 if (message_get_severity(message) > MSG_INFO) {
846 ambsdtar_print_message(message);
847 }
848 delete_message(message);
849 }
850 } else {
851 fprintf(mesgstream, "ERROR No STATE-DIR\n");
852 exit(1);
853 }
854
855 if (argument->calcsize) {
856 char *dirname;
857 int nb_exclude;
858 int nb_include;
859 messagelist_t mlist = NULL;
860 messagelist_t mesglist = NULL;
861
862 if (bsdtar_target) {
863 dirname = bsdtar_target;
864 } else {
865 dirname = argument->dle.device;
866 }
867 ambsdtar_build_exinclude(&argument->dle,
868 &nb_exclude, &file_exclude,
869 &nb_include, &file_include, dirname, &mlist);
870 for (mesglist = mlist; mesglist != NULL; mesglist = mesglist->next){
871 message_t *message = mesglist->data;
872 if (message_get_severity(message) > MSG_INFO)
873 fprintf(stdout, "ERROR %s\n", get_message(message));
874 delete_message(message);
875 }
876 g_slist_free(mlist);
877 mlist = NULL;
878
879 run_calcsize(argument->config, "BSDTAR", argument->dle.disk, dirname,
880 argument->level, file_exclude, file_include);
881
882 if (argument->verbose == 0) {
883 if (file_exclude)
884 unlink(file_exclude);
885 if (file_include)
886 unlink(file_include);
887 }
888 amfree(file_exclude);
889 amfree(file_include);
890 amfree(qdisk);
891 return;
892 }
893
894 if (!bsdtar_path) {
895 errmsg = g_strdup(_("BSDTAR-PATH not defined"));
896 goto common_error;
897 }
898
899 if ((message = check_exec_for_suid_message("BSDTAR_PATH", bsdtar_path, &bsdtar_realpath))) {
900 errmsg = g_strdup(get_message(message));
901 delete_message(message);
902 goto common_error;
903 }
904
905 qdisk = quote_string(argument->dle.disk);
906 for (levels = argument->level; levels != NULL; levels = levels->next) {
907 char *timestamps;
908 messagelist_t mlist = NULL;
909 messagelist_t mesglist = NULL;
910
911 level = GPOINTER_TO_INT(levels->data);
912 timestamps = ambsdtar_get_timestamps(argument, level, stdout, CMD_ESTIMATE);
913 argv_ptr = ambsdtar_build_argv(bsdtar_realpath,
914 argument, timestamps, &file_exclude,
915 &file_include, CMD_ESTIMATE, &mlist);
916 for (mesglist = mlist; mesglist != NULL; mesglist = mesglist->next){
917 message_t *message = mesglist->data;
918 if (message_get_severity(message) > MSG_INFO)
919 fprintf(stdout, "ERROR %s\n", get_message(message));
920 delete_message(message);
921 }
922 g_slist_free(mlist);
923 mlist = NULL;
924
925 amfree(timestamps);
926
927 start_time = curclock();
928
929 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
930 errmsg = g_strdup_printf(_("Cannot access /dev/null : %s"),
931 strerror(errno));
932 goto common_exit;
933 }
934
935 tarpid = pipespawnv(bsdtar_realpath, STDERR_PIPE, 1,
936 &nullfd, &nullfd, &pipefd,
937 (char **)argv_ptr->pdata);
938
939 dumpout = fdopen(pipefd,"r");
940 if (!dumpout) {
941 error(_("Can't fdopen: %s"), strerror(errno));
942 /*NOTREACHED*/
943 }
944
945 size = (off_t)-1;
946 while (size < 0 && (fgets(line, sizeof(line), dumpout) != NULL)) {
947 if (strlen(line) > 0 && line[strlen(line)-1] == '\n') {
948 /* remove trailling \n */
949 line[strlen(line)-1] = '\0';
950 }
951 if (line[0] == '\0')
952 continue;
953 g_debug("%s", line);
954 /* check for size match */
955 /*@ignore@*/
956 for(rp = re_table; rp->regex != NULL; rp++) {
957 if(match(rp->regex, line)) {
958 if (rp->typ == DMP_SIZE) {
959 off_t blocksize = gblocksize;
960 size = ((the_num(line, rp->field)*rp->scale+1023.0)/1024.0);
961 if(size < 0.0)
962 size = 1.0; /* found on NeXT -- sigh */
963 if (!blocksize) {
964 blocksize = 20;
965 }
966 blocksize /= 2;
967 size = (size+blocksize-1) / blocksize;
968 size *= blocksize;
969 }
970 break;
971 }
972 }
973 /*@end@*/
974 }
975
976 while (fgets(line, sizeof(line), dumpout) != NULL) {
977 g_debug("%s", line);
978 }
979
980 g_debug(".....");
981 g_debug(_("estimate time for %s level %d: %s"),
982 qdisk,
983 level,
984 walltime_str(timessub(curclock(), start_time)));
985 if(size == (off_t)-1) {
986 errmsg = g_strdup_printf(_("no size line match in %s output"), bsdtar_path);
987 g_debug(_("%s for %s"), errmsg, qdisk);
988 g_debug(".....");
989 } else if(size == (off_t)0 && argument->level == 0) {
990 g_debug(_("possible %s problem -- is \"%s\" really empty?"),
991 bsdtar_path, argument->dle.disk);
992 g_debug(".....");
993 }
994 g_debug(_("estimate size for %s level %d: %lld KB"),
995 qdisk,
996 level,
997 (long long)size);
998
999 (void)kill(-tarpid, SIGTERM);
1000
1001 g_debug(_("waiting for %s \"%s\" child"), bsdtar_path, qdisk);
1002 waitpid(tarpid, &wait_status, 0);
1003 if (WIFSIGNALED(wait_status)) {
1004 strbuf = g_string_new(errmsg);
1005 g_string_append_printf(strbuf, "%s terminated with signal %d: see %s",
1006 bsdtar_path, WTERMSIG(wait_status), dbfn());
1007 g_free(errmsg);
1008 errmsg = g_string_free(strbuf, FALSE);
1009 ambsdtar_exit_value = 1;
1010 } else if (WIFEXITED(wait_status)) {
1011 if (exit_value[WEXITSTATUS(wait_status)] == 1) {
1012 strbuf = g_string_new(errmsg);
1013 g_string_append_printf(strbuf, "%s exited with status %d: see %s",
1014 bsdtar_path, WEXITSTATUS(wait_status), dbfn());
1015 g_free(errmsg);
1016 errmsg = g_string_free(strbuf, FALSE);
1017 ambsdtar_exit_value = 1;
1018 } else {
1019 /* Normal exit */
1020 }
1021 } else {
1022 errmsg = g_strdup_printf(_("%s got bad exit: see %s"),
1023 bsdtar_path, dbfn());
1024 ambsdtar_exit_value = 1;
1025 }
1026 g_debug(_("after %s %s wait"), bsdtar_path, qdisk);
1027
1028 common_exit:
1029 if (errmsg) {
1030 g_debug("%s", errmsg);
1031 fprintf(stdout, "ERROR %s\n", errmsg);
1032 amfree(errmsg);
1033 }
1034
1035 if (argument->verbose == 0) {
1036 if (file_exclude)
1037 unlink(file_exclude);
1038 if (file_include)
1039 unlink(file_include);
1040 }
1041
1042 g_ptr_array_free_full(argv_ptr);
1043 amfree(bsdtar_path);
1044 amfree(incrname);
1045
1046 aclose(nullfd);
1047 afclose(dumpout);
1048
1049 fprintf(stdout, "%d %lld 1\n", level, (long long)size);
1050 }
1051 amfree(qdisk);
1052 amfree(file_exclude);
1053 amfree(file_include);
1054 amfree(errmsg);
1055 amfree(qerrmsg);
1056 amfree(incrname);
1057 return;
1058
1059 common_error:
1060 qerrmsg = quote_string(errmsg);
1061 amfree(qdisk);
1062 g_debug("%s", errmsg);
1063 fprintf(stdout, "ERROR %s\n", qerrmsg);
1064 ambsdtar_exit_value = 1;
1065 amfree(file_exclude);
1066 amfree(file_include);
1067 amfree(errmsg);
1068 amfree(qerrmsg);
1069 amfree(incrname);
1070 return;
1071 }
1072
1073 typedef struct filter_s {
1074 int fd;
1075 char *name;
1076 char *buffer;
1077 gint64 first; /* first byte used */
1078 gint64 size; /* number of byte use in the buffer */
1079 gint64 allocated_size; /* allocated size of the buffer */
1080 event_handle_t *event;
1081 int out;
1082 } filter_t;
1083
1084 static off_t dump_size = -1;
1085 static int dataf = 1;
1086 static int index_in;
1087 static FILE *indexstream = NULL;
1088
1089 static void read_fd(int fd, char *name, event_fn_t fn);
1090 static void read_data_out(void *cookie);
1091 static void read_text(void *cookie);
1092
1093 static void
1094 read_fd(
1095 int fd,
1096 char *name,
1097 event_fn_t fn)
1098 {
1099 filter_t *filter = g_new0(filter_t, 1);
1100 filter->fd = fd;
1101 filter->name = g_strdup(name);
1102 filter->event = event_create((event_id_t)filter->fd, EV_READFD,
1103 fn, filter);
1104 event_activate(filter->event);
1105 }
1106
1107 static void
1108 read_data_out(
1109 void *cookie)
1110 {
1111 filter_t *filter = cookie;
1112 ssize_t nread;
1113 ssize_t nwrite;
1114
1115 if (!filter->buffer) {
1116 filter->buffer = malloc(32768);
1117 }
1118 nread = read(filter->fd, filter->buffer, 32768);
1119 if (nread <= 0) {
1120 aclose(filter->fd);
1121 aclose(dataf);
1122 aclose(index_in);
1123 event_release(filter->event);
1124 if (nread < 0) {
1125 }
1126 g_free(filter->buffer);
1127 g_free(filter->name);
1128 g_free(filter);
1129 } else {
1130 nwrite = full_write(dataf, filter->buffer, nread);
1131 if (nwrite != nread) {
1132 }
1133 nwrite = full_write(index_in, filter->buffer, nread);
1134 if (nwrite != nread) {
1135 }
1136 }
1137 }
1138
1139 static void
1140 read_text(
1141 void *cookie)
1142 {
1143 filter_t *filter = cookie;
1144 char *line;
1145 char *p;
1146 amregex_t *rp;
1147 char *type;
1148 char startchr;
1149 ssize_t nread;
1150 int len;
1151
1152 if (filter->buffer == NULL) {
1153 /* allocate initial buffer */
1154 filter->buffer = g_malloc(32768);
1155 filter->first = 0;
1156 filter->size = 0;
1157 filter->allocated_size = 32768;
1158 } else if (filter->first > 0) {
1159 if (filter->allocated_size - filter->size - filter->first < 1024) {
1160 memmove(filter->buffer, filter->buffer + filter->first,
1161 filter->size);
1162 filter->first = 0;
1163 }
1164 } else if (filter->allocated_size - filter->size < 1024) {
1165 /* double the size of the buffer */
1166 filter->allocated_size *= 2;
1167 filter->buffer = g_realloc(filter->buffer, filter->allocated_size);
1168 }
1169
1170 nread = read(filter->fd, filter->buffer + filter->first + filter->size,
1171 filter->allocated_size - filter->first - filter->size - 2);
1172
1173 if (nread <= 0) {
1174 event_release(filter->event);
1175 aclose(filter->fd);
1176 if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') {
1177 /* Add a '\n' at end of buffer */
1178 filter->buffer[filter->first + filter->size] = '\n';
1179 filter->size++;
1180 }
1181 } else {
1182 filter->size += nread;
1183 }
1184
1185 /* process all complete lines */
1186 line = filter->buffer + filter->first;
1187 line[filter->size] = '\0';
1188 while (line < filter->buffer + filter->first + filter->size &&
1189 (p = strchr(line, '\n')) != NULL) {
1190 *p = '\0';
1191 if (g_str_equal(filter->name, "data err")) {
1192 g_debug("data err: %s", line);
1193 for(rp = re_table; rp->regex != NULL; rp++) {
1194 if(match(rp->regex, line)) {
1195 break;
1196 }
1197 }
1198 if(rp->typ == DMP_SIZE) {
1199 off_t blocksize = gblocksize;
1200 dump_size = (off_t)((the_num(line, rp->field)* rp->scale+1023.0)/1024.0);
1201 if(dump_size < 0.0)
1202 dump_size = 1.0; /* found on NeXT -- sigh */
1203 if (!blocksize) {
1204 blocksize = 20;
1205 }
1206 blocksize /= 2;
1207 dump_size = (dump_size+blocksize-1) / blocksize;
1208 dump_size *= blocksize;
1209 }
1210 switch(rp->typ) {
1211 case DMP_NORMAL:
1212 type = "normal";
1213 startchr = '|';
1214 break;
1215 case DMP_IGNORE:
1216 continue;
1217 case DMP_STRANGE:
1218 type = "strange";
1219 startchr = '?';
1220 break;
1221 case DMP_SIZE:
1222 type = "size";
1223 startchr = '|';
1224 break;
1225 case DMP_ERROR:
1226 type = "error";
1227 startchr = '?';
1228 break;
1229 default:
1230 type = "unknown";
1231 startchr = '!';
1232 break;
1233 }
1234 g_debug("%3d: %7s(%c): %s", rp->srcline, type, startchr, line);
1235 fprintf(mesgstream,"%c %s\n", startchr, line);
1236 } else if (g_str_equal(filter->name, "index out")) {
1237 g_fprintf(indexstream, "%s\n", line+1); // remove initial '.'
1238 } else if (g_str_equal(filter->name, "index err")) {
1239 g_debug("index err: %s", line);
1240 g_fprintf(mesgstream, "? %s\n", line);
1241 } else {
1242 g_debug("unknown: %s", line);
1243 }
1244 len = p - line + 1;
1245 filter->first += len;
1246 filter->size -= len;
1247 line = p + 1;
1248 }
1249
1250 if (nread <= 0) {
1251 g_free(filter->name);
1252 g_free(filter->buffer);
1253 g_free(filter);
1254 }
1255
1256 }
1257 static void
1258 ambsdtar_backup(
1259 application_argument_t *argument)
1260 {
1261 int dumpin;
1262 char *qdisk;
1263 char *timestamps;
1264 int indexf = 4;
1265 int outf;
1266 int data_out;
1267 int index_out;
1268 int index_err;
1269 char *errmsg = NULL;
1270 amwait_t wait_status;
1271 GPtrArray *argv_ptr;
1272 pid_t tarpid;
1273 pid_t indexpid = 0;
1274 time_t tt;
1275 char *file_exclude;
1276 char *file_include;
1277 char new_timestamps[64];
1278 char *option;
1279 char *bsdtar_realpath;
1280 messagelist_t mlist = NULL;
1281 messagelist_t mesglist = NULL;
1282 message_t *message;
1283
1284 if (!bsdtar_path) {
1285 fprintf(mesgstream, "sendbackup: error [BSDTAR-PATH not defined]\n");
1286 exit(1);
1287 }
1288
1289 if ((message = check_exec_for_suid_message("BSDTAR_PATH", bsdtar_path, &bsdtar_realpath))) {
1290 fprintf(mesgstream, "sendbackup: error [%s]", get_message(message));
1291 exit(1);
1292 }
1293
1294 if ((option = validate_command_options(argument))) {
1295 fprintf(mesgstream, "sendbackup: error [Invalid '%s' COMMAND-OPTIONS]\n", option);
1296 exit(1);
1297 }
1298
1299 if (state_dir && strlen(state_dir) == 0)
1300 state_dir = NULL;
1301 if (state_dir) {
1302 message = check_dir_message(state_dir, R_OK|W_OK);
1303 if (message) {
1304 if (message_get_severity(message) > MSG_INFO) {
1305 ambsdtar_print_message(message);
1306 }
1307 delete_message(message);
1308 }
1309 } else {
1310 fprintf(mesgstream, "sendbackup: error [STATE-DIR not defined]\n");
1311 exit(1);
1312 }
1313
1314 if (!argument->level) {
1315 fprintf(mesgstream, "sendbackup: error [No level argument]\n");
1316 exit(1);
1317 }
1318 if (!argument->dle.disk) {
1319 fprintf(mesgstream, "sendbackup: error [No disk argument]\n");
1320 exit(1);
1321 }
1322 if (!argument->dle.device) {
1323 fprintf(mesgstream, "sendbackup: error [No device argument]\n");
1324 exit(1);
1325 }
1326
1327 qdisk = quote_string(argument->dle.disk);
1328
1329 tt = time(NULL);
1330 ctime_r(&tt, new_timestamps);
1331 new_timestamps[strlen(new_timestamps)-1] = '\0';
1332 timestamps = ambsdtar_get_timestamps(argument,
1333 GPOINTER_TO_INT(argument->level->data),
1334 mesgstream, CMD_BACKUP);
1335 argv_ptr = ambsdtar_build_argv(bsdtar_realpath,
1336 argument, timestamps, &file_exclude,
1337 &file_include, CMD_BACKUP, &mlist);
1338 for (mesglist = mlist; mesglist != NULL; mesglist = mesglist->next){
1339 message_t *message = mesglist->data;
1340 if (message_get_severity(message) <= MSG_INFO) {
1341 fprintf(mesgstream, "| %s\n", get_message(message));
1342 } else {
1343 fprintf(mesgstream, "? %s\n", get_message(message));
1344 }
1345 delete_message(message);
1346 }
1347 g_slist_free(mlist);
1348 mlist = NULL;
1349
1350 if (argument->dle.create_index) {
1351 tarpid = pipespawnv(bsdtar_path, STDIN_PIPE|STDOUT_PIPE|STDERR_PIPE, 1,
1352 &dumpin, &data_out, &outf, (char **)argv_ptr->pdata);
1353 g_ptr_array_free_full(argv_ptr);
1354 argv_ptr = g_ptr_array_new();
1355 g_ptr_array_add(argv_ptr, g_strdup(bsdtar_path));
1356 g_ptr_array_add(argv_ptr, g_strdup("tf"));
1357 g_ptr_array_add(argv_ptr, g_strdup("-"));
1358 g_ptr_array_add(argv_ptr, NULL);
1359 indexpid = pipespawnv(bsdtar_path, STDIN_PIPE|STDOUT_PIPE|STDERR_PIPE, 1,
1360 &index_in, &index_out, &index_err,
1361 (char **)argv_ptr->pdata);
1362 g_ptr_array_free_full(argv_ptr);
1363
1364 aclose(dumpin);
1365
1366 indexstream = fdopen(indexf, "w");
1367 if (!indexstream) {
1368 error(_("error indexstream(%d): %s\n"), indexf, strerror(errno));
1369 }
1370 read_fd(data_out , "data out" , &read_data_out);
1371 read_fd(outf , "data err" , &read_text);
1372 read_fd(index_out, "index out", &read_text);
1373 read_fd(index_err, "index err", &read_text);
1374 } else {
1375 tarpid = pipespawnv(bsdtar_path, STDIN_PIPE|STDERR_PIPE, 1,
1376 &dumpin, &dataf, &outf, (char **)argv_ptr->pdata);
1377 g_ptr_array_free_full(argv_ptr);
1378 aclose(dumpin);
1379 aclose(dataf);
1380 read_fd(outf, "data err", &read_text);
1381 }
1382
1383 event_loop(0);
1384
1385 waitpid(tarpid, &wait_status, 0);
1386 if (WIFSIGNALED(wait_status)) {
1387 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
1388 bsdtar_path, WTERMSIG(wait_status), dbfn());
1389 ambsdtar_exit_value = 1;
1390 } else if (WIFEXITED(wait_status)) {
1391 if (exit_value[WEXITSTATUS(wait_status)] == 1) {
1392 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
1393 bsdtar_path, WEXITSTATUS(wait_status), dbfn());
1394 ambsdtar_exit_value = 1;
1395 } else {
1396 /* Normal exit */
1397 }
1398 } else {
1399 errmsg = g_strdup_printf(_("%s got bad exit: see %s"),
1400 bsdtar_path, dbfn());
1401 ambsdtar_exit_value = 1;
1402 }
1403 if (argument->dle.create_index) {
1404 waitpid(indexpid, &wait_status, 0);
1405 if (WIFSIGNALED(wait_status)) {
1406 errmsg = g_strdup_printf(_("'%s index' terminated with signal %d: see %s"),
1407 bsdtar_path, WTERMSIG(wait_status), dbfn());
1408 ambsdtar_exit_value = 1;
1409 } else if (WIFEXITED(wait_status)) {
1410 if (exit_value[WEXITSTATUS(wait_status)] == 1) {
1411 errmsg = g_strdup_printf(_("'%s index' exited with status %d: see %s"),
1412 bsdtar_path, WEXITSTATUS(wait_status), dbfn());
1413 ambsdtar_exit_value = 1;
1414 } else {
1415 /* Normal exit */
1416 }
1417 } else {
1418 errmsg = g_strdup_printf(_("'%s index' got bad exit: see %s"),
1419 bsdtar_path, dbfn());
1420 ambsdtar_exit_value = 1;
1421 }
1422 }
1423 g_debug(_("after %s %s wait"), bsdtar_path, qdisk);
1424 g_debug(_("ambsdtar: %s: pid %ld"), bsdtar_path, (long)tarpid);
1425 if (errmsg) {
1426 g_debug("%s", errmsg);
1427 g_fprintf(mesgstream, "sendbackup: error [%s]\n", errmsg);
1428 ambsdtar_exit_value = 1;
1429 } else if (argument->dle.record) {
1430 ambsdtar_set_timestamps(argument,
1431 GPOINTER_TO_INT(argument->level->data),
1432 new_timestamps,
1433 mesgstream);
1434 }
1435
1436
1437 g_debug("sendbackup: size %lld", (long long)dump_size);
1438 fprintf(mesgstream, "sendbackup: size %lld\n", (long long)dump_size);
1439
1440 if (argument->dle.create_index)
1441 fclose(indexstream);
1442
1443 fclose(mesgstream);
1444
1445 if (argument->verbose == 0) {
1446 if (file_exclude)
1447 unlink(file_exclude);
1448 if (file_include)
1449 unlink(file_include);
1450 }
1451
1452 amfree(file_exclude);
1453 amfree(file_include);
1454 amfree(timestamps);
1455 amfree(qdisk);
1456 amfree(bsdtar_path);
1457 amfree(errmsg);
1458 }
1459
1460 static ssize_t
1461 read_filter_buffer(
1462 filter_t *filter)
1463 {
1464 ssize_t nread;
1465
1466 if (filter->buffer == NULL) {
1467 /* allocate initial buffer */
1468 filter->buffer = g_malloc(2048);
1469 filter->first = 0;
1470 filter->size = 0;
1471 filter->allocated_size = 2048;
1472 } else if (filter->first > 0) {
1473 if (filter->allocated_size - filter->size - filter->first < 1024) {
1474 memmove(filter->buffer, filter->buffer + filter->first,
1475 filter->size);
1476 filter->first = 0;
1477 }
1478 } else if (filter->allocated_size - filter->size < 1024) {
1479 /* double the size of the buffer */
1480 filter->allocated_size *= 2;
1481 filter->buffer = g_realloc(filter->buffer, filter->allocated_size);
1482 }
1483
1484 nread = read(filter->fd, filter->buffer + filter->first + filter->size,
1485 filter->allocated_size - filter->first - filter->size - 2);
1486
1487 if (nread <= 0) {
1488 event_release(filter->event);
1489 aclose(filter->fd);
1490 if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') {
1491 /* Add a '\n' at end of buffer */
1492 filter->buffer[filter->first + filter->size] = '\n';
1493 filter->size++;
1494 }
1495 } else {
1496 filter->size += nread;
1497 }
1498
1499 return nread;
1500 }
1501
1502 static void
1503 handle_restore_stdin(
1504 void *cookie)
1505 {
1506 filter_t *filter = cookie;
1507 ssize_t nread;
1508
1509 if (filter->buffer == NULL) {
1510 /* allocate initial buffer */
1511 filter->buffer = g_malloc(65536);
1512 filter->first = 0;
1513 filter->size = 0;
1514 filter->allocated_size = 65536;
1515 }
1516 nread = read(filter->fd, filter->buffer, filter->allocated_size);
1517
1518 if (nread > 0) {
1519 /* process the complete buffer */
1520 int nwrite = full_write(filter->out , filter->buffer, nread);
1521 if (nwrite < nread) {
1522 ambsdtar_exit_value = 1;
1523 g_debug("wrote only %d bytes", nwrite);
1524 event_release(filter->event);
1525 filter->event = NULL;
1526 aclose(filter->fd);
1527 g_free(filter->buffer);
1528 filter->buffer = NULL;
1529 aclose(filter->out);
1530 }
1531 } else {
1532 event_release(filter->event);
1533 filter->event = NULL;
1534 aclose(filter->fd);
1535 g_free(filter->buffer);
1536 filter->buffer = NULL;
1537 aclose(filter->out);
1538 }
1539 }
1540
1541 static void
1542 handle_restore_stdout(
1543 void *cookie)
1544 {
1545 filter_t *filter = cookie;
1546 ssize_t nread;
1547 char *b, *p;
1548 gint64 len;
1549
1550 nread = read_filter_buffer(filter);
1551
1552 /* process all complete lines */
1553 b = filter->buffer + filter->first;
1554 b[filter->size] = '\0';
1555 while (b < filter->buffer + filter->first + filter->size &&
1556 (p = strchr(b, '\n')) != NULL) {
1557 *p = '\0';
1558 g_fprintf(stdout, "%s\n", b);
1559 len = p - b + 1;
1560 filter->first += len;
1561 filter->size -= len;
1562 b = p + 1;
1563 }
1564
1565 if (nread <= 0) {
1566 g_free(filter->buffer);
1567 }
1568 }
1569
1570 static void
1571 handle_restore_stderr(
1572 void *cookie)
1573 {
1574 filter_t *filter = cookie;
1575 ssize_t nread;
1576 char *b, *p;
1577 gint64 len;
1578
1579 nread = read_filter_buffer(filter);
1580
1581 /* process all complete lines */
1582 b = filter->buffer + filter->first;
1583 b[filter->size] = '\0';
1584 while (b < filter->buffer + filter->first + filter->size &&
1585 (p = strchr(b, '\n')) != NULL) {
1586 *p = '\0';
1587 if (*b == 'x' && *(b+1) == ' ') {
1588 g_fprintf(stdout, "%s\n", b+2);
1589 } else {
1590 gboolean line_is_error = TRUE;
1591 if (restore_ok) {
1592 if ((strncmp(b, "bsdtar: copyfile unpack (./.) failed: Operation not permitted", 61) == 0) ||
1593 (strncmp(b, "bsdtar: Error exit delayed from previous errors.", 48) == 0)) {
1594 line_is_error = FALSE;
1595 } else {
1596 restore_ok = FALSE;
1597 }
1598 }
1599 if (line_is_error) {
1600 g_fprintf(stderr, "%s\n", b);
1601 }
1602 }
1603 len = p - b + 1;
1604 filter->first += len;
1605 filter->size -= len;
1606 b = p + 1;
1607 }
1608
1609 if (nread <= 0) {
1610 g_free(filter->buffer);
1611 }
1612 }
1613
1614 static void
1615 ambsdtar_restore(
1616 application_argument_t *argument)
1617 {
1618 GPtrArray *argv_ptr = g_ptr_array_new();
1619 int j;
1620 char *include_filename = NULL;
1621 char *exclude_filename = NULL;
1622 int tarpid;
1623 filter_t in_buf;
1624 filter_t out_buf;
1625 filter_t err_buf;
1626 int tarin, tarout, tarerr;
1627 char *errmsg = NULL;
1628 amwait_t wait_status;
1629 char *bsdtar_realpath;
1630 message_t *message;
1631
1632 restore_ok = TRUE;
1633 if (!bsdtar_path) {
1634 error(_("BSDTAR-PATH not defined"));
1635 }
1636
1637 if ((message = check_exec_for_suid_message("BSDTAR_PATH", bsdtar_path, &bsdtar_realpath))) {
1638 fprintf(stderr, "%s\n", get_message(message));
1639 delete_message(message);
1640 exit(1);
1641 }
1642
1643 if (!security_allow_to_restore()) {
1644 error("The user is not allowed to restore files");
1645 }
1646
1647 g_ptr_array_add(argv_ptr, g_strdup(bsdtar_realpath));
1648 g_ptr_array_add(argv_ptr, g_strdup("--numeric-owner"));
1649 /* ignore trailing zero blocks on input (this was the default until tar-1.21) */
1650 if (argument->tar_blocksize) {
1651 g_ptr_array_add(argv_ptr, g_strdup("--block-size"));
1652 g_ptr_array_add(argv_ptr, g_strdup(argument->tar_blocksize));
1653 }
1654 g_ptr_array_add(argv_ptr, g_strdup("-xvf"));
1655 g_ptr_array_add(argv_ptr, g_strdup("-"));
1656 if (bsdtar_sparse) {
1657 g_ptr_array_add(argv_ptr, g_strdup("-S"));
1658 }
1659 if (bsdtar_target) {
1660 struct stat stat_buf;
1661 if(stat(bsdtar_target, &stat_buf) != 0) {
1662 fprintf(stderr,"can not stat directory %s: %s\n", bsdtar_target, strerror(errno));
1663 exit(1);
1664 }
1665 if (!S_ISDIR(stat_buf.st_mode)) {
1666 fprintf(stderr,"%s is not a directory\n", bsdtar_target);
1667 exit(1);
1668 }
1669 if (access(bsdtar_target, W_OK) != 0) {
1670 fprintf(stderr, "Can't write to %s: %s\n", bsdtar_target, strerror(errno));
1671 exit(1);
1672 }
1673 g_ptr_array_add(argv_ptr, g_strdup("--directory"));
1674 g_ptr_array_add(argv_ptr, g_strdup(bsdtar_target));
1675 }
1676
1677 if (argument->dle.exclude_list &&
1678 argument->dle.exclude_list->nb_element == 1) {
1679 FILE *exclude;
1680 char *sdisk;
1681 int in_argv;
1682 int entry_in_exclude = 0;
1683 char line[2*PATH_MAX];
1684 FILE *exclude_list;
1685
1686 if (argument->dle.disk) {
1687 sdisk = sanitise_filename(argument->dle.disk);
1688 } else {
1689 sdisk = g_strdup_printf("no_dle-%d", (int)getpid());
1690 }
1691 exclude_filename= g_strjoin(NULL, AMANDA_TMPDIR, "/", "exclude-", sdisk, NULL);
1692 exclude_list = fopen(argument->dle.exclude_list->first->name, "r");
1693 if (!exclude_list) {
1694 fprintf(stderr, "Cannot open exclude file '%s': %s\n",
1695 argument->dle.exclude_list->first->name, strerror(errno));
1696 error("Cannot open exclude file '%s': %s\n",
1697 argument->dle.exclude_list->first->name, strerror(errno));
1698 /*NOTREACHED*/
1699 }
1700 exclude = fopen(exclude_filename, "w");
1701 if (!exclude) {
1702 fprintf(stderr, "Cannot open exclude file '%s': %s\n",
1703 exclude_filename, strerror(errno));
1704 fclose(exclude_list);
1705 error("Cannot open exclude file '%s': %s\n",
1706 exclude_filename, strerror(errno));
1707 /*NOTREACHED*/
1708 }
1709 while (fgets(line, 2*PATH_MAX, exclude_list)) {
1710 char *escaped;
1711 line[strlen(line)-1] = '\0'; /* remove '\n' */
1712 escaped = escape_tar_glob(line, &in_argv);
1713 if (in_argv) {
1714 g_ptr_array_add(argv_ptr, "--exclude");
1715 g_ptr_array_add(argv_ptr, escaped);
1716 } else {
1717 fprintf(exclude,"%s\n", escaped);
1718 entry_in_exclude++;
1719 amfree(escaped);
1720 }
1721 }
1722 fclose(exclude_list);
1723 fclose(exclude);
1724 g_ptr_array_add(argv_ptr, g_strdup("--exclude-from"));
1725 g_ptr_array_add(argv_ptr, exclude_filename);
1726 }
1727
1728 {
1729 GPtrArray *argv_include = g_ptr_array_new();
1730 FILE *include;
1731 char *sdisk;
1732 int in_argv;
1733 guint i;
1734 int entry_in_include = 0;
1735
1736 if (argument->dle.disk) {
1737 sdisk = sanitise_filename(argument->dle.disk);
1738 } else {
1739 sdisk = g_strdup_printf("no_dle-%d", (int)getpid());
1740 }
1741 include_filename = g_strjoin(NULL, AMANDA_TMPDIR, "/", "include-", sdisk, NULL);
1742 include = fopen(include_filename, "w");
1743 if (!include) {
1744 fprintf(stderr, "Cannot open include file '%s': %s\n",
1745 include_filename, strerror(errno));
1746 error("Cannot open include file '%s': %s\n",
1747 include_filename, strerror(errno));
1748 /*NOTREACHED*/
1749 }
1750 if (argument->dle.include_list &&
1751 argument->dle.include_list->nb_element == 1) {
1752 char line[2*PATH_MAX];
1753 FILE *include_list = fopen(argument->dle.include_list->first->name, "r");
1754 if (!include_list) {
1755 fclose(include);
1756 fprintf(stderr, "Cannot open include file '%s': %s\n",
1757 argument->dle.include_list->first->name,
1758 strerror(errno));
1759 error("Cannot open include file '%s': %s\n",
1760 argument->dle.include_list->first->name,
1761 strerror(errno));
1762 /*NOTREACHED*/
1763 }
1764 while (fgets(line, 2*PATH_MAX, include_list)) {
1765 char *escaped;
1766 line[strlen(line)-1] = '\0'; /* remove '\n' */
1767 if (!g_str_equal(line, ".")) {
1768 escaped = escape_tar_glob(line, &in_argv);
1769 if (in_argv) {
1770 g_ptr_array_add(argv_include, escaped);
1771 } else {
1772 fprintf(include,"%s\n", escaped);
1773 entry_in_include++;
1774 amfree(escaped);
1775 }
1776 }
1777 }
1778 fclose(include_list);
1779 }
1780
1781 for (j=1; j< argument->argc; j++) {
1782 if (!g_str_equal(argument->argv[j], ".")) {
1783 char *escaped = escape_tar_glob(argument->argv[j], &in_argv);
1784 if (in_argv) {
1785 g_ptr_array_add(argv_include, escaped);
1786 } else {
1787 fprintf(include,"%s\n", escaped);
1788 entry_in_include++;
1789 amfree(escaped);
1790 }
1791 }
1792 }
1793 fclose(include);
1794
1795 if (entry_in_include) {
1796 g_ptr_array_add(argv_ptr, g_strdup("--files-from"));
1797 g_ptr_array_add(argv_ptr, include_filename);
1798 }
1799
1800 for (i = 0; i < argv_include->len; i++) {
1801 g_ptr_array_add(argv_ptr, (char *)g_ptr_array_index(argv_include,i));
1802 }
1803 amfree(sdisk);
1804 }
1805 g_ptr_array_add(argv_ptr, NULL);
1806
1807 debug_executing(argv_ptr);
1808
1809 tarpid = pipespawnv(bsdtar_path, STDIN_PIPE|STDOUT_PIPE|STDERR_PIPE, 1,
1810 &tarin, &tarout, &tarerr, (char **)argv_ptr->pdata);
1811
1812 in_buf.fd = 0;
1813 in_buf.out = tarin;
1814 in_buf.name = "stdin";
1815 in_buf.buffer = NULL;
1816 in_buf.first = 0;
1817 in_buf.size = 0;
1818 in_buf.allocated_size = 0;
1819
1820 out_buf.fd = tarout;
1821 out_buf.name = "stdout";
1822 out_buf.buffer = NULL;
1823 out_buf.first = 0;
1824 out_buf.size = 0;
1825 out_buf.allocated_size = 0;
1826
1827 err_buf.fd = tarerr;
1828 err_buf.name = "stderr";
1829 err_buf.buffer = NULL;
1830 err_buf.first = 0;
1831 err_buf.size = 0;
1832 err_buf.allocated_size = 0;
1833
1834 in_buf.event = event_create((event_id_t)0, EV_READFD,
1835 handle_restore_stdin, &in_buf);
1836 out_buf.event = event_create((event_id_t)tarout, EV_READFD,
1837 handle_restore_stdout, &out_buf);
1838 err_buf.event = event_create((event_id_t)tarerr, EV_READFD,
1839 handle_restore_stderr, &err_buf);
1840 event_activate(in_buf.event);
1841 event_activate(out_buf.event);
1842 event_activate(err_buf.event);
1843
1844 event_loop(0);
1845
1846 waitpid(tarpid, &wait_status, 0);
1847 if (WIFSIGNALED(wait_status)) {
1848 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
1849 bsdtar_path, WTERMSIG(wait_status), dbfn());
1850 ambsdtar_exit_value = 1;
1851 } else if (WIFEXITED(wait_status) && WEXITSTATUS(wait_status) > 0) {
1852 if (!restore_ok || WEXITSTATUS(wait_status) != 1) {
1853 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
1854 bsdtar_path, WEXITSTATUS(wait_status), dbfn());
1855 ambsdtar_exit_value = 1;
1856 }
1857 }
1858
1859 g_debug(_("ambsdtar: %s: pid %ld"), bsdtar_path, (long)tarpid);
1860 if (errmsg) {
1861 g_debug("%s", errmsg);
1862 fprintf(stderr, "error [%s]\n", errmsg);
1863 }
1864
1865 if (argument->verbose == 0) {
1866 if (exclude_filename)
1867 unlink(exclude_filename);
1868 unlink(include_filename);
1869 }
1870 amfree(bsdtar_path);
1871 amfree(include_filename);
1872 amfree(exclude_filename);
1873
1874 g_free(errmsg);
1875 }
1876
1877 static void
1878 ambsdtar_validate(
1879 application_argument_t *argument G_GNUC_UNUSED)
1880 {
1881 char *cmd = NULL;
1882 GPtrArray *argv_ptr = g_ptr_array_new();
1883 char **env;
1884 char *e;
1885 char buf[32768];
1886
1887 if (!bsdtar_path) {
1888 g_debug("BSDTAR-PATH not set; Piping to /dev/null");
1889 fprintf(stderr,"BSDTAR-PATH not set; Piping to /dev/null\n");
1890 goto pipe_to_null;
1891 }
1892
1893 cmd = g_strdup(bsdtar_path);
1894 g_ptr_array_add(argv_ptr, g_strdup(bsdtar_path));
1895 /* ignore trailing zero blocks on input (this was the default until tar-1.21) */
1896 g_ptr_array_add(argv_ptr, g_strdup("-tf"));
1897 g_ptr_array_add(argv_ptr, g_strdup("-"));
1898 g_ptr_array_add(argv_ptr, NULL);
1899
1900 debug_executing(argv_ptr);
1901 env = safe_env();
1902 execve(cmd, (char **)argv_ptr->pdata, env);
1903 e = strerror(errno);
1904 g_debug("failed to execute %s: %s; Piping to /dev/null", cmd, e);
1905 fprintf(stderr,"failed to execute %s: %s; Piping to /dev/null\n", cmd, e);
1906 free_env(env);
1907 pipe_to_null:
1908 while (read(0, buf, 32768) > 0) {
1909 }
1910 amfree(cmd);
1911 }
1912
1913 static void
1914 ambsdtar_index(
1915 application_argument_t *argument G_GNUC_UNUSED)
1916 {
1917 char *cmd = NULL;
1918 GPtrArray *argv_ptr = g_ptr_array_new();
1919 int datain = 0;
1920 int indexf;
1921 int errf = 2;
1922 pid_t tarpid;
1923 FILE *indexstream;
1924 char line[32768];
1925 amwait_t wait_status;
1926 char *errmsg = NULL;
1927
1928 if (!bsdtar_path) {
1929 g_debug("BSDTAR-PATH not set");
1930 fprintf(stderr,"BSDTAR-PATH not set");
1931 while (read(0, line, 32768) > 0) {
1932 }
1933 exit(1);
1934 }
1935
1936 cmd = g_strdup(bsdtar_path);
1937 g_ptr_array_add(argv_ptr, g_strdup(bsdtar_path));
1938 /* ignore trailing zero blocks on input (this was the default until tar-1.21) */
1939 g_ptr_array_add(argv_ptr, g_strdup("-tf"));
1940 g_ptr_array_add(argv_ptr, g_strdup("-"));
1941 g_ptr_array_add(argv_ptr, NULL);
1942
1943 tarpid = pipespawnv(cmd, STDOUT_PIPE, 0,
1944 &datain, &indexf, &errf, (char **)argv_ptr->pdata);
1945 aclose(datain);
1946
1947 indexstream = fdopen(indexf, "r");
1948 if (!indexstream) {
1949 error(_("error indexstream(%d): %s\n"), indexf, strerror(errno));
1950 }
1951
1952 fprintf(stdout, "/\n");
1953 while (fgets(line, sizeof(line), indexstream) != NULL) {
1954 if (strlen(line) > 0 && line[strlen(line)-1] == '\n') {
1955 /* remove trailling \n */
1956 line[strlen(line)-1] = '\0';
1957 }
1958 if (*line == '.' && *(line+1) == '/') { /* filename */
1959 fprintf(stdout, "%s\n", &line[1]); /* remove . */
1960 }
1961 }
1962 fclose(indexstream);
1963 waitpid(tarpid, &wait_status, 0);
1964 if (WIFSIGNALED(wait_status)) {
1965 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
1966 cmd, WTERMSIG(wait_status), dbfn());
1967 } else if (WIFEXITED(wait_status)) {
1968 if (exit_value[WEXITSTATUS(wait_status)] == 1) {
1969 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
1970 cmd, WEXITSTATUS(wait_status), dbfn());
1971 } else {
1972 /* Normal exit */
1973 }
1974 } else {
1975 errmsg = g_strdup_printf(_("%s got bad exit: see %s"),
1976 cmd, dbfn());
1977 }
1978 g_debug(_("ambsdtar: %s: pid %ld"), cmd, (long)tarpid);
1979 if (errmsg) {
1980 g_debug("%s", errmsg);
1981 fprintf(stderr, "error [%s]\n", errmsg);
1982 }
1983
1984 amfree(cmd);
1985 }
1986
1987 static void
1988 ambsdtar_build_exinclude(
1989 dle_t *dle,
1990 int *nb_exclude,
1991 char **file_exclude,
1992 int *nb_include,
1993 char **file_include,
1994 char *dirname,
1995 messagelist_t *mlist)
1996 {
1997 int n_exclude = 0;
1998 int n_include = 0;
1999 char *exclude = NULL;
2000 char *include = NULL;
2001
2002 if (dle->exclude_file) n_exclude += dle->exclude_file->nb_element;
2003 if (dle->exclude_list) n_exclude += dle->exclude_list->nb_element;
2004 if (dle->include_file) n_include += dle->include_file->nb_element;
2005 if (dle->include_list) n_include += dle->include_list->nb_element;
2006
2007 if (n_exclude > 0) exclude = build_exclude(dle, mlist);
2008 if (n_include > 0) include = build_include(dle, dirname, mlist);
2009
2010 if (nb_exclude)
2011 *nb_exclude = n_exclude;
2012 if (file_exclude)
2013 *file_exclude = exclude;
2014 else
2015 amfree(exclude);
2016
2017 if (nb_include)
2018 *nb_include = n_include;
2019 if (file_include)
2020 *file_include = include;
2021 else
2022 amfree(include);
2023 }
2024
2025 static char *
2026 ambsdtar_get_timestamps(
2027 application_argument_t *argument,
2028 int level,
2029 FILE *mesgstream,
2030 int command)
2031 {
2032 char *basename = NULL;
2033 char *filename = NULL;
2034 int infd;
2035 char *inputname = NULL;
2036 char *errmsg = NULL;
2037
2038 if (state_dir) {
2039 char number[NUM_STR_SIZE];
2040 int baselevel;
2041 char *sdisk = sanitise_filename(argument->dle.disk);
2042 FILE *tt;
2043 char line[1024];
2044
2045 basename = g_strjoin(NULL, state_dir,
2046 "/",
2047 argument->host,
2048 sdisk,
2049 NULL);
2050 amfree(sdisk);
2051
2052 snprintf(number, sizeof(number), "%d", level);
2053 filename = g_strjoin(NULL, basename, "_", number, NULL);
2054
2055 /*
2056 * Open the timestamps file from the previous level. Search
2057 * backward until one is found. If none are found (which will also
2058 * be true for a level 0), arrange to read from /dev/null.
2059 */
2060 baselevel = level;
2061 infd = -1;
2062 inputname = g_strdup("AA");
2063 while (infd == -1 && inputname != NULL) {
2064 amfree(inputname);
2065 if (--baselevel >= 0) {
2066 snprintf(number, sizeof(number), "%d", baselevel);
2067 inputname = g_strconcat(basename, "_", number, NULL);
2068 } else {
2069 inputname = NULL;
2070 amfree(basename);
2071 amfree(filename);
2072 g_debug("Using NULL timestamps");
2073 return NULL;
2074 }
2075 if ((infd = open(inputname, O_RDONLY)) == -1) {
2076
2077 errmsg = g_strdup_printf(_("ambsdtar: error opening %s: %s"),
2078 inputname, strerror(errno));
2079 g_debug("%s", errmsg);
2080 if (baselevel == 0) {
2081 if (command == CMD_ESTIMATE) {
2082 fprintf(mesgstream, "ERROR %s\n", errmsg);
2083 } else {
2084 fprintf(mesgstream, "sendbackup: error [%s]\n", errmsg);
2085 }
2086 exit(1);
2087 }
2088 amfree(errmsg);
2089 }
2090 }
2091
2092 tt = fdopen(infd, "r");
2093 if (!tt) {
2094 g_debug("Failed to fdopen '%s' to '%s'", inputname, strerror(errno));
2095 close(infd);
2096 infd = -1;
2097 } else if (!fgets(line, 1024, tt)) {
2098 errmsg = g_strdup_printf(_("ambsdtar: error reading '%s': %s"),
2099 inputname, strerror(errno));
2100 if (command == CMD_ESTIMATE) {
2101 fprintf(mesgstream, "ERROR %s\n", errmsg);
2102 } else {
2103 fprintf(mesgstream, "sendbackup: error [%s]\n", errmsg);
2104 }
2105 fclose(tt);
2106 exit(1);
2107 } else {
2108 fclose(tt);
2109 tt = NULL;
2110 infd = -1;
2111 g_debug("Read timestamps '%s' to '%s'", line, filename);
2112 }
2113 amfree(basename);
2114 amfree(filename);
2115 amfree(inputname);
2116 return g_strdup(line);
2117 } else {
2118 errmsg = _("STATE-DIR is not defined");
2119 g_debug("%s", errmsg);
2120 if (command == CMD_ESTIMATE) {
2121 fprintf(mesgstream, "ERROR %s\n", errmsg);
2122 } else {
2123 fprintf(mesgstream, "sendbackup: error [%s]\n", errmsg);
2124 }
2125 exit(1);
2126 }
2127 return NULL;
2128 }
2129
2130 static void
2131 ambsdtar_set_timestamps(
2132 application_argument_t *argument,
2133 int level,
2134 char *timestamps,
2135 FILE *mesgstream)
2136 {
2137 char *basename = NULL;
2138 char *filename = NULL;
2139 char *errmsg = NULL;
2140
2141 if (state_dir) {
2142 char number[NUM_STR_SIZE];
2143 char *sdisk = sanitise_filename(argument->dle.disk);
2144 FILE *tt;
2145
2146 basename = g_strjoin(NULL, state_dir,
2147 "/",
2148 argument->host,
2149 sdisk,
2150 NULL);
2151 amfree(sdisk);
2152
2153 snprintf(number, sizeof(number), "%d", level);
2154 filename = g_strjoin(NULL, basename, "_", number, NULL);
2155
2156 tt = fopen(filename, "w");
2157 if (!tt) {
2158 errmsg = g_strdup_printf("Failed to open '%s': %s",
2159 filename, strerror(errno));
2160 g_debug("%s", errmsg);
2161 fprintf(mesgstream, "sendbackup: error [%s]\n", errmsg);
2162 } else {
2163 fprintf(tt, "%s", timestamps);
2164 fclose(tt);
2165 g_debug("Wrote timestamps '%s' to '%s'", timestamps, filename);
2166 }
2167 amfree(basename);
2168 amfree(filename);
2169 } else {
2170 errmsg = _("STATE-DIR is not defined");
2171 g_debug("%s", errmsg);
2172 fprintf(mesgstream, "sendabckup: error [%s]\n", errmsg);
2173 exit(1);
2174 }
2175 }
2176
2177 static GPtrArray *
2178 ambsdtar_build_argv(
2179 char *bsdtar_realpath,
2180 application_argument_t *argument,
2181 char *timestamps,
2182 char **file_exclude,
2183 char **file_include,
2184 int command,
2185 messagelist_t *mlist)
2186 {
2187 int nb_exclude = 0;
2188 int nb_include = 0;
2189 char *dirname;
2190 char tmppath[PATH_MAX];
2191 GPtrArray *argv_ptr = g_ptr_array_new();
2192 GSList *copt;
2193
2194 if (bsdtar_target) {
2195 dirname = bsdtar_target;
2196 } else {
2197 dirname = argument->dle.device;
2198 }
2199
2200 ambsdtar_build_exinclude(&argument->dle,
2201 &nb_exclude, file_exclude,
2202 &nb_include, file_include, dirname, mlist);
2203
2204 g_ptr_array_add(argv_ptr, g_strdup(bsdtar_realpath));
2205
2206 g_ptr_array_add(argv_ptr, g_strdup("--create"));
2207 g_ptr_array_add(argv_ptr, g_strdup("--file"));
2208 if (command == CMD_ESTIMATE) {
2209 g_ptr_array_add(argv_ptr, g_strdup("/dev/null"));
2210 } else {
2211 g_ptr_array_add(argv_ptr, g_strdup("-"));
2212 }
2213 g_ptr_array_add(argv_ptr, g_strdup("--directory"));
2214 canonicalize_pathname(dirname, tmppath);
2215 g_ptr_array_add(argv_ptr, g_strdup(tmppath));
2216 if (timestamps) {
2217 g_ptr_array_add(argv_ptr, g_strdup("--newer"));
2218 g_ptr_array_add(argv_ptr, g_strdup(timestamps));
2219 }
2220 if (bsdtar_onefilesystem)
2221 g_ptr_array_add(argv_ptr, g_strdup("--one-file-system"));
2222 if (argument->tar_blocksize) {
2223 g_ptr_array_add(argv_ptr, g_strdup("--block-size"));
2224 g_ptr_array_add(argv_ptr, g_strdup(argument->tar_blocksize));
2225 }
2226 g_ptr_array_add(argv_ptr, g_strdup("--totals"));
2227
2228 for (copt = argument->command_options; copt != NULL; copt = copt->next) {
2229 g_ptr_array_add(argv_ptr, g_strdup((char *)copt->data));
2230 }
2231
2232 if (*file_exclude) {
2233 g_ptr_array_add(argv_ptr, g_strdup("--exclude-from"));
2234 g_ptr_array_add(argv_ptr, g_strdup(*file_exclude));
2235 }
2236
2237 if (*file_include) {
2238 g_ptr_array_add(argv_ptr, g_strdup("--files-from"));
2239 g_ptr_array_add(argv_ptr, g_strdup(*file_include));
2240 } else {
2241 g_ptr_array_add(argv_ptr, g_strdup("."));
2242 }
2243 g_ptr_array_add(argv_ptr, NULL);
2244
2245 return(argv_ptr);
2246 }
2247