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