]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Add limit option to the estimate command
authorEric Bollengier <eric@baculasystems.com>
Tue, 31 Jan 2023 08:48:55 +0000 (09:48 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:57:01 +0000 (13:57 +0200)
bacula/src/dird/ua_cmds.c
bacula/src/filed/estimate.c
bacula/src/filed/job.c
bacula/src/findlib/find_one.c
bacula/src/jcr.h

index fbbee6951997b4ccc8a7f48fd4cc552860f36f4c..78ad931f7ddfd1cb6d37b95323f3ee98ef413824 100644 (file)
@@ -119,7 +119,7 @@ static struct cmdstruct commands[] = {                                      /* C
  { NT_("disable"),    disable_cmd,   _("Disable a job, attributes batch process"), NT_("job=<name> | jobs all | client=<name> | schedule=<name> | storage=<name> | batch"),  true},
  { NT_("enable"),     enable_cmd,    _("Enable a job, attributes batch process"), NT_("job=<name> | client=<name> | schedule=<name> | storage=<name> | batch"),   true},
  { NT_("estimate"),   estimate_cmd,  _("Performs FileSet estimate, listing gives full listing"),
-   NT_("fileset=<fs> client=<cli> level=<level> accurate=<yes/no> job=<job> listing"), true},
+   NT_("fileset=<fs> client=<cli> level=<level> accurate=<yes/no> job=<job> limit=<int> 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);
    }
index dca1aee07dff2c940bea5ad7c3a8b1959a981e95..a70f8885a37c5a07e4f357c4cd76f70295c06c10 100644 (file)
@@ -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;
 }
index 84e4cf49fd73d94fe874aa31ef44dd917f6d3f66..d7f2d14c00a622b0ad74d4856531f8d49129cd38 100644 (file)
@@ -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);
index 2bf988bd26e3b7f41288ae75494c71eb2732a36a..7e58b507342c608b5cf45793aa2152e75c157931 100644 (file)
@@ -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);
index 2993f3904a4fd5ffb13e642ca0fd55d2c9b03f7b..7dbf1cbdf6a4a161ec3a2be8cf5645672cc96881 100644 (file)
@@ -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 */