]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_select.c
Fix newvol.c
[bacula/bacula] / bacula / src / dird / ua_select.c
index 60d8a3739a4390f5c0d13d4e72469e80119021d2..5b15d69f68bcff0acc552ecb1fcf1f0ed747244c 100644 (file)
@@ -3,6 +3,8 @@
  *   Bacula Director -- User Agent Prompt and Selection code
  *
  *     Kern Sibbald, October MMI
+ *
+ *   Version  $Id$
  */
 
 /*
 /* Imported variables */
 
 
-/* Exported functions */
+/*
+ * Confirm a retention period
+ */
+int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
+{
+   char ed1[30];
 
-int do_prompt(UAContext *ua, char *msg, char *prompt);
-void add_prompt(UAContext *ua, char *prompt);
-void start_prompt(UAContext *ua, char *msg);
-STORE *select_storage_resource(UAContext *ua);
-JOB *select_job_resource(UAContext *ua);
+   for ( ;; ) {
+       bsendmsg(ua, _("The current %s retention period is: %s\n"), 
+         msg, edit_utime(*ret, ed1));
+       if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
+         return 0;
+       }
+       if (strcasecmp(ua->cmd, _("mod")) == 0) {
+          if (!get_cmd(ua, _("Enter new retention period: "))) {
+            return 0;
+         }
+         if (!duration_to_utime(ua->cmd, ret)) {
+             bsendmsg(ua, _("Invalid period.\n"));
+            continue;
+         }
+         continue;
+       }
+       if (strcasecmp(ua->cmd, _("yes")) == 0) {
+         break;
+       }
+    }
+    return 1;
+}
 
 /* 
  * Given a list of keywords, find the first one
@@ -74,7 +98,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);
+   return do_prompt(ua, msg, NULL, 0);
 }
 
 
@@ -92,7 +116,7 @@ STORE *select_storage_resource(UAContext *ua)
       add_prompt(ua, store->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Storage resource"), name);
+   do_prompt(ua, _("Select Storage resource"), name, sizeof(name));
    store = (STORE *)GetResWithName(R_STORAGE, name);
    return store;
 }
@@ -100,7 +124,7 @@ STORE *select_storage_resource(UAContext *ua)
 /* 
  * Select a FileSet resource from prompt list
  */
-FILESET *select_fs_resource(UAContext *ua)
+FILESET *select_fileset_resource(UAContext *ua)
 {
    char name[MAX_NAME_LENGTH];   
    FILESET *fs = NULL;
@@ -111,7 +135,7 @@ FILESET *select_fs_resource(UAContext *ua)
       add_prompt(ua, fs->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select FileSet resource"), name);
+   do_prompt(ua, _("Select FileSet resource"), name, sizeof(name));
    fs = (FILESET *)GetResWithName(R_FILESET, name);
    return fs;
 }
@@ -139,7 +163,7 @@ CAT *get_catalog_resource(UAContext *ua)
         add_prompt(ua, catalog->hdr.name);
       }
       UnlockRes();
-      do_prompt(ua, _("Select Catalog resource"), name);
+      do_prompt(ua, _("Select Catalog resource"), name, sizeof(name));
       catalog = (CAT *)GetResWithName(R_CATALOG, name);
    }
    return catalog;
@@ -160,11 +184,33 @@ JOB *select_job_resource(UAContext *ua)
       add_prompt(ua, job->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Job resource"), name);
+   do_prompt(ua, _("Select Job resource"), name, sizeof(name));
    job = (JOB *)GetResWithName(R_JOB, name);
    return job;
 }
 
