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