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