]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
0c479ccd7c5be46b09e8b9102c92549ce5e86c0b
[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    POOL_DBR pr;
653    MEDIA_DBR mr;
654    POOLMEM *query;
655    char ed1[30];
656
657    if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
658       return 0;
659    }
660
661    for (int done=0; !done; ) {
662       if (!db_get_media_record(ua->jcr, ua->db, &mr)) {
663          if (mr.MediaId != 0) {
664             bsendmsg(ua, _("Volume record for MediaId %d not found.\n"), mr.MediaId);
665          } else {
666             bsendmsg(ua, _("Volume record for %s not found.\n"), mr.VolumeName);
667          }
668          return 0;
669       }
670       bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
671       start_prompt(ua, _("Parameters to modify:\n"));
672       add_prompt(ua, _("Volume Status"));
673       add_prompt(ua, _("Volume Retention Period"));
674       add_prompt(ua, _("Volume Use Duration"));
675       add_prompt(ua, _("Maximum Volume Jobs"));
676       add_prompt(ua, _("Maximum Volume Files"));
677       add_prompt(ua, _("Maximum Volume Bytes"));
678       add_prompt(ua, _("Recycle Flag"));
679       add_prompt(ua, _("Slot"));
680       add_prompt(ua, _("Volume Files"));
681       add_prompt(ua, _("Done"));
682       switch (do_prompt(ua, _("Select parameter to modify"), NULL, 0)) {
683       case 0:                         /* Volume Status */
684          /* Modify Volume Status */
685          bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
686          start_prompt(ua, _("Possible Values are:\n"));
687          add_prompt(ua, "Append");      /* Better not translate these as */
688          add_prompt(ua, "Archive");     /* They are known in the database code */
689          add_prompt(ua, "Disabled");
690          add_prompt(ua, "Full");
691          add_prompt(ua, "Used");
692          if (strcmp(mr.VolStatus, "Purged") == 0) {
693             add_prompt(ua, "Recycle");
694          }
695          add_prompt(ua, "Read-Only");
696          if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
697             return 1;
698          }
699          bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
700          query = get_pool_memory(PM_MESSAGE);
701          Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
702             mr.VolStatus, mr.MediaId);
703          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
704             bsendmsg(ua, "%s", db_strerror(ua->db));
705          } else {
706             bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
707          }
708          free_pool_memory(query);
709          break;
710       case 1:                         /* Retention */
711          bsendmsg(ua, _("Current retention seconds is: %s\n"),
712             edit_utime(mr.VolRetention, ed1));
713          if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
714             return 0;
715          }
716          if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
717             bsendmsg(ua, _("Invalid retention period specified.\n"));
718             break;
719          }
720          query = get_pool_memory(PM_MESSAGE);
721          Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
722             edit_uint64(mr.VolRetention, ed1), mr.MediaId);
723          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
724             bsendmsg(ua, "%s", db_strerror(ua->db));
725          } else {
726             bsendmsg(ua, _("New retention seconds is: %s\n"),
727                edit_utime(mr.VolRetention, ed1));
728          }
729          free_pool_memory(query);
730          break;
731
732       case 2:                         /* Use Duration */
733          bsendmsg(ua, _("Current use duration is: %s\n"),
734             edit_utime(mr.VolUseDuration, ed1));
735          if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
736             return 0;
737          }
738          if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
739             bsendmsg(ua, _("Invalid use duration specified.\n"));
740             break;
741          }
742          query = get_pool_memory(PM_MESSAGE);
743          Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
744             edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
745          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
746             bsendmsg(ua, "%s", db_strerror(ua->db));
747          } else {
748             bsendmsg(ua, _("New use duration is: %s\n"),
749                edit_utime(mr.VolUseDuration, ed1));
750          }
751          free_pool_memory(query);
752          break;
753
754       case 3:                         /* Max Jobs */
755          int32_t maxjobs;
756          bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
757          if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
758             return 0;
759          }
760          maxjobs = atoi(ua->cmd);
761          if (maxjobs < 0) {
762             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
763             break;
764          } 
765          query = get_pool_memory(PM_MESSAGE);
766          Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
767             maxjobs, mr.MediaId);
768          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
769             bsendmsg(ua, "%s", db_strerror(ua->db));
770          } else {
771             bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
772          }
773          free_pool_memory(query);
774          break;
775
776       case 4:                         /* Max Files */
777          int32_t maxfiles;
778          bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
779          if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
780             return 0;
781          }
782          maxfiles = atoi(ua->cmd);
783          if (maxfiles < 0) {
784             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
785             break;
786          } 
787          query = get_pool_memory(PM_MESSAGE);
788          Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
789             maxfiles, mr.MediaId);
790          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
791             bsendmsg(ua, "%s", db_strerror(ua->db));
792          } else {
793             bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
794          }
795          free_pool_memory(query);
796          break;
797
798       case 5:                         /* Max Bytes */
799          uint64_t maxbytes;
800          bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
801          if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
802             return 0;
803          }
804          if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
805             bsendmsg(ua, _("Invalid byte size specification.\n"));
806             break;
807          } 
808          query = get_pool_memory(PM_MESSAGE);
809          Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
810             edit_uint64(maxbytes, ed1), mr.MediaId);
811          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
812             bsendmsg(ua, "%s", db_strerror(ua->db));
813          } else {
814             bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
815          }
816          free_pool_memory(query);
817          break;
818
819
820       case 6:                         /* Recycle */
821          int recycle;
822          bsendmsg(ua, _("Current recycle flag is: %s\n"),
823             mr.Recycle==1?_("yes"):_("no"));
824          if (!get_cmd(ua, _("Enter new Recycle status: "))) {
825             return 0;
826          }
827          if (strcasecmp(ua->cmd, _("yes")) == 0) {
828             recycle = 1;
829          } else if (strcasecmp(ua->cmd, _("no")) == 0) {
830             recycle = 0;
831          } else {
832             bsendmsg(ua, _("Invalid recycle status specified.\n"));
833             break;
834          }
835          query = get_pool_memory(PM_MESSAGE);
836          Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
837             recycle, mr.MediaId);
838          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
839             bsendmsg(ua, "%s", db_strerror(ua->db));
840          } else {       
841             bsendmsg(ua, _("New recycle flag is: %s\n"),
842                mr.Recycle==1?_("yes"):_("no"));
843          }
844          free_pool_memory(query);
845          break;
846
847       case 7:                         /* Slot */
848          int slot;
849          bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
850          if (!get_cmd(ua, _("Enter new Slot: "))) {
851             return 0;
852          }
853          slot = atoi(ua->cmd);
854          if (slot < 0) {
855             bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
856             break;
857          } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
858             bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
859                pr.MaxVols);
860             break;
861          }
862          query = get_pool_memory(PM_MESSAGE);
863          Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
864             slot, mr.MediaId);
865          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
866             bsendmsg(ua, "%s", db_strerror(ua->db));
867          } else {
868             bsendmsg(ua, "New Slot is: %d\n", slot);
869          }
870          free_pool_memory(query);
871          break;
872
873       case 8:                         /* Volume Files */
874          int32_t VolFiles;
875          bsendmsg(ua, _("Warning changing Volume Files can result\n"
876                         "in loss of data on your Volume\n\n"));
877          bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
878          if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
879             return 0;
880          }
881          VolFiles = atoi(ua->cmd);
882          if (VolFiles < 0) {
883             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
884             break;
885          } 
886          if (VolFiles != (int)(mr.VolFiles + 1)) {
887             bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
888             if (!get_cmd(ua, _("Continue? (yes/no): ")) || 
889                  strcasecmp(ua->cmd, "yes") != 0) {
890                break;
891             }
892          }
893          query = get_pool_memory(PM_MESSAGE);
894          Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
895             VolFiles, mr.MediaId);
896          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
897             bsendmsg(ua, "%s", db_strerror(ua->db));
898          } else {
899             bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
900          }
901          free_pool_memory(query);
902          break;
903
904       default:                        /* Done or error */
905          bsendmsg(ua, "Selection done.\n");
906          return 1;
907       }
908    }
909    return 1;
910 }
911
912 /* 
913  * Update pool record -- pull info from current POOL resource
914  */
915 static int update_pool(UAContext *ua)
916 {
917    POOL_DBR  pr;
918    int id;
919    POOL *pool;
920    POOLMEM *query;       
921    
922    
923    pool = get_pool_resource(ua);
924    if (!pool) {
925       return 0;
926    }
927
928    memset(&pr, 0, sizeof(pr));
929    strcpy(pr.Name, pool->hdr.name);
930    if (!get_pool_dbr(ua, &pr)) {
931       return 0;
932    }
933
934    set_pooldbr_from_poolres(&pr, pool, 0); /* update */
935
936    id = db_update_pool_record(ua->jcr, ua->db, &pr);
937    if (id <= 0) {
938       bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
939          id, db_strerror(ua->db));
940    }
941    query = get_pool_memory(PM_MESSAGE);
942    Mmsg(&query, list_pool, pr.PoolId);
943    db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
944    free_pool_memory(query);
945    bsendmsg(ua, _("Pool DB record updated from resource.\n"));
946    return 1;
947 }
948
949
950 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
951 {
952    BSOCK *sd;
953
954    ua->jcr->store = store;
955    /* Try connecting for up to 15 seconds */
956    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
957       store->hdr.name, store->address, store->SDport);
958    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
959       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
960       return;
961    }
962    Dmsg0(120, _("Connected to storage daemon\n"));
963    sd = ua->jcr->store_bsock;
964    bnet_fsend(sd, "setdebug=%d\n", level);
965    if (bnet_recv(sd) >= 0) {
966       bsendmsg(ua, "%s", sd->msg);
967    }
968    bnet_sig(sd, BNET_TERMINATE);
969    bnet_close(sd);
970    ua->jcr->store_bsock = NULL;
971    return;  
972 }
973    
974 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
975 {
976    BSOCK *fd;
977
978    /* Connect to File daemon */
979
980    ua->jcr->client = client;
981    /* Try to connect for 15 seconds */
982    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
983       client->hdr.name, client->address, client->FDport);
984    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
985       bsendmsg(ua, _("Failed to connect to Client.\n"));
986       return;
987    }
988    Dmsg0(120, "Connected to file daemon\n");
989    fd = ua->jcr->file_bsock;
990    bnet_fsend(fd, "setdebug=%d\n", level);
991    if (bnet_recv(fd) >= 0) {
992       bsendmsg(ua, "%s", fd->msg);
993    }
994    bnet_sig(fd, BNET_TERMINATE);
995    bnet_close(fd);
996    ua->jcr->file_bsock = NULL;
997
998    return;  
999 }
1000
1001
1002 static void do_all_setdebug(UAContext *ua, int level)
1003 {
1004    STORE *store, **unique_store;
1005    CLIENT *client, **unique_client;
1006    int i, j, found;
1007
1008    /* Director */
1009    debug_level = level;
1010
1011    /* Count Storage items */
1012    LockRes();
1013    store = NULL;
1014    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1015       { }
1016    unique_store = (STORE **) malloc(i * sizeof(STORE));
1017    /* Find Unique Storage address/port */         
1018    store = (STORE *)GetNextRes(R_STORAGE, NULL);
1019    i = 0;
1020    unique_store[i++] = store;
1021    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1022       found = 0;
1023       for (j=0; j<i; j++) {
1024          if (strcmp(unique_store[j]->address, store->address) == 0 &&
1025              unique_store[j]->SDport == store->SDport) {
1026             found = 1;
1027             break;
1028          }
1029       }
1030       if (!found) {
1031          unique_store[i++] = store;
1032          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1033       }
1034    }
1035    UnlockRes();
1036
1037    /* Call each unique Storage daemon */
1038    for (j=0; j<i; j++) {
1039       do_storage_setdebug(ua, unique_store[j], level);
1040    }
1041    free(unique_store);
1042
1043    /* Count Client items */
1044    LockRes();
1045    client = NULL;
1046    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1047       { }
1048    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1049    /* Find Unique Client address/port */         
1050    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1051    i = 0;
1052    unique_client[i++] = client;
1053    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1054       found = 0;
1055       for (j=0; j<i; j++) {
1056          if (strcmp(unique_client[j]->address, client->address) == 0 &&
1057              unique_client[j]->FDport == client->FDport) {
1058             found = 1;
1059             break;
1060          }
1061       }
1062       if (!found) {
1063          unique_client[i++] = client;
1064          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1065       }
1066    }
1067    UnlockRes();
1068
1069    /* Call each unique File daemon */
1070    for (j=0; j<i; j++) {
1071       do_client_setdebug(ua, unique_client[j], level);
1072    }
1073    free(unique_client);
1074 }
1075
1076 /*
1077  * setdebug level=nn all
1078  */
1079 static int setdebugcmd(UAContext *ua, char *cmd)
1080 {
1081    STORE *store;
1082    CLIENT *client;
1083    int level;
1084    int i;
1085
1086    if (!open_db(ua)) {
1087       return 1;
1088    }
1089    Dmsg1(120, "setdebug:%s:\n", cmd);
1090
1091    level = -1;
1092    for (i=1; i<ua->argc; i++) {
1093       if (strcasecmp(ua->argk[i], _("level")) == 0 && ua->argv[i]) {
1094          level = atoi(ua->argv[i]);
1095          break;
1096       }
1097    }
1098    if (level < 0) {
1099       if (!get_cmd(ua, _("Enter new debug level: "))) {
1100          return 1;
1101       }
1102       level = atoi(ua->cmd);
1103    }
1104    if (level < 0) {
1105       bsendmsg(ua, _("level cannot be negative.\n"));
1106       return 1;
1107    }
1108
1109    /* General debug? */
1110    for (i=1; i<ua->argc; i++) {
1111       if (strcasecmp(ua->argk[i], _("all")) == 0) {
1112          do_all_setdebug(ua, level);
1113          return 1;
1114       }
1115       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1116           strcasecmp(ua->argk[i], _("director")) == 0) {
1117          debug_level = level;
1118          return 1;
1119       }
1120       if (strcasecmp(ua->argk[i], _("client")) == 0) {
1121          client = NULL;
1122          if (ua->argv[i]) {
1123             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1124             if (client) {
1125                do_client_setdebug(ua, client, level);
1126                return 1;
1127             }
1128          }
1129          client = select_client_resource(ua);   
1130          if (client) {
1131             do_client_setdebug(ua, client, level);
1132             return 1;
1133          }
1134       }
1135
1136       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1137           strcasecmp(ua->argk[i], _("storage")) == 0) {
1138          store = NULL;
1139          if (ua->argv[i]) {
1140             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1141             if (store) {
1142                do_storage_setdebug(ua, store, level);
1143                return 1;
1144             }
1145          }
1146          store = get_storage_resource(ua, 0);
1147          if (store) {
1148             do_storage_setdebug(ua, store, level);
1149             return 1;
1150          }
1151       }
1152    } 
1153    /*
1154     * We didn't find an appropriate keyword above, so
1155     * prompt the user.
1156     */
1157    start_prompt(ua, _("Available daemons are: \n"));
1158    add_prompt(ua, _("Director"));
1159    add_prompt(ua, _("Storage"));
1160    add_prompt(ua, _("Client"));
1161    add_prompt(ua, _("All"));
1162    switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1163       case 0:                         /* Director */
1164          debug_level = level;
1165          break;
1166       case 1:
1167          store = get_storage_resource(ua, 0);
1168          if (store) {
1169             do_storage_setdebug(ua, store, level);
1170          }
1171          break;
1172       case 2:
1173          client = select_client_resource(ua);
1174          if (client) {
1175             do_client_setdebug(ua, client, level);
1176          }
1177          break;
1178       case 3:
1179          do_all_setdebug(ua, level);
1180          break;
1181       default:
1182          break;
1183    }
1184    return 1;
1185 }
1186
1187 /*
1188  * print time
1189  */
1190 static int timecmd(UAContext *ua, char *cmd)
1191 {
1192    char sdt[50];
1193    time_t ttime = time(NULL);
1194    struct tm tm;
1195    localtime_r(&ttime, &tm);
1196    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1197    bsendmsg(ua, "%s\n", sdt);
1198    return 1;
1199 }
1200
1201
1202
1203 /*
1204  * Delete Pool records (should purge Media with it).
1205  *
1206  *  delete pool=<pool-name>
1207  *  delete media pool=<pool-name> volume=<name>
1208  */
1209 static int deletecmd(UAContext *ua, char *cmd)
1210 {
1211    static char *keywords[] = {
1212       N_("volume"),
1213       N_("pool"),
1214       NULL};
1215
1216    if (!open_db(ua)) {
1217       return 1;
1218    }
1219
1220    bsendmsg(ua, _(
1221 "In general it is not a good idea to delete either a\n"
1222 "Pool or a Volume since they may contain data.\n\n"));
1223      
1224    switch (find_arg_keyword(ua, keywords)) {
1225       case 0:
1226          delete_volume(ua);     
1227          return 1;
1228       case 1:
1229          delete_pool(ua);
1230          return 1;
1231       default:
1232          break;
1233    }
1234    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1235       case 0:
1236          delete_volume(ua);
1237          break;
1238       case 1:
1239          delete_pool(ua);
1240          break;
1241       default:
1242          bsendmsg(ua, _("Nothing done.\n"));
1243          break;
1244    }
1245    return 1;
1246 }
1247
1248 /*
1249  * Delete media records from database -- dangerous 
1250  */
1251 static int delete_volume(UAContext *ua)
1252 {
1253    POOL_DBR pr;
1254    MEDIA_DBR mr;
1255
1256    if (!select_pool_and_media_dbr(ua, &pr, &mr)) {
1257       return 1;
1258    }
1259    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1260       "and all Jobs saved on that volume from the Catalog\n"),
1261       mr.VolumeName);
1262
1263    if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1264       return 1;
1265    }
1266    if (strcasecmp(ua->cmd, _("yes")) == 0) {
1267       db_delete_media_record(ua->jcr, ua->db, &mr);
1268    }
1269    return 1;
1270 }
1271
1272 /*
1273  * Delete a pool record from the database -- dangerous   
1274  */
1275 static int delete_pool(UAContext *ua)
1276 {
1277    POOL_DBR  pr;
1278    
1279    memset(&pr, 0, sizeof(pr));
1280
1281    if (!get_pool_dbr(ua, &pr)) {
1282       return 1;
1283    }
1284    if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1285       return 1;
1286    }
1287    if (strcasecmp(ua->cmd, _("yes")) == 0) {
1288       db_delete_pool_record(ua->jcr, ua->db, &pr);
1289    }
1290    return 1;
1291 }
1292
1293
1294 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1295 {
1296    STORE *store;
1297    BSOCK *sd;
1298    char dev_name[MAX_NAME_LENGTH];
1299
1300
1301    if (!open_db(ua)) {
1302       return;
1303    }
1304    Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1305
1306    store = get_storage_resource(ua, 1);
1307    if (!store) {
1308       return;
1309    }
1310
1311    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1312       store->media_type, store->dev_name);
1313
1314    ua->jcr->store = store;
1315    if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1316       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1317       return;
1318    }
1319    sd = ua->jcr->store_bsock;
1320    strcpy(dev_name, store->dev_name);
1321    bash_spaces(dev_name);
1322    if (mount) {
1323       bnet_fsend(sd, "mount %s", dev_name);
1324    } else {
1325       bnet_fsend(sd, "unmount %s", dev_name);
1326    }
1327    while (bnet_recv(sd) >= 0) {
1328       bsendmsg(ua, "%s", sd->msg);
1329       if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1330           /***** ****FIXME**** fix JobStatus */
1331       }
1332    }
1333    bnet_sig(sd, BNET_TERMINATE);
1334    bnet_close(sd);
1335    ua->jcr->store_bsock = NULL;
1336 }
1337
1338 /*
1339  * mount [storage | device] <name>
1340  */
1341 static int mountcmd(UAContext *ua, char *cmd)
1342 {
1343    do_mount_cmd(1, ua, cmd);          /* mount */
1344    return 1;
1345 }
1346
1347
1348 /*
1349  * unmount [storage | device] <name>
1350  */
1351 static int unmountcmd(UAContext *ua, char *cmd)
1352 {
1353    do_mount_cmd(0, ua, cmd);          /* unmount */
1354    return 1;
1355 }
1356
1357
1358 /*
1359  * Switch databases
1360  *   use catalog=<name>
1361  */
1362 static int usecmd(UAContext *ua, char *cmd)
1363 {
1364    CAT *oldcatalog, *catalog;
1365
1366
1367    close_db(ua);                      /* close any previously open db */
1368    oldcatalog = ua->catalog;
1369
1370    if (!(catalog = get_catalog_resource(ua))) {
1371       ua->catalog = oldcatalog;
1372    } else {
1373       ua->catalog = catalog;
1374    }
1375    if (open_db(ua)) {
1376       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1377          ua->catalog->hdr.name, ua->catalog->db_name);
1378    }
1379    return 1;
1380 }
1381
1382 int quitcmd(UAContext *ua, char *cmd) 
1383 {
1384    ua->quit = TRUE;
1385    return 1;
1386 }
1387
1388 static int helpcmd(UAContext *ua, char *cmd)
1389 {
1390    unsigned int i;
1391
1392 /* usage(); */
1393    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1394    for (i=0; i<comsize; i++) {
1395       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1396    }
1397    bsendmsg(ua, "\n");
1398    return 1;
1399 }
1400
1401 static int versioncmd(UAContext *ua, char *cmd)
1402 {
1403    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1404    return 1;
1405 }
1406
1407
1408 /* A bit brain damaged in that if the user has not done
1409  * a "use catalog xxx" command, we simply find the first
1410  * catalog resource and open it.
1411  */
1412 int open_db(UAContext *ua)
1413 {
1414    if (ua->db) {
1415       return 1;
1416    }
1417    if (!ua->catalog) {
1418       LockRes();
1419       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1420       UnlockRes();
1421       if (!ua->catalog) {    
1422          bsendmsg(ua, _("Could not find a Catalog resource\n"));
1423          return 0;
1424       } else {
1425          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"), 
1426             ua->catalog->hdr.name, ua->catalog->db_name);
1427       }
1428    }
1429
1430    Dmsg0(150, "Open database\n");
1431    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1432                              ua->catalog->db_password, ua->catalog->db_address,
1433                              ua->catalog->db_port, ua->catalog->db_socket);
1434    if (!db_open_database(ua->jcr, ua->db)) {
1435       bsendmsg(ua, _("Could not open DB %s: ERR=%s"), 
1436          ua->catalog->db_name, db_strerror(ua->db));
1437       close_db(ua);
1438       return 0;
1439    }
1440    ua->jcr->db = ua->db;
1441    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1442    return 1;
1443 }
1444
1445 void close_db(UAContext *ua)
1446 {
1447    if (ua->db) {
1448       db_close_database(ua->jcr, ua->db);
1449    }
1450    ua->db = NULL;
1451    ua->jcr->db = NULL;
1452 }