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