]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_select.c
Fix: clock diff, Dan's patch, Nic's patch, segfault
[bacula/bacula] / bacula / src / dird / ua_select.c
index 16b668233c99529bfe08cecc2d59f6f2dcab9ea3..0b1e1ef07e7309ebe95856a139af815ad45267c2 100644 (file)
@@ -29,8 +29,6 @@
 
 #include "bacula.h"
 #include "dird.h"
-#include "ua.h"
-
 
 /* Imported variables */
 
@@ -59,7 +57,10 @@ int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
          continue;
        }
        if (strcasecmp(ua->cmd, _("yes")) == 0) {
-         break;
+         return 1;
+       }
+       if (strcasecmp(ua->cmd, _("no")) == 0) {
+         return 0;
        }
     }
     return 1;
@@ -73,9 +74,8 @@ int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
  */
 int find_arg_keyword(UAContext *ua, char **list)
 {
-   int i, j;
-   for (i=1; i<ua->argc; i++) {
-      for(j=0; list[j]; j++) {
+   for (int i=1; i<ua->argc; i++) {
+      for(int j=0; list[j]; j++) {
         if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
            return j;
         }
@@ -84,6 +84,42 @@ int find_arg_keyword(UAContext *ua, char **list)
    return -1;
 }
 
+/* 
+ * Given one keyword, find the first one that
+ *   is in the argument list.
+ * Returns: argk index (always gt 0)
+ *         -1 if not found
+ */
+int find_arg(UAContext *ua, char *keyword)
+{
+   for (int i=1; i<ua->argc; i++) {
+      if (strcasecmp(keyword, ua->argk[i]) == 0) {
+        return i;
+      }
+   }
+   return -1;
+}
+
+/* 
+ * Given a single keyword, find it in the argument list, but
+ *   it must have a value
+ * Returns: -1 if not found or no value
+ *          list index (base 0) on success
+ */
+int find_arg_with_value(UAContext *ua, char *keyword)
+{
+   for (int i=1; i<ua->argc; i++) {
+      if (strcasecmp(keyword, ua->argk[i]) == 0) {
+        if (ua->argv[i]) {
+           return i;
+        } else {
+           return -1;
+        }
+      }
+   }
+   return -1;
+}
+
 /* 
  * Given a list of keywords, prompt the user 
  * to choose one.
@@ -98,7 +134,7 @@ int do_keyword_prompt(UAContext *ua, char *msg, char **list)
    for (i=0; list[i]; i++) {
       add_prompt(ua, list[i]);
    }
-   return do_prompt(ua, msg, NULL, 0);
+   return do_prompt(ua, "", msg, NULL, 0);
 }
 
 
@@ -116,7 +152,7 @@ STORE *select_storage_resource(UAContext *ua)
       add_prompt(ua, store->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Storage resource"), name, sizeof(name));
+   do_prompt(ua, _("Storage"),  _("Select Storage resource"), name, sizeof(name));
    store = (STORE *)GetResWithName(R_STORAGE, name);
    return store;
 }
@@ -135,7 +171,7 @@ FILESET *select_fileset_resource(UAContext *ua)
       add_prompt(ua, fs->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select FileSet resource"), name, sizeof(name));
+   do_prompt(ua, _("FileSet"), _("Select FileSet resource"), name, sizeof(name));
    fs = (FILESET *)GetResWithName(R_FILESET, name);
    return fs;
 }
@@ -163,7 +199,7 @@ CAT *get_catalog_resource(UAContext *ua)
         add_prompt(ua, catalog->hdr.name);
       }
       UnlockRes();
-      do_prompt(ua, _("Select Catalog resource"), name, sizeof(name));
+      do_prompt(ua, _("Catalog"),  _("Select Catalog resource"), name, sizeof(name));
       catalog = (CAT *)GetResWithName(R_CATALOG, name);
    }
    return catalog;
@@ -184,7 +220,7 @@ JOB *select_job_resource(UAContext *ua)
       add_prompt(ua, job->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Job resource"), name, sizeof(name));
+   do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name));
    job = (JOB *)GetResWithName(R_JOB, name);
    return job;
 }
@@ -205,7 +241,7 @@ JOB *select_restore_job_resource(UAContext *ua)
       }
    }
    UnlockRes();
-   do_prompt(ua, _("Select Restore Job"), name, sizeof(name));
+   do_prompt(ua, _("Job"), _("Select Restore Job"), name, sizeof(name));
    job = (JOB *)GetResWithName(R_JOB, name);
    return job;
 }
@@ -226,7 +262,7 @@ CLIENT *select_client_resource(UAContext *ua)
       add_prompt(ua, client->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Client (File daemon) resource"), name, sizeof(name));
+   do_prompt(ua, _("Client"),  _("Select Client (File daemon) resource"), name, sizeof(name));
    client = (CLIENT *)GetResWithName(R_CLIENT, name);
    return client;
 }
@@ -242,7 +278,8 @@ CLIENT *get_client_resource(UAContext *ua)
    int i;
    
    for (i=1; i<ua->argc; i++) {
-      if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
+      if ((strcasecmp(ua->argk[i], _("client")) == 0 ||
+           strcasecmp(ua->argk[i], _("fd")) == 0) && ua->argv[i]) {
         client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
         if (client) {
            return client;
@@ -275,7 +312,8 @@ int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
       bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
    }
    for (i=1; i<ua->argc; i++) {
-      if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
+      if ((strcasecmp(ua->argk[i], _("client")) == 0 ||               
+           strcasecmp(ua->argk[i], _("fd")) == 0) && ua->argv[i]) {
         bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
         if (!db_get_client_record(ua->jcr, ua->db, cr)) {
             bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
@@ -311,7 +349,7 @@ int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
       return 0;
    }
    if (num_clients <= 0) {
-      bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
+      bsendmsg(ua, _("No clients defined. You must run a job before using this command.\n"));
       return 0;
    }
      
@@ -324,7 +362,7 @@ int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
       add_prompt(ua, ocr.Name);
    }
    free(ids);
-   if (do_prompt(ua, _("Select the Client"), name, sizeof(name)) < 0) {
+   if (do_prompt(ua, _("Client"),  _("Select the Client"), name, sizeof(name)) < 0) {
       return 0;
    }
    memset(&ocr, 0, sizeof(ocr));
@@ -352,26 +390,12 @@ int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
  */
 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
 {
-   int i;
-
    if (pr->Name[0]) {                /* If name already supplied */
       if (db_get_pool_record(ua->jcr, ua->db, pr)) {
         return pr->PoolId;
       }
       bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), pr->Name, db_strerror(ua->db));
    }
-   for (i=1; i<ua->argc; i++) {
-      if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
-        bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
-        if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
-            bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
-                    db_strerror(ua->db));
-           pr->PoolId = 0;
-           break;
-        }
-        return pr->PoolId;
-      }
-   }
    if (!select_pool_dbr(ua, pr)) {  /* try once more */
       return 0;
    }
