/
ua_select.c
1599 lines (1467 loc) · 43.2 KB
/
ua_select.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2001-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2014 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* BAREOS Director -- User Agent Prompt and Selection code
*
* Kern Sibbald, October MMI
*/
#include "bareos.h"
#include "dird.h"
/* Imported variables */
extern struct s_jl joblevels[];
/*
* Confirm a retention period
*/
int confirm_retention(UAContext *ua, utime_t *ret, const char *msg)
{
char ed1[100];
int val;
int yes_in_arg = find_arg(ua, NT_("yes"));
for ( ;; ) {
ua->info_msg(_("The current %s retention period is: %s\n"),
msg, edit_utime(*ret, ed1, sizeof(ed1)));
if (yes_in_arg != -1) {
return 1;
}
if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
return 0;
}
if (bstrcasecmp(ua->cmd, _("mod"))) {
if (!get_cmd(ua, _("Enter new retention period: "))) {
return 0;
}
if (!duration_to_utime(ua->cmd, ret)) {
ua->error_msg(_("Invalid period.\n"));
continue;
}
continue;
}
if (is_yesno(ua->cmd, &val)) {
return val; /* is 1 for yes, 0 for no */
}
}
return 1;
}
/*
* Given a list of keywords, find the first one
* that is in the argument list.
* Returns: -1 if not found
* index into list (base 0) on success
*/
int find_arg_keyword(UAContext *ua, const char **list)
{
for (int i = 1; i < ua->argc; i++) {
for(int j=0; list[j]; j++) {
if (bstrcasecmp(list[j], ua->argk[i])) {
return j;
}
}
}
return -1;
}
/*
* Given one keyword, find the first one that
* is in the argument list.
* Returns: argk index (always gt 0)
* -1 if not found
*/
int find_arg(UAContext *ua, const char *keyword)
{
for (int i = 1; i < ua->argc; i++) {
if (bstrcasecmp(keyword, ua->argk[i])) {
return i;
}
}
return -1;
}
/*
* Given a single keyword, find it in the argument list, but
* it must have a value
* Returns: -1 if not found or no value
* list index (base 0) on success
*/
int find_arg_with_value(UAContext *ua, const char *keyword)
{
for (int i = 1; i < ua->argc; i++) {
if (bstrcasecmp(keyword, ua->argk[i])) {
if (ua->argv[i]) {
return i;
} else {
return -1;
}
}
}
return -1;
}
/*
* Given a list of keywords, prompt the user
* to choose one.
*
* Returns: -1 on failure
* index into list (base 0) on success
*/
int do_keyword_prompt(UAContext *ua, const char *msg, const char **list)
{
int i;
start_prompt(ua, _("You have the following choices:\n"));
for (i = 0; list[i]; i++) {
add_prompt(ua, list[i]);
}
return do_prompt(ua, "", msg, NULL, 0);
}
/*
* Select a Storage resource from prompt list
*/
STORERES *select_storage_resource(UAContext *ua, bool autochanger_only)
{
char name[MAX_NAME_LENGTH];
STORERES *store;
if (autochanger_only) {
start_prompt(ua, _("The defined Autochanger Storage resources are:\n"));
} else {
start_prompt(ua, _("The defined Storage resources are:\n"));
}
LockRes();
foreach_res(store, R_STORAGE) {
if (acl_access_ok(ua, Storage_ACL, store->name())) {
if (autochanger_only && !store->autochanger) {
continue;
} else {
add_prompt(ua, store->name());
}
}
}
UnlockRes();
if (do_prompt(ua, _("Storage"), _("Select Storage resource"), name, sizeof(name)) < 0) {
return NULL;
}
store = (STORERES *)GetResWithName(R_STORAGE, name);
return store;
}
/*
* Select a FileSet resource from prompt list
*/
FILESETRES *select_fileset_resource(UAContext *ua)
{
char name[MAX_NAME_LENGTH];
FILESETRES *fs;
start_prompt(ua, _("The defined FileSet resources are:\n"));
LockRes();
foreach_res(fs, R_FILESET) {
if (acl_access_ok(ua, FileSet_ACL, fs->name())) {
add_prompt(ua, fs->name());
}
}
UnlockRes();
if (do_prompt(ua, _("FileSet"), _("Select FileSet resource"), name, sizeof(name)) < 0) {
return NULL;
}
fs = (FILESETRES *)GetResWithName(R_FILESET, name);
return fs;
}
/*
* Get a catalog resource from prompt list
*/
CATRES *get_catalog_resource(UAContext *ua)
{
char name[MAX_NAME_LENGTH];
CATRES *catalog = NULL;
int i;
for (i = 1; i < ua->argc; i++) {
if (bstrcasecmp(ua->argk[i], NT_("catalog")) && ua->argv[i]) {
if (acl_access_ok(ua, Catalog_ACL, ua->argv[i])) {
catalog = (CATRES *)GetResWithName(R_CATALOG, ua->argv[i]);
break;
}
}
}
if (ua->gui && !catalog) {
LockRes();
catalog = (CATRES *)GetNextRes(R_CATALOG, NULL);
UnlockRes();
if (!catalog) {
ua->error_msg(_("Could not find a Catalog resource\n"));
return NULL;
} else if (!acl_access_ok(ua, Catalog_ACL, catalog->name())) {
ua->error_msg(_("You must specify a \"use <catalog-name>\" command before continuing.\n"));
return NULL;
}
return catalog;
}
if (!catalog) {
start_prompt(ua, _("The defined Catalog resources are:\n"));
LockRes();
foreach_res(catalog, R_CATALOG) {
if (acl_access_ok(ua, Catalog_ACL, catalog->name())) {
add_prompt(ua, catalog->name());
}
}
UnlockRes();
if (do_prompt(ua, _("Catalog"), _("Select Catalog resource"), name, sizeof(name)) < 0) {
return NULL;
}
catalog = (CATRES *)GetResWithName(R_CATALOG, name);
}
return catalog;
}
/*
* Select a job to enable or disable
*/
JOBRES *select_enable_disable_job_resource(UAContext *ua, bool enable)
{
char name[MAX_NAME_LENGTH];
JOBRES *job;
LockRes();
start_prompt(ua, _("The defined Job resources are:\n"));
foreach_res(job, R_JOB) {
if (!acl_access_ok(ua, Job_ACL, job->name())) {
continue;
}
if (job->enabled == enable) { /* Already enabled/disabled? */
continue; /* yes, skip */
}
add_prompt(ua, job->name());
}
UnlockRes();
if (do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name)) < 0) {
return NULL;
}
job = (JOBRES *)GetResWithName(R_JOB, name);
return job;
}
/*
* Select a Job resource from prompt list
*/
JOBRES *select_job_resource(UAContext *ua)
{
char name[MAX_NAME_LENGTH];
JOBRES *job;
start_prompt(ua, _("The defined Job resources are:\n"));
LockRes();
foreach_res(job, R_JOB) {
if (acl_access_ok(ua, Job_ACL, job->name())) {
add_prompt(ua, job->name());
}
}
UnlockRes();
if (do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name)) < 0) {
return NULL;
}
job = (JOBRES *)GetResWithName(R_JOB, name);
return job;
}
/*
* Select a Restore Job resource from argument or prompt
*/
JOBRES *get_restore_job(UAContext *ua)
{
JOBRES *job;
int i = find_arg_with_value(ua, NT_("restorejob"));
if (i >= 0 && acl_access_ok(ua, Job_ACL, ua->argv[i])) {
job = (JOBRES *)GetResWithName(R_JOB, ua->argv[i]);
if (job && job->JobType == JT_RESTORE) {
return job;
}
ua->error_msg(_("Error: Restore Job resource \"%s\" does not exist.\n"), ua->argv[i]);
}
return select_restore_job_resource(ua);
}
/*
* Select a Restore Job resource from prompt list
*/
JOBRES *select_restore_job_resource(UAContext *ua)
{
char name[MAX_NAME_LENGTH];
JOBRES *job;
start_prompt(ua, _("The defined Restore Job resources are:\n"));
LockRes();
foreach_res(job, R_JOB) {
if (job->JobType == JT_RESTORE && acl_access_ok(ua, Job_ACL, job->name())) {
add_prompt(ua, job->name());
}
}
UnlockRes();
if (do_prompt(ua, _("Job"), _("Select Restore Job"), name, sizeof(name)) < 0) {
return NULL;
}
job = (JOBRES *)GetResWithName(R_JOB, name);
return job;
}
/*
* Select a client resource from prompt list
*/
CLIENTRES *select_client_resource(UAContext *ua)
{
char name[MAX_NAME_LENGTH];
CLIENTRES *client;
start_prompt(ua, _("The defined Client resources are:\n"));
LockRes();
foreach_res(client, R_CLIENT) {
if (acl_access_ok(ua, Client_ACL, client->name())) {
add_prompt(ua, client->name());
}
}
UnlockRes();
if (do_prompt(ua, _("Client"), _("Select Client (File daemon) resource"), name, sizeof(name)) < 0) {
return NULL;
}
client = (CLIENTRES *)GetResWithName(R_CLIENT, name);
return client;
}
/*
* Select a client to enable or disable
*/
CLIENTRES *select_enable_disable_client_resource(UAContext *ua, bool enable)
{
char name[MAX_NAME_LENGTH];
CLIENTRES *client;
LockRes();
start_prompt(ua, _("The defined Client resources are:\n"));
foreach_res(client, R_CLIENT) {
if (!acl_access_ok(ua, Client_ACL, client->name())) {
continue;
}
if (client->enabled == enable) { /* Already enabled/disabled? */
continue; /* yes, skip */
}
add_prompt(ua, client->name());
}
UnlockRes();
if (do_prompt(ua, _("Client"), _("Select Client resource"), name, sizeof(name)) < 0) {
return NULL;
}
client = (CLIENTRES *)GetResWithName(R_CLIENT, name);
return client;
}
/*
* Get client resource, start by looking for
* client=<client-name>
* if we don't find the keyword, we prompt the user.
*/
CLIENTRES *get_client_resource(UAContext *ua)
{
CLIENTRES *client = NULL;
int i;
for (i = 1; i < ua->argc; i++) {
if ((bstrcasecmp(ua->argk[i], NT_("client")) ||
bstrcasecmp(ua->argk[i], NT_("fd"))) && ua->argv[i]) {
if (!acl_access_ok(ua, Client_ACL, ua->argv[i])) {
break;
}
client = (CLIENTRES *)GetResWithName(R_CLIENT, ua->argv[i]);
if (client) {
return client;
}
ua->error_msg(_("Error: Client resource %s does not exist.\n"), ua->argv[i]);
break;
}
}
return select_client_resource(ua);
}
/*
* Select a schedule to enable or disable
*/
SCHEDRES *select_enable_disable_schedule_resource(UAContext *ua, bool enable)
{
char name[MAX_NAME_LENGTH];
SCHEDRES *sched;
LockRes();
start_prompt(ua, _("The defined Schedule resources are:\n"));
foreach_res(sched, R_SCHEDULE) {
if (!acl_access_ok(ua, Schedule_ACL, sched->name())) {
continue;
}
if (sched->enabled == enable) { /* Already enabled/disabled? */
continue; /* yes, skip */
}
add_prompt(ua, sched->name());
}
UnlockRes();
if (do_prompt(ua, _("Schedule"), _("Select Schedule resource"), name, sizeof(name)) < 0) {
return NULL;
}
sched = (SCHEDRES *)GetResWithName(R_SCHEDULE, name);
return sched;
}
/* Scan what the user has entered looking for:
*
* client=<client-name>
*
* if error or not found, put up a list of client DBRs
* to choose from.
*
* returns: 0 on error
* 1 on success and fills in CLIENT_DBR
*/
bool get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
{
int i;
if (cr->Name[0]) { /* If name already supplied */
if (db_get_client_record(ua->jcr, ua->db, cr)) {
return 1;
}
ua->error_msg(_("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
}
for (i = 1; i < ua->argc; i++) {
if ((bstrcasecmp(ua->argk[i], NT_("client")) ||
bstrcasecmp(ua->argk[i], NT_("fd"))) && ua->argv[i]) {
if (!acl_access_ok(ua, Client_ACL, ua->argv[i])) {
break;
}
bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
if (!db_get_client_record(ua->jcr, ua->db, cr)) {
ua->error_msg(_("Could not find Client \"%s\": ERR=%s"), ua->argv[i],
db_strerror(ua->db));
cr->ClientId = 0;
break;
}
return 1;
}
}
if (!select_client_dbr(ua, cr)) { /* try once more by proposing a list */
return 0;
}
return 1;
}
/*
* Select a Client record from the catalog
* Returns 1 on success
* 0 on failure
*/
bool select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
{
CLIENT_DBR ocr;
char name[MAX_NAME_LENGTH];
int num_clients, i;
uint32_t *ids;
cr->ClientId = 0;
if (!db_get_client_ids(ua->jcr, ua->db, &num_clients, &ids)) {
ua->error_msg(_("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
return 0;
}
if (num_clients <= 0) {
ua->error_msg(_("No clients defined. You must run a job before using this command.\n"));
return 0;
}
start_prompt(ua, _("Defined Clients:\n"));
for (i = 0; i < num_clients; i++) {
ocr.ClientId = ids[i];
if (!db_get_client_record(ua->jcr, ua->db, &ocr) ||
!acl_access_ok(ua, Client_ACL, ocr.Name)) {
continue;
}
add_prompt(ua, ocr.Name);
}
free(ids);
if (do_prompt(ua, _("Client"), _("Select the Client"), name, sizeof(name)) < 0) {
return 0;
}
memset(&ocr, 0, sizeof(ocr));
bstrncpy(ocr.Name, name, sizeof(ocr.Name));
if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
ua->error_msg(_("Could not find Client \"%s\": ERR=%s"), name, db_strerror(ua->db));
return 0;
}
memcpy(cr, &ocr, sizeof(ocr));
return 1;
}
/* Scan what the user has entered looking for:
*
* argk=<pool-name>
*
* where argk can be : pool, recyclepool, scratchpool, nextpool etc..
*
* if error or not found, put up a list of pool DBRs
* to choose from.
*
* returns: false on error
* true on success and fills in POOL_DBR
*/
bool get_pool_dbr(UAContext *ua, POOL_DBR *pr, const char *argk)
{
if (pr->Name[0]) { /* If name already supplied */
if (db_get_pool_record(ua->jcr, ua->db, pr) &&
acl_access_ok(ua, Pool_ACL, pr->Name)) {
return true;
}
ua->error_msg(_("Could not find Pool \"%s\": ERR=%s"), pr->Name, db_strerror(ua->db));
}
if (!select_pool_dbr(ua, pr, argk)) { /* try once more */
return false;
}
return true;
}
/*
* Select a Pool record from catalog
* argk can be pool, recyclepool, scratchpool etc..
*/
bool select_pool_dbr(UAContext *ua, POOL_DBR *pr, const char *argk)
{
POOL_DBR opr;
char name[MAX_NAME_LENGTH];
int num_pools, i;
uint32_t *ids;
for (i = 1; i < ua->argc; i++) {
if (bstrcasecmp(ua->argk[i], argk) && ua->argv[i] &&
acl_access_ok(ua, Pool_ACL, ua->argv[i])) {
bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
ua->error_msg(_("Could not find Pool \"%s\": ERR=%s"), ua->argv[i], db_strerror(ua->db));
pr->PoolId = 0;
break;
}
return true;
}
}
pr->PoolId = 0;
if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
ua->error_msg(_("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
return 0;
}
if (num_pools <= 0) {
ua->error_msg(_("No pools defined. Use the \"create\" command to create one.\n"));
return false;
}
start_prompt(ua, _("Defined Pools:\n"));
if (bstrcmp(argk, NT_("recyclepool"))) {
add_prompt(ua, _("*None*"));
}
for (i = 0; i < num_pools; i++) {
opr.PoolId = ids[i];
if (!db_get_pool_record(ua->jcr, ua->db, &opr) ||
!acl_access_ok(ua, Pool_ACL, opr.Name)) {
continue;
}
add_prompt(ua, opr.Name);
}
free(ids);
if (do_prompt(ua, _("Pool"), _("Select the Pool"), name, sizeof(name)) < 0) {
return false;
}
memset(&opr, 0, sizeof(opr));
/* *None* is only returned when selecting a recyclepool, and in that case
* the calling code is only interested in opr.Name, so then we can leave
* pr as all zero.
*/
if (!bstrcmp(name, _("*None*"))) {
bstrncpy(opr.Name, name, sizeof(opr.Name));
if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
ua->error_msg(_("Could not find Pool \"%s\": ERR=%s"), name, db_strerror(ua->db));
return false;
}
}
memcpy(pr, &opr, sizeof(opr));
return true;
}
/*
* Select a Pool and a Media (Volume) record from the database
*/
int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
{
if (!select_media_dbr(ua, mr)) {
return 0;
}
memset(pr, 0, sizeof(POOL_DBR));
pr->PoolId = mr->PoolId;
if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
ua->error_msg("%s", db_strerror(ua->db));
return 0;
}
if (!acl_access_ok(ua, Pool_ACL, pr->Name, true)) {
ua->error_msg(_("No access to Pool \"%s\"\n"), pr->Name);
return 0;
}
return 1;
}
/*
* Select a Media (Volume) record from the database
*/
int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
{
int i;
int ret = 0;
POOLMEM *err = get_pool_memory(PM_FNAME);
*err=0;
memset(mr, 0, sizeof(MEDIA_DBR));
i = find_arg_with_value(ua, NT_("volume"));
if (i >= 0) {
if (is_name_valid(ua->argv[i], &err)) {
bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
} else {
goto bail_out;
}
}
if (mr->VolumeName[0] == 0) {
POOL_DBR pr;
memset(&pr, 0, sizeof(pr));
/*
* Get the pool from pool=<pool-name>
*/
if (!get_pool_dbr(ua, &pr)) {
goto bail_out;
}
mr->PoolId = pr.PoolId;
db_list_media_records(ua->jcr, ua->db, mr, printit, ua, HORZ_LIST);
if (!get_cmd(ua, _("Enter *MediaId or Volume name: "))) {
goto bail_out;
}
if (ua->cmd[0] == '*' && is_a_number(ua->cmd+1)) {
mr->MediaId = str_to_int64(ua->cmd+1);
} else if (is_name_valid(ua->cmd, &err)) {
bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
} else {
goto bail_out;
}
}
if (!db_get_media_record(ua->jcr, ua->db, mr)) {
pm_strcpy(err, db_strerror(ua->db));
goto bail_out;
}
ret = 1;
bail_out:
if (!ret && *err) {
ua->error_msg("%s", err);
}
free_pool_memory(err);
return ret;
}
/*
* Select a pool resource from prompt list
*/
POOLRES *select_pool_resource(UAContext *ua)
{
char name[MAX_NAME_LENGTH];
POOLRES *pool;
start_prompt(ua, _("The defined Pool resources are:\n"));
LockRes();
foreach_res(pool, R_POOL) {
if (acl_access_ok(ua, Pool_ACL, pool->name())) {
add_prompt(ua, pool->name());
}
}
UnlockRes();
if (do_prompt(ua, _("Pool"), _("Select Pool resource"), name, sizeof(name)) < 0) {
return NULL;
}
pool = (POOLRES *)GetResWithName(R_POOL, name);
return pool;
}
/*
* If you are thinking about using it, you
* probably want to use select_pool_dbr()
* or get_pool_dbr() above.
*/
POOLRES *get_pool_resource(UAContext *ua)
{
POOLRES *pool = NULL;
int i;
i = find_arg_with_value(ua, NT_("pool"));
if (i >= 0 && acl_access_ok(ua, Pool_ACL, ua->argv[i])) {
pool = (POOLRES *)GetResWithName(R_POOL, ua->argv[i]);
if (pool) {
return pool;
}
ua->error_msg(_("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
}
return select_pool_resource(ua);
}
/*
* List all jobs and ask user to select one
*/
int select_job_dbr(UAContext *ua, JOB_DBR *jr)
{
db_list_job_records(ua->jcr, ua->db, jr, printit, ua, HORZ_LIST);
if (!get_pint(ua, _("Enter the JobId to select: "))) {
return 0;
}
jr->JobId = ua->int64_val;
if (!db_get_job_record(ua->jcr, ua->db, jr)) {
ua->error_msg("%s", db_strerror(ua->db));
return 0;
}
return jr->JobId;
}
/*
* Scan what the user has entered looking for:
*
* jobid=nn
*
* if error or not found, put up a list of Jobs
* to choose from.
*
* returns: 0 on error
* JobId on success and fills in JOB_DBR
*/
int get_job_dbr(UAContext *ua, JOB_DBR *jr)
{
int i;
for (i = 1; i < ua->argc; i++) {
if (bstrcasecmp(ua->argk[i], NT_("ujobid")) && ua->argv[i]) {
jr->JobId = 0;
bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
} else if (bstrcasecmp(ua->argk[i], NT_("jobid")) && ua->argv[i]) {
jr->JobId = str_to_int64(ua->argv[i]);
jr->Job[0] = 0;
} else {
continue;
}
if (!db_get_job_record(ua->jcr, ua->db, jr)) {
ua->error_msg(_("Could not find Job \"%s\": ERR=%s"), ua->argv[i],
db_strerror(ua->db));
jr->JobId = 0;
break;
}
return jr->JobId;
}
jr->JobId = 0;
jr->Job[0] = 0;
for (i = 1; i < ua->argc; i++) {
if ((bstrcasecmp(ua->argk[i], NT_("jobname")) ||
bstrcasecmp(ua->argk[i], NT_("job"))) && ua->argv[i]) {
jr->JobId = 0;
bstrncpy(jr->Name, ua->argv[i], sizeof(jr->Name));
break;
}
}
if (!select_job_dbr(ua, jr)) { /* try once more */
return 0;
}
return jr->JobId;
}
/*
* Implement unique set of prompts
*/
void start_prompt(UAContext *ua, const char *msg)
{
if (ua->max_prompts == 0) {
ua->max_prompts = 10;
ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
}
ua->num_prompts = 1;
ua->prompt[0] = bstrdup(msg);
}
/*
* Add to prompts -- keeping them unique
*/
void add_prompt(UAContext *ua, const char *prompt)
{
int i;
if (ua->num_prompts == ua->max_prompts) {
ua->max_prompts *= 2;
ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
ua->max_prompts);
}
for (i = 1; i < ua->num_prompts; i++) {
if (bstrcmp(ua->prompt[i], prompt)) {
return;
}
}
ua->prompt[ua->num_prompts++] = bstrdup(prompt);
}
/*
* Display prompts and get user's choice
*
* Returns: -1 on error
* index base 0 on success, and choice is copied to prompt if not NULL
* prompt is set to the chosen prompt item string
*/
int do_prompt(UAContext *ua, const char *automsg, const char *msg,
char *prompt, int max_prompt)
{
int i, item;
POOL_MEM pmsg(PM_MESSAGE);
BSOCK *user = ua->UA_sock;
if (prompt) {
*prompt = 0;
}
if (ua->num_prompts == 2) {
item = 1;
if (prompt) {
bstrncpy(prompt, ua->prompt[1], max_prompt);
}
ua->send_msg(_("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
goto done;
}
/* If running non-interactive, bail out */
if (ua->batch) {
/* First print the choices he wanted to make */
ua->send_msg(ua->prompt[0]);
for (i = 1; i < ua->num_prompts; i++) {
ua->send_msg("%6d: %s\n", i, ua->prompt[i]);
}
/* Now print error message */
ua->send_msg(_("Your request has multiple choices for \"%s\". Selection is not possible in batch mode.\n"), automsg);
item = -1;
goto done;
}
if (ua->api) user->signal(BNET_START_SELECT);
ua->send_msg(ua->prompt[0]);
for (i = 1; i < ua->num_prompts; i++) {
if (ua->api) {
ua->send_msg("%s", ua->prompt[i]);
} else {
ua->send_msg("%6d: %s\n", i, ua->prompt[i]);
}
}
if (ua->api) user->signal(BNET_END_SELECT);
for ( ;; ) {
/* First item is the prompt string, not the items */
if (ua->num_prompts == 1) {
ua->error_msg(_("Selection list for \"%s\" is empty!\n"), automsg);
item = -1; /* list is empty ! */
break;
}
if (ua->num_prompts == 2) {
item = 1;
ua->send_msg(_("Automatically selected: %s\n"), ua->prompt[1]);
if (prompt) {
bstrncpy(prompt, ua->prompt[1], max_prompt);
}
break;
} else {
Mmsg(pmsg, "%s (1-%d): ", msg, ua->num_prompts - 1);
}
/* Either a . or an @ will get you out of the loop */
if (ua->api) user->signal(BNET_SELECT_INPUT);
if (!get_pint(ua, pmsg.c_str())) {
item = -1; /* error */
ua->info_msg(_("Selection aborted, nothing done.\n"));
break;
}
item = ua->pint32_val;
if (item < 1 || item >= ua->num_prompts) {
ua->warning_msg(_("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
continue;
}
if (prompt) {
bstrncpy(prompt, ua->prompt[item], max_prompt);
}
break;
}
done:
for (i = 0; i < ua->num_prompts; i++) {
free(ua->prompt[i]);
}
ua->num_prompts = 0;
return item>0 ? item-1 : item;
}
/*
* We scan what the user has entered looking for :
* - storage=<storage-resource>
* - job=<job_name>
* - jobid=<jobid>
* - ? (prompt him with storage list)
* - <some-error> (prompt him with storage list)
*
* If use_default is set, we assume that any keyword without a value
* is the name of the Storage resource wanted.
*
* If autochangers_only is given, we limit the output to autochangers only.
*/
STORERES *get_storage_resource(UAContext *ua, bool use_default, bool autochangers_only)
{
int i;
JCR *jcr;
int jobid;
char ed1[50];
char *store_name = NULL;
STORERES *store = NULL;
Dmsg1(100, "get_storage_resource: autochangers_only is %d\n", autochangers_only);
for (i = 1; i < ua->argc; i++) {
/*
* Ignore any zapped keyword.
*/
if (*ua->argk[i] == 0) {
continue;
}
if (use_default && !ua->argv[i]) {
/*
* Ignore barcode, barcodes, encrypt, scan and slots keywords.
*/
if (bstrcasecmp("barcode", ua->argk[i]) ||
bstrcasecmp("barcodes", ua->argk[i]) ||
bstrcasecmp("encrypt", ua->argk[i]) ||
bstrcasecmp("scan", ua->argk[i]) ||
bstrcasecmp("slots", ua->argk[i])) {
continue;
}
/*
* Default argument is storage
*/
if (store_name) {
ua->error_msg(_("Storage name given twice.\n"));
return NULL;
}
store_name = ua->argk[i];
if (*store_name == '?') {
*store_name = 0;
break;
}
} else {
if (bstrcasecmp(ua->argk[i], NT_("storage")) ||
bstrcasecmp(ua->argk[i], NT_("sd"))) {
store_name = ua->argv[i];
break;