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