3 * Bacula Director -- User Agent Prompt and Selection code
5 * Kern Sibbald, October MMI
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Imported variables */
37 * Confirm a retention period
39 int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
44 bsendmsg(ua, _("The current %s retention period is: %s\n"),
45 msg, edit_utime(*ret, ed1));
46 if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
49 if (strcasecmp(ua->cmd, _("mod")) == 0) {
50 if (!get_cmd(ua, _("Enter new retention period: "))) {
53 if (!duration_to_utime(ua->cmd, ret)) {
54 bsendmsg(ua, _("Invalid period.\n"));
59 if (strcasecmp(ua->cmd, _("yes")) == 0) {
67 * Given a list of keywords, find the first one
68 * that is in the argument list.
69 * Returns: -1 if not found
70 * index into list (base 0) on success
72 int find_arg_keyword(UAContext *ua, char **list)
75 for (i=1; i<ua->argc; i++) {
76 for(j=0; list[j]; j++) {
77 if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
86 * Given a single keyword, find it in the argument list.
87 * Returns: -1 if not found
88 * list index (base 0) on success
90 int find_arg(UAContext *ua, char *keyword)
92 for (int i=1; i<ua->argc; i++) {
93 if (strcasecmp(keyword, ua->argk[i]) == 0) {
102 * Given a list of keywords, prompt the user
105 * Returns: -1 on failure
106 * index into list (base 0) on success
108 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
111 start_prompt(ua, _("You have the following choices:\n"));
112 for (i=0; list[i]; i++) {
113 add_prompt(ua, list[i]);
115 return do_prompt(ua, msg, NULL, 0);
120 * Select a Storage resource from prompt list
122 STORE *select_storage_resource(UAContext *ua)
124 char name[MAX_NAME_LENGTH];
127 start_prompt(ua, _("The defined Storage resources are:\n"));
129 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
130 add_prompt(ua, store->hdr.name);
133 do_prompt(ua, _("Select Storage resource"), name, sizeof(name));
134 store = (STORE *)GetResWithName(R_STORAGE, name);
139 * Select a FileSet resource from prompt list
141 FILESET *select_fileset_resource(UAContext *ua)
143 char name[MAX_NAME_LENGTH];
146 start_prompt(ua, _("The defined FileSet resources are:\n"));
148 while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
149 add_prompt(ua, fs->hdr.name);
152 do_prompt(ua, _("Select FileSet resource"), name, sizeof(name));
153 fs = (FILESET *)GetResWithName(R_FILESET, name);
159 * Get a catalog resource from prompt list
161 CAT *get_catalog_resource(UAContext *ua)
163 char name[MAX_NAME_LENGTH];
167 for (i=1; i<ua->argc; i++) {
168 if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
169 catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
174 start_prompt(ua, _("The defined Catalog resources are:\n"));
176 while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
177 add_prompt(ua, catalog->hdr.name);
180 do_prompt(ua, _("Select Catalog resource"), name, sizeof(name));
181 catalog = (CAT *)GetResWithName(R_CATALOG, name);
188 * Select a Job resource from prompt list
190 JOB *select_job_resource(UAContext *ua)
192 char name[MAX_NAME_LENGTH];
195 start_prompt(ua, _("The defined Job resources are:\n"));
197 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
198 add_prompt(ua, job->hdr.name);
201 do_prompt(ua, _("Select Job resource"), name, sizeof(name));
202 job = (JOB *)GetResWithName(R_JOB, name);
207 * Select a Restore Job resource from prompt list
209 JOB *select_restore_job_resource(UAContext *ua)
211 char name[MAX_NAME_LENGTH];
214 start_prompt(ua, _("The defined Restore Job resources are:\n"));
216 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
217 if (job->JobType == JT_RESTORE) {
218 add_prompt(ua, job->hdr.name);
222 do_prompt(ua, _("Select Restore Job"), name, sizeof(name));
223 job = (JOB *)GetResWithName(R_JOB, name);
230 * Select a client resource from prompt list
232 CLIENT *select_client_resource(UAContext *ua)
234 char name[MAX_NAME_LENGTH];
235 CLIENT *client = NULL;
237 start_prompt(ua, _("The defined Client resources are:\n"));
239 while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
240 add_prompt(ua, client->hdr.name);
243 do_prompt(ua, _("Select Client (File daemon) resource"), name, sizeof(name));
244 client = (CLIENT *)GetResWithName(R_CLIENT, name);
249 * Get client resource, start by looking for
250 * client=<client-name>
251 * if we don't find the keyword, we prompt the user.
253 CLIENT *get_client_resource(UAContext *ua)
255 CLIENT *client = NULL;
258 for (i=1; i<ua->argc; i++) {
259 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
260 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
264 bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
268 return select_client_resource(ua);
271 /* Scan what the user has entered looking for:
273 * client=<client-name>
275 * if error or not found, put up a list of client DBRs
278 * returns: 0 on error
279 * 1 on success and fills in CLIENT_DBR
281 int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
285 if (cr->Name[0]) { /* If name already supplied */
286 if (db_get_client_record(ua->jcr, ua->db, cr)) {
289 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
291 for (i=1; i<ua->argc; i++) {
292 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
293 bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
294 if (!db_get_client_record(ua->jcr, ua->db, cr)) {
295 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
296 db_strerror(ua->db));
303 if (!select_client_dbr(ua, cr)) { /* try once more by proposing a list */
310 * Select a Client record from the catalog
311 * Returns 1 on success
314 int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
317 char name[MAX_NAME_LENGTH];
323 if (!db_get_client_ids(ua->jcr, ua->db, &num_clients, &ids)) {
324 bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
327 if (num_clients <= 0) {
328 bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
332 start_prompt(ua, _("Defined Clients:\n"));
333 for (i=0; i < num_clients; i++) {
334 ocr.ClientId = ids[i];
335 if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
338 add_prompt(ua, ocr.Name);
341 if (do_prompt(ua, _("Select the Client"), name, sizeof(name)) < 0) {
344 memset(&ocr, 0, sizeof(ocr));
345 bstrncpy(ocr.Name, name, sizeof(ocr.Name));
347 if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
348 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
351 memcpy(cr, &ocr, sizeof(ocr));
357 /* Scan what the user has entered looking for:
361 * if error or not found, put up a list of pool DBRs
364 * returns: 0 on error
365 * 1 on success and fills in POOL_DBR
367 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
369 if (pr->Name[0]) { /* If name already supplied */
370 if (db_get_pool_record(ua->jcr, ua->db, pr)) {
373 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), pr->Name, db_strerror(ua->db));
375 if (!select_pool_dbr(ua, pr)) { /* try once more */
382 * Select a Pool record from the catalog
384 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
387 char name[MAX_NAME_LENGTH];
391 for (i=1; i<ua->argc; i++) {
392 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
393 bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
394 if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
395 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
396 db_strerror(ua->db));
405 if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
406 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
409 if (num_pools <= 0) {
410 bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
414 start_prompt(ua, _("Defined Pools:\n"));
415 for (i=0; i < num_pools; i++) {
417 if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
420 add_prompt(ua, opr.Name);
423 if (do_prompt(ua, _("Select the Pool"), name, sizeof(name)) < 0) {
426 memset(&opr, 0, sizeof(opr));
427 bstrncpy(opr.Name, name, sizeof(opr.Name));
429 if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
430 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
433 memcpy(pr, &opr, sizeof(opr));
438 * Select a Pool and a Media (Volume) record from the database
440 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
443 if (!select_media_dbr(ua, mr)) {
446 memset(pr, 0, sizeof(POOL_DBR));
447 pr->PoolId = mr->PoolId;
448 if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
449 bsendmsg(ua, "%s", db_strerror(ua->db));
455 /* Select a Media (Volume) record from the database */
456 int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
460 memset(mr, 0, sizeof(MEDIA_DBR));
462 i = find_arg(ua, "volume");
463 if (i >= 0 && ua->argv[i]) {
464 bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
466 if (mr->VolumeName[0] == 0) {
468 memset(&pr, 0, sizeof(pr));
469 /* Get the pool from pool=<pool-name> */
470 if (!get_pool_dbr(ua, &pr)) {
473 mr->PoolId = pr.PoolId;
474 db_list_media_records(ua->jcr, ua->db, mr, prtit, ua, 0);
475 if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
478 if (is_a_number(ua->cmd)) {
479 mr->MediaId = atoi(ua->cmd);
481 bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
485 if (!db_get_media_record(ua->jcr, ua->db, mr)) {
486 bsendmsg(ua, "%s", db_strerror(ua->db));
494 * Select a pool resource from prompt list
496 POOL *select_pool_resource(UAContext *ua)
498 char name[MAX_NAME_LENGTH];
501 start_prompt(ua, _("The defined Pool resources are:\n"));
503 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
504 add_prompt(ua, pool->hdr.name);
507 do_prompt(ua, _("Select Pool resource"), name, sizeof(name));
508 pool = (POOL *)GetResWithName(R_POOL, name);
514 * If you are thinking about using it, you
515 * probably want to use select_pool_dbr()
516 * or get_pool_dbr() above.
518 POOL *get_pool_resource(UAContext *ua)
523 for (i=1; i<ua->argc; i++) {
524 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
525 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
529 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
533 return select_pool_resource(ua);
537 * List all jobs and ask user to select one
539 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
541 db_list_job_records(ua->jcr, ua->db, jr, prtit, ua, 0);
542 if (!get_cmd(ua, _("Enter the JobId to select: "))) {
545 jr->JobId = atoi(ua->cmd);
546 if (!db_get_job_record(ua->jcr, ua->db, jr)) {
547 bsendmsg(ua, "%s", db_strerror(ua->db));
555 /* Scan what the user has entered looking for:
559 * if error or not found, put up a list of Jobs
562 * returns: 0 on error
563 * JobId on success and fills in JOB_DBR
565 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
569 for (i=1; i<ua->argc; i++) {
570 if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
572 bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
573 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
574 jr->JobId = atoi(ua->argv[i]);
578 if (!db_get_job_record(ua->jcr, ua->db, jr)) {
579 bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
580 db_strerror(ua->db));
587 if (!select_job_dbr(ua, jr)) { /* try once more */
594 * Implement unique set of prompts
596 void start_prompt(UAContext *ua, char *msg)
598 if (ua->max_prompts == 0) {
599 ua->max_prompts = 10;
600 ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
603 ua->prompt[0] = bstrdup(msg);
607 * Add to prompts -- keeping them unique
609 void add_prompt(UAContext *ua, char *prompt)
612 if (ua->num_prompts == ua->max_prompts) {
613 ua->max_prompts *= 2;
614 ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
617 for (i=1; i < ua->num_prompts; i++) {
618 if (strcmp(ua->prompt[i], prompt) == 0) {
622 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
626 * Display prompts and get user's choice
628 * Returns: -1 on error
629 * index base 0 on success, and choice
630 * is copied to prompt if not NULL
632 int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
635 char pmsg[MAXSTRING];
637 bsendmsg(ua, ua->prompt[0]);
638 for (i=1; i < ua->num_prompts; i++) {
639 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
647 /* First item is the prompt string, not the items */
648 if (ua->num_prompts == 1) {
649 bsendmsg(ua, _("Selection is empty!\n"));
650 item = 0; /* list is empty ! */
653 if (ua->num_prompts == 2) {
655 bsendmsg(ua, _("Item 1 selected automatically.\n"));
657 bstrncpy(prompt, ua->prompt[1], max_prompt);
661 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
663 /* Either a . or an @ will get you out of the loop */
664 if (!get_cmd(ua, pmsg) || *ua->cmd == '.' || *ua->cmd == '@') {
665 item = -1; /* error */
666 bsendmsg(ua, _("Selection aborted, nothing done.\n"));
669 item = atoi(ua->cmd);
670 if (item < 1 || item >= ua->num_prompts) {
671 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
675 bstrncpy(prompt, ua->prompt[item], max_prompt);
680 for (i=0; i < ua->num_prompts; i++) {
689 * We scan what the user has entered looking for
690 * storage=<storage-resource>
693 * ? (prompt him with storage list)
694 * <some-error> (prompt him with storage list)
696 * If use_default is set, we assume that any keyword without a value
697 * is the name of the Storage resource wanted.
699 STORE *get_storage_resource(UAContext *ua, int use_default)
701 char *store_name = NULL;
708 for (i=1; i<ua->argc; i++) {
709 if (use_default && !ua->argv[i]) {
710 /* Default argument is storage */
712 bsendmsg(ua, _("Storage name given twice.\n"));
715 /* Ignore barcode(s) keywords */
716 if (strncasecmp("barcode", ua->argk[i], 7) == 0) {
719 store_name = ua->argk[i];
720 if (*store_name == '?') {
725 if (strcasecmp(ua->argk[i], _("storage")) == 0) {
726 store_name = ua->argv[i];
729 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
730 jobid = atoi(ua->argv[i]);
732 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
735 if (!(jcr=get_jcr_by_id(jobid))) {
736 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
743 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
744 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
745 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
755 if (!store && store_name) {
756 store = (STORE *)GetResWithName(R_STORAGE, store_name);
758 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
761 /* No keywords found, so present a selection list */
763 store = select_storage_resource(ua);
770 * Scan looking for mediatype=
772 * if not found or error, put up selection list
774 * Returns: 0 on error
775 * 1 on success, MediaType is set
777 int get_media_type(UAContext *ua, char *MediaType, int max_media)
782 i = find_arg(ua, "mediatype");
783 if (i >= 0 && ua->argv[i]) {
784 bstrncpy(MediaType, ua->argv[i], max_media);
788 start_prompt(ua, _("Media Types defined in conf file:\n"));
790 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
791 add_prompt(ua, store->media_type);
794 return (do_prompt(ua, _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;