]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
- Fix overriding storage specification to be done
[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-2004 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 extern jobq_t job_queue;              /* job queue */
41
42
43 extern char *list_pool;
44
45 /* Imported functions */
46 extern int status_cmd(UAContext *ua, const char *cmd);
47 extern int list_cmd(UAContext *ua, const char *cmd);
48 extern int llist_cmd(UAContext *ua, const char *cmd);
49 extern int show_cmd(UAContext *ua, const char *cmd);
50 extern int messagescmd(UAContext *ua, const char *cmd);
51 extern int autodisplay_cmd(UAContext *ua, const char *cmd);
52 extern int gui_cmd(UAContext *ua, const char *cmd);
53 extern int sqlquerycmd(UAContext *ua, const char *cmd);
54 extern int querycmd(UAContext *ua, const char *cmd);
55 extern int retentioncmd(UAContext *ua, const char *cmd);
56 extern int prunecmd(UAContext *ua, const char *cmd);
57 extern int purgecmd(UAContext *ua, const char *cmd);
58 extern int restore_cmd(UAContext *ua, const char *cmd);
59 extern int label_cmd(UAContext *ua, const char *cmd);
60 extern int relabel_cmd(UAContext *ua, const char *cmd);
61 extern int update_slots(UAContext *ua);  /* ua_label.c */
62
63 /* Forward referenced functions */
64 static int add_cmd(UAContext *ua, const char *cmd);  
65 static int create_cmd(UAContext *ua, const char *cmd); 
66 static int cancel_cmd(UAContext *ua, const char *cmd); 
67 static int setdebug_cmd(UAContext *ua, const char *cmd);
68 static int trace_cmd(UAContext *ua, const char *cmd);
69 static int var_cmd(UAContext *ua, const char *cmd);
70 static int estimate_cmd(UAContext *ua, const char *cmd);
71 static int help_cmd(UAContext *ua, const char *cmd);
72 static int delete_cmd(UAContext *ua, const char *cmd);
73 static int use_cmd(UAContext *ua, const char *cmd);
74 static int unmount_cmd(UAContext *ua, const char *cmd);
75 static int version_cmd(UAContext *ua, const char *cmd);
76 static int automount_cmd(UAContext *ua, const char *cmd);
77 static int time_cmd(UAContext *ua, const char *cmd);
78 static int reload_cmd(UAContext *ua, const char *cmd);
79 static int update_volume(UAContext *ua);
80 static int update_pool(UAContext *ua);
81 static int delete_volume(UAContext *ua);
82 static int delete_pool(UAContext *ua);
83 static void delete_job(UAContext *ua);
84 static int mount_cmd(UAContext *ua, const char *cmd);
85 static int release_cmd(UAContext *ua, const char *cmd);
86 static int update_cmd(UAContext *ua, const char *cmd);
87 static int wait_cmd(UAContext *ua, const char *cmd);
88 static int setip_cmd(UAContext *ua, const char *cmd);
89 static int python_cmd(UAContext *ua, const char *cmd);
90 static void do_job_delete(UAContext *ua, JobId_t JobId);
91 static void delete_job_id_range(UAContext *ua, char *tok);
92
93 int qhelp_cmd(UAContext *ua, const char *cmd);
94 int quit_cmd(UAContext *ua, const char *cmd);
95
96
97 struct cmdstruct { const char *key; int (*func)(UAContext *ua, const char *cmd); const char *help; }; 
98 static struct cmdstruct commands[] = {
99  { N_("add"),        add_cmd,         _("add media to a pool")},
100  { N_("autodisplay"), autodisplay_cmd, _("autodisplay [on/off] -- console messages")},
101  { N_("automount"),   automount_cmd,  _("automount [on/off] -- after label")},
102  { N_("cancel"),     cancel_cmd,    _("cancel job=nnn -- cancel a job")},
103  { N_("create"),     create_cmd,    _("create DB Pool from resource")},  
104  { N_("delete"),     delete_cmd,    _("delete [pool=<pool-name> | media volume=<volume-name>]")},    
105  { N_("estimate"),   estimate_cmd,  _("performs FileSet estimate, listing gives full listing")},
106  { N_("exit"),       quit_cmd,      _("exit = quit")},
107  { N_("gui"),        gui_cmd,       _("gui [on/off] -- non-interactive gui mode")},
108  { N_("help"),       help_cmd,      _("print this command")},
109  { N_("list"),       list_cmd,      _("list [pools | jobs | jobtotals | media <pool> | files jobid=<nn>]; from catalog")},
110  { N_("label"),      label_cmd,     _("label a tape")},
111  { N_("llist"),      llist_cmd,     _("full or long list like list command")},
112  { N_("messages"),   messagescmd,   _("messages")},
113  { N_("mount"),      mount_cmd,     _("mount <storage-name>")},
114  { N_("prune"),      prunecmd,      _("prune expired records from catalog")},
115  { N_("purge"),      purgecmd,      _("purge records from catalog")},
116  { N_("python"),     python_cmd,    _("python control commands")},
117  { N_("quit"),       quit_cmd,      _("quit")},
118  { N_("query"),      querycmd,      _("query catalog")},
119  { N_("restore"),    restore_cmd,   _("restore files")},
120  { N_("relabel"),    relabel_cmd,   _("relabel a tape")},
121  { N_("release"),    release_cmd,   _("release <storage-name>")},
122  { N_("reload"),     reload_cmd,    _("reload conf file")},
123  { N_("run"),        run_cmd,       _("run <job-name>")},
124  { N_("status"),     status_cmd,    _("status [storage | client]=<name>")},
125  { N_("setdebug"),   setdebug_cmd,  _("sets debug level")},
126  { N_("setip"),      setip_cmd,     _("sets new client address -- if authorized")},
127  { N_("show"),       show_cmd,      _("show (resource records) [jobs | pools | ... | all]")},
128  { N_("sqlquery"),   sqlquerycmd,   _("use SQL to query catalog")}, 
129  { N_("time"),       time_cmd,      _("print current time")},
130  { N_("trace"),      trace_cmd,     _("turn on/off trace to file")},
131  { N_("unmount"),    unmount_cmd,   _("unmount <storage-name>")},
132  { N_("umount"),     unmount_cmd,   _("umount <storage-name> for old-time Unix guys")},
133  { N_("update"),     update_cmd,    _("update Volume or Pool")},
134  { N_("use"),        use_cmd,       _("use catalog xxx")},
135  { N_("var"),        var_cmd,       _("does variable expansion")},
136  { N_("version"),    version_cmd,   _("print Director version")},
137  { N_("wait"),       wait_cmd,      _("wait until no jobs are running")},
138              };
139 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
140
141 /*
142  * Execute a command from the UA
143  */
144 int do_a_command(UAContext *ua, const char *cmd)
145 {
146    unsigned int i;
147    int len, stat;
148    bool found = false;
149
150    stat = 1;
151
152    Dmsg1(200, "Command: %s\n", ua->UA_sock->msg);
153    if (ua->argc == 0) {
154       return 1;
155    }
156
157    len = strlen(ua->argk[0]);
158    for (i=0; i<comsize; i++) {     /* search for command */
159       if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
160          if (!acl_access_ok(ua, Command_ACL, ua->argk[0], len)) {
161             break;
162          }
163          stat = (*commands[i].func)(ua, cmd);   /* go execute command */
164          found = true;
165          break;
166       }
167    }
168    if (!found) {
169       bnet_fsend(ua->UA_sock, _("%s: is an illegal command.\n"), ua->argk[0]);
170    }
171    return stat;
172 }
173
174 /*
175  * This is a common routine used to stuff the Pool DB record defaults
176  *   into the Media DB record just before creating a media (Volume) 
177  *   record.
178  */
179 void set_pool_dbr_defaults_in_media_dbr(MEDIA_DBR *mr, POOL_DBR *pr)
180 {
181    mr->PoolId = pr->PoolId;
182    bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));
183    mr->Recycle = pr->Recycle;
184    mr->VolRetention = pr->VolRetention;
185    mr->VolUseDuration = pr->VolUseDuration;
186    mr->MaxVolJobs = pr->MaxVolJobs;
187    mr->MaxVolFiles = pr->MaxVolFiles;
188    mr->MaxVolBytes = pr->MaxVolBytes;
189 }
190
191
192 /*
193  *  Add Volumes to an existing Pool
194  */
195 static int add_cmd(UAContext *ua, const char *cmd) 
196 {
197    POOL_DBR pr;
198    MEDIA_DBR mr;
199    int num, i, max, startnum;
200    int first_id = 0;
201    char name[MAX_NAME_LENGTH];
202    STORE *store;
203    int Slot = 0, InChanger = 0;
204
205    bsendmsg(ua, _(
206 "You probably don't want to be using this command since it\n"
207 "creates database records without labeling the Volumes.\n"
208 "You probably want to use the \"label\" command.\n\n"));
209
210    if (!open_db(ua)) {
211       return 1;
212    }
213
214    memset(&pr, 0, sizeof(pr));
215    memset(&mr, 0, sizeof(mr));
216
217    if (!get_pool_dbr(ua, &pr)) {
218       return 1;
219    }
220
221    Dmsg4(120, "id=%d Num=%d Max=%d type=%s\n", pr.PoolId, pr.NumVols,
222       pr.MaxVols, pr.PoolType);
223
224    while (pr.MaxVols > 0 && pr.NumVols >= pr.MaxVols) {
225       bsendmsg(ua, _("Pool already has maximum volumes = %d\n"), pr.MaxVols);
226       for (;;) {
227          if (!get_pint(ua, _("Enter new maximum (zero for unlimited): "))) {
228             return 1;
229          }
230          pr.MaxVols = ua->pint32_val;
231       }
232    }
233
234    /* Get media type */
235    if ((store = get_storage_resource(ua, 0)) != NULL) {
236       bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
237    } else if (!get_media_type(ua, mr.MediaType, sizeof(mr.MediaType))) {
238       return 1;
239    }
240       
241    if (pr.MaxVols == 0) {
242       max = 1000;
243    } else {
244       max = pr.MaxVols - pr.NumVols;
245    }
246    for (;;) {
247       char buf[100]; 
248       bsnprintf(buf, sizeof(buf), _("Enter number of Volumes to create. 0=>fixed name. Max=%d: "), max);
249       if (!get_pint(ua, buf)) {
250          return 1;
251       }
252       num = ua->pint32_val;
253       if (num < 0 || num > max) {
254          bsendmsg(ua, _("The number must be between 0 and %d\n"), max);
255          continue;
256       }
257       break;
258    }
259 getVolName:
260    if (num == 0) {
261       if (!get_cmd(ua, _("Enter Volume name: "))) {
262          return 1;
263       }
264    } else {
265       if (!get_cmd(ua, _("Enter base volume name: "))) {
266          return 1;
267       }
268    }
269    /* Don't allow | in Volume name because it is the volume separator character */
270    if (!is_volume_name_legal(ua, ua->cmd)) {
271       goto getVolName;
272    }
273    if (strlen(ua->cmd) >= MAX_NAME_LENGTH-10) {
274       bsendmsg(ua, _("Volume name too long.\n"));
275       goto getVolName;
276    }
277    if (strlen(ua->cmd) == 0) {
278       bsendmsg(ua, _("Volume name must be at least one character long.\n"));
279       goto getVolName;
280    }
281
282    bstrncpy(name, ua->cmd, sizeof(name));
283    if (num > 0) {
284       strcat(name, "%04d");
285
286       for (;;) {
287          if (!get_pint(ua, _("Enter the starting number: "))) {
288             return 1;
289          }
290          startnum = ua->pint32_val;
291          if (startnum < 1) {
292             bsendmsg(ua, _("Start number must be greater than zero.\n"));
293             continue;
294          }
295          break;
296       }
297    } else {
298       startnum = 1;
299       num = 1;
300    }
301
302    if (store && store->autochanger) {
303       if (!get_pint(ua, _("Enter slot (0 for none): "))) {
304          return 1;
305       }
306       Slot = ua->pint32_val;
307       if (!get_yesno(ua, _("InChanger? yes/no: "))) {
308          return 1;
309       }
310       InChanger = ua->pint32_val;
311    }
312            
313    set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
314    for (i=startnum; i < num+startnum; i++) { 
315       bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), name, i);
316       mr.Slot = Slot++;
317       mr.InChanger = InChanger;
318       Dmsg1(200, "Create Volume %s\n", mr.VolumeName);
319       if (!db_create_media_record(ua->jcr, ua->db, &mr)) {
320          bsendmsg(ua, "%s", db_strerror(ua->db));
321          return 1;
322       }
323       if (i == startnum) {
324          first_id = mr.PoolId;
325       }
326    }
327    pr.NumVols += num;
328    Dmsg0(200, "Update pool record.\n"); 
329    if (db_update_pool_record(ua->jcr, ua->db, &pr) != 1) {
330       bsendmsg(ua, "%s", db_strerror(ua->db));
331       return 1;
332    }
333    bsendmsg(ua, _("%d Volumes created in pool %s\n"), num, pr.Name);
334
335    return 1;
336 }
337
338 /*
339  * Turn auto mount on/off  
340  * 
341  *  automount on 
342  *  automount off
343  */
344 int automount_cmd(UAContext *ua, const char *cmd)
345 {
346    char *onoff;
347
348    if (ua->argc != 2) {
349       if (!get_cmd(ua, _("Turn on or off? "))) {
350             return 1;
351       }
352       onoff = ua->cmd;
353    } else {
354       onoff = ua->argk[1];
355    }
356
357    ua->automount = (strcasecmp(onoff, _("off")) == 0) ? 0 : 1;
358    return 1; 
359 }
360
361
362 /*
363  * Cancel a job
364  */
365 static int cancel_cmd(UAContext *ua, const char *cmd)
366 {
367    int i, ret;
368    int njobs = 0;
369    JCR *jcr = NULL;
370    char JobName[MAX_NAME_LENGTH];
371
372    if (!open_db(ua)) {
373       return 1;
374    }
375
376    for (i=1; i<ua->argc; i++) {
377       if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
378          uint32_t JobId;
379          if (!ua->argv[i]) {
380             break;
381          }
382          JobId = str_to_int64(ua->argv[i]);
383          if (!(jcr=get_jcr_by_id(JobId))) {
384             bsendmsg(ua, _("JobId %d is not running.\n"),  JobId);
385             return 1;
386          }
387          break;
388       } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
389          if (!ua->argv[i]) {
390             break;
391          }
392          if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
393             bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
394             return 1;
395          }
396          break;
397       }
398    }
399    /* If we still do not have a jcr,
400     *   throw up a list and ask the user to select one.
401     */
402    if (!jcr) {
403       /* Count Jobs running */
404       lock_jcr_chain();
405       foreach_jcr(jcr) {
406          if (jcr->JobId == 0) {      /* this is us */
407             free_locked_jcr(jcr);
408             continue;
409          }
410          free_locked_jcr(jcr);
411          njobs++;
412       }
413       unlock_jcr_chain();
414
415       if (njobs == 0) {
416          bsendmsg(ua, _("No Jobs running.\n"));
417          return 1;
418       }
419       start_prompt(ua, _("Select Job:\n"));
420       lock_jcr_chain();
421       foreach_jcr(jcr) {
422          if (jcr->JobId == 0) {      /* this is us */
423             free_locked_jcr(jcr);
424             continue;
425          }
426          add_prompt(ua, jcr->Job);
427          free_locked_jcr(jcr);
428       }
429       unlock_jcr_chain();
430
431       if (do_prompt(ua, _("Job"),  _("Choose Job to cancel"), JobName, sizeof(JobName)) < 0) {
432          return 1;
433       }
434       if (njobs == 1) {
435          if (!get_yesno(ua, _("Confirm cancel (yes/no): ")) || ua->pint32_val == 0) {
436             return 1;
437          }
438       }
439       /* NOTE! This increments the ref_count */
440       jcr = get_jcr_by_full_name(JobName);
441       if (!jcr) {
442          bsendmsg(ua, _("Job %s not found.\n"), JobName);
443          return 1;
444       }
445    }
446
447    ret = cancel_job(ua, jcr);
448    free_jcr(jcr);
449
450    return ret;
451 }
452
453 /*
454  * This is a common routine to create or update a
455  *   Pool DB base record from a Pool Resource. We handle
456  *   the setting of MaxVols and NumVols slightly differently
457  *   depending on if we are creating the Pool or we are
458  *   simply bringing it into agreement with the resource (updage).
459  */
460 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
461 {
462    strcpy(pr->PoolType, pool->pool_type);
463    if (op == POOL_OP_CREATE) {
464       pr->MaxVols = pool->max_volumes;
465       pr->NumVols = 0;
466    } else {          /* update pool */
467       if (pr->MaxVols != pool->max_volumes) {
468          pr->MaxVols = pool->max_volumes;
469       }
470       if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
471          pr->MaxVols = pr->NumVols;
472       }
473    }
474    pr->UseOnce = pool->use_volume_once;
475    pr->UseCatalog = pool->use_catalog;
476    pr->AcceptAnyVolume = pool->accept_any_volume;
477    pr->Recycle = pool->Recycle;
478    pr->VolRetention = pool->VolRetention;
479    pr->VolUseDuration = pool->VolUseDuration;
480    pr->MaxVolJobs = pool->MaxVolJobs;
481    pr->MaxVolFiles = pool->MaxVolFiles;
482    pr->MaxVolBytes = pool->MaxVolBytes;
483    pr->AutoPrune = pool->AutoPrune;
484    pr->Recycle = pool->Recycle;
485    if (pool->label_format) {
486       strcpy(pr->LabelFormat, pool->label_format);
487    } else {
488       strcpy(pr->LabelFormat, "*");    /* none */
489    }
490 }
491
492
493 /*
494  * Create a pool record from a given Pool resource
495  *   Also called from backup.c
496  * Returns: -1  on error
497  *           0  record already exists
498  *           1  record created
499  */
500
501 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
502 {
503    POOL_DBR  pr;
504
505    memset(&pr, 0, sizeof(POOL_DBR));
506
507    strcpy(pr.Name, pool->hdr.name);
508
509    if (db_get_pool_record(jcr, db, &pr)) {
510       /* Pool Exists */
511       if (op == POOL_OP_UPDATE) {  /* update request */
512          set_pooldbr_from_poolres(&pr, pool, op);
513          db_update_pool_record(jcr, db, &pr);
514       }
515       return 0;                       /* exists */
516    }
517
518    set_pooldbr_from_poolres(&pr, pool, op);
519
520    if (!db_create_pool_record(jcr, db, &pr)) {
521       return -1;                      /* error */
522    }
523    return 1;
524 }
525
526
527
528 /*
529  * Create a Pool Record in the database.
530  *  It is always created from the Resource record.
531  */
532 static int create_cmd(UAContext *ua, const char *cmd) 
533 {
534    POOL *pool;
535
536    if (!open_db(ua)) {
537       return 1;
538    }
539
540    pool = get_pool_resource(ua);
541    if (!pool) {
542       return 1;
543    }
544
545    switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
546    case 0:
547       bsendmsg(ua, _("Error: Pool %s already exists.\n"
548                "Use update to change it.\n"), pool->hdr.name);
549       break;
550
551    case -1:
552       bsendmsg(ua, "%s", db_strerror(ua->db));
553       break;
554
555    default:
556      break;
557    }
558    bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
559    return 1;
560 }
561
562
563 extern DIRRES *director;
564
565 /*
566  * Python control command
567  *  python restart (restarts interpreter)
568  */
569 static int python_cmd(UAContext *ua, const char *cmd)
570 {
571    if (strcasecmp(ua->argk[1], _("restart")) == 0) {
572       term_python_interpreter();
573       init_python_interpreter(director->hdr.name, director->scripts_directory ?
574          director->scripts_directory : ".");
575       bsendmsg(ua, _("Python interpreter restarted.\n"));
576    } else {
577       bsendmsg(ua, _("Nothing done.\n"));
578    }
579    return 1;
580 }
581
582
583 /*
584  * Set a new address in a Client resource. We do this only
585  *  if the Console name is the same as the Client name 
586  *  and the Console can access the client.
587  */
588 static int setip_cmd(UAContext *ua, const char *cmd) 
589 {
590    CLIENT *client;
591    char buf[1024]; 
592    if (!ua->cons || !acl_access_ok(ua, Client_ACL, ua->cons->hdr.name)) {
593       bsendmsg(ua, _("Illegal command from this console.\n"));
594       return 1;
595    }
596    LockRes();
597    client = (CLIENT *)GetResWithName(R_CLIENT, ua->cons->hdr.name);
598
599    if (!client) {
600       bsendmsg(ua, _("Client \"%s\" not found.\n"), ua->cons->hdr.name);
601       goto get_out;
602    }
603    if (client->address) {
604       free(client->address);
605    }
606    /* MA Bug 6 remove ifdef */
607    sockaddr_to_ascii(&(ua->UA_sock->client_addr), buf, sizeof(buf)); 
608    client->address = bstrdup(buf);
609    bsendmsg(ua, _("Client \"%s\" address set to %s\n"),
610             client->hdr.name, client->address);
611 get_out:
612    UnlockRes();
613    return 1;
614 }
615
616
617 /*
618  * Update a Pool Record in the database.
619  *  It is always updated from the Resource record.
620  *
621  *    update pool=<pool-name>
622  *         updates pool from Pool resource
623  *    update media pool=<pool-name> volume=<volume-name>
624  *         changes pool info for volume
625  *    update slots [scan=...]
626  *         updates autochanger slots
627  */
628 static int update_cmd(UAContext *ua, const char *cmd) 
629 {
630    static const char *kw[] = {
631       N_("media"),  /* 0 */
632       N_("volume"), /* 1 */
633       N_("pool"),   /* 2 */
634       N_("slots"),  /* 3 */
635       NULL};
636
637    if (!open_db(ua)) {
638       return 1;
639    }
640
641    switch (find_arg_keyword(ua, kw)) {
642    case 0:
643    case 1:
644       update_volume(ua);
645       return 1;
646    case 2:
647       update_pool(ua);
648       return 1;
649    case 3:
650       update_slots(ua);
651       return 1;
652    default:
653       break;
654    }
655     
656    start_prompt(ua, _("Update choice:\n"));
657    add_prompt(ua, _("Volume parameters"));
658    add_prompt(ua, _("Pool from resource"));
659    add_prompt(ua, _("Slots from autochanger"));
660    switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
661    case 0:
662       update_volume(ua);
663       break;
664    case 1:
665       update_pool(ua);
666       break;
667    case 2:
668       update_slots(ua);
669       break;
670    default:
671       break;
672    }
673    return 1;
674 }
675
676 static void update_volstatus(UAContext *ua, const char *val, MEDIA_DBR *mr)
677 {
678    POOLMEM *query = get_pool_memory(PM_MESSAGE);
679    const char *kw[] = {
680       "Append",
681       "Archive",
682       "Disabled",
683       "Full",
684       "Used", 
685       "Cleaning", 
686       "Recycle",
687       "Read-Only",
688       NULL};
689    bool found = false;
690    int i;
691
692    for (i=0; kw[i]; i++) {
693       if (strcasecmp(val, kw[i]) == 0) {
694          found = true;
695          break;
696       }
697    }
698    if (!found) {
699       bsendmsg(ua, _("Invalid VolStatus specified: %s\n"), val);
700    } else {
701       bstrncpy(mr->VolStatus, kw[i], sizeof(mr->VolStatus));
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    }
710    free_pool_memory(query);
711 }
712
713 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
714 {
715    char ed1[150];
716    POOLMEM *query;
717    if (!duration_to_utime(val, &mr->VolRetention)) {
718       bsendmsg(ua, _("Invalid retention period specified: %s\n"), val);
719       return;
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 period is: %s\n"),
728          edit_utime(mr->VolRetention, ed1, sizeof(ed1)));
729    }
730    free_pool_memory(query);
731 }
732
733 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
734 {
735    char ed1[150];
736    POOLMEM *query;
737
738    if (!duration_to_utime(val, &mr->VolUseDuration)) {
739       bsendmsg(ua, _("Invalid use duration specified: %s\n"), val);
740       return;
741    }
742    query = get_pool_memory(PM_MESSAGE);
743    Mmsg(query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
744       edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
745    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
746       bsendmsg(ua, "%s", db_strerror(ua->db));
747    } else {
748       bsendmsg(ua, _("New use duration is: %s\n"),
749          edit_utime(mr->VolUseDuration, ed1, sizeof(ed1)));
750    }
751    free_pool_memory(query);
752 }
753
754 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
755 {
756    POOLMEM *query = get_pool_memory(PM_MESSAGE);
757    Mmsg(query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
758       val, mr->MediaId);
759    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
760       bsendmsg(ua, "%s", db_strerror(ua->db));
761    } else {
762       bsendmsg(ua, _("New max jobs is: %s\n"), val);
763    }
764    free_pool_memory(query);
765 }
766
767 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
768 {
769    POOLMEM *query = get_pool_memory(PM_MESSAGE);
770    Mmsg(query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
771       val, mr->MediaId);
772    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
773       bsendmsg(ua, "%s", db_strerror(ua->db));
774    } else {
775       bsendmsg(ua, _("New max files is: %s\n"), val);
776    }
777    free_pool_memory(query);
778 }
779
780 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
781 {
782    uint64_t maxbytes;
783    char ed1[50];
784    POOLMEM *query;
785
786    if (!size_to_uint64(val, strlen(val), &maxbytes)) {
787       bsendmsg(ua, _("Invalid max. bytes specification: %s\n"), val);
788       return;
789    } 
790    query = get_pool_memory(PM_MESSAGE);
791    Mmsg(query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
792       edit_uint64(maxbytes, ed1), mr->MediaId);
793    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
794       bsendmsg(ua, "%s", db_strerror(ua->db));
795    } else {
796       bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
797    }
798    free_pool_memory(query);
799 }
800
801 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
802 {
803    int recycle;
804    POOLMEM *query;
805    if (strcasecmp(val, _("yes")) == 0) {
806       recycle = 1;
807    } else if (strcasecmp(val, _("no")) == 0) {
808       recycle = 0;
809    } else {
810       bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
811       return;
812    }
813    query = get_pool_memory(PM_MESSAGE);
814    Mmsg(query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
815       recycle, mr->MediaId);
816    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
817       bsendmsg(ua, "%s", db_strerror(ua->db));
818    } else {       
819       bsendmsg(ua, _("New Recycle flag is: %s\n"),
820          mr->Recycle==1?_("yes"):_("no"));
821    }
822    free_pool_memory(query);
823 }
824
825 /* Modify the Pool in which this Volume is located */
826 static void update_vol_pool(UAContext *ua, char *val, MEDIA_DBR *mr, POOL_DBR *opr)
827 {
828    POOL_DBR pr;
829    POOLMEM *query;
830
831    memset(&pr, 0, sizeof(pr));
832    bstrncpy(pr.Name, val, sizeof(pr.Name));
833    if (!get_pool_dbr(ua, &pr)) {
834       return;
835    }
836    mr->PoolId = pr.PoolId;            /* set new PoolId */
837    /*
838     */
839    query = get_pool_memory(PM_MESSAGE);
840    db_lock(ua->db);
841    Mmsg(query, "UPDATE Media SET PoolId=%d WHERE MediaId=%u",
842       mr->PoolId, mr->MediaId);
843    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
844       bsendmsg(ua, "%s", db_strerror(ua->db));
845    } else {       
846       bsendmsg(ua, _("New Pool is: %s\n"), pr.Name);
847       opr->NumVols--;
848       if (!db_update_pool_record(ua->jcr, ua->db, opr)) {
849          bsendmsg(ua, "%s", db_strerror(ua->db));
850       }
851       pr.NumVols++;
852       if (!db_update_pool_record(ua->jcr, ua->db, &pr)) {
853          bsendmsg(ua, "%s", db_strerror(ua->db));
854       }
855       db_make_inchanger_unique(ua->jcr, ua->db, mr);
856    }
857    db_unlock(ua->db);
858    free_pool_memory(query);
859 }
860
861 /*
862  * Refresh the Volume information from the Pool record
863  */
864 static void update_volfrompool(UAContext *ua, MEDIA_DBR *mr)
865 {
866    POOL_DBR pr;
867
868    memset(&pr, 0, sizeof(pr));
869    pr.PoolId = mr->PoolId;
870    if (!db_get_pool_record(ua->jcr, ua->db, &pr) ||
871        !acl_access_ok(ua, Pool_ACL, pr.Name)) {
872       return;
873    }
874    set_pool_dbr_defaults_in_media_dbr(mr, &pr);
875    if (!db_update_media_defaults(ua->jcr, ua->db, mr)) {
876       bsendmsg(ua, _("Error updating Volume record: ERR=%s"), db_strerror(ua->db));
877    } else {
878       bsendmsg(ua, _("Volume defaults updated from \"%s\" Pool record.\n"),
879          pr.Name);
880    }
881 }
882
883 /*
884  * Refresh the Volume information from the Pool record
885  *   for all Volumes
886  */
887 static void update_all_vols_from_pool(UAContext *ua)
888 {
889    POOL_DBR pr;
890    MEDIA_DBR mr;
891
892    memset(&pr, 0, sizeof(pr));
893    memset(&mr, 0, sizeof(mr));
894    if (!get_pool_dbr(ua, &pr)) {
895       return;
896    }
897    set_pool_dbr_defaults_in_media_dbr(&mr, &pr);
898    mr.PoolId = pr.PoolId;
899    if (!db_update_media_defaults(ua->jcr, ua->db, &mr)) {
900       bsendmsg(ua, _("Error updating Volume records: ERR=%s"), db_strerror(ua->db));
901    } else {
902       bsendmsg(ua, _("All Volume defaults updated from Pool record.\n"));
903    }
904 }
905
906
907 /*
908  * Update a media record -- allows you to change the
909  *  Volume status. E.g. if you want Bacula to stop
910  *  writing on the volume, set it to anything other
911  *  than Append.
912  */              
913 static int update_volume(UAContext *ua)
914 {
915    MEDIA_DBR mr;
916    POOL_DBR pr;
917    POOLMEM *query;
918    char ed1[130];
919    bool done = false;
920    const char *kw[] = {
921       N_("VolStatus"),                /* 0 */
922       N_("VolRetention"),             /* 1 */
923       N_("VolUse"),                   /* 2 */
924       N_("MaxVolJobs"),               /* 3 */
925       N_("MaxVolFiles"),              /* 4 */
926       N_("MaxVolBytes"),              /* 5 */
927       N_("Recycle"),                  /* 6 */
928       N_("Pool"),                     /* 7 */
929       N_("FromPool"),                 /* 8 */
930       N_("AllFromPool"),              /* 9 */
931       NULL };
932
933    for (int i=0; kw[i]; i++) {
934       int j;
935       POOL_DBR pr;
936       if ((j=find_arg_with_value(ua, kw[i])) > 0) {
937          if (i != 9 && !select_media_dbr(ua, &mr)) {
938             return 0;
939          }
940          switch (i) {
941          case 0:
942             update_volstatus(ua, ua->argv[j], &mr);
943             break;
944          case 1:
945             update_volretention(ua, ua->argv[j], &mr);
946             break;
947          case 2:
948             update_voluseduration(ua, ua->argv[j], &mr);
949             break;
950          case 3:
951             update_volmaxjobs(ua, ua->argv[j], &mr);
952             break;
953          case 4:
954             update_volmaxfiles(ua, ua->argv[j], &mr);
955             break;
956          case 5:
957             update_volmaxbytes(ua, ua->argv[j], &mr);
958             break;
959          case 6:
960             update_volrecycle(ua, ua->argv[j], &mr);
961             break;
962          case 7:
963             memset(&pr, 0, sizeof(POOL_DBR));
964             pr.PoolId = mr.PoolId;
965             if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
966                bsendmsg(ua, "%s", db_strerror(ua->db));
967                break;
968             }
969             update_vol_pool(ua, ua->argv[j], &mr, &pr);
970             break;
971          case 8:
972             update_volfrompool(ua, &mr);
973             return 1;
974          case 9:
975             update_all_vols_from_pool(ua);
976             return 1;
977          }
978          done = true;
979       }
980    }
981
982    for ( ; !done; ) {
983       if (!select_media_dbr(ua, &mr)) {
984          return 0;
985       }
986       bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
987       start_prompt(ua, _("Parameters to modify:\n"));
988       add_prompt(ua, _("Volume Status"));
989       add_prompt(ua, _("Volume Retention Period"));
990       add_prompt(ua, _("Volume Use Duration"));
991       add_prompt(ua, _("Maximum Volume Jobs"));
992       add_prompt(ua, _("Maximum Volume Files"));
993       add_prompt(ua, _("Maximum Volume Bytes"));
994       add_prompt(ua, _("Recycle Flag"));
995       add_prompt(ua, _("Slot"));
996       add_prompt(ua, _("InChanger Flag"));
997       add_prompt(ua, _("Volume Files"));
998       add_prompt(ua, _("Pool"));
999       add_prompt(ua, _("Volume from Pool"));
1000       add_prompt(ua, _("All Volumes from Pool"));
1001       add_prompt(ua, _("Done"));
1002       switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
1003       case 0:                         /* Volume Status */
1004          /* Modify Volume Status */
1005          bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
1006          start_prompt(ua, _("Possible Values are:\n"));
1007          add_prompt(ua, "Append");      /* Better not translate these as */
1008          add_prompt(ua, "Archive");     /* They are known in the database code */
1009          add_prompt(ua, "Disabled");
1010          add_prompt(ua, "Full");
1011          add_prompt(ua, "Used");
1012          add_prompt(ua, "Cleaning");
1013          if (strcmp(mr.VolStatus, "Purged") == 0) {
1014             add_prompt(ua, "Recycle");
1015          }
1016          add_prompt(ua, "Read-Only");
1017          if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
1018             return 1;
1019          }
1020          update_volstatus(ua, ua->cmd, &mr);
1021          break;
1022       case 1:                         /* Retention */
1023          bsendmsg(ua, _("Current retention period is: %s\n"),
1024             edit_utime(mr.VolRetention, ed1, sizeof(ed1)));
1025          if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
1026             return 0;
1027          }
1028          update_volretention(ua, ua->cmd, &mr);
1029          break;
1030
1031       case 2:                         /* Use Duration */
1032          bsendmsg(ua, _("Current use duration is: %s\n"),
1033             edit_utime(mr.VolUseDuration, ed1, sizeof(ed1)));
1034          if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
1035             return 0;
1036          }
1037          update_voluseduration(ua, ua->cmd, &mr);
1038          break;
1039
1040       case 3:                         /* Max Jobs */
1041          bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
1042          if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
1043             return 0;
1044          }
1045          update_volmaxjobs(ua, ua->cmd, &mr);
1046          break;
1047
1048       case 4:                         /* Max Files */
1049          bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
1050          if (!get_pint(ua, _("Enter new Maximum Files: "))) {
1051             return 0;
1052          }
1053          update_volmaxfiles(ua, ua->cmd, &mr);
1054          break;
1055
1056       case 5:                         /* Max Bytes */
1057          bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
1058          if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
1059             return 0;
1060          }
1061          update_volmaxbytes(ua, ua->cmd, &mr);
1062          break;
1063
1064
1065       case 6:                         /* Recycle */
1066          bsendmsg(ua, _("Current recycle flag is: %s\n"),
1067             mr.Recycle==1?_("yes"):_("no"));
1068          if (!get_yesno(ua, _("Enter new Recycle status: "))) {
1069             return 0;
1070          }
1071          update_volrecycle(ua, ua->cmd, &mr);
1072          break;
1073
1074       case 7:                         /* Slot */
1075          int Slot;
1076
1077          memset(&pr, 0, sizeof(POOL_DBR));
1078          pr.PoolId = mr.PoolId;
1079          if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1080             bsendmsg(ua, "%s", db_strerror(ua->db));
1081             return 0;
1082          }
1083          bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
1084          if (!get_pint(ua, _("Enter new Slot: "))) {
1085             return 0;
1086          }
1087          Slot = ua->pint32_val;
1088          if (pr.MaxVols > 0 && Slot > (int)pr.MaxVols) {
1089             bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
1090                pr.MaxVols);
1091             break;
1092          }
1093          mr.Slot = Slot;
1094          /*
1095           * Make sure to use db_update... rather than doing this directly,
1096           *   so that any Slot is handled correctly. 
1097           */
1098          if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1099             bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1100          } else {
1101             bsendmsg(ua, _("New Slot is: %d\n"), mr.Slot);
1102          }
1103          break;
1104
1105       case 8:                         /* InChanger */
1106          bsendmsg(ua, _("Current InChanger flag is: %d\n"), mr.InChanger);
1107          if (!get_yesno(ua, _("Set InChanger flag? yes/no: "))) {
1108             return 0;
1109          }
1110          mr.InChanger = ua->pint32_val;
1111          /*
1112           * Make sure to use db_update... rather than doing this directly,
1113           *   so that any Slot is handled correctly. 
1114           */
1115          if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
1116             bsendmsg(ua, _("Error updating media record Slot: ERR=%s"), db_strerror(ua->db));
1117          } else {
1118             bsendmsg(ua, _("New InChanger flag is: %d\n"), mr.InChanger);
1119          }
1120          break;
1121
1122
1123       case 9:                         /* Volume Files */
1124          int32_t VolFiles;
1125          bsendmsg(ua, _("Warning changing Volume Files can result\n"
1126                         "in loss of data on your Volume\n\n"));
1127          bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
1128          if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
1129             return 0;
1130          }
1131          VolFiles = ua->pint32_val;
1132          if (VolFiles != (int)(mr.VolFiles + 1)) {
1133             bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
1134             if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
1135                break;
1136             }
1137          }
1138          query = get_pool_memory(PM_MESSAGE);
1139          Mmsg(query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
1140             VolFiles, mr.MediaId);
1141          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
1142             bsendmsg(ua, "%s", db_strerror(ua->db));
1143          } else {
1144             bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
1145          }
1146          free_pool_memory(query);
1147          break;
1148
1149       case 10:                        /* Volume's Pool */
1150          memset(&pr, 0, sizeof(POOL_DBR));
1151          pr.PoolId = mr.PoolId;
1152          if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
1153             bsendmsg(ua, "%s", db_strerror(ua->db));
1154             return 0;
1155          }
1156          bsendmsg(ua, _("Current Pool is: %s\n"), pr.Name);
1157          if (!get_cmd(ua, _("Enter new Pool name: "))) {
1158             return 0;
1159          }
1160          update_vol_pool(ua, ua->cmd, &mr, &pr);
1161          return 1;
1162
1163       case 11:
1164          update_volfrompool(ua, &mr);
1165          return 1;
1166       case 12:
1167          update_all_vols_from_pool(ua);
1168          return 1;
1169       default:                        /* Done or error */
1170          bsendmsg(ua, "Selection done.\n");
1171          return 1;
1172       }
1173    }
1174    return 1;
1175 }
1176
1177 /* 
1178  * Update pool record -- pull info from current POOL resource
1179  */
1180 static int update_pool(UAContext *ua)
1181 {
1182    POOL_DBR  pr;
1183    int id;
1184    POOL *pool;
1185    POOLMEM *query;       
1186    
1187    pool = get_pool_resource(ua);
1188    if (!pool) {
1189       return 0;
1190    }
1191
1192    memset(&pr, 0, sizeof(pr));
1193    strcpy(pr.Name, pool->hdr.name);
1194    if (!get_pool_dbr(ua, &pr)) {
1195       return 0;
1196    }
1197
1198    set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
1199
1200    id = db_update_pool_record(ua->jcr, ua->db, &pr);
1201    if (id <= 0) {
1202       bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
1203          id, db_strerror(ua->db));
1204    }
1205    query = get_pool_memory(PM_MESSAGE);
1206    Mmsg(query, list_pool, pr.PoolId);
1207    db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
1208    free_pool_memory(query);
1209    bsendmsg(ua, _("Pool DB record updated from resource.\n"));
1210    return 1;
1211 }
1212
1213
1214 static void do_storage_setdebug(UAContext *ua, STORE *store, int level, int trace_flag)
1215 {
1216    BSOCK *sd;
1217    JCR *jcr = ua->jcr;
1218
1219    set_storage(jcr, store);
1220    /* Try connecting for up to 15 seconds */
1221    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
1222       store->hdr.name, store->address, store->SDport);
1223    if (!connect_to_storage_daemon(jcr, 1, 15, 0)) {
1224       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1225       return;
1226    }
1227    Dmsg0(120, _("Connected to storage daemon\n"));
1228    sd = jcr->store_bsock;
1229    bnet_fsend(sd, "setdebug=%d trace=%d\n", level, trace_flag);
1230    if (bnet_recv(sd) >= 0) {
1231       bsendmsg(ua, "%s", sd->msg);
1232    }
1233    bnet_sig(sd, BNET_TERMINATE);
1234    bnet_close(sd);
1235    jcr->store_bsock = NULL;
1236    return;  
1237 }
1238    
1239 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level, int trace_flag)
1240 {
1241    BSOCK *fd;
1242
1243    /* Connect to File daemon */
1244
1245    ua->jcr->client = client;
1246    /* Try to connect for 15 seconds */
1247    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
1248       client->hdr.name, client->address, client->FDport);
1249    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1250       bsendmsg(ua, _("Failed to connect to Client.\n"));
1251       return;
1252    }
1253    Dmsg0(120, "Connected to file daemon\n");
1254    fd = ua->jcr->file_bsock;
1255    bnet_fsend(fd, "setdebug=%d trace=%d\n", level, trace_flag);
1256    if (bnet_recv(fd) >= 0) {
1257       bsendmsg(ua, "%s", fd->msg);
1258    }
1259    bnet_sig(fd, BNET_TERMINATE);
1260    bnet_close(fd);
1261    ua->jcr->file_bsock = NULL;
1262    return;  
1263 }
1264
1265
1266 static void do_all_setdebug(UAContext *ua, int level, int trace_flag)
1267 {
1268    STORE *store, **unique_store;
1269    CLIENT *client, **unique_client;
1270    int i, j, found;
1271
1272    /* Director */
1273    debug_level = level;
1274
1275    /* Count Storage items */
1276    LockRes();
1277    store = NULL;
1278    i = 0;
1279    foreach_res(store, R_STORAGE) {
1280       i++;
1281    }
1282    unique_store = (STORE **) malloc(i * sizeof(STORE));
1283    /* Find Unique Storage address/port */         
1284    store = (STORE *)GetNextRes(R_STORAGE, NULL);
1285    i = 0;
1286    unique_store[i++] = store;
1287    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1288       found = 0;
1289       for (j=0; j<i; j++) {
1290          if (strcmp(unique_store[j]->address, store->address) == 0 &&
1291              unique_store[j]->SDport == store->SDport) {
1292             found = 1;
1293             break;
1294          }
1295       }
1296       if (!found) {
1297          unique_store[i++] = store;
1298          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1299       }
1300    }
1301    UnlockRes();
1302
1303    /* Call each unique Storage daemon */
1304    for (j=0; j<i; j++) {
1305       do_storage_setdebug(ua, unique_store[j], level, trace_flag);
1306    }
1307    free(unique_store);
1308
1309    /* Count Client items */
1310    LockRes();
1311    client = NULL;
1312    i = 0;
1313    foreach_res(client, R_CLIENT) {
1314       i++;
1315    }
1316    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1317    /* Find Unique Client address/port */         
1318    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1319    i = 0;
1320    unique_client[i++] = client;
1321    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1322       found = 0;
1323       for (j=0; j<i; j++) {
1324          if (strcmp(unique_client[j]->address, client->address) == 0 &&
1325              unique_client[j]->FDport == client->FDport) {
1326             found = 1;
1327             break;
1328          }
1329       }
1330       if (!found) {
1331          unique_client[i++] = client;
1332          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1333       }
1334    }
1335    UnlockRes();
1336
1337    /* Call each unique File daemon */
1338    for (j=0; j<i; j++) {
1339       do_client_setdebug(ua, unique_client[j], level, trace_flag);
1340    }
1341    free(unique_client);
1342 }
1343
1344 /*
1345  * setdebug level=nn all trace=1/0
1346  */
1347 static int setdebug_cmd(UAContext *ua, const char *cmd)
1348 {
1349    STORE *store;
1350    CLIENT *client;
1351    int level;
1352    int trace_flag = -1;
1353    int i;
1354
1355    if (!open_db(ua)) {
1356       return 1;
1357    }
1358    Dmsg1(120, "setdebug:%s:\n", cmd);
1359
1360    level = -1;
1361    i = find_arg_with_value(ua, _("level"));
1362    if (i >= 0) {
1363       level = atoi(ua->argv[i]);
1364    }
1365    if (level < 0) {
1366       if (!get_pint(ua, _("Enter new debug level: "))) {
1367          return 1;
1368       }
1369       level = ua->pint32_val;
1370    }
1371
1372    /* Look for trace flag. -1 => not change */
1373    i = find_arg_with_value(ua, _("trace"));
1374    if (i >= 0) {
1375       trace_flag = atoi(ua->argv[i]);
1376       if (trace_flag > 0) {
1377          trace_flag = 1;
1378       }
1379    }
1380
1381    /* General debug? */
1382    for (i=1; i<ua->argc; i++) {
1383       if (strcasecmp(ua->argk[i], _("all")) == 0) {
1384          do_all_setdebug(ua, level, trace_flag);
1385          return 1;
1386       }
1387       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1388           strcasecmp(ua->argk[i], _("director")) == 0) {
1389          debug_level = level;
1390          set_trace(trace_flag);
1391          return 1;
1392       }
1393       if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1394           strcasecmp(ua->argk[i], _("fd")) == 0) {
1395          client = NULL;
1396          if (ua->argv[i]) {
1397             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1398             if (client) {
1399                do_client_setdebug(ua, client, level, trace_flag);
1400                return 1;
1401             }
1402          }
1403          client = select_client_resource(ua);   
1404          if (client) {
1405             do_client_setdebug(ua, client, level, trace_flag);
1406             return 1;
1407          }
1408       }
1409
1410       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1411           strcasecmp(ua->argk[i], _("storage")) == 0 ||
1412           strcasecmp(ua->argk[i], _("sd")) == 0) {
1413          store = NULL;
1414          if (ua->argv[i]) {
1415             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1416             if (store) {
1417                do_storage_setdebug(ua, store, level, trace_flag);
1418                return 1;
1419             }
1420          }
1421          store = get_storage_resource(ua, 0);
1422          if (store) {
1423             do_storage_setdebug(ua, store, level, trace_flag);
1424             return 1;
1425          }
1426       }
1427    } 
1428    /*
1429     * We didn't find an appropriate keyword above, so
1430     * prompt the user.
1431     */
1432    start_prompt(ua, _("Available daemons are: \n"));
1433    add_prompt(ua, _("Director"));
1434    add_prompt(ua, _("Storage"));
1435    add_prompt(ua, _("Client"));
1436    add_prompt(ua, _("All"));
1437    switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1438    case 0:                         /* Director */
1439       debug_level = level;
1440       set_trace(trace_flag);
1441       break;
1442    case 1:
1443       store = get_storage_resource(ua, 0);
1444       if (store) {
1445          do_storage_setdebug(ua, store, level, trace_flag);
1446       }
1447       break;
1448    case 2:
1449       client = select_client_resource(ua);
1450       if (client) {
1451          do_client_setdebug(ua, client, level, trace_flag);
1452       }
1453       break;
1454    case 3:
1455       do_all_setdebug(ua, level, trace_flag);
1456       break;
1457    default:
1458       break;
1459    }
1460    return 1;
1461 }
1462
1463 /*
1464  * Turn debug tracing to file on/off
1465  */
1466 static int trace_cmd(UAContext *ua, const char *cmd)
1467 {
1468    char *onoff;
1469
1470    if (ua->argc != 2) {
1471       if (!get_cmd(ua, _("Turn on or off? "))) {
1472             return 1;
1473       }
1474       onoff = ua->cmd;
1475    } else {
1476       onoff = ua->argk[1];
1477    }
1478
1479    set_trace((strcasecmp(onoff, _("off")) == 0) ? false : true);
1480    return 1; 
1481
1482 }
1483
1484 static int var_cmd(UAContext *ua, const char *cmd)
1485 {
1486    POOLMEM *val = get_pool_memory(PM_FNAME);
1487    char *var;
1488
1489    if (!open_db(ua)) {
1490       return 1;
1491    }
1492    for (var=ua->cmd; *var != ' '; ) {    /* skip command */
1493       var++;
1494    }
1495    while (*var == ' ') {                 /* skip spaces */
1496       var++;
1497    }
1498    Dmsg1(100, "Var=%s:\n", var);
1499    variable_expansion(ua->jcr, var, &val);  
1500    bsendmsg(ua, "%s\n", val);
1501    free_pool_memory(val);
1502    return 1;
1503 }
1504
1505 static int estimate_cmd(UAContext *ua, const char *cmd)
1506 {
1507    JOB *job = NULL;
1508    CLIENT *client = NULL;
1509    FILESET *fileset = NULL;
1510    FILESET_DBR fsr;
1511    int listing = 0;
1512    char since[MAXSTRING];
1513    JCR *jcr = ua->jcr;
1514
1515    jcr->JobLevel = L_FULL;
1516    for (int i=1; i<ua->argc; i++) {
1517       if (strcasecmp(ua->argk[i], _("client")) == 0 ||
1518           strcasecmp(ua->argk[i], _("fd")) == 0) {
1519          if (ua->argv[i]) {
1520             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1521             continue;
1522          }
1523       }
1524       if (strcasecmp(ua->argk[i], _("job")) == 0) {
1525          if (ua->argv[i]) {
1526             job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1527             continue;
1528          }
1529       }
1530       if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1531          if (ua->argv[i]) {
1532             fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1533             continue;
1534          }
1535       }
1536       if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1537          listing = 1;
1538          continue;
1539       }
1540       if (strcasecmp(ua->argk[i], _("level")) == 0) {
1541          if (!get_level_from_name(ua->jcr, ua->argv[i])) {
1542             bsendmsg(ua, _("Level %s not valid.\n"), ua->argv[i]);
1543          }
1544          continue;
1545       }
1546    } 
1547    if (!job && !(client && fileset)) {
1548       if (!(job = select_job_resource(ua))) {
1549          return 1;
1550       }
1551    }
1552    if (!job) {
1553       job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1554       if (!job) {
1555          bsendmsg(ua, _("No job specified.\n"));
1556          return 1;
1557       }
1558    }
1559    if (!client) {
1560       client = job->client;
1561    }
1562    if (!fileset) {
1563       fileset = job->fileset;
1564    }
1565    jcr->client = client;
1566    jcr->fileset = fileset;
1567    close_db(ua);
1568    ua->catalog = client->catalog;
1569
1570    if (!open_db(ua)) {
1571       return 1;
1572    }
1573
1574    jcr->job = job;
1575    jcr->JobType = JT_BACKUP;
1576    init_jcr_job_record(jcr);
1577
1578    if (!get_or_create_client_record(jcr)) {
1579       return 1;
1580    }
1581    if (!get_or_create_fileset_record(jcr, &fsr)) {
1582       return 1;
1583    }
1584    
1585    get_level_since_time(ua->jcr, since, sizeof(since));
1586
1587    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1588       job->client->hdr.name, job->client->address, job->client->FDport);
1589    if (!connect_to_file_daemon(jcr, 1, 15, 0)) {
1590       bsendmsg(ua, _("Failed to connect to Client.\n"));
1591       return 1;
1592    }
1593
1594    if (!send_include_list(jcr)) {
1595       bsendmsg(ua, _("Error sending include list.\n"));
1596       goto bail_out;
1597    }
1598
1599    if (!send_exclude_list(jcr)) {
1600       bsendmsg(ua, _("Error sending exclude list.\n"));
1601       goto bail_out;
1602    }
1603
1604    if (!send_level_command(jcr)) {
1605       goto bail_out;
1606    }
1607
1608    bnet_fsend(jcr->file_bsock, "estimate listing=%d\n", listing);
1609    while (bnet_recv(jcr->file_bsock) >= 0) {
1610       bsendmsg(ua, "%s", jcr->file_bsock->msg);
1611    }
1612
1613 bail_out:
1614    if (jcr->file_bsock) {
1615       bnet_sig(jcr->file_bsock, BNET_TERMINATE);
1616       bnet_close(jcr->file_bsock);
1617       jcr->file_bsock = NULL;
1618    }
1619    return 1;
1620 }
1621
1622
1623 /*
1624  * print time
1625  */
1626 static int time_cmd(UAContext *ua, const char *cmd)
1627 {
1628    char sdt[50];
1629    time_t ttime = time(NULL);
1630    struct tm tm;
1631    localtime_r(&ttime, &tm);
1632    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1633    bsendmsg(ua, "%s\n", sdt);
1634    return 1;
1635 }
1636
1637 /*
1638  * reload the conf file
1639  */
1640 extern "C" void reload_config(int sig);
1641
1642 static int reload_cmd(UAContext *ua, const char *cmd)
1643 {
1644    reload_config(1);   
1645    return 1;
1646 }
1647
1648 /*
1649  * Delete Pool records (should purge Media with it).
1650  *
1651  *  delete pool=<pool-name>
1652  *  delete volume pool=<pool-name> volume=<name>
1653  *  delete job jobid=xxx
1654  */
1655 static int delete_cmd(UAContext *ua, const char *cmd)
1656 {
1657    static const char *keywords[] = {
1658       N_("volume"),
1659       N_("pool"),
1660       N_("job"),
1661       N_("jobid"),
1662       NULL};
1663
1664    if (!open_db(ua)) {
1665       return 1;
1666    }
1667
1668    switch (find_arg_keyword(ua, keywords)) {      
1669    case 0:
1670       delete_volume(ua);     
1671       return 1;
1672    case 1:
1673       delete_pool(ua);
1674       return 1;
1675    case 2:
1676    case 3:
1677       int i;
1678       while ((i=find_arg(ua, _("jobid"))) > 0) {
1679          delete_job(ua);
1680          *ua->argk[i] = 0;         /* zap keyword already visited */
1681       }
1682       return 1;
1683    default:
1684       break;
1685    }
1686
1687    bsendmsg(ua, _(
1688 "In general it is not a good idea to delete either a\n"
1689 "Pool or a Volume since they may contain data.\n\n"));
1690
1691    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1692    case 0:
1693       delete_volume(ua);
1694       break;
1695    case 1:
1696       delete_pool(ua);
1697       break;
1698    case 2:
1699       delete_job(ua);
1700       return 1;
1701    default:
1702       bsendmsg(ua, _("Nothing done.\n"));
1703       break;
1704    }
1705    return 1;
1706 }
1707
1708
1709 /*
1710  * delete_job has been modified to parse JobID lists like the
1711  * following:
1712  * delete job JobID=3,4,6,7-11,14
1713  * 
1714  * Thanks to Phil Stracchino for the above addition.
1715  */
1716
1717 static void delete_job(UAContext *ua)
1718 {
1719    JobId_t JobId;
1720    char *s,*sep,*tok;
1721
1722    int i = find_arg_with_value(ua, _("jobid"));
1723    if (i >= 0) {
1724       if (strchr(ua->argv[i], ',') != NULL || strchr(ua->argv[i], '-') != NULL) {
1725         s = bstrdup(ua->argv[i]);
1726         tok = s;
1727         /*
1728          * We could use strtok() here.  But we're not going to, because:
1729          * (a) strtok() is deprecated, having been replaced by strsep();
1730          * (b) strtok() is broken in significant ways.
1731          * we could use strsep() instead, but it's not universally available.
1732          * so we grow our own using strchr().
1733          */
1734         sep = strchr(tok, ',');
1735         while (sep != NULL) {
1736            *sep = '\0';
1737            if (strchr(tok, '-')) {
1738                delete_job_id_range(ua, tok);
1739            } else {
1740               JobId = str_to_int64(tok);
1741               do_job_delete(ua, JobId);
1742            }
1743            tok = ++sep;
1744            sep = strchr(tok, ',');
1745         }
1746         /* pick up the last token */
1747         if (strchr(tok, '-')) {
1748             delete_job_id_range(ua, tok);
1749         } else {
1750             JobId = str_to_int64(tok);
1751             do_job_delete(ua, JobId);
1752         }
1753         
1754          free(s);
1755       } else {
1756          JobId = str_to_int64(ua->argv[i]);
1757         do_job_delete(ua, JobId);
1758       }
1759    } else if (!get_pint(ua, _("Enter JobId to delete: "))) {
1760       return;
1761    } else {
1762       JobId = ua->pint32_val;
1763       do_job_delete(ua, JobId);
1764    }
1765 }
1766
1767 /*
1768  * we call delete_job_id_range to parse range tokens and iterate over ranges
1769  */
1770 static void delete_job_id_range(UAContext *ua, char *tok)
1771 {
1772    char *tok2;
1773    JobId_t j,j1,j2;
1774
1775    tok2 = strchr(tok, '-');
1776    *tok2 = '\0';
1777    tok2++;
1778    j1 = str_to_int64(tok);
1779    j2 = str_to_int64(tok2);
1780    for (j=j1; j<=j2; j++) {
1781       do_job_delete(ua, j);
1782    }
1783 }
1784
1785 /*
1786  * do_job_delete now performs the actual delete operation atomically
1787  * we always return 1 because C++ is pissy about void functions
1788  */
1789
1790 static void do_job_delete(UAContext *ua, JobId_t JobId)
1791 {
1792    POOLMEM *query = get_pool_memory(PM_MESSAGE);
1793
1794    Mmsg(query, "DELETE FROM Job WHERE JobId=%u", JobId);
1795    db_sql_query(ua->db, query, NULL, (void *)NULL);
1796    Mmsg(query, "DELETE FROM File WHERE JobId=%u", JobId);
1797    db_sql_query(ua->db, query, NULL, (void *)NULL);
1798    Mmsg(query, "DELETE FROM JobMedia WHERE JobId=%u", JobId);
1799    db_sql_query(ua->db, query, NULL, (void *)NULL);
1800    free_pool_memory(query);
1801    bsendmsg(ua, _("Job %u and associated records deleted from the catalog.\n"), JobId);
1802 }
1803
1804 /*
1805  * Delete media records from database -- dangerous 
1806  */
1807 static int delete_volume(UAContext *ua)
1808 {
1809    MEDIA_DBR mr;
1810
1811    if (!select_media_dbr(ua, &mr)) {
1812       return 1;
1813    }
1814    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1815       "and all Jobs saved on that volume from the Catalog\n"),
1816       mr.VolumeName);
1817
1818    if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1819       return 1;
1820    }
1821    if (ua->pint32_val) {
1822       db_delete_media_record(ua->jcr, ua->db, &mr);
1823    }
1824    return 1;
1825 }
1826
1827 /*
1828  * Delete a pool record from the database -- dangerous   
1829  */
1830 static int delete_pool(UAContext *ua)
1831 {
1832    POOL_DBR  pr;
1833    
1834    memset(&pr, 0, sizeof(pr));
1835
1836    if (!get_pool_dbr(ua, &pr)) {
1837       return 1;
1838    }
1839    if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1840       return 1;
1841    }
1842    if (ua->pint32_val) {
1843       db_delete_pool_record(ua->jcr, ua->db, &pr);
1844    }
1845    return 1;
1846 }
1847
1848
1849 static void do_mount_cmd(UAContext *ua, const char *command)
1850 {
1851    STORE *store;
1852    BSOCK *sd;
1853    JCR *jcr = ua->jcr;
1854    char dev_name[MAX_NAME_LENGTH];
1855
1856    if (!open_db(ua)) {
1857       return;
1858    }
1859    Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1860
1861    store = get_storage_resource(ua, 1);
1862    if (!store) {
1863       return;
1864    }
1865
1866    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1867       store->media_type, store->dev_name);
1868
1869    set_storage(jcr, store);
1870    if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
1871       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1872       return;
1873    }
1874    sd = jcr->store_bsock;
1875    bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
1876    bash_spaces(dev_name);
1877    bnet_fsend(sd, "%s %s", command, dev_name);
1878    while (bnet_recv(sd) >= 0) {
1879       bsendmsg(ua, "%s", sd->msg);
1880    }
1881    bnet_sig(sd, BNET_TERMINATE);
1882    bnet_close(sd);
1883    jcr->store_bsock = NULL;
1884 }
1885
1886 /*
1887  * mount [storage | device] <name>
1888  */
1889 static int mount_cmd(UAContext *ua, const char *cmd)
1890 {
1891    do_mount_cmd(ua, "mount");          /* mount */
1892    return 1;
1893 }
1894
1895
1896 /*
1897  * unmount [storage | device] <name>
1898  */
1899 static int unmount_cmd(UAContext *ua, const char *cmd)
1900 {
1901    do_mount_cmd(ua, "unmount");          /* unmount */
1902    return 1;
1903 }
1904
1905
1906 /*
1907  * release [storage | device] <name>
1908  */
1909 static int release_cmd(UAContext *ua, const char *cmd)
1910 {
1911    do_mount_cmd(ua, "release");          /* release */
1912    return 1;
1913 }
1914
1915
1916 /*
1917  * Switch databases
1918  *   use catalog=<name>
1919  */
1920 static int use_cmd(UAContext *ua, const char *cmd)
1921 {
1922    CAT *oldcatalog, *catalog;
1923
1924
1925    close_db(ua);                      /* close any previously open db */
1926    oldcatalog = ua->catalog;
1927
1928    if (!(catalog = get_catalog_resource(ua))) {
1929       ua->catalog = oldcatalog;
1930    } else {
1931       ua->catalog = catalog;
1932    }
1933    if (open_db(ua)) {
1934       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1935          ua->catalog->hdr.name, ua->catalog->db_name);
1936    }
1937    return 1;
1938 }
1939
1940 int quit_cmd(UAContext *ua, const char *cmd) 
1941 {
1942    ua->quit = TRUE;
1943    return 1;
1944 }
1945
1946 /*
1947  * Wait until no job is running 
1948  */
1949 int wait_cmd(UAContext *ua, const char *cmd) 
1950 {
1951    JCR *jcr;
1952    bmicrosleep(0, 200000);            /* let job actually start */
1953    for (bool running=true; running; ) {
1954       running = false;
1955       lock_jcr_chain();
1956       foreach_jcr(jcr) {
1957          if (jcr->JobId != 0) {
1958             running = true;
1959             free_locked_jcr(jcr);
1960             break;
1961          }
1962          free_locked_jcr(jcr);
1963       }
1964       unlock_jcr_chain();
1965       if (running) {
1966          bmicrosleep(1, 0);
1967       }
1968    }
1969    return 1;
1970 }
1971
1972
1973 static int help_cmd(UAContext *ua, const char *cmd)
1974 {
1975    unsigned int i;
1976
1977    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1978    for (i=0; i<comsize; i++) {
1979       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1980    }
1981    bsendmsg(ua, _("\nWhen at a prompt, entering a period cancels the command.\n\n"));
1982    return 1;
1983 }
1984
1985 int qhelp_cmd(UAContext *ua, const char *cmd)
1986 {
1987    unsigned int i;
1988
1989    for (i=0; i<comsize; i++) {
1990       bsendmsg(ua, _("%s %s\n"), _(commands[i].key), _(commands[i].help));
1991    }
1992    return 1;
1993 }
1994
1995 static int version_cmd(UAContext *ua, const char *cmd)
1996 {
1997    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1998    return 1;
1999 }
2000
2001
2002 /* A bit brain damaged in that if the user has not done
2003  * a "use catalog xxx" command, we simply find the first
2004  * catalog resource and open it.
2005  */
2006 int open_db(UAContext *ua)
2007 {
2008    if (ua->db) {
2009       return 1;
2010    }
2011    if (!ua->catalog) {
2012       LockRes();
2013       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
2014       UnlockRes();
2015       if (!ua->catalog) {    
2016          bsendmsg(ua, _("Could not find a Catalog resource\n"));
2017          return 0;
2018       } else {
2019          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"), 
2020             ua->catalog->hdr.name, ua->catalog->db_name);
2021       }
2022    }
2023
2024    ua->jcr->catalog = ua->catalog;
2025
2026    Dmsg0(150, "Open database\n");
2027    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
2028                              ua->catalog->db_password, ua->catalog->db_address,
2029                              ua->catalog->db_port, ua->catalog->db_socket,
2030                              ua->catalog->mult_db_connections);
2031    if (!ua->db || !db_open_database(ua->jcr, ua->db)) {
2032       bsendmsg(ua, _("Could not open database \"%s\".\n"),
2033                  ua->catalog->db_name);
2034       if (ua->db) {
2035          bsendmsg(ua, "%s", db_strerror(ua->db));
2036       }
2037       close_db(ua);
2038       return 0;
2039    }
2040    ua->jcr->db = ua->db;
2041    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
2042    return 1;
2043 }
2044
2045 void close_db(UAContext *ua)
2046 {
2047    if (ua->db) {
2048       db_close_database(ua->jcr, ua->db);
2049       ua->db = NULL;
2050       if (ua->jcr) {
2051          ua->jcr->db = NULL;
2052       }
2053    }
2054 }