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