2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: sendsize.c 10421 2008-03-06 18:48:30Z martineau $
29 * send estimated backup sizes using dump
34 #include "pipespawn.h"
35 #include "amfeatures.h"
36 #include "amandates.h"
40 #include "client_util.h"
47 #define sendsize_debug(i, ...) do { \
48 if ((i) <= debug_sebdsize) { \
49 dbprintf(__VA_ARGS__); \
54 # define SETPGRP setpgid(getpid(), getpid())
55 # define SETPGRP_FAILED() do { \
56 dbprintf(_("setpgid(%ld,%ld) failed: %s\n"), \
57 (long)getpid(), (long)getpid(), strerror(errno)); \
61 #if defined(SETPGRP_VOID)
62 # define SETPGRP setpgrp()
63 # define SETPGRP_FAILED() do { \
64 dbprintf(_("setpgrp() failed: %s\n"), strerror(errno)); \
68 # define SETPGRP setpgrp(0, getpid())
69 # define SETPGRP_FAILED() do { \
70 dbprintf(_("setpgrp(0,%ld) failed: %s\n"), \
71 (long)getpid(), strerror(errno)); \
77 typedef struct level_estimates_s {
81 int server; /* server can do estimate */
84 typedef struct disk_estimates_s {
85 struct disk_estimates_s *next;
93 level_estimate_t est[DUMP_LEVELS];
96 disk_estimates_t *est_list;
98 static am_feature_t *our_features = NULL;
99 static char *our_feature_string = NULL;
100 static g_option_t *g_options = NULL;
101 static gboolean amandates_started = FALSE;
103 /* local functions */
104 int main(int argc, char **argv);
105 void dle_add_diskest(dle_t *dle);
106 void calc_estimates(disk_estimates_t *est);
107 void free_estimates(disk_estimates_t *est);
108 void dump_calc_estimates(disk_estimates_t *);
109 void star_calc_estimates(disk_estimates_t *);
110 void smbtar_calc_estimates(disk_estimates_t *);
111 void gnutar_calc_estimates(disk_estimates_t *);
112 void application_api_calc_estimate(disk_estimates_t *);
113 void generic_calc_estimates(disk_estimates_t *);
122 disk_estimates_t *est;
123 disk_estimates_t *est1;
124 disk_estimates_t *est_prev;
128 char *err_extra = NULL;
134 char *qamdevice = NULL;
139 (void)argc; /* Quiet unused parameter warning */
140 (void)argv; /* Quiet unused parameter warning */
145 * Configure program for internationalization:
146 * 1) Only set the message locale for now.
147 * 2) Set textdomain for all amanda related programs to "amanda"
148 * We don't want to be forced to support dozens of message catalogs.
150 setlocale(LC_MESSAGES, "C");
151 textdomain("amanda");
157 set_pname("sendsize");
159 /* Don't die when child closes pipe */
160 signal(SIGPIPE, SIG_IGN);
162 add_amanda_log_handler(amanda_log_stderr);
163 add_amanda_log_handler(amanda_log_syslog);
164 dbopen(DBG_SUBDIR_CLIENT);
166 dbprintf(_("version %s\n"), VERSION);
168 our_features = am_init_feature_set();
169 our_feature_string = am_feature_to_string(our_features);
171 config_init(CONFIG_INIT_CLIENT, NULL);
172 /* (check for config errors comes later) */
174 check_running_as(RUNNING_AS_CLIENT_LOGIN);
176 /* handle all service requests */
178 for(; (line = agets(stdin)) != NULL; free(line)) {
181 if(strncmp_const(line, "OPTIONS ") == 0) {
182 g_options = parse_g_options(line+8, 1);
183 if(!g_options->hostname) {
184 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
185 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
186 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
189 g_printf("OPTIONS ");
190 if(am_has_feature(g_options->features, fe_rep_options_features)) {
191 g_printf("features=%s;", our_feature_string);
193 if(am_has_feature(g_options->features, fe_rep_options_maxdumps)) {
194 g_printf("maxdumps=%d;", g_options->maxdumps);
196 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
197 g_printf("hostname=%s;", g_options->hostname);
202 if (g_options->config) {
203 /* overlay this configuration on the existing (nameless) configuration */
204 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
207 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
210 /* check for any config errors now */
211 if (config_errors(&errlist) >= CFGERR_ERRORS) {
212 char *errstr = config_errors_to_error_string(errlist);
213 g_printf("%s\n", errstr);
218 if (am_has_feature(g_options->features, fe_req_xml)) {
228 skip_whitespace(s, ch); /* find the program name */
230 err_extra = stralloc(_("no program name"));
231 goto err; /* no program name */
233 dle->program = s - 1;
234 skip_non_whitespace(s, ch);
237 dle->program_is_application_api=0;
238 if(strncmp_const(dle->program, "CALCSIZE") == 0) {
239 skip_whitespace(s, ch); /* find the program name */
241 err_extra = stralloc(_("no program name"));
244 dle->estimatelist = g_slist_append(dle->estimatelist,
245 GINT_TO_POINTER(ES_CALCSIZE));
246 dle->program = s - 1;
247 skip_non_whitespace(s, ch);
249 if (strcmp(dle->program,"APPLICATION") == 0) {
250 dle->program_is_application_api=1;
251 skip_whitespace(s, ch); /* find dumper name */
253 goto err; /* no program */
255 dle->program = s - 1;
256 skip_non_whitespace(s, ch);
261 dle->estimatelist = g_slist_append(dle->estimatelist,
262 GINT_TO_POINTER(ES_CLIENT));
263 if (strcmp(dle->program,"APPLICATION") == 0) {
264 dle->program_is_application_api=1;
265 skip_whitespace(s, ch); /* find dumper name */
267 goto err; /* no program */
269 dle->program = s - 1;
270 skip_non_whitespace(s, ch);
274 dle->program = stralloc(dle->program);
276 skip_whitespace(s, ch); /* find the disk name */
278 err_extra = stralloc(_("no disk name"));
279 goto err; /* no disk name */
286 skip_quoted_string(s, ch);
287 s[-1] = '\0'; /* terminate the disk name */
288 qdisk = stralloc(fp);
289 dle->disk = unquote_string(qdisk);
291 skip_whitespace(s, ch); /* find the device or level */
293 err_extra = stralloc(_("bad level"));
296 if(!isdigit((int)s[-1])) {
298 skip_quoted_string(s, ch);
300 qamdevice = stralloc(fp);
301 dle->device = unquote_string(qamdevice);
302 skip_whitespace(s, ch); /* find level number */
305 dle->device = stralloc(dle->disk);
306 qamdevice = stralloc(qdisk);
309 /* find the level number */
310 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
311 err_extra = stralloc(_("bad level"));
312 goto err; /* bad level */
314 if (level < 0 || level >= DUMP_LEVELS) {
315 err_extra = stralloc(_("bad level"));
319 alevel = g_new0(level_t, 1);
320 alevel->level = level;
321 dle->levellist = g_slist_append(dle->levellist, alevel);
323 skip_whitespace(s, ch); /* find the dump date */
325 err_extra = stralloc(_("no dumpdate"));
326 goto err; /* no dumpdate */
329 skip_non_whitespace(s, ch);
331 (void)dumpdate; /* XXX: Set but not used */
333 dle->spindle = 0; /* default spindle */
335 skip_whitespace(s, ch); /* find the spindle */
337 if(sscanf(s - 1, "%d", &dle->spindle) != 1) {
338 err_extra = stralloc(_("bad spindle"));
339 goto err; /* bad spindle */
343 skip_whitespace(s, ch); /* find the parameters */
345 if(strncmp_const(s-1, "OPTIONS |;") == 0) {
353 if(strncmp_const(s-1, "exclude-file=") == 0) {
354 qlist = unquote_string(s+12);
356 append_sl(dle->exclude_file, qlist);
358 } else if(strncmp_const(s-1, "exclude-list=") == 0) {
359 qlist = unquote_string(s+12);
361 append_sl(dle->exclude_list, qlist);
363 } else if(strncmp_const(s-1, "include-file=") == 0) {
364 qlist = unquote_string(s+12);
366 append_sl(dle->include_file, qlist);
368 } else if(strncmp_const(s-1, "include-list=") == 0) {
369 qlist = unquote_string(s+12);
371 append_sl(dle->include_list, qlist);
374 err_extra = vstrallocf(_("Invalid parameter (%s)"), s-1);
375 goto err; /* should have gotten to end */
377 skip_quoted_string(s, ch);
378 skip_whitespace(s, ch); /* find the inclusion list */
386 dle_add_diskest(dle);
389 if (g_options == NULL) {
390 g_printf(_("ERROR [Missing OPTIONS line in sendsize input]\n"));
391 error(_("Missing OPTIONS line in sendsize input\n"));
396 if (am_has_feature(g_options->features, fe_req_xml)) {
400 dles = amxml_parse_node_FILE(stdin, &errmsg);
405 for (dle = dles; dle != NULL; dle = dle->next) {
406 dle_add_diskest(dle);
410 if (amandates_started) {
413 amandates_started = FALSE;
416 for(est = est_list; est != NULL; est = est->next) {
417 run_client_scripts(EXECUTE_ON_PRE_HOST_ESTIMATE, g_options, est->dle,
427 * See if we need to wait for a child before we can do anything
432 amwait_t child_status;
435 dbprintf(_("waiting for any estimate child: %d running\n"),
437 child_pid = wait(&child_status);
438 if(child_pid == -1) {
439 error(_("wait failed: %s"), strerror(errno));
443 if (!WIFEXITED(child_status) || WEXITSTATUS(child_status) != 0) {
444 char *child_name = vstrallocf(_("child %ld"), (long)child_pid);
445 char *child_status_str = str_exit_status(child_name, child_status);
446 dbprintf("%s\n", child_status_str);
447 amfree(child_status_str);
452 * Find the child and mark it done.
454 for(est = est_list; est != NULL; est = est->next) {
455 if(est->child == child_pid) {
460 dbprintf(_("unexpected child %ld\n"), (long)child_pid);
465 run_client_scripts(EXECUTE_ON_POST_DLE_ESTIMATE, g_options,
470 * If we are already running the maximum number of children
471 * go back and wait until one of them finishes.
473 if(dumpsrunning >= g_options->maxdumps) {
476 continue; /* have to wait first */
479 * Find a new child to start.
481 for(est = est_list; est != NULL; est = est->next) {
483 done = 0; /* more to do */
485 if(est->child != 0 || est->done) {
486 continue; /* child is running or done */
489 * Make sure there is no spindle conflict.
491 if(est->dle->spindle != -1) {
492 for(est1 = est_list; est1 != NULL; est1 = est1->next) {
493 if(est1->child == 0 || est == est1 || est1->done) {
495 * Ignore anything not yet started, ourself,
496 * and anything completed.
500 if(est1->dle->spindle == est->dle->spindle) {
501 break; /* oops -- they match */
505 continue; /* spindle conflict */
508 break; /* start this estimate */
511 if(dumpsrunning > 0) {
512 need_wait = 1; /* nothing to do but wait */
516 run_client_scripts(EXECUTE_ON_PRE_DLE_ESTIMATE, g_options,
519 if((est->child = fork()) == 0) {
520 calc_estimates(est); /* child does the estimate */
522 } else if(est->child == -1) {
523 error(_("calc_estimates fork failed: %s"), strerror(errno));
526 dumpsrunning++; /* parent */
530 for(est = est_list; est != NULL; est = est->next) {
531 run_client_scripts(EXECUTE_ON_POST_HOST_ESTIMATE, g_options, est->dle,
536 for(est = est_list; est != NULL; est = est->next) {
542 amfree(our_feature_string);
543 am_release_feature_set(our_features);
545 free_g_options(g_options);
551 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET '%s'\n"), err_extra);
552 dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
555 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET\n"));
556 dbprintf(_("REQ packet is bogus\n"));
559 free_g_options(g_options);
570 disk_estimates_t *newp, *curp;
574 levellist_t levellist;
575 char *amandates_file;
576 gboolean need_amandates = FALSE;
579 if (dle->levellist == NULL) {
580 g_printf(_("ERROR Missing level in request\n"));
584 /* should we use amandates for this? */
585 for (el = dle->estimatelist; el != NULL; el=el->next) {
586 estimate_t estimate = (estimate_t)GPOINTER_TO_INT(el->data);
587 if (estimate == ES_CALCSIZE)
588 need_amandates = TRUE;
591 if (strcmp(dle->program, "GNUTAR") == 0) {
592 /* GNUTAR only needs amandates if gnutar_list_dir is NULL */
593 char *gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
594 if (!gnutar_list_dir || !*gnutar_list_dir)
595 need_amandates = TRUE;
598 /* start amandates here, before adding this DLE to est_list, in case
599 * we encounter an error. */
600 if (need_amandates) {
601 if (!amandates_started) {
602 amandates_file = getconf_str(CNF_AMANDATES);
603 if(!start_amandates(amandates_file, 0)) {
604 char *errstr = strerror(errno);
605 char *qamname = quote_string(dle->disk);
606 char *errmsg = vstrallocf(_("could not open %s: %s"), amandates_file, errstr);
607 char *qerrmsg = quote_string(errmsg);
608 g_printf(_("%s %d ERROR %s\n"), qamname, 0, qerrmsg);
614 amandates_started = TRUE;
618 levellist = dle->levellist;
619 while (levellist != NULL) {
620 level_t *alevel = (level_t *)levellist->data;
621 if (alevel->level < 0)
623 if (alevel->level >= DUMP_LEVELS)
624 alevel->level = DUMP_LEVELS - 1;
625 levellist = g_slist_next(levellist);
628 for(curp = est_list; curp != NULL; curp = curp->next) {
629 if(strcmp(curp->dle->disk, dle->disk) == 0) {
630 /* already have disk info, just note the level request */
631 levellist = dle->levellist;
632 while (levellist != NULL) {
633 level_t *alevel = (level_t *)levellist->data;
634 int level = alevel->level;
635 curp->est[level].needestimate = 1;
636 curp->est[level].server = alevel->server;
637 levellist = g_slist_next(levellist);
644 newp = (disk_estimates_t *) alloc(SIZEOF(disk_estimates_t));
645 memset(newp, 0, SIZEOF(*newp));
646 newp->next = est_list;
648 newp->qamname = quote_string(dle->disk);
650 newp->qamdevice = quote_string(dle->device);
651 newp->dirname = amname_to_dirname(dle->device);
652 newp->qdirname = quote_string(newp->dirname);
654 newp->qamdevice = stralloc("");
655 newp->dirname = stralloc("");
656 newp->qdirname = stralloc("");
658 levellist = dle->levellist;
659 while (levellist != NULL) {
660 level_t *alevel = (level_t *)levellist->data;
661 newp->est[alevel->level].needestimate = 1;
662 newp->est[alevel->level].server = alevel->server;
663 levellist = g_slist_next(levellist);
667 /* fill in dump-since dates */
668 if (need_amandates) {
669 amdp = amandates_lookup(newp->dle->disk);
671 newp->est[0].dumpsince = EPOCH;
672 for(dumplev = 0; dumplev < DUMP_LEVELS; dumplev++) {
673 dumpdate = amdp->dates[dumplev];
674 for(estlev = dumplev+1; estlev < DUMP_LEVELS; estlev++) {
675 if(dumpdate > newp->est[estlev].dumpsince)
676 newp->est[estlev].dumpsince = dumpdate;
680 /* just zero everything out */
681 for(dumplev = 0; dumplev < DUMP_LEVELS; dumplev++) {
682 newp->est[dumplev].dumpsince = 0;
690 disk_estimates_t * est)
692 amfree(est->qamname);
693 amfree(est->qamdevice);
694 amfree(est->dirname);
695 amfree(est->qdirname);
702 * ------------------------------------------------------------------------
708 disk_estimates_t * est)
710 dbprintf(_("calculating for amname %s, dirname %s, spindle %d %s\n"),
711 est->qamname, est->qdirname, est->dle->spindle, est->dle->program);
713 if(est->dle->program_is_application_api == 1)
714 application_api_calc_estimate(est);
719 estimate_t estimate_method = ES_ES;
720 estimate_t client_method = ES_ES;
722 /* find estimate method to use */
723 for (el = est->dle->estimatelist; el != NULL; el = el->next) {
724 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
725 if (estimate == ES_SERVER) {
726 if (estimate_method == ES_ES)
727 estimate_method = ES_SERVER;
729 if (estimate == ES_CLIENT ||
730 (estimate == ES_CALCSIZE &&
731 (est->dle->device[0] != '/' || est->dle->device[1] != '/'))) {
732 if (client_method == ES_ES)
733 client_method = estimate;
734 if (estimate_method == ES_ES)
735 estimate_method = estimate;
739 /* do server estimate */
740 if (estimate_method == ES_SERVER) {
741 for (level = 0; level < DUMP_LEVELS; level++) {
742 if (est->est[level].needestimate) {
743 if (est->est[level].server || client_method == ES_ES) {
744 g_printf(_("%s %d SIZE -1\n"), est->qamname, level);
745 est->est[level].needestimate = 0;
751 if (client_method == ES_ES && estimate_method != ES_SERVER) {
752 g_printf(_("%s %d SIZE -2\n"), est->qamname, 0);
753 dbprintf(_("Can't use CALCSIZE for samba estimate: %s %s\n"),
754 est->qamname, est->qdirname);
755 } else if (client_method == ES_CALCSIZE) {
756 generic_calc_estimates(est);
757 } else if (client_method == ES_CLIENT) {
758 #ifndef USE_GENERIC_CALCSIZE
759 if (strcmp(est->dle->program, "DUMP") == 0)
760 dump_calc_estimates(est);
764 if (strcmp(est->dle->program, "GNUTAR") == 0 &&
765 est->dle->device[0] == '/' && est->dle->device[1] == '/')
766 smbtar_calc_estimates(est);
770 if (strcmp(est->dle->program, "GNUTAR") == 0)
771 gnutar_calc_estimates(est);
774 dbprintf(_("Invalid program: %s %s %s\n"),
775 est->qamname, est->qdirname, est->dle->program);
779 dbprintf(_("done with amname %s dirname %s spindle %d\n"),
780 est->qamname, est->qdirname, est->dle->spindle);
784 * ------------------------------------------------------------------------
788 /* local functions */
789 off_t getsize_dump(dle_t *dle, int level, char **errmsg);
790 off_t getsize_smbtar(dle_t *dle, int level, char **errmsg);
791 off_t getsize_gnutar(dle_t *dle, int level, time_t dumpsince, char **errmsg);
792 off_t getsize_application_api(disk_estimates_t *est, int nb_level,
793 int *levels, backup_support_option_t *bsu);
794 off_t handle_dumpline(char *str);
795 double first_num(char *str);
798 application_api_calc_estimate(
799 disk_estimates_t * est)
803 int levels[DUMP_LEVELS];
805 backup_support_option_t *bsu;
809 estimate_t estimate_method = ES_ES;
810 estimate_t client_method = ES_ES;
811 int has_calcsize = 0;
814 bsu = backup_support_option(est->dle->program, g_options, est->dle->disk,
815 est->dle->device, &errarray);
818 for (i=0; i < errarray->len; i++) {
822 line = g_ptr_array_index(errarray, i);
823 if(am_has_feature(g_options->features,
824 fe_rep_sendsize_quoted_error)) {
825 errmsg = g_strdup_printf(_("Application '%s': %s"),
826 est->dle->program, line);
827 qerrmsg = quote_string(errmsg);
828 for (level = 0; level < DUMP_LEVELS; level++) {
829 if (est->est[level].needestimate) {
830 g_printf(_("%s %d ERROR %s\n"),
831 est->dle->disk, level, qerrmsg);
832 dbprintf(_("%s %d ERROR %s\n"),
833 est->qamname, level, qerrmsg);
840 if (i == 0) { /* nothing in errarray */
843 errmsg = g_strdup_printf(
844 _("Application '%s': cannon execute support command"),
846 qerrmsg = quote_string(errmsg);
847 for (level = 0; level < DUMP_LEVELS; level++) {
848 if (est->est[level].needestimate) {
849 g_printf(_("%s %d ERROR %s\n"),
850 est->dle->disk, level, qerrmsg);
851 dbprintf(_("%s %d ERROR %s\n"),
852 est->qamname, level, qerrmsg);
858 for (level = 0; level < DUMP_LEVELS; level++) {
859 est->est[level].needestimate = 0;
861 g_ptr_array_free(errarray, TRUE);
864 if (est->dle->data_path == DATA_PATH_AMANDA &&
865 (bsu->data_path_set & DATA_PATH_AMANDA)==0) {
866 g_printf("%s %d ERROR application %s doesn't support amanda data-path\n", est->qamname, 0, est->dle->program);
869 if (est->dle->data_path == DATA_PATH_DIRECTTCP &&
870 (bsu->data_path_set & DATA_PATH_DIRECTTCP)==0) {
871 g_printf("%s %d ERROR application %s doesn't support directtcp data-path\n", est->qamname, 0, est->dle->program);
875 /* find estimate method to use */
876 for (el = est->dle->estimatelist; el != NULL; el = el->next) {
877 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
878 if (estimate == ES_CLIENT)
880 if (estimate == ES_CALCSIZE)
882 if (estimate == ES_SERVER) {
883 if (estimate_method == ES_ES)
884 estimate_method = ES_SERVER;
886 if ((estimate == ES_CLIENT && bsu->client_estimate) ||
887 (estimate == ES_CALCSIZE && bsu->calcsize)) {
888 if (client_method == ES_ES)
889 client_method = estimate;
890 if (estimate_method == ES_ES)
891 estimate_method = estimate;
895 for(level = 0; level < DUMP_LEVELS; level++) {
896 if (est->est[level].needestimate) {
897 if (level > bsu->max_level) {
898 /* planner will not even consider this level */
899 g_printf("%s %d SIZE %lld\n", est->qamname, level,
901 est->est[level].needestimate = 0;
902 dbprintf(_("Application '%s' can't estimate level %d\n"),
903 est->dle->program, level);
904 } else if (estimate_method == ES_ES) {
905 g_printf("%s %d SIZE %lld\n", est->qamname, level,
907 est->est[level].needestimate = 0;
908 if (am_has_feature(g_options->features,
909 fe_rep_sendsize_quoted_error)) {
910 char *errmsg, *qerrmsg;
911 if (has_client && !bsu->client_estimate &&
912 has_calcsize && !bsu->calcsize) {
913 errmsg = vstrallocf(_("Application '%s' can't do CLIENT or CALCSIZE estimate"),
915 } else if (has_client && !bsu->client_estimate) {
916 errmsg = vstrallocf(_("Application '%s' can't do CLIENT estimate"),
918 } else if (has_calcsize && !bsu->calcsize) {
919 errmsg = vstrallocf(_("Application '%s' can't do CALCSIZE estimate"),
922 errmsg = vstrallocf(_("Application '%s' can't do estimate"),
925 qerrmsg = quote_string(errmsg);
926 dbprintf(_("errmsg is %s\n"), errmsg);
927 g_printf("%s %d ERROR %s\n",
928 est->qamname, 0, qerrmsg);
932 } else if (estimate_method == ES_SERVER &&
933 (est->est[level].server || client_method == ES_ES)) {
934 /* planner will consider this level, */
935 /* but use a server-side estimate */
936 g_printf("%s %d SIZE -1\n", est->qamname, level);
937 est->est[level].needestimate = 0;
938 } else if (client_method == ES_CLIENT) {
939 levels[nb_level++] = level;
940 } else if (client_method == ES_CALCSIZE) {
941 levels[nb_level++] = level;
949 if (bsu->multi_estimate) {
950 for (i=0;i<nb_level;i++) {
951 dbprintf(_("getting size via application API for %s %s level %d\n"),
952 est->qamname, est->qamdevice, levels[i]);
954 getsize_application_api(est, nb_level, levels, bsu);
957 for(level = 0; level < DUMP_LEVELS; level++) {
958 if (est->est[level].needestimate) {
960 _("getting size via application API for %s %s level %d\n"),
961 est->qamname, est->qamdevice, level);
963 getsize_application_api(est, 1, levels, bsu);
971 generic_calc_estimates(
972 disk_estimates_t * est)
974 int pipefd = -1, nullfd = -1;
978 GPtrArray *argv_ptr = g_ptr_array_new();
979 char number[NUM_STR_SIZE];
985 char *file_exclude = NULL;
986 char *file_include = NULL;
988 FILE *dumpout = NULL;
989 off_t size = (off_t)1;
992 amwait_t wait_status;
993 char *errmsg = NULL, *qerrmsg;
994 char tmppath[PATH_MAX];
997 cmd = vstralloc(amlibexecdir, "/", "calcsize", NULL);
999 g_ptr_array_add(argv_ptr, stralloc("calcsize"));
1000 if (g_options->config)
1001 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
1003 g_ptr_array_add(argv_ptr, stralloc("NOCONFIG"));
1005 g_ptr_array_add(argv_ptr, stralloc(est->dle->program));
1006 canonicalize_pathname(est->dle->disk, tmppath);
1007 g_ptr_array_add(argv_ptr, stralloc(tmppath));
1008 canonicalize_pathname(est->dirname, tmppath);
1009 g_ptr_array_add(argv_ptr, stralloc(tmppath));
1011 if (est->dle->exclude_file)
1012 nb_exclude += est->dle->exclude_file->nb_element;
1013 if (est->dle->exclude_list)
1014 nb_exclude += est->dle->exclude_list->nb_element;
1015 if (est->dle->include_file)
1016 nb_include += est->dle->include_file->nb_element;
1017 if (est->dle->include_list)
1018 nb_include += est->dle->include_list->nb_element;
1021 file_exclude = build_exclude(est->dle, 0);
1023 file_include = build_include(est->dle, 0);
1026 g_ptr_array_add(argv_ptr, stralloc("-X"));
1027 g_ptr_array_add(argv_ptr, stralloc(file_exclude));
1031 g_ptr_array_add(argv_ptr, stralloc("-I"));
1032 g_ptr_array_add(argv_ptr, stralloc(file_include));
1034 start_time = curclock();
1036 command = (char *)g_ptr_array_index(argv_ptr, 0);
1037 cmdline = stralloc(command);
1038 for(i = 1; i < argv_ptr->len-1; i++) {
1039 cmdline = vstrextend(&cmdline, " ",
1040 (char *)g_ptr_array_index(argv_ptr, i), NULL);
1042 dbprintf(_("running: \"%s\"\n"), cmdline);
1045 for(level = 0; level < DUMP_LEVELS; level++) {
1046 if(est->est[level].needestimate) {
1047 g_snprintf(number, SIZEOF(number), "%d", level);
1048 g_ptr_array_add(argv_ptr, stralloc(number));
1049 dbprintf(" %s", number);
1050 g_snprintf(number, SIZEOF(number),
1051 "%ld", (long)est->est[level].dumpsince);
1052 g_ptr_array_add(argv_ptr, stralloc(number));
1053 dbprintf(" %s", number);
1056 g_ptr_array_add(argv_ptr, NULL);
1059 fflush(stderr); fflush(stdout);
1061 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
1062 errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
1064 dbprintf("%s\n", errmsg);
1068 calcpid = pipespawnv(cmd, STDERR_PIPE, 0,
1069 &nullfd, &nullfd, &pipefd, (char **)argv_ptr->pdata);
1072 dumpout = fdopen(pipefd,"r");
1074 error(_("Can't fdopen: %s"), strerror(errno));
1077 match_expr = vstralloc(" %d SIZE %lld", NULL);
1078 len = strlen(est->qamname);
1079 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1080 long long size_ = (long long)0;
1081 if (line[0] == '\0' || (int)strlen(line) <= len)
1083 /* Don't use sscanf for est->qamname because it can have a '%'. */
1084 if (strncmp(line, est->qamname, len) == 0 &&
1085 sscanf(line+len, match_expr, &level, &size_) == 2) {
1086 g_printf("%s\n", line); /* write to amandad */
1087 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1092 size = (off_t)size_;
1096 dbprintf(_("waiting for %s %s child (pid=%d)\n"),
1097 command, est->qamdevice, (int)calcpid);
1098 waitpid(calcpid, &wait_status, 0);
1099 if (WIFSIGNALED(wait_status)) {
1100 errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
1101 "calcsize", WTERMSIG(wait_status),
1103 } else if (WIFEXITED(wait_status)) {
1104 if (WEXITSTATUS(wait_status) != 0) {
1105 errmsg = vstrallocf(_("%s exited with status %d: see %s"),
1106 "calcsize", WEXITSTATUS(wait_status),
1112 errmsg = vstrallocf(_("%s got bad exit: see %s"),
1113 "calcsize", dbfn());
1115 dbprintf(_("after %s %s wait: child pid=%d status=%d\n"),
1116 command, est->qamdevice,
1117 (int)calcpid, WEXITSTATUS(wait_status));
1119 dbprintf(_(".....\n"));
1120 dbprintf(_("estimate time for %s: %s\n"),
1122 walltime_str(timessub(curclock(), start_time)));
1125 if (errmsg && errmsg[0] != '\0') {
1126 if(am_has_feature(g_options->features, fe_rep_sendsize_quoted_error)) {
1127 qerrmsg = quote_string(errmsg);
1128 dbprintf(_("errmsg is %s\n"), errmsg);
1129 g_printf("%s %d ERROR %s\n",
1130 est->qamname, 0, qerrmsg);
1135 g_ptr_array_free_full(argv_ptr);
1141 dump_calc_estimates(
1142 disk_estimates_t * est)
1146 char *errmsg=NULL, *qerrmsg;
1148 for(level = 0; level < DUMP_LEVELS; level++) {
1149 if(est->est[level].needestimate) {
1150 dbprintf(_("getting size via dump for %s level %d\n"),
1151 est->qamname, level);
1152 size = getsize_dump(est->dle, level, &errmsg);
1156 g_printf(_("%s %d SIZE %lld\n"),
1157 est->qamname, level, (long long)size);
1158 if (errmsg && errmsg[0] != '\0') {
1159 if(am_has_feature(g_options->features,
1160 fe_rep_sendsize_quoted_error)) {
1161 qerrmsg = quote_string(errmsg);
1162 dbprintf(_("errmsg is %s\n"), errmsg);
1163 g_printf("%s %d ERROR %s\n",
1164 est->qamname, level, qerrmsg);
1171 amfunlock(1, "size");
1178 smbtar_calc_estimates(
1179 disk_estimates_t * est)
1183 char *errmsg = NULL, *qerrmsg;
1185 for(level = 0; level < DUMP_LEVELS; level++) {
1186 if(est->est[level].needestimate) {
1187 dbprintf(_("getting size via smbclient for %s level %d\n"),
1188 est->qamname, level);
1189 size = getsize_smbtar(est->dle, level, &errmsg);
1193 g_printf(_("%s %d SIZE %lld\n"),
1194 est->qamname, level, (long long)size);
1195 if (errmsg && errmsg[0] != '\0') {
1196 if(am_has_feature(g_options->features,
1197 fe_rep_sendsize_quoted_error)) {
1198 qerrmsg = quote_string(errmsg);
1199 dbprintf(_("errmsg is %s\n"), errmsg);
1200 g_printf("%s %d ERROR %s\n",
1201 est->qamname, level, qerrmsg);
1208 amfunlock(1, "size");
1216 gnutar_calc_estimates(
1217 disk_estimates_t * est)
1221 char *errmsg = NULL, *qerrmsg;
1223 for(level = 0; level < DUMP_LEVELS; level++) {
1224 if (est->est[level].needestimate) {
1225 dbprintf(_("getting size via gnutar for %s level %d\n"),
1226 est->qamname, level);
1227 size = getsize_gnutar(est->dle, level,
1228 est->est[level].dumpsince,
1233 g_printf(_("%s %d SIZE %lld\n"),
1234 est->qamname, level, (long long)size);
1235 if (errmsg && errmsg[0] != '\0') {
1236 if(am_has_feature(g_options->features,
1237 fe_rep_sendsize_quoted_error)) {
1238 qerrmsg = quote_string(errmsg);
1239 dbprintf(_("errmsg is %s\n"), errmsg);
1240 g_printf(_("%s %d ERROR %s\n"),
1241 est->qamname, level, qerrmsg);
1248 amfunlock(1, "size");
1254 typedef struct regex_scale_s {
1260 regex_scale_t re_size[] = {
1262 {" DUMP: estimated -*[0-9][0-9]* tape blocks", 1024},
1263 {" DUMP: [Ee]stimated [0-9][0-9]* blocks", 512},
1264 {" DUMP: [Ee]stimated [0-9][0-9]* bytes", 1}, /* Ultrix 4.4 */
1265 {" UFSDUMP: estimated [0-9][0-9]* blocks", 512}, /* NEC EWS-UX */
1266 {"dump: Estimate: [0-9][0-9]* tape blocks", 1024}, /* OSF/1 */
1267 {"backup: There are an estimated [0-9][0-9]* tape blocks.",1024}, /* AIX */
1268 {"backup: estimated [0-9][0-9]* 1k blocks", 1024}, /* AIX */
1269 {"backup: estimated [0-9][0-9]* tape blocks", 1024}, /* AIX */
1270 {"backup: [0-9][0-9]* tape blocks on [0-9][0-9]* tape(s)",1024}, /* AIX */
1271 {"backup: [0-9][0-9]* 1k blocks on [0-9][0-9]* volume(s)",1024}, /* AIX */
1272 {"dump: Estimate: [0-9][0-9]* blocks being output to pipe",1024},
1274 {"dump: Dumping [0-9][0-9]* bytes, ", 1}, /* DU 4.0 vdump */
1275 {"DUMP: estimated [0-9][0-9]* KB output", 1024}, /* HPUX */
1276 {"DUMP: estimated [0-9][0-9]* KB\\.", 1024}, /* NetApp */
1277 {" UFSDUMP: estimated [0-9][0-9]* blocks", 512}, /* Sinix */
1279 #ifdef HAVE_DUMP_ESTIMATE
1280 {"[0-9][0-9]* blocks, [0-9][0-9]*.[0-9][0-9]* volumes", 1024},
1281 /* DU 3.2g dump -E */
1282 {"^[0-9][0-9]* blocks$", 1024}, /* DU 4.0 dump -E */
1283 {"^[0-9][0-9]*$", 1}, /* Solaris ufsdump -S */
1288 {"vdump: Dumping [0-9][0-9]* bytes, ", 1}, /* OSF/1 vdump */
1292 {"vxdump: estimated [0-9][0-9]* blocks", 512}, /* HPUX's vxdump */
1293 {" VXDUMP: estimated [0-9][0-9]* blocks", 512}, /* Sinix */
1297 {"xfsdump: estimated dump size: [0-9][0-9]* bytes", 1}, /* Irix 6.2 xfs */
1301 {"Total bytes written: [0-9][0-9]*", 1}, /* Gnutar client */
1305 #if SAMBA_VERSION >= 2
1306 #define SAMBA_DEBUG_LEVEL "0"
1307 {"Total number of bytes: [0-9][0-9]*", 1}, /* Samba du */
1309 #define SAMBA_DEBUG_LEVEL "3"
1310 {"Total bytes listed: [0-9][0-9]*", 1}, /* Samba dir */
1324 int pipefd[2], nullfd, stdoutfd, killctl[2];
1328 char *dumpkeys = NULL;
1329 char *device = NULL;
1330 char *fstype = NULL;
1334 char *rundump_cmd = NULL;
1335 char level_str[NUM_STR_SIZE];
1338 char *qdisk = quote_string(dle->disk);
1341 amwait_t wait_status;
1342 #if defined(DUMP) || defined(VDUMP) || defined(VXDUMP) || defined(XFSDUMP)
1347 return -2; /* planner will not even consider this level */
1349 g_snprintf(level_str, SIZEOF(level_str), "%d", level);
1351 device = amname_to_devname(dle->device);
1352 qdevice = quote_string(device);
1353 fstype = amname_to_fstype(dle->device);
1355 dbprintf(_("calculating for device %s with %s\n"),
1358 cmd = vstralloc(amlibexecdir, "/rundump", NULL);
1359 rundump_cmd = stralloc(cmd);
1360 if (g_options->config)
1361 config = g_options->config;
1363 config = "NOCONFIG";
1364 if ((stdoutfd = nullfd = open("/dev/null", O_RDWR)) == -1) {
1365 *errmsg = vstrallocf(_("getsize_dump could not open /dev/null: %s"),
1367 dbprintf("%s\n", *errmsg);
1369 amfree(rundump_cmd);
1376 pipefd[0] = pipefd[1] = killctl[0] = killctl[1] = -1;
1377 if (pipe(pipefd) < 0) {
1378 *errmsg = vstrallocf(_("getsize_dump could create data pipes: %s"),
1380 dbprintf("%s\n", *errmsg);
1382 amfree(rundump_cmd);
1390 #ifdef XFSDUMP /* { */
1392 if (strcmp(fstype, "xfs") == 0)
1397 name = stralloc(" (xfsdump)");
1398 dbprintf(_("running \"%s%s -F -J -l %s - %s\"\n"),
1399 cmd, name, level_str, qdevice);
1403 #ifdef VXDUMP /* { */
1405 if (strcmp(fstype, "vxfs") == 0)
1411 name = stralloc(" (vxdump)");
1413 name = stralloc("");
1414 cmd = newstralloc(cmd, VXDUMP);
1415 config = skip_argument;
1418 dumpkeys = vstralloc(level_str, "s", "f", NULL);
1419 dbprintf(_("running \"%s%s %s 1048576 - %s\"\n"),
1420 cmd, name, dumpkeys, qdevice);
1424 #ifdef VDUMP /* { */
1426 if (strcmp(fstype, "advfs") == 0)
1431 name = stralloc(" (vdump)");
1432 dumpkeys = vstralloc(level_str, "b", "f", NULL);
1433 dbprintf(_("running \"%s%s %s 60 - %s\"\n"),
1434 cmd, name, dumpkeys, qdevice);
1440 # ifdef USE_RUNDUMP /* { */
1441 # ifdef AIX_BACKUP /* { */
1442 name = stralloc(" (backup)");
1444 name = vstralloc(" (", DUMP, ")", NULL);
1447 name = stralloc("");
1448 cmd = newstralloc(cmd, DUMP);
1449 config = skip_argument;
1453 # ifdef AIX_BACKUP /* { */
1454 dumpkeys = vstralloc("-", level_str, "f", NULL);
1455 dbprintf(_("running \"%s%s %s - %s\"\n"),
1456 cmd, name, dumpkeys, qdevice);
1458 # ifdef HAVE_DUMP_ESTIMATE
1459 # define PARAM_DUMP_ESTIMATE HAVE_DUMP_ESTIMATE
1461 # define PARAM_DUMP_ESTIMATE ""
1463 # ifdef HAVE_HONOR_NODUMP
1464 # define PARAM_HONOR_NODUMP "h"
1466 # define PARAM_HONOR_NODUMP ""
1468 dumpkeys = vstralloc(level_str,
1469 PARAM_DUMP_ESTIMATE,
1473 # ifdef HAVE_DUMP_ESTIMATE
1474 stdoutfd = pipefd[1];
1477 # ifdef HAVE_HONOR_NODUMP /* { */
1478 dbprintf(_("running \"%s%s %s 0 1048576 - %s\"\n"),
1479 cmd, name, dumpkeys, qdevice);
1481 dbprintf(_("running \"%s%s %s 1048576 - %s\"\n"),
1482 cmd, name, dumpkeys, qdevice);
1489 error(_("no dump program available"));
1493 if (pipe(killctl) < 0) {
1494 dbprintf(_("Could not create pipe: %s\n"), strerror(errno));
1495 /* Message will be printed later... */
1496 killctl[0] = killctl[1] = -1;
1499 start_time = curclock();
1500 switch(dumppid = fork()) {
1502 *errmsg = vstrallocf(_("cannot fork for killpgrp: %s"),
1504 dbprintf("%s\n", *errmsg);
1507 amfree(rundump_cmd);
1516 case 0: /* child process */
1519 else if (killctl[0] == -1 || killctl[1] == -1)
1520 dbprintf(_("Trying without killpgrp\n"));
1524 dbprintf(_("fork failed, trying without killpgrp\n"));
1530 char *killpgrp_cmd = vstralloc(amlibexecdir, "/killpgrp", NULL);
1531 dbprintf(_("running %s\n"), killpgrp_cmd);
1532 dup2(killctl[0], 0);
1539 if (g_options->config)
1540 config = g_options->config;
1542 config = "NOCONFIG";
1544 execle(killpgrp_cmd, killpgrp_cmd, config, (char *)0,
1546 dbprintf(_("cannot execute %s: %s\n"),
1547 killpgrp_cmd, strerror(errno));
1551 case 0: /* child process */
1560 if (killctl[0] != -1)
1562 if (killctl[1] != -1)
1568 if (strcmp(fstype, "xfs") == 0)
1573 execle(cmd, "rundump", config, "xfsdump", "-F", "-J", "-l",
1574 level_str, "-", device, (char *)0, safe_env());
1576 execle(cmd, "xfsdump", "-F", "-J", "-l",
1577 level_str, "-", device, (char *)0, safe_env());
1582 if (strcmp(fstype, "vxfs") == 0)
1587 execle(cmd, "rundump", config, "vxdump", dumpkeys, "1048576",
1588 "-", device, (char *)0, safe_env());
1590 execle(cmd, "vxdump", dumpkeys, "1048576", "-",
1591 device, (char *)0, safe_env());
1596 if (strcmp(fstype, "advfs") == 0)
1601 execle(cmd, "rundump", config, "vdump", dumpkeys, "60", "-",
1602 device, (char *)0, safe_env());
1604 execle(cmd, "vdump", dumpkeys, "60", "-",
1605 device, (char *)0, safe_env());
1611 execle(cmd, "rundump", config, "backup", dumpkeys, "-",
1612 device, (char *)0, safe_env());
1614 execle(cmd, "backup", dumpkeys, "-",
1615 device, (char *)0, safe_env());
1618 execle(cmd, "rundump", config, "dump", dumpkeys,
1619 #ifdef HAVE_HONOR_NODUMP
1622 "1048576", "-", device, (char *)0, safe_env());
1624 execle(cmd, "dump", dumpkeys,
1625 #ifdef HAVE_HONOR_NODUMP
1628 "1048576", "-", device, (char *)0, safe_env());
1633 error(_("exec %s failed or no dump program available: %s"),
1634 cmd, strerror(errno));
1640 amfree(rundump_cmd);
1643 if (killctl[0] != -1)
1645 dumpout = fdopen(pipefd[0],"r");
1647 error(_("Can't fdopen: %s"), strerror(errno));
1651 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1652 if (line[0] == '\0')
1654 dbprintf("%s\n", line);
1655 size = handle_dumpline(line);
1656 if(size > (off_t)-1) {
1658 while ((line = agets(dumpout)) != NULL) {
1659 if (line[0] != '\0')
1664 dbprintf("%s\n", line);
1671 dbprintf(".....\n");
1672 dbprintf(_("estimate time for %s level %d: %s\n"),
1675 walltime_str(timessub(curclock(), start_time)));
1676 if(size == (off_t)-1) {
1677 *errmsg = vstrallocf(_("no size line match in %s%s output"),
1679 dbprintf(_("%s for %s\n"),
1682 dbprintf(".....\n");
1683 dbprintf(_("Run %s%s manually to check for errors\n"),
1685 } else if(size == (off_t)0 && level == 0) {
1686 dbprintf(_("possible %s%s problem -- is \"%s\" really empty?\n"),
1687 cmd, name, dle->disk);
1688 dbprintf(".....\n");
1690 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1696 if (killctl[1] != -1) {
1697 dbprintf(_("asking killpgrp to terminate\n"));
1699 for(s = 5; s > 0; --s) {
1701 if (waitpid(dumppid, NULL, WNOHANG) != -1)
1707 * First, try to kill the dump process nicely. If it ignores us
1708 * for several seconds, hit it harder.
1710 dbprintf(_("sending SIGTERM to process group %ld\n"), (long)dumppid);
1711 if (kill(-dumppid, SIGTERM) == -1) {
1712 dbprintf(_("kill failed: %s\n"), strerror(errno));
1714 /* Now check whether it dies */
1715 for(s = 5; s > 0; --s) {
1717 if (waitpid(dumppid, NULL, WNOHANG) != -1)
1721 dbprintf(_("sending SIGKILL to process group %ld\n"), (long)dumppid);
1722 if (kill(-dumppid, SIGKILL) == -1) {
1723 dbprintf(_("kill failed: %s\n"), strerror(errno));
1725 for(s = 5; s > 0; --s) {
1727 if (waitpid(dumppid, NULL, WNOHANG) != -1)
1731 dbprintf(_("waiting for %s%s \"%s\" child\n"), cmd, name, qdisk);
1732 waitpid(dumppid, &wait_status, 0);
1733 if (WIFSIGNALED(wait_status)) {
1734 *errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
1735 cmd, WTERMSIG(wait_status), dbfn());
1736 } else if (WIFEXITED(wait_status)) {
1737 if (WEXITSTATUS(wait_status) != 0) {
1738 *errmsg = vstrallocf(_("%s exited with status %d: see %s"),
1739 cmd, WEXITSTATUS(wait_status), dbfn());
1744 *errmsg = vstrallocf(_("%s got bad exit: see %s"),
1747 dbprintf(_("after %s%s %s wait\n"), cmd, name, qdisk);
1772 int pipefd = -1, nullfd = -1, passwdfd = -1;
1776 char *tarkeys, *sharename, *user_and_password = NULL, *domain = NULL;
1777 char *share = NULL, *subdir = NULL;
1784 char *error_pn = NULL;
1785 char *qdisk = quote_string(dle->disk);
1786 amwait_t wait_status;
1788 error_pn = stralloc2(get_pname(), "-smbclient");
1791 return -2; /* planner will not even consider this level */
1793 parsesharename(dle->device, &share, &subdir);
1797 set_pname(error_pn);
1799 error(_("cannot parse disk entry %s for share/subdir"), qdisk);
1802 if ((subdir) && (SAMBA_VERSION < 2)) {
1805 set_pname(error_pn);
1807 error(_("subdirectory specified for share %s but samba not v2 or better"), qdisk);
1810 if ((user_and_password = findpass(share, &domain)) == NULL) {
1813 memset(domain, '\0', strlen(domain));
1816 set_pname(error_pn);
1818 error(_("cannot find password for %s"), dle->disk);
1821 lpass = strlen(user_and_password);
1822 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
1823 memset(user_and_password, '\0', (size_t)lpass);
1824 amfree(user_and_password);
1826 memset(domain, '\0', strlen(domain));
1829 set_pname(error_pn);
1831 error(_("password field not \'user%%pass\' for %s"), dle->disk);
1835 pwtext_len = strlen(pwtext);
1836 if ((sharename = makesharename(share, 0)) == NULL) {
1837 memset(user_and_password, '\0', (size_t)lpass);
1838 amfree(user_and_password);
1840 memset(domain, '\0', strlen(domain));
1843 set_pname(error_pn);
1845 error(_("cannot make share name of %s"), share);
1848 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
1849 memset(user_and_password, '\0', (size_t)lpass);
1850 amfree(user_and_password);
1852 memset(domain, '\0', strlen(domain));
1855 set_pname(error_pn);
1858 error(_("could not open /dev/null: %s\n"),
1863 #if SAMBA_VERSION >= 2
1865 tarkeys = "archive 0;recurse;du";
1867 tarkeys = "archive 1;recurse;du";
1870 tarkeys = "archive 0;recurse;dir";
1872 tarkeys = "archive 1;recurse;dir";
1875 start_time = curclock();
1877 if (pwtext_len > 0) {
1878 pw_fd_env = "PASSWD_FD";
1880 pw_fd_env = "dummy_PASSWD_FD";
1882 dumppid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE, 0,
1883 &nullfd, &nullfd, &pipefd,
1884 pw_fd_env, &passwdfd,
1887 "-d", SAMBA_DEBUG_LEVEL,
1888 *user_and_password ? "-U" : skip_argument,
1889 *user_and_password ? user_and_password : skip_argument,
1891 domain ? "-W" : skip_argument,
1892 domain ? domain : skip_argument,
1893 #if SAMBA_VERSION >= 2
1894 subdir ? "-D" : skip_argument,
1895 subdir ? subdir : skip_argument,
1900 memset(domain, '\0', strlen(domain));
1904 if(pwtext_len > 0 && full_write(passwdfd, pwtext, pwtext_len) < pwtext_len) {
1905 int save_errno = errno;
1907 memset(user_and_password, '\0', (size_t)lpass);
1908 amfree(user_and_password);
1910 set_pname(error_pn);
1912 error(_("password write failed: %s"), strerror(save_errno));
1915 memset(user_and_password, '\0', (size_t)lpass);
1916 amfree(user_and_password);
1922 dumpout = fdopen(pipefd,"r");
1924 error(_("Can't fdopen: %s"), strerror(errno));
1928 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1929 if (line[0] == '\0')
1931 dbprintf("%s\n", line);
1932 size = handle_dumpline(line);
1935 while ((line = agets(dumpout)) != NULL) {
1936 if (line[0] != '\0')
1941 dbprintf("%s\n", line);
1948 dbprintf(".....\n");
1949 dbprintf(_("estimate time for %s level %d: %s\n"),
1952 walltime_str(timessub(curclock(), start_time)));
1953 if(size == (off_t)-1) {
1954 *errmsg = vstrallocf(_("no size line match in %s output"),
1956 dbprintf(_("%s for %s\n"),
1958 dbprintf(".....\n");
1959 } else if(size == (off_t)0 && level == 0) {
1960 dbprintf(_("possible %s problem -- is \"%s\" really empty?\n"),
1961 SAMBA_CLIENT, dle->disk);
1962 dbprintf(".....\n");
1964 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1969 kill(-dumppid, SIGTERM);
1971 dbprintf(_("waiting for %s \"%s\" child\n"), SAMBA_CLIENT, qdisk);
1972 waitpid(dumppid, &wait_status, 0);
1973 if (WIFSIGNALED(wait_status)) {
1974 *errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
1975 SAMBA_CLIENT, WTERMSIG(wait_status), dbfn());
1976 } else if (WIFEXITED(wait_status)) {
1977 if (WEXITSTATUS(wait_status) != 0) {
1978 *errmsg = vstrallocf(_("%s exited with status %d: see %s"),
1979 SAMBA_CLIENT, WEXITSTATUS(wait_status),
1985 *errmsg = vstrallocf(_("%s got bad exit: see %s"),
1986 SAMBA_CLIENT, dbfn());
1988 dbprintf(_("after %s %s wait\n"), SAMBA_CLIENT, qdisk);
2008 int pipefd = -1, nullfd = -1;
2010 off_t size = (off_t)-1;
2011 FILE *dumpout = NULL;
2012 char *incrname = NULL;
2013 char *basename = NULL;
2014 char *dirname = NULL;
2015 char *inputname = NULL;
2020 char *command = NULL;
2021 char dumptimestr[80];
2025 GPtrArray *argv_ptr = g_ptr_array_new();
2026 char *file_exclude = NULL;
2027 char *file_include = NULL;
2032 char *qdisk = quote_string(dle->disk);
2033 char *gnutar_list_dir;
2034 amwait_t wait_status;
2035 char tmppath[PATH_MAX];
2038 return -2; /* planner will not even consider this level */
2040 if(dle->exclude_file) nb_exclude += dle->exclude_file->nb_element;
2041 if(dle->exclude_list) nb_exclude += dle->exclude_list->nb_element;
2042 if(dle->include_file) nb_include += dle->include_file->nb_element;
2043 if(dle->include_list) nb_include += dle->include_list->nb_element;
2045 if(nb_exclude > 0) file_exclude = build_exclude(dle, 0);
2046 if(nb_include > 0) file_include = build_include(dle, 0);
2048 gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
2049 if (strlen(gnutar_list_dir) == 0)
2050 gnutar_list_dir = NULL;
2051 if (gnutar_list_dir) {
2052 char number[NUM_STR_SIZE];
2054 char *sdisk = sanitise_filename(dle->disk);
2056 basename = vstralloc(gnutar_list_dir,
2058 g_options->hostname,
2063 g_snprintf(number, SIZEOF(number), "%d", level);
2064 incrname = vstralloc(basename, "_", number, ".new", NULL);
2068 * Open the listed incremental file from the previous level. Search
2069 * backward until one is found. If none are found (which will also
2070 * be true for a level 0), arrange to read from /dev/null.
2074 while (infd == -1) {
2075 if (--baselevel >= 0) {
2076 g_snprintf(number, SIZEOF(number), "%d", baselevel);
2077 inputname = newvstralloc(inputname,
2078 basename, "_", number, NULL);
2080 inputname = newstralloc(inputname, "/dev/null");
2082 if ((infd = open(inputname, O_RDONLY)) == -1) {
2084 *errmsg = vstrallocf(_("gnutar: error opening %s: %s"),
2085 inputname, strerror(errno));
2086 dbprintf("%s\n", *errmsg);
2087 if (baselevel < 0) {
2095 * Copy the previous listed incremental file to the new one.
2097 if ((outfd = open(incrname, O_WRONLY|O_CREAT, 0600)) == -1) {
2098 *errmsg = vstrallocf(_("opening %s: %s"),
2099 incrname, strerror(errno));
2100 dbprintf("%s\n", *errmsg);
2104 while ((nb = read(infd, &buf, SIZEOF(buf))) > 0) {
2105 if (full_write(outfd, &buf, (size_t)nb) < (size_t)nb) {
2106 *errmsg = vstrallocf(_("writing to %s: %s"),
2107 incrname, strerror(errno));
2108 dbprintf("%s\n", *errmsg);
2114 *errmsg = vstrallocf(_("reading from %s: %s"),
2115 inputname, strerror(errno));
2116 dbprintf("%s\n", *errmsg);
2120 if (close(infd) != 0) {
2121 *errmsg = vstrallocf(_("closing %s: %s"),
2122 inputname, strerror(errno));
2123 dbprintf("%s\n", *errmsg);
2126 if (close(outfd) != 0) {
2127 *errmsg = vstrallocf(_("closing %s: %s"),
2128 incrname, strerror(errno));
2129 dbprintf("%s\n", *errmsg);
2137 gmtm = gmtime(&dumpsince);
2138 g_snprintf(dumptimestr, SIZEOF(dumptimestr),
2139 "%04d-%02d-%02d %2d:%02d:%02d GMT",
2140 gmtm->tm_year + 1900, gmtm->tm_mon+1, gmtm->tm_mday,
2141 gmtm->tm_hour, gmtm->tm_min, gmtm->tm_sec);
2143 dirname = amname_to_dirname(dle->device);
2145 cmd = vstralloc(amlibexecdir, "/", "runtar", NULL);
2146 g_ptr_array_add(argv_ptr, stralloc("runtar"));
2147 if (g_options->config)
2148 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
2150 g_ptr_array_add(argv_ptr, stralloc("NOCONFIG"));
2153 g_ptr_array_add(argv_ptr, stralloc(GNUTAR));
2155 g_ptr_array_add(argv_ptr, stralloc("tar"));
2157 g_ptr_array_add(argv_ptr, stralloc("--create"));
2158 g_ptr_array_add(argv_ptr, stralloc("--file"));
2159 g_ptr_array_add(argv_ptr, stralloc("/dev/null"));
2160 /* use --numeric-owner for estimates, to reduce the number of user/group
2161 * lookups required */
2162 g_ptr_array_add(argv_ptr, stralloc("--numeric-owner"));
2163 g_ptr_array_add(argv_ptr, stralloc("--directory"));
2164 canonicalize_pathname(dirname, tmppath);
2165 g_ptr_array_add(argv_ptr, stralloc(tmppath));
2166 g_ptr_array_add(argv_ptr, stralloc("--one-file-system"));
2167 if (gnutar_list_dir) {
2168 g_ptr_array_add(argv_ptr, stralloc("--listed-incremental"));
2169 g_ptr_array_add(argv_ptr, stralloc(incrname));
2171 g_ptr_array_add(argv_ptr, stralloc("--incremental"));
2172 g_ptr_array_add(argv_ptr, stralloc("--newer"));
2173 g_ptr_array_add(argv_ptr, stralloc(dumptimestr));
2175 #ifdef ENABLE_GNUTAR_ATIME_PRESERVE
2176 /* --atime-preserve causes gnutar to call
2177 * utime() after reading files in order to
2178 * adjust their atime. However, utime()
2179 * updates the file's ctime, so incremental
2180 * dumps will think the file has changed. */
2181 g_ptr_array_add(argv_ptr, stralloc("--atime-preserve"));
2183 g_ptr_array_add(argv_ptr, stralloc("--sparse"));
2184 g_ptr_array_add(argv_ptr, stralloc("--ignore-failed-read"));
2185 g_ptr_array_add(argv_ptr, stralloc("--totals"));
2188 g_ptr_array_add(argv_ptr, stralloc("--exclude-from"));
2189 g_ptr_array_add(argv_ptr, stralloc(file_exclude));
2193 g_ptr_array_add(argv_ptr, stralloc("--files-from"));
2194 g_ptr_array_add(argv_ptr, stralloc(file_include));
2197 g_ptr_array_add(argv_ptr, stralloc("."));
2199 g_ptr_array_add(argv_ptr, NULL);
2201 start_time = curclock();
2203 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
2204 *errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
2206 dbprintf("%s\n", *errmsg);
2210 command = (char *)g_ptr_array_index(argv_ptr, 0);
2211 dumppid = pipespawnv(cmd, STDERR_PIPE, 0,
2212 &nullfd, &nullfd, &pipefd, (char **)argv_ptr->pdata);
2214 dumpout = fdopen(pipefd,"r");
2216 error(_("Can't fdopen: %s"), strerror(errno));
2220 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
2221 if (line[0] == '\0')
2223 dbprintf("%s\n", line);
2224 size = handle_dumpline(line);
2225 if(size > (off_t)-1) {
2227 while ((line = agets(dumpout)) != NULL) {
2228 if (line[0] != '\0') {
2234 dbprintf("%s\n", line);
2242 dbprintf(".....\n");
2243 dbprintf(_("estimate time for %s level %d: %s\n"),
2246 walltime_str(timessub(curclock(), start_time)));
2247 if(size == (off_t)-1) {
2248 *errmsg = vstrallocf(_("no size line match in %s output"),
2250 dbprintf(_("%s for %s\n"), *errmsg, qdisk);
2251 dbprintf(".....\n");
2252 } else if(size == (off_t)0 && level == 0) {
2253 dbprintf(_("possible %s problem -- is \"%s\" really empty?\n"),
2254 command, dle->disk);
2255 dbprintf(".....\n");
2257 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
2262 kill(-dumppid, SIGTERM);
2264 dbprintf(_("waiting for %s \"%s\" child\n"),
2266 waitpid(dumppid, &wait_status, 0);
2267 if (WIFSIGNALED(wait_status)) {
2268 *errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
2269 cmd, WTERMSIG(wait_status), dbfn());
2270 } else if (WIFEXITED(wait_status)) {
2271 if (WEXITSTATUS(wait_status) != 0) {
2272 *errmsg = vstrallocf(_("%s exited with status %d: see %s"),
2273 cmd, WEXITSTATUS(wait_status), dbfn());
2278 *errmsg = vstrallocf(_("%s got bad exit: see %s"),
2281 dbprintf(_("after %s %s wait\n"), command, qdisk);
2292 g_ptr_array_free_full(argv_ptr);
2295 amfree(file_exclude);
2296 amfree(file_include);
2308 getsize_application_api(
2309 disk_estimates_t *est,
2312 backup_support_option_t *bsu)
2314 dle_t *dle = est->dle;
2315 int pipeinfd[2], pipeoutfd[2], pipeerrfd[2];
2317 off_t size = (off_t)-1;
2325 GPtrArray *argv_ptr = g_ptr_array_new();
2326 char *newoptstr = NULL;
2329 char *qdisk = quote_string(dle->disk);
2330 char *qamdevice = quote_string(dle->device);
2331 amwait_t wait_status;
2332 char levelstr[NUM_STR_SIZE];
2335 char *errmsg = NULL;
2336 estimate_t estimate;
2339 cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
2341 g_ptr_array_add(argv_ptr, stralloc(dle->program));
2342 g_ptr_array_add(argv_ptr, stralloc("estimate"));
2343 if (bsu->message_line == 1) {
2344 g_ptr_array_add(argv_ptr, stralloc("--message"));
2345 g_ptr_array_add(argv_ptr, stralloc("line"));
2347 if (g_options->config && bsu->config == 1) {
2348 g_ptr_array_add(argv_ptr, stralloc("--config"));
2349 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
2351 if (g_options->hostname && bsu->host == 1) {
2352 g_ptr_array_add(argv_ptr, stralloc("--host"));
2353 g_ptr_array_add(argv_ptr, stralloc(g_options->hostname));
2355 g_ptr_array_add(argv_ptr, stralloc("--device"));
2356 g_ptr_array_add(argv_ptr, stralloc(dle->device));
2357 if (dle->disk && bsu->disk == 1) {
2358 g_ptr_array_add(argv_ptr, stralloc("--disk"));
2359 g_ptr_array_add(argv_ptr, stralloc(dle->disk));
2361 for (j=0; j < nb_level; j++) {
2362 g_ptr_array_add(argv_ptr, stralloc("--level"));
2363 g_snprintf(levelstr,SIZEOF(levelstr),"%d", levels[j]);
2364 g_ptr_array_add(argv_ptr, stralloc(levelstr));
2366 /* find the first in ES_CLIENT and ES_CALCSIZE */
2367 estimate = ES_CLIENT;
2368 for (el = dle->estimatelist; el != NULL; el = el->next) {
2369 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
2370 if ((estimate == ES_CLIENT && bsu->client_estimate) ||
2371 (estimate == ES_CALCSIZE && bsu->calcsize))
2373 estimate = ES_CLIENT;
2375 if (estimate == ES_CALCSIZE && bsu->calcsize) {
2376 g_ptr_array_add(argv_ptr, stralloc("--calcsize"));
2379 application_property_add_to_argv(argv_ptr, dle, bsu, g_options->features);
2381 for (scriptlist = dle->scriptlist; scriptlist != NULL;
2382 scriptlist = scriptlist->next) {
2383 script = (script_t *)scriptlist->data;
2384 if (script->result && script->result->proplist) {
2385 property_add_to_argv(argv_ptr, script->result->proplist);
2389 g_ptr_array_add(argv_ptr, NULL);
2391 cmdline = stralloc(cmd);
2392 for(i = 1; i < argv_ptr->len-1; i++)
2393 cmdline = vstrextend(&cmdline, " ",
2394 (char *)g_ptr_array_index(argv_ptr, i), NULL);
2395 dbprintf("running: \"%s\"\n", cmdline);
2398 if (pipe(pipeerrfd) < 0) {
2399 errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2404 if (pipe(pipeinfd) < 0) {
2405 errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2410 if (pipe(pipeoutfd) < 0) {
2411 errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2416 start_time = curclock();
2418 switch(dumppid = fork()) {
2425 dup2(pipeinfd[0], 0);
2426 dup2(pipeoutfd[1], 1);
2427 dup2(pipeerrfd[1], 2);
2428 aclose(pipeinfd[1]);
2429 aclose(pipeoutfd[0]);
2430 aclose(pipeerrfd[0]);
2433 execve(cmd, (char **)argv_ptr->pdata, safe_env());
2434 error(_("exec %s failed: %s"), cmd, strerror(errno));
2439 aclose(pipeinfd[0]);
2440 aclose(pipeoutfd[1]);
2441 aclose(pipeerrfd[1]);
2442 aclose(pipeinfd[1]);
2444 dumpout = fdopen(pipeoutfd[0],"r");
2446 error(_("Can't fdopen: %s"), strerror(errno));
2450 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
2451 long long size1_ = (long long)0;
2452 long long size2_ = (long long)0;
2454 if (line[0] == '\0')
2456 dbprintf("%s\n", line);
2457 if (strncmp(line,"ERROR ", 6) == 0) {
2458 char *errmsg, *qerrmsg;
2460 errmsg = stralloc(line+6);
2461 qerrmsg = quote_string(errmsg);
2462 dbprintf(_("errmsg is %s\n"), errmsg);
2463 g_printf(_("%s %d ERROR %s\n"), est->qamname, levels[0], qerrmsg);
2467 i = sscanf(line, "%d %lld %lld", &level, &size1_, &size2_);
2469 i = sscanf(line, "%lld %lld", &size1_, &size2_);
2472 char *errmsg, *qerrmsg;
2474 errmsg = vstrallocf(_("bad line %s"), line);
2475 qerrmsg = quote_string(errmsg);
2476 dbprintf(_("errmsg is %s\n"), errmsg);
2477 g_printf(_("%s %d ERROR %s\n"), est->qamname, levels[0], qerrmsg);
2482 size1 = (off_t)size1_;
2483 size2 = (off_t)size2_;
2484 if (size1 <= 0 || size2 <=0)
2486 else if (size1 * size2 > 0)
2487 size = size1 * size2;
2488 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
2492 g_printf("%s %d SIZE %lld\n", est->qamname, level, (long long)size);
2496 dumperr = fdopen(pipeerrfd[0],"r");
2498 error(_("Can't fdopen: %s"), strerror(errno));
2502 while ((line = agets(dumperr)) != NULL) {
2503 if (strlen(line) > 0) {
2504 char *err = g_strdup_printf(_("Application '%s': %s"),
2505 dle->program, line);
2506 char *qerr = quote_string(err);
2507 for (j=0; j < nb_level; j++) {
2508 fprintf(stdout, "%s %d ERROR %s\n",
2509 est->qamname, levels[j], qerr);
2511 dbprintf("ERROR %s", qerr);
2518 dbprintf(".....\n");
2519 if (nb_level == 1) {
2520 dbprintf(_("estimate time for %s level %d: %s\n"), qamdevice,
2521 levels[0], walltime_str(timessub(curclock(), start_time)));
2523 dbprintf(_("estimate time for %s all level: %s\n"), qamdevice,
2524 walltime_str(timessub(curclock(), start_time)));
2527 kill(-dumppid, SIGTERM);
2529 dbprintf(_("waiting for %s \"%s\" child\n"), cmd, qdisk);
2530 waitpid(dumppid, &wait_status, 0);
2531 if (WIFSIGNALED(wait_status)) {
2532 errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
2533 cmd, WTERMSIG(wait_status), dbfn());
2534 } else if (WIFEXITED(wait_status)) {
2535 if (WEXITSTATUS(wait_status) != 0) {
2536 errmsg = vstrallocf(_("%s exited with status %d: see %s"), cmd,
2537 WEXITSTATUS(wait_status), dbfn());
2542 errmsg = vstrallocf(_("%s got bad exit: see %s"),
2545 dbprintf(_("after %s %s wait\n"), cmd, qdisk);
2553 g_ptr_array_free_full(argv_ptr);
2558 char *qerrmsg = quote_string(errmsg);
2559 dbprintf(_("errmsg is %s\n"), errmsg);
2560 for (j=0; j < nb_level; j++) {
2561 g_printf(_("%s %d ERROR %s\n"), est->qamname, levels[j], qerrmsg);
2571 * Returns the value of the first integer in a string.
2583 while(ch && !isdigit(ch)) ch = *str++;
2585 while(isdigit(ch) || (ch == '.')) ch = *str++;
2594 * Checks the dump output line against the error and size regex tables.
2604 /* check for size match */
2606 for(rp = re_size; rp->regex != NULL; rp++) {
2607 if(match(rp->regex, str)) {
2608 size = ((first_num(str)*rp->scale+1023.0)/1024.0);
2610 size = 1.0; /* found on NeXT -- sigh */