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