@@ -388,6 +412,18 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
    int num_pools, i;
    uint32_t *ids; 
 
+   for (i=1; i<ua->argc; i++) {
+      if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
+        bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
+        if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
+            bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
+                    db_strerror(ua->db));
+           pr->PoolId = 0;
+           break;
+        }
+        return 1;
+      }
+   }
 
    pr->PoolId = 0;
    if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
@@ -408,7 +444,7 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
       add_prompt(ua, opr.Name);
    }
    free(ids);
-   if (do_prompt(ua, _("Select the Pool"), name, sizeof(name)) < 0) {
+   if (do_prompt(ua, _("Pool"),  _("Select the Pool"), name, sizeof(name)) < 0) {
       return 0;
    }
    memset(&opr, 0, sizeof(opr));
@@ -427,26 +463,39 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
  */
 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
 {
-   int i;
-   static char *kw[] = {
-      N_("volume"),
-      NULL};
 
+   if (!select_media_dbr(ua, mr)) {
+      return 0;
+   }
    memset(pr, 0, sizeof(POOL_DBR));
-   memset(mr, 0, sizeof(MEDIA_DBR));
-
-   /* Get the pool, possibly from pool=<pool-name> */
-   if (!get_pool_dbr(ua, pr)) {
+   pr->PoolId = mr->PoolId;
+   if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
+      bsendmsg(ua, "%s", db_strerror(ua->db));
       return 0;
    }
-   mr->PoolId = pr->PoolId;
+   return 1;
+}
+
+/* Select a Media (Volume) record from the database */
+int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
+{
+   int i;
 
-   i = find_arg_keyword(ua, kw);
-   if (i == 0 && ua->argv[i]) {
+   memset(mr, 0, sizeof(MEDIA_DBR));
+
+   i = find_arg_with_value(ua, "volume");
+   if (i >= 0) {
       bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
    }
    if (mr->VolumeName[0] == 0) {
-      db_list_media_records(ua->jcr, ua->db, mr, prtit, ua);
+      POOL_DBR pr;
+      memset(&pr, 0, sizeof(pr));
+      /* Get the pool from pool=<pool-name> */
+      if (!get_pool_dbr(ua, &pr)) {
+        return 0;
+      }
+      mr->PoolId = pr.PoolId;
+      db_list_media_records(ua->jcr, ua->db, mr, prtit, ua, HORZ_LIST);
       if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
         return 0;
       }
@@ -479,8 +528,9 @@ POOL *select_pool_resource(UAContext *ua)
       add_prompt(ua, pool->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Pool resource"), name, sizeof(name));
+   do_prompt(ua, _("Pool"), _("Select Pool resource"), name, sizeof(name));
    pool = (POOL *)GetResWithName(R_POOL, name);
+   return pool;
 }
 
 
@@ -492,18 +542,15 @@ POOL *select_pool_resource(UAContext *ua)
 POOL *get_pool_resource(UAContext *ua)
 {
    POOL *pool = NULL;
-   char name[MAX_NAME_LENGTH];
    int i;
    
-   for (i=1; i<ua->argc; i++) {
-      if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
-        pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
-        if (pool) {
-           return pool;
-        }
-         bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
-        break;
+   i = find_arg_with_value(ua, "pool");
+   if (i >= 0) {
+      pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
+      if (pool) {
+        return pool;
       }
+      bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
    }
    return select_pool_resource(ua);
 }
@@ -513,11 +560,11 @@ POOL *get_pool_resource(UAContext *ua)
  */
 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
 {
-   db_list_job_records(ua->jcr, ua->db, jr, prtit, ua);
-   if (!get_cmd(ua, _("Enter the JobId to select: "))) {
+   db_list_job_records(ua->jcr, ua->db, jr, prtit, ua, HORZ_LIST);
+   if (!get_pint(ua, _("Enter the JobId to select: "))) {
       return 0;
    }
-   jr->JobId = atoi(ua->cmd);
+   jr->JobId = ua->pint32_val;
    if (!db_get_job_record(ua->jcr, ua->db, jr)) {
       bsendmsg(ua, "%s", db_strerror(ua->db));
       return 0;
@@ -604,11 +651,19 @@ void add_prompt(UAContext *ua, char *prompt)
  *           index base 0 on success, and choice
  *              is copied to prompt if not NULL
  */
-int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
+int do_prompt(UAContext *ua, char *automsg, char *msg, char *prompt, int max_prompt)
 {
    int i, item;
    char pmsg[MAXSTRING];
 
+   if (ua->num_prompts == 2) {
+      item = 1;
+      if (prompt) {
+        bstrncpy(prompt, ua->prompt[1], max_prompt);
+      }
+      bsendmsg(ua, _("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
+      goto done;
+   }
    bsendmsg(ua, ua->prompt[0]);
    for (i=1; i < ua->num_prompts; i++) {
       bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
@@ -621,6 +676,7 @@ int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
    for ( ;; ) {
       /* First item is the prompt string, not the items */
       if (ua->num_prompts == 1) { 
+         bsendmsg(ua, _("Selection is empty!\n"));
         item = 0;                    /* list is empty ! */
         break;
       }
@@ -635,12 +691,12 @@ int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
          sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
       }
       /* Either a . or an @ will get you out of the loop */
-      if (!get_cmd(ua, pmsg) || *ua->cmd == '.' || *ua->cmd == '@') {
+      if (!get_pint(ua, pmsg)) {
         item = -1;                   /* error */
          bsendmsg(ua, _("Selection aborted, nothing done.\n"));
         break;
       }
-      item = atoi(ua->cmd);
+      item = ua->pint32_val;
       if (item < 1 || item >= ua->num_prompts) {
          bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
         continue;
@@ -651,6 +707,7 @@ int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
       break;
    }
                              
+done:
    for (i=0; i < ua->num_prompts; i++) {
       free(ua->prompt[i]);
    }
@@ -661,46 +718,45 @@ int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
 
 /*
  * We scan what the user has entered looking for
- *    <storage-resource>
- *    device=<device-name>     ???? does this work ????
  *    storage=<storage-resource>
  *    job=<job_name>
  *    jobid=<jobid>
  *    ?             (prompt him with storage list)
  *    <some-error>   (prompt him with storage list)
+ *
+ * If use_default is set, we assume that any keyword without a value
+ *   is the name of the Storage resource wanted.
  */
-STORE *get_storage_resource(UAContext *ua, char *cmd)
+STORE *get_storage_resource(UAContext *ua, int use_default)
 {
-   char *store_name, *device_name;
-   STORE *store;
+   char *store_name = NULL;
+   STORE *store = NULL;
    int jobid;
    JCR *jcr;
    int i;
       
-   if (ua->argc == 1) {
-      return select_storage_resource(ua);
-   }
-   
-   device_name = NULL;
-   store_name = NULL;
 
    for (i=1; i<ua->argc; i++) {
-      if (!ua->argv[i]) {
+      if (use_default && !ua->argv[i]) {
         /* Default argument is storage */
         if (store_name) {
             bsendmsg(ua, _("Storage name given twice.\n"));
            return NULL;
         }
+        /* Ignore barcode(s) keywords */
+         if (strncasecmp("barcode", ua->argk[i], 7) == 0) {
+           continue;
+        }
         store_name = ua->argk[i];
          if (*store_name == '?') {
-           return select_storage_resource(ua);
+           *store_name = 0;
+           break;
         }
       } else {
-         if (strcasecmp(ua->argk[i], _("device")) == 0) {
-           device_name = ua->argv[i];
-
-         } else if (strcasecmp(ua->argk[i], _("storage")) == 0) {
+         if (strcasecmp(ua->argk[i], _("storage")) == 0 ||
+             strcasecmp(ua->argk[i], _("sd")) == 0) {
            store_name = ua->argv[i];
+           break;
 
          } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
            jobid = atoi(ua->argv[i]);
@@ -714,7 +770,7 @@ STORE *get_storage_resource(UAContext *ua, char *cmd)
            }
            store = jcr->store;
            free_jcr(jcr);
-           return store;
+           break;
 
          } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
            if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
@@ -723,24 +779,18 @@ STORE *get_storage_resource(UAContext *ua, char *cmd)
            }
            store = jcr->store;
            free_jcr(jcr);
-           return store;
-
-        } else {
-            bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
-           return NULL;
-        }
+           break;
+       }
       }
    }
 
-   if (!store_name) {
-     bsendmsg(ua, _("A storage device name must be given.\n"));
-     store = NULL;
-   } else {
+   if (!store && store_name) {
       store = (STORE *)GetResWithName(R_STORAGE, store_name);
       if (!store) {
          bsendmsg(ua, "Storage resource %s: not found\n", store_name);
       }
    }
+   /* No keywords found, so present a selection list */
    if (!store) {
       store = select_storage_resource(ua);
    }
@@ -760,12 +810,9 @@ int get_media_type(UAContext *ua, char *MediaType, int max_media)
 {
    STORE *store;
    int i;
-   static char *keyword[] = {
-      "mediatype",
-      NULL};
 
-   i = find_arg_keyword(ua, keyword);
-   if (i >= 0 && ua->argv[i]) {
+   i = find_arg_with_value(ua, "mediatype");
+   if (i >= 0) {
       bstrncpy(MediaType, ua->argv[i], max_media);
       return 1;
    }
@@ -776,5 +823,5 @@ int get_media_type(UAContext *ua, char *MediaType, int max_media)
       add_prompt(ua, store->media_type);
    }
    UnlockRes();
-   return (do_prompt(ua, _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;
+   return (do_prompt(ua, _("Media Type"), _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;
 }