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