+/* 
+ * Select a Restore Job resource from prompt list
+ */
+JOB *select_restore_job_resource(UAContext *ua)
+{
+   char name[MAX_NAME_LENGTH];   
+   JOB *job = NULL;
+
+   start_prompt(ua, _("The defined Restore Job resources are:\n"));
+   LockRes();
+   while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
+      if (job->JobType == JT_RESTORE) {
+        add_prompt(ua, job->hdr.name);
+      }
+   }
+   UnlockRes();
+   do_prompt(ua, _("Select Restore Job"), name, sizeof(name));
+   job = (JOB *)GetResWithName(R_JOB, name);
+   return job;
+}
+
+
 
 /* 
  * Select a client resource from prompt list
@@ -180,7 +226,7 @@ CLIENT *select_client_resource(UAContext *ua)
       add_prompt(ua, client->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Client (File daemon) resource"), name);
+   do_prompt(ua, _("Select Client (File daemon) resource"), name, sizeof(name));
    client = (CLIENT *)GetResWithName(R_CLIENT, name);
    return client;
 }
@@ -208,6 +254,92 @@ CLIENT *get_client_resource(UAContext *ua)
    return select_client_resource(ua);
 }
 
+/* Scan what the user has entered looking for:
+ * 
+ *  client=<client-name>
+ *
+ *  if error or not found, put up a list of client DBRs
+ *  to choose from.
+ *
+ *   returns: 0 on error
+ *           1 on success and fills in CLIENT_DBR
+ */
+int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
+{
+   int i;
+
+   if (cr->Name[0]) {                /* If name already supplied */
+      if (db_get_client_record(ua->db, cr)) {
+        return 1;
+      }
+      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]) {
+        bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
+        if (!db_get_client_record(ua->db, cr)) {
+            bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
+                    db_strerror(ua->db));
+           cr->ClientId = 0;
+           break;
+        }
+        return 1;
+      }
+   }
+   if (!select_client_dbr(ua, cr)) {  /* try once more by proposing a list */
+      return 0;
+   }
+   return 1;
+}
+
+/*
+ * Select a Client record from the catalog
+ *  Returns 1 on success
+ *         0 on failure
+ */
+int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
+{
+   CLIENT_DBR ocr;
+   char name[MAX_NAME_LENGTH];
+   int num_clients, i;
+   uint32_t *ids; 
+
+
+   cr->ClientId = 0;
+   if (!db_get_client_ids(ua->db, &num_clients, &ids)) {
+      bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
+      return 0;
+   }
+   if (num_clients <= 0) {
+      bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
+      return 0;
+   }
+     
+   start_prompt(ua, _("Defined Clients:\n"));
+   for (i=0; i < num_clients; i++) {
+      ocr.ClientId = ids[i];
+      if (!db_get_client_record(ua->db, &ocr)) {
+        continue;
+      }
+      add_prompt(ua, ocr.Name);
+   }
+   free(ids);
+   if (do_prompt(ua, _("Select the Client"), name, sizeof(name)) < 0) {
+      return 0;
+   }
+   memset(&ocr, 0, sizeof(ocr));
+   bstrncpy(ocr.Name, name, sizeof(ocr.Name));
+
+   if (!db_get_client_record(ua->db, &ocr)) {
+      bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
+      return 0;
+   }
+   memcpy(cr, &ocr, sizeof(ocr));
+   return 1;
+}
+
+
+
 /* Scan what the user has entered looking for:
  * 
  *  pool=<pool-name>   
@@ -216,15 +348,21 @@ CLIENT *get_client_resource(UAContext *ua)
  *  to choose from.
  *
  *   returns: 0 on error
- *           poolid on success and fills in POOL_DBR
+ *           1 on success and fills in POOL_DBR
  */
 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->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]) {
-        strcpy(pr->Name, ua->argv[i]);
+        bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
         if (!db_get_pool_record(ua->db, pr)) {
             bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
                     db_strerror(ua->db));
@@ -237,7 +375,7 @@ int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
    if (!select_pool_dbr(ua, pr)) {  /* try once more */
       return 0;
    }
-   return pr->PoolId;
+   return 1;
 }
 
 /*
@@ -270,18 +408,18 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
       add_prompt(ua, opr.Name);
    }
    free(ids);
-   if (do_prompt(ua, _("Select the Pool"), name) < 0) {
+   if (do_prompt(ua, _("Select the Pool"), name, sizeof(name)) < 0) {
       return 0;
    }
-   memset(&opr, 0, sizeof(pr));
-   strcpy(opr.Name, name);
+   memset(&opr, 0, sizeof(opr));
+   bstrncpy(opr.Name, name, sizeof(opr.Name));
 
    if (!db_get_pool_record(ua->db, &opr)) {
       bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
       return 0;
    }
    memcpy(pr, &opr, sizeof(opr));
-   return opr.PoolId;
+   return 1;
 }
 
 /*
@@ -289,8 +427,10 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
  */
 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
 {
-   int found = FALSE;
    int i;
+   static char *kw[] = {
+      N_("volume"),
+      NULL};
 
    memset(pr, 0, sizeof(POOL_DBR));
    memset(mr, 0, sizeof(MEDIA_DBR));
@@ -301,23 +441,22 @@ int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
    }
    mr->PoolId = pr->PoolId;
 
