]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
Misc see kes-1.30a
[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_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 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    case JS_WaitJobRes:
427    case JS_WaitClientRes:
428    case JS_WaitStoreRes:
429    case JS_WaitMaxJobs:
430    case JS_WaitStartTime:
431       set_jcr_job_status(jcr, JS_Canceled);
432       bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
433               jcr->JobId, jcr->Job);
434 #ifndef USE_SEMAPHORE
435       workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
436 #endif
437       free_jcr(jcr);
438       return 1;
439          
440    default:
441       set_jcr_job_status(jcr, JS_Canceled);
442
443       /* Cancel File daemon */
444       if (jcr->file_bsock) {
445          ua->jcr->client = jcr->client;
446          if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
447             bsendmsg(ua, _("Failed to connect to File daemon.\n"));
448             free_jcr(jcr);
449             return 1;
450          }
451          Dmsg0(200, "Connected to file daemon\n");
452          fd = ua->jcr->file_bsock;
453          bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
454          while (bnet_recv(fd) >= 0) {
455             bsendmsg(ua, "%s", fd->msg);
456          }
457          bnet_sig(fd, BNET_TERMINATE);
458          bnet_close(fd);
459          ua->jcr->file_bsock = NULL;
460       }
461
462       /* Cancel Storage daemon */
463       if (jcr->store_bsock) {
464          ua->jcr->store = jcr->store;
465          if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
466             bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
467             free_jcr(jcr);
468             return 1;
469          }
470          Dmsg0(200, "Connected to storage daemon\n");
471          sd = ua->jcr->store_bsock;
472          bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
473          while (bnet_recv(sd) >= 0) {
474             bsendmsg(ua, "%s", sd->msg);
475          }
476          bnet_sig(sd, BNET_TERMINATE);
477          bnet_close(sd);
478          ua->jcr->store_bsock = NULL;
479       }
480    }
481    free_jcr(jcr);
482
483    return 1; 
484 }
485
486 /*
487  * This is a common routine to create or update a
488  *   Pool DB base record from a Pool Resource. We handle
489  *   the setting of MaxVols and NumVols slightly differently
490  *   depending on if we are creating the Pool or we are
491  *   simply bringing it into agreement with the resource (updage).
492  */
493 void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, int create)
494 {
495    strcpy(pr->PoolType, pool->pool_type);
496    if (create) {
497       pr->MaxVols = pool->max_volumes;
498       pr->NumVols = 0;
499    } else {          /* update pool */
500       if (pr->MaxVols != pool->max_volumes) {
501          pr->MaxVols = pool->max_volumes;
502       }
503       if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
504          pr->MaxVols = pr->NumVols;
505       }
506    }
507    pr->UseOnce = pool->use_volume_once;
508    pr->UseCatalog = pool->use_catalog;
509    pr->AcceptAnyVolume = pool->accept_any_volume;
510    pr->Recycle = pool->Recycle;
511    pr->VolRetention = pool->VolRetention;
512    pr->VolUseDuration = pool->VolUseDuration;
513    pr->MaxVolJobs = pool->MaxVolJobs;
514    pr->MaxVolFiles = pool->MaxVolFiles;
515    pr->MaxVolBytes = pool->MaxVolBytes;
516    if (pool->label_format) {
517       strcpy(pr->LabelFormat, pool->label_format);
518    } else {
519       strcpy(pr->LabelFormat, "*");    /* none */
520    }
521 }
522
523
524 /*
525  * Create a pool record from a given Pool resource
526  *   Also called from backup.c
527  * Returns: -1  on error
528  *           0  record already exists
529  *           1  record created
530  */
531
532 int create_pool(JCR *jcr, B_DB *db, POOL *pool, int create)
533 {
534    POOL_DBR  pr;
535
536    memset(&pr, 0, sizeof(POOL_DBR));
537
538    strcpy(pr.Name, pool->hdr.name);
539
540    if (db_get_pool_record(jcr, db, &pr)) {
541       /* Pool Exists */
542       if (!create) {  /* update request */
543          set_pooldbr_from_poolres(&pr, pool, 0);
544          db_update_pool_record(jcr, db, &pr);
545       }
546       return 0;                       /* exists */
547    }
548
549    set_pooldbr_from_poolres(&pr, pool, 1);
550
551    if (!db_create_pool_record(jcr, db, &pr)) {
552       return -1;                      /* error */
553    }
554    return 1;
555 }
556
557
558
559 /*
560  * Create a Pool Record in the database.
561  *  It is always created from the Resource record.
562  */
563 static int createcmd(UAContext *ua, char *cmd) 
564 {
565    POOL *pool;
566
567    if (!open_db(ua)) {
568       return 1;
569    }
570
571    pool = get_pool_resource(ua);
572    if (!pool) {
573       return 1;
574    }
575
576    switch (create_pool(ua->jcr, ua->db, pool, 1)) {
577    case 0:
578       bsendmsg(ua, _("Error: Pool %s already exists.\n\
579 Use update to change it.\n"), pool->hdr.name);
580       break;
581
582    case -1:
583       bsendmsg(ua, db_strerror(ua->db));
584       break;
585
586    default:
587      break;
588    }
589    bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
590    return 1;
591 }
592
593
594
595
596 /*
597  * Update a Pool Record in the database.
598  *  It is always updated from the Resource record.
599  *
600  *    update pool=<pool-name>
601  *         updates pool from Pool resource
602  *    update media pool=<pool-name> volume=<volume-name>
603  *         changes pool info for volume
604  */
605 static int updatecmd(UAContext *ua, char *cmd) 
606 {
607    static char *kw[] = {
608       N_("media"),  /* 0 */
609       N_("volume"), /* 1 */
610       N_("pool"),   /* 2 */
611       N_("slots"),  /* 3 */
612       NULL};
613
614    if (!open_db(ua)) {
615       return 1;
616    }
617
618    switch (find_arg_keyword(ua, kw)) {
619       case 0:
620       case 1:
621          update_volume(ua);
622          return 1;
623       case 2:
624          update_pool(ua);
625          return 1;
626       case 3:
627          update_slots(ua);
628          return 1;
629       default:
630          break;
631    }
632     
633    start_prompt(ua, _("Update choice:\n"));
634    add_prompt(ua, _("Volume parameters"));
635    add_prompt(ua, _("Pool from resource"));
636    add_prompt(ua, _("Slots from autochanger"));
637    switch (do_prompt(ua, _("Choose catalog item to update"), NULL, 0)) {
638       case 0:
639          update_volume(ua);
640          break;
641       case 1:
642          update_pool(ua);
643          break;
644       case 2:
645          update_slots(ua);
646          break;
647       default:
648          break;
649    }
650    return 1;
651 }
652
653 /*
654  * Update a media record -- allows you to change the
655  *  Volume status. E.g. if you want Bacula to stop
656  *  writing on the volume, set it to anything other
657  *  than Append.
658  */              
659 static int update_volume(UAContext *ua)
660 {
661    MEDIA_DBR mr;
662    POOLMEM *query;
663    char ed1[30];
664
665
666    for (int done=0; !done; ) {
667       if (!select_media_dbr(ua, &mr)) {
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          add_prompt(ua, "Cleaning");
693          if (strcmp(mr.VolStatus, "Purged") == 0) {
694             add_prompt(ua, "Recycle");
695          }
696          add_prompt(ua, "Read-Only");
697          if (do_prompt(ua, _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
698             return 1;
699          }
700          bstrncpy(mr.VolStatus, ua->cmd, sizeof(mr.VolStatus));
701          query = get_pool_memory(PM_MESSAGE);
702          Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
703             mr.VolStatus, mr.MediaId);
704          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
705             bsendmsg(ua, "%s", db_strerror(ua->db));
706          } else {
707             bsendmsg(ua, _("New Volume status is: %s\n"), mr.VolStatus);
708          }
709          free_pool_memory(query);
710          break;
711       case 1:                         /* Retention */
712          bsendmsg(ua, _("Current retention seconds is: %s\n"),
713             edit_utime(mr.VolRetention, ed1));
714          if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
715             return 0;
716          }
717          if (!duration_to_utime(ua->cmd, &mr.VolRetention)) {
718             bsendmsg(ua, _("Invalid retention period specified.\n"));
719             break;
720          }
721          query = get_pool_memory(PM_MESSAGE);
722          Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
723             edit_uint64(mr.VolRetention, ed1), mr.MediaId);
724          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
725             bsendmsg(ua, "%s", db_strerror(ua->db));
726          } else {
727             bsendmsg(ua, _("New retention seconds is: %s\n"),
728                edit_utime(mr.VolRetention, ed1));
729          }
730          free_pool_memory(query);
731          break;
732
733       case 2:                         /* Use Duration */
734          bsendmsg(ua, _("Current use duration is: %s\n"),
735             edit_utime(mr.VolUseDuration, ed1));
736          if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
737             return 0;
738          }
739          if (!duration_to_utime(ua->cmd, &mr.VolUseDuration)) {
740             bsendmsg(ua, _("Invalid use duration specified.\n"));
741             break;
742          }
743          query = get_pool_memory(PM_MESSAGE);
744          Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
745             edit_uint64(mr.VolUseDuration, ed1), mr.MediaId);
746          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
747             bsendmsg(ua, "%s", db_strerror(ua->db));
748          } else {
749             bsendmsg(ua, _("New use duration is: %s\n"),
750                edit_utime(mr.VolUseDuration, ed1));
751          }
752          free_pool_memory(query);
753          break;
754
755       case 3:                         /* Max Jobs */
756          int32_t maxjobs;
757          bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
758          if (!get_cmd(ua, _("Enter new Maximum Jobs: "))) {
759             return 0;
760          }
761          maxjobs = atoi(ua->cmd);
762          if (maxjobs < 0) {
763             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
764             break;
765          } 
766          query = get_pool_memory(PM_MESSAGE);
767          Mmsg(&query, "UPDATE Media SET MaxVolJobs=%u WHERE MediaId=%u",
768             maxjobs, mr.MediaId);
769          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
770             bsendmsg(ua, "%s", db_strerror(ua->db));
771          } else {
772             bsendmsg(ua, _("New max jobs is: %u\n"), maxjobs);
773          }
774          free_pool_memory(query);
775          break;
776
777       case 4:                         /* Max Files */
778          int32_t maxfiles;
779          bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
780          if (!get_cmd(ua, _("Enter new Maximum Files: "))) {
781             return 0;
782          }
783          maxfiles = atoi(ua->cmd);
784          if (maxfiles < 0) {
785             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
786             break;
787          } 
788          query = get_pool_memory(PM_MESSAGE);
789          Mmsg(&query, "UPDATE Media SET MaxVolFiles=%u WHERE MediaId=%u",
790             maxfiles, mr.MediaId);
791          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
792             bsendmsg(ua, "%s", db_strerror(ua->db));
793          } else {
794             bsendmsg(ua, _("New max files is: %u\n"), maxfiles);
795          }
796          free_pool_memory(query);
797          break;
798
799       case 5:                         /* Max Bytes */
800          uint64_t maxbytes;
801          bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
802          if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
803             return 0;
804          }
805          if (!size_to_uint64(ua->cmd, strlen(ua->cmd), &maxbytes)) {
806             bsendmsg(ua, _("Invalid byte size specification.\n"));
807             break;
808          } 
809          query = get_pool_memory(PM_MESSAGE);
810          Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
811             edit_uint64(maxbytes, ed1), mr.MediaId);
812          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
813             bsendmsg(ua, "%s", db_strerror(ua->db));
814          } else {
815             bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
816          }
817          free_pool_memory(query);
818          break;
819
820
821       case 6:                         /* Recycle */
822          int recycle;
823          bsendmsg(ua, _("Current recycle flag is: %s\n"),
824             mr.Recycle==1?_("yes"):_("no"));
825          if (!get_cmd(ua, _("Enter new Recycle status: "))) {
826             return 0;
827          }
828          if (strcasecmp(ua->cmd, _("yes")) == 0) {
829             recycle = 1;
830          } else if (strcasecmp(ua->cmd, _("no")) == 0) {
831             recycle = 0;
832          } else {
833             bsendmsg(ua, _("Invalid recycle status specified.\n"));
834             break;
835          }
836          query = get_pool_memory(PM_MESSAGE);
837          Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
838             recycle, mr.MediaId);
839          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
840             bsendmsg(ua, "%s", db_strerror(ua->db));
841          } else {       
842             bsendmsg(ua, _("New recycle flag is: %s\n"),
843                mr.Recycle==1?_("yes"):_("no"));
844          }
845          free_pool_memory(query);
846          break;
847
848       case 7:                         /* Slot */
849          int slot;
850          POOL_DBR pr;
851
852          memset(&pr, 0, sizeof(POOL_DBR));
853          pr.PoolId = mr.PoolId;
854          if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
855             bsendmsg(ua, "%s", db_strerror(ua->db));
856             return 0;
857          }
858          bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
859          if (!get_cmd(ua, _("Enter new Slot: "))) {
860             return 0;
861          }
862          slot = atoi(ua->cmd);
863          if (slot < 0) {
864             bsendmsg(ua, _("Invalid slot, it must be 0 or greater\n"));
865             break;
866          } else if (pr.MaxVols > 0 && slot >(int)pr.MaxVols) {
867             bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
868                pr.MaxVols);
869             break;
870          }
871          query = get_pool_memory(PM_MESSAGE);
872          Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
873             slot, mr.MediaId);
874          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
875             bsendmsg(ua, "%s", db_strerror(ua->db));
876          } else {
877             bsendmsg(ua, "New Slot is: %d\n", slot);
878          }
879          free_pool_memory(query);
880          break;
881
882       case 8:                         /* Volume Files */
883          int32_t VolFiles;
884          bsendmsg(ua, _("Warning changing Volume Files can result\n"
885                         "in loss of data on your Volume\n\n"));
886          bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
887          if (!get_cmd(ua, _("Enter new number of Files for Volume: "))) {
888             return 0;
889          }
890          VolFiles = atoi(ua->cmd);
891          if (VolFiles < 0) {
892             bsendmsg(ua, _("Invalid number, it must be 0 or greater\n"));
893             break;
894          } 
895          if (VolFiles != (int)(mr.VolFiles + 1)) {
896             bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
897             if (!get_cmd(ua, _("Continue? (yes/no): ")) || 
898                  strcasecmp(ua->cmd, "yes") != 0) {
899                break;
900             }
901          }
902          query = get_pool_memory(PM_MESSAGE);
903          Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
904             VolFiles, mr.MediaId);
905          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
906             bsendmsg(ua, "%s", db_strerror(ua->db));
907          } else {
908             bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
909          }
910          free_pool_memory(query);
911          break;
912
913       default:                        /* Done or error */
914          bsendmsg(ua, "Selection done.\n");
915          return 1;
916       }
917    }
918    return 1;
919 }
920
921 /* 
922  * Update pool record -- pull info from current POOL resource
923  */
924 static int update_pool(UAContext *ua)
925 {
926    POOL_DBR  pr;
927    int id;
928    POOL *pool;
929    POOLMEM *query;       
930    
931    
932    pool = get_pool_resource(ua);
933    if (!pool) {
934       return 0;
935    }
936
937    memset(&pr, 0, sizeof(pr));
938    strcpy(pr.Name, pool->hdr.name);
939    if (!get_pool_dbr(ua, &pr)) {
940       return 0;
941    }
942
943    set_pooldbr_from_poolres(&pr, pool, 0); /* update */
944
945    id = db_update_pool_record(ua->jcr, ua->db, &pr);
946    if (id <= 0) {
947       bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
948          id, db_strerror(ua->db));
949    }
950    query = get_pool_memory(PM_MESSAGE);
951    Mmsg(&query, list_pool, pr.PoolId);
952    db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, 0);
953    free_pool_memory(query);
954    bsendmsg(ua, _("Pool DB record updated from resource.\n"));
955    return 1;
956 }
957
958
959 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
960 {
961    BSOCK *sd;
962
963    ua->jcr->store = store;
964    /* Try connecting for up to 15 seconds */
965    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
966       store->hdr.name, store->address, store->SDport);
967    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
968       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
969       return;
970    }
971    Dmsg0(120, _("Connected to storage daemon\n"));
972    sd = ua->jcr->store_bsock;
973    bnet_fsend(sd, "setdebug=%d\n", level);
974    if (bnet_recv(sd) >= 0) {
975       bsendmsg(ua, "%s", sd->msg);
976    }
977    bnet_sig(sd, BNET_TERMINATE);
978    bnet_close(sd);
979    ua->jcr->store_bsock = NULL;
980    return;  
981 }
982    
983 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
984 {
985    BSOCK *fd;
986
987    /* Connect to File daemon */
988
989    ua->jcr->client = client;
990    /* Try to connect for 15 seconds */
991    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
992       client->hdr.name, client->address, client->FDport);
993    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
994       bsendmsg(ua, _("Failed to connect to Client.\n"));
995       return;
996    }
997    Dmsg0(120, "Connected to file daemon\n");
998    fd = ua->jcr->file_bsock;
999    bnet_fsend(fd, "setdebug=%d\n", level);
1000    if (bnet_recv(fd) >= 0) {
1001       bsendmsg(ua, "%s", fd->msg);
1002    }
1003    bnet_sig(fd, BNET_TERMINATE);
1004    bnet_close(fd);
1005    ua->jcr->file_bsock = NULL;
1006
1007    return;  
1008 }
1009
1010
1011 static void do_all_setdebug(UAContext *ua, int level)
1012 {
1013    STORE *store, **unique_store;
1014    CLIENT *client, **unique_client;
1015    int i, j, found;
1016
1017    /* Director */
1018    debug_level = level;
1019
1020    /* Count Storage items */
1021    LockRes();
1022    store = NULL;
1023    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1024       { }
1025    unique_store = (STORE **) malloc(i * sizeof(STORE));
1026    /* Find Unique Storage address/port */         
1027    store = (STORE *)GetNextRes(R_STORAGE, NULL);
1028    i = 0;
1029    unique_store[i++] = store;
1030    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1031       found = 0;
1032       for (j=0; j<i; j++) {
1033          if (strcmp(unique_store[j]->address, store->address) == 0 &&
1034              unique_store[j]->SDport == store->SDport) {
1035             found = 1;
1036             break;
1037          }
1038       }
1039       if (!found) {
1040          unique_store[i++] = store;
1041          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1042       }
1043    }
1044    UnlockRes();
1045
1046    /* Call each unique Storage daemon */
1047    for (j=0; j<i; j++) {
1048       do_storage_setdebug(ua, unique_store[j], level);
1049    }
1050    free(unique_store);
1051
1052    /* Count Client items */
1053    LockRes();
1054    client = NULL;
1055    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1056       { }
1057    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1058    /* Find Unique Client address/port */         
1059    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1060    i = 0;
1061    unique_client[i++] = client;
1062    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1063       found = 0;
1064       for (j=0; j<i; j++) {
1065          if (strcmp(unique_client[j]->address, client->address) == 0 &&
1066              unique_client[j]->FDport == client->FDport) {
1067             found = 1;
1068             break;
1069          }
1070       }
1071       if (!found) {
1072          unique_client[i++] = client;
1073          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1074       }
1075    }
1076    UnlockRes();
1077
1078    /* Call each unique File daemon */
1079    for (j=0; j<i; j++) {
1080       do_client_setdebug(ua, unique_client[j], level);
1081    }
1082    free(unique_client);
1083 }
1084
1085 /*
1086  * setdebug level=nn all
1087  */
1088 static int setdebugcmd(UAContext *ua, char *cmd)
1089 {
1090    STORE *store;
1091    CLIENT *client;
1092    int level;
1093    int i;
1094
1095    if (!open_db(ua)) {
1096       return 1;
1097    }
1098    Dmsg1(120, "setdebug:%s:\n", cmd);
1099
1100    level = -1;
1101    i = find_arg_with_value(ua, _("level"));
1102    if (i >= 0) {
1103       level = atoi(ua->argv[i]);
1104    }
1105    if (level < 0) {
1106       if (!get_cmd(ua, _("Enter new debug level: "))) {
1107          return 1;
1108       }
1109       level = atoi(ua->cmd);
1110    }
1111    if (level < 0) {
1112       bsendmsg(ua, _("level cannot be negative.\n"));
1113       return 1;
1114    }
1115
1116    /* General debug? */
1117    for (i=1; i<ua->argc; i++) {
1118       if (strcasecmp(ua->argk[i], _("all")) == 0) {
1119          do_all_setdebug(ua, level);
1120          return 1;
1121       }
1122       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1123           strcasecmp(ua->argk[i], _("director")) == 0) {
1124          debug_level = level;
1125          return 1;
1126       }
1127       if (strcasecmp(ua->argk[i], _("client")) == 0) {
1128          client = NULL;
1129          if (ua->argv[i]) {
1130             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1131             if (client) {
1132                do_client_setdebug(ua, client, level);
1133                return 1;
1134             }
1135          }
1136          client = select_client_resource(ua);   
1137          if (client) {
1138             do_client_setdebug(ua, client, level);
1139             return 1;
1140          }
1141       }
1142
1143       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1144           strcasecmp(ua->argk[i], _("storage")) == 0) {
1145          store = NULL;
1146          if (ua->argv[i]) {
1147             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1148             if (store) {
1149                do_storage_setdebug(ua, store, level);
1150                return 1;
1151             }
1152          }
1153          store = get_storage_resource(ua, 0);
1154          if (store) {
1155             do_storage_setdebug(ua, store, level);
1156             return 1;
1157          }
1158       }
1159    } 
1160    /*
1161     * We didn't find an appropriate keyword above, so
1162     * prompt the user.
1163     */
1164    start_prompt(ua, _("Available daemons are: \n"));
1165    add_prompt(ua, _("Director"));
1166    add_prompt(ua, _("Storage"));
1167    add_prompt(ua, _("Client"));
1168    add_prompt(ua, _("All"));
1169    switch(do_prompt(ua, _("Select daemon type to set debug level"), NULL, 0)) {
1170       case 0:                         /* Director */
1171          debug_level = level;
1172          break;
1173       case 1:
1174          store = get_storage_resource(ua, 0);
1175          if (store) {
1176             do_storage_setdebug(ua, store, level);
1177          }
1178          break;
1179       case 2:
1180          client = select_client_resource(ua);
1181          if (client) {
1182             do_client_setdebug(ua, client, level);
1183          }
1184          break;
1185       case 3:
1186          do_all_setdebug(ua, level);
1187          break;
1188       default:
1189          break;
1190    }
1191    return 1;
1192 }
1193
1194 /*
1195  * print time
1196  */
1197 static int timecmd(UAContext *ua, char *cmd)
1198 {
1199    char sdt[50];
1200    time_t ttime = time(NULL);
1201    struct tm tm;
1202    localtime_r(&ttime, &tm);
1203    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1204    bsendmsg(ua, "%s\n", sdt);
1205    return 1;
1206 }
1207
1208
1209
1210 /*
1211  * Delete Pool records (should purge Media with it).
1212  *
1213  *  delete pool=<pool-name>
1214  *  delete media pool=<pool-name> volume=<name>
1215  */
1216 static int deletecmd(UAContext *ua, char *cmd)
1217 {
1218    static char *keywords[] = {
1219       N_("volume"),
1220       N_("pool"),
1221       NULL};
1222
1223    if (!open_db(ua)) {
1224       return 1;
1225    }
1226
1227    bsendmsg(ua, _(
1228 "In general it is not a good idea to delete either a\n"
1229 "Pool or a Volume since they may contain data.\n\n"));
1230      
1231    switch (find_arg_keyword(ua, keywords)) {
1232       case 0:
1233          delete_volume(ua);     
1234          return 1;
1235       case 1:
1236          delete_pool(ua);
1237          return 1;
1238       default:
1239          break;
1240    }
1241    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1242       case 0:
1243          delete_volume(ua);
1244          break;
1245       case 1:
1246          delete_pool(ua);
1247          break;
1248       default:
1249          bsendmsg(ua, _("Nothing done.\n"));
1250          break;
1251    }
1252    return 1;
1253 }
1254
1255 /*
1256  * Delete media records from database -- dangerous 
1257  */
1258 static int delete_volume(UAContext *ua)
1259 {
1260    MEDIA_DBR mr;
1261
1262    if (!select_media_dbr(ua, &mr)) {
1263       return 1;
1264    }
1265    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1266       "and all Jobs saved on that volume from the Catalog\n"),
1267       mr.VolumeName);
1268
1269    if (!get_cmd(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1270       return 1;
1271    }
1272    if (strcasecmp(ua->cmd, _("yes")) == 0) {
1273       db_delete_media_record(ua->jcr, ua->db, &mr);
1274    }
1275    return 1;
1276 }
1277
1278 /*
1279  * Delete a pool record from the database -- dangerous   
1280  */
1281 static int delete_pool(UAContext *ua)
1282 {
1283    POOL_DBR  pr;
1284    
1285    memset(&pr, 0, sizeof(pr));
1286
1287    if (!get_pool_dbr(ua, &pr)) {
1288       return 1;
1289    }
1290    if (!get_cmd(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1291       return 1;
1292    }
1293    if (strcasecmp(ua->cmd, _("yes")) == 0) {
1294       db_delete_pool_record(ua->jcr, ua->db, &pr);
1295    }
1296    return 1;
1297 }
1298
1299
1300 static void do_mount_cmd(int mount, UAContext *ua, char *cmd)
1301 {
1302    STORE *store;
1303    BSOCK *sd;
1304    char dev_name[MAX_NAME_LENGTH];
1305
1306
1307    if (!open_db(ua)) {
1308       return;
1309    }
1310    Dmsg1(120, "mount: %s\n", ua->UA_sock->msg);
1311
1312    store = get_storage_resource(ua, 1);
1313    if (!store) {
1314       return;
1315    }
1316
1317    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1318       store->media_type, store->dev_name);
1319
1320    ua->jcr->store = store;
1321    if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1322       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1323       return;
1324    }
1325    sd = ua->jcr->store_bsock;
1326    strcpy(dev_name, store->dev_name);
1327    bash_spaces(dev_name);
1328    if (mount) {
1329       bnet_fsend(sd, "mount %s", dev_name);
1330    } else {
1331       bnet_fsend(sd, "unmount %s", dev_name);
1332    }
1333    while (bnet_recv(sd) >= 0) {
1334       bsendmsg(ua, "%s", sd->msg);
1335       if (strncmp(sd->msg, "3001 OK mount.", 14) == 0) {
1336           /***** ****FIXME**** fix JobStatus */
1337       }
1338    }
1339    bnet_sig(sd, BNET_TERMINATE);
1340    bnet_close(sd);
1341    ua->jcr->store_bsock = NULL;
1342 }
1343
1344 /*
1345  * mount [storage | device] <name>
1346  */
1347 static int mountcmd(UAContext *ua, char *cmd)
1348 {
1349    do_mount_cmd(1, ua, cmd);          /* mount */
1350    return 1;
1351 }
1352
1353
1354 /*
1355  * unmount [storage | device] <name>
1356  */
1357 static int unmountcmd(UAContext *ua, char *cmd)
1358 {
1359    do_mount_cmd(0, ua, cmd);          /* unmount */
1360    return 1;
1361 }
1362
1363
1364 /*
1365  * Switch databases
1366  *   use catalog=<name>
1367  */
1368 static int usecmd(UAContext *ua, char *cmd)
1369 {
1370    CAT *oldcatalog, *catalog;
1371
1372
1373    close_db(ua);                      /* close any previously open db */
1374    oldcatalog = ua->catalog;
1375
1376    if (!(catalog = get_catalog_resource(ua))) {
1377       ua->catalog = oldcatalog;
1378    } else {
1379       ua->catalog = catalog;
1380    }
1381    if (open_db(ua)) {
1382       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1383          ua->catalog->hdr.name, ua->catalog->db_name);
1384    }
1385    return 1;
1386 }
1387
1388 int quitcmd(UAContext *ua, char *cmd) 
1389 {
1390    ua->quit = TRUE;
1391    return 1;
1392 }
1393
1394 /*
1395  * Wait until no job is running 
1396  */
1397 int waitcmd(UAContext *ua, char *cmd) 
1398 {
1399    bmicrosleep(0, 200000);            /* let job actually start */
1400    for (int running=1; running; ) {
1401       running = 0;
1402       lock_jcr_chain();
1403       for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1404          if (jcr->JobId != 0) {
1405             running = 1;
1406             free_locked_jcr(jcr);
1407             break;
1408          }
1409          free_locked_jcr(jcr);
1410       }
1411       unlock_jcr_chain();
1412       if (running) {
1413          bmicrosleep(1, 0);
1414       }
1415    }
1416    return 1;
1417 }
1418
1419
1420 static int helpcmd(UAContext *ua, char *cmd)
1421 {
1422    unsigned int i;
1423
1424 /* usage(); */
1425    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1426    for (i=0; i<comsize; i++) {
1427       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1428    }
1429    bsendmsg(ua, "\n");
1430    return 1;
1431 }
1432
1433 static int versioncmd(UAContext *ua, char *cmd)
1434 {
1435    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1436    return 1;
1437 }
1438
1439
1440 /* A bit brain damaged in that if the user has not done
1441  * a "use catalog xxx" command, we simply find the first
1442  * catalog resource and open it.
1443  */
1444 int open_db(UAContext *ua)
1445 {
1446    if (ua->db) {
1447       return 1;
1448    }
1449    if (!ua->catalog) {
1450       LockRes();
1451       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1452       UnlockRes();
1453       if (!ua->catalog) {    
1454          bsendmsg(ua, _("Could not find a Catalog resource\n"));
1455          return 0;
1456       } else {
1457          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"), 
1458             ua->catalog->hdr.name, ua->catalog->db_name);
1459       }
1460    }
1461
1462    Dmsg0(150, "Open database\n");
1463    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1464                              ua->catalog->db_password, ua->catalog->db_address,
1465                              ua->catalog->db_port, ua->catalog->db_socket);
1466    if (!db_open_database(ua->jcr, ua->db)) {
1467       bsendmsg(ua, _("Could not open DB %s: ERR=%s"), 
1468          ua->catalog->db_name, db_strerror(ua->db));
1469       close_db(ua);
1470       return 0;
1471    }
1472    ua->jcr->db = ua->db;
1473    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1474    return 1;
1475 }
1476
1477 void close_db(UAContext *ua)
1478 {
1479    if (ua->db) {
1480       db_close_database(ua->jcr, ua->db);
1481    }
1482    ua->db = NULL;
1483    ua->jcr->db = NULL;
1484 }