From: Eric Bollengier Date: Tue, 31 Jan 2023 08:48:55 +0000 (+0100) Subject: Add limit option to the estimate command X-Git-Tag: Beta-15.0.0~245 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d9165a2e327d1c83839cefbb781264164eb66973;p=thirdparty%2Fbacula.git Add limit option to the estimate command --- diff --git a/bacula/src/dird/ua_cmds.c b/bacula/src/dird/ua_cmds.c index fbbee6951..78ad931f7 100644 --- a/bacula/src/dird/ua_cmds.c +++ b/bacula/src/dird/ua_cmds.c @@ -119,7 +119,7 @@ static struct cmdstruct commands[] = { /* C { NT_("disable"), disable_cmd, _("Disable a job, attributes batch process"), NT_("job= | jobs all | client= | schedule= | storage= | batch"), true}, { NT_("enable"), enable_cmd, _("Enable a job, attributes batch process"), NT_("job= | client= | schedule= | storage= | batch"), true}, { NT_("estimate"), estimate_cmd, _("Performs FileSet estimate, listing gives full listing"), - NT_("fileset= client= level= accurate= job= listing"), true}, + NT_("fileset= client= level= accurate= job= limit= listing"), true}, { NT_("exit"), quit_cmd, _("Terminate Bconsole session"), NT_(""), false}, { NT_("gui"), gui_cmd, _("Non-interactive gui mode"), NT_("on | off"), false}, @@ -1410,6 +1410,7 @@ static int estimate_cmd(UAContext *ua, const char *cmd) FILESET *fileset = NULL; POOL_MEM buf; int listing = 0; + int limit = 0; char since[MAXSTRING]; JCR *jcr = ua->jcr; int accurate=-1; @@ -1501,6 +1502,21 @@ static int estimate_cmd(UAContext *ua, const char *cmd) return 1; } } + if (strcasecmp(ua->argk[i], NT_("limit")) == 0) { + if (ua->argv[i]) { + if (!is_a_number(ua->argv[i])) { + ua->error_msg(_("Invalid value for limit. " + "It must be a positive number\n")); + return 1; + } + limit = str_to_int64(ua->argv[i]); + continue; + } else { + ua->error_msg(_("Limit value missing.\n")); + return 1; + } + } + } if (!job && !(client && fileset)) { if (!(job = select_job_resource(ua))) { @@ -1589,12 +1605,12 @@ static int estimate_cmd(UAContext *ua, const char *cmd) * If the job is in accurate mode, we send the list of * all files to FD. */ - Dmsg1(40, "estimate accurate=%d\n", jcr->accurate); + Dmsg2(40, "estimate accurate=%d limit=%d\n", jcr->accurate, limit); if (!send_accurate_current_files(jcr)) { goto bail_out; } - jcr->file_bsock->fsend("estimate listing=%d\n", listing); + jcr->file_bsock->fsend("estimate listing=%d limit=%d\n", listing, limit); while (jcr->file_bsock->recv() >= 0) { ua->send_msg("%s", jcr->file_bsock->msg); } diff --git a/bacula/src/filed/estimate.c b/bacula/src/filed/estimate.c index dca1aee07..a70f8885a 100644 --- a/bacula/src/filed/estimate.c +++ b/bacula/src/filed/estimate.c @@ -112,6 +112,15 @@ static int tally_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level) attr.olname = (POOLMEM *)ff_pkt->link; print_ls_output(jcr, &attr, M_INFO); } + + /* Stop very long estimate commands */ + if (jcr->estimate_limit > 0 && + jcr->num_files_examined > (uint32_t)jcr->estimate_limit) + { + Dmsg1(50, "Stop estimate after %d files\n", jcr->estimate_limit); + return 0; + } + /* TODO: Add loop over jcr->file_list to get Accurate deleted files*/ return 1; } diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index 84e4cf49f..d7f2d14c0 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -181,6 +181,7 @@ static char restoreobjcmd1[] = "restoreobject JobId=%u %d,%d,%d,%d,%d,%d\n"; static char endrestoreobjectcmd[] = "restoreobject end\n"; static char verifycmd[] = "verify level=%30s"; static char estimatecmd[] = "estimate listing=%d"; +static char estimatecmd2[] = "estimate listing=%d limit=%d"; static char runbefore[] = "RunBeforeJob %s"; static char runafter[] = "RunAfterJob %s"; static char runscript[] = "Run OnSuccess=%d OnFailure=%d AbortOnError=%d When=%d Command=%s"; @@ -2496,12 +2497,15 @@ static int estimate_cmd(JCR *jcr) { BSOCK *dir = jcr->dir_bsock; char ed1[50], ed2[50]; + jcr->estimate_limit=-1; - if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) { - pm_strcpy(jcr->errmsg, dir->msg); - Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg); - dir->fsend(_("2992 Bad estimate command.\n")); - return 0; + if (sscanf(dir->msg, estimatecmd2, &jcr->listing, &jcr->estimate_limit) != 2) { + if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) { + pm_strcpy(jcr->errmsg, dir->msg); + Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg); + dir->fsend(_("2992 Bad estimate command.\n")); + return 0; + } } setup_find_files(jcr, jcr->director); diff --git a/bacula/src/findlib/find_one.c b/bacula/src/findlib/find_one.c index 2bf988bd2..7e58b5073 100644 --- a/bacula/src/findlib/find_one.c +++ b/bacula/src/findlib/find_one.c @@ -823,7 +823,7 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, * before traversing it. */ rtn_stat = 1; - while (!job_canceled(jcr)) { + while (!job_canceled(jcr) && rtn_stat == 1) { char *p, *q, *s; int l; int i; @@ -857,12 +857,11 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, } *q = *s = 0; if (!file_is_excluded(ff_pkt, link)) { - rtn_stat = find_one_file(jcr, ff_pkt, handle_file, link, snap_link, our_device, false); + rtn_stat = find_one_file(jcr, ff_pkt, handle_file, link, snap_link, our_device, false); /* TODO: and error here is ignored */ if (ff_pkt->linked) { ff_pkt->linked->FileIndex = ff_pkt->FileIndex; } } - } closedir(directory); free(link); diff --git a/bacula/src/jcr.h b/bacula/src/jcr.h index 2993f3904..7dbf1cbdf 100644 --- a/bacula/src/jcr.h +++ b/bacula/src/jcr.h @@ -475,6 +475,7 @@ public: int incremental; /* set if incremental for SINCE */ utime_t mtime; /* begin time for SINCE */ int listing; /* job listing in estimate */ + int estimate_limit; /* Number of files to display in estimate */ long Ticket; /* Ticket */ char *big_buf; /* I/O buffer */ POOLMEM *compress_buf; /* Compression buffer */