3c2815a89100fce4e47fde302858b0f05b9c4374
[debian/amanda] / client-src / sendsize.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /* 
27  * $Id: sendsize.c 10421 2008-03-06 18:48:30Z martineau $
28  *
29  * send estimated backup sizes using dump
30  */
31
32 #include "amanda.h"
33 #include "match.h"
34 #include "pipespawn.h"
35 #include "amfeatures.h"
36 #include "amandates.h"
37 #include "clock.h"
38 #include "util.h"
39 #include "getfsent.h"
40 #include "client_util.h"
41 #include "conffile.h"
42
43 #ifdef SAMBA_CLIENT
44 #include "findpass.h"
45 #endif
46
47 #define sendsize_debug(i, ...) do {     \
48         if ((i) <= debug_sebdsize) {    \
49             dbprintf(__VA_ARGS__);      \
50         }                               \
51 } while (0)
52
53 #ifdef HAVE_SETPGID
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)); \
58 } while(0)
59
60 #else /* () line 0 */
61 #if defined(SETPGRP_VOID)
62 #  define SETPGRP       setpgrp()
63 #  define SETPGRP_FAILED() do {                                         \
64     dbprintf(_("setpgrp() failed: %s\n"), strerror(errno));             \
65 } while(0)
66
67 #else
68 #  define SETPGRP       setpgrp(0, getpid())
69 #  define SETPGRP_FAILED() do {                                         \
70     dbprintf(_("setpgrp(0,%ld) failed: %s\n"),                          \
71               (long)getpid(), strerror(errno));                 \
72 } while(0)
73
74 #endif
75 #endif
76
77 typedef struct level_estimates_s {
78     time_t dumpsince;
79     int estsize;
80     int needestimate;
81     int server;         /* server can do estimate */
82 } level_estimate_t;
83
84 typedef struct disk_estimates_s {
85     struct disk_estimates_s *next;
86     char *qamname;
87     char *qamdevice;
88     char *dirname;
89     char *qdirname;
90     pid_t child;
91     int done;
92     dle_t *dle;
93     level_estimate_t est[DUMP_LEVELS];
94 } disk_estimates_t;
95
96 disk_estimates_t *est_list;
97
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;
102
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 *);
114
115 int
116 main(
117     int         argc,
118     char **     argv)
119 {
120     int level;
121     char *dumpdate;
122     disk_estimates_t *est;
123     disk_estimates_t *est1;
124     disk_estimates_t *est_prev;
125     char *line = NULL;
126     char *s, *fp;
127     int ch;
128     char *err_extra = NULL;
129     int done;
130     int need_wait;
131     int dumpsrunning;
132     char *qdisk = NULL;
133     char *qlist = NULL;
134     char *qamdevice = NULL;
135     dle_t *dle;
136     GSList *errlist;
137     level_t *alevel;
138
139     (void)argc; /* Quiet unused parameter warning */
140     (void)argv; /* Quiet unused parameter warning */
141
142     /* initialize */
143
144     /*
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.
149      */  
150     setlocale(LC_MESSAGES, "C");
151     textdomain("amanda"); 
152
153     safe_fd(-1, 0);
154     openbsd_fd_inform();
155     safe_cd();
156
157     set_pname("sendsize");
158
159     /* Don't die when child closes pipe */
160     signal(SIGPIPE, SIG_IGN);
161
162     add_amanda_log_handler(amanda_log_stderr);
163     add_amanda_log_handler(amanda_log_syslog);
164     dbopen(DBG_SUBDIR_CLIENT);
165     startclock();
166     dbprintf(_("version %s\n"), VERSION);
167
168     our_features = am_init_feature_set();
169     our_feature_string = am_feature_to_string(our_features);
170
171     config_init(CONFIG_INIT_CLIENT, NULL);
172     /* (check for config errors comes later) */
173
174     check_running_as(RUNNING_AS_CLIENT_LOGIN);
175
176     /* handle all service requests */
177
178     for(; (line = agets(stdin)) != NULL; free(line)) {
179         if (line[0] == '\0')
180             continue;
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';
187             }
188
189             g_printf("OPTIONS ");
190             if(am_has_feature(g_options->features, fe_rep_options_features)) {
191                 g_printf("features=%s;", our_feature_string);
192             }
193             if(am_has_feature(g_options->features, fe_rep_options_maxdumps)) {
194                 g_printf("maxdumps=%d;", g_options->maxdumps);
195             }
196             if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
197                 g_printf("hostname=%s;", g_options->hostname);
198             }
199             g_printf("\n");
200             fflush(stdout);
201
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,
205                             g_options->config);
206
207                 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
208             }
209
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);
214                 dbclose();
215                 return 1;
216             }
217
218             if (am_has_feature(g_options->features, fe_req_xml)) {
219                 break;
220             }
221             continue;
222         }
223
224         dle = alloc_dle();
225         s = line;
226         ch = *s++;
227
228         skip_whitespace(s, ch);                 /* find the program name */
229         if(ch == '\0') {
230             err_extra = stralloc(_("no program name"));
231             goto err;                           /* no program name */
232         }
233         dle->program = s - 1;
234         skip_non_whitespace(s, ch);
235         s[-1] = '\0';
236
237         dle->program_is_application_api=0;
238         if(strncmp_const(dle->program, "CALCSIZE") == 0) {
239             skip_whitespace(s, ch);             /* find the program name */
240             if(ch == '\0') {
241                 err_extra = stralloc(_("no program name"));
242                 goto err;
243             }
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);
248             s[-1] = '\0';
249             if (strcmp(dle->program,"APPLICATION") == 0) {
250                 dle->program_is_application_api=1;
251                 skip_whitespace(s, ch);         /* find dumper name */
252                 if (ch == '\0') {
253                     goto err;                   /* no program */
254                 }
255                 dle->program = s - 1;
256                 skip_non_whitespace(s, ch);
257                 s[-1] = '\0';
258             }
259         }
260         else {
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 */
266                 if (ch == '\0') {
267                     goto err;                   /* no program */
268                 }
269                 dle->program = s - 1;
270                 skip_non_whitespace(s, ch);
271                 s[-1] = '\0';
272             }
273         }
274         dle->program = stralloc(dle->program);
275
276         skip_whitespace(s, ch);                 /* find the disk name */
277         if(ch == '\0') {
278             err_extra = stralloc(_("no disk name"));
279             goto err;                           /* no disk name */
280         }
281
282         if (qdisk != NULL)
283             amfree(qdisk);
284
285         fp = s - 1;
286         skip_quoted_string(s, ch);
287         s[-1] = '\0';                           /* terminate the disk name */
288         qdisk = stralloc(fp);
289         dle->disk = unquote_string(qdisk);
290
291         skip_whitespace(s, ch);                 /* find the device or level */
292         if (ch == '\0') {
293             err_extra = stralloc(_("bad level"));
294             goto err;
295         }
296         if(!isdigit((int)s[-1])) {
297             fp = s - 1;
298             skip_quoted_string(s, ch);
299             s[-1] = '\0';
300             qamdevice = stralloc(fp);
301             dle->device = unquote_string(qamdevice);
302             skip_whitespace(s, ch);             /* find level number */
303         }
304         else {
305             dle->device = stralloc(dle->disk);
306             qamdevice = stralloc(qdisk);
307         }
308
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 */
313         }
314         if (level < 0 || level >= DUMP_LEVELS) {
315             err_extra = stralloc(_("bad level"));
316             goto err;
317         }
318         skip_integer(s, ch);
319         alevel = g_new0(level_t, 1);
320         alevel->level = level;
321         dle->levellist = g_slist_append(dle->levellist, alevel);
322
323         skip_whitespace(s, ch);                 /* find the dump date */
324         if(ch == '\0') {
325             err_extra = stralloc(_("no dumpdate"));
326             goto err;                           /* no dumpdate */
327         }
328         dumpdate = s - 1;
329         skip_non_whitespace(s, ch);
330         s[-1] = '\0';
331         (void)dumpdate;                         /* XXX: Set but not used */
332
333         dle->spindle = 0;                       /* default spindle */
334
335         skip_whitespace(s, ch);                 /* find the spindle */
336         if(ch != '\0') {
337             if(sscanf(s - 1, "%d", &dle->spindle) != 1) {
338                 err_extra = stralloc(_("bad spindle"));
339                 goto err;                       /* bad spindle */
340             }
341             skip_integer(s, ch);
342
343             skip_whitespace(s, ch);             /* find the parameters */
344             if(ch != '\0') {
345                 if(strncmp_const(s-1, "OPTIONS |;") == 0) {
346                     parse_options(s + 8,
347                                   dle,
348                                   g_options->features,
349                                   0);
350                 }
351                 else {
352                     while (ch != '\0') {
353                         if(strncmp_const(s-1, "exclude-file=") == 0) {
354                             qlist = unquote_string(s+12);
355                             dle->exclude_file =
356                                 append_sl(dle->exclude_file, qlist);
357                             amfree(qlist);
358                         } else if(strncmp_const(s-1, "exclude-list=") == 0) {
359                             qlist = unquote_string(s+12);
360                             dle->exclude_list =
361                                 append_sl(dle->exclude_list, qlist);
362                             amfree(qlist);
363                         } else if(strncmp_const(s-1, "include-file=") == 0) {
364                             qlist = unquote_string(s+12);
365                             dle->include_file =
366                                 append_sl(dle->include_file, qlist);
367                             amfree(qlist);
368                         } else if(strncmp_const(s-1, "include-list=") == 0) {
369                             qlist = unquote_string(s+12);
370                             dle->include_list =
371                                 append_sl(dle->include_list, qlist);
372                             amfree(qlist);
373                         } else {
374                             err_extra = vstrallocf(_("Invalid parameter (%s)"), s-1);
375                             goto err;           /* should have gotten to end */
376                         }
377                         skip_quoted_string(s, ch);
378                         skip_whitespace(s, ch); /* find the inclusion list */
379                         amfree(qlist);
380                     }
381                 }
382             }
383         }
384
385         /*@ignore@*/
386         dle_add_diskest(dle);
387         /*@end@*/
388     }
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"));
392         /*NOTREACHED*/
393     }
394     amfree(line);
395
396     if (am_has_feature(g_options->features, fe_req_xml)) {
397         char    *errmsg = NULL;
398         dle_t   *dles, *dle;
399
400         dles = amxml_parse_node_FILE(stdin, &errmsg);
401         if (errmsg) {
402             err_extra = errmsg;
403             goto err;
404         }
405         for (dle = dles; dle != NULL; dle = dle->next) {
406             dle_add_diskest(dle);
407         }
408     }
409
410     if (amandates_started) {
411         finish_amandates();
412         free_amandates();
413         amandates_started = FALSE;
414     }
415
416     for(est = est_list; est != NULL; est = est->next) {
417         run_client_scripts(EXECUTE_ON_PRE_HOST_ESTIMATE, g_options, est->dle,
418                            stdout);
419     }
420
421     dumpsrunning = 0;
422     need_wait = 0;
423     done = 0;
424     while(! done) {
425         done = 1;
426         /*
427          * See if we need to wait for a child before we can do anything
428          * else in this pass.
429          */
430         if(need_wait) {
431             pid_t child_pid;
432             amwait_t child_status;
433
434             need_wait = 0;
435             dbprintf(_("waiting for any estimate child: %d running\n"),
436                       dumpsrunning);
437             child_pid = wait(&child_status);
438             if(child_pid == -1) {
439                 error(_("wait failed: %s"), strerror(errno));
440                 /*NOTREACHED*/
441             }
442
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);
448                 amfree(child_name);
449             }
450
451             /*
452              * Find the child and mark it done.
453              */
454             for(est = est_list; est != NULL; est = est->next) {
455                 if(est->child == child_pid) {
456                     break;
457                 }
458             }
459             if(est == NULL) {
460                 dbprintf(_("unexpected child %ld\n"), (long)child_pid);
461             } else {
462                 est->done = 1;
463                 est->child = 0;
464                 dumpsrunning--;
465                 run_client_scripts(EXECUTE_ON_POST_DLE_ESTIMATE, g_options,
466                                    est->dle, stdout);
467             }
468         }
469         /*
470          * If we are already running the maximum number of children
471          * go back and wait until one of them finishes.
472          */
473         if(dumpsrunning >= g_options->maxdumps) {
474             done = 0;
475             need_wait = 1;
476             continue;                           /* have to wait first */
477         }
478         /*
479          * Find a new child to start.
480          */
481         for(est = est_list; est != NULL; est = est->next) {
482             if(est->done == 0) {
483                 done = 0;                       /* more to do */
484             }
485             if(est->child != 0 || est->done) {
486                 continue;                       /* child is running or done */
487             }
488             /*
489              * Make sure there is no spindle conflict.
490              */
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) {
494                         /*
495                          * Ignore anything not yet started, ourself,
496                          * and anything completed.
497                          */
498                         continue;
499                     }
500                     if(est1->dle->spindle == est->dle->spindle) {
501                         break;                  /* oops -- they match */
502                     }
503                 }
504                 if(est1 != NULL) {
505                     continue;                   /* spindle conflict */
506                 }
507             }
508             break;                              /* start this estimate */
509         }
510         if(est == NULL) {
511             if(dumpsrunning > 0) {
512                 need_wait = 1;                  /* nothing to do but wait */
513             }
514         } else {
515             done = 0;
516             run_client_scripts(EXECUTE_ON_PRE_DLE_ESTIMATE, g_options,
517                                est->dle, stdout);
518
519             if((est->child = fork()) == 0) {
520                 calc_estimates(est);            /* child does the estimate */
521                 exit(0);
522             } else if(est->child == -1) {
523                 error(_("calc_estimates fork failed: %s"), strerror(errno));
524                 /*NOTREACHED*/
525             }
526             dumpsrunning++;                     /* parent */
527         }
528     }
529
530     for(est = est_list; est != NULL; est = est->next) {
531         run_client_scripts(EXECUTE_ON_POST_HOST_ESTIMATE, g_options, est->dle,
532                            stdout);
533     }
534
535     est_prev = NULL;
536     for(est = est_list; est != NULL; est = est->next) {
537         free_estimates(est);
538         amfree(est_prev);
539         est_prev = est;
540     }
541     amfree(est_prev);
542     amfree(our_feature_string);
543     am_release_feature_set(our_features);
544     our_features = NULL;
545     free_g_options(g_options);
546
547     dbclose();
548     return 0;
549  err:
550     if (err_extra) {
551         g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET '%s'\n"), err_extra);
552         dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
553         amfree(err_extra);
554     } else {
555         g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET\n"));
556         dbprintf(_("REQ packet is bogus\n"));
557     }
558
559     free_g_options(g_options);
560
561     dbclose();
562     return 1;
563 }
564
565
566 void
567 dle_add_diskest(
568     dle_t    *dle)
569 {
570     disk_estimates_t *newp, *curp;
571     amandates_t *amdp;
572     int dumplev, estlev;
573     time_t dumpdate;
574     levellist_t levellist;
575     char *amandates_file;
576     gboolean need_amandates = FALSE;
577     estimatelist_t el;
578
579     if (dle->levellist == NULL) {
580         g_printf(_("ERROR Missing level in request\n"));
581         return;
582     }
583
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;
589     }
590
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;
596     }
597
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);
609                 amfree(qamname);
610                 amfree(errmsg);
611                 amfree(qerrmsg);
612                 return;
613             }
614             amandates_started = TRUE;
615         }
616     }
617
618     levellist = dle->levellist;
619     while (levellist != NULL) {
620         level_t *alevel = (level_t *)levellist->data;
621         if (alevel->level < 0)
622             alevel->level = 0;
623         if (alevel->level >= DUMP_LEVELS)
624             alevel->level = DUMP_LEVELS - 1;
625         levellist = g_slist_next(levellist);
626     }
627
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);
638             }
639
640             return;
641         }
642     }
643
644     newp = (disk_estimates_t *) alloc(SIZEOF(disk_estimates_t));
645     memset(newp, 0, SIZEOF(*newp));
646     newp->next = est_list;
647     est_list = newp;
648     newp->qamname = quote_string(dle->disk);
649     if (dle->device) {
650         newp->qamdevice = quote_string(dle->device);
651         newp->dirname = amname_to_dirname(dle->device);
652         newp->qdirname = quote_string(newp->dirname);
653     } else {
654         newp->qamdevice = stralloc("");
655         newp->dirname = stralloc("");
656         newp->qdirname = stralloc("");
657     }
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);
664     }
665     newp->dle = dle;
666
667     /* fill in dump-since dates */
668     if (need_amandates) {
669         amdp = amandates_lookup(newp->dle->disk);
670
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;
677             }
678         }
679     } else {
680         /* just zero everything out */
681         for(dumplev = 0; dumplev < DUMP_LEVELS; dumplev++) {
682             newp->est[dumplev].dumpsince = 0;
683         }
684     }
685 }
686
687
688 void
689 free_estimates(
690     disk_estimates_t *  est)
691 {
692     amfree(est->qamname);
693     amfree(est->qamdevice);
694     amfree(est->dirname);
695     amfree(est->qdirname);
696     if(est->dle) {
697 /* free DLE */
698     }
699 }
700
701 /*
702  * ------------------------------------------------------------------------
703  *
704  */
705
706 void
707 calc_estimates(
708     disk_estimates_t *  est)
709 {
710     dbprintf(_("calculating for amname %s, dirname %s, spindle %d %s\n"),
711              est->qamname, est->qdirname, est->dle->spindle, est->dle->program);
712
713     if(est->dle->program_is_application_api ==  1)
714         application_api_calc_estimate(est);
715     else {
716         estimatelist_t el;
717         estimate_t     estimate;
718         int            level;
719         estimate_t     estimate_method = ES_ES;
720         estimate_t     client_method = ES_ES;
721
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;
728             }
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;
736             }
737         }
738
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;
746                     }
747                 }
748             }
749         }
750
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);
761             else
762 #endif
763 #ifdef SAMBA_CLIENT
764             if (strcmp(est->dle->program, "GNUTAR") == 0 &&
765                 est->dle->device[0] == '/' && est->dle->device[1] == '/')
766                 smbtar_calc_estimates(est);
767             else
768 #endif
769 #ifdef GNUTAR
770             if (strcmp(est->dle->program, "GNUTAR") == 0)
771                 gnutar_calc_estimates(est);
772             else
773 #endif
774                 dbprintf(_("Invalid program: %s %s %s\n"),
775                          est->qamname, est->qdirname, est->dle->program);
776         }
777     }
778
779     dbprintf(_("done with amname %s dirname %s spindle %d\n"),
780               est->qamname, est->qdirname, est->dle->spindle);
781 }
782
783 /*
784  * ------------------------------------------------------------------------
785  *
786  */
787
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);
796
797 void
798 application_api_calc_estimate(
799     disk_estimates_t *  est)
800 {
801     int    level;
802     int    i;
803     int    levels[DUMP_LEVELS];
804     int    nb_level = 0;
805     backup_support_option_t *bsu;
806     GPtrArray               *errarray;
807     estimatelist_t el;
808     estimate_t     estimate;
809     estimate_t     estimate_method = ES_ES;
810     estimate_t     client_method = ES_ES;
811     int            has_calcsize = 0;
812     int            has_client = 0;
813
814     bsu = backup_support_option(est->dle->program, g_options, est->dle->disk,
815                                 est->dle->device, &errarray);
816     if (!bsu) {
817         guint  i;
818         for (i=0; i < errarray->len; i++) {
819             char  *line;
820             char  *errmsg;
821             char  *qerrmsg;
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);
834                     }
835                 }
836                 amfree(errmsg);
837                 amfree(qerrmsg);
838             }
839         }
840         if (i == 0) { /* nothing in errarray */
841             char  *errmsg;
842             char  *qerrmsg;
843             errmsg = g_strdup_printf(
844                 _("Application '%s': cannon execute support command"),
845                 est->dle->program);
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);
853                 }
854             }
855             amfree(errmsg);
856             amfree(qerrmsg);
857         }
858         for (level = 0; level < DUMP_LEVELS; level++) {
859             est->est[level].needestimate = 0;
860         }
861         g_ptr_array_free(errarray, TRUE);
862     }
863
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);
867         return;
868     }
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);
872         return;
873     }
874
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)
879             has_client = 1;
880         if (estimate == ES_CALCSIZE)
881             has_calcsize = 1;
882         if (estimate == ES_SERVER) {
883             if (estimate_method == ES_ES)
884                 estimate_method = ES_SERVER;
885         }
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;
892         }
893     }
894
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,
900                          (long long)-2);
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,
906                          (long long)-2);
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"),
914                                             est->dle->program);
915                     } else if (has_client && !bsu->client_estimate) {
916                         errmsg = vstrallocf(_("Application '%s' can't do CLIENT estimate"),
917                                             est->dle->program);
918                     } else if (has_calcsize && !bsu->calcsize) {
919                         errmsg = vstrallocf(_("Application '%s' can't do CALCSIZE estimate"),
920                                             est->dle->program);
921                     } else {
922                         errmsg = vstrallocf(_("Application '%s' can't do estimate"),
923                                             est->dle->program);
924                     }
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);
929                     amfree(errmsg);
930                     amfree(qerrmsg);
931                 }
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;
942             }
943         }
944     }
945
946     if (nb_level == 0)
947         return;
948
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]);
953         }
954         getsize_application_api(est, nb_level, levels, bsu);
955
956     } else {
957         for(level = 0; level < DUMP_LEVELS; level++) {
958             if (est->est[level].needestimate) {
959                 dbprintf(
960                     _("getting size via application API for %s %s level %d\n"),
961                     est->qamname, est->qamdevice, level);
962                 levels[0] = level;
963                 getsize_application_api(est, 1, levels, bsu);
964             }
965         }
966     }
967 }
968
969
970 void
971 generic_calc_estimates(
972     disk_estimates_t *  est)
973 {
974     int pipefd = -1, nullfd = -1;
975     char *cmd;
976     char *cmdline;
977     char *command;
978     GPtrArray *argv_ptr = g_ptr_array_new();
979     char number[NUM_STR_SIZE];
980     unsigned int i;
981     int level;
982     pid_t calcpid;
983     int nb_exclude = 0;
984     int nb_include = 0;
985     char *file_exclude = NULL;
986     char *file_include = NULL;
987     times_t start_time;
988     FILE *dumpout = NULL;
989     off_t size = (off_t)1;
990     char *line = NULL;
991     char *match_expr;
992     amwait_t wait_status;
993     char *errmsg = NULL, *qerrmsg;
994     char tmppath[PATH_MAX];
995     int len;
996
997     cmd = vstralloc(amlibexecdir, "/", "calcsize", NULL);
998
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));
1002     else
1003         g_ptr_array_add(argv_ptr, stralloc("NOCONFIG"));
1004
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));
1010
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;
1019
1020     if (nb_exclude > 0)
1021         file_exclude = build_exclude(est->dle, 0);
1022     if (nb_include > 0)
1023         file_include = build_include(est->dle, 0);
1024
1025     if(file_exclude) {
1026         g_ptr_array_add(argv_ptr, stralloc("-X"));
1027         g_ptr_array_add(argv_ptr, stralloc(file_exclude));
1028     }
1029
1030     if(file_include) {
1031         g_ptr_array_add(argv_ptr, stralloc("-I"));
1032         g_ptr_array_add(argv_ptr, stralloc(file_include));
1033     }
1034     start_time = curclock();
1035
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);
1041     }
1042     dbprintf(_("running: \"%s\"\n"), cmdline);
1043     amfree(cmdline);
1044
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);
1054         }
1055     }
1056     g_ptr_array_add(argv_ptr, NULL);
1057     dbprintf("\n");
1058
1059     fflush(stderr); fflush(stdout);
1060
1061     if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
1062         errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
1063                             strerror(errno));
1064         dbprintf("%s\n", errmsg);
1065         goto common_exit;
1066     }
1067
1068     calcpid = pipespawnv(cmd, STDERR_PIPE, 0,
1069                          &nullfd, &nullfd, &pipefd, (char **)argv_ptr->pdata);
1070     amfree(cmd);
1071
1072     dumpout = fdopen(pipefd,"r");
1073     if (!dumpout) {
1074         error(_("Can't fdopen: %s"), strerror(errno));
1075         /*NOTREACHED*/
1076     }
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)
1082             continue;
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"),
1088                       est->qamname,
1089                       level,
1090                       size_);
1091         }
1092         size = (off_t)size_;
1093     }
1094     amfree(match_expr);
1095
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),
1102                             dbfn());
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),
1107                                 dbfn());
1108         } else {
1109             /* Normal exit */
1110         }
1111     } else {
1112         errmsg = vstrallocf(_("%s got bad exit: see %s"),
1113                              "calcsize", dbfn());
1114     }
1115     dbprintf(_("after %s %s wait: child pid=%d status=%d\n"),
1116              command, est->qamdevice,
1117               (int)calcpid, WEXITSTATUS(wait_status));
1118
1119     dbprintf(_(".....\n"));
1120     dbprintf(_("estimate time for %s: %s\n"),
1121               est->qamname,
1122               walltime_str(timessub(curclock(), start_time)));
1123
1124 common_exit:
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);
1131             amfree(qerrmsg);
1132         }
1133     }
1134     amfree(errmsg);
1135     g_ptr_array_free_full(argv_ptr);
1136     amfree(cmd);
1137 }
1138
1139
1140 void
1141 dump_calc_estimates(
1142     disk_estimates_t *  est)
1143 {
1144     int level;
1145     off_t size;
1146     char *errmsg=NULL, *qerrmsg;
1147
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);
1153
1154             amflock(1, "size");
1155
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);
1165                     amfree(qerrmsg);
1166                 }
1167             }
1168             amfree(errmsg);
1169             fflush(stdout);
1170
1171             amfunlock(1, "size");
1172         }
1173     }
1174 }
1175
1176 #ifdef SAMBA_CLIENT
1177 void
1178 smbtar_calc_estimates(
1179     disk_estimates_t *  est)
1180 {
1181     int level;
1182     off_t size;
1183     char  *errmsg = NULL, *qerrmsg;
1184
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);
1190
1191             amflock(1, "size");
1192
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);
1202                     amfree(qerrmsg);
1203                 }
1204             }
1205             amfree(errmsg);
1206             fflush(stdout);
1207
1208             amfunlock(1, "size");
1209         }
1210     }
1211 }
1212 #endif
1213
1214 #ifdef GNUTAR
1215 void
1216 gnutar_calc_estimates(
1217     disk_estimates_t *  est)
1218 {
1219     int level;
1220     off_t size;
1221     char *errmsg = NULL, *qerrmsg;
1222
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,
1229                                   &errmsg);
1230
1231             amflock(1, "size");
1232
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);
1242                     amfree(qerrmsg);
1243                 }
1244             }
1245             amfree(errmsg);
1246             fflush(stdout);
1247
1248             amfunlock(1, "size");
1249         }
1250     }
1251 }
1252 #endif
1253
1254 typedef struct regex_scale_s {
1255     char *regex;
1256     int scale;
1257 } regex_scale_t;
1258
1259 /*@ignore@*/
1260 regex_scale_t re_size[] = {
1261 #ifdef DUMP
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},
1273                                                               /* DU 4.0 dump  */
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 */
1278
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 */
1284 #endif
1285 #endif
1286
1287 #ifdef VDUMP
1288     {"vdump: Dumping [0-9][0-9]* bytes, ", 1},                 /* OSF/1 vdump */
1289 #endif
1290     
1291 #ifdef VXDUMP
1292     {"vxdump: estimated [0-9][0-9]* blocks", 512},           /* HPUX's vxdump */
1293     {"  VXDUMP: estimated [0-9][0-9]* blocks", 512},                 /* Sinix */
1294 #endif
1295
1296 #ifdef XFSDUMP
1297     {"xfsdump: estimated dump size: [0-9][0-9]* bytes", 1},  /* Irix 6.2 xfs */
1298 #endif
1299
1300 #ifdef GNUTAR
1301     {"Total bytes written: [0-9][0-9]*", 1},                /* Gnutar client */
1302 #endif
1303
1304 #ifdef SAMBA_CLIENT
1305 #if SAMBA_VERSION >= 2
1306 #define SAMBA_DEBUG_LEVEL "0"
1307     {"Total number of bytes: [0-9][0-9]*", 1},                   /* Samba du */
1308 #else
1309 #define SAMBA_DEBUG_LEVEL "3"
1310     {"Total bytes listed: [0-9][0-9]*", 1},                     /* Samba dir */
1311 #endif
1312 #endif
1313
1314     { NULL, 0 }
1315 };
1316 /*@end@*/
1317
1318 off_t
1319 getsize_dump(
1320     dle_t      *dle,
1321     int         level,
1322     char      **errmsg)
1323 {
1324     int pipefd[2], nullfd, stdoutfd, killctl[2];
1325     pid_t dumppid;
1326     off_t size;
1327     FILE *dumpout;
1328     char *dumpkeys = NULL;
1329     char *device = NULL;
1330     char *fstype = NULL;
1331     char *cmd = NULL;
1332     char *name = NULL;
1333     char *line = NULL;
1334     char *rundump_cmd = NULL;
1335     char level_str[NUM_STR_SIZE];
1336     int s;
1337     times_t start_time;
1338     char *qdisk = quote_string(dle->disk);
1339     char *qdevice;
1340     char *config;
1341     amwait_t wait_status;
1342 #if defined(DUMP) || defined(VDUMP) || defined(VXDUMP) || defined(XFSDUMP)
1343     int is_rundump = 1;
1344 #endif
1345
1346     if (level > 9)
1347         return -2; /* planner will not even consider this level */
1348
1349     g_snprintf(level_str, SIZEOF(level_str), "%d", level);
1350
1351     device = amname_to_devname(dle->device);
1352     qdevice = quote_string(device);
1353     fstype = amname_to_fstype(dle->device);
1354
1355     dbprintf(_("calculating for device %s with %s\n"),
1356               qdevice, fstype);
1357
1358     cmd = vstralloc(amlibexecdir, "/rundump", NULL);
1359     rundump_cmd = stralloc(cmd);
1360     if (g_options->config)
1361         config = g_options->config;
1362     else
1363         config = "NOCONFIG";
1364     if ((stdoutfd = nullfd = open("/dev/null", O_RDWR)) == -1) {
1365         *errmsg = vstrallocf(_("getsize_dump could not open /dev/null: %s"),
1366                              strerror(errno));
1367         dbprintf("%s\n", *errmsg);
1368         amfree(cmd);
1369         amfree(rundump_cmd);
1370         amfree(fstype);
1371         amfree(device);
1372         amfree(qdevice);
1373         amfree(qdisk);
1374         return(-1);
1375     }
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"),
1379                              strerror(errno));
1380         dbprintf("%s\n", *errmsg);
1381         amfree(cmd);
1382         amfree(rundump_cmd);
1383         amfree(fstype);
1384         amfree(device);
1385         amfree(qdevice);
1386         amfree(qdisk);
1387         return(-1);
1388     }
1389
1390 #ifdef XFSDUMP                                          /* { */
1391 #ifdef DUMP                                             /* { */
1392     if (strcmp(fstype, "xfs") == 0)
1393 #else                                                   /* } { */
1394     if (1)
1395 #endif                                                  /* } */
1396     {
1397         name = stralloc(" (xfsdump)");
1398         dbprintf(_("running \"%s%s -F -J -l %s - %s\"\n"),
1399                   cmd, name, level_str, qdevice);
1400     }
1401     else
1402 #endif                                                  /* } */
1403 #ifdef VXDUMP                                           /* { */
1404 #ifdef DUMP                                             /* { */
1405     if (strcmp(fstype, "vxfs") == 0)
1406 #else                                                   /* } { */
1407     if (1)
1408 #endif                                                  /* } */
1409     {
1410 #ifdef USE_RUNDUMP
1411         name = stralloc(" (vxdump)");
1412 #else
1413         name = stralloc("");
1414         cmd = newstralloc(cmd, VXDUMP);
1415         config = skip_argument;
1416         is_rundump = 0;
1417 #endif
1418         dumpkeys = vstralloc(level_str, "s", "f", NULL);
1419         dbprintf(_("running \"%s%s %s 1048576 - %s\"\n"),
1420                   cmd, name, dumpkeys, qdevice);
1421     }
1422     else
1423 #endif                                                  /* } */
1424 #ifdef VDUMP                                            /* { */
1425 #ifdef DUMP                                             /* { */
1426     if (strcmp(fstype, "advfs") == 0)
1427 #else                                                   /* } { */
1428     if (1)
1429 #endif                                                  /* } */
1430     {
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);
1435     }
1436     else
1437 #endif                                                  /* } */
1438 #ifdef DUMP                                             /* { */
1439     if (1) {
1440 # ifdef USE_RUNDUMP                                     /* { */
1441 #  ifdef AIX_BACKUP                                     /* { */
1442         name = stralloc(" (backup)");
1443 #  else                                                 /* } { */
1444         name = vstralloc(" (", DUMP, ")", NULL);
1445 #  endif                                                /* } */
1446 # else                                                  /* } { */
1447         name = stralloc("");
1448         cmd = newstralloc(cmd, DUMP);
1449         config = skip_argument;
1450         is_rundump = 0;
1451 # endif                                                 /* } */
1452
1453 # ifdef AIX_BACKUP                                      /* { */
1454         dumpkeys = vstralloc("-", level_str, "f", NULL);
1455         dbprintf(_("running \"%s%s %s - %s\"\n"),
1456                   cmd, name, dumpkeys, qdevice);
1457 # else                                                  /* } { */
1458 #  ifdef HAVE_DUMP_ESTIMATE
1459 #    define PARAM_DUMP_ESTIMATE HAVE_DUMP_ESTIMATE
1460 #  else
1461 #    define PARAM_DUMP_ESTIMATE ""
1462 #  endif
1463 #  ifdef HAVE_HONOR_NODUMP
1464 #    define PARAM_HONOR_NODUMP "h"
1465 #  else
1466 #    define PARAM_HONOR_NODUMP ""
1467 #  endif
1468         dumpkeys = vstralloc(level_str,
1469                              PARAM_DUMP_ESTIMATE,
1470                              PARAM_HONOR_NODUMP,
1471                              "s", "f", NULL);
1472
1473 #  ifdef HAVE_DUMP_ESTIMATE
1474         stdoutfd = pipefd[1];
1475 #  endif
1476
1477 #  ifdef HAVE_HONOR_NODUMP                              /* { */
1478         dbprintf(_("running \"%s%s %s 0 1048576 - %s\"\n"),
1479                   cmd, name, dumpkeys, qdevice);
1480 #  else                                                 /* } { */
1481         dbprintf(_("running \"%s%s %s 1048576 - %s\"\n"),
1482                   cmd, name, dumpkeys, qdevice);
1483 #  endif                                                /* } */
1484 # endif                                                 /* } */
1485     }
1486     else
1487 #endif                                                  /* } */
1488     {
1489         error(_("no dump program available"));
1490         /*NOTREACHED*/
1491     }
1492
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;
1497     }
1498
1499     start_time = curclock();
1500     switch(dumppid = fork()) {
1501     case -1:
1502         *errmsg = vstrallocf(_("cannot fork for killpgrp: %s"),
1503                              strerror(errno));
1504         dbprintf("%s\n", *errmsg);
1505         amfree(dumpkeys);
1506         amfree(cmd);
1507         amfree(rundump_cmd);
1508         amfree(device);
1509         amfree(qdevice);
1510         amfree(qdisk);
1511         amfree(name);
1512         amfree(fstype);
1513         return -1;
1514     default:
1515         break; 
1516     case 0:     /* child process */
1517         if(SETPGRP == -1)
1518             SETPGRP_FAILED();
1519         else if (killctl[0] == -1 || killctl[1] == -1)
1520             dbprintf(_("Trying without killpgrp\n"));
1521         else {
1522             switch(fork()) {
1523             case -1:
1524                 dbprintf(_("fork failed, trying without killpgrp\n"));
1525                 break;
1526
1527             default:
1528             {
1529                 char *config;
1530                 char *killpgrp_cmd = vstralloc(amlibexecdir, "/killpgrp", NULL);
1531                 dbprintf(_("running %s\n"), killpgrp_cmd);
1532                 dup2(killctl[0], 0);
1533                 dup2(nullfd, 1);
1534                 dup2(nullfd, 2);
1535                 close(pipefd[0]);
1536                 close(pipefd[1]);
1537                 close(killctl[1]);
1538                 close(nullfd);
1539                 if (g_options->config)
1540                     config = g_options->config;
1541                 else
1542                     config = "NOCONFIG";
1543                 safe_fd(-1, 0);
1544                 execle(killpgrp_cmd, killpgrp_cmd, config, (char *)0,
1545                        safe_env());
1546                 dbprintf(_("cannot execute %s: %s\n"),
1547                           killpgrp_cmd, strerror(errno));
1548                 exit(-1);
1549             }
1550
1551             case 0:  /* child process */
1552                 break;
1553             }
1554         }
1555
1556         dup2(nullfd, 0);
1557         dup2(stdoutfd, 1);
1558         dup2(pipefd[1], 2);
1559         aclose(pipefd[0]);
1560         if (killctl[0] != -1)
1561             aclose(killctl[0]);
1562         if (killctl[1] != -1)
1563             aclose(killctl[1]);
1564         safe_fd(-1, 0);
1565
1566 #ifdef XFSDUMP
1567 #ifdef DUMP
1568         if (strcmp(fstype, "xfs") == 0)
1569 #else
1570         if (1)
1571 #endif
1572             if (is_rundump)
1573                 execle(cmd, "rundump", config, "xfsdump", "-F", "-J", "-l",
1574                        level_str, "-", device, (char *)0, safe_env());
1575             else
1576                 execle(cmd, "xfsdump", "-F", "-J", "-l",
1577                        level_str, "-", device, (char *)0, safe_env());
1578         else
1579 #endif
1580 #ifdef VXDUMP
1581 #ifdef DUMP
1582         if (strcmp(fstype, "vxfs") == 0)
1583 #else
1584         if (1)
1585 #endif
1586             if (is_rundump)
1587                 execle(cmd, "rundump", config, "vxdump", dumpkeys, "1048576",
1588                        "-", device, (char *)0, safe_env());
1589             else
1590                 execle(cmd, "vxdump", dumpkeys, "1048576", "-",
1591                        device, (char *)0, safe_env());
1592         else
1593 #endif
1594 #ifdef VDUMP
1595 #ifdef DUMP
1596         if (strcmp(fstype, "advfs") == 0)
1597 #else
1598         if (1)
1599 #endif
1600             if (is_rundump)
1601                 execle(cmd, "rundump", config, "vdump", dumpkeys, "60", "-",
1602                        device, (char *)0, safe_env());
1603             else
1604                 execle(cmd, "vdump", dumpkeys, "60", "-",
1605                        device, (char *)0, safe_env());
1606         else
1607 #endif
1608 #ifdef DUMP
1609 # ifdef AIX_BACKUP
1610             if (is_rundump)
1611                 execle(cmd, "rundump", config, "backup", dumpkeys, "-",
1612                        device, (char *)0, safe_env());
1613             else
1614                 execle(cmd, "backup", dumpkeys, "-",
1615                        device, (char *)0, safe_env());
1616 # else
1617             if (is_rundump) {
1618                 execle(cmd, "rundump", config, "dump", dumpkeys, 
1619 #ifdef HAVE_HONOR_NODUMP
1620                        "0",
1621 #endif
1622                        "1048576", "-", device, (char *)0, safe_env());
1623             } else {
1624                 execle(cmd, "dump", dumpkeys, 
1625 #ifdef HAVE_HONOR_NODUMP
1626                        "0",
1627 #endif
1628                        "1048576", "-", device, (char *)0, safe_env());
1629             }
1630 # endif
1631 #endif
1632         {
1633             error(_("exec %s failed or no dump program available: %s"),
1634                   cmd, strerror(errno));
1635             /*NOTREACHED*/
1636         }
1637     }
1638
1639     amfree(dumpkeys);
1640     amfree(rundump_cmd);
1641
1642     aclose(pipefd[1]);
1643     if (killctl[0] != -1)
1644         aclose(killctl[0]);
1645     dumpout = fdopen(pipefd[0],"r");
1646     if (!dumpout) {
1647         error(_("Can't fdopen: %s"), strerror(errno));
1648         /*NOTREACHED*/
1649     }
1650
1651     for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1652         if (line[0] == '\0')
1653             continue;
1654         dbprintf("%s\n", line);
1655         size = handle_dumpline(line);
1656         if(size > (off_t)-1) {
1657             amfree(line);
1658             while ((line = agets(dumpout)) != NULL) {
1659                 if (line[0] != '\0')
1660                     break;
1661                 amfree(line);
1662             }
1663             if (line != NULL) {
1664                 dbprintf("%s\n", line);
1665             }
1666             break;
1667         }
1668     }
1669     amfree(line);
1670
1671     dbprintf(".....\n");
1672     dbprintf(_("estimate time for %s level %d: %s\n"),
1673               qdisk,
1674               level,
1675               walltime_str(timessub(curclock(), start_time)));
1676     if(size == (off_t)-1) {
1677         *errmsg = vstrallocf(_("no size line match in %s%s output"),
1678                              cmd, name);
1679         dbprintf(_("%s for %s\n"),
1680                   *errmsg, qdisk);
1681
1682         dbprintf(".....\n");
1683         dbprintf(_("Run %s%s manually to check for errors\n"),
1684                     cmd, name);
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");
1689     } else {
1690             dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1691               qdisk,
1692               level,
1693               (long long)size);
1694     }
1695
1696     if (killctl[1] != -1) {
1697         dbprintf(_("asking killpgrp to terminate\n"));
1698         aclose(killctl[1]);
1699         for(s = 5; s > 0; --s) {
1700             sleep(1);
1701             if (waitpid(dumppid, NULL, WNOHANG) != -1)
1702                 goto terminated;
1703         }
1704     }
1705     
1706     /*
1707      * First, try to kill the dump process nicely.  If it ignores us
1708      * for several seconds, hit it harder.
1709      */
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));
1713     }
1714     /* Now check whether it dies */
1715     for(s = 5; s > 0; --s) {
1716         sleep(1);
1717         if (waitpid(dumppid, NULL, WNOHANG) != -1)
1718             goto terminated;
1719     }
1720
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));
1724     }
1725     for(s = 5; s > 0; --s) {
1726         sleep(1);
1727         if (waitpid(dumppid, NULL, WNOHANG) != -1)
1728             goto terminated;
1729     }
1730
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());
1740         } else {
1741             /* Normal exit */
1742         }
1743     } else {
1744         *errmsg = vstrallocf(_("%s got bad exit: see %s"),
1745                              cmd, dbfn());
1746     }
1747     dbprintf(_("after %s%s %s wait\n"), cmd, name, qdisk);
1748
1749  terminated:
1750
1751     aclose(nullfd);
1752     afclose(dumpout);
1753
1754     amfree(device);
1755     amfree(qdevice);
1756     amfree(qdisk);
1757     amfree(fstype);
1758
1759     amfree(cmd);
1760     amfree(name);
1761
1762     return size;
1763 }
1764
1765 #ifdef SAMBA_CLIENT
1766 off_t
1767 getsize_smbtar(
1768     dle_t      *dle,
1769     int         level,
1770     char      **errmsg)
1771 {
1772     int pipefd = -1, nullfd = -1, passwdfd = -1;
1773     pid_t dumppid;
1774     off_t size;
1775     FILE *dumpout;
1776     char *tarkeys, *sharename, *user_and_password = NULL, *domain = NULL;
1777     char *share = NULL, *subdir = NULL;
1778     size_t lpass;
1779     char *pwtext;
1780     size_t pwtext_len;
1781     char *line;
1782     char *pw_fd_env;
1783     times_t start_time;
1784     char *error_pn = NULL;
1785     char *qdisk = quote_string(dle->disk);
1786     amwait_t wait_status;
1787
1788     error_pn = stralloc2(get_pname(), "-smbclient");
1789
1790     if (level > 1)
1791         return -2; /* planner will not even consider this level */
1792
1793     parsesharename(dle->device, &share, &subdir);
1794     if (!share) {
1795         amfree(share);
1796         amfree(subdir);
1797         set_pname(error_pn);
1798         amfree(error_pn);
1799         error(_("cannot parse disk entry %s for share/subdir"), qdisk);
1800         /*NOTREACHED*/
1801     }
1802     if ((subdir) && (SAMBA_VERSION < 2)) {
1803         amfree(share);
1804         amfree(subdir);
1805         set_pname(error_pn);
1806         amfree(error_pn);
1807         error(_("subdirectory specified for share %s but samba not v2 or better"), qdisk);
1808         /*NOTREACHED*/
1809     }
1810     if ((user_and_password = findpass(share, &domain)) == NULL) {
1811
1812         if(domain) {
1813             memset(domain, '\0', strlen(domain));
1814             amfree(domain);
1815         }
1816         set_pname(error_pn);
1817         amfree(error_pn);
1818         error(_("cannot find password for %s"), dle->disk);
1819         /*NOTREACHED*/
1820     }
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);
1825         if(domain) {
1826             memset(domain, '\0', strlen(domain));
1827             amfree(domain);
1828         }
1829         set_pname(error_pn);
1830         amfree(error_pn);
1831         error(_("password field not \'user%%pass\' for %s"), dle->disk);
1832         /*NOTREACHED*/
1833     }
1834     *pwtext++ = '\0';
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);
1839         if(domain) {
1840             memset(domain, '\0', strlen(domain));
1841             amfree(domain);
1842         }
1843         set_pname(error_pn);
1844         amfree(error_pn);
1845         error(_("cannot make share name of %s"), share);
1846         /*NOTREACHED*/
1847     }
1848     if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
1849         memset(user_and_password, '\0', (size_t)lpass);
1850         amfree(user_and_password);
1851         if(domain) {
1852             memset(domain, '\0', strlen(domain));
1853             amfree(domain);
1854         }
1855         set_pname(error_pn);
1856         amfree(error_pn);
1857         amfree(sharename);
1858         error(_("could not open /dev/null: %s\n"),
1859               strerror(errno));
1860         /*NOTREACHED*/
1861     }
1862
1863 #if SAMBA_VERSION >= 2
1864     if (level == 0)
1865         tarkeys = "archive 0;recurse;du";
1866     else
1867         tarkeys = "archive 1;recurse;du";
1868 #else
1869     if (level == 0)
1870         tarkeys = "archive 0;recurse;dir";
1871     else
1872         tarkeys = "archive 1;recurse;dir";
1873 #endif
1874
1875     start_time = curclock();
1876
1877     if (pwtext_len > 0) {
1878         pw_fd_env = "PASSWD_FD";
1879     } else {
1880         pw_fd_env = "dummy_PASSWD_FD";
1881     }
1882     dumppid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE, 0,
1883               &nullfd, &nullfd, &pipefd, 
1884               pw_fd_env, &passwdfd,
1885               "smbclient",
1886               sharename,
1887               "-d", SAMBA_DEBUG_LEVEL,
1888               *user_and_password ? "-U" : skip_argument,
1889               *user_and_password ? user_and_password : skip_argument,
1890               "-E",
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,
1896 #endif
1897               "-c", tarkeys,
1898               NULL);
1899     if(domain) {
1900         memset(domain, '\0', strlen(domain));
1901         amfree(domain);
1902     }
1903     aclose(nullfd);
1904     if(pwtext_len > 0 && full_write(passwdfd, pwtext, pwtext_len) < pwtext_len) {
1905         int save_errno = errno;
1906
1907         memset(user_and_password, '\0', (size_t)lpass);
1908         amfree(user_and_password);
1909         aclose(passwdfd);
1910         set_pname(error_pn);
1911         amfree(error_pn);
1912         error(_("password write failed: %s"), strerror(save_errno));
1913         /*NOTREACHED*/
1914     }
1915     memset(user_and_password, '\0', (size_t)lpass);
1916     amfree(user_and_password);
1917     aclose(passwdfd);
1918     amfree(sharename);
1919     amfree(share);
1920     amfree(subdir);
1921     amfree(error_pn);
1922     dumpout = fdopen(pipefd,"r");
1923     if (!dumpout) {
1924         error(_("Can't fdopen: %s"), strerror(errno));
1925         /*NOTREACHED*/
1926     }
1927
1928     for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
1929         if (line[0] == '\0')
1930             continue;
1931         dbprintf("%s\n", line);
1932         size = handle_dumpline(line);
1933         if(size > -1) {
1934             amfree(line);
1935             while ((line = agets(dumpout)) != NULL) {
1936                 if (line[0] != '\0')
1937                     break;
1938                 amfree(line);
1939             }
1940             if(line != NULL) {
1941                 dbprintf("%s\n", line);
1942             }
1943             break;
1944         }
1945     }
1946     amfree(line);
1947
1948     dbprintf(".....\n");
1949     dbprintf(_("estimate time for %s level %d: %s\n"),
1950               qdisk,
1951               level,
1952               walltime_str(timessub(curclock(), start_time)));
1953     if(size == (off_t)-1) {
1954         *errmsg = vstrallocf(_("no size line match in %s output"),
1955                              SAMBA_CLIENT);
1956         dbprintf(_("%s for %s\n"),
1957                   *errmsg, qdisk);
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");
1963     }
1964     dbprintf(_("estimate size for %s level %d: %lld KB\n"),
1965               qdisk,
1966               level,
1967               (long long)size);
1968
1969     kill(-dumppid, SIGTERM);
1970
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),
1980                                  dbfn());
1981         } else {
1982             /* Normal exit */
1983         }
1984     } else {
1985         *errmsg = vstrallocf(_("%s got bad exit: see %s"),
1986                              SAMBA_CLIENT, dbfn());
1987     }
1988     dbprintf(_("after %s %s wait\n"), SAMBA_CLIENT, qdisk);
1989
1990     afclose(dumpout);
1991     pipefd = -1;
1992
1993     amfree(error_pn);
1994     amfree(qdisk);
1995
1996     return size;
1997 }
1998 #endif
1999
2000 #ifdef GNUTAR
2001 off_t
2002 getsize_gnutar(
2003     dle_t      *dle,
2004     int         level,
2005     time_t      dumpsince,
2006     char      **errmsg)
2007 {
2008     int pipefd = -1, nullfd = -1;
2009     pid_t dumppid;
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;
2016     FILE *in = NULL;
2017     FILE *out = NULL;
2018     char *line = NULL;
2019     char *cmd = NULL;
2020     char *command = NULL;
2021     char dumptimestr[80];
2022     struct tm *gmtm;
2023     int nb_exclude = 0;
2024     int nb_include = 0;
2025     GPtrArray *argv_ptr = g_ptr_array_new();
2026     char *file_exclude = NULL;
2027     char *file_include = NULL;
2028     times_t start_time;
2029     int infd, outfd;
2030     ssize_t nb;
2031     char buf[32768];
2032     char *qdisk = quote_string(dle->disk);
2033     char *gnutar_list_dir;
2034     amwait_t wait_status;
2035     char tmppath[PATH_MAX];
2036
2037     if (level > 9)
2038         return -2; /* planner will not even consider this level */
2039
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;
2044
2045     if(nb_exclude > 0) file_exclude = build_exclude(dle, 0);
2046     if(nb_include > 0) file_include = build_include(dle, 0);
2047
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];
2053         int baselevel;
2054         char *sdisk = sanitise_filename(dle->disk);
2055
2056         basename = vstralloc(gnutar_list_dir,
2057                              "/",
2058                              g_options->hostname,
2059                              sdisk,
2060                              NULL);
2061         amfree(sdisk);
2062
2063         g_snprintf(number, SIZEOF(number), "%d", level);
2064         incrname = vstralloc(basename, "_", number, ".new", NULL);
2065         unlink(incrname);
2066
2067         /*
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.
2071          */
2072         baselevel = level;
2073         infd = -1;
2074         while (infd == -1) {
2075             if (--baselevel >= 0) {
2076                 g_snprintf(number, SIZEOF(number), "%d", baselevel);
2077                 inputname = newvstralloc(inputname,
2078                                          basename, "_", number, NULL);
2079             } else {
2080                 inputname = newstralloc(inputname, "/dev/null");
2081             }
2082             if ((infd = open(inputname, O_RDONLY)) == -1) {
2083
2084                 *errmsg = vstrallocf(_("gnutar: error opening %s: %s"),
2085                                      inputname, strerror(errno));
2086                 dbprintf("%s\n", *errmsg);
2087                 if (baselevel < 0) {
2088                     goto common_exit;
2089                 }
2090                 amfree(*errmsg);
2091             }
2092         }
2093
2094         /*
2095          * Copy the previous listed incremental file to the new one.
2096          */
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);
2101             goto common_exit;
2102         }
2103
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);
2109                 goto common_exit;
2110             }
2111         }
2112
2113         if (nb < 0) {
2114             *errmsg = vstrallocf(_("reading from %s: %s"),
2115                                  inputname, strerror(errno));
2116             dbprintf("%s\n", *errmsg);
2117             goto common_exit;
2118         }
2119
2120         if (close(infd) != 0) {
2121             *errmsg = vstrallocf(_("closing %s: %s"),
2122                                  inputname, strerror(errno));
2123             dbprintf("%s\n", *errmsg);
2124             goto common_exit;
2125         }
2126         if (close(outfd) != 0) {
2127             *errmsg = vstrallocf(_("closing %s: %s"),
2128                                  incrname, strerror(errno));
2129             dbprintf("%s\n", *errmsg);
2130             goto common_exit;
2131         }
2132
2133         amfree(inputname);
2134         amfree(basename);
2135     }
2136
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);
2142
2143     dirname = amname_to_dirname(dle->device);
2144
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));
2149     else
2150         g_ptr_array_add(argv_ptr, stralloc("NOCONFIG"));
2151
2152 #ifdef GNUTAR
2153     g_ptr_array_add(argv_ptr, stralloc(GNUTAR));
2154 #else
2155     g_ptr_array_add(argv_ptr, stralloc("tar"));
2156 #endif
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));
2170     } else {
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));
2174     }
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"));
2182 #endif
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"));
2186
2187     if(file_exclude) {
2188         g_ptr_array_add(argv_ptr, stralloc("--exclude-from"));
2189         g_ptr_array_add(argv_ptr, stralloc(file_exclude));
2190     }
2191
2192     if(file_include) {
2193         g_ptr_array_add(argv_ptr, stralloc("--files-from"));
2194         g_ptr_array_add(argv_ptr, stralloc(file_include));
2195     }
2196     else {
2197         g_ptr_array_add(argv_ptr, stralloc("."));
2198     }
2199     g_ptr_array_add(argv_ptr, NULL);
2200
2201     start_time = curclock();
2202
2203     if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
2204         *errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
2205                              strerror(errno));
2206         dbprintf("%s\n", *errmsg);
2207         goto common_exit;
2208     }
2209
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);
2213
2214     dumpout = fdopen(pipefd,"r");
2215     if (!dumpout) {
2216         error(_("Can't fdopen: %s"), strerror(errno));
2217         /*NOTREACHED*/
2218     }
2219
2220     for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
2221         if (line[0] == '\0')
2222             continue;
2223         dbprintf("%s\n", line);
2224         size = handle_dumpline(line);
2225         if(size > (off_t)-1) {
2226             amfree(line);
2227             while ((line = agets(dumpout)) != NULL) {
2228                 if (line[0] != '\0') {
2229                     break;
2230                 }
2231                 amfree(line);
2232             }
2233             if (line != NULL) {
2234                 dbprintf("%s\n", line);
2235                 break;
2236             }
2237             break;
2238         }
2239     }
2240     amfree(line);
2241
2242     dbprintf(".....\n");
2243     dbprintf(_("estimate time for %s level %d: %s\n"),
2244               qdisk,
2245               level,
2246               walltime_str(timessub(curclock(), start_time)));
2247     if(size == (off_t)-1) {
2248         *errmsg = vstrallocf(_("no size line match in %s output"),
2249                              command);
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");
2256     }
2257     dbprintf(_("estimate size for %s level %d: %lld KB\n"),
2258               qdisk,
2259               level,
2260               (long long)size);
2261
2262     kill(-dumppid, SIGTERM);
2263
2264     dbprintf(_("waiting for %s \"%s\" child\n"),
2265              command, qdisk);
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());
2274         } else {
2275             /* Normal exit */
2276         }
2277     } else {
2278         *errmsg = vstrallocf(_("%s got bad exit: see %s"),
2279                              cmd, dbfn());
2280     }
2281     dbprintf(_("after %s %s wait\n"), command, qdisk);
2282
2283 common_exit:
2284
2285     if (incrname) {
2286         unlink(incrname);
2287     }
2288     amfree(incrname);
2289     amfree(basename);
2290     amfree(dirname);
2291     amfree(inputname);
2292     g_ptr_array_free_full(argv_ptr);
2293     amfree(qdisk);
2294     amfree(cmd);
2295     amfree(file_exclude);
2296     amfree(file_include);
2297
2298     aclose(nullfd);
2299     afclose(dumpout);
2300     afclose(in);
2301     afclose(out);
2302
2303     return size;
2304 }
2305 #endif
2306
2307 off_t
2308 getsize_application_api(
2309     disk_estimates_t         *est,
2310     int                       nb_level,
2311     int                      *levels,
2312     backup_support_option_t  *bsu)
2313 {
2314     dle_t *dle = est->dle;
2315     int pipeinfd[2], pipeoutfd[2], pipeerrfd[2];
2316     pid_t dumppid;
2317     off_t size = (off_t)-1;
2318     FILE *dumpout;
2319     FILE *dumperr;
2320     char *line = NULL;
2321     char *cmd = NULL;
2322     char *cmdline;
2323     guint i;
2324     int   j;
2325     GPtrArray *argv_ptr = g_ptr_array_new();
2326     char *newoptstr = NULL;
2327     off_t size1, size2;
2328     times_t start_time;
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];
2333     GSList   *scriptlist;
2334     script_t *script;
2335     char     *errmsg = NULL;
2336     estimate_t     estimate;
2337     estimatelist_t el;
2338
2339     cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
2340
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"));
2346     }
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));
2350     }
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));
2354     }
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));
2360     }
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));
2365     }
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))
2372             break;
2373         estimate = ES_CLIENT;
2374     }
2375     if (estimate == ES_CALCSIZE && bsu->calcsize) {
2376         g_ptr_array_add(argv_ptr, stralloc("--calcsize"));
2377     }
2378
2379     application_property_add_to_argv(argv_ptr, dle, bsu, g_options->features);
2380
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);
2386         }
2387     }
2388
2389     g_ptr_array_add(argv_ptr, NULL);
2390
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);
2396     amfree(cmdline);
2397
2398     if (pipe(pipeerrfd) < 0) {
2399         errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2400                             strerror(errno));
2401         goto common_exit;
2402     }
2403
2404     if (pipe(pipeinfd) < 0) {
2405         errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2406                             strerror(errno));
2407         goto common_exit;
2408     }
2409
2410     if (pipe(pipeoutfd) < 0) {
2411         errmsg = vstrallocf(_("getsize_application_api could not create data pipes: %s"),
2412                             strerror(errno));
2413         goto common_exit;
2414     }
2415
2416     start_time = curclock();
2417
2418     switch(dumppid = fork()) {
2419     case -1:
2420       size = (off_t)-1;
2421       goto common_exit;
2422     default:
2423       break; /* parent */
2424     case 0:
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]);
2431       safe_fd(-1, 0);
2432
2433       execve(cmd, (char **)argv_ptr->pdata, safe_env());
2434       error(_("exec %s failed: %s"), cmd, strerror(errno));
2435       /*NOTREACHED*/
2436     }
2437     amfree(newoptstr);
2438
2439     aclose(pipeinfd[0]);
2440     aclose(pipeoutfd[1]);
2441     aclose(pipeerrfd[1]);
2442     aclose(pipeinfd[1]);
2443
2444     dumpout = fdopen(pipeoutfd[0],"r");
2445     if (!dumpout) {
2446         error(_("Can't fdopen: %s"), strerror(errno));
2447         /*NOTREACHED*/
2448     }
2449
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;
2453         int  level = 0;
2454         if (line[0] == '\0')
2455             continue;
2456         dbprintf("%s\n", line);
2457         if (strncmp(line,"ERROR ", 6) == 0) {
2458             char *errmsg, *qerrmsg;
2459
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);
2464             amfree(qerrmsg);
2465             continue;
2466         }
2467         i = sscanf(line, "%d %lld %lld", &level, &size1_, &size2_);
2468         if (i != 3) {
2469             i = sscanf(line, "%lld %lld", &size1_, &size2_);
2470             level = levels[0];
2471             if (i != 2) {
2472                 char *errmsg, *qerrmsg;
2473
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);
2478                 amfree(qerrmsg);
2479                 continue;
2480             }
2481         }
2482         size1 = (off_t)size1_;
2483         size2 = (off_t)size2_;
2484         if (size1 <= 0 || size2 <=0)
2485             size = -1;
2486         else if (size1 * size2 > 0)
2487             size = size1 * size2;
2488         dbprintf(_("estimate size for %s level %d: %lld KB\n"),
2489                  qamdevice,
2490                  level,
2491                  (long long)size);
2492         g_printf("%s %d SIZE %lld\n", est->qamname, level, (long long)size);
2493     }
2494     amfree(line);
2495
2496     dumperr = fdopen(pipeerrfd[0],"r");
2497     if (!dumperr) {
2498         error(_("Can't fdopen: %s"), strerror(errno));
2499         /*NOTREACHED*/
2500     }
2501
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);
2510             }
2511             dbprintf("ERROR %s", qerr);
2512             amfree(err);
2513             amfree(qerr);
2514         }
2515         amfree(line);
2516     }
2517
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)));
2522     } else {
2523         dbprintf(_("estimate time for %s all level: %s\n"), qamdevice,
2524                  walltime_str(timessub(curclock(), start_time)));
2525     }
2526
2527     kill(-dumppid, SIGTERM);
2528
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());
2538         } else {
2539             /* Normal exit */
2540         }
2541     } else {
2542         errmsg = vstrallocf(_("%s got bad exit: see %s"),
2543                             cmd, dbfn());
2544     }
2545     dbprintf(_("after %s %s wait\n"), cmd, qdisk);
2546
2547     afclose(dumpout);
2548     afclose(dumperr);
2549
2550 common_exit:
2551
2552     amfree(cmd);
2553     g_ptr_array_free_full(argv_ptr);
2554     amfree(newoptstr);
2555     amfree(qdisk);
2556     amfree(qamdevice);
2557     if (errmsg) {
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);
2562         }
2563         amfree(errmsg);
2564         amfree(qerrmsg);
2565     }
2566     return size;
2567 }
2568
2569
2570 /*
2571  * Returns the value of the first integer in a string.
2572  */
2573
2574 double
2575 first_num(
2576     char *      str)
2577 {
2578     char *start;
2579     int ch;
2580     double d;
2581
2582     ch = *str++;
2583     while(ch && !isdigit(ch)) ch = *str++;
2584     start = str-1;
2585     while(isdigit(ch) || (ch == '.')) ch = *str++;
2586     str[-1] = '\0';
2587     d = atof(start);
2588     str[-1] = (char)ch;
2589     return d;
2590 }
2591
2592
2593 /*
2594  * Checks the dump output line against the error and size regex tables.
2595  */
2596
2597 off_t
2598 handle_dumpline(
2599     char *      str)
2600 {
2601     regex_scale_t *rp;
2602     double size;
2603
2604     /* check for size match */
2605     /*@ignore@*/
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);
2609             if(size < 0.0)
2610                 size = 1.0;             /* found on NeXT -- sigh */
2611             return (off_t)size;
2612         }
2613     }
2614     /*@end@*/
2615     return (off_t)-1;
2616 }