]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_select.c
f3afec0d056e8f8e5e52c4aa40c3bd20b6e2b3ed
[bacula/bacula] / bacula / src / dird / ua_select.c
1 /*
2  *
3  *   Bacula Director -- User Agent Prompt and Selection code
4  *
5  *     Kern Sibbald, October MMI
6  *
7  *   Version  $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
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.
17
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.
22
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,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 /* Imported variables */
34
35
36 /*
37  * Confirm a retention period
38  */
39 int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
40 {
41    char ed1[30];
42
43    for ( ;; ) {
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): "))) {
47           return 0;
48        }
49        if (strcasecmp(ua->cmd, _("mod")) == 0) {
50           if (!get_cmd(ua, _("Enter new retention period: "))) {
51              return 0;
52           }
53           if (!duration_to_utime(ua->cmd, ret)) {
54              bsendmsg(ua, _("Invalid period.\n"));
55              continue;
56           }
57           continue;
58        }
59        if (strcasecmp(ua->cmd, _("yes")) == 0) {
60           return 1;
61        }
62        if (strcasecmp(ua->cmd, _("no")) == 0) {
63           return 0;
64        }
65     }
66     return 1;
67 }
68
69 /* 
70  * Given a list of keywords, find the first one
71  *  that is in the argument list.
72  * Returns: -1 if not found
73  *          index into list (base 0) on success
74  */
75 int find_arg_keyword(UAContext *ua, char **list)
76 {
77    for (int i=1; i<ua->argc; i++) {
78       for(int j=0; list[j]; j++) {
79          if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
80             return j;
81          }
82       }
83    }
84    return -1;
85 }
86
87 int find_arg(UAContext *ua, char *keyword)
88 {
89    for (int i=1; i<ua->argc; i++) {
90       if (strcasecmp(keyword, ua->argk[i]) == 0) {
91          return i;
92       }
93    }
94    return -1;
95 }
96
97 /* 
98  * Given a single keyword, find it in the argument list, but
99  *   it must have a value
100  * Returns: -1 if not found or no value
101  *           list index (base 0) on success
102  */
103 int find_arg_with_value(UAContext *ua, char *keyword)
104 {
105    for (int i=1; i<ua->argc; i++) {
106       if (strcasecmp(keyword, ua->argk[i]) == 0) {
107          if (ua->argv[i]) {
108             return i;
109          } else {
110             return -1;
111          }
112       }
113    }
114    return -1;
115 }
116
117 /* 
118  * Given a list of keywords, prompt the user 
119  * to choose one.
120  *
121  * Returns: -1 on failure
122  *          index into list (base 0) on success
123  */
124 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
125 {
126    int i;
127    start_prompt(ua, _("You have the following choices:\n"));
128    for (i=0; list[i]; i++) {
129       add_prompt(ua, list[i]);
130    }
131    return do_prompt(ua, "", msg, NULL, 0);
132 }
133
134
135 /* 
136  * Select a Storage resource from prompt list
137  */
138 STORE *select_storage_resource(UAContext *ua)
139 {
140    char name[MAX_NAME_LENGTH];    
141    STORE *store = NULL;
142
143    start_prompt(ua, _("The defined Storage resources are:\n"));
144    LockRes();
145    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
146       add_prompt(ua, store->hdr.name);
147    }
148    UnlockRes();
149    do_prompt(ua, _("Storage"),  _("Select Storage resource"), name, sizeof(name));
150    store = (STORE *)GetResWithName(R_STORAGE, name);
151    return store;
152 }
153
154 /* 
155  * Select a FileSet resource from prompt list
156  */
157 FILESET *select_fileset_resource(UAContext *ua)
158 {
159    char name[MAX_NAME_LENGTH];    
160    FILESET *fs = NULL;
161
162    start_prompt(ua, _("The defined FileSet resources are:\n"));
163    LockRes();
164    while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
165       add_prompt(ua, fs->hdr.name);
166    }
167    UnlockRes();
168    do_prompt(ua, _("FileSet"), _("Select FileSet resource"), name, sizeof(name));
169    fs = (FILESET *)GetResWithName(R_FILESET, name);
170    return fs;
171 }
172
173
174 /* 
175  * Get a catalog resource from prompt list
176  */
177 CAT *get_catalog_resource(UAContext *ua)
178 {
179    char name[MAX_NAME_LENGTH];    
180    CAT *catalog = NULL;
181    int i;
182
183    for (i=1; i<ua->argc; i++) {
184       if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
185          catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
186          break;
187       }
188    }
189    if (!catalog) {
190       start_prompt(ua, _("The defined Catalog resources are:\n"));
191       LockRes();
192       while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
193          add_prompt(ua, catalog->hdr.name);
194       }
195       UnlockRes();
196       do_prompt(ua, _("Catalog"),  _("Select Catalog resource"), name, sizeof(name));
197       catalog = (CAT *)GetResWithName(R_CATALOG, name);
198    }
199    return catalog;
200 }
201
202
203 /* 
204  * Select a Job resource from prompt list
205  */
206 JOB *select_job_resource(UAContext *ua)
207 {
208    char name[MAX_NAME_LENGTH];    
209    JOB *job = NULL;
210
211    start_prompt(ua, _("The defined Job resources are:\n"));
212    LockRes();
213    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
214       add_prompt(ua, job->hdr.name);
215    }
216    UnlockRes();
217    do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name));
218    job = (JOB *)GetResWithName(R_JOB, name);
219    return job;
220 }
221
222 /* 
223  * Select a Restore Job resource from prompt list
224  */
225 JOB *select_restore_job_resource(UAContext *ua)
226 {
227    char name[MAX_NAME_LENGTH];    
228    JOB *job = NULL;
229
230    start_prompt(ua, _("The defined Restore Job resources are:\n"));
231    LockRes();
232    while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
233       if (job->JobType == JT_RESTORE) {
234          add_prompt(ua, job->hdr.name);
235       }
236    }
237    UnlockRes();
238    do_prompt(ua, _("Job"), _("Select Restore Job"), name, sizeof(name));
239    job = (JOB *)GetResWithName(R_JOB, name);
240    return job;
241 }
242
243
244
245 /* 
246  * Select a client resource from prompt list
247  */
248 CLIENT *select_client_resource(UAContext *ua)
249 {
250    char name[MAX_NAME_LENGTH];    
251    CLIENT *client = NULL;
252
253    start_prompt(ua, _("The defined Client resources are:\n"));
254    LockRes();
255    while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
256       add_prompt(ua, client->hdr.name);
257    }
258    UnlockRes();
259    do_prompt(ua, _("Client"),  _("Select Client (File daemon) resource"), name, sizeof(name));
260    client = (CLIENT *)GetResWithName(R_CLIENT, name);
261    return client;
262 }
263
264 /*
265  *  Get client resource, start by looking for
266  *   client=<client-name>
267  *  if we don't find the keyword, we prompt the user.
268  */
269 CLIENT *get_client_resource(UAContext *ua)
270 {
271    CLIENT *client = NULL;
272    int i;
273    
274    for (i=1; i<ua->argc; i++) {
275       if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
276          client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
277          if (client) {
278             return client;
279          }
280          bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
281          break;
282       }
283    }
284    return select_client_resource(ua);
285 }
286
287 /* Scan what the user has entered looking for:
288  * 
289  *  client=<client-name>
290  *
291  *  if error or not found, put up a list of client DBRs
292  *  to choose from.
293  *
294  *   returns: 0 on error
295  *            1 on success and fills in CLIENT_DBR
296  */
297 int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
298 {
299    int i;
300
301    if (cr->Name[0]) {                 /* If name already supplied */
302       if (db_get_client_record(ua->jcr, ua->db, cr)) {
303          return 1;
304       }
305       bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
306    }
307    for (i=1; i<ua->argc; i++) {
308       if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
309          bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
310          if (!db_get_client_record(ua->jcr, ua->db, cr)) {
311             bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
312                      db_strerror(ua->db));
313             cr->ClientId = 0;
314             break;
315          }
316          return 1;
317       }
318    }
319    if (!select_client_dbr(ua, cr)) {  /* try once more by proposing a list */
320       return 0;
321    }
322    return 1;
323 }
324
325 /*
326  * Select a Client record from the catalog
327  *  Returns 1 on success
328  *          0 on failure
329  */
330 int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
331 {
332    CLIENT_DBR ocr;
333    char name[MAX_NAME_LENGTH];
334    int num_clients, i;
335    uint32_t *ids; 
336
337
338    cr->ClientId = 0;
339    if (!db_get_client_ids(ua->jcr, ua->db, &num_clients, &ids)) {
340       bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
341       return 0;
342    }
343    if (num_clients <= 0) {
344       bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
345       return 0;
346    }
347      
348    start_prompt(ua, _("Defined Clients:\n"));
349    for (i=0; i < num_clients; i++) {
350       ocr.ClientId = ids[i];
351       if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
352          continue;
353       }
354       add_prompt(ua, ocr.Name);
355    }
356    free(ids);
357    if (do_prompt(ua, _("Client"),  _("Select the Client"), name, sizeof(name)) < 0) {
358       return 0;
359    }
360    memset(&ocr, 0, sizeof(ocr));
361    bstrncpy(ocr.Name, name, sizeof(ocr.Name));
362
363    if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
364       bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
365       return 0;
366    }
367    memcpy(cr, &ocr, sizeof(ocr));
368    return 1;
369 }
370
371
372
373 /* Scan what the user has entered looking for:
374  * 
375  *  pool=<pool-name>   
376  *
377  *  if error or not found, put up a list of pool DBRs
378  *  to choose from.
379  *
380  *   returns: 0 on error
381  *            1 on success and fills in POOL_DBR
382  */
383 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
384 {
385    if (pr->Name[0]) {                 /* If name already supplied */
386       if (db_get_pool_record(ua->jcr, ua->db, pr)) {
387          return pr->PoolId;
388       }
389       bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), pr->Name, db_strerror(ua->db));
390    }
391    if (!select_pool_dbr(ua, pr)) {  /* try once more */
392       return 0;
393    }
394    return 1;
395 }
396
397 /*
398  * Select a Pool record from the catalog
399  */
400 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
401 {
402    POOL_DBR opr;
403    char name[MAX_NAME_LENGTH];
404    int num_pools, i;
405    uint32_t *ids; 
406
407    for (i=1; i<ua->argc; i++) {
408       if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
409          bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
410          if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
411             bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
412                      db_strerror(ua->db));
413             pr->PoolId = 0;
414             break;
415          }
416          return 1;
417       }
418    }
419
420    pr->PoolId = 0;
421    if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
422       bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
423       return 0;
424    }
425    if (num_pools <= 0) {
426       bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
427       return 0;
428    }
429      
430    start_prompt(ua, _("Defined Pools:\n"));
431    for (i=0; i < num_pools; i++) {
432       opr.PoolId = ids[i];
433       if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
434          continue;
435       }
436       add_prompt(ua, opr.Name);
437    }
438    free(ids);
439    if (do_prompt(ua, _("Pool"),  _("Select the Pool"), name, sizeof(name)) < 0) {
440       return 0;
441    }
442    memset(&opr, 0, sizeof(opr));
443    bstrncpy(opr.Name, name, sizeof(opr.Name));
444
445    if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
446       bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
447       return 0;
448    }
449    memcpy(pr, &opr, sizeof(opr));
450    return 1;
451 }
452
453 /*
454  * Select a Pool and a Media (Volume) record from the database
455  */
456 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
457 {
458
459    if (!select_media_dbr(ua, mr)) {
460       return 0;
461    }
462    memset(pr, 0, sizeof(POOL_DBR));
463    pr->PoolId = mr->PoolId;
464    if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
465       bsendmsg(ua, "%s", db_strerror(ua->db));
466       return 0;
467    }
468    return 1;
469 }
470
471 /* Select a Media (Volume) record from the database */
472 int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
473 {
474    int i;
475
476    memset(mr, 0, sizeof(MEDIA_DBR));
477
478    i = find_arg_with_value(ua, "volume");
479    if (i >= 0) {
480       bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
481    }
482    if (mr->VolumeName[0] == 0) {
483       POOL_DBR pr;
484       memset(&pr, 0, sizeof(pr));
485       /* Get the pool from pool=<pool-name> */
486       if (!get_pool_dbr(ua, &pr)) {
487          return 0;
488       }
489       mr->PoolId = pr.PoolId;
490       db_list_media_records(ua->jcr, ua->db, mr, prtit, ua, HORZ_LIST);
491       if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
492          return 0;
493       }
494       if (is_a_number(ua->cmd)) {
495          mr->MediaId = atoi(ua->cmd);
496       } else {
497          bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
498       }
499    }
500
501    if (!db_get_media_record(ua->jcr, ua->db, mr)) {
502       bsendmsg(ua, "%s", db_strerror(ua->db));
503       return 0;
504    }
505    return 1;
506 }
507
508
509 /* 
510  * Select a pool resource from prompt list
511  */
512 POOL *select_pool_resource(UAContext *ua)
513 {
514    char name[MAX_NAME_LENGTH];    
515    POOL *pool = NULL;
516
517    start_prompt(ua, _("The defined Pool resources are:\n"));
518    LockRes();
519    while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
520       add_prompt(ua, pool->hdr.name);
521    }
522    UnlockRes();
523    do_prompt(ua, _("Pool"), _("Select Pool resource"), name, sizeof(name));
524    pool = (POOL *)GetResWithName(R_POOL, name);
525    return pool;
526 }
527
528
529 /*
530  *  If you are thinking about using it, you
531  *  probably want to use select_pool_dbr() 
532  *  or get_pool_dbr() above.
533  */
534 POOL *get_pool_resource(UAContext *ua)
535 {
536    POOL *pool = NULL;
537    int i;
538    
539    i = find_arg_with_value(ua, "pool");
540    if (i >= 0) {
541       pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
542       if (pool) {
543          return pool;
544       }
545       bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
546    }
547    return select_pool_resource(ua);
548 }
549
550 /*
551  * List all jobs and ask user to select one
552  */
553 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
554 {
555    db_list_job_records(ua->jcr, ua->db, jr, prtit, ua, HORZ_LIST);
556    if (!get_pint(ua, _("Enter the JobId to select: "))) {
557       return 0;
558    }
559    jr->JobId = ua->pint32_val;
560    if (!db_get_job_record(ua->jcr, ua->db, jr)) {
561       bsendmsg(ua, "%s", db_strerror(ua->db));
562       return 0;
563    }
564    return jr->JobId;
565
566 }
567
568
569 /* Scan what the user has entered looking for:
570  * 
571  *  jobid=nn
572  *
573  *  if error or not found, put up a list of Jobs
574  *  to choose from.
575  *
576  *   returns: 0 on error
577  *            JobId on success and fills in JOB_DBR
578  */
579 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
580 {
581    int i;
582
583    for (i=1; i<ua->argc; i++) {
584       if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
585          jr->JobId = 0;
586          bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
587       } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
588          jr->JobId = atoi(ua->argv[i]);
589       } else {
590          continue;
591       }
592       if (!db_get_job_record(ua->jcr, ua->db, jr)) {
593          bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
594                   db_strerror(ua->db));
595          jr->JobId = 0;
596          break;
597       }
598       return jr->JobId;
599    }
600
601    if (!select_job_dbr(ua, jr)) {  /* try once more */
602       return 0;
603    }
604    return jr->JobId;
605 }
606
607 /*
608  * Implement unique set of prompts 
609  */
610 void start_prompt(UAContext *ua, char *msg)
611 {
612   if (ua->max_prompts == 0) {
613      ua->max_prompts = 10;
614      ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
615   }
616   ua->num_prompts = 1;
617   ua->prompt[0] = bstrdup(msg);
618 }
619
620 /*
621  * Add to prompts -- keeping them unique 
622  */
623 void add_prompt(UAContext *ua, char *prompt)
624 {
625    int i;
626    if (ua->num_prompts == ua->max_prompts) {
627       ua->max_prompts *= 2;
628       ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
629          ua->max_prompts);
630     }
631     for (i=1; i < ua->num_prompts; i++) {
632        if (strcmp(ua->prompt[i], prompt) == 0) {
633           return;
634        }
635     }
636     ua->prompt[ua->num_prompts++] = bstrdup(prompt);
637 }
638
639 /*
640  * Display prompts and get user's choice
641  *
642  *  Returns: -1 on error
643  *            index base 0 on success, and choice
644  *               is copied to prompt if not NULL
645  */
646 int do_prompt(UAContext *ua, char *automsg, char *msg, char *prompt, int max_prompt)
647 {
648    int i, item;
649    char pmsg[MAXSTRING];
650
651    if (ua->num_prompts == 2) {
652       item = 1;
653       if (prompt) {
654          bstrncpy(prompt, ua->prompt[1], max_prompt);
655       }
656       bsendmsg(ua, _("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
657       goto done;
658    }
659    bsendmsg(ua, ua->prompt[0]);
660    for (i=1; i < ua->num_prompts; i++) {
661       bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
662    }
663
664    if (prompt) {
665       *prompt = 0;
666    }
667
668    for ( ;; ) {
669       /* First item is the prompt string, not the items */
670       if (ua->num_prompts == 1) { 
671          bsendmsg(ua, _("Selection is empty!\n"));
672          item = 0;                    /* list is empty ! */
673          break;
674       }
675       if (ua->num_prompts == 2) {
676          item = 1;
677          bsendmsg(ua, _("Item 1 selected automatically.\n"));
678          if (prompt) {
679             bstrncpy(prompt, ua->prompt[1], max_prompt);
680          }
681          break;
682       } else {
683          sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
684       }
685       /* Either a . or an @ will get you out of the loop */
686       if (!get_pint(ua, pmsg)) {
687          item = -1;                   /* error */
688          bsendmsg(ua, _("Selection aborted, nothing done.\n"));
689          break;
690       }
691       item = ua->pint32_val;
692       if (item < 1 || item >= ua->num_prompts) {
693          bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
694          continue;
695       }
696       if (prompt) {
697          bstrncpy(prompt, ua->prompt[item], max_prompt);
698       }
699       break;
700    }
701                               
702 done:
703    for (i=0; i < ua->num_prompts; i++) {
704       free(ua->prompt[i]);
705    }
706    ua->num_prompts = 0;
707    return item - 1;
708 }
709
710
711 /*
712  * We scan what the user has entered looking for
713  *    storage=<storage-resource>
714  *    job=<job_name>
715  *    jobid=<jobid>
716  *    ?              (prompt him with storage list)
717  *    <some-error>   (prompt him with storage list)
718  *
719  * If use_default is set, we assume that any keyword without a value
720  *   is the name of the Storage resource wanted.
721  */
722 STORE *get_storage_resource(UAContext *ua, int use_default)
723 {
724    char *store_name = NULL;
725    STORE *store = NULL;
726    int jobid;
727    JCR *jcr;
728    int i;
729       
730
731    for (i=1; i<ua->argc; i++) {
732       if (use_default && !ua->argv[i]) {
733          /* Default argument is storage */
734          if (store_name) {
735             bsendmsg(ua, _("Storage name given twice.\n"));
736             return NULL;
737          }
738          /* Ignore barcode(s) keywords */
739          if (strncasecmp("barcode", ua->argk[i], 7) == 0) {
740             continue;
741          }
742          store_name = ua->argk[i];
743          if (*store_name == '?') {
744             *store_name = 0;
745             break;
746          }
747       } else {
748          if (strcasecmp(ua->argk[i], _("storage")) == 0) {
749             store_name = ua->argv[i];
750             break;
751
752          } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
753             jobid = atoi(ua->argv[i]);
754             if (jobid <= 0) {
755                bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
756                return NULL;
757             }
758             if (!(jcr=get_jcr_by_id(jobid))) {
759                bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
760                return NULL;
761             }
762             store = jcr->store;
763             free_jcr(jcr);
764             break;
765
766          } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
767             if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
768                bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
769                return NULL;
770             }
771             store = jcr->store;
772             free_jcr(jcr);
773             break;
774         }
775       }
776    }
777
778    if (!store && store_name) {
779       store = (STORE *)GetResWithName(R_STORAGE, store_name);
780       if (!store) {
781          bsendmsg(ua, "Storage resource %s: not found\n", store_name);
782       }
783    }
784    /* No keywords found, so present a selection list */
785    if (!store) {
786       store = select_storage_resource(ua);
787    }
788    return store;
789 }
790
791
792 /*
793  * Scan looking for mediatype= 
794  *
795  *  if not found or error, put up selection list
796  *
797  *  Returns: 0 on error
798  *           1 on success, MediaType is set
799  */
800 int get_media_type(UAContext *ua, char *MediaType, int max_media)
801 {
802    STORE *store;
803    int i;
804
805    i = find_arg_with_value(ua, "mediatype");
806    if (i >= 0) {
807       bstrncpy(MediaType, ua->argv[i], max_media);
808       return 1;
809    }
810
811    start_prompt(ua, _("Media Types defined in conf file:\n"));
812    LockRes();
813    for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
814       add_prompt(ua, store->media_type);
815    }
816    UnlockRes();
817    return (do_prompt(ua, _("Media Type"), _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;
818 }