X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Fdird%2Fua_select.c;h=99df0d3a73a9827eed515bf4ef7cea5398424eff;hb=b8b2ed2a6db4fb8436647d438185a364951375fc;hp=5992c63f4957283b699943b58e9d8fcfd4cb3d26;hpb=c3009688855647dbb3e9997fc569963fd320783d;p=bacula%2Fbacula diff --git a/bacula/src/dird/ua_select.c b/bacula/src/dird/ua_select.c index 5992c63f49..99df0d3a73 100644 --- a/bacula/src/dird/ua_select.c +++ b/bacula/src/dird/ua_select.c @@ -4,11 +4,11 @@ * * Kern Sibbald, October MMI * - * $Id$ + * Version $Id$ */ /* - Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker + Copyright (C) 2000-2003 Kern Sibbald and John Walker This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -29,30 +29,20 @@ #include "bacula.h" #include "dird.h" -#include "ua.h" - /* Imported variables */ -/* Exported functions */ - -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); - /* * Confirm a retention period */ -int confirm_retention(UAContext *ua, btime_t *ret, char *msg) +int confirm_retention(UAContext *ua, utime_t *ret, char *msg) { char ed1[30]; for ( ;; ) { bsendmsg(ua, _("The current %s retention period is: %s\n"), - msg, edit_btime(*ret, ed1)); + msg, edit_utime(*ret, ed1)); if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) { return 0; } @@ -60,14 +50,17 @@ int confirm_retention(UAContext *ua, btime_t *ret, char *msg) if (!get_cmd(ua, _("Enter new retention period: "))) { return 0; } - if (!string_to_btime(ua->cmd, ret)) { + if (!duration_to_utime(ua->cmd, ret)) { bsendmsg(ua, _("Invalid period.\n")); continue; } continue; } if (strcasecmp(ua->cmd, _("yes")) == 0) { - break; + return 1; + } + if (strcasecmp(ua->cmd, _("no")) == 0) { + return 0; } } return 1; @@ -81,9 +74,8 @@ int confirm_retention(UAContext *ua, btime_t *ret, char *msg) */ int find_arg_keyword(UAContext *ua, char **list) { - int i, j; - for (i=1; iargc; i++) { - for(j=0; list[j]; j++) { + for (int i=1; iargc; i++) { + for(int j=0; list[j]; j++) { if (strcasecmp(_(list[j]), ua->argk[i]) == 0) { return j; } @@ -92,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; iargc; 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; iargc; 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. @@ -106,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); + return do_prompt(ua, "", msg, NULL, 0); } @@ -116,15 +144,17 @@ int do_keyword_prompt(UAContext *ua, char *msg, char **list) STORE *select_storage_resource(UAContext *ua) { char name[MAX_NAME_LENGTH]; - STORE *store = NULL; + STORE *store; start_prompt(ua, _("The defined Storage resources are:\n")); LockRes(); - while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) { - add_prompt(ua, store->hdr.name); + foreach_res(store, R_STORAGE) { + if (acl_access_ok(ua, Storage_ACL, store->hdr.name)) { + add_prompt(ua, store->hdr.name); + } } UnlockRes(); - do_prompt(ua, _("Select Storage resource"), name); + do_prompt(ua, _("Storage"), _("Select Storage resource"), name, sizeof(name)); store = (STORE *)GetResWithName(R_STORAGE, name); return store; } @@ -132,18 +162,20 @@ 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; + FILESET *fs; start_prompt(ua, _("The defined FileSet resources are:\n")); LockRes(); - while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) { - add_prompt(ua, fs->hdr.name); + foreach_res(fs, R_FILESET) { + if (acl_access_ok(ua, FileSet_ACL, fs->hdr.name)) { + add_prompt(ua, fs->hdr.name); + } } UnlockRes(); - do_prompt(ua, _("Select FileSet resource"), name); + do_prompt(ua, _("FileSet"), _("Select FileSet resource"), name, sizeof(name)); fs = (FILESET *)GetResWithName(R_FILESET, name); return fs; } @@ -160,18 +192,22 @@ CAT *get_catalog_resource(UAContext *ua) for (i=1; iargc; i++) { if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) { - catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]); - break; + if (acl_access_ok(ua, Catalog_ACL, ua->argv[i])) { + catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]); + break; + } } } if (!catalog) { start_prompt(ua, _("The defined Catalog resources are:\n")); LockRes(); - while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) { - add_prompt(ua, catalog->hdr.name); + foreach_res(catalog, R_CATALOG) { + if (acl_access_ok(ua, Catalog_ACL, catalog->hdr.name)) { + add_prompt(ua, catalog->hdr.name); + } } UnlockRes(); - do_prompt(ua, _("Select Catalog resource"), name); + do_prompt(ua, _("Catalog"), _("Select Catalog resource"), name, sizeof(name)); catalog = (CAT *)GetResWithName(R_CATALOG, name); } return catalog; @@ -184,19 +220,43 @@ CAT *get_catalog_resource(UAContext *ua) JOB *select_job_resource(UAContext *ua) { char name[MAX_NAME_LENGTH]; - JOB *job = NULL; + JOB *job; start_prompt(ua, _("The defined Job resources are:\n")); LockRes(); - while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) { - add_prompt(ua, job->hdr.name); + foreach_res(job, R_JOB) { + if (acl_access_ok(ua, Job_ACL, job->hdr.name)) { + add_prompt(ua, job->hdr.name); + } } UnlockRes(); - do_prompt(ua, _("Select Job resource"), name); + do_prompt(ua, _("Job"), _("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; + + start_prompt(ua, _("The defined Restore Job resources are:\n")); + LockRes(); + foreach_res(job, R_JOB) { + if (job->JobType == JT_RESTORE && acl_access_ok(ua, Job_ACL, job->hdr.name)) { + add_prompt(ua, job->hdr.name); + } + } + UnlockRes(); + do_prompt(ua, _("Job"), _("Select Restore Job"), name, sizeof(name)); + job = (JOB *)GetResWithName(R_JOB, name); + return job; +} + + /* * Select a client resource from prompt list @@ -204,15 +264,17 @@ JOB *select_job_resource(UAContext *ua) CLIENT *select_client_resource(UAContext *ua) { char name[MAX_NAME_LENGTH]; - CLIENT *client = NULL; + CLIENT *client; start_prompt(ua, _("The defined Client resources are:\n")); LockRes(); - while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) { - add_prompt(ua, client->hdr.name); + foreach_res(client, R_CLIENT) { + if (acl_access_ok(ua, Client_ACL, client->hdr.name)) { + add_prompt(ua, client->hdr.name); + } } UnlockRes(); - do_prompt(ua, _("Select Client (File daemon) resource"), name); + do_prompt(ua, _("Client"), _("Select Client (File daemon) resource"), name, sizeof(name)); client = (CLIENT *)GetResWithName(R_CLIENT, name); return client; } @@ -228,7 +290,11 @@ CLIENT *get_client_resource(UAContext *ua) int i; for (i=1; iargc; 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]) { + if (!acl_access_ok(ua, Client_ACL, ua->argv[i])) { + break; + } client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]); if (client) { return client; @@ -242,34 +308,118 @@ CLIENT *get_client_resource(UAContext *ua) /* Scan what the user has entered looking for: * - * pool= + * client= * - * if error or not found, put up a list of pool DBRs + * if error or not found, put up a list of client DBRs * to choose from. * * returns: 0 on error - * poolid on success and fills in POOL_DBR + * 1 on success and fills in CLIENT_DBR */ -int get_pool_dbr(UAContext *ua, POOL_DBR *pr) +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->jcr, ua->db, cr)) { + return 1; + } + bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db)); + } for (i=1; iargc; i++) { - if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) { - strcpy(pr->Name, ua->argv[i]); - if (!db_get_pool_record(ua->db, pr)) { - bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i], + if ((strcasecmp(ua->argk[i], _("client")) == 0 || + strcasecmp(ua->argk[i], _("fd")) == 0) && ua->argv[i]) { + if (!acl_access_ok(ua, Client_ACL, ua->argv[i])) { + break; + } + 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], db_strerror(ua->db)); - pr->PoolId = 0; + 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->jcr, 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. You must run a job before using this command.\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->jcr, ua->db, &ocr) || + !acl_access_ok(ua, Client_ACL, ocr.Name)) { + continue; + } + add_prompt(ua, ocr.Name); + } + free(ids); + if (do_prompt(ua, _("Client"), _("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->jcr, 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= + * + * if error or not found, put up a list of pool DBRs + * to choose from. + * + * returns: 0 on error + * 1 on success and fills in POOL_DBR + */ +int get_pool_dbr(UAContext *ua, POOL_DBR *pr) +{ + if (pr->Name[0]) { /* If name already supplied */ + if (db_get_pool_record(ua->jcr, ua->db, pr) && + acl_access_ok(ua, Pool_ACL, pr->Name)) { return pr->PoolId; } + bsendmsg(ua, _("Could not find Pool \"%s\": ERR=%s"), pr->Name, db_strerror(ua->db)); } if (!select_pool_dbr(ua, pr)) { /* try once more */ return 0; } - return pr->PoolId; + return 1; } /* @@ -282,9 +432,22 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr) int num_pools, i; uint32_t *ids; + for (i=1; iargc; i++) { + if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i] && + acl_access_ok(ua, Pool_ACL, 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->db, &num_pools, &ids)) { + if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) { bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db)); return 0; } @@ -296,24 +459,25 @@ int select_pool_dbr(UAContext *ua, POOL_DBR *pr) start_prompt(ua, _("Defined Pools:\n")); for (i=0; i < num_pools; i++) { opr.PoolId = ids[i]; - if (!db_get_pool_record(ua->db, &opr)) { + if (!db_get_pool_record(ua->jcr, ua->db, &opr) || + !acl_access_ok(ua, Pool_ACL, opr.Name)) { continue; } add_prompt(ua, opr.Name); } free(ids); - if (do_prompt(ua, _("Select the Pool"), name) < 0) { + if (do_prompt(ua, _("Pool"), _("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)); + if (!db_get_pool_record(ua->jcr, 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; } /* @@ -321,36 +485,54 @@ 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; + 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= */ - 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; + } + if (!acl_access_ok(ua, Pool_ACL, pr->Name)) { + bsendmsg(ua, _("No access to Pool \"%s\"\n"), pr->Name); return 0; } - mr->PoolId = pr->PoolId; + return 1; +} - /* See if a volume name is specified as an argument */ - for (i=1; iargc; i++) { - if (strcasecmp(ua->argk[i], _("volume")) == 0 && ua->argv[i]) { - found = TRUE; - break; - } +/* Select a Media (Volume) record from the database */ +int select_media_dbr(UAContext *ua, MEDIA_DBR *mr) +{ + int 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 (found) { - strcpy(mr->VolumeName, ua->argv[i]); - } else { - db_list_media_records(ua->db, mr, prtit, ua); - if (!get_cmd(ua, _("Enter the Volume name to delete: "))) { - return 01; + if (mr->VolumeName[0] == 0) { + POOL_DBR pr; + memset(&pr, 0, sizeof(pr)); + /* Get the pool from pool= */ + 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; + } + 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)) { + + if (!db_get_media_record(ua->jcr, ua->db, mr)) { bsendmsg(ua, "%s", db_strerror(ua->db)); return 0; } @@ -358,8 +540,29 @@ int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr) } +/* + * Select a pool resource from prompt list + */ +POOL *select_pool_resource(UAContext *ua) +{ + char name[MAX_NAME_LENGTH]; + POOL *pool; + + start_prompt(ua, _("The defined Pool resources are:\n")); + LockRes(); + foreach_res(pool, R_POOL) { + if (acl_access_ok(ua, Pool_ACL, pool->hdr.name)) { + add_prompt(ua, pool->hdr.name); + } + } + UnlockRes(); + do_prompt(ua, _("Pool"), _("Select Pool resource"), name, sizeof(name)); + pool = (POOL *)GetResWithName(R_POOL, name); + return pool; +} + + /* - * This routine is ONLY used in the create command. * If you are thinking about using it, you * probably want to use select_pool_dbr() * or get_pool_dbr() above. @@ -367,28 +570,17 @@ int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr) POOL *get_pool_resource(UAContext *ua) { POOL *pool = NULL; - char name[MAX_NAME_LENGTH]; int i; - for (i=1; iargc; 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 && acl_access_ok(ua, Pool_ACL, 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]); } - start_prompt(ua, _("The defined Pool resources are:\n")); - LockRes(); - while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) { - add_prompt(ua, pool->hdr.name); - } - UnlockRes(); - do_prompt(ua, _("Select Pool resource"), name); - pool = (POOL *)GetResWithName(R_POOL, name); - return pool; + return select_pool_resource(ua); } /* @@ -396,12 +588,12 @@ POOL *get_pool_resource(UAContext *ua) */ int select_job_dbr(UAContext *ua, JOB_DBR *jr) { - db_list_job_records(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); - if (!db_get_job_record(ua->db, jr)) { + jr->JobId = ua->pint32_val; + if (!db_get_job_record(ua->jcr, ua->db, jr)) { bsendmsg(ua, "%s", db_strerror(ua->db)); return 0; } @@ -427,14 +619,14 @@ int get_job_dbr(UAContext *ua, JOB_DBR *jr) for (i=1; iargc; 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]); + jr->JobId = str_to_int64(ua->argv[i]); } else { continue; } - if (!db_get_job_record(ua->db, jr)) { - bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i], + if (!db_get_job_record(ua->jcr, ua->db, jr)) { + bsendmsg(ua, _("Could not find Job \"%s\": ERR=%s"), ua->argv[i], db_strerror(ua->db)); jr->JobId = 0; break; @@ -448,9 +640,6 @@ int get_job_dbr(UAContext *ua, JOB_DBR *jr) return jr->JobId; } - - - /* * Implement unique set of prompts */ @@ -458,7 +647,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); @@ -472,7 +661,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++) { @@ -490,11 +679,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 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]); @@ -505,32 +702,40 @@ int do_prompt(UAContext *ua, char *msg, char *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; + } 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 { 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; } if (prompt) { - strcpy(prompt, ua->prompt[item]); + bstrncpy(prompt, ua->prompt[item], max_prompt); } break; } +done: for (i=0; i < ua->num_prompts; i++) { free(ua->prompt[i]); } @@ -541,31 +746,31 @@ int do_prompt(UAContext *ua, char *msg, char *prompt) /* * We scan what the user has entered looking for - * - * device= ???? does this work ???? * storage= * job= * jobid= * ? (prompt him with storage list) * (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; iargc; i++) { - if (!ua->argv[i]) { + if (use_default && !ua->argv[i]) { + /* Ignore scan and barcode(s) keywords */ + if (strncasecmp("scan", ua->argk[i], 4) == 0 || + strncasecmp("barcode", ua->argk[i], 7) == 0) { + continue; + } /* Default argument is storage */ if (store_name) { bsendmsg(ua, _("Storage name given twice.\n")); @@ -573,17 +778,17 @@ STORE *get_storage_resource(UAContext *ua, char *cmd) } 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]); + jobid = str_to_int64(ua->argv[i]); if (jobid <= 0) { bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]); return NULL; @@ -594,33 +799,33 @@ 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]))) { - bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]); + bsendmsg(ua, _("Job \"%s\" is not running.\n"), ua->argv[i]); return NULL; } 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 && !acl_access_ok(ua, Storage_ACL, store->hdr.name)) { + store = NULL; + } + + if (!store && store_name) { store = (STORE *)GetResWithName(R_STORAGE, store_name); if (!store) { - bsendmsg(ua, "Storage resource %s: not found\n", store_name); + bsendmsg(ua, "Storage resource \"%s\": not found\n", store_name); } } + if (store && !acl_access_ok(ua, Storage_ACL, store->hdr.name)) { + store = NULL; + } + /* No keywords found, so present a selection list */ if (!store) { store = select_storage_resource(ua); } @@ -636,25 +841,22 @@ 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; - static char *keyword[] = { - "mediatype", - NULL}; - i = find_arg_keyword(ua, keyword); - if (i >= 0 && ua->argv[i]) { - strcpy(MediaType, ua->argv[i]); + i = find_arg_with_value(ua, "mediatype"); + if (i >= 0) { + bstrncpy(MediaType, ua->argv[i], max_media); return 1; } start_prompt(ua, _("Media Types defined in conf file:\n")); LockRes(); - for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) { + foreach_res(store, R_STORAGE) { add_prompt(ua, store->media_type); } UnlockRes(); - return (do_prompt(ua, _("Select the Media Type"), MediaType) < 0) ? 0 : 1; + return (do_prompt(ua, _("Media Type"), _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1; }