]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
Cleanup storage keyword scanning
[bacula/bacula] / bacula / src / dird / ua_cmds.c
1 /*
2  *
3  *   Bacula Director -- User Agent Commands
4  *
5  *     Kern Sibbald, September MM
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 subroutines */
34 extern void run_job(JCR *jcr);
35
36 /* Imported variables */
37 extern int r_first;
38 extern int r_last;
39 extern struct s_res resources[];
40 extern char my_name[];
41 #ifndef USE_SEMAPHORE 
42 extern workq_t job_wq;                /* work queue */
43 #endif
44
45 extern char *list_pool;
46
47 /* Imported functions */
48 extern int statuscmd(UAContext *ua, char *cmd);
49 extern int listcmd(UAContext *ua, char *cmd);
50 extern int llistcmd(UAContext *ua, char *cmd);
51 extern int showcmd(UAContext *ua, char *cmd);
52 extern int messagescmd(UAContext *ua, char *cmd);
53 extern int autodisplaycmd(UAContext *ua, char *cmd);
54 extern int sqlquerycmd(UAContext *ua, char *cmd);
55 extern int querycmd(UAContext *ua, char *cmd);
56 extern int runcmd(UAContext *ua, char *cmd);
57 extern int retentioncmd(UAContext *ua, char *cmd);
58 extern int prunecmd(UAContext *ua, char *cmd);
59 extern int purgecmd(UAContext *ua, char *cmd);
60 extern int restorecmd(UAContext *ua, char *cmd);
61 extern int labelcmd(UAContext *ua, char *cmd);
62 extern int relabelcmd(UAContext *ua, char *cmd);
63
64 /* Forward referenced functions */
65 static int addcmd(UAContext *ua, char *cmd),  createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
66 static int setdebugcmd(UAContext *ua, char *cmd);
67 static int helpcmd(UAContext *ua, char *cmd);
68 static int deletecmd(UAContext *ua, char *cmd);
69 static int usecmd(UAContext *ua, char *cmd),  unmountcmd(UAContext *ua, char *cmd);
70 static int versioncmd(UAContext *ua, char *cmd), automountcmd(UAContext *ua, char *cmd);
71 static int timecmd(UAContext *ua, char *cmd);
72 static int update_volume(UAContext *ua);
73 static int update_pool(UAContext *ua);
74 static int delete_volume(UAContext *ua);
75 static int delete_pool(UAContext *ua);
76 static int mountcmd(UAContext *ua, char *cmd);
77 static int updatecmd(UAContext *ua, char *cmd);
78
79 int quitcmd(UAContext *ua, char *cmd);
80
81
82 struct cmdstruct { char *key; int (*func)(UAContext *ua, char *cmd); char *help; }; 
83 static struct cmdstruct commands[] = {
84  { N_("add"),        addcmd,       _("add media to a pool")},
85  { N_("autodisplay"), autodisplaycmd, _("autodisplay [on/off] -- console messages")},
86  { N_("automount"),   automountcmd,   _("automount [on/off] -- after label")},
87  { N_("cancel"),     cancelcmd,    _("cancel job=nnn -- cancel a job")},
88  { N_("create"),     createcmd,    _("create DB Pool from resource")},  
89  { N_("delete"),     deletecmd,    _("delete [pool=<pool-name> | media volume=<volume-name>]")},    
90  { N_("help"),       helpcmd,      _("print this command")},
91  { N_("label"),      labelcmd,     _("label a tape")},
92  { N_("relabel"),    relabelcmd,   _("relabel a tape")},
93  { N_("list"),       listcmd,      _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
94  { N_("llist"),      llistcmd,     _("full or long list like list command")},
95  { N_("messages"),   messagescmd,  _("messages")},
96  { N_("mount"),      mountcmd,     _("mount <storage-name>")},
97  { N_("restore"),    restorecmd,   _("restore files")},
98  { N_("prune"),      prunecmd,     _("prune expired records from catalog")},
99  { N_("purge"),      purgecmd,     _("purge records from catalog")},
100  { N_("run"),        runcmd,       _("run <job-name>")},
101  { N_("setdebug"),   setdebugcmd,  _("sets debug level")},
102  { N_("show"),       showcmd,      _("show (resource records) [jobs | pools | ... | all]")},
103  { N_("sqlquery"),   sqlquerycmd,  _("use SQL to query catalog")}, 
104  { N_("status"),     statuscmd,    _("status [storage | client]=<name>")},
105  { N_("unmount"),    unmountcmd,   _("unmount <storage-name>")},
106  { N_("update"),     updatecmd,    _("update Volume or Pool")},
107  { N_("use"),        usecmd,       _("use catalog xxx")},
108  { N_("version"),    versioncmd,   _("print Director version")},
109  { N_("quit"),       quitcmd,      _("quit")},
110  { N_("query"),      querycmd,     _("query catalog")},
111  { N_("time"),       timecmd,      _("print current time")},
112  { N_("exit"),       quitcmd,      _("exit = quit")},
113              };
114 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
115
116 /*
117  * Execute a command from the UA
118  */
119 int do_a_command(UAContext *ua, char *cmd)
120 {
121    unsigned int i;
122    int len, stat;
123    int found;
124
125    found = 0;
126    stat = 1;
127
128    Dmsg1(120, "Command: %s\n", ua->UA_sock->msg);
129    if (ua->argc == 0) {
130       return 1;
131    }
132
133    len = strlen(ua->argk[0]);
134    for (i=0; i<comsize; i++)       /* search for command */
135       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
136          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
137          found = 1;
138          break;
139       }
140    if (!found) {
141       strcat(ua->UA_sock->msg, _(": is an illegal command\n"));
142       ua->UA_sock->msglen = strlen(ua->UA_sock->msg);
143       bnet_send(ua->UA_sock);
144    }
145    return stat;
146 }
147
148 /*
149  * This is a common routine used to stuff the Pool DB record defaults
150  *   into the Media DB record just before creating a media (Volume) 
151  *   record.
152  */
153 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
154 {
155    mr->PoolId = pr->PoolId;
156    strcpy(mr->VolStatus, "Append");
157    mr->Recycle = pr->Recycle;
158    mr->VolRetention = pr->VolRetention;
159    mr->VolUseDuration = pr->VolUseDuration;
160    mr->MaxVolJobs = pr->MaxVolJobs;
161    mr->MaxVolFiles = pr->MaxVolFiles;
162    mr->MaxVolBytes = pr->MaxVolBytes;
163 }
164
165
166 /*
167  *  Add Volumes to an existing Pool
168  */
169 static int addcmd(UAContext *ua, char *cmd) 
170 {
171    POOL_DBR pr;
172    MEDIA_DBR mr;
173    int num, i, max, startnum;
174    int first_id = 0;
175    char name[MAX_NAME_LENGTH];
176    STORE *store;
177    int slot = 0;
178
179    bsendmsg(ua, _(
180 "You probably don't want to be using this command since it\n"
181 "creates database records without labeling the Volumes.\n"
182 "You probably want to use the \"label\" command.\n\n"));
183
184    if (!open_db(ua)) {
185       return 1;
186    }
187
188    memset(&pr, 0, sizeof(pr));
189    memset(&mr, 0, sizeof(mr));
190
191    if (!get_pool_dbr(ua, &pr)) {
192       return 1;
193    }
194
195    Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
196       pr.MaxVols, pr.PoolType);
197
198    while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
199       bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
200       for (;;) {
201          if (!get_cmd(ua, _("Enter new maximum (zero for unlimited): "))) {
202             return 1;
203          }
204          pr.MaxVols = atoi(ua->cmd);
205          if (pr.MaxVols < 0) {
206             bsendmsg(ua, _("Max vols must be zero or greater.\n"));
207             continue;
208          }
209          break;
210       }
211    }
212
213    /* Get media type */
214    if ((store = get_storage_resource(ua, cmd, 0)) != NULL) {
215       strcpy(mr.MediaType, store->media_type);
216    } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
217       return 1;
218    }
219       
220    if (pr.MaxVols == 0) {
221       max = 1000;
222    } else {
223       max = pr.MaxVols - pr.NumVols;
224    }
225    for (;;) {
226       char buf[100]; 
227       sprintf(buf, _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
228       if (!get_cmd(ua, buf)) {
229          return 1;
230       }
231       num = atoi(ua->cmd);
232       if (num < 0 || num > max) {
233          bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
234          continue;
235       }
236       break;
237    }
238 getVolName:
239    if (num == 0) {
240       if (!get_cmd(ua, _("Enter Volume name: "))) {
241          return 1;
242       }
243    } else {
244       if (!get_cmd(ua, _("Enter base volume name: "))) {
245          return 1;
246       }
247    }
248    /* Don't allow | in Volume name because it is the volume separator character */
249    if (strchr(ua->cmd, '|')) {
250       bsendmsg(ua, _("Illegal character | in a volume name.\n"));
251       goto getVolName;
252    }
253    if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
254       bsendmsg(ua, _("Volume name too long.\n"));
255       goto getVolName;
256    }
257    if (strlen(ua->cmd) == 0) {
258       bsendmsg(ua, _("Volume name must be at least one character long.\n"));
259       goto getVolName;
260    }
261
262    strcpy(name, ua->cmd);
263    if (num > 0) {
264       strcat(name, "%04d");
265
266       for (;;) {
267          if (!get_cmd(ua, _("Enter the starting number: "))) {
268             return 1;
269          }
270          startnum = atoi(ua->cmd);
271          if (startnum < 1) {
272             bsendmsg(ua, _("Start number must be greater than zero.\n"));
273             continue;
274          }
275          break;
276       }
277    } else {
278       startnum = 1;
279       num = 1;
280    }
281
282    if (store && store->autochanger) {
283       if (!get_cmd(ua, _("Enter slot (0 for none): "))) {
284          return 1;
285       }
286       slot = atoi(ua->cmd);
287    }
288            
289    set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
290    for (i=startnum; i < num+startnum; i++) { 
291       sprintf(mr.VolumeName, name, i);
292       mr.Slot = slot++;
293       Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
294       if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
295          bsendmsg(ua, db_strerror(ua->db));
296          return 1;
297       }
298       if (i == startnum) {
299          first_id = mr.PoolId;
300       }
301    }
302    pr.NumVols += num;
303    Dmsg0(200, "Update pool record.\n"); 
304    if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
305       bsendmsg(ua, db_strerror(ua->db));
306       return 1;
307    }
308    bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
309
310    return 1;
311 }
312
313 /*
314  * Turn auto mount on/off  
315  * 
316  *  automount on 
317  *  automount off
318  */
319 int automountcmd(UAContext *ua, char *cmd)
320 {
321    char *onoff;
322
323    if (ua->argc != 2) {
324       if (!get_cmd(ua, _("Turn on or off? "))) {
325             return 1;
326       }
327       onoff = ua->cmd;
328    } else {
329       onoff = ua->argk[1];
330    }
331
332    ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
333    return 1; 
334 }
335
336
337 /*
338  * Cancel a job
339  */
340 static int cancelcmd(UAContext *ua, char *cmd)
341 {
342    int i;
343    int njobs = 0;
344    BSOCK *sd, *fd;
345    JCR *jcr = NULL;
346    char JobName[MAX_NAME_LENGTH];
347
348    if (!open_db(ua)) {
349       return 1;
350    }
351
352    for (i=1; i<ua->argc; i++) {
353       if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
354          if (!ua->argv[i]) {
355             break;
356          }
357          if (!(jcr=get_jcr_by_id(atoi(ua->argv[i])))) {
358             bsendmsg(ua, _("JobId %d is not running.\n"), atoi(ua->argv[i]));
359             return 1;
360          }
361          break;
362       } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
363          if (!ua->argv[i]) {
364             break;
365          }
366          if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
367             bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
368             return 1;
369          }
370          break;
371       }
372    }
373    /* If we still do not have a jcr,
374     *   throw up a list and ask the user to select one.
375     */
376    if (!jcr) {
377       /* Count Jobs running */
378       lock_jcr_chain();
379       for (jcr=NULL; (jcr=get_next_jcr(jcr)); njobs++) {
380          if (jcr->JobId == 0) {      /* this is us */
381             free_locked_jcr(jcr);
382             njobs--;
383             continue;
384          }
385          free_locked_jcr(jcr);
386       }
387       unlock_jcr_chain();
388
389       if (njobs == 0) {
390          bsendmsg(ua, _("No Jobs running.\n"));
391          return 1;
392       }
393       start_prompt(ua, _("Select Job:\n"));
394       lock_jcr_chain();
395       for (jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
396          if (jcr->JobId == 0) {      /* this is us */
397             free_locked_jcr(jcr);
398             continue;
399          }
400          add_prompt(ua, jcr->Job);
401          free_locked_jcr(jcr);
402       }
403       unlock_jcr_chain();
404
405       if (do_prompt(ua, _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
406          return 1;
407       }
408       if (njobs == 1) {
409          if (!get_cmd(ua, _("Confirm cancel (yes/no): "))) {
410             return 1;
411          }
412          if (strcasecmp(ua->cmd, _("yes")) != 0) {
413             return 1;
414          }
415       }
416       jcr = get_jcr_by_full_name(JobName);
417       if (!jcr) {
418          bsendmsg(ua, _("Job %s not found.\n"), JobName);
419          return 1;
420       }
421    }
422      
423    switch (jcr->JobStatus) {
424    case JS_Created:
425       set_jcr_job_status(jcr, JS_Canceled);
426       bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
427               jcr->JobId, jcr->Job);
428 #ifndef USE_SEMAPHORE
429       workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
430 #endif
431       free_jcr(jcr);
432       return 1;
433          
434    default:
435       set_jcr_job_status(jcr, JS_Canceled);
436       /* Cancel File daemon */
437       ua->jcr->client = jcr->client;
438       if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
439          bsendmsg(ua, _("Failed to connect to File daemon.\n"));
440          free_jcr(jcr);
441          return 1;
442       }
443       Dmsg0(200, "Connected to file daemon\n");
444       fd = ua->jcr->file_bsock;
445       bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
446       while (bnet_recv(fd) >= 0) {
447          bsendmsg(ua, "%s", fd->msg);
448       }
449       bnet_sig(fd, BNET_TERMINATE);
450       bnet_close(fd);
451       ua->jcr->file_bsock = NULL;
452
453       /* Cancel Storage daemon */
454       ua->jcr->store = jcr->store;
455       if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
456          bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
457          free_jcr(jcr);
458          return 1;
459       }
460       Dmsg0(200, "Connected to storage daemon\n");
461       sd = ua->jcr->store_bsock;
462       bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
463       while (bnet_recv(sd) >= 0) {
464          bsendmsg(ua, "%s", sd->msg);
465       }
466       bnet_sig(sd, BNET_TERMINATE);
467       bnet_close(sd);
468       ua->jcr->store_bsock = NULL;
469
470    }
471    free_jcr(jcr);
472
473    return 1; 
474 }
475
476 /*
477  * This is a common routine to create or update a
478  *   Pool DB base record from a Pool Resource. We handle
479  *   the setting of MaxVols and NumVols slightly differently
480  *   depending on if we are creating the Pool or we are
481  *   simply bringing it into agreement with the resource (updage).
482  */
483 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
484 {
485    strcpy(pr->PoolType, pool->pool_type);
486    if (create) {
487       pr->MaxVols = pool->max_volumes;
488       pr->NumVols = 0;
489    } else {          /* update pool */
490       if (pr->MaxVols != pool->max_volumes) {
491          pr->MaxVols = pool->max_volumes;
492       }
493       if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
494          pr->MaxVols = pr->NumVols;
495       }
496    }
497    pr->UseOnce = pool->use_volume_once;
498    pr->UseCatalog = pool->use_catalog;
499    pr->AcceptAnyVolume = pool->accept_any_volume;
500    pr->Recycle = pool->Recycle;
501    pr->VolRetention = pool->VolRetention;
502    pr->VolUseDuration = pool->VolUseDuration;
503    pr->MaxVolJobs = pool->MaxVolJobs;
504    pr->MaxVolFiles = pool->MaxVolFiles;
505    pr->MaxVolBytes = pool->MaxVolBytes;
506    if (pool->label_format) {
507       strcpy(pr->LabelFormat, pool->label_format);
508    } else {
509       strcpy(pr->LabelFormat, "*");    /* none */
510    }
511 }
512
513
514 /*
515  * Create a pool record from a given Pool resource
516  *   Also called from backup.c
517  * Returns: -1  on error
518  *           0  record already exists
519  *           1  record created
520  */
521
522 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
523 {
524    POOL_DBR  pr;
525
526    memset(&pr, 0, sizeof(POOL_DBR));
527
528    strcpy(pr.Name, pool->hdr.name);
529
530    if (db_get_pool_record(jcr, db, &pr)) {
531       /* Pool Exists */
532       if (!create) {  /* update request */
533          set_pooldbr_from_poolres(&pr, pool, 0);
534          db_update_pool_record(jcr, db, &pr);
535       }
536       return 0;                       /* exists */
537    }
538
539    set_pooldbr_from_poolres(&pr, pool, 1);
540
541    if (!db_create_pool_record(jcr, db, &pr)) {
542       return -1;                      /* error */
543    }
544    return 1;
545 }
546
547
548
549 /*
550  * Create a Pool Record in the database.
551  *  It is always created from the Resource record.
552  */
553 static int createcmd(UAContext *ua, char *cmd) 
554 {
555    POOL *pool;
556
557    if (!open_db(ua)) {
558       return 1;
559    }
560
561    pool = get_pool_resource(ua);
562    if (!pool) {
563       return 1;
564    }
565
566    switch (create_pool(ua->jcr, ua->db, pool, 1)) {
567    case 0:
568       bsendmsg(ua, _("Error: Pool %s already exists.\n\
569 Use update to change it.\n"), pool->hdr.name);
570       break;
571
572    case -1:
573       bsendmsg(ua, db_strerror(ua->db));
574       break;
575
576    default:
577      break;
578    }
579    bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
580    return 1;
581 }
582
583
584
585
586 /*
587  * Update a Pool Record in the database.
588  *  It is always updated from the Resource record.
589  *
590  *    update pool=<pool-name>
591  *         updates pool from Pool resource
592  *    update media pool=<pool-name> volume=<volume-name>
593  *         changes pool info for volume
594  */
595 static int updatecmd(UAContext *ua, char *cmd) 
596 {
597    static char *kw[] = {
598       N_("media"),
599       N_("volume"),
600       N_("pool"),
601       NULL};
602
603    if (!open_db(ua)) {
604       return 1;
605    }
606
607    switch (find_arg_keyword(ua, kw)) {
608       case 0:
609       case 1:
610          update_volume(ua);
611          return 1;
612       case 2:
613          update_pool(ua);
614          return 1;
615       default:
616          break;
617    }
618     
619    start_prompt(ua, _("Update choice:\n"));
620    add_prompt(ua, _("Volume parameters"));
621    add_prompt(ua, _("Pool from resource"));
622    switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
623       case 0:
624          update_volume(ua);
625          break;
626       case 1:
627          update_pool(ua);
628          break;
629       default:
630          break;
631    }
632    return 1;
633 }
634
635 /*
636  * Update a media record -- allows you to change the
637  *  Volume status. E.g. if you want Bacula to stop
638  *  writing on the volume, set it to anything other
639  *  than Append.
640  */              
641 static int update_volume(UAContext *ua)
642 {
643    POOL_DBR pr;
644    MEDIA_DBR mr;
645    POOLMEM *query;
646    char ed1[30];
647
648    if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
649       return 0;
650    }
651
652    for (int done=0; !done; ) {
653       if (!db_get_media_record(ua->jcr, ua->db, &mr)) {
654          if (mr.MediaId != 0) {
655             bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
656          } else {
657             bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
658          }
659          return 0;
660       }
661       bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
662       start_prompt(ua, _("Parameters to modify:\n"));
663       add_prompt(ua, _("Volume Status"));
664       add_prompt(ua, _("Volume Retention Period"));
665       add_prompt(ua, _("Volume Use Duration"));
666       add_prompt(ua, _("Maximum Volume Jobs"));
667       add_prompt(ua, _("Maximum Volume Files"));
668       add_prompt(ua, _("Maximum Volume Bytes"));
669       add_prompt(ua, _("Recycle Flag"));
670       add_prompt(ua, _("Slot"));
671       add_prompt(ua, _("Volume Files"));
672       add_prompt(ua, _("Done"));
673       switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
674       case 0:                         /* Volume Status */
675          /* Modify Volume Status */
676          bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
677          start_prompt(ua, _("Possible Values are:\n"));
678          add_prompt(ua, "Append");      /* Better not translate these as */
679          add_prompt(ua, "Archive");     /* They are known in the database code */
680          add_prompt(ua, "Disabled");
681          add_prompt(ua, "Full");
682          add_prompt(ua, "Used");
683          if (strcmp(mr.VolStatus, "Purged") == 0) {
684             add_prompt(ua, "Recycle");
685          }
686          add_prompt(ua, "Read-Only");
687          if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
688             return 1;
689          }
690          bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
691          query = get_pool_memory(PM_MESSAGE);
692          Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
693             mr.VolStatus, mr.MediaId);
694          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
695             bsendmsg(ua, "%s", db_strerror(ua->db));
696          } else {
697             bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
698          }
699          free_pool_memory(query);
700          break;
701       case 1:                         /* Retention */
702          bsendmsg(ua, _("Current retention seconds is: %s\n"),
703             edit_utime(mr.VolRetention, ed1));
704          if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
705             return 0;
706          }
707          if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
708             bsendmsg(ua, _("Invalid retention period specified.\n"));
709             break;
710          }
711          query = get_pool_memory(PM_MESSAGE);
712          Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
713             edit_uint64(mr.VolRetention, ed1), mr.MediaId);
714          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
715             bsendmsg(ua, "%s", db_strerror(ua->db));
716          } else {
717             bsendmsg(ua, _("New retention seconds is: %s\n"),
718                edit_utime(mr.VolRetention, ed1));
719          }
720          free_pool_memory(query);
721          break;
722
723       case 2:                         /* Use Duration */
724          bsendmsg(ua, _("Current use duration is: %s\n"),
725             edit_utime(mr.VolUseDuration, ed1));
726          if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
727             return 0;
728          }
729          if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
730             bsendmsg(ua, _("Invalid use duration specified.\n"));
731             break;
732          }
733          query = get_pool_memory(PM_MESSAGE);
734          Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
735             edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
736          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
737             bsendmsg(ua, "%s", db_strerror(ua->db));
738          } else {
739             bsendmsg(ua, _("New use duration is: %s\n"),
740                edit_utime(mr.VolUseDuration, ed1));
741          }
742          free_pool_memory(query);
743          break;
744
745       case 3:                         /* Max Jobs */
746          int32_t maxjobs;
747          bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
748          if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
749             return 0;
750          }
751          maxjobs = atoi(ua->cmd);
752          if (maxjobs < 0) {
753             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
754             break;
755          } 
756          query = get_pool_memory(PM_MESSAGE);
757          Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
758             maxjobs, mr.MediaId);
759          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
760             bsendmsg(ua, "%s", db_strerror(ua->db));
761          } else {
762             bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
763          }
764          free_pool_memory(query);
765          break;
766
767       case 4:                         /* Max Files */
768          int32_t maxfiles;
769          bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
770          if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
771             return 0;
772          }
773          maxfiles = atoi(ua->cmd);
774          if (maxfiles < 0) {
775             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
776             break;
777          } 
778          query = get_pool_memory(PM_MESSAGE);
779          Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
780             maxfiles, mr.MediaId);
781          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
782             bsendmsg(ua, "%s", db_strerror(ua->db));
783          } else {
784             bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
785          }
786          free_pool_memory(query);
787          break;
788
789       case 5:                         /* Max Bytes */
790          uint64_t maxbytes;
791          bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
792          if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
793             return 0;
794          }
795          if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
796             bsendmsg(ua, _("Invalid byte size specification.\n"));
797             break;
798          } 
799          query = get_pool_memory(PM_MESSAGE);
800          Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
801             edit_uint64(maxbytes, ed1), mr.MediaId);
802          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
803             bsendmsg(ua, "%s", db_strerror(ua->db));
804          } else {
805             bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
806          }
807          free_pool_memory(query);
808          break;
809
810
811       case 6:                         /* Recycle */
812          int recycle;
813          bsendmsg(ua, _("Current recycle flag is: %s\n"),
814             mr.Recycle==1?_("yes"):_("no"));
815          if (!get_cmd(ua, _("Enter new Recycle status: "))) {
816             return 0;
817          }
818          if (strcasecmp(ua->cmd, _("yes")) == 0) {
819             recycle = 1;
820          } else if (strcasecmp(ua->cmd, _("no")) == 0) {
821             recycle = 0;
822          } else {
823             bsendmsg(ua, _("Invalid recycle status specified.\n"));
824             break;
825          }
826          query = get_pool_memory(PM_MESSAGE);
827          Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
828             recycle, mr.MediaId);
829          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
830             bsendmsg(ua, "%s", db_strerror(ua->db));
831          } else {       
832             bsendmsg(ua, _("New recycle flag is: %s\n"),
833                mr.Recycle==1?_("yes"):_("no"));
834          }
835          free_pool_memory(query);
836          break;
837
838       case 7:                         /* Slot */
839          int slot;
840          bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
841          if (!get_cmd(ua, _("Enter new Slot: "))) {
842             return 0;
843          }
844          slot = atoi(ua->cmd);
845          if (slot < 0) {
846             bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
847             break;
848          } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
849             bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
850                pr.MaxVols);
851             break;
852          }
853          query = get_pool_memory(PM_MESSAGE);
854          Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
855             slot, mr.MediaId);
856          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
857             bsendmsg(ua, "%s", db_strerror(ua->db));
858          } else {
859             bsendmsg(ua, "New Slot is: %d\n", slot);
860          }
861          free_pool_memory(query);
862          break;
863
864       case 8:                         /* Volume Files */
865          int32_t VolFiles;
866          bsendmsg(ua, _("Warning changing Volume Files can result\n"
867                         "in loss of data on your Volume\n\n"));
868          bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
869          if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
870             return 0;
871          }
872          VolFiles = atoi(ua->cmd);
873          if (VolFiles < 0) {
874             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
875             break;
876          } 
877          if (VolFiles != (int)(mr.VolFiles + 1)) {
878             bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
879             if (!get_cmd(ua, _("Continue? (yes/no): ")) || 
880                  strcasecmp(ua->cmd, "yes") != 0) {
881                break;
882             }
883          }
884          query = get_pool_memory(PM_MESSAGE);
885          Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
886             VolFiles, mr.MediaId);
887          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
888             bsendmsg(ua, "%s", db_strerror(ua->db));
889          } else {
890             bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
891          }
892          free_pool_memory(query);
893          break;
894
895       default:                        /* Done or error */
896          bsendmsg(ua, "Selection done.\n");
897          return 1;
898       }
899    }
900    return 1;
901 }
902
903 /* 
904  * Update pool record -- pull info from current POOL resource
905  */
906 static int update_pool(UAContext *ua)
907 {
908    POOL_DBR  pr;
909    int id;
910    POOL *pool;
911    POOLMEM *query;       
912    
913    
914    pool = get_pool_resource(ua);
915    if (!pool) {
916       return 0;
917    }
918
919    memset(&pr, 0, sizeof(pr));
920    strcpy(pr.Name, pool->hdr.name);
921    if (!get_pool_dbr(ua, &pr)) {
922       return 0;
923    }
924
925    set_pooldbr_from_poolres(&pr, pool, 0); /* update */
926
927    id = db_update_pool_record(ua->jcr, ua->db, &pr);
928    if (id <= 0) {
929       bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
930          id, db_strerror(ua->db));
931    }
932    query = get_pool_memory(PM_MESSAGE);
933    Mmsg(&query, list_pool, pr.PoolId);
934    db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
935    free_pool_memory(query);
936    bsendmsg(ua, _("Pool DB record updated from resource.\n"));
937    return 1;
938 }
939
940
941 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
942 {
943    BSOCK *sd;
944
945    ua->jcr->store = store;
946    /* Try connecting for up to 15 seconds */
947    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
948       store->hdr.name, store->address, store->SDport);
949    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
950       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
951       return;
952    }
953    Dmsg0(120, _("Connected to storage daemon\n"));
954    sd = ua->jcr->store_bsock;
955    bnet_fsend(sd, "setdebug=%d\n", level);
956    if (bnet_recv(sd) >= 0) {
957       bsendmsg(ua, "%s", sd->msg);
958    }
959    bnet_sig(sd, BNET_TERMINATE);
960    bnet_close(sd);
961    ua->jcr->store_bsock = NULL;
962    return;  
963 }
964    
965 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
966 {
967    BSOCK *fd;
968
969    /* Connect to File daemon */
970
971    ua->jcr->client = client;
972    /* Try to connect for 15 seconds */
973    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
974       client->hdr.name, client->address, client->FDport);
975    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
976       bsendmsg(ua, _("Failed to connect to Client.\n"));
977       return;
978    }
979    Dmsg0(120, "Connected to file daemon\n");
980    fd = ua->jcr->file_bsock;
981    bnet_fsend(fd, "setdebug=%d\n", level);
982    if (bnet_recv(fd) >= 0) {
983       bsendmsg(ua, "%s", fd->msg);
984    }
985    bnet_sig(fd, BNET_TERMINATE);
986    bnet_close(fd);
987    ua->jcr->file_bsock = NULL;
988
989    return;  
990 }
991
992
993 static void do_all_setdebug(UAContext *ua, int level)
994 {
995    STORE *store, **unique_store;
996    CLIENT *client, **unique_client;
997    int i, j, found;
998
999    /* Director */
1000    debug_level = level;
1001
1002    /* Count Storage items */
1003    LockRes();
1004    store = NULL;
1005    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1006       { }
1007    unique_store = (STORE **) malloc(i * sizeof(STORE));
1008    /* Find Unique Storage address/port */         
1009    store = (STORE *)GetNextRes(R_STORAGE, NULL);
1010    i = 0;
1011    unique_store[i++] = store;
1012    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1013       found = 0;
1014       for (j=0; j<i; j++) {
1015          if (strcmp(unique_store[j]->address, store->address) == 0 &&
1016              unique_store[j]->SDport == store->SDport) {
1017             found = 1;
1018             break;
1019          }
1020       }
1021       if (!found) {
1022          unique_store[i++] = store;
1023          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1024       }
1025    }
1026    UnlockRes();
1027
1028    /* Call each unique Storage daemon */
1029    for (j=0; j<i; j++) {
1030       do_storage_setdebug(ua, unique_store[j], level);
1031    }
1032    free(unique_store);
1033
1034    /* Count Client items */
1035    LockRes();
1036    client = NULL;
1037    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1038       { }
1039    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1040    /* Find Unique Client address/port */         
1041    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1042    i = 0;
1043    unique_client[i++] = client;
1044    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1045       found = 0;
1046       for (j=0; j<i; j++) {
1047          if (strcmp(unique_client[j]->address, client->address) == 0 &&
1048              unique_client[j]->FDport == client->FDport) {
1049             found = 1;
1050             break;
1051          }
1052       }
1053       if (!found) {
1054          unique_client[i++] = client;
1055          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1056       }
1057    }
1058    UnlockRes();
1059
1060    /* Call each unique File daemon */
1061    for (j=0; j<i; j++) {
1062       do_client_setdebug(ua, unique_client[j], level);
1063    }
1064    free(unique_client);
1065 }
1066
1067 /*
1068  * setdebug level=nn all
1069  */
1070 static int setdebugcmd(UAContext *ua, char *cmd)
1071 {
1072    STORE *store;
1073    CLIENT *client;
1074    int level;
1075    int i;
1076
1077    if (!open_db(ua)) {
1078       return 1;
1079    }
1080    Dmsg1(120, "setdebug:%s:\n", cmd);
1081
1082    level = -1;
1083    for (i=1; i<ua->argc; i++) {
1084       if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1085          level = atoi(ua->argv[i]);
1086          break;
1087       }
1088    }
1089    if (level < 0) {
1090       if (!get_cmd(ua, _("Enter new debug level: "))) {
1091          return 1;
1092       }
1093       level = atoi(ua->cmd);
1094    }
1095    if (level < 0) {
1096       bsendmsg(ua, _("level cannot be negative.\n"));
1097       return 1;
1098    }
1099
1100    /* General debug? */
1101    for (i=1; i<ua->argc; i++) {
1102       if (strcasecmp(ua->argk[i], _("all")) == 0) {
1103          do_all_setdebug(ua, level);
1104          return 1;
1105       }
1106       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1107           strcasecmp(ua->argk[i], _("director")) == 0) {
1108          debug_level = level;
1109          return 1;
1110       }
1111       if (strcasecmp(ua->argk[i], _("client")) == 0) {
1112          client = NULL;
1113          if (ua->argv[i]) {
1114             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1115             if (client) {
1116                do_client_setdebug(ua, client, level);
1117                return 1;
1118             }
1119          }
1120          client = select_client_resource(ua);   
1121          if (client) {
1122             do_client_setdebug(ua, client, level);
1123             return 1;
1124          }
1125       }
1126
1127       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1128           strcasecmp(ua->argk[i], _("storage")) == 0) {
1129          store = NULL;
1130          if (ua->argv[i]) {
1131             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1132             if (store) {
1133                do_storage_setdebug(ua, store, level);
1134                return 1;
1135             }
1136          }
1137          store = get_storage_resource(ua, cmd, 0);
1138          if (store) {
1139             do_storage_setdebug(ua, store, level);
1140             return 1;
1141          }
1142       }
1143    } 
1144    /*
1145     * We didn't find an appropriate keyword above, so
1146     * prompt the user.
1147     */
1148    start_prompt(ua, _("Available daemons are: \n"));
1149    add_prompt(ua, _("Director"));
1150    add_prompt(ua, _("Storage"));
1151    add_prompt(ua, _("Client"));
1152    add_prompt(ua, _("All"));
1153    switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1154       case 0:                         /* Director */
1155          debug_level = level;
1156          break;
1157       case 1:
1158          store = get_storage_resource(ua, cmd, 0);
1159          if (store) {
1160             do_storage_setdebug(ua, store, level);
1161          }
1162          break;
1163       case 2:
1164          client = select_client_resource(ua);
1165          if (client) {
1166             do_client_setdebug(ua, client, level);
1167          }
1168          break;
1169       case 3:
1170          do_all_setdebug(ua, level);
1171          break;
1172       default:
1173          break;
1174    }
1175    return 1;
1176 }
1177
1178 /*
1179  * print time
1180  */
1181 static int timecmd(UAContext *ua, char *cmd)
1182 {
1183    char sdt[50];
1184    time_t ttime = time(NULL);
1185    struct tm tm;
1186    localtime_r(&ttime, &tm);
1187    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1188    bsendmsg(ua, "%s\n", sdt);
1189    return 1;
1190 }
1191
1192
1193
1194 /*
1195  * Delete Pool records (should purge Media with it).
1196  *
1197  *  delete pool=<pool-name>
1198  *  delete media pool=<pool-name> volume=<name>
1199  */
1200 static int deletecmd(UAContext *ua, char *cmd)
1201 {
1202    static char *keywords[] = {
1203       N_("volume"),
1204       N_("pool"),
1205       NULL};
1206
1207    if (!open_db(ua)) {
1208       return 1;
1209    }
1210
1211    bsendmsg(ua, _(
1212 "In general it is not a good idea to delete either a\n"
1213 "Pool or a Volume since they may contain data.\n\n"));
1214      
1215    switch (find_arg_keyword(ua, keywords)) {
1216       case 0:
1217          delete_volume(ua);     
1218          return 1;
1219       case 1:
1220          delete_pool(ua);
1221          return 1;
1222       default:
1223          break;
1224    }
1225    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1226       case 0:
1227          delete_volume(ua);
1228          break;
1229       case 1:
1230          delete_pool(ua);
1231          break;
1232       default:
1233          bsendmsg(ua, _("Nothing done.\n"));
1234          break;
1235    }
1236    return 1;
1237 }
1238
1239 /*
1240  * Delete media records from database -- dangerous 
1241  */
1242 static int delete_volume(UAContext *ua)
1243 {
1244    POOL_DBR pr;
1245    MEDIA_DBR mr;
1246
1247    if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1248       return 1;
1249    }
1250    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1251       "and all Jobs saved on that volume from the Catalog\n"),
1252       mr.VolumeName);
1253
1254    if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1255       return 1;
1256    }
1257    if (strcasecmp(ua->cmd, _("yes")) == 0) {
1258       db_delete_media_record(ua->jcr, ua->db, &mr);
1259    }
1260    return 1;
1261 }
1262
1263 /*
1264  * Delete a pool record from the database -- dangerous   
1265  */
1266 static int delete_pool(UAContext *ua)
1267 {
1268    POOL_DBR  pr;
1269    
1270    memset(&pr, 0, sizeof(pr));
1271
1272    if (!get_pool_dbr(ua, &pr)) {
1273       return 1;
1274    }
1275    if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1276       return 1;
1277    }
1278    if (strcasecmp(ua->cmd, _("yes")) == 0) {
1279       db_delete_pool_record(ua->jcr, ua->db, &pr);
1280    }
1281    return 1;
1282 }
1283
1284
1285 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1286 {
1287    STORE *store;
1288    BSOCK *sd;
1289    char dev_name[MAX_NAME_LENGTH];
1290
1291
1292    if (!open_db(ua)) {
1293       return;
1294    }
1295    Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1296
1297    store = get_storage_resource(ua, cmd, 1);
1298    if (!store) {
1299       return;
1300    }
1301
1302    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1303       store->media_type, store->dev_name);
1304
1305    ua->jcr->store = store;
1306    if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1307       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1308       return;
1309    }
1310    sd = ua->jcr->store_bsock;
1311    strcpy(dev_name, store->dev_name);
1312    bash_spaces(dev_name);
1313    if (mount) {
1314       bnet_fsend(sd, "mount %s", dev_name);
1315    } else {
1316       bnet_fsend(sd, "unmount %s", dev_name);
1317    }
1318    while (bnet_recv(sd) >= 0) {
1319       bsendmsg(ua, "%s", sd->msg);
1320       if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1321           /***** ****FIXME**** fix JobStatus */
1322       }
1323    }
1324    bnet_sig(sd, BNET_TERMINATE);
1325    bnet_close(sd);
1326    ua->jcr->store_bsock = NULL;
1327 }
1328
1329 /*
1330  * mount [storage | device] <name>
1331  */
1332 static int mountcmd(UAContext *ua, char *cmd)
1333 {
1334    do_mount_cmd(1, ua, cmd);          /* mount */
1335    return 1;
1336 }
1337
1338
1339 /*
1340  * unmount [storage | device] <name>
1341  */
1342 static int unmountcmd(UAContext *ua, char *cmd)
1343 {
1344    do_mount_cmd(0, ua, cmd);          /* unmount */
1345    return 1;
1346 }
1347
1348
1349 /*
1350  * Switch databases
1351  *   use catalog=<name>
1352  */
1353 static int usecmd(UAContext *ua, char *cmd)
1354 {
1355    CAT *oldcatalog, *catalog;
1356
1357
1358    close_db(ua);                      /* close any previously open db */
1359    oldcatalog = ua->catalog;
1360
1361    if (!(catalog = get_catalog_resource(ua))) {
1362       ua->catalog = oldcatalog;
1363    } else {
1364       ua->catalog = catalog;
1365    }
1366    if (open_db(ua)) {
1367       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1368          ua->catalog->hdr.name, ua->catalog->db_name);
1369    }
1370    return 1;
1371 }
1372
1373 int quitcmd(UAContext *ua, char *cmd) 
1374 {
1375    ua->quit = TRUE;
1376    return 1;
1377 }
1378
1379 static int helpcmd(UAContext *ua, char *cmd)
1380 {
1381    unsigned int i;
1382
1383 /* usage(); */
1384    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1385    for (i=0; i<comsize; i++) {
1386       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1387    }
1388    bsendmsg(ua, "\n");
1389    return 1;
1390 }
1391
1392 static int versioncmd(UAContext *ua, char *cmd)
1393 {
1394    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1395    return 1;
1396 }
1397
1398
1399 /* A bit brain damaged in that if the user has not done
1400  * a "use catalog xxx" command, we simply find the first
1401  * catalog resource and open it.
1402  */
1403 int open_db(UAContext *ua)
1404 {
1405    if (ua->db) {
1406       return 1;
1407    }
1408    if (!ua->catalog) {
1409       LockRes();
1410       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1411       UnlockRes();
1412       if (!ua->catalog) {    
1413          bsendmsg(ua, _("Could not find a Catalog resource\n"));
1414          return 0;
1415       } else {
1416          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"), 
1417             ua->catalog->hdr.name, ua->catalog->db_name);
1418       }
1419    }
1420
1421    Dmsg0(150, "Open database\n");
1422    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1423                              ua->catalog->db_password, ua->catalog->db_address,
1424                              ua->catalog->db_port, ua->catalog->db_socket);
1425    if (!db_open_database(ua->jcr, ua->db)) {
1426       bsendmsg(ua, _("Could not open DB %s: ERR=%s"), 
1427          ua->catalog->db_name, db_strerror(ua->db));
1428       close_db(ua);
1429       return 0;
1430    }
1431    ua->jcr->db = ua->db;
1432    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1433    return 1;
1434 }
1435
1436 void close_db(UAContext *ua)
1437 {
1438    if (ua->db) {
1439       db_close_database(ua->jcr, ua->db);
1440    }
1441    ua->db = NULL;
1442    ua->jcr->db = NULL;
1443 }