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