-   /* See if a volume name is specified as an argument */
-   for (i=1; i<ua->argc; i++) {
-      if (strcasecmp(ua->argk[i], _("volume")) == 0 && ua->argv[i]) {
-        found = TRUE;
-        break;
-      }
+   i = find_arg_keyword(ua, kw);
+   if (i == 0 && ua->argv[i]) {
+      bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
    }
-   if (found) {
-      strcpy(mr->VolumeName, ua->argv[i]);
-   } else {
+   if (mr->VolumeName[0] == 0) {
       db_list_media_records(ua->db, mr, prtit, ua);
-      if (!get_cmd(ua, _("Enter the Volume name to delete: "))) {
-        return 01;
+      if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
+        return 0;
+      }
+      if (is_a_number(ua->cmd)) {
+        mr->MediaId = atoi(ua->cmd);
+      } else {
+        bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
       }
-      strcpy(mr->VolumeName, ua->cmd);
    }
-   mr->MediaId = 0;
+
    if (!db_get_media_record(ua->db, mr)) {
       bsendmsg(ua, "%s", db_strerror(ua->db));
       return 0;
@@ -354,7 +493,7 @@ POOL *get_pool_resource(UAContext *ua)
       add_prompt(ua, pool->hdr.name);
    }
    UnlockRes();
-   do_prompt(ua, _("Select Pool resource"), name);
+   do_prompt(ua, _("Select Pool resource"), name, sizeof(name));
    pool = (POOL *)GetResWithName(R_POOL, name);
    return pool;
 }
@@ -395,7 +534,7 @@ int get_job_dbr(UAContext *ua, JOB_DBR *jr)
    for (i=1; i<ua->argc; i++) {
       if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
         jr->JobId = 0;
-        strcpy(jr->Job, ua->argv[i]);
+        bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
       } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
         jr->JobId = atoi(ua->argv[i]);
       } else {
@@ -416,9 +555,6 @@ int get_job_dbr(UAContext *ua, JOB_DBR *jr)
    return jr->JobId;
 }
 
-
-
-
 /*
  * Implement unique set of prompts 
  */
@@ -426,7 +562,7 @@ void start_prompt(UAContext *ua, char *msg)
 {
   if (ua->max_prompts == 0) {
      ua->max_prompts = 10;
-     ua->prompt = (char **) bmalloc(sizeof(char *) * ua->max_prompts);
+     ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
   }
   ua->num_prompts = 1;
   ua->prompt[0] = bstrdup(msg);
@@ -440,7 +576,7 @@ void add_prompt(UAContext *ua, char *prompt)
    int i;
    if (ua->num_prompts == ua->max_prompts) {
       ua->max_prompts *= 2;
-      ua->prompt = (char **) brealloc(ua->prompt, sizeof(char *) *
+      ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
         ua->max_prompts);
     }
     for (i=1; i < ua->num_prompts; i++) {
@@ -458,7 +594,7 @@ 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 do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
 {
    int i, item;
    char pmsg[MAXSTRING];
@@ -473,11 +609,16 @@ int do_prompt(UAContext *ua, char *msg, char *prompt)
    }
 
    for ( ;; ) {
+      /* First item is the prompt string, not the items */
+      if (ua->num_prompts == 1) { 
+        item = 0;                    /* list is empty ! */
+        break;
+      }
       if (ua->num_prompts == 2) {
         item = 1;
          bsendmsg(ua, _("Item 1 selected automatically.\n"));
         if (prompt) {
-           strcpy(prompt, ua->prompt[1]);
+           bstrncpy(prompt, ua->prompt[1], max_prompt);
         }
         break;
       } else {
@@ -486,6 +627,7 @@ int do_prompt(UAContext *ua, char *msg, char *prompt)
       /* Either a . or an @ will get you out of the loop */
       if (!get_cmd(ua, pmsg) || *ua->cmd == '.' || *ua->cmd == '@') {
         item = -1;                   /* error */
+         bsendmsg(ua, _("Selection aborted, nothing done.\n"));
         break;
       }
       item = atoi(ua->cmd);
@@ -494,7 +636,7 @@ int do_prompt(UAContext *ua, char *msg, char *prompt)
         continue;
       }
       if (prompt) {
-        strcpy(prompt, ua->prompt[item]);
+        bstrncpy(prompt, ua->prompt[item], max_prompt);
       }
       break;
    }
@@ -604,7 +746,7 @@ STORE *get_storage_resource(UAContext *ua, char *cmd)
  *  Returns: 0 on error
  *          1 on success, MediaType is set
  */
-int get_media_type(UAContext *ua, char *MediaType)
+int get_media_type(UAContext *ua, char *MediaType, int max_media)
 {
    STORE *store;
    int i;
@@ -614,7 +756,7 @@ int get_media_type(UAContext *ua, char *MediaType)
 
    i = find_arg_keyword(ua, keyword);
    if (i >= 0 && ua->argv[i]) {
-      strcpy(MediaType, ua->argv[i]);
+      bstrncpy(MediaType, ua->argv[i], max_media);
       return 1;
    }
 
@@ -624,5 +766,5 @@ int get_media_type(UAContext *ua, char *MediaType)
       add_prompt(ua, store->media_type);
    }
    UnlockRes();
-   return (do_prompt(ua, _("Select the Media Type"), MediaType) < 0) ? 0 : 1;
+   return (do_prompt(ua, _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;
 }