]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_cmds.c
Fix tree code from bashing restore args + pass prefix links to FD + sort Console...
[bacula/bacula] / bacula / src / dird / ua_cmds.c
1 /*
2  *
3  *   Bacula Director -- User Agent Commands
4  *
5  *     Kern Sibbald, September MM
6  *
7  *   Version $Id$
8  */
9
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 /* Imported subroutines */
34
35 /* Imported variables */
36 extern int r_first;
37 extern int r_last;
38 extern struct s_res resources[];
39 extern char my_name[];
40 #ifndef USE_SEMAPHORE 
41 extern workq_t job_wq;                /* work queue */
42 #endif
43
44 extern char *list_pool;
45
46 /* Imported functions */
47 extern int statuscmd(UAContext *ua, char *cmd);
48 extern int listcmd(UAContext *ua, char *cmd);
49 extern int llistcmd(UAContext *ua, char *cmd);
50 extern int show_cmd(UAContext *ua, char *cmd);
51 extern int messagescmd(UAContext *ua, char *cmd);
52 extern int autodisplaycmd(UAContext *ua, char *cmd);
53 extern int sqlquerycmd(UAContext *ua, char *cmd);
54 extern int querycmd(UAContext *ua, char *cmd);
55 extern int runcmd(UAContext *ua, char *cmd);
56 extern int retentioncmd(UAContext *ua, char *cmd);
57 extern int prunecmd(UAContext *ua, char *cmd);
58 extern int purgecmd(UAContext *ua, char *cmd);
59 extern int restorecmd(UAContext *ua, char *cmd);
60 extern int labelcmd(UAContext *ua, char *cmd);
61 extern int relabelcmd(UAContext *ua, char *cmd);
62 extern int update_slots(UAContext *ua);  /* ua_label.c */
63
64 /* Forward referenced functions */
65 static int add_cmd(UAContext *ua, char *cmd),  createcmd(UAContext *ua, char *cmd), cancelcmd(UAContext *ua, char *cmd);
66 static int setdebug_cmd(UAContext *ua, char *cmd);
67 static int 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"),     cancelcmd,     _("cancel job=nnn -- cancel a job")},
92  { N_("create"),     createcmd,     _("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 debug=1 give full listing")},
95  { N_("exit"),       quit_cmd,      _("exit = quit")},
96  { N_("help"),       help_cmd,      _("print this command")},
97  { N_("label"),      labelcmd,      _("label a tape")},
98  { N_("list"),       listcmd,       _("list [pools | jobs | jobtotals | media <pool> | files job=<nn>]; from catalog")},
99  { N_("llist"),      llistcmd,      _("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"),    relabelcmd,    _("relabel a tape")},
107  { N_("release"),    release_cmd,   _("release <storage-name>")},
108  { N_("restore"),    restorecmd,    _("restore files")},
109  { N_("run"),        runcmd,        _("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"),     statuscmd,     _("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    strcpy(mr->VolStatus, "Append");
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 cancelcmd(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_WaitMaxJobs:
429    case JS_WaitStartTime:
430       set_jcr_job_status(jcr, JS_Canceled);
431       bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
432               jcr->JobId, jcr->Job);
433 #ifndef USE_SEMAPHORE
434       workq_remove(&job_wq, jcr->work_item); /* attempt to remove it from queue */
435 #endif
436       free_jcr(jcr);
437       return 1;
438          
439    default:
440       set_jcr_job_status(jcr, JS_Canceled);
441
442       /* Cancel File daemon */
443       if (jcr->file_bsock) {
444          ua->jcr->client = jcr->client;
445          if (!connect_to_file_daemon(ua->jcr, 10, FDConnectTimeout, 1)) {
446             bsendmsg(ua, _("Failed to connect to File daemon.\n"));
447             free_jcr(jcr);
448             return 1;
449          }
450          Dmsg0(200, "Connected to file daemon\n");
451          fd = ua->jcr->file_bsock;
452          bnet_fsend(fd, "cancel Job=%s\n", jcr->Job);
453          while (bnet_recv(fd) >= 0) {
454             bsendmsg(ua, "%s", fd->msg);
455          }
456          bnet_sig(fd, BNET_TERMINATE);
457          bnet_close(fd);
458          ua->jcr->file_bsock = NULL;
459       }
460
461       /* Cancel Storage daemon */
462       if (jcr->store_bsock) {
463          ua->jcr->store = jcr->store;
464          if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
465             bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
466             free_jcr(jcr);
467             return 1;
468          }
469          Dmsg0(200, "Connected to storage daemon\n");
470          sd = ua->jcr->store_bsock;
471          bnet_fsend(sd, "cancel Job=%s\n", jcr->Job);
472          while (bnet_recv(sd) >= 0) {
473             bsendmsg(ua, "%s", sd->msg);
474          }
475          bnet_sig(sd, BNET_TERMINATE);
476          bnet_close(sd);
477          ua->jcr->store_bsock = NULL;
478       }
479    }
480    free_jcr(jcr);
481
482    return 1; 
483 }
484
485 /*
486  * This is a common routine to create or update a
487  *   Pool DB base record from a Pool Resource. We handle
488  *   the setting of MaxVols and NumVols slightly differently
489  *   depending on if we are creating the Pool or we are
490  *   simply bringing it into agreement with the resource (updage).
491  */
492 static void set_pooldbr_from_poolres(POOL_DBR *pr, POOL *pool, e_pool_op op)
493 {
494    strcpy(pr->PoolType, pool->pool_type);
495    if (op == POOL_OP_CREATE) {
496       pr->MaxVols = pool->max_volumes;
497       pr->NumVols = 0;
498    } else {          /* update pool */
499       if (pr->MaxVols != pool->max_volumes) {
500          pr->MaxVols = pool->max_volumes;
501       }
502       if (pr->MaxVols != 0 && pr->MaxVols < pr->NumVols) {
503          pr->MaxVols = pr->NumVols;
504       }
505    }
506    pr->UseOnce = pool->use_volume_once;
507    pr->UseCatalog = pool->use_catalog;
508    pr->AcceptAnyVolume = pool->accept_any_volume;
509    pr->Recycle = pool->Recycle;
510    pr->VolRetention = pool->VolRetention;
511    pr->VolUseDuration = pool->VolUseDuration;
512    pr->MaxVolJobs = pool->MaxVolJobs;
513    pr->MaxVolFiles = pool->MaxVolFiles;
514    pr->MaxVolBytes = pool->MaxVolBytes;
515    pr->AutoPrune = pool->AutoPrune;
516    pr->Recycle = pool->Recycle;
517    if (pool->label_format) {
518       strcpy(pr->LabelFormat, pool->label_format);
519    } else {
520       strcpy(pr->LabelFormat, "*");    /* none */
521    }
522 }
523
524
525 /*
526  * Create a pool record from a given Pool resource
527  *   Also called from backup.c
528  * Returns: -1  on error
529  *           0  record already exists
530  *           1  record created
531  */
532
533 int create_pool(JCR *jcr, B_DB *db, POOL *pool, e_pool_op op)
534 {
535    POOL_DBR  pr;
536
537    memset(&pr, 0, sizeof(POOL_DBR));
538
539    strcpy(pr.Name, pool->hdr.name);
540
541    if (db_get_pool_record(jcr, db, &pr)) {
542       /* Pool Exists */
543       if (op == POOL_OP_UPDATE) {  /* update request */
544          set_pooldbr_from_poolres(&pr, pool, op);
545          db_update_pool_record(jcr, db, &pr);
546       }
547       return 0;                       /* exists */
548    }
549
550    set_pooldbr_from_poolres(&pr, pool, op);
551
552    if (!db_create_pool_record(jcr, db, &pr)) {
553       return -1;                      /* error */
554    }
555    return 1;
556 }
557
558
559
560 /*
561  * Create a Pool Record in the database.
562  *  It is always created from the Resource record.
563  */
564 static int createcmd(UAContext *ua, char *cmd) 
565 {
566    POOL *pool;
567
568    if (!open_db(ua)) {
569       return 1;
570    }
571
572    pool = get_pool_resource(ua);
573    if (!pool) {
574       return 1;
575    }
576
577    switch (create_pool(ua->jcr, ua->db, pool, POOL_OP_CREATE)) {
578    case 0:
579       bsendmsg(ua, _("Error: Pool %s already exists.\n\
580 Use update to change it.\n"), pool->hdr.name);
581       break;
582
583    case -1:
584       bsendmsg(ua, db_strerror(ua->db));
585       break;
586
587    default:
588      break;
589    }
590    bsendmsg(ua, _("Pool %s created.\n"), pool->hdr.name);
591    return 1;
592 }
593
594
595
596
597 /*
598  * Update a Pool Record in the database.
599  *  It is always updated from the Resource record.
600  *
601  *    update pool=<pool-name>
602  *         updates pool from Pool resource
603  *    update media pool=<pool-name> volume=<volume-name>
604  *         changes pool info for volume
605  */
606 static int update_cmd(UAContext *ua, char *cmd) 
607 {
608    static char *kw[] = {
609       N_("media"),  /* 0 */
610       N_("volume"), /* 1 */
611       N_("pool"),   /* 2 */
612       N_("slots"),  /* 3 */
613       NULL};
614
615    if (!open_db(ua)) {
616       return 1;
617    }
618
619    switch (find_arg_keyword(ua, kw)) {
620    case 0:
621    case 1:
622       update_volume(ua);
623       return 1;
624    case 2:
625       update_pool(ua);
626       return 1;
627    case 3:
628       update_slots(ua);
629       return 1;
630    default:
631       break;
632    }
633     
634    start_prompt(ua, _("Update choice:\n"));
635    add_prompt(ua, _("Volume parameters"));
636    add_prompt(ua, _("Pool from resource"));
637    add_prompt(ua, _("Slots from autochanger"));
638    switch (do_prompt(ua, _("item"), _("Choose catalog item to update"), NULL, 0)) {
639    case 0:
640       update_volume(ua);
641       break;
642    case 1:
643       update_pool(ua);
644       break;
645    case 2:
646       update_slots(ua);
647       break;
648    default:
649       break;
650    }
651    return 1;
652 }
653
654 static void update_volstatus(UAContext *ua, char *val, MEDIA_DBR *mr)
655 {
656    POOLMEM *query = get_pool_memory(PM_MESSAGE);
657    bstrncpy(mr->VolStatus, val, sizeof(mr->VolStatus));
658    Mmsg(&query, "UPDATE Media SET VolStatus='%s' WHERE MediaId=%u",
659       mr->VolStatus, mr->MediaId);
660    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
661       bsendmsg(ua, "%s", db_strerror(ua->db));
662    } else {
663       bsendmsg(ua, _("New Volume status is: %s\n"), mr->VolStatus);
664    }
665    free_pool_memory(query);
666 }
667
668 static void update_volretention(UAContext *ua, char *val, MEDIA_DBR *mr)
669 {
670    char ed1[50];
671    POOLMEM *query = get_pool_memory(PM_MESSAGE);
672    if (!duration_to_utime(val, &mr->VolRetention)) {
673       bsendmsg(ua, _("Invalid retention period specified.\n"));
674       return;
675    }
676    Mmsg(&query, "UPDATE Media SET VolRetention=%s WHERE MediaId=%u",
677       edit_uint64(mr->VolRetention, ed1), mr->MediaId);
678    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
679       bsendmsg(ua, "%s", db_strerror(ua->db));
680    } else {
681       bsendmsg(ua, _("New retention seconds is: %s\n"),
682          edit_utime(mr->VolRetention, ed1));
683    }
684    free_pool_memory(query);
685 }
686
687 static void update_voluseduration(UAContext *ua, char *val, MEDIA_DBR *mr)
688 {
689    char ed1[50];
690    POOLMEM *query = get_pool_memory(PM_MESSAGE);
691    if (!duration_to_utime(val, &mr->VolUseDuration)) {
692       bsendmsg(ua, _("Invalid use duration specified.\n"));
693       return;
694    }
695    query = get_pool_memory(PM_MESSAGE);
696    Mmsg(&query, "UPDATE Media SET VolUseDuration=%s WHERE MediaId=%u",
697       edit_uint64(mr->VolUseDuration, ed1), mr->MediaId);
698    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
699       bsendmsg(ua, "%s", db_strerror(ua->db));
700    } else {
701       bsendmsg(ua, _("New use duration is: %s\n"),
702          edit_utime(mr->VolUseDuration, ed1));
703    }
704    free_pool_memory(query);
705 }
706
707 static void update_volmaxjobs(UAContext *ua, char *val, MEDIA_DBR *mr)
708 {
709    POOLMEM *query = get_pool_memory(PM_MESSAGE);
710    Mmsg(&query, "UPDATE Media SET MaxVolJobs=%s WHERE MediaId=%u",
711       val, mr->MediaId);
712    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
713       bsendmsg(ua, "%s", db_strerror(ua->db));
714    } else {
715       bsendmsg(ua, _("New max jobs is: %s\n"), val);
716    }
717    free_pool_memory(query);
718 }
719
720 static void update_volmaxfiles(UAContext *ua, char *val, MEDIA_DBR *mr)
721 {
722    POOLMEM *query = get_pool_memory(PM_MESSAGE);
723    Mmsg(&query, "UPDATE Media SET MaxVolFiles=%s WHERE MediaId=%u",
724       val, mr->MediaId);
725    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
726       bsendmsg(ua, "%s", db_strerror(ua->db));
727    } else {
728       bsendmsg(ua, _("New max files is: %s\n"), val);
729    }
730    free_pool_memory(query);
731 }
732
733 static void update_volmaxbytes(UAContext *ua, char *val, MEDIA_DBR *mr)
734 {
735    uint64_t maxbytes;
736    char ed1[50];
737    POOLMEM *query = get_pool_memory(PM_MESSAGE);
738    if (!size_to_uint64(val, strlen(val), &maxbytes)) {
739       bsendmsg(ua, _("Invalid byte size specification.\n"));
740       return;
741    } 
742    Mmsg(&query, "UPDATE Media SET MaxVolBytes=%s WHERE MediaId=%u",
743       edit_uint64(maxbytes, ed1), mr->MediaId);
744    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
745       bsendmsg(ua, "%s", db_strerror(ua->db));
746    } else {
747       bsendmsg(ua, _("New Max bytes is: %s\n"), edit_uint64(maxbytes, ed1));
748    }
749    free_pool_memory(query);
750 }
751
752 static void update_volrecycle(UAContext *ua, char *val, MEDIA_DBR *mr)
753 {
754    int recycle;
755    POOLMEM *query = get_pool_memory(PM_MESSAGE);
756    if (strcasecmp(val, _("yes")) == 0) {
757       recycle = 1;
758    } else if (strcasecmp(val, _("no")) == 0) {
759       recycle = 0;
760    } else {
761       bsendmsg(ua, _("Invalid value. It must by yes or no.\n"));
762       return;
763    }
764    Mmsg(&query, "UPDATE Media SET Recycle=%d WHERE MediaId=%u",
765       recycle, mr->MediaId);
766    if (!db_sql_query(ua->db, query, NULL, NULL)) {  
767       bsendmsg(ua, "%s", db_strerror(ua->db));
768    } else {       
769       bsendmsg(ua, _("New recycle flag is: %s\n"),
770          mr->Recycle==1?_("yes"):_("no"));
771    }
772    free_pool_memory(query);
773 }
774
775 /*
776  * Update a media record -- allows you to change the
777  *  Volume status. E.g. if you want Bacula to stop
778  *  writing on the volume, set it to anything other
779  *  than Append.
780  */              
781 static int update_volume(UAContext *ua)
782 {
783    MEDIA_DBR mr;
784    POOLMEM *query;
785    char ed1[30];
786    bool done = false;
787    char *kw[] = {
788       N_("VolStatus"),
789       N_("VolRetention"),
790       N_("VolUse"),
791       N_("MaxVolJobs"),
792       N_("MaxVolFiles"),
793       N_("Recycle"),
794       NULL };
795
796    if (!select_media_dbr(ua, &mr)) {
797       return 0;
798    }
799    for (int i=0; kw[i]; i++) {
800       if (find_arg_with_value(ua, kw[i]) > 0) {
801          switch (i) {
802          case 0:
803             update_volstatus(ua, ua->argv[i], &mr);
804             break;
805          }
806          done = true;
807       }
808    }
809
810    for ( ; !done; ) {
811       if (!select_media_dbr(ua, &mr)) {
812          return 0;
813       }
814       bsendmsg(ua, _("Updating Volume \"%s\"\n"), mr.VolumeName);
815       start_prompt(ua, _("Parameters to modify:\n"));
816       add_prompt(ua, _("Volume Status"));
817       add_prompt(ua, _("Volume Retention Period"));
818       add_prompt(ua, _("Volume Use Duration"));
819       add_prompt(ua, _("Maximum Volume Jobs"));
820       add_prompt(ua, _("Maximum Volume Files"));
821       add_prompt(ua, _("Maximum Volume Bytes"));
822       add_prompt(ua, _("Recycle Flag"));
823       add_prompt(ua, _("Slot"));
824       add_prompt(ua, _("Volume Files"));
825       add_prompt(ua, _("Done"));
826       switch (do_prompt(ua, "", _("Select parameter to modify"), NULL, 0)) {
827       case 0:                         /* Volume Status */
828          /* Modify Volume Status */
829          bsendmsg(ua, _("Current Volume status is: %s\n"), mr.VolStatus);
830          start_prompt(ua, _("Possible Values are:\n"));
831          add_prompt(ua, "Append");      /* Better not translate these as */
832          add_prompt(ua, "Archive");     /* They are known in the database code */
833          add_prompt(ua, "Disabled");
834          add_prompt(ua, "Full");
835          add_prompt(ua, "Used");
836          add_prompt(ua, "Cleaning");
837          if (strcmp(mr.VolStatus, "Purged") == 0) {
838             add_prompt(ua, "Recycle");
839          }
840          add_prompt(ua, "Read-Only");
841          if (do_prompt(ua, "", _("Choose new Volume Status"), ua->cmd, sizeof(mr.VolStatus)) < 0) {
842             return 1;
843          }
844          update_volstatus(ua, ua->cmd, &mr);
845          break;
846       case 1:                         /* Retention */
847          bsendmsg(ua, _("Current retention seconds is: %s\n"),
848             edit_utime(mr.VolRetention, ed1));
849          if (!get_cmd(ua, _("Enter Volume Retention period: "))) {
850             return 0;
851          }
852          update_volretention(ua, ua->cmd, &mr);
853          break;
854
855       case 2:                         /* Use Duration */
856          bsendmsg(ua, _("Current use duration is: %s\n"),
857             edit_utime(mr.VolUseDuration, ed1));
858          if (!get_cmd(ua, _("Enter Volume Use Duration: "))) {
859             return 0;
860          }
861          update_voluseduration(ua, ua->cmd, &mr);
862          break;
863
864       case 3:                         /* Max Jobs */
865          bsendmsg(ua, _("Current max jobs is: %u\n"), mr.MaxVolJobs);
866          if (!get_pint(ua, _("Enter new Maximum Jobs: "))) {
867             return 0;
868          }
869          update_volmaxjobs(ua, ua->cmd, &mr);
870          break;
871
872       case 4:                         /* Max Files */
873          bsendmsg(ua, _("Current max files is: %u\n"), mr.MaxVolFiles);
874          if (!get_pint(ua, _("Enter new Maximum Files: "))) {
875             return 0;
876          }
877          update_volmaxfiles(ua, ua->cmd, &mr);
878          break;
879
880       case 5:                         /* Max Bytes */
881          bsendmsg(ua, _("Current value is: %s\n"), edit_uint64(mr.MaxVolBytes, ed1));
882          if (!get_cmd(ua, _("Enter new Maximum Bytes: "))) {
883             return 0;
884          }
885          update_volmaxbytes(ua, ua->cmd, &mr);
886          break;
887
888
889       case 6:                         /* Recycle */
890          bsendmsg(ua, _("Current recycle flag is: %s\n"),
891             mr.Recycle==1?_("yes"):_("no"));
892          if (!get_yesno(ua, _("Enter new Recycle status: "))) {
893             return 0;
894          }
895          update_volrecycle(ua, ua->cmd, &mr);
896          break;
897
898       case 7:                         /* Slot */
899          int slot;
900          POOL_DBR pr;
901
902          memset(&pr, 0, sizeof(POOL_DBR));
903          pr.PoolId = mr.PoolId;
904          if (!db_get_pool_record(ua->jcr, ua->db, &pr)) {
905             bsendmsg(ua, "%s", db_strerror(ua->db));
906             return 0;
907          }
908          bsendmsg(ua, _("Current Slot is: %d\n"), mr.Slot);
909          if (!get_pint(ua, _("Enter new Slot: "))) {
910             return 0;
911          }
912          slot = ua->pint32_val;
913          if (pr.MaxVols > 0 && slot > (int)pr.MaxVols) {
914             bsendmsg(ua, _("Invalid slot, it must be between 0 and %d\n"),
915                pr.MaxVols);
916             break;
917          }
918          query = get_pool_memory(PM_MESSAGE);
919          Mmsg(&query, "UPDATE Media SET Slot=%d WHERE MediaId=%u",
920             slot, mr.MediaId);
921          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
922             bsendmsg(ua, "%s", db_strerror(ua->db));
923          } else {
924             bsendmsg(ua, "New Slot is: %d\n", slot);
925          }
926          free_pool_memory(query);
927          break;
928
929       case 8:                         /* Volume Files */
930          int32_t VolFiles;
931          bsendmsg(ua, _("Warning changing Volume Files can result\n"
932                         "in loss of data on your Volume\n\n"));
933          bsendmsg(ua, _("Current Volume Files is: %u\n"), mr.VolFiles);
934          if (!get_pint(ua, _("Enter new number of Files for Volume: "))) {
935             return 0;
936          }
937          VolFiles = ua->pint32_val;
938          if (VolFiles != (int)(mr.VolFiles + 1)) {
939             bsendmsg(ua, _("Normally, you should only increase Volume Files by one!\n"));
940             if (!get_yesno(ua, _("Continue? (yes/no): ")) || ua->pint32_val == 0) {
941                break;
942             }
943          }
944          query = get_pool_memory(PM_MESSAGE);
945          Mmsg(&query, "UPDATE Media SET VolFiles=%u WHERE MediaId=%u",
946             VolFiles, mr.MediaId);
947          if (!db_sql_query(ua->db, query, NULL, NULL)) {  
948             bsendmsg(ua, "%s", db_strerror(ua->db));
949          } else {
950             bsendmsg(ua, _("New Volume Files is: %u\n"), VolFiles);
951          }
952          free_pool_memory(query);
953          break;
954
955       default:                        /* Done or error */
956          bsendmsg(ua, "Selection done.\n");
957          return 1;
958       }
959    }
960    return 1;
961 }
962
963 /* 
964  * Update pool record -- pull info from current POOL resource
965  */
966 static int update_pool(UAContext *ua)
967 {
968    POOL_DBR  pr;
969    int id;
970    POOL *pool;
971    POOLMEM *query;       
972    
973    pool = get_pool_resource(ua);
974    if (!pool) {
975       return 0;
976    }
977
978    memset(&pr, 0, sizeof(pr));
979    strcpy(pr.Name, pool->hdr.name);
980    if (!get_pool_dbr(ua, &pr)) {
981       return 0;
982    }
983
984    set_pooldbr_from_poolres(&pr, pool, POOL_OP_UPDATE); /* update */
985
986    id = db_update_pool_record(ua->jcr, ua->db, &pr);
987    if (id <= 0) {
988       bsendmsg(ua, _("db_update_pool_record returned %d. ERR=%s\n"),
989          id, db_strerror(ua->db));
990    }
991    query = get_pool_memory(PM_MESSAGE);
992    Mmsg(&query, list_pool, pr.PoolId);
993    db_list_sql_query(ua->jcr, ua->db, query, prtit, ua, 1, HORZ_LIST);
994    free_pool_memory(query);
995    bsendmsg(ua, _("Pool DB record updated from resource.\n"));
996    return 1;
997 }
998
999
1000 static void do_storage_setdebug(UAContext *ua, STORE *store, int level)
1001 {
1002    BSOCK *sd;
1003
1004    ua->jcr->store = store;
1005    /* Try connecting for up to 15 seconds */
1006    bsendmsg(ua, _("Connecting to Storage daemon %s at %s:%d\n"), 
1007       store->hdr.name, store->address, store->SDport);
1008    if (!connect_to_storage_daemon(ua->jcr, 1, 15, 0)) {
1009       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1010       return;
1011    }
1012    Dmsg0(120, _("Connected to storage daemon\n"));
1013    sd = ua->jcr->store_bsock;
1014    bnet_fsend(sd, "setdebug=%d\n", level);
1015    if (bnet_recv(sd) >= 0) {
1016       bsendmsg(ua, "%s", sd->msg);
1017    }
1018    bnet_sig(sd, BNET_TERMINATE);
1019    bnet_close(sd);
1020    ua->jcr->store_bsock = NULL;
1021    return;  
1022 }
1023    
1024 static void do_client_setdebug(UAContext *ua, CLIENT *client, int level)
1025 {
1026    BSOCK *fd;
1027
1028    /* Connect to File daemon */
1029
1030    ua->jcr->client = client;
1031    /* Try to connect for 15 seconds */
1032    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"), 
1033       client->hdr.name, client->address, client->FDport);
1034    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1035       bsendmsg(ua, _("Failed to connect to Client.\n"));
1036       return;
1037    }
1038    Dmsg0(120, "Connected to file daemon\n");
1039    fd = ua->jcr->file_bsock;
1040    bnet_fsend(fd, "setdebug=%d\n", level);
1041    if (bnet_recv(fd) >= 0) {
1042       bsendmsg(ua, "%s", fd->msg);
1043    }
1044    bnet_sig(fd, BNET_TERMINATE);
1045    bnet_close(fd);
1046    ua->jcr->file_bsock = NULL;
1047    return;  
1048 }
1049
1050
1051 static void do_all_setdebug(UAContext *ua, int level)
1052 {
1053    STORE *store, **unique_store;
1054    CLIENT *client, **unique_client;
1055    int i, j, found;
1056
1057    /* Director */
1058    debug_level = level;
1059
1060    /* Count Storage items */
1061    LockRes();
1062    store = NULL;
1063    for (i=0; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); i++)
1064       { }
1065    unique_store = (STORE **) malloc(i * sizeof(STORE));
1066    /* Find Unique Storage address/port */         
1067    store = (STORE *)GetNextRes(R_STORAGE, NULL);
1068    i = 0;
1069    unique_store[i++] = store;
1070    while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
1071       found = 0;
1072       for (j=0; j<i; j++) {
1073          if (strcmp(unique_store[j]->address, store->address) == 0 &&
1074              unique_store[j]->SDport == store->SDport) {
1075             found = 1;
1076             break;
1077          }
1078       }
1079       if (!found) {
1080          unique_store[i++] = store;
1081          Dmsg2(140, "Stuffing: %s:%d\n", store->address, store->SDport);
1082       }
1083    }
1084    UnlockRes();
1085
1086    /* Call each unique Storage daemon */
1087    for (j=0; j<i; j++) {
1088       do_storage_setdebug(ua, unique_store[j], level);
1089    }
1090    free(unique_store);
1091
1092    /* Count Client items */
1093    LockRes();
1094    client = NULL;
1095    for (i=0; (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)); i++)
1096       { }
1097    unique_client = (CLIENT **) malloc(i * sizeof(CLIENT));
1098    /* Find Unique Client address/port */         
1099    client = (CLIENT *)GetNextRes(R_CLIENT, NULL);
1100    i = 0;
1101    unique_client[i++] = client;
1102    while ((client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client))) {
1103       found = 0;
1104       for (j=0; j<i; j++) {
1105          if (strcmp(unique_client[j]->address, client->address) == 0 &&
1106              unique_client[j]->FDport == client->FDport) {
1107             found = 1;
1108             break;
1109          }
1110       }
1111       if (!found) {
1112          unique_client[i++] = client;
1113          Dmsg2(140, "Stuffing: %s:%d\n", client->address, client->FDport);
1114       }
1115    }
1116    UnlockRes();
1117
1118    /* Call each unique File daemon */
1119    for (j=0; j<i; j++) {
1120       do_client_setdebug(ua, unique_client[j], level);
1121    }
1122    free(unique_client);
1123 }
1124
1125 /*
1126  * setdebug level=nn all
1127  */
1128 static int setdebug_cmd(UAContext *ua, char *cmd)
1129 {
1130    STORE *store;
1131    CLIENT *client;
1132    int level;
1133    int i;
1134
1135    if (!open_db(ua)) {
1136       return 1;
1137    }
1138    Dmsg1(120, "setdebug:%s:\n", cmd);
1139
1140    level = -1;
1141    i = find_arg_with_value(ua, _("level"));
1142    if (i >= 0) {
1143       level = atoi(ua->argv[i]);
1144    }
1145    if (level < 0) {
1146       if (!get_pint(ua, _("Enter new debug level: "))) {
1147          return 1;
1148       }
1149       level = ua->pint32_val;
1150    }
1151
1152    /* General debug? */
1153    for (i=1; i<ua->argc; i++) {
1154       if (strcasecmp(ua->argk[i], _("all")) == 0) {
1155          do_all_setdebug(ua, level);
1156          return 1;
1157       }
1158       if (strcasecmp(ua->argk[i], _("dir")) == 0 ||
1159           strcasecmp(ua->argk[i], _("director")) == 0) {
1160          debug_level = level;
1161          return 1;
1162       }
1163       if (strcasecmp(ua->argk[i], _("client")) == 0) {
1164          client = NULL;
1165          if (ua->argv[i]) {
1166             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1167             if (client) {
1168                do_client_setdebug(ua, client, level);
1169                return 1;
1170             }
1171          }
1172          client = select_client_resource(ua);   
1173          if (client) {
1174             do_client_setdebug(ua, client, level);
1175             return 1;
1176          }
1177       }
1178
1179       if (strcasecmp(ua->argk[i], _("store")) == 0 ||
1180           strcasecmp(ua->argk[i], _("storage")) == 0) {
1181          store = NULL;
1182          if (ua->argv[i]) {
1183             store = (STORE *)GetResWithName(R_STORAGE, ua->argv[i]);
1184             if (store) {
1185                do_storage_setdebug(ua, store, level);
1186                return 1;
1187             }
1188          }
1189          store = get_storage_resource(ua, 0);
1190          if (store) {
1191             do_storage_setdebug(ua, store, level);
1192             return 1;
1193          }
1194       }
1195    } 
1196    /*
1197     * We didn't find an appropriate keyword above, so
1198     * prompt the user.
1199     */
1200    start_prompt(ua, _("Available daemons are: \n"));
1201    add_prompt(ua, _("Director"));
1202    add_prompt(ua, _("Storage"));
1203    add_prompt(ua, _("Client"));
1204    add_prompt(ua, _("All"));
1205    switch(do_prompt(ua, "", _("Select daemon type to set debug level"), NULL, 0)) {
1206    case 0:                         /* Director */
1207       debug_level = level;
1208       break;
1209    case 1:
1210       store = get_storage_resource(ua, 0);
1211       if (store) {
1212          do_storage_setdebug(ua, store, level);
1213       }
1214       break;
1215    case 2:
1216       client = select_client_resource(ua);
1217       if (client) {
1218          do_client_setdebug(ua, client, level);
1219       }
1220       break;
1221    case 3:
1222       do_all_setdebug(ua, level);
1223       break;
1224    default:
1225       break;
1226    }
1227    return 1;
1228 }
1229
1230 static int var_cmd(UAContext *ua, char *cmd)
1231 {
1232    POOLMEM *var = get_pool_memory(PM_FNAME);
1233    for (int i=1; i<ua->argc; i++) {
1234       if (ua->argk[i] && variable_expansion(ua->jcr, ua->argk[i], &var)) {
1235          bsendmsg(ua, "%s\n", var);
1236       }
1237    }
1238    free_pool_memory(var);
1239    return 1;
1240 }
1241
1242 static int estimate_cmd(UAContext *ua, char *cmd)
1243 {
1244    JOB *job = NULL;
1245    CLIENT *client = NULL;
1246    FILESET *fileset = NULL;
1247    int listing = 0;
1248    BSOCK *fd;
1249    char since[MAXSTRING];
1250
1251    for (int i=1; i<ua->argc; i++) {
1252       if (strcasecmp(ua->argk[i], _("client")) == 0) {
1253          if (ua->argv[i]) {
1254             client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
1255             continue;
1256          }
1257       }
1258       if (strcasecmp(ua->argk[i], _("job")) == 0) {
1259          if (ua->argv[i]) {
1260             job = (JOB *)GetResWithName(R_JOB, ua->argv[i]);
1261             continue;
1262          }
1263       }
1264       if (strcasecmp(ua->argk[i], _("fileset")) == 0) {
1265          if (ua->argv[i]) {
1266             fileset = (FILESET *)GetResWithName(R_FILESET, ua->argv[i]);
1267             continue;
1268          }
1269       }
1270       if (strcasecmp(ua->argk[i], _("listing")) == 0) {
1271          listing = 1;
1272       }
1273    } 
1274    if (!job && !(client && fileset)) {
1275       if (!(job = select_job_resource(ua))) {
1276          return 1;
1277       }
1278    }
1279    if (!job) {
1280       job = (JOB *)GetResWithName(R_JOB, ua->argk[1]);
1281    }
1282    if (!client) {
1283       client = job->client;
1284    }
1285    if (!fileset) {
1286       fileset = job->fileset;
1287    }
1288    ua->jcr->client = client;
1289    ua->jcr->fileset = fileset;
1290    close_db(ua);
1291    ua->catalog = client->catalog;
1292
1293    if (!open_db(ua)) {
1294       return 1;
1295    }
1296
1297    get_level_since_time(ua->jcr, since, sizeof(since));
1298
1299    bsendmsg(ua, _("Connecting to Client %s at %s:%d\n"),
1300       job->client->hdr.name, job->client->address, job->client->FDport);
1301    if (!connect_to_file_daemon(ua->jcr, 1, 15, 0)) {
1302       bsendmsg(ua, _("Failed to connect to Client.\n"));
1303       return 1;
1304    }
1305    fd = ua->jcr->file_bsock;
1306
1307    if (!send_include_list(ua->jcr)) {
1308       bsendmsg(ua, _("Error sending include list.\n"));
1309       return 1;
1310    }
1311
1312    if (!send_exclude_list(ua->jcr)) {
1313       bsendmsg(ua, _("Error sending exclude list.\n"));
1314       return 1;
1315    }
1316
1317    if (!send_level_command(ua->jcr)) {
1318       return 1;
1319    }
1320
1321    bnet_fsend(fd, "estimate listing=%d\n", listing);
1322    while (bnet_recv(fd) >= 0) {
1323       bsendmsg(ua, "%s", fd->msg);
1324    }
1325
1326    bnet_sig(fd, BNET_TERMINATE);
1327    bnet_close(fd);
1328    ua->jcr->file_bsock = NULL;
1329
1330    return 1;
1331 }
1332
1333
1334 /*
1335  * print time
1336  */
1337 static int time_cmd(UAContext *ua, char *cmd)
1338 {
1339    char sdt[50];
1340    time_t ttime = time(NULL);
1341    struct tm tm;
1342    localtime_r(&ttime, &tm);
1343    strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
1344    bsendmsg(ua, "%s\n", sdt);
1345    return 1;
1346 }
1347
1348
1349
1350 /*
1351  * Delete Pool records (should purge Media with it).
1352  *
1353  *  delete pool=<pool-name>
1354  *  delete media pool=<pool-name> volume=<name>
1355  */
1356 static int delete_cmd(UAContext *ua, char *cmd)
1357 {
1358    static char *keywords[] = {
1359       N_("volume"),
1360       N_("pool"),
1361       NULL};
1362
1363    if (!open_db(ua)) {
1364       return 1;
1365    }
1366
1367    bsendmsg(ua, _(
1368 "In general it is not a good idea to delete either a\n"
1369 "Pool or a Volume since they may contain data.\n\n"));
1370      
1371    switch (find_arg_keyword(ua, keywords)) {
1372    case 0:
1373       delete_volume(ua);     
1374       return 1;
1375    case 1:
1376       delete_pool(ua);
1377       return 1;
1378    default:
1379       break;
1380    }
1381    switch (do_keyword_prompt(ua, _("Choose catalog item to delete"), keywords)) {
1382    case 0:
1383       delete_volume(ua);
1384       break;
1385    case 1:
1386       delete_pool(ua);
1387       break;
1388    default:
1389       bsendmsg(ua, _("Nothing done.\n"));
1390       break;
1391    }
1392    return 1;
1393 }
1394
1395 /*
1396  * Delete media records from database -- dangerous 
1397  */
1398 static int delete_volume(UAContext *ua)
1399 {
1400    MEDIA_DBR mr;
1401
1402    if (!select_media_dbr(ua, &mr)) {
1403       return 1;
1404    }
1405    bsendmsg(ua, _("\nThis command will delete volume %s\n"
1406       "and all Jobs saved on that volume from the Catalog\n"),
1407       mr.VolumeName);
1408
1409    if (!get_yesno(ua, _("Are you sure you want to delete this Volume? (yes/no): "))) {
1410       return 1;
1411    }
1412    if (ua->pint32_val) {
1413       db_delete_media_record(ua->jcr, ua->db, &mr);
1414    }
1415    return 1;
1416 }
1417
1418 /*
1419  * Delete a pool record from the database -- dangerous   
1420  */
1421 static int delete_pool(UAContext *ua)
1422 {
1423    POOL_DBR  pr;
1424    
1425    memset(&pr, 0, sizeof(pr));
1426
1427    if (!get_pool_dbr(ua, &pr)) {
1428       return 1;
1429    }
1430    if (!get_yesno(ua, _("Are you sure you want to delete this Pool? (yes/no): "))) {
1431       return 1;
1432    }
1433    if (ua->pint32_val) {
1434       db_delete_pool_record(ua->jcr, ua->db, &pr);
1435    }
1436    return 1;
1437 }
1438
1439
1440 static void do_mount_cmd(UAContext *ua, char *command)
1441 {
1442    STORE *store;
1443    BSOCK *sd;
1444    char dev_name[MAX_NAME_LENGTH];
1445
1446
1447    if (!open_db(ua)) {
1448       return;
1449    }
1450    Dmsg2(120, "%s: %s\n", command, ua->UA_sock->msg);
1451
1452    store = get_storage_resource(ua, 1);
1453    if (!store) {
1454       return;
1455    }
1456
1457    Dmsg2(120, "Found storage, MediaType=%s DevName=%s\n",
1458       store->media_type, store->dev_name);
1459
1460    ua->jcr->store = store;
1461    if (!connect_to_storage_daemon(ua->jcr, 10, SDConnectTimeout, 1)) {
1462       bsendmsg(ua, _("Failed to connect to Storage daemon.\n"));
1463       return;
1464    }
1465    sd = ua->jcr->store_bsock;
1466    strcpy(dev_name, store->dev_name);
1467    bash_spaces(dev_name);
1468    bnet_fsend(sd, "%s %s", command, dev_name);
1469    while (bnet_recv(sd) >= 0) {
1470       bsendmsg(ua, "%s", sd->msg);
1471    }
1472    bnet_sig(sd, BNET_TERMINATE);
1473    bnet_close(sd);
1474    ua->jcr->store_bsock = NULL;
1475 }
1476
1477 /*
1478  * mount [storage | device] <name>
1479  */
1480 static int mount_cmd(UAContext *ua, char *cmd)
1481 {
1482    do_mount_cmd(ua, "mount");          /* mount */
1483    return 1;
1484 }
1485
1486
1487 /*
1488  * unmount [storage | device] <name>
1489  */
1490 static int unmount_cmd(UAContext *ua, char *cmd)
1491 {
1492    do_mount_cmd(ua, "unmount");          /* unmount */
1493    return 1;
1494 }
1495
1496
1497 /*
1498  * release [storage | device] <name>
1499  */
1500 static int release_cmd(UAContext *ua, char *cmd)
1501 {
1502    do_mount_cmd(ua, "release");          /* release */
1503    return 1;
1504 }
1505
1506
1507 /*
1508  * Switch databases
1509  *   use catalog=<name>
1510  */
1511 static int use_cmd(UAContext *ua, char *cmd)
1512 {
1513    CAT *oldcatalog, *catalog;
1514
1515
1516    close_db(ua);                      /* close any previously open db */
1517    oldcatalog = ua->catalog;
1518
1519    if (!(catalog = get_catalog_resource(ua))) {
1520       ua->catalog = oldcatalog;
1521    } else {
1522       ua->catalog = catalog;
1523    }
1524    if (open_db(ua)) {
1525       bsendmsg(ua, _("Using Catalog name=%s DB=%s\n"),
1526          ua->catalog->hdr.name, ua->catalog->db_name);
1527    }
1528    return 1;
1529 }
1530
1531 int quit_cmd(UAContext *ua, char *cmd) 
1532 {
1533    ua->quit = TRUE;
1534    return 1;
1535 }
1536
1537 /*
1538  * Wait until no job is running 
1539  */
1540 int wait_cmd(UAContext *ua, char *cmd) 
1541 {
1542    bmicrosleep(0, 200000);            /* let job actually start */
1543    for (int running=1; running; ) {
1544       running = 0;
1545       lock_jcr_chain();
1546       for (JCR *jcr=NULL; (jcr=get_next_jcr(jcr)); ) {
1547          if (jcr->JobId != 0) {
1548             running = 1;
1549             free_locked_jcr(jcr);
1550             break;
1551          }
1552          free_locked_jcr(jcr);
1553       }
1554       unlock_jcr_chain();
1555       if (running) {
1556          bmicrosleep(1, 0);
1557       }
1558    }
1559    return 1;
1560 }
1561
1562
1563 static int help_cmd(UAContext *ua, char *cmd)
1564 {
1565    unsigned int i;
1566
1567 /* usage(); */
1568    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
1569    for (i=0; i<comsize; i++) {
1570       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
1571    }
1572    bsendmsg(ua, "\n");
1573    return 1;
1574 }
1575
1576 static int version_cmd(UAContext *ua, char *cmd)
1577 {
1578    bsendmsg(ua, "%s Version: " VERSION " (" BDATE ")\n", my_name);
1579    return 1;
1580 }
1581
1582
1583 /* A bit brain damaged in that if the user has not done
1584  * a "use catalog xxx" command, we simply find the first
1585  * catalog resource and open it.
1586  */
1587 int open_db(UAContext *ua)
1588 {
1589    if (ua->db) {
1590       return 1;
1591    }
1592    if (!ua->catalog) {
1593       LockRes();
1594       ua->catalog = (CAT *)GetNextRes(R_CATALOG, NULL);
1595       UnlockRes();
1596       if (!ua->catalog) {    
1597          bsendmsg(ua, _("Could not find a Catalog resource\n"));
1598          return 0;
1599       } else {
1600          bsendmsg(ua, _("Using default Catalog name=%s DB=%s\n"), 
1601             ua->catalog->hdr.name, ua->catalog->db_name);
1602       }
1603    }
1604
1605    ua->jcr->catalog = ua->catalog;
1606
1607    Dmsg0(150, "Open database\n");
1608    ua->db = db_init_database(ua->jcr, ua->catalog->db_name, ua->catalog->db_user,
1609                              ua->catalog->db_password, ua->catalog->db_address,
1610                              ua->catalog->db_port, ua->catalog->db_socket);
1611    if (!db_open_database(ua->jcr, ua->db)) {
1612       bsendmsg(ua, _("Could not open DB %s: ERR=%s"), 
1613          ua->catalog->db_name, db_strerror(ua->db));
1614       close_db(ua);
1615       return 0;
1616    }
1617    ua->jcr->db = ua->db;
1618    Dmsg1(150, "DB %s opened\n", ua->catalog->db_name);
1619    return 1;
1620 }
1621
1622 void close_db(UAContext *ua)
1623 {
1624    if (ua->db) {
1625       db_close_database(ua->jcr, ua->db);
1626    }
1627    ua->db = NULL;
1628 }