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