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;
125 disk_estimates_t *est_next;
129 char *err_extra = NULL;
135 char *qamdevice = NULL;
140 (void)argc; /* Quiet unused parameter warning */
141 (void)argv; /* Quiet unused parameter warning */
146 * Configure program for internationalization:
147 * 1) Only set the message locale for now.
148 * 2) Set textdomain for all amanda related programs to "amanda"
149 * We don't want to be forced to support dozens of message catalogs.
151 setlocale(LC_MESSAGES, "C");
152 textdomain("amanda");
158 set_pname("sendsize");
160 /* Don't die when child closes pipe */
161 signal(SIGPIPE, SIG_IGN);
163 add_amanda_log_handler(amanda_log_stderr);
164 add_amanda_log_handler(amanda_log_syslog);
165 dbopen(DBG_SUBDIR_CLIENT);
167 dbprintf(_("version %s\n"), VERSION);
169 our_features = am_init_feature_set();
170 our_feature_string = am_feature_to_string(our_features);
172 config_init(CONFIG_INIT_CLIENT, NULL);
173 /* (check for config errors comes later) */
175 check_running_as(RUNNING_AS_CLIENT_LOGIN);
177 /* handle all service requests */
179 for(; (line = agets(stdin)) != NULL; free(line)) {
182 if(strncmp_const(line, "OPTIONS ") == 0) {
183 g_options = parse_g_options(line+8, 1);
184 if(!g_options->hostname) {
185 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
186 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
187 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
190 g_printf("OPTIONS ");
191 if(am_has_feature(g_options->features, fe_rep_options_features)) {
192 g_printf("features=%s;", our_feature_string);
194 if(am_has_feature(g_options->features, fe_rep_options_maxdumps)) {
195 g_printf("maxdumps=%d;", g_options->maxdumps);
197 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
198 g_printf("hostname=%s;", g_options->hostname);
203 if (g_options->config) {
204 /* overlay this configuration on the existing (nameless) configuration */
205 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
208 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
211 /* check for any config errors now */
212 if (config_errors(&errlist) >= CFGERR_ERRORS) {
213 char *errstr = config_errors_to_error_string(errlist);
214 g_printf("%s\n", errstr);
219 if (am_has_feature(g_options->features, fe_req_xml)) {
229 skip_whitespace(s, ch); /* find the program name */
231 err_extra = stralloc(_("no program name"));
232 goto err; /* no program name */
234 dle->program = s - 1;
235 skip_non_whitespace(s, ch);
238 dle->program_is_application_api=0;
239 if(strncmp_const(dle->program, "CALCSIZE") == 0) {
240 skip_whitespace(s, ch); /* find the program name */
242 err_extra = stralloc(_("no program name"));
245 dle->estimatelist = g_slist_append(dle->estimatelist,
246 GINT_TO_POINTER(ES_CALCSIZE));
247 dle->program = s - 1;
248 skip_non_whitespace(s, ch);
250 if (strcmp(dle->program,"APPLICATION") == 0) {
251 dle->program_is_application_api=1;
252 skip_whitespace(s, ch); /* find dumper name */
254 goto err; /* no program */
256 dle->program = s - 1;
257 skip_non_whitespace(s, ch);
262 dle->estimatelist = g_slist_append(dle->estimatelist,
263 GINT_TO_POINTER(ES_CLIENT));
264 if (strcmp(dle->program,"APPLICATION") == 0) {
265 dle->program_is_application_api=1;
266 skip_whitespace(s, ch); /* find dumper name */
268 goto err; /* no program */
270 dle->program = s - 1;
271 skip_non_whitespace(s, ch);
275 dle->program = stralloc(dle->program);
277 skip_whitespace(s, ch); /* find the disk name */
279 err_extra = stralloc(_("no disk name"));
280 goto err; /* no disk name */
287 skip_quoted_string(s, ch);
288 s[-1] = '\0'; /* terminate the disk name */
289 qdisk = stralloc(fp);
290 dle->disk = unquote_string(qdisk);
292 skip_whitespace(s, ch); /* find the device or level */
294 err_extra = stralloc(_("bad level"));
297 if(!isdigit((int)s[-1])) {
299 skip_quoted_string(s, ch);
301 qamdevice = stralloc(fp);
302 dle->device = unquote_string(qamdevice);
303 skip_whitespace(s, ch); /* find level number */
306 dle->device = stralloc(dle->disk);
307 qamdevice = stralloc(qdisk);
310 /* find the level number */
311 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
312 err_extra = stralloc(_("bad level"));
313 goto err; /* bad level */
315 if (level < 0 || level >= DUMP_LEVELS) {
316 err_extra = stralloc(_("bad level"));
320 alevel = g_new0(level_t, 1);
321 alevel->level = level;
322 dle->levellist = g_slist_append(dle->levellist, alevel);
324 skip_whitespace(s, ch); /* find the dump date */
326 err_extra = stralloc(_("no dumpdate"));
327 goto err; /* no dumpdate */
330 skip_non_whitespace(s, ch);
332 (void)dumpdate; /* XXX: Set but not used */
334 dle->spindle = 0; /* default spindle */
336 skip_whitespace(s, ch); /* find the spindle */
338 if(sscanf(s - 1, "%d", &dle->spindle) != 1) {
339 err_extra = stralloc(_("bad spindle"));
340 goto err; /* bad spindle */
344 skip_whitespace(s, ch); /* find the parameters */
346 if(strncmp_const(s-1, "OPTIONS |;") == 0) {
354 if(strncmp_const(s-1, "exclude-file=") == 0) {
355 qlist = unquote_string(s+12);
357 append_sl(dle->exclude_file, qlist);
359 } else if(strncmp_const(s-1, "exclude-list=") == 0) {
360 qlist = unquote_string(s+12);
362 append_sl(dle->exclude_list, qlist);
364 } else if(strncmp_const(s-1, "include-file=") == 0) {
365 qlist = unquote_string(s+12);
367 append_sl(dle->include_file, qlist);
369 } else if(strncmp_const(s-1, "include-list=") == 0) {
370 qlist = unquote_string(s+12);
372 append_sl(dle->include_list, qlist);
375 err_extra = vstrallocf(_("Invalid parameter (%s)"), s-1);
376 goto err; /* should have gotten to end */
378 skip_quoted_string(s, ch);
379 skip_whitespace(s, ch); /* find the inclusion list */
387 dle_add_diskest(dle);
390 if (g_options == NULL) {
391 g_printf(_("ERROR [Missing OPTIONS line in sendsize input]\n"));
392 error(_("Missing OPTIONS line in sendsize input\n"));
397 if (am_has_feature(g_options->features, fe_req_xml)) {
401 dles = amxml_parse_node_FILE(stdin, &errmsg);
406 for (dle = dles; dle != NULL; dle = dle->next) {
407 dle_add_diskest(dle);
411 if (amandates_started) {
414 amandates_started = FALSE;
418 for(est = est_list; est != NULL; est = est_next) {
419 int good = merge_dles_properties(est->dle, 0);
420 est_next = est->next;
422 if (est == est_list) {
425 est_prev->next = est_next;
431 for(est = est_list; est != NULL; est = est->next) {
432 run_client_scripts(EXECUTE_ON_PRE_HOST_ESTIMATE, g_options, est->dle,
442 * See if we need to wait for a child before we can do anything
447 amwait_t child_status;
450 dbprintf(_("waiting for any estimate child: %d running\n"),
452 child_pid = wait(&child_status);
453 if(child_pid == -1) {
454 error(_("wait failed: %s"), strerror(errno));
458 if (!WIFEXITED(child_status) || WEXITSTATUS(child_status) != 0) {
459 char *child_name = vstrallocf(_("child %ld"), (long)child_pid);
460 char *child_status_str = str_exit_status(child_name, child_status);
461 dbprintf("%s\n", child_status_str);
462 amfree(child_status_str);
467 * Find the child and mark it done.
469 for(est = est_list; est != NULL; est = est->next) {
470 if(est->child == child_pid) {
475 dbprintf(_("unexpected child %ld\n"), (long)child_pid);
480 run_client_scripts(EXECUTE_ON_POST_DLE_ESTIMATE, g_options,
485 * If we are already running the maximum number of children
486 * go back and wait until one of them finishes.
488 if(dumpsrunning >= g_options->maxdumps) {
491 continue; /* have to wait first */
494 * Find a new child to start.
496 for(est = est_list; est != NULL; est = est->next) {
498 done = 0; /* more to do */
500 if(est->child != 0 || est->done) {
501 continue; /* child is running or done */
504 * Make sure there is no spindle conflict.
506 if(est->dle->spindle != -1) {
507 for(est1 = est_list; est1 != NULL; est1 = est1->next) {
508 if(est1->child == 0 || est == est1 || est1->done) {
510 * Ignore anything not yet started, ourself,
511 * and anything completed.
515 if(est1->dle->spindle == est->dle->spindle) {
516 break; /* oops -- they match */
520 continue; /* spindle conflict */
523 break; /* start this estimate */
526 if(dumpsrunning > 0) {
527 need_wait = 1; /* nothing to do but wait */
531 run_client_scripts(EXECUTE_ON_PRE_DLE_ESTIMATE, g_options,
534 if((est->child = fork()) == 0) {
535 calc_estimates(est); /* child does the estimate */
537 } else if(est->child == -1) {
538 error(_("calc_estimates fork failed: %s"), strerror(errno));
541 dumpsrunning++; /* parent */
545 for(est = est_list; est != NULL; est = est->next) {
546 run_client_scripts(EXECUTE_ON_POST_HOST_ESTIMATE, g_options, est->dle,
551 for(est = est_list; est != NULL; est = est->next) {
557 amfree(our_feature_string);
558 am_release_feature_set(our_features);
560 free_g_options(g_options);
566 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET '%s'\n"), err_extra);
567 dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
570 g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET\n"));
571 dbprintf(_("REQ packet is bogus\n"));
574 free_g_options(g_options);
585 disk_estimates_t *newp, *curp;
589 levellist_t levellist;
590 char *amandates_file;
591 gboolean need_amandates = FALSE;
594 if (dle->levellist == NULL) {
595 g_printf(_("ERROR Missing level in request\n"));
599 /* should we use amandates for this? */
600 for (el = dle->estimatelist; el != NULL; el=el->next) {
601 estimate_t estimate = (estimate_t)GPOINTER_TO_INT(el->data);
602 if (estimate == ES_CALCSIZE)
603 need_amandates = TRUE;
606 if (strcmp(dle->program, "GNUTAR") == 0) {
607 /* GNUTAR only needs amandates if gnutar_list_dir is NULL */
608 char *gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
609 if (!gnutar_list_dir || !*gnutar_list_dir)
610 need_amandates = TRUE;
613 /* start amandates here, before adding this DLE to est_list, in case
614 * we encounter an error. */
615 if (need_amandates) {
616 if (!amandates_started) {
617 amandates_file = getconf_str(CNF_AMANDATES);
618 if(!start_amandates(amandates_file, 0)) {
619 char *errstr = strerror(errno);
620 char *qamname = quote_string(dle->disk);
621 char *errmsg = vstrallocf(_("could not open %s: %s"), amandates_file, errstr);
622 char *qerrmsg = quote_string(errmsg);
623 g_printf(_("%s %d ERROR %s\n"), qamname, 0, qerrmsg);
629 amandates_started = TRUE;
633 levellist = dle->levellist;
634 while (levellist != NULL) {
635 level_t *alevel = (level_t *)levellist->data;
636 if (alevel->level < 0)
638 if (alevel->level >= DUMP_LEVELS)
639 alevel->level = DUMP_LEVELS - 1;
640 levellist = g_slist_next(levellist);
643 for(curp = est_list; curp != NULL; curp = curp->next) {
644 if(strcmp(curp->dle->disk, dle->disk) == 0) {
645 /* already have disk info, just note the level request */
646 levellist = dle->levellist;
647 while (levellist != NULL) {
648 level_t *alevel = (level_t *)levellist->data;
649 int level = alevel->level;
650 curp->est[level].needestimate = 1;
651 curp->est[level].server = alevel->server;
652 levellist = g_slist_next(levellist);
659 newp = (disk_estimates_t *) alloc(SIZEOF(disk_estimates_t));
660 memset(newp, 0, SIZEOF(*newp));
661 newp->next = est_list;
663 newp->qamname = quote_string(dle->disk);
665 newp->qamdevice = quote_string(dle->device);
666 newp->dirname = amname_to_dirname(dle->device);
667 newp->qdirname = quote_string(newp->dirname);
669 newp->qamdevice = stralloc("");
670 newp->dirname = stralloc("");
671 newp->qdirname = stralloc("");
673 levellist = dle->levellist;
674 while (levellist != NULL) {
675 level_t *alevel = (level_t *)levellist->data;
676 newp->est[alevel->level].needestimate = 1;
677 newp->est[alevel->level].server = alevel->server;
678 levellist = g_slist_next(levellist);
682 /* fill in dump-since dates */
683 if (need_amandates) {
684 amdp = amandates_lookup(newp->dle->disk);
686 newp->est[0].dumpsince = EPOCH;
687 for(dumplev = 0; dumplev < DUMP_LEVELS; dumplev++) {
688 dumpdate = amdp->dates[dumplev];
689 for(estlev = dumplev+1; estlev < DUMP_LEVELS; estlev++) {
690 if(dumpdate > newp->est[estlev].dumpsince)
691 newp->est[estlev].dumpsince = dumpdate;
695 /* just zero everything out */
696 for(dumplev = 0; dumplev < DUMP_LEVELS; dumplev++) {
697 newp->est[dumplev].dumpsince = 0;
705 disk_estimates_t * est)
707 amfree(est->qamname);
708 amfree(est->qamdevice);
709 amfree(est->dirname);
710 amfree(est->qdirname);
717 * ------------------------------------------------------------------------
723 disk_estimates_t * est)
725 dbprintf(_("calculating for amname %s, dirname %s, spindle %d %s\n"),
726 est->qamname, est->qdirname, est->dle->spindle, est->dle->program);
728 if(est->dle->program_is_application_api == 1)
729 application_api_calc_estimate(est);
734 estimate_t estimate_method = ES_ES;
735 estimate_t client_method = ES_ES;
737 /* find estimate method to use */
738 for (el = est->dle->estimatelist; el != NULL; el = el->next) {
739 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
740 if (estimate == ES_SERVER) {
741 if (estimate_method == ES_ES)
742 estimate_method = ES_SERVER;
744 if (estimate == ES_CLIENT ||
745 (estimate == ES_CALCSIZE &&
746 (est->dle->device[0] != '/' || est->dle->device[1] != '/'))) {
747 if (client_method == ES_ES)
748 client_method = estimate;
749 if (estimate_method == ES_ES)
750 estimate_method = estimate;
754 /* do server estimate */
755 if (estimate_method == ES_SERVER) {
756 for (level = 0; level < DUMP_LEVELS; level++) {
757 if (est->est[level].needestimate) {
758 if (est->est[level].server || client_method == ES_ES) {
759 g_printf(_("%s %d SIZE -1\n"), est->qamname, level);
760 est->est[level].needestimate = 0;
766 if (client_method == ES_ES && estimate_method != ES_SERVER) {
767 g_printf(_("%s %d SIZE -2\n"), est->qamname, 0);
768 dbprintf(_("Can't use CALCSIZE for samba estimate: %s %s\n"),
769 est->qamname, est->qdirname);
770 } else if (client_method == ES_CALCSIZE) {
771 generic_calc_estimates(est);
772 } else if (client_method == ES_CLIENT) {
773 #ifndef USE_GENERIC_CALCSIZE
774 if (strcmp(est->dle->program, "DUMP") == 0)
775 dump_calc_estimates(est);
779 if (strcmp(est->dle->program, "GNUTAR") == 0 &&
780 est->dle->device[0] == '/' && est->dle->device[1] == '/')
781 smbtar_calc_estimates(est);
785 if (strcmp(est->dle->program, "GNUTAR") == 0)
786 gnutar_calc_estimates(est);
789 dbprintf(_("Invalid program: %s %s %s\n"),
790 est->qamname, est->qdirname, est->dle->program);
794 dbprintf(_("done with amname %s dirname %s spindle %d\n"),
795 est->qamname, est->qdirname, est->dle->spindle);
799 * ------------------------------------------------------------------------
803 /* local functions */
804 off_t getsize_dump(dle_t *dle, int level, char **errmsg);
805 off_t getsize_smbtar(dle_t *dle, int level, char **errmsg);
806 off_t getsize_gnutar(dle_t *dle, int level, time_t dumpsince, char **errmsg);
807 off_t getsize_application_api(disk_estimates_t *est, int nb_level,
808 int *levels, backup_support_option_t *bsu);
809 off_t handle_dumpline(char *str);
810 double first_num(char *str);
813 application_api_calc_estimate(
814 disk_estimates_t * est)
818 int levels[DUMP_LEVELS];
820 backup_support_option_t *bsu;
824 estimate_t estimate_method = ES_ES;
825 estimate_t client_method = ES_ES;
826 int has_calcsize = 0;
829 bsu = backup_support_option(est->dle->program, g_options, est->dle->disk,
830 est->dle->device, &errarray);
833 for (i=0; i < errarray->len; i++) {
837 line = g_ptr_array_index(errarray, i);
838 if(am_has_feature(g_options->features,
839 fe_rep_sendsize_quoted_error)) {
840 errmsg = g_strdup_printf(_("Application '%s': %s"),
841 est->dle->program, line);
842 qerrmsg = quote_string(errmsg);
843 for (level = 0; level < DUMP_LEVELS; level++) {
844 if (est->est[level].needestimate) {
845 g_printf(_("%s %d ERROR %s\n"),
846 est->dle->disk, level, qerrmsg);
847 dbprintf(_("%s %d ERROR %s\n"),
848 est->qamname, level, qerrmsg);
855 if (i == 0) { /* nothing in errarray */
858 errmsg = g_strdup_printf(
859 _("Application '%s': cannon execute support command"),
861 qerrmsg = quote_string(errmsg);
862 for (level = 0; level < DUMP_LEVELS; level++) {
863 if (est->est[level].needestimate) {
864 g_printf(_("%s %d ERROR %s\n"),
865 est->dle->disk, level, qerrmsg);
866 dbprintf(_("%s %d ERROR %s\n"),
867 est->qamname, level, qerrmsg);
873 for (level = 0; level < DUMP_LEVELS; level++) {
874 est->est[level].needestimate = 0;
876 g_ptr_array_free(errarray, TRUE);
879 if (est->dle->data_path == DATA_PATH_AMANDA &&
880 (bsu->data_path_set & DATA_PATH_AMANDA)==0) {
881 g_printf("%s %d ERROR application %s doesn't support amanda data-path\n", est->qamname, 0, est->dle->program);
885 if (est->dle->data_path == DATA_PATH_DIRECTTCP &&
886 (bsu->data_path_set & DATA_PATH_DIRECTTCP)==0) {
887 g_printf("%s %d ERROR application %s doesn't support directtcp data-path\n", est->qamname, 0, est->dle->program);
892 /* find estimate method to use */
893 for (el = est->dle->estimatelist; el != NULL; el = el->next) {
894 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
895 if (estimate == ES_CLIENT)
897 if (estimate == ES_CALCSIZE)
899 if (estimate == ES_SERVER) {
900 if (estimate_method == ES_ES)
901 estimate_method = ES_SERVER;
903 if ((estimate == ES_CLIENT && bsu->client_estimate) ||
904 (estimate == ES_CALCSIZE && bsu->calcsize)) {
905 if (client_method == ES_ES)
906 client_method = estimate;
907 if (estimate_method == ES_ES)
908 estimate_method = estimate;
912 for(level = 0; level < DUMP_LEVELS; level++) {
913 if (est->est[level].needestimate) {
914 if (level > bsu->max_level) {
915 /* planner will not even consider this level */
916 g_printf("%s %d SIZE %lld\n", est->qamname, level,
918 est->est[level].needestimate = 0;
919 dbprintf(_("Application '%s' can't estimate level %d\n"),
920 est->dle->program, level);
921 } else if (estimate_method == ES_ES) {
922 g_printf("%s %d SIZE %lld\n", est->qamname, level,
924 est->est[level].needestimate = 0;
925 if (am_has_feature(g_options->features,
926 fe_rep_sendsize_quoted_error)) {
927 char *errmsg, *qerrmsg;
928 if (has_client && !bsu->client_estimate &&
929 has_calcsize && !bsu->calcsize) {
930 errmsg = vstrallocf(_("Application '%s' can't do CLIENT or CALCSIZE estimate"),
932 } else if (has_client && !bsu->client_estimate) {
933 errmsg = vstrallocf(_("Application '%s' can't do CLIENT estimate"),
935 } else if (has_calcsize && !bsu->calcsize) {
936 errmsg = vstrallocf(_("Application '%s' can't do CALCSIZE estimate"),
939 errmsg = vstrallocf(_("Application '%s' can't do estimate"),
942 qerrmsg = quote_string(errmsg);
943 dbprintf(_("errmsg is %s\n"), errmsg);
944 g_printf("%s %d ERROR %s\n",
945 est->qamname, 0, qerrmsg);
949 } else if (estimate_method == ES_SERVER &&
950 (est->est[level].server || client_method == ES_ES)) {
951 /* planner will consider this level, */
952 /* but use a server-side estimate */
953 g_printf("%s %d SIZE -1\n", est->qamname, level);
954 est->est[level].needestimate = 0;
955 } else if (client_method == ES_CLIENT) {
956 levels[nb_level++] = level;
957 } else if (client_method == ES_CALCSIZE) {
958 levels[nb_level++] = level;
968 if (bsu->multi_estimate) {
969 for (i=0;i<nb_level;i++) {
970 dbprintf(_("getting size via application API for %s %s level %d\n"),
971 est->qamname, est->qamdevice, levels[i]);
973 getsize_application_api(est, nb_level, levels, bsu);
976 for(level = 0; level < DUMP_LEVELS; level++) {
977 if (est->est[level].needestimate) {
979 _("getting size via application API for %s %s level %d\n"),
980 est->qamname, est->qamdevice, level);
982 getsize_application_api(est, 1, levels, bsu);
992 generic_calc_estimates(
993 disk_estimates_t * est)
995 int pipefd = -1, nullfd = -1;
999 GPtrArray *argv_ptr = g_ptr_array_new();
1000 char number[NUM_STR_SIZE];
1006 char *file_exclude = NULL;
1007 char *file_include = NULL;
1009 FILE *dumpout = NULL;
1010 off_t size = (off_t)1;
1013 amwait_t wait_status;
1014 char *errmsg = NULL, *qerrmsg;
1015 char tmppath[PATH_MAX];
1018 cmd = vstralloc(amlibexecdir, "/", "calcsize", NULL);
1020 g_ptr_array_add(argv_ptr, stralloc("calcsize"));
1021 if (g_options->config)
1022 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
1024 g_ptr_array_add(argv_ptr, stralloc("NOCONFIG"));
1026 g_ptr_array_add(argv_ptr, stralloc(est->dle->program));
1027 canonicalize_pathname(est->dle->disk, tmppath);
1028 g_ptr_array_add(argv_ptr, stralloc(tmppath));
1029 canonicalize_pathname(est->dirname, tmppath);
1030 g_ptr_array_add(argv_ptr, stralloc(tmppath));
1032 if (est->dle->exclude_file)
1033 nb_exclude += est->dle->exclude_file->nb_element;
1034 if (est->dle->exclude_list)
1035 nb_exclude += est->dle->exclude_list->nb_element;
1036 if (est->dle->include_file)
1037 nb_include += est->dle->include_file->nb_element;
1038 if (est->dle->include_list)
1039 nb_include += est->dle->include_list->nb_element;
1042 file_exclude = build_exclude(est->dle, 0);
1044 file_include = build_include(est->dle, 0);
1047 g_ptr_array_add(argv_ptr, stralloc("-X"));
1048 g_ptr_array_add(argv_ptr, stralloc(file_exclude));
1052 g_ptr_array_add(argv_ptr, stralloc("-I"));
1053 g_ptr_array_add(argv_ptr, stralloc(file_include));
1055 start_time = curclock();
1057 command = (char *)g_ptr_array_index(argv_ptr, 0);
1058 cmdline = stralloc(command);
1059 for(i = 1; i < argv_ptr->len-1; i++) {
1060 cmdline = vstrextend(&cmdline, " ",
1061 (char *)g_ptr_array_index(argv_ptr, i), NULL);
1063 dbprintf(_("running: \"%s\"\n"), cmdline);
1066 for(level = 0; level < DUMP_LEVELS; level++) {
1067 if(est->est[level].needestimate) {
1068 g_snprintf(number, SIZEOF(number), "%d", level);
1069 g_ptr_array_add(argv_ptr, stralloc(number));
1070 dbprintf(" %s", number);
1071 g_snprintf(number, SIZEOF(number),
1072 "%ld", (long)est->est[level].dumpsince);
1073 g_ptr_array_add(argv_ptr, stralloc(number));
1074 dbprintf(" %s", number);
1077 g_ptr_array_add(argv_ptr, NULL);
1080 fflush(stderr); fflush(stdout);
1082 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
1083 errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
1085 dbprintf("%s\n", errmsg);
1089 calcpid = pipespawnv(cmd, STDERR_PIPE, 0,
1090 &nullfd, &nullfd, &pipefd, (char **)argv_ptr->pdata);
1093 dumpout = fdopen(pipefd,"r");
1095 error(_("Can't fdopen: %s"), strerror(errno));
1098 match_expr = vstralloc(" %d SIZE %lld", NULL);
1099 len = strlen(est->qamname);
1100 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1101 long long size_ = (long long)0;
1102 if (line[0] == '\0' || (int)strlen(line) <= len)
1104 /* Don't use sscanf for est->qamname because it can have a '%'. */
1105 if (strncmp(line, est->qamname, len) == 0 &&
1106 sscanf(line+len, match_expr, &level, &size_) == 2) {
1107 g_printf("%s\n", line); /* write to amandad */
1108 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1113 size = (off_t)size_;
1117 dbprintf(_("waiting for %s %s child (pid=%d)\n"),
1118 command, est->qamdevice, (int)calcpid);
1119 waitpid(calcpid, &wait_status, 0);
1120 if (WIFSIGNALED(wait_status)) {
1121 errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
1122 "calcsize", WTERMSIG(wait_status),
1124 } else if (WIFEXITED(wait_status)) {
1125 if (WEXITSTATUS(wait_status) != 0) {
1126 errmsg = vstrallocf(_("%s exited with status %d: see %s"),
1127 "calcsize", WEXITSTATUS(wait_status),
1133 errmsg = vstrallocf(_("%s got bad exit: see %s"),
1134 "calcsize", dbfn());
1136 dbprintf(_("after %s %s wait: child pid=%d status=%d\n"),
1137 command, est->qamdevice,
1138 (int)calcpid, WEXITSTATUS(wait_status));
1140 dbprintf(_(".....\n"));
1141 dbprintf(_("estimate time for %s: %s\n"),
1143 walltime_str(timessub(curclock(), start_time)));
1146 if (errmsg && errmsg[0] != '\0') {
1147 if(am_has_feature(g_options->features, fe_rep_sendsize_quoted_error)) {
1148 qerrmsg = quote_string(errmsg);
1149 dbprintf(_("errmsg is %s\n"), errmsg);
1150 g_printf("%s %d ERROR %s\n",
1151 est->qamname, 0, qerrmsg);
1156 g_ptr_array_free_full(argv_ptr);
1162 dump_calc_estimates(
1163 disk_estimates_t * est)
1167 char *errmsg=NULL, *qerrmsg;
1169 for(level = 0; level < DUMP_LEVELS; level++) {
1170 if(est->est[level].needestimate) {
1171 dbprintf(_("getting size via dump for %s level %d\n"),
1172 est->qamname, level);
1173 size = getsize_dump(est->dle, level, &errmsg);
1177 g_printf(_("%s %d SIZE %lld\n"),
1178 est->qamname, level, (long long)size);
1179 if (errmsg && errmsg[0] != '\0') {
1180 if(am_has_feature(g_options->features,
1181 fe_rep_sendsize_quoted_error)) {
1182 qerrmsg = quote_string(errmsg);
1183 dbprintf(_("errmsg is %s\n"), errmsg);
1184 g_printf("%s %d ERROR %s\n",
1185 est->qamname, level, qerrmsg);
1192 amfunlock(1, "size");
1199 smbtar_calc_estimates(
1200 disk_estimates_t * est)
1204 char *errmsg = NULL, *qerrmsg;
1206 for(level = 0; level < DUMP_LEVELS; level++) {
1207 if(est->est[level].needestimate) {
1208 dbprintf(_("getting size via smbclient for %s level %d\n"),
1209 est->qamname, level);
1210 size = getsize_smbtar(est->dle, level, &errmsg);
1214 g_printf(_("%s %d SIZE %lld\n"),
1215 est->qamname, level, (long long)size);
1216 if (errmsg && errmsg[0] != '\0') {
1217 if(am_has_feature(g_options->features,
1218 fe_rep_sendsize_quoted_error)) {
1219 qerrmsg = quote_string(errmsg);
1220 dbprintf(_("errmsg is %s\n"), errmsg);
1221 g_printf("%s %d ERROR %s\n",
1222 est->qamname, level, qerrmsg);
1229 amfunlock(1, "size");
1237 gnutar_calc_estimates(
1238 disk_estimates_t * est)
1242 char *errmsg = NULL, *qerrmsg;
1244 for(level = 0; level < DUMP_LEVELS; level++) {
1245 if (est->est[level].needestimate) {
1246 dbprintf(_("getting size via gnutar for %s level %d\n"),
1247 est->qamname, level);
1248 size = getsize_gnutar(est->dle, level,
1249 est->est[level].dumpsince,
1254 g_printf(_("%s %d SIZE %lld\n"),
1255 est->qamname, level, (long long)size);
1256 if (errmsg && errmsg[0] != '\0') {
1257 if(am_has_feature(g_options->features,
1258 fe_rep_sendsize_quoted_error)) {
1259 qerrmsg = quote_string(errmsg);
1260 dbprintf(_("errmsg is %s\n"), errmsg);
1261 g_printf(_("%s %d ERROR %s\n"),
1262 est->qamname, level, qerrmsg);
1269 amfunlock(1, "size");
1275 typedef struct regex_scale_s {
1281 regex_scale_t re_size[] = {
1283 {" DUMP: estimated -*[0-9][0-9]* tape blocks", 1024},
1284 {" DUMP: [Ee]stimated [0-9][0-9]* blocks", 512},
1285 {" DUMP: [Ee]stimated [0-9][0-9]* bytes", 1}, /* Ultrix 4.4 */
1286 {" UFSDUMP: estimated [0-9][0-9]* blocks", 512}, /* NEC EWS-UX */
1287 {"dump: Estimate: [0-9][0-9]* tape blocks", 1024}, /* OSF/1 */
1288 {"backup: There are an estimated [0-9][0-9]* tape blocks.",1024}, /* AIX */
1289 {"backup: estimated [0-9][0-9]* 1k blocks", 1024}, /* AIX */
1290 {"backup: estimated [0-9][0-9]* tape blocks", 1024}, /* AIX */
1291 {"backup: [0-9][0-9]* tape blocks on [0-9][0-9]* tape(s)",1024}, /* AIX */
1292 {"backup: [0-9][0-9]* 1k blocks on [0-9][0-9]* volume(s)",1024}, /* AIX */
1293 {"dump: Estimate: [0-9][0-9]* blocks being output to pipe",1024},
1295 {"dump: Dumping [0-9][0-9]* bytes, ", 1}, /* DU 4.0 vdump */
1296 {"DUMP: estimated [0-9][0-9]* KB output", 1024}, /* HPUX */
1297 {"DUMP: estimated [0-9][0-9]* KB\\.", 1024}, /* NetApp */
1298 {" UFSDUMP: estimated [0-9][0-9]* blocks", 512}, /* Sinix */
1300 #ifdef HAVE_DUMP_ESTIMATE
1301 {"[0-9][0-9]* blocks, [0-9][0-9]*.[0-9][0-9]* volumes", 1024},
1302 /* DU 3.2g dump -E */
1303 {"^[0-9][0-9]* blocks$", 1024}, /* DU 4.0 dump -E */
1304 {"^[0-9][0-9]*$", 1}, /* Solaris ufsdump -S */
1309 {"vdump: Dumping [0-9][0-9]* bytes, ", 1}, /* OSF/1 vdump */
1313 {"vxdump: estimated [0-9][0-9]* blocks", 512}, /* HPUX's vxdump */
1314 {" VXDUMP: estimated [0-9][0-9]* blocks", 512}, /* Sinix */
1318 {"xfsdump: estimated dump size: [0-9][0-9]* bytes", 1}, /* Irix 6.2 xfs */
1322 {"Total bytes written: [0-9][0-9]*", 1}, /* Gnutar client */
1326 #if SAMBA_VERSION >= 2
1327 #define SAMBA_DEBUG_LEVEL "0"
1328 {"Total number of bytes: [0-9][0-9]*", 1}, /* Samba du */
1330 #define SAMBA_DEBUG_LEVEL "3"
1331 {"Total bytes listed: [0-9][0-9]*", 1}, /* Samba dir */
1345 int pipefd[2], nullfd, stdoutfd, killctl[2];
1349 char *dumpkeys = NULL;
1350 char *device = NULL;
1351 char *fstype = NULL;
1355 char *rundump_cmd = NULL;
1356 char level_str[NUM_STR_SIZE];
1359 char *qdisk = quote_string(dle->disk);
1362 amwait_t wait_status;
1363 #if defined(DUMP) || defined(VDUMP) || defined(VXDUMP) || defined(XFSDUMP)
1368 return -2; /* planner will not even consider this level */
1370 g_snprintf(level_str, SIZEOF(level_str), "%d", level);
1372 device = amname_to_devname(dle->device);
1373 qdevice = quote_string(device);
1374 fstype = amname_to_fstype(dle->device);
1376 dbprintf(_("calculating for device %s with %s\n"),
1379 cmd = vstralloc(amlibexecdir, "/rundump", NULL);
1380 rundump_cmd = stralloc(cmd);
1381 if (g_options->config)
1382 config = g_options->config;
1384 config = "NOCONFIG";
1385 if ((stdoutfd = nullfd = open("/dev/null", O_RDWR)) == -1) {
1386 *errmsg = vstrallocf(_("getsize_dump could not open /dev/null: %s"),
1388 dbprintf("%s\n", *errmsg);
1390 amfree(rundump_cmd);
1397 pipefd[0] = pipefd[1] = killctl[0] = killctl[1] = -1;
1398 if (pipe(pipefd) < 0) {
1399 *errmsg = vstrallocf(_("getsize_dump could create data pipes: %s"),
1401 dbprintf("%s\n", *errmsg);
1403 amfree(rundump_cmd);
1411 #ifdef XFSDUMP /* { */
1413 if (strcmp(fstype, "xfs") == 0)
1418 name = stralloc(" (xfsdump)");
1419 dbprintf(_("running \"%s%s -F -J -l %s - %s\"\n"),
1420 cmd, name, level_str, qdevice);
1424 #ifdef VXDUMP /* { */
1426 if (strcmp(fstype, "vxfs") == 0)
1432 name = stralloc(" (vxdump)");
1434 name = stralloc("");
1435 cmd = newstralloc(cmd, VXDUMP);
1436 config = skip_argument;
1439 dumpkeys = vstralloc(level_str, "s", "f", NULL);
1440 dbprintf(_("running \"%s%s %s 1048576 - %s\"\n"),
1441 cmd, name, dumpkeys, qdevice);
1445 #ifdef VDUMP /* { */
1447 if (strcmp(fstype, "advfs") == 0)
1452 name = stralloc(" (vdump)");
1453 dumpkeys = vstralloc(level_str, "b", "f", NULL);
1454 dbprintf(_("running \"%s%s %s 60 - %s\"\n"),
1455 cmd, name, dumpkeys, qdevice);
1461 # ifdef USE_RUNDUMP /* { */
1462 # ifdef AIX_BACKUP /* { */
1463 name = stralloc(" (backup)");
1465 name = vstralloc(" (", DUMP, ")", NULL);
1468 name = stralloc("");
1469 cmd = newstralloc(cmd, DUMP);
1470 config = skip_argument;
1474 # ifdef AIX_BACKUP /* { */
1475 dumpkeys = vstralloc("-", level_str, "f", NULL);
1476 dbprintf(_("running \"%s%s %s - %s\"\n"),
1477 cmd, name, dumpkeys, qdevice);
1479 # ifdef HAVE_DUMP_ESTIMATE
1480 # define PARAM_DUMP_ESTIMATE HAVE_DUMP_ESTIMATE
1482 # define PARAM_DUMP_ESTIMATE ""
1484 # ifdef HAVE_HONOR_NODUMP
1485 # define PARAM_HONOR_NODUMP "h"
1487 # define PARAM_HONOR_NODUMP ""
1489 dumpkeys = vstralloc(level_str,
1490 PARAM_DUMP_ESTIMATE,
1494 # ifdef HAVE_DUMP_ESTIMATE
1495 stdoutfd = pipefd[1];
1498 # ifdef HAVE_HONOR_NODUMP /* { */
1499 dbprintf(_("running \"%s%s %s 0 1048576 - %s\"\n"),
1500 cmd, name, dumpkeys, qdevice);
1502 dbprintf(_("running \"%s%s %s 1048576 - %s\"\n"),
1503 cmd, name, dumpkeys, qdevice);
1510 error(_("no dump program available"));
1514 if (pipe(killctl) < 0) {
1515 dbprintf(_("Could not create pipe: %s\n"), strerror(errno));
1516 /* Message will be printed later... */
1517 killctl[0] = killctl[1] = -1;
1520 start_time = curclock();
1521 switch(dumppid = fork()) {
1523 *errmsg = vstrallocf(_("cannot fork for killpgrp: %s"),
1525 dbprintf("%s\n", *errmsg);
1528 amfree(rundump_cmd);
1537 case 0: /* child process */
1540 else if (killctl[0] == -1 || killctl[1] == -1)
1541 dbprintf(_("Trying without killpgrp\n"));
1545 dbprintf(_("fork failed, trying without killpgrp\n"));
1551 char *killpgrp_cmd = vstralloc(amlibexecdir, "/killpgrp", NULL);
1552 dbprintf(_("running %s\n"), killpgrp_cmd);
1553 dup2(killctl[0], 0);
1560 if (g_options->config)
1561 config = g_options->config;
1563 config = "NOCONFIG";
1565 execle(killpgrp_cmd, killpgrp_cmd, config, (char *)0,
1567 dbprintf(_("cannot execute %s: %s\n"),
1568 killpgrp_cmd, strerror(errno));
1572 case 0: /* child process */
1581 if (killctl[0] != -1)
1583 if (killctl[1] != -1)
1589 if (strcmp(fstype, "xfs") == 0)
1594 execle(cmd, "rundump", config, "xfsdump", "-F", "-J", "-l",
1595 level_str, "-", device, (char *)0, safe_env());
1597 execle(cmd, "xfsdump", "-F", "-J", "-l",
1598 level_str, "-", device, (char *)0, safe_env());
1603 if (strcmp(fstype, "vxfs") == 0)
1608 execle(cmd, "rundump", config, "vxdump", dumpkeys, "1048576",
1609 "-", device, (char *)0, safe_env());
1611 execle(cmd, "vxdump", dumpkeys, "1048576", "-",
1612 device, (char *)0, safe_env());
1617 if (strcmp(fstype, "advfs") == 0)
1622 execle(cmd, "rundump", config, "vdump", dumpkeys, "60", "-",
1623 device, (char *)0, safe_env());
1625 execle(cmd, "vdump", dumpkeys, "60", "-",
1626 device, (char *)0, safe_env());
1632 execle(cmd, "rundump", config, "backup", dumpkeys, "-",
1633 device, (char *)0, safe_env());
1635 execle(cmd, "backup", dumpkeys, "-",
1636 device, (char *)0, safe_env());
1639 execle(cmd, "rundump", config, "dump", dumpkeys,
1640 #ifdef HAVE_HONOR_NODUMP
1643 "1048576", "-", device, (char *)0, safe_env());
1645 execle(cmd, "dump", dumpkeys,
1646 #ifdef HAVE_HONOR_NODUMP
1649 "1048576", "-", device, (char *)0, safe_env());
1654 error(_("exec %s failed or no dump program available: %s"),
1655 cmd, strerror(errno));
1661 amfree(rundump_cmd);
1664 if (killctl[0] != -1)
1666 dumpout = fdopen(pipefd[0],"r");
1668 error(_("Can't fdopen: %s"), strerror(errno));
1672 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1673 if (line[0] == '\0')
1675 dbprintf("%s\n", line);
1676 size = handle_dumpline(line);
1677 if(size > (off_t)-1) {
1679 while ((line = agets(dumpout)) != NULL) {
1680 if (line[0] != '\0')
1685 dbprintf("%s\n", line);
1692 dbprintf(".....\n");
1693 dbprintf(_("estimate time for %s level %d: %s\n"),
1696 walltime_str(timessub(curclock(), start_time)));
1697 if(size == (off_t)-1) {
1698 *errmsg = vstrallocf(_("no size line match in %s%s output"),
1700 dbprintf(_("%s for %s\n"),
1703 dbprintf(".....\n");
1704 dbprintf(_("Run %s%s manually to check for errors\n"),
1706 } else if(size == (off_t)0 && level == 0) {
1707 dbprintf(_("possible %s%s problem -- is \"%s\" really empty?\n"),
1708 cmd, name, dle->disk);
1709 dbprintf(".....\n");
1711 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1717 if (killctl[1] != -1) {
1718 dbprintf(_("asking killpgrp to terminate\n"));
1720 for(s = 5; s > 0; --s) {
1722 if (waitpid(dumppid, NULL, WNOHANG) != -1)
1728 * First, try to kill the dump process nicely. If it ignores us
1729 * for several seconds, hit it harder.
1731 dbprintf(_("sending SIGTERM to process group %ld\n"), (long)dumppid);
1732 if (kill(-dumppid, SIGTERM) == -1) {
1733 dbprintf(_("kill failed: %s\n"), strerror(errno));
1735 /* Now check whether it dies */
1736 for(s = 5; s > 0; --s) {
1738 if (waitpid(dumppid, NULL, WNOHANG) != -1)
1742 dbprintf(_("sending SIGKILL to process group %ld\n"), (long)dumppid);
1743 if (kill(-dumppid, SIGKILL) == -1) {
1744 dbprintf(_("kill failed: %s\n"), strerror(errno));
1746 for(s = 5; s > 0; --s) {
1748 if (waitpid(dumppid, NULL, WNOHANG) != -1)
1752 dbprintf(_("waiting for %s%s \"%s\" child\n"), cmd, name, qdisk);
1753 waitpid(dumppid, &wait_status, 0);
1754 if (WIFSIGNALED(wait_status)) {
1755 *errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
1756 cmd, WTERMSIG(wait_status), dbfn());
1757 } else if (WIFEXITED(wait_status)) {
1758 if (WEXITSTATUS(wait_status) != 0) {
1759 *errmsg = vstrallocf(_("%s exited with status %d: see %s"),
1760 cmd, WEXITSTATUS(wait_status), dbfn());
1765 *errmsg = vstrallocf(_("%s got bad exit: see %s"),
1768 dbprintf(_("after %s%s %s wait\n"), cmd, name, qdisk);
1793 int pipefd = -1, nullfd = -1, passwdfd = -1;
1797 char *tarkeys, *sharename, *user_and_password = NULL, *domain = NULL;
1798 char *share = NULL, *subdir = NULL;
1805 char *error_pn = NULL;
1806 char *qdisk = quote_string(dle->disk);
1807 amwait_t wait_status;
1809 error_pn = stralloc2(get_pname(), "-smbclient");
1812 return -2; /* planner will not even consider this level */
1814 parsesharename(dle->device, &share, &subdir);
1818 set_pname(error_pn);
1820 error(_("cannot parse disk entry %s for share/subdir"), qdisk);
1823 if ((subdir) && (SAMBA_VERSION < 2)) {
1826 set_pname(error_pn);
1828 error(_("subdirectory specified for share %s but samba not v2 or better"), qdisk);
1831 if ((user_and_password = findpass(share, &domain)) == NULL) {
1834 memset(domain, '\0', strlen(domain));
1837 set_pname(error_pn);
1839 error(_("cannot find password for %s"), dle->disk);
1842 lpass = strlen(user_and_password);
1843 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
1844 memset(user_and_password, '\0', (size_t)lpass);
1845 amfree(user_and_password);
1847 memset(domain, '\0', strlen(domain));
1850 set_pname(error_pn);
1852 error(_("password field not \'user%%pass\' for %s"), dle->disk);
1856 pwtext_len = strlen(pwtext);
1857 if ((sharename = makesharename(share, 0)) == NULL) {
1858 memset(user_and_password, '\0', (size_t)lpass);
1859 amfree(user_and_password);
1861 memset(domain, '\0', strlen(domain));
1864 set_pname(error_pn);
1866 error(_("cannot make share name of %s"), share);
1869 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
1870 memset(user_and_password, '\0', (size_t)lpass);
1871 amfree(user_and_password);
1873 memset(domain, '\0', strlen(domain));
1876 set_pname(error_pn);
1879 error(_("could not open /dev/null: %s\n"),
1884 #if SAMBA_VERSION >= 2
1886 tarkeys = "archive 0;recurse;du";
1888 tarkeys = "archive 1;recurse;du";
1891 tarkeys = "archive 0;recurse;dir";
1893 tarkeys = "archive 1;recurse;dir";
1896 start_time = curclock();
1898 if (pwtext_len > 0) {
1899 pw_fd_env = "PASSWD_FD";
1901 pw_fd_env = "dummy_PASSWD_FD";
1903 dumppid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE, 0,
1904 &nullfd, &nullfd, &pipefd,
1905 pw_fd_env, &passwdfd,
1908 "-d", SAMBA_DEBUG_LEVEL,
1909 *user_and_password ? "-U" : skip_argument,
1910 *user_and_password ? user_and_password : skip_argument,
1912 domain ? "-W" : skip_argument,
1913 domain ? domain : skip_argument,
1914 #if SAMBA_VERSION >= 2
1915 subdir ? "-D" : skip_argument,
1916 subdir ? subdir : skip_argument,
1921 memset(domain, '\0', strlen(domain));
1925 if(pwtext_len > 0 && full_write(passwdfd, pwtext, pwtext_len) < pwtext_len) {
1926 int save_errno = errno;
1928 memset(user_and_password, '\0', (size_t)lpass);
1929 amfree(user_and_password);
1931 set_pname(error_pn);
1933 error(_("password write failed: %s"), strerror(save_errno));
1936 memset(user_and_password, '\0', (size_t)lpass);
1937 amfree(user_and_password);
1943 dumpout = fdopen(pipefd,"r");
1945 error(_("Can't fdopen: %s"), strerror(errno));
1949 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1950 if (line[0] == '\0')
1952 dbprintf("%s\n", line);
1953 size = handle_dumpline(line);
1956 while ((line = agets(dumpout)) != NULL) {
1957 if (line[0] != '\0')
1962 dbprintf("%s\n", line);
1969 dbprintf(".....\n");
1970 dbprintf(_("estimate time for %s level %d: %s\n"),
1973 walltime_str(timessub(curclock(), start_time)));
1974 if(size == (off_t)-1) {
1975 *errmsg = vstrallocf(_("no size line match in %s output"),
1977 dbprintf(_("%s for %s\n"),
1979 dbprintf(".....\n");
1980 } else if(size == (off_t)0 && level == 0) {
1981 dbprintf(_("possible %s problem -- is \"%s\" really empty?\n"),
1982 SAMBA_CLIENT, dle->disk);
1983 dbprintf(".....\n");
1985 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1990 kill(-dumppid, SIGTERM);
1992 dbprintf(_("waiting for %s \"%s\" child\n"), SAMBA_CLIENT, qdisk);
1993 waitpid(dumppid, &wait_status, 0);
1994 if (WIFSIGNALED(wait_status)) {
1995 *errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
1996 SAMBA_CLIENT, WTERMSIG(wait_status), dbfn());
1997 } else if (WIFEXITED(wait_status)) {
1998 if (WEXITSTATUS(wait_status) != 0) {
1999 *errmsg = vstrallocf(_("%s exited with status %d: see %s"),
2000 SAMBA_CLIENT, WEXITSTATUS(wait_status),
2006 *errmsg = vstrallocf(_("%s got bad exit: see %s"),
2007 SAMBA_CLIENT, dbfn());
2009 dbprintf(_("after %s %s wait\n"), SAMBA_CLIENT, qdisk);
2029 int pipefd = -1, nullfd = -1;
2031 off_t size = (off_t)-1;
2032 FILE *dumpout = NULL;
2033 char *incrname = NULL;
2034 char *basename = NULL;
2035 char *dirname = NULL;
2036 char *inputname = NULL;
2041 char *command = NULL;
2042 char dumptimestr[80];
2046 GPtrArray *argv_ptr = g_ptr_array_new();
2047 char *file_exclude = NULL;
2048 char *file_include = NULL;
2053 char *qdisk = quote_string(dle->disk);
2054 char *gnutar_list_dir;
2055 amwait_t wait_status;
2056 char tmppath[PATH_MAX];
2059 return -2; /* planner will not even consider this level */
2061 if(dle->exclude_file) nb_exclude += dle->exclude_file->nb_element;
2062 if(dle->exclude_list) nb_exclude += dle->exclude_list->nb_element;
2063 if(dle->include_file) nb_include += dle->include_file->nb_element;
2064 if(dle->include_list) nb_include += dle->include_list->nb_element;
2066 if(nb_exclude > 0) file_exclude = build_exclude(dle, 0);
2067 if(nb_include > 0) file_include = build_include(dle, 0);
2069 gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
2070 if (strlen(gnutar_list_dir) == 0)
2071 gnutar_list_dir = NULL;
2072 if (gnutar_list_dir) {
2073 char number[NUM_STR_SIZE];
2075 char *sdisk = sanitise_filename(dle->disk);
2077 basename = vstralloc(gnutar_list_dir,
2079 g_options->hostname,
2084 g_snprintf(number, SIZEOF(number), "%d", level);
2085 incrname = vstralloc(basename, "_", number, ".new", NULL);
2089 * Open the listed incremental file from the previous level. Search
2090 * backward until one is found. If none are found (which will also
2091 * be true for a level 0), arrange to read from /dev/null.
2095 while (infd == -1) {
2096 if (--baselevel >= 0) {
2097 g_snprintf(number, SIZEOF(number), "%d", baselevel);
2098 inputname = newvstralloc(inputname,
2099 basename, "_", number, NULL);
2101 inputname = newstralloc(inputname, "/dev/null");
2103 if ((infd = open(inputname, O_RDONLY)) == -1) {
2105 *errmsg = vstrallocf(_("gnutar: error opening %s: %s"),
2106 inputname, strerror(errno));
2107 dbprintf("%s\n", *errmsg);
2108 if (baselevel < 0) {
2116 * Copy the previous listed incremental file to the new one.
2118 if ((outfd = open(incrname, O_WRONLY|O_CREAT, 0600)) == -1) {
2119 *errmsg = vstrallocf(_("opening %s: %s"),
2120 incrname, strerror(errno));
2121 dbprintf("%s\n", *errmsg);
2125 while ((nb = read(infd, &buf, SIZEOF(buf))) > 0) {
2126 if (full_write(outfd, &buf, (size_t)nb) < (size_t)nb) {
2127 *errmsg = vstrallocf(_("writing to %s: %s"),
2128 incrname, strerror(errno));
2129 dbprintf("%s\n", *errmsg);
2135 *errmsg = vstrallocf(_("reading from %s: %s"),
2136 inputname, strerror(errno));
2137 dbprintf("%s\n", *errmsg);
2141 if (close(infd) != 0) {
2142 *errmsg = vstrallocf(_("closing %s: %s"),
2143 inputname, strerror(errno));
2144 dbprintf("%s\n", *errmsg);
2147 if (close(outfd) != 0) {
2148 *errmsg = vstrallocf(_("closing %s: %s"),
2149 incrname, strerror(errno));
2150 dbprintf("%s\n", *errmsg);
2158 gmtm = gmtime(&dumpsince);
2159 g_snprintf(dumptimestr, SIZEOF(dumptimestr),
2160 "%04d-%02d-%02d %2d:%02d:%02d GMT",
2161 gmtm->tm_year + 1900, gmtm->tm_mon+1, gmtm->tm_mday,
2162 gmtm->tm_hour, gmtm->tm_min, gmtm->tm_sec);
2164 dirname = amname_to_dirname(dle->device);
2166 cmd = vstralloc(amlibexecdir, "/", "runtar", NULL);
2167 g_ptr_array_add(argv_ptr, stralloc("runtar"));
2168 if (g_options->config)
2169 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
2171 g_ptr_array_add(argv_ptr, stralloc("NOCONFIG"));
2174 g_ptr_array_add(argv_ptr, stralloc(GNUTAR));
2176 g_ptr_array_add(argv_ptr, stralloc("tar"));
2178 g_ptr_array_add(argv_ptr, stralloc("--create"));
2179 g_ptr_array_add(argv_ptr, stralloc("--file"));
2180 g_ptr_array_add(argv_ptr, stralloc("/dev/null"));
2181 /* use --numeric-owner for estimates, to reduce the number of user/group
2182 * lookups required */
2183 g_ptr_array_add(argv_ptr, stralloc("--numeric-owner"));
2184 g_ptr_array_add(argv_ptr, stralloc("--directory"));
2185 canonicalize_pathname(dirname, tmppath);
2186 g_ptr_array_add(argv_ptr, stralloc(tmppath));
2187 g_ptr_array_add(argv_ptr, stralloc("--one-file-system"));
2188 if (gnutar_list_dir) {
2189 g_ptr_array_add(argv_ptr, stralloc("--listed-incremental"));
2190 g_ptr_array_add(argv_ptr, stralloc(incrname));
2192 g_ptr_array_add(argv_ptr, stralloc("--incremental"));
2193 g_ptr_array_add(argv_ptr, stralloc("--newer"));
2194 g_ptr_array_add(argv_ptr, stralloc(dumptimestr));
2196 #ifdef ENABLE_GNUTAR_ATIME_PRESERVE
2197 /* --atime-preserve causes gnutar to call
2198 * utime() after reading files in order to
2199 * adjust their atime. However, utime()
2200 * updates the file's ctime, so incremental
2201 * dumps will think the file has changed. */
2202 g_ptr_array_add(argv_ptr, stralloc("--atime-preserve"));
2204 g_ptr_array_add(argv_ptr, stralloc("--sparse"));
2205 g_ptr_array_add(argv_ptr, stralloc("--ignore-failed-read"));
2206 g_ptr_array_add(argv_ptr, stralloc("--totals"));
2209 g_ptr_array_add(argv_ptr, stralloc("--exclude-from"));
2210 g_ptr_array_add(argv_ptr, stralloc(file_exclude));
2214 g_ptr_array_add(argv_ptr, stralloc("--files-from"));
2215 g_ptr_array_add(argv_ptr, stralloc(file_include));
2218 g_ptr_array_add(argv_ptr, stralloc("."));
2220 g_ptr_array_add(argv_ptr, NULL);
2222 start_time = curclock();
2224 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
2225 *errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
2227 dbprintf("%s\n", *errmsg);
2231 command = (char *)g_ptr_array_index(argv_ptr, 0);
2232 dumppid = pipespawnv(cmd, STDERR_PIPE, 0,
2233 &nullfd, &nullfd, &pipefd, (char **)argv_ptr->pdata);
2235 dumpout = fdopen(pipefd,"r");
2237 error(_("Can't fdopen: %s"), strerror(errno));
2241 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
2242 if (line[0] == '\0')
2244 dbprintf("%s\n", line);
2245 size = handle_dumpline(line);
2246 if(size > (off_t)-1) {
2248 while ((line = agets(dumpout)) != NULL) {
2249 if (line[0] != '\0') {
2255 dbprintf("%s\n", line);
2263 dbprintf(".....\n");
2264 dbprintf(_("estimate time for %s level %d: %s\n"),
2267 walltime_str(timessub(curclock(), start_time)));
2268 if(size == (off_t)-1) {
2269 *errmsg = vstrallocf(_("no size line match in %s output"),
2271 dbprintf(_("%s for %s\n"), *errmsg, qdisk);
2272 dbprintf(".....\n");
2273 } else if(size == (off_t)0 && level == 0) {
2274 dbprintf(_("possible %s problem -- is \"%s\" really empty?\n"),
2275 command, dle->disk);
2276 dbprintf(".....\n");
2278 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
2283 kill(-dumppid, SIGTERM);
2285 dbprintf(_("waiting for %s \"%s\" child\n"),
2287 waitpid(dumppid, &wait_status, 0);
2288 if (WIFSIGNALED(wait_status)) {
2289 *errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
2290 cmd, WTERMSIG(wait_status), dbfn());
2291 } else if (WIFEXITED(wait_status)) {
2292 if (WEXITSTATUS(wait_status) != 0) {
2293 *errmsg = vstrallocf(_("%s exited with status %d: see %s"),
2294 cmd, WEXITSTATUS(wait_status), dbfn());
2299 *errmsg = vstrallocf(_("%s got bad exit: see %s"),
2302 dbprintf(_("after %s %s wait\n"), command, qdisk);
2313 g_ptr_array_free_full(argv_ptr);
2316 amfree(file_exclude);
2317 amfree(file_include);
2329 getsize_application_api(
2330 disk_estimates_t *est,
2333 backup_support_option_t *bsu)
2335 dle_t *dle = est->dle;
2336 int pipeinfd[2], pipeoutfd[2], pipeerrfd[2];
2338 off_t size = (off_t)-1;
2346 GPtrArray *argv_ptr = g_ptr_array_new();
2347 char *newoptstr = NULL;
2350 char *qdisk = quote_string(dle->disk);
2351 char *qamdevice = quote_string(dle->device);
2352 amwait_t wait_status;
2353 char levelstr[NUM_STR_SIZE];
2356 char *errmsg = NULL;
2357 estimate_t estimate;
2360 cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
2362 g_ptr_array_add(argv_ptr, stralloc(dle->program));
2363 g_ptr_array_add(argv_ptr, stralloc("estimate"));
2364 if (bsu->message_line == 1) {
2365 g_ptr_array_add(argv_ptr, stralloc("--message"));
2366 g_ptr_array_add(argv_ptr, stralloc("line"));
2368 if (g_options->config && bsu->config == 1) {
2369 g_ptr_array_add(argv_ptr, stralloc("--config"));
2370 g_ptr_array_add(argv_ptr, stralloc(g_options->config));
2372 if (g_options->hostname && bsu->host == 1) {
2373 g_ptr_array_add(argv_ptr, stralloc("--host"));
2374 g_ptr_array_add(argv_ptr, stralloc(g_options->hostname));
2376 g_ptr_array_add(argv_ptr, stralloc("--device"));
2377 g_ptr_array_add(argv_ptr, stralloc(dle->device));
2378 if (dle->disk && bsu->disk == 1) {
2379 g_ptr_array_add(argv_ptr, stralloc("--disk"));
2380 g_ptr_array_add(argv_ptr, stralloc(dle->disk));
2382 for (j=0; j < nb_level; j++) {
2383 g_ptr_array_add(argv_ptr, stralloc("--level"));
2384 g_snprintf(levelstr,SIZEOF(levelstr),"%d", levels[j]);
2385 g_ptr_array_add(argv_ptr, stralloc(levelstr));
2387 /* find the first in ES_CLIENT and ES_CALCSIZE */
2388 estimate = ES_CLIENT;
2389 for (el = dle->estimatelist; el != NULL; el = el->next) {
2390 estimate = (estimate_t)GPOINTER_TO_INT(el->data);
2391 if ((estimate == ES_CLIENT && bsu->client_estimate) ||
2392 (estimate == ES_CALCSIZE && bsu->calcsize))
2394 estimate = ES_CLIENT;
2396 if (estimate == ES_CALCSIZE && bsu->calcsize) {
2397 g_ptr_array_add(argv_ptr, stralloc("--calcsize"));
2400 application_property_add_to_argv(argv_ptr, dle, bsu, g_options->features);
2402 for (scriptlist = dle->scriptlist; scriptlist != NULL;
2403 scriptlist = scriptlist->next) {
2404 script = (script_t *)scriptlist->data;
2405 if (script->result && script->result->proplist) {
2406 property_add_to_argv(argv_ptr, script->result->proplist);
2410 g_ptr_array_add(argv_ptr, NULL);
2412 cmdline = stralloc(cmd);
2413 for(i = 1; i < argv_ptr->len-1; i++)
2414 cmdline = vstrextend(&cmdline, " ",
2415 (char *)g_ptr_array_index(argv_ptr, i), NULL);
2416 dbprintf("running: \"%s\"\n", cmdline);
2419 if (pipe(pipeerrfd) < 0) {
2420 errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2425 if (pipe(pipeinfd) < 0) {
2426 errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2431 if (pipe(pipeoutfd) < 0) {
2432 errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2437 start_time = curclock();
2439 switch(dumppid = fork()) {
2446 dup2(pipeinfd[0], 0);
2447 dup2(pipeoutfd[1], 1);
2448 dup2(pipeerrfd[1], 2);
2449 aclose(pipeinfd[1]);
2450 aclose(pipeoutfd[0]);
2451 aclose(pipeerrfd[0]);
2454 execve(cmd, (char **)argv_ptr->pdata, safe_env());
2455 error(_("exec %s failed: %s"), cmd, strerror(errno));
2460 aclose(pipeinfd[0]);
2461 aclose(pipeoutfd[1]);
2462 aclose(pipeerrfd[1]);
2463 aclose(pipeinfd[1]);
2465 dumpout = fdopen(pipeoutfd[0],"r");
2467 error(_("Can't fdopen: %s"), strerror(errno));
2471 for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
2472 long long size1_ = (long long)0;
2473 long long size2_ = (long long)0;
2475 if (line[0] == '\0')
2477 dbprintf("%s\n", line);
2478 if (strncmp(line,"ERROR ", 6) == 0) {
2479 char *errmsg, *qerrmsg;
2481 errmsg = stralloc(line+6);
2482 qerrmsg = quote_string(errmsg);
2483 dbprintf(_("errmsg is %s\n"), errmsg);
2484 g_printf(_("%s %d ERROR %s\n"), est->qamname, levels[0], qerrmsg);
2488 i = sscanf(line, "%d %lld %lld", &level, &size1_, &size2_);
2490 i = sscanf(line, "%lld %lld", &size1_, &size2_);
2493 char *errmsg, *qerrmsg;
2495 errmsg = vstrallocf(_("bad line %s"), line);
2496 qerrmsg = quote_string(errmsg);
2497 dbprintf(_("errmsg is %s\n"), errmsg);
2498 g_printf(_("%s %d ERROR %s\n"), est->qamname, levels[0], qerrmsg);
2503 size1 = (off_t)size1_;
2504 size2 = (off_t)size2_;
2505 if (size1 <= 0 || size2 <=0)
2507 else if (size1 * size2 > 0)
2508 size = size1 * size2;
2509 dbprintf(_("estimate size for %s level %d: %lld KB\n"),
2513 g_printf("%s %d SIZE %lld\n", est->qamname, level, (long long)size);
2517 dumperr = fdopen(pipeerrfd[0],"r");
2519 error(_("Can't fdopen: %s"), strerror(errno));
2523 while ((line = agets(dumperr)) != NULL) {
2524 if (strlen(line) > 0) {
2525 char *err = g_strdup_printf(_("Application '%s': %s"),
2526 dle->program, line);
2527 char *qerr = quote_string(err);
2528 for (j=0; j < nb_level; j++) {
2529 fprintf(stdout, "%s %d ERROR %s\n",
2530 est->qamname, levels[j], qerr);
2532 dbprintf("ERROR %s", qerr);
2539 dbprintf(".....\n");
2540 if (nb_level == 1) {
2541 dbprintf(_("estimate time for %s level %d: %s\n"), qamdevice,
2542 levels[0], walltime_str(timessub(curclock(), start_time)));
2544 dbprintf(_("estimate time for %s all level: %s\n"), qamdevice,
2545 walltime_str(timessub(curclock(), start_time)));
2548 kill(-dumppid, SIGTERM);
2550 dbprintf(_("waiting for %s \"%s\" child\n"), cmd, qdisk);
2551 waitpid(dumppid, &wait_status, 0);
2552 if (WIFSIGNALED(wait_status)) {
2553 errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
2554 cmd, WTERMSIG(wait_status), dbfn());
2555 } else if (WIFEXITED(wait_status)) {
2556 if (WEXITSTATUS(wait_status) != 0) {
2557 errmsg = vstrallocf(_("%s exited with status %d: see %s"), cmd,
2558 WEXITSTATUS(wait_status), dbfn());
2563 errmsg = vstrallocf(_("%s got bad exit: see %s"),
2566 dbprintf(_("after %s %s wait\n"), cmd, qdisk);
2574 g_ptr_array_free_full(argv_ptr);
2579 char *qerrmsg = quote_string(errmsg);
2580 dbprintf(_("errmsg is %s\n"), errmsg);
2581 for (j=0; j < nb_level; j++) {
2582 g_printf(_("%s %d ERROR %s\n"), est->qamname, levels[j], qerrmsg);
2592 * Returns the value of the first integer in a string.
2604 while(ch && !isdigit(ch)) ch = *str++;
2606 while(isdigit(ch) || (ch == '.')) ch = *str++;
2615 * Checks the dump output line against the error and size regex tables.
2625 /* check for size match */
2627 for(rp = re_size; rp->regex != NULL; rp++) {
2628 if(match(rp->regex, str)) {
2629 size = ((first_num(str)*rp->scale+1023.0)/1024.0);
2631 size = 1.0; /* found on NeXT -- sigh */