Debian Codegraph @

application-src/amstar.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 * $Id: amstar.c 8888 2007-10-02 13:40:42Z martineau $
30 *
31 * send estimated backup sizes using dump
32 */
33
34 /* PROPERTY:
35 *
36 * STAR-PATH (default STAR)
37 * STAR-TARDUMP
38 * STAR-DLE-TARDUMP
39 * ONE-FILE-SYSTEM
40 * SPARSE
41 * NORMAL
42 * IGNORE
43 * STRANGE
44 * INCLUDE-LIST (for restore only)
45 * EXCLUDE-FILE
46 * EXCLUDE-LIST
47 * TARGET
48 */
49
50 #include "amanda.h"
51 #include "match.h"
52 #include "pipespawn.h"
53 #include "amfeatures.h"
54 #include "amandates.h"
55 #include "clock.h"
56 #include "amutil.h"
57 #include "getfsent.h"
58 #include "client_util.h"
59 #include "conffile.h"
60 #include "getopt.h"
61 #include "sendbackup.h"
62 #include "security-file.h"
63 #include "ammessage.h"
64 #include "event.h"
65
66 int debug_application = 1;
67 #define application_debug(i, ...) do { \
68 if ((i) <= debug_application) { \
69 dbprintf(__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("star: [0-9][0-9]* blocks", 10240, 1),
76 AM_NORMAL_RE("^could not open conf file"),
77 AM_NORMAL_RE("^Type of this level "),
78 AM_NORMAL_RE("^Date of this level "),
79 AM_NORMAL_RE("^Date of last level "),
80 AM_NORMAL_RE("^Dump record level "),
81 AM_NORMAL_RE("^Throughput"),
82 AM_NORMAL_RE("^.*is sparse$"),
83
84 #ifdef IGNORE_TAR_ERRORS
85 AM_NORMAL_RE("^.*shrunk*$"),
86 AM_NORMAL_RE("^.*changed size.*$"),
87 AM_NORMAL_RE("^.*Cannot listxattr for.*$"),
88 AM_NORMAL_RE("^.Cannot: stat .*$"),
89 AM_NORMAL_RE("^.Missing links .*$"),
90 AM_NORMAL_RE("^.Cannot get xattr.*$"),
91 AM_NORMAL_RE("^.Cannot.*acl.*$"),
92 #endif
93
94 AM_NORMAL_RE("^star: dumped [0-9][0-9]* (tar )?files"),
95 AM_NORMAL_RE("^.*The following problems occurred during .* processing.*$"),
96 AM_NORMAL_RE("^.*Processed all possible files, despite earlier errors.*$"),
97 AM_NORMAL_RE("^.*not written due to problems during backup.*$"),
98
99 AM_STRANGE_RE("^Perform a level 0 dump first.*$"),
100
101 AM_ERROR_RE("amstar: error"),
102
103 /* catch-all: DMP_STRANGE is returned for all other lines */
104 AM_STRANGE_RE(NULL)
105 };
106 static amregex_t *re_table;
107
108 /* local functions */
109 int main(int argc, char **argv);
110
111 typedef struct application_argument_s {
112 char *config;
113 char *host;
114 int message;
115 int collection;
116 int calcsize;
117 GSList *level;
118 GSList *command_options;
119 dle_t dle;
120 int argc;
121 char **argv;
122 } application_argument_t;
123
124 enum { CMD_ESTIMATE, CMD_BACKUP };
125
126 static void amstar_support(application_argument_t *argument);
127 static void amstar_selfcheck(application_argument_t *argument);
128 static void amstar_estimate(application_argument_t *argument);
129 static void amstar_backup(application_argument_t *argument);
130 static void amstar_restore(application_argument_t *argument);
131 static void amstar_validate(application_argument_t *argument);
132 static void amstar_index(application_argument_t *argument);
133 static GPtrArray *amstar_build_argv(char *star_realpath,
134 application_argument_t *argument,
135 int level,
136 int command,
137 FILE *mesgstream);
138 static int check_device(application_argument_t *argument);
139
140 static char *command;
141 static char *star_path;
142 static char *star_tardumps;
143 static int star_dle_tardumps;
144 static int star_onefilesystem;
145 static int star_sparse;
146 static int star_acl;
147 static char *star_target;
148 static GSList *normal_message = NULL;
149 static GSList *ignore_message = NULL;
150 static GSList *strange_message = NULL;
151 static FILE *mesgstream = NULL;
152 static int amstar_exit_value = 0;
153
154
155 static struct option long_options[] = {
156 {"config" , 1, NULL, 1},
157 {"host" , 1, NULL, 2},
158 {"disk" , 1, NULL, 3},
159 {"device" , 1, NULL, 4},
160 {"level" , 1, NULL, 5},
161 {"index" , 1, NULL, 6},
162 {"message" , 1, NULL, 7},
163 {"collection" , 0, NULL, 8},
164 {"record" , 0, NULL, 9},
165 {"star-path" , 1, NULL, 10},
166 {"star-tardump" , 1, NULL, 11},
167 {"star-dle-tardump", 1, NULL, 12},
168 {"one-file-system" , 1, NULL, 13},
169 {"sparse" , 1, NULL, 14},
170 {"calcsize" , 0, NULL, 15},
171 {"normal" , 1, NULL, 16},
172 {"ignore" , 1, NULL, 17},
173 {"strange" , 1, NULL, 18},
174 {"include-list" , 1, NULL, 19},
175 {"exclude-list" , 1, NULL, 20},
176 {"directory" , 1, NULL, 21},
177 {"command-options" , 1, NULL, 22},
178 {"exclude-file" , 1, NULL, 23},
179 {"acl" , 1, NULL, 24},
180 {"include-file" , 1, NULL, 25},
181 {"target" , 1, NULL, 26},
182 { NULL, 0, NULL, 0}
183 };
184
185 typedef struct filter_s {
186 int fd;
187 char *name;
188 char *buffer;
189 gint64 first; /* first byte used */
190 gint64 size; /* number of byte use in the buffer */
191 gint64 allocated_size; /* allocated size of the buffer */
192 event_handle_t *event;
193 int out;
194 } filter_t;
195
196 static void read_fd(int fd, char *name, event_fn_t fn);
197 static void read_text(void *cookie);
198
199 static void
200 read_fd(
201 int fd,
202 char *name,
203 event_fn_t fn)
204 {
205 filter_t *filter = g_new0(filter_t, 1);
206 filter->fd = fd;
207 filter->name = g_strdup(name);
208 filter->event = event_create((event_id_t)filter->fd, EV_READFD,
209 fn, filter);
210 event_activate(filter->event);
211 }
212
213 static void
214 read_text(
215 void *cookie)
216 {
217 filter_t *filter = cookie;
218 char *line;
219 char *p;
220 ssize_t nread;
221 int len;
222
223 if (filter->buffer == NULL) {
224 /* allocate initial buffer */
225 filter->buffer = g_malloc(32768);
226 filter->first = 0;
227 filter->size = 0;
228 filter->allocated_size = 32768;
229 } else if (filter->first > 0) {
230 if (filter->allocated_size - filter->size - filter->first < 1024) {
231 memmove(filter->buffer, filter->buffer + filter->first,
232 filter->size);
233 filter->first = 0;
234 }
235 } else if (filter->allocated_size - filter->size < 1024) {
236 /* double the size of the buffer */
237 filter->allocated_size *= 2;
238 filter->buffer = g_realloc(filter->buffer, filter->allocated_size);
239 }
240
241 nread = read(filter->fd, filter->buffer + filter->first + filter->size,
242 filter->allocated_size - filter->first - filter->size - 2);
243
244 if (nread <= 0) {
245 event_release(filter->event);
246 aclose(filter->fd);
247 if (filter->size > 0 && filter->buffer[filter->first + filter->size - 1] != '\n') {
248 /* Add a '\n' at end of buffer */
249 filter->buffer[filter->first + filter->size] = '\n';
250 filter->size++;
251 }
252 } else {
253 filter->size += nread;
254 }
255
256 /* process all complete lines */
257 line = filter->buffer + filter->first;
258 line[filter->size] = '\0';
259 while (line < filter->buffer + filter->first + filter->size &&
260 (p = strchr(line, '\n')) != NULL) {
261 *p = '\0';
262 if (g_str_equal(filter->name, "restore stdout")) {
263 g_fprintf(stdout, "%s\n", line);
264 } else if (g_str_equal(filter->name, "restore stderr")) {
265 if (!match("^star: [0-9]* blocks?", line)) {
266 g_fprintf(stderr, "%s\n", line);
267 }
268 } else if (g_str_equal(filter->name, "validate stdout")) {
269 g_fprintf(stdout, "%s\n", line);
270 } else if (g_str_equal(filter->name, "validate stderr")) {
271 if (!match("^star: [0-9]* blocks?", line)) {
272 g_fprintf(stderr, "%s\n", line);
273 }
274 } else {
275 g_debug("unknown: %s", line);
276 }
277 len = p - line + 1;
278 filter->first += len;
279 filter->size -= len;
280 line = p + 1;
281 }
282
283 if (nread <= 0) {
284 g_free(filter->name);
285 g_free(filter->buffer);
286 g_free(filter);
287 }
288 }
289
290 static message_t *
291 amstar_print_message(
292 message_t *message)
293 {
294 if (strcasecmp(command, "selfcheck") == 0) {
295 return print_message(message);
296 }
297 if (message_get_severity(message) <= MSG_INFO) {
298 if (g_str_equal(command, "estimate")) {
299 fprintf(stdout, "OK %s\n", get_message(message));
300 } else if (g_str_equal(command, "backup")) {
301 fprintf(mesgstream, "| %s\n", get_message(message));
302 } else {
303 fprintf(stdout, "%s\n", get_message(message));
304 }
305 } else {
306 amstar_exit_value = 1;
307 if (g_str_equal(command, "estimate")) {
308 fprintf(stdout, "ERROR %s\n", get_message(message));
309 } else if (g_str_equal(command, "backup")) {
310 fprintf(mesgstream, "sendbackup: error [%s]\n", get_message(message));
311 } else {
312 fprintf(stdout, "%s\n", get_message(message));
313 }
314 }
315 return message;
316 }
317
318
319
320 int
321 main(
322 int argc,
323 char ** argv)
324 {
325 int c;
326 application_argument_t argument;
327 char *star_sparse_value = NULL;
328 char *star_acl_value = NULL;
329
330 #ifdef STAR
331 star_path = g_strdup(STAR);
332 #else
333 star_path = NULL;
334 #endif
335 star_tardumps = g_strdup("/etc/tardumps");
336 star_dle_tardumps = 0;
337 star_onefilesystem = 1;
338 star_sparse = 1;
339 star_acl = 1;
340 star_target = NULL;
341
342 glib_init();
343
344 /* initialize */
345
346 /*
347 * Configure program for internationalization:
348 * 1) Only set the message locale for now.
349 * 2) Set textdomain for all amanda related programs to "amanda"
350 * We don't want to be forced to support dozens of message catalogs.
351 */
352 setlocale(LC_MESSAGES, "C");
353 textdomain("amanda");
354
355 if (argc < 2) {
356 printf("ERROR no command given to amstar\n");
357 error(_("No command given to amstar"));
358 }
359
360 /* drop root privileges */
361 if (!set_root_privs(0)) {
362 if (g_str_equal(argv[1], "selfcheck")) {
363 printf("ERROR amstar must be run setuid root\n");
364 }
365 error(_("amstar must be run setuid root"));
366 }
367
368 safe_fd(3, 2);
369
370 set_pname("amstar");
371 set_pcomponent("application");
372 set_pmodule("amstar");
373
374 /* Don't die when child closes pipe */
375 signal(SIGPIPE, SIG_IGN);
376
377 #if defined(USE_DBMALLOC)
378 malloc_size_1 = malloc_inuse(&malloc_hist_1);
379 #endif
380
381 add_amanda_log_handler(amanda_log_stderr);
382 add_amanda_log_handler(amanda_log_syslog);
383 dbopen(DBG_SUBDIR_CLIENT);
384 startclock();
385 dbprintf(_("version %s\n"), VERSION);
386
387 config_init(CONFIG_INIT_CLIENT|CONFIG_INIT_GLOBAL, NULL);
388
389 //check_running_as(RUNNING_AS_DUMPUSER_PREFERRED);
390 //root for amrecover
391 //RUNNING_AS_CLIENT_LOGIN from selfcheck, sendsize, sendbackup
392
393 /* parse argument */
394 command = argv[1];
395
396 if (strcasecmp(command,"selfcheck") == 0) {
397 fprintf(stdout, "MESSAGE JSON\n");
398 }
399
400 argument.config = NULL;
401 argument.host = NULL;
402 argument.message = 0;
403 argument.collection = 0;
404 argument.calcsize = 0;
405 argument.level = NULL;
406 argument.command_options = NULL;
407 init_dle(&argument.dle);
408 argument.dle.record = 0;
409
410 opterr = 0;
411 while (1) {
412 int option_index = 0;
413 c = getopt_long (argc, argv, "", long_options, &option_index);
414 if (c == -1)
415 break;
416
417 switch (c) {
418 case 1: if (optarg) {
419 amfree(argument.config);
420 argument.config = g_strdup(optarg);
421 }
422 break;
423 case 2: if (optarg) {
424 amfree(argument.host);
425 argument.host = g_strdup(optarg);
426 }
427 break;
428 case 3: if (optarg) {
429 amfree(argument.dle.disk);
430 argument.dle.disk = g_strdup(optarg);
431 }
432 break;
433 case 4: if (optarg) {
434 amfree(argument.dle.device);
435 argument.dle.device = g_strdup(optarg);
436 }
437 break;
438 case 5: if (optarg) {
439 argument.level = g_slist_append(argument.level,
440 GINT_TO_POINTER(atoi(optarg)));
441 }
442 break;
443 case 6: argument.dle.create_index = 1;
444 break;
445 case 7: argument.message = 1;
446 break;
447 case 8: argument.collection = 1;
448 break;
449 case 9: argument.dle.record = 1;
450 break;
451 case 10: if (optarg) {
452 amfree(star_path);
453 star_path = g_strdup(optarg);
454 }
455 break;
456 case 11: if (optarg) {
457 amfree(star_tardumps);
458 star_tardumps = g_strdup(optarg);
459 }
460 break;
461 case 12: if (optarg && strcasecmp(optarg, "NO") == 0)
462 star_dle_tardumps = 0;
463 else if (optarg && strcasecmp(optarg, "YES") == 0)
464 star_dle_tardumps = 1;
465 else if (strcasecmp(command, "selfcheck") == 0)
466 printf(_("ERROR [%s: bad STAR-DLE-TARDUMP property value (%s)]\n"), get_pname(), optarg);
467 break;
468 case 13: if (optarg && strcasecmp(optarg, "YES") != 0) {
469 /* This option is required to be YES */
470 /* star_onefilesystem = 0; */
471 }
472 break;
473 case 14: amfree(star_sparse_value);
474 star_sparse_value = g_strdup(optarg);
475 break;
476 case 15: argument.calcsize = 1;
477 break;
478 case 16: if (optarg)
479 normal_message =
480 g_slist_append(normal_message, optarg);
481 break;
482 case 17: if (optarg)
483 ignore_message =
484 g_slist_append(ignore_message, optarg);
485 break;
486 case 18: if (optarg)
487 strange_message =
488 g_slist_append(strange_message, optarg);
489 break;
490 case 19: if (optarg)
491 argument.dle.include_list =
492 append_sl(argument.dle.include_list, optarg);
493 break;
494 case 20: if (optarg)
495 argument.dle.exclude_list =
496 append_sl(argument.dle.exclude_list, optarg);
497 break;
498 case 21: if (optarg) {
499 amfree(star_target);
500 star_target = g_strdup(optarg);
501 }
502 break;
503 case 22: if (optarg)
504 argument.command_options =
505 g_slist_append(argument.command_options,
506 g_strdup(optarg));
507 break;
508 case 23: if (optarg)
509 argument.dle.exclude_file =
510 append_sl(argument.dle.exclude_file, optarg);
511 break;
512 case 24: amfree(star_acl_value);
513 star_acl_value = g_strdup(optarg);
514 break;
515 case 25: if (optarg)
516 argument.dle.include_file =
517 append_sl(argument.dle.include_file, optarg);
518 break;
519 case 26: if (optarg) {
520 amfree(star_target);
521 star_target = g_strdup(optarg);
522 }
523 break;
524 case ':':
525 case '?':
526 break;
527 }
528 }
529
530 if (g_str_equal(command, "backup")) {
531 mesgstream = fdopen(3, "w");
532 if (!mesgstream) {
533 error(_("error mesgstream(%d): %s\n"), 3, strerror(errno));
534 }
535 }
536
537 if (!argument.dle.disk && argument.dle.device)
538 argument.dle.disk = g_strdup(argument.dle.device);
539 if (!argument.dle.device && argument.dle.disk)
540 argument.dle.device = g_strdup(argument.dle.disk);
541
542 if (star_sparse_value) {
543 if (strcasecmp(star_sparse_value, "NO") == 0) {
544 star_sparse = 0;
545 } else if (strcasecmp(star_sparse_value, "YES") == 0) {
546 star_sparse = 1;
547 } else {
548 delete_message(amstar_print_message(build_message(
549 AMANDA_FILE, __LINE__, 3701008, MSG_ERROR, 4,
550 "value", star_sparse_value,
551 "disk", argument.dle.disk,
552 "device", argument.dle.device,
553 "hostname", argument.host)));
554 }
555 }
556
557 if (star_acl_value) {
558 if (strcasecmp(star_acl_value, "NO") == 0){
559 star_acl = 0;
560 } else if (strcasecmp(star_acl_value, "YES") == 0){
561 star_acl = 1;
562 } else {
563 delete_message(amstar_print_message(build_message(
564 AMANDA_FILE, __LINE__, 3701009, MSG_ERROR, 4,
565 "value", star_acl_value,
566 "disk", argument.dle.disk,
567 "device", argument.dle.device,
568 "hostname", argument.host)));
569 }
570 }
571
572 argument.argc = argc - optind;
573 argument.argv = argv + optind;
574
575 if (argument.config) {
576 /* overlay this configuration on the existing (nameless) configuration */
577 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
578 argument.config);
579 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
580
581 }
582
583 if (config_errors(NULL) >= CFGERR_ERRORS) {
584 g_critical(_("errors processing config file"));
585 }
586
587 re_table = build_re_table(init_re_table, normal_message, ignore_message,
588 strange_message);
589
590 if (g_str_equal(command, "support")) {
591 amstar_support(&argument);
592 } else if (g_str_equal(command, "selfcheck")) {
593 amstar_selfcheck(&argument);
594 } else if (g_str_equal(command, "estimate")) {
595 amstar_estimate(&argument);
596 } else if (g_str_equal(command, "backup")) {
597 amstar_backup(&argument);
598 } else if (g_str_equal(command, "restore")) {
599 amstar_restore(&argument);
600 } else if (g_str_equal(command, "validate")) {
601 amstar_validate(&argument);
602 } else if (g_str_equal(command, "index")) {
603 amstar_index(&argument);
604 } else {
605 fprintf(stderr, "Unknown command `%s'.\n", command);
606 exit (1);
607 }
608
609 dbclose();
610
611 return amstar_exit_value;
612 }
613
614 static char *validate_command_options(
615 application_argument_t *argument)
616 {
617 GSList *copt;
618
619 for (copt = argument->command_options; copt != NULL; copt = copt->next) {
620 char *opt = (char *)copt->data;
621
622 if (g_str_has_prefix(opt, "new-volume-script") ||
623 g_str_has_prefix(opt, "-new-volume-script") ||
624 g_str_has_prefix(opt, "--new-volume-script")) {
625 return opt;
626 }
627 }
628
629 return NULL;
630 }
631
632 static void
633 amstar_support(
634 application_argument_t *argument)
635 {
636 (void)argument;
637 fprintf(stdout, "CONFIG YES\n");
638 fprintf(stdout, "HOST YES\n");
639 fprintf(stdout, "DISK YES\n");
640 fprintf(stdout, "MAX-LEVEL 9\n");
641 fprintf(stdout, "INDEX-LINE YES\n");
642 fprintf(stdout, "INDEX-XML NO\n");
643 fprintf(stdout, "MESSAGE-LINE YES\n");
644 fprintf(stdout, "MESSAGE-SELFCHECK-JSON YES\n");
645 fprintf(stdout, "MESSAGE-XML NO\n");
646 fprintf(stdout, "RECORD YES\n");
647 fprintf(stdout, "INCLUDE-FILE YES\n");
648 fprintf(stdout, "INCLUDE-LIST YES\n");
649 fprintf(stdout, "EXCLUDE-FILE YES\n");
650 fprintf(stdout, "EXCLUDE-LIST YES\n");
651 fprintf(stdout, "COLLECTION NO\n");
652 fprintf(stdout, "MULTI-ESTIMATE YES\n");
653 fprintf(stdout, "CALCSIZE YES\n");
654 fprintf(stdout, "CLIENT-ESTIMATE YES\n");
655 }
656
657 static void
658 amstar_selfcheck(
659 application_argument_t *argument)
660 {
661 char *option;
662
663 if (argument->dle.disk) {
664 delete_message(amstar_print_message(build_message(
665 AMANDA_FILE, __LINE__, 3701000, MSG_INFO, 3,
666 "disk", argument->dle.disk,
667 "device", argument->dle.device,
668 "hostname", argument->host)));
669 }
670
671 delete_message(amstar_print_message(build_message(
672 AMANDA_FILE, __LINE__, 3701001, MSG_INFO, 4,
673 "version", VERSION,
674 "disk", argument->dle.disk,
675 "device", argument->dle.device,
676 "hostname", argument->host)));
677
678 delete_message(amstar_print_message(build_message(
679 AMANDA_FILE, __LINE__, 3701004, MSG_INFO, 3,
680 "disk", argument->dle.disk,
681 "device", argument->dle.device,
682 "hostname", argument->host)));
683
684 if (argument->dle.device) {
685 delete_message(amstar_print_message(build_message(
686 AMANDA_FILE, __LINE__, 3701019, MSG_INFO, 4,
687 "directory", star_target,
688 "disk", argument->dle.disk,
689 "device", argument->dle.device,
690 "hostname", argument->host)));
691 }
692 if (star_target) {
693 delete_message(amstar_print_message(build_message(
694 AMANDA_FILE, __LINE__, 3701018, MSG_INFO, 4,
695 "directory", star_target,
696 "disk", argument->dle.disk,
697 "device", argument->dle.device,
698 "hostname", argument->host)));
699 }
700
701 if (((argument->dle.include_list &&
702 argument->dle.include_list->nb_element >= 0) ||
703 (argument->dle.include_file &&
704 argument->dle.include_file->nb_element >= 0)) &&
705 ((argument->dle.exclude_list &&
706 argument->dle.exclude_list->nb_element >= 0) ||
707 (argument->dle.exclude_file &&
708 argument->dle.exclude_file->nb_element >= 0))) {
709 delete_message(amstar_print_message(build_message(
710 AMANDA_FILE, __LINE__, 3701017, MSG_ERROR, 3,
711 "disk", argument->dle.disk,
712 "device", argument->dle.device,
713 "hostname", argument->host)));
714 }
715
716 if ((option = validate_command_options(argument))) {
717 delete_message(amstar_print_message(build_message(
718 AMANDA_FILE, __LINE__, 3701014, MSG_ERROR, 4,
719 "disk", argument->dle.disk,
720 "device", argument->dle.device,
721 "hostname", argument->host,
722 "command-options", option)));
723 }
724
725 if (star_path) {
726 char *star_realpath = NULL;
727 message_t *message;
728 if ((message = check_exec_for_suid_message("STAR_PATH", star_path, &star_realpath))) {
729 delete_message(amstar_print_message(message));
730 } else {
731 message = amstar_print_message(check_file_message(star_path, X_OK));
732 if (message && message_get_severity(message) <= MSG_INFO) {
733 char *star_version;
734 GPtrArray *argv_ptr = g_ptr_array_new();
735
736 g_ptr_array_add(argv_ptr, star_realpath);
737 g_ptr_array_add(argv_ptr, "--version");
738 g_ptr_array_add(argv_ptr, NULL);
739
740 star_version = get_first_line(argv_ptr);
741
742 if (star_version) {
743 char *sv, *sv1;
744 for (sv = star_version; *sv && !g_ascii_isdigit(*sv); sv++);
745 for (sv1 = sv; *sv1 && *sv1 != ' '; sv1++);
746 *sv1 = '\0';
747 delete_message(amstar_print_message(build_message(
748 AMANDA_FILE, __LINE__, 3701002, MSG_INFO, 4,
749 "star-version", sv,
750 "disk", argument->dle.disk,
751 "device", argument->dle.device,
752 "hostname", argument->host)));
753 } else {
754 printf(_("ERROR Can't get %s version\n"), star_path);
755 delete_message(amstar_print_message(build_message(
756 AMANDA_FILE, __LINE__, 3701003, MSG_INFO, 4,
757 "star-path", star_path,
758 "disk", argument->dle.disk,
759 "device", argument->dle.device,
760 "hostname", argument->host)));
761 }
762 g_ptr_array_free(argv_ptr, TRUE);
763 amfree(star_version);
764 }
765 if (message)
766 delete_message(message);
767 }
768 if (star_realpath)
769 g_free(star_realpath);
770 } else {
771 delete_message(amstar_print_message(build_message(
772 AMANDA_FILE, __LINE__, 3701005, MSG_ERROR, 3,
773 "disk", argument->dle.disk,
774 "device", argument->dle.device,
775 "hostname", argument->host)));
776 }
777
778 delete_message(amstar_print_message(check_file_message(star_tardumps, W_OK)));
779
780 if (argument->calcsize) {
781 char *calcsize = g_strjoin(NULL, amlibexecdir, "/", "calcsize", NULL);
782 delete_message(amstar_print_message(check_file_message(calcsize, X_OK)));
783 delete_message(amstar_print_message(check_suid_message(calcsize)));
784 amfree(calcsize);
785 }
786
787 {
788 char *amandates_file;
789 amandates_file = getconf_str(CNF_AMANDATES);
790 delete_message(amstar_print_message(check_file_message(amandates_file, R_OK|W_OK)));
791 }
792
793 set_root_privs(1);
794 if (argument->dle.device) {
795 delete_message(amstar_print_message(check_dir_message(argument->dle.device, R_OK)));
796 }
797 set_root_privs(0);
798 }
799
800 static void
801 amstar_estimate(
802 application_argument_t *argument)
803 {
804 GPtrArray *argv_ptr;
805 int nullfd;
806 int pipefd;
807 FILE *dumpout = NULL;
808 off_t size = -1;
809 char line[32768];
810 char *errmsg = NULL;
811 char *qerrmsg;
812 char *qdisk = NULL;
813 amwait_t wait_status;
814 int starpid;
815 amregex_t *rp;
816 times_t start_time;
817 int level = 0;
818 GSList *levels = NULL;
819 char *option;
820 char *star_realpath = NULL;
821 message_t *message;
822
823 if (!argument->level) {
824 fprintf(stderr, "ERROR No level argument\n");
825 error(_("No level argument"));
826 }
827 if (!argument->dle.disk) {
828 fprintf(stderr, "ERROR No disk argument\n");
829 error(_("No disk argument"));
830 }
831 if (!argument->dle.device) {
832 fprintf(stderr, "ERROR No device argument\n");
833 error(_("No device argument"));
834 }
835
836 if (argument->dle.include_list &&
837 argument->dle.include_list->nb_element >= 0) {
838 fprintf(stderr, "ERROR include-list not supported for backup\n");
839 }
840
841 if (check_device(argument) == 0) {
842 return;
843 }
844
845 if ((option = validate_command_options(argument))) {
846 fprintf(stdout, "ERROR Invalid '%s' COMMAND-OPTIONS\n", option);
847 error("Invalid '%s' COMMAND-OPTIONS", option);
848 }
849
850 if (argument->calcsize) {
851 char *dirname;
852
853 if (star_target) {
854 dirname = star_target;
855 } else {
856 dirname = argument->dle.device;
857 }
858 run_calcsize(argument->config, "STAR", argument->dle.disk, dirname,
859 argument->level, NULL, NULL);
860 return;
861 }
862
863 qdisk = quote_string(argument->dle.disk);
864 if (!star_path) {
865 errmsg = g_strdup(_("STAR-PATH not defined"));
866 goto common_error;
867 }
868 if ((message = check_exec_for_suid_message("STAR_PATH", star_path, &star_realpath))) {
869 errmsg = g_strdup(get_message(message));
870 delete_message(message);
871 goto common_error;
872 }
873
874 if ((message = check_file_message(star_tardumps, W_OK))) {
875 if (message_get_severity(message) > MSG_INFO) {
876 errmsg = g_strdup(get_message(message));
877 delete_message(message);
878 goto common_error;
879 }
880 delete_message(message);
881 }
882
883 start_time = curclock();
884
885 qdisk = quote_string(argument->dle.disk);
886 for (levels = argument->level; levels != NULL; levels = levels->next) {
887 level = GPOINTER_TO_INT(levels->data);
888 argv_ptr = amstar_build_argv(star_realpath, argument, level, CMD_ESTIMATE, NULL);
889
890 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
891 errmsg = g_strdup_printf(_("Cannot access /dev/null : %s"),
892 strerror(errno));
893 goto common_error;
894 }
895
896 starpid = pipespawnv(star_realpath, STDERR_PIPE, 1,
897 &nullfd, &nullfd, &pipefd,
898 (char **)argv_ptr->pdata);
899
900 dumpout = fdopen(pipefd,"r");
901 if (!dumpout) {
902 errmsg = g_strdup_printf(_("Can't fdopen: %s"), strerror(errno));
903 aclose(nullfd);
904 goto common_error;
905 }
906
907 size = (off_t)-1;
908 while (size < 0 && (fgets(line, sizeof(line), dumpout)) != NULL) {
909 if (strlen(line) > 0 && line[strlen(line)-1] == '\n') {
910 /* remove trailling \n */
911 line[strlen(line)-1] = '\0';
912 }
913 if (line[0] == '\0')
914 continue;
915 dbprintf("%s\n", line);
916 /* check for size match */
917 /*@ignore@*/
918 for(rp = re_table; rp->regex != NULL; rp++) {
919 if(match(rp->regex, line)) {
920 if (rp->typ == DMP_SIZE) {
921 size = ((the_num(line, rp->field)*rp->scale+1023.0)/1024.0);
922 if(size < 0.0)
923 size = 1.0; /* found on NeXT -- sigh */
924 }
925 break;
926 }
927 }
928 /*@end@*/
929 }
930
931 while ((fgets(line, sizeof(line), dumpout)) != NULL) {
932 dbprintf("%s", line);
933 }
934
935 dbprintf(".....\n");
936 dbprintf(_("estimate time for %s level %d: %s\n"),
937 qdisk,
938 level,
939 walltime_str(timessub(curclock(), start_time)));
940 if(size == (off_t)-1) {
941 errmsg = g_strdup_printf(_("no size line match in %s output"),
942 star_realpath);
943 dbprintf(_("%s for %s\n"), errmsg, qdisk);
944 dbprintf(".....\n");
945 qerrmsg = quote_string(errmsg);
946 fprintf(stdout, "ERROR %s\n", qerrmsg);
947 amfree(errmsg);
948 amfree(qerrmsg);
949 } else if(size == (off_t)0 && argument->level == 0) {
950 dbprintf(_("possible %s problem -- is \"%s\" really empty?\n"),
951 star_realpath, argument->dle.disk);
952 dbprintf(".....\n");
953 }
954 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
955 qdisk,
956 level,
957 (long long)size);
958
959 (void)kill(-starpid, SIGTERM);
960
961 dbprintf(_("waiting for %s \"%s\" child\n"), star_realpath, qdisk);
962 waitpid(starpid, &wait_status, 0);
963 if (WIFSIGNALED(wait_status)) {
964 amfree(errmsg);
965 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
966 star_realpath, WTERMSIG(wait_status), dbfn());
967 dbprintf(_("%s for %s\n"), errmsg, qdisk);
968 dbprintf(".....\n");
969 qerrmsg = quote_string(errmsg);
970 fprintf(stdout, "ERROR %s\n", qerrmsg);
971 amfree(errmsg);
972 amfree(qerrmsg);
973 } else if (WIFEXITED(wait_status)) {
974 if (WEXITSTATUS(wait_status) != 0) {
975 amfree(errmsg);
976 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
977 star_realpath, WEXITSTATUS(wait_status), dbfn());
978 dbprintf(_("%s for %s\n"), errmsg, qdisk);
979 dbprintf(".....\n");
980 qerrmsg = quote_string(errmsg);
981 fprintf(stdout, "ERROR %s\n", qerrmsg);
982 amfree(errmsg);
983 amfree(qerrmsg);
984 } else {
985 /* Normal exit */
986 }
987 } else {
988 amfree(errmsg);
989 errmsg = g_strdup_printf(_("%s got bad exit: see %s"), star_realpath, dbfn());
990 dbprintf(_("%s for %s\n"), errmsg, qdisk);
991 dbprintf(".....\n");
992 qerrmsg = quote_string(errmsg);
993 fprintf(stdout, "ERROR %s\n", qerrmsg);
994 amfree(errmsg);
995 amfree(qerrmsg);
996 }
997 dbprintf(_("after %s %s wait\n"), star_realpath, qdisk);
998
999 g_ptr_array_free_full(argv_ptr);
1000
1001 aclose(nullfd);
1002 afclose(dumpout);
1003
1004 fprintf(stdout, "%d %lld 1\n", level, (long long)size);
1005 }
1006 amfree(qdisk);
1007 amfree(star_realpath);
1008 return;
1009
1010 common_error:
1011 dbprintf("%s\n", errmsg);
1012 qerrmsg = quote_string(errmsg);
1013 amfree(qdisk);
1014 dbprintf("%s", errmsg);
1015 fprintf(stdout, "ERROR %s\n", qerrmsg);
1016 amfree(errmsg);
1017 amfree(qerrmsg);
1018 amfree(star_realpath);
1019 }
1020
1021 static void
1022 amstar_backup(
1023 application_argument_t *argument)
1024 {
1025 int dumpin;
1026 char *qdisk;
1027 char line[32768];
1028 amregex_t *rp;
1029 off_t dump_size = -1;
1030 char *type;
1031 char startchr;
1032 GPtrArray *argv_ptr;
1033 int starpid;
1034 int dataf = 1;
1035 int indexf = 4;
1036 int outf;
1037 FILE *indexstream = NULL;
1038 FILE *outstream;
1039 int level;
1040 regex_t regex_root;
1041 regex_t regex_dir;
1042 regex_t regex_file;
1043 regex_t regex_special;
1044 regex_t regex_symbolic;
1045 regex_t regex_hard;
1046 char *option;
1047 char *star_realpath = NULL;
1048 message_t *message;
1049
1050 if (!argument->level) {
1051 fprintf(mesgstream, "sendbackup: error [No level argument]\n");
1052 error(_("No level argument"));
1053 }
1054 if (!argument->dle.disk) {
1055 fprintf(mesgstream, "sendbackup: error [No disk argument]\n");
1056 error(_("No disk argument"));
1057 }
1058 if (!argument->dle.device) {
1059 fprintf(mesgstream, "sendbackup: error [No device argument]\n");
1060 error(_("No device argument"));
1061 }
1062
1063 if ((message = check_exec_for_suid_message("STAR_PATH", star_path, &star_realpath))) {
1064 fprintf(mesgstream, "sendbackup: error [%s]", get_message(message));
1065 exit(1);
1066 }
1067
1068 if ((message = check_file_message(star_tardumps, W_OK))) {
1069 if (message_get_severity(message) > MSG_INFO) {
1070 fprintf(mesgstream, "sendbackup: error [%s]", get_message(message));
1071 delete_message(message);
1072 exit(1);
1073 }
1074 delete_message(message);
1075 }
1076
1077 if ((option = validate_command_options(argument))) {
1078 fprintf(mesgstream, "sendbackup: error [Invalid '%s' COMMAND-OPTIONS]\n", option);
1079 exit(1);
1080 }
1081
1082 if (argument->dle.include_list &&
1083 argument->dle.include_list->nb_element >= 0) {
1084 fprintf(mesgstream, "sendbackup: error [include-list not supported for backup]\n");
1085 }
1086
1087 level = GPOINTER_TO_INT(argument->level->data);
1088
1089 qdisk = quote_string(argument->dle.disk);
1090
1091 argv_ptr = amstar_build_argv(star_realpath, argument, level, CMD_BACKUP, mesgstream);
1092
1093 starpid = pipespawnv(star_realpath, STDIN_PIPE|STDERR_PIPE, 1,
1094 &dumpin, &dataf, &outf, (char **)argv_ptr->pdata);
1095
1096 g_ptr_array_free_full(argv_ptr);
1097 /* close the write ends of the pipes */
1098 aclose(dumpin);
1099 aclose(dataf);
1100 if (argument->dle.create_index) {
1101 indexstream = fdopen(indexf, "w");
1102 if (!indexstream) {
1103 fprintf(mesgstream, "sendbackup: error [error indexstream(%d): %s]\n", indexf, strerror(errno));
1104 error(_("error indexstream(%d): %s\n"), indexf, strerror(errno));
1105 }
1106 }
1107 outstream = fdopen(outf, "r");
1108 if (!outstream) {
1109 fprintf(mesgstream, "sendbackup: error [error outstream(%d): %s]\n", outf, strerror(errno));
1110 error(_("error outstream(%d): %s\n"), outf, strerror(errno));
1111 }
1112
1113 regcomp(&regex_root, "^a \\.\\/ directory$", REG_EXTENDED|REG_NEWLINE);
1114 regcomp(&regex_dir, "^a (.*) directory$", REG_EXTENDED|REG_NEWLINE);
1115 regcomp(&regex_file, "^a (.*) (.*) bytes", REG_EXTENDED|REG_NEWLINE);
1116 regcomp(&regex_special, "^a (.*) special", REG_EXTENDED|REG_NEWLINE);
1117 regcomp(&regex_symbolic, "^a (.*) symbolic", REG_EXTENDED|REG_NEWLINE);
1118 regcomp(&regex_hard, "^a (.*) link to", REG_EXTENDED|REG_NEWLINE);
1119
1120 while ((fgets(line, sizeof(line), outstream)) != NULL) {
1121 regmatch_t regmatch[3];
1122
1123 if (strlen(line) > 0 && line[strlen(line)-1] == '\n') {
1124 /* remove trailling \n */
1125 line[strlen(line)-1] = '\0';
1126 }
1127
1128 if (regexec(&regex_root, line, 1, regmatch, 0) == 0) {
1129 if (argument->dle.create_index)
1130 fprintf(indexstream, "%s\n", "/");
1131 continue;
1132 }
1133
1134 if (regexec(&regex_dir, line, 3, regmatch, 0) == 0) {
1135 if (argument->dle.create_index && regmatch[1].rm_so == 2) {
1136 line[regmatch[1].rm_eo]='\0';
1137 fprintf(indexstream, "/%s\n", &line[regmatch[1].rm_so]);
1138 }
1139 continue;
1140 }
1141
1142 if (regexec(&regex_file, line, 3, regmatch, 0) == 0 ||
1143 regexec(&regex_special, line, 3, regmatch, 0) == 0 ||
1144 regexec(&regex_symbolic, line, 3, regmatch, 0) == 0 ||
1145 regexec(&regex_hard, line, 3, regmatch, 0) == 0) {
1146 if (argument->dle.create_index && regmatch[1].rm_so == 2) {
1147 line[regmatch[1].rm_eo]='\0';
1148 fprintf(indexstream, "/%s\n", &line[regmatch[1].rm_so]);
1149 }
1150 continue;
1151 }
1152
1153 for (rp = re_table; rp->regex != NULL; rp++) {
1154 if (match(rp->regex, line)) {
1155 break;
1156 }
1157 }
1158 if (rp->typ == DMP_SIZE) {
1159 dump_size = (off_t)((the_num(line, rp->field)* rp->scale+1023.0)/1024.0);
1160 }
1161 switch (rp->typ) {
1162 case DMP_IGNORE:
1163 continue;
1164 case DMP_NORMAL:
1165 type = "normal";
1166 startchr = '|';
1167 break;
1168 case DMP_STRANGE:
1169 type = "strange";
1170 startchr = '?';
1171 break;
1172 case DMP_SIZE:
1173 type = "size";
1174 startchr = '|';
1175 break;
1176 case DMP_ERROR:
1177 type = "error";
1178 startchr = '?';
1179 break;
1180 default:
1181 type = "unknown";
1182 startchr = '!';
1183 break;
1184 }
1185 dbprintf("%3d: %7s(%c): %s\n", rp->srcline, type, startchr, line);
1186 fprintf(mesgstream,"%c %s\n", startchr, line);
1187 }
1188 fclose(outstream);
1189
1190 regfree(&regex_root);
1191 regfree(&regex_dir);
1192 regfree(&regex_file);
1193 regfree(&regex_special);
1194 regfree(&regex_symbolic);
1195 regfree(&regex_hard);
1196
1197 dbprintf(_("gnutar: %s: pid %ld\n"), star_realpath, (long)starpid);
1198
1199 dbprintf("sendbackup: size %lld\n", (long long)dump_size);
1200 fprintf(mesgstream, "sendbackup: size %lld\n", (long long)dump_size);
1201
1202 fclose(mesgstream);
1203 if (argument->dle.create_index)
1204 fclose(indexstream);
1205
1206 amfree(qdisk);
1207 amfree(star_realpath);
1208 }
1209
1210 static void
1211 amstar_restore(
1212 application_argument_t *argument)
1213 {
1214 GPtrArray *argv_ptr = g_ptr_array_new();
1215 int j;
1216 char *star_realpath = NULL;
1217 message_t *message;
1218 int datain = 0;
1219 int outf;
1220 int errf;
1221 int star_pid;
1222 amwait_t wait_status;
1223 char *errmsg = NULL;
1224
1225 if (!star_path) {
1226 error(_("STAR-PATH not defined"));
1227 }
1228
1229 if ((message = check_exec_for_suid_message("STAR_PATH", star_path, &star_realpath))) {
1230 fprintf(stderr, "%s\n", get_message(message));
1231 delete_message(message);
1232 exit(1);
1233 }
1234
1235 if (!security_allow_to_restore()) {
1236 error("The user is not allowed to restore files");
1237 }
1238
1239 g_ptr_array_add(argv_ptr, g_strdup(star_realpath));
1240 if (star_target) {
1241 struct stat stat_buf;
1242 if(stat(star_target, &stat_buf) != 0) {
1243 fprintf(stderr,"can not stat directory %s: %s\n", star_target, strerror(errno));
1244 exit(1);
1245 }
1246 if (!S_ISDIR(stat_buf.st_mode)) {
1247 fprintf(stderr,"%s is not a directory\n", star_target);
1248 exit(1);
1249 }
1250 if (access(star_target, W_OK) != 0 ) {
1251 fprintf(stderr, "Can't write to %s: %s\n", star_target, strerror(errno));
1252 exit(1);
1253 }
1254
1255 g_ptr_array_add(argv_ptr, g_strdup("-C"));
1256 g_ptr_array_add(argv_ptr, g_strdup(star_target));
1257 }
1258 g_ptr_array_add(argv_ptr, g_strdup("-x"));
1259 g_ptr_array_add(argv_ptr, g_strdup("-v"));
1260 g_ptr_array_add(argv_ptr, g_strdup("-xattr"));
1261 g_ptr_array_add(argv_ptr, g_strdup("-acl"));
1262 g_ptr_array_add(argv_ptr, g_strdup("errctl=WARN|SAMEFILE|SETTIME|DIFF|SETACL|SETXATTR|SETMODE|BADACL *"));
1263 g_ptr_array_add(argv_ptr, g_strdup("-no-fifo"));
1264 g_ptr_array_add(argv_ptr, g_strdup("-f"));
1265 g_ptr_array_add(argv_ptr, g_strdup("-"));
1266
1267 if (argument->dle.exclude_list &&
1268 argument->dle.exclude_list->nb_element == 1) {
1269 g_ptr_array_add(argv_ptr, g_strdup("-exclude-from"));
1270 g_ptr_array_add(argv_ptr,
1271 g_strdup(argument->dle.exclude_list->first->name));
1272 }
1273
1274 if (argument->dle.include_list &&
1275 argument->dle.include_list->nb_element == 1) {
1276 FILE *include_list = fopen(argument->dle.include_list->first->name, "r");
1277 if (include_list) {
1278 char line[2*PATH_MAX+2];
1279 while (fgets(line, 2*PATH_MAX, include_list)) {
1280 line[strlen(line)-1] = '\0'; /* remove '\n' */
1281 if (g_str_has_prefix(line, "./"))
1282 g_ptr_array_add(argv_ptr, g_strdup(line+2)); /* remove ./ */
1283 else if (!g_str_equal(line, "."))
1284 g_ptr_array_add(argv_ptr, g_strdup(line));
1285 }
1286 fclose(include_list);
1287 }
1288 }
1289 for (j=1; j< argument->argc; j++) {
1290 if (g_str_has_prefix(argument->argv[j], "./"))
1291 g_ptr_array_add(argv_ptr, g_strdup(argument->argv[j]+2));/*remove ./ */
1292 else if (!g_str_equal(argument->argv[j], "."))
1293 g_ptr_array_add(argv_ptr, g_strdup(argument->argv[j]));
1294 }
1295 g_ptr_array_add(argv_ptr, NULL);
1296
1297 debug_executing(argv_ptr);
1298 star_pid = pipespawnv(star_realpath, STDOUT_PIPE|STDERR_PIPE, 1,
1299 &datain, &outf, &errf, (char **)argv_ptr->pdata);
1300 aclose(datain);
1301
1302 read_fd(outf, "restore stdout", &read_text);
1303 read_fd(errf, "restore stderr", &read_text);
1304
1305 event_loop(0);
1306 waitpid(star_pid, &wait_status, 0);
1307 if (WIFSIGNALED(wait_status)) {
1308 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
1309 star_path, WTERMSIG(wait_status), dbfn());
1310 amstar_exit_value = 1;
1311 } else if (WIFEXITED(wait_status)) {
1312 if (WEXITSTATUS(wait_status) != 0) {
1313 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
1314 star_path, WEXITSTATUS(wait_status), dbfn());
1315 amstar_exit_value = 1;
1316 } else {
1317 /* Normal exit */
1318 }
1319 } else {
1320 errmsg = g_strdup_printf(_("%s got bad exit: see %s"),
1321 star_path, dbfn());
1322 amstar_exit_value = 1;
1323 }
1324
1325 if (errmsg) {
1326 g_debug("%s", errmsg);
1327 fprintf(stderr,"%s\n", errmsg);
1328 }
1329 g_ptr_array_free_full(argv_ptr);
1330 return;
1331 }
1332
1333 static void
1334 amstar_validate(
1335 application_argument_t *argument G_GNUC_UNUSED)
1336 {
1337 char *cmd;
1338 GPtrArray *argv_ptr = g_ptr_array_new();
1339 char buf[32768];
1340 int datain = 0;
1341 int outf;
1342 int errf;
1343 int star_pid;
1344 amwait_t wait_status;
1345 char *errmsg = NULL;
1346
1347 set_root_privs(-1);
1348
1349 if (!star_path) {
1350 dbprintf("STAR-PATH not set; Piping to /dev/null\n");
1351 fprintf(stderr,"STAR-PATH not set; Piping to /dev/null\n");
1352 goto pipe_to_null;
1353 }
1354
1355 cmd = g_strdup(star_path);
1356
1357 g_ptr_array_add(argv_ptr, g_strdup(star_path));
1358 g_ptr_array_add(argv_ptr, g_strdup("-t"));
1359 g_ptr_array_add(argv_ptr, g_strdup("-f"));
1360 g_ptr_array_add(argv_ptr, g_strdup("-"));
1361 g_ptr_array_add(argv_ptr, NULL);
1362
1363 debug_executing(argv_ptr);
1364 star_pid = pipespawnv(cmd, STDOUT_PIPE|STDERR_PIPE, 0,
1365 &datain, &outf, &errf, (char **)argv_ptr->pdata);
1366 aclose(datain);
1367
1368 read_fd(outf, "validate stdout", &read_text);
1369 read_fd(errf, "validate stderr", &read_text);
1370
1371 event_loop(0);
1372 waitpid(star_pid, &wait_status, 0);
1373 if (WIFSIGNALED(wait_status)) {
1374 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
1375 star_path, WTERMSIG(wait_status), dbfn());
1376 amstar_exit_value = 1;
1377 } else if (WIFEXITED(wait_status)) {
1378 if (WEXITSTATUS(wait_status) != 0) {
1379 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
1380 star_path, WEXITSTATUS(wait_status), dbfn());
1381 amstar_exit_value = 1;
1382 } else {
1383 /* Normal exit */
1384 }
1385 } else {
1386 errmsg = g_strdup_printf(_("%s got bad exit: see %s"),
1387 star_path, dbfn());
1388 amstar_exit_value = 1;
1389 }
1390
1391 if (errmsg) {
1392 g_debug("%s", errmsg);
1393 fprintf(stderr,"%s\n", errmsg);
1394 }
1395 g_ptr_array_free_full(argv_ptr);
1396 return;
1397
1398 pipe_to_null:
1399 while (read(0, buf, 32768) > 0) {
1400 }
1401 g_ptr_array_free_full(argv_ptr);
1402 }
1403
1404 static void
1405 amstar_index(
1406 application_argument_t *argument G_GNUC_UNUSED)
1407 {
1408 char *cmd;
1409 GPtrArray *argv_ptr = g_ptr_array_new();
1410 int datain = 0;
1411 int indexf;
1412 int errf = 2;
1413 pid_t tarpid;
1414 FILE *indexstream;
1415 char line[32768];
1416 amwait_t wait_status;
1417 char *errmsg = NULL;
1418
1419 if (!star_path) {
1420 dbprintf("STAR-PATH not set; Piping to /dev/null\n");
1421 fprintf(stderr,"STAR-PATH not set; Piping to /dev/null\n");
1422 while (read(0, line, 32768) > 0) {
1423 }
1424 exit(1);
1425 }
1426
1427 cmd = g_strdup(star_path);
1428
1429 g_ptr_array_add(argv_ptr, g_strdup(star_path));
1430 g_ptr_array_add(argv_ptr, g_strdup("-t"));
1431 g_ptr_array_add(argv_ptr, g_strdup("-f"));
1432 g_ptr_array_add(argv_ptr, g_strdup("-"));
1433 g_ptr_array_add(argv_ptr, NULL);
1434
1435 tarpid = pipespawnv(cmd, STDOUT_PIPE, 0,
1436 &datain, &indexf, &errf, (char **)argv_ptr->pdata);
1437 aclose(datain);
1438
1439 indexstream = fdopen(indexf, "r");
1440 if (!indexstream) {
1441 error(_("error indexstream(%d): %s\n"), indexf, strerror(errno));
1442 }
1443
1444 while (fgets(line, sizeof(line), indexstream) != NULL) {
1445 if (strlen(line) > 0 && line[strlen(line)-1] == '\n') {
1446 /* remove trailling \n */
1447 line[strlen(line)-1] = '\0';
1448 }
1449 if (*line == '.' && *(line+1) == '/') { /* filename */
1450 fprintf(stdout, "%s\n", &line[1]); /* remove . */
1451 }
1452 }
1453 fclose(indexstream);
1454 waitpid(tarpid, &wait_status, 0);
1455 if (WIFSIGNALED(wait_status)) {
1456 errmsg = g_strdup_printf(_("%s terminated with signal %d: see %s"),
1457 cmd, WTERMSIG(wait_status), dbfn());
1458 } else if (WIFEXITED(wait_status)) {
1459 if (WEXITSTATUS(wait_status) > 0) {
1460 errmsg = g_strdup_printf(_("%s exited with status %d: see %s"),
1461 cmd, WEXITSTATUS(wait_status), dbfn());
1462 } else {
1463 /* Normal exit */
1464 }
1465 } else {
1466 errmsg = g_strdup_printf(_("%s got bad exit: see %s"),
1467 cmd, dbfn());
1468 }
1469 dbprintf(_("amstar: %s: pid %ld\n"), cmd, (long)tarpid);
1470 if (errmsg) {
1471 dbprintf("%s", errmsg);
1472 fprintf(stderr, "error [%s]\n", errmsg);
1473 }
1474
1475 amfree(cmd);
1476 }
1477
1478 static GPtrArray *amstar_build_argv(
1479 char *star_realpath,
1480 application_argument_t *argument,
1481 int level,
1482 int command,
1483 FILE *mesgstream)
1484 {
1485 char *dirname;
1486 char *fsname;
1487 char levelstr[NUM_STR_SIZE+7];
1488 GPtrArray *argv_ptr = g_ptr_array_new();
1489 char *s;
1490 char *tardumpfile;
1491 GSList *copt;
1492
1493 if (star_target) {
1494 dirname = star_target;
1495 } else {
1496 dirname = argument->dle.device;
1497 }
1498 fsname = g_strjoin(NULL, "fs-name=", dirname, NULL);
1499 for (s = fsname; *s != '\0'; s++) {
1500 if (iscntrl((int)*s))
1501 *s = '-';
1502 }
1503 snprintf(levelstr, sizeof(levelstr), "-level=%d", level);
1504
1505 if (star_dle_tardumps) {
1506 char *sdisk = sanitise_filename(argument->dle.disk);
1507 tardumpfile = g_strjoin(NULL, star_tardumps, sdisk, NULL);
1508 amfree(sdisk);
1509 } else {
1510 tardumpfile = g_strdup(star_tardumps);
1511 }
1512
1513 g_ptr_array_add(argv_ptr, g_strdup(star_realpath));
1514
1515 g_ptr_array_add(argv_ptr, g_strdup("-c"));
1516 g_ptr_array_add(argv_ptr, g_strdup("-f"));
1517 if (command == CMD_ESTIMATE) {
1518 g_ptr_array_add(argv_ptr, g_strdup("/dev/null"));
1519 } else {
1520 g_ptr_array_add(argv_ptr, g_strdup("-"));
1521 }
1522 g_ptr_array_add(argv_ptr, g_strdup("-C"));
1523
1524 #if defined(__CYGWIN__)
1525 {
1526 char tmppath[PATH_MAX];
1527
1528 cygwin_conv_to_full_posix_path(dirname, tmppath);
1529 g_ptr_array_add(argv_ptr, g_strdup(tmppath));
1530 }
1531 #else
1532 g_ptr_array_add(argv_ptr, g_strdup(dirname));
1533 #endif
1534 g_ptr_array_add(argv_ptr, g_strdup(fsname));
1535 if (star_onefilesystem)
1536 g_ptr_array_add(argv_ptr, g_strdup("-xdev"));
1537 g_ptr_array_add(argv_ptr, g_strdup("-link-dirs"));
1538 g_ptr_array_add(argv_ptr, g_strdup(levelstr));
1539 g_ptr_array_add(argv_ptr, g_strconcat("tardumps=", tardumpfile, NULL));
1540 if (command == CMD_BACKUP)
1541 g_ptr_array_add(argv_ptr, g_strdup("-wtardumps"));
1542
1543 g_ptr_array_add(argv_ptr, g_strdup("-xattr"));
1544 if (star_acl)
1545 g_ptr_array_add(argv_ptr, g_strdup("-acl"));
1546 g_ptr_array_add(argv_ptr, g_strdup("H=exustar"));
1547 g_ptr_array_add(argv_ptr, g_strdup("errctl=WARN|SAMEFILE|DIFF|GROW|SHRINK|SPECIALFILE|GETXATTR|BADACL *"));
1548 if (star_sparse)
1549 g_ptr_array_add(argv_ptr, g_strdup("-sparse"));
1550 g_ptr_array_add(argv_ptr, g_strdup("-dodesc"));
1551
1552 if (command == CMD_BACKUP && argument->dle.create_index)
1553 g_ptr_array_add(argv_ptr, g_strdup("-v"));
1554
1555 if (((argument->dle.include_file &&
1556 argument->dle.include_file->nb_element >= 1) ||
1557 (argument->dle.include_list &&
1558 argument->dle.include_list->nb_element >= 1)) &&
1559 ((argument->dle.exclude_file &&
1560 argument->dle.exclude_file->nb_element >= 1) ||
1561 (argument->dle.exclude_list &&
1562 argument->dle.exclude_list->nb_element >= 1))) {
1563
1564 if (mesgstream && command == CMD_BACKUP) {
1565 fprintf(mesgstream, "? include and exclude specified, disabling exclude\n");
1566 }
1567 free_sl(argument->dle.exclude_file);
1568 argument->dle.exclude_file = NULL;
1569 free_sl(argument->dle.exclude_list);
1570 argument->dle.exclude_list = NULL;
1571 }
1572
1573 if ((argument->dle.exclude_file &&
1574 argument->dle.exclude_file->nb_element >= 1) ||
1575 (argument->dle.exclude_list &&
1576 argument->dle.exclude_list->nb_element >= 1)) {
1577 g_ptr_array_add(argv_ptr, g_strdup("-match-tree"));
1578 g_ptr_array_add(argv_ptr, g_strdup("-not"));
1579 }
1580 if (argument->dle.exclude_file &&
1581 argument->dle.exclude_file->nb_element >= 1) {
1582 sle_t *excl;
1583 for (excl = argument->dle.exclude_file->first; excl != NULL;
1584 excl = excl->next) {
1585 char *ex;
1586 if (strncmp(excl->name, "./", 2) == 0) {
1587 ex = g_strdup_printf("pat=%s", excl->name+2);
1588 } else {
1589 ex = g_strdup_printf("pat=%s", excl->name);
1590 }
1591 g_ptr_array_add(argv_ptr, ex);
1592 }
1593 }
1594 if (argument->dle.exclude_list &&
1595 argument->dle.exclude_list->nb_element >= 1) {
1596 sle_t *excl;
1597 for (excl = argument->dle.exclude_list->first; excl != NULL;
1598 excl = excl->next) {
1599 char *exclname = fixup_relative(excl->name, argument->dle.device);
1600 FILE *exclude;
1601 char *aexc;
1602 if ((exclude = fopen(exclname, "r")) != NULL) {
1603 while ((aexc = pgets(exclude)) != NULL) {
1604 if (aexc[0] != '\0') {
1605 char *ex;
1606 if (strncmp(aexc, "./", 2) == 0) {
1607 ex = g_strdup_printf("pat=%s", aexc+2);
1608 } else {
1609 ex = g_strdup_printf("pat=%s", aexc);
1610 }
1611 g_ptr_array_add(argv_ptr, ex);
1612 }
1613 amfree(aexc);
1614 }
1615 fclose(exclude);
1616 }
1617 amfree(exclname);
1618 }
1619 }
1620
1621 if (argument->dle.include_file &&
1622 argument->dle.include_file->nb_element >= 1) {
1623 sle_t *excl;
1624 for (excl = argument->dle.include_file->first; excl != NULL;
1625 excl = excl->next) {
1626 char *ex;
1627 if (strncmp(excl->name, "./", 2) == 0) {
1628 ex = g_strdup_printf("pat=%s", excl->name+2);
1629 } else {
1630 ex = g_strdup_printf("pat=%s", excl->name);
1631 }
1632 g_ptr_array_add(argv_ptr, ex);
1633 }
1634 }
1635 if (argument->dle.include_list &&
1636 argument->dle.include_list->nb_element >= 1) {
1637 sle_t *excl;
1638 for (excl = argument->dle.include_list->first; excl != NULL;
1639 excl = excl->next) {
1640 char *exclname = fixup_relative(excl->name, argument->dle.device);
1641 FILE *include;
1642 char *aexc;
1643 if ((include = fopen(exclname, "r")) != NULL) {
1644 while ((aexc = pgets(include)) != NULL) {
1645 if (aexc[0] != '\0') {
1646 char *ex;
1647 if (strncmp(aexc, "./", 2) == 0) {
1648 ex = g_strdup_printf("pat=%s", aexc+2);
1649 } else {
1650 ex = g_strdup_printf("pat=%s", aexc);
1651 }
1652 g_ptr_array_add(argv_ptr, ex);
1653 }
1654 amfree(aexc);
1655 }
1656 fclose(include);
1657 }
1658 amfree(exclname);
1659 }
1660 }
1661
1662 /* It is best to place command_options at the and of command line.
1663 * For example '-find' option requires that it is the last option used.
1664 * See: http://cdrecord.berlios.de/private/man/star/star.1.html
1665 */
1666 for (copt = argument->command_options; copt != NULL; copt = copt->next) {
1667 g_ptr_array_add(argv_ptr, g_strdup((char *)copt->data));
1668 }
1669
1670 g_ptr_array_add(argv_ptr, g_strdup("."));
1671
1672 g_ptr_array_add(argv_ptr, NULL);
1673
1674 amfree(tardumpfile);
1675 amfree(fsname);
1676
1677 return(argv_ptr);
1678 }
1679
1680 static int
1681 check_device(
1682 application_argument_t *argument)
1683 {
1684 char *qdevice;
1685 struct stat stat_buf;
1686
1687 qdevice = quote_string(argument->dle.device);
1688 set_root_privs(1);
1689 if (!stat(argument->dle.device, &stat_buf)) {
1690 if (!S_ISDIR(stat_buf.st_mode)) {
1691 set_root_privs(0);
1692 g_fprintf(stderr, _("ERROR %s is not a directory\n"), qdevice);
1693 amfree(qdevice);
1694 return 0;
1695 }
1696 } else {
1697 set_root_privs(0);
1698 g_fprintf(stderr, _("ERROR can not stat %s: %s\n"), qdevice,
1699 strerror(errno));
1700 amfree(qdevice);
1701 return 0;
1702 }
1703 if (access(argument->dle.device, R_OK|X_OK) == -1) {
1704 set_root_privs(0);
1705 g_fprintf(stderr, _("ERROR can not access %s: %s\n"),
1706 argument->dle.device, strerror(errno));
1707 amfree(qdevice);
1708 return 0;
1709 }
1710 set_root_privs(0);
1711 amfree(qdevice);
1712 return 1;
1713 }
1714