-
Notifications
You must be signed in to change notification settings - Fork 32
/
iadmin.cpp
2892 lines (2600 loc) · 105 KB
/
iadmin.cpp
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
#include <irods/filesystem.hpp>
#include <irods/get_grid_configuration_value.h>
#include <irods/irods_at_scope_exit.hpp>
#include <irods/irods_client_api_table.hpp>
#include <irods/irods_configuration_keywords.hpp>
#include <irods/irods_pack_table.hpp>
#include <irods/irods_string_tokenize.hpp>
#include <irods/key_value_proxy.hpp>
#include <irods/parseCommandLine.h>
#include <irods/query_builder.hpp>
#include <irods/rodsClient.h>
#include <irods/rodsError.h>
#include <irods/rodsErrorTable.h>
#include <irods/rodsLog.h>
#include <irods/set_grid_configuration_value.h>
#include <irods/user.hpp>
#include <irods/user_administration.hpp>
#include <irods/version.hpp>
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
#include <termios.h>
#include <unistd.h>
#include <boost/lexical_cast.hpp>
#include <fmt/compile.h>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#define BIG_STR 3000
int veryVerbose = 0;
char localZone[BIG_STR] = "";
rcComm_t *Conn;
rodsEnv myEnv;
int lastCommandStatus = 0;
void usage( char *subOpt );
namespace {
namespace fs = irods::experimental::filesystem;
using attrs_t = std::vector<std::pair<std::string_view, std::string_view>>;
const attrs_t genquery_attrs = {
{"COLL_ID", COLL_ID_KW}, // not modifiable with iadmin modrepl
{"DATA_CREATE_TIME", DATA_CREATE_KW},
{"DATA_CHECKSUM", CHKSUM_KW},
{"DATA_EXPIRY", DATA_EXPIRY_KW},
{"DATA_ID", DATA_ID_KW}, // not modifiable with iadmin modrepl
{"DATA_REPL_STATUS", REPL_STATUS_KW},
{"DATA_MAP_ID", DATA_MAP_ID_KW}, // not modifiable with iadmin modrepl
{"DATA_MODE", DATA_MODE_KW},
{"DATA_NAME", DATA_NAME_KW}, // not modifiable with iadmin modrepl
{"DATA_OWNER_NAME", DATA_OWNER_KW},
{"DATA_OWNER_ZONE", DATA_OWNER_ZONE_KW},
{"DATA_PATH", FILE_PATH_KW},
{"DATA_REPL_NUM", REPL_NUM_KW},
{"DATA_SIZE", DATA_SIZE_KW},
{"DATA_STATUS", STATUS_STRING_KW},
{"DATA_TYPE_NAME", DATA_TYPE_KW},
{"DATA_VERSION", VERSION_KW},
{"DATA_MODIFY_TIME", DATA_MODIFY_KW},
{"DATA_COMMENTS", DATA_COMMENTS_KW},
//{"DATA_RESC_GROUP_NAME", DATA_RESC_GROUP_NAME_KW}, // missing from genquery since 4.2
{"DATA_RESC_HIER", RESC_HIER_STR_KW}, // not modifiable with iadmin modrepl
{"DATA_RESC_ID", RESC_ID_KW},
{"DATA_RESC_NAME", RESC_NAME_KW} // not modifiable with iadmin modrepl
};
using args_vector_t = std::vector<std::string_view>;
auto get_args_vector(
char** argv,
const std::size_t argc) -> args_vector_t
{
args_vector_t args;
int i = 0;
while (0 != strcmp(argv[i], "")) {
args.push_back(argv[i++]);
}
if (args.size() < argc) {
throw std::invalid_argument("Input arguments do not match expected values.");
}
return args;
} // get_args_vector
using bigint_type = int64_t;
using data_object_option_t = std::variant<bigint_type, fs::path>;
using data_id_t = std::variant_alternative<0, data_object_option_t>::type;
using logical_path_t = std::variant_alternative<1, data_object_option_t>::type;
auto get_data_object_value(
const std::string_view option,
const std::string_view input) -> data_object_option_t
{
data_object_option_t v;
if ("data_id" == option) {
try {
v = boost::lexical_cast<data_id_t>(input.data());
} catch (const boost::bad_lexical_cast&) {
std::stringstream msg;
msg << "Invalid input [" << input << "] for data_id.";
throw std::invalid_argument(msg.str());
}
}
else if ("logical_path" == option) {
const auto logical_path = fs::path{input.data()}.lexically_normal();
if (!logical_path.is_absolute()) {
throw std::invalid_argument("Provided logical_path must be absolute.");
}
v = logical_path;
}
return v;
} // get_data_object_value
using int_type = int;
using repl_option_t = std::variant<int_type, std::string_view>;
using replica_number_t = std::variant_alternative<0, repl_option_t>::type;
using resource_hierarchy_t = std::variant_alternative<1, repl_option_t>::type;
auto get_replica_value(
const std::string_view option,
const std::string_view input) -> repl_option_t
{
repl_option_t v;
if ("replica_number" == option) {
try {
v = boost::lexical_cast<replica_number_t>(input.data());
if (veryVerbose) {
std::cout << __FUNCTION__ << ": cast [" << std::get<replica_number_t>(v) << "]";
std::cout << " from [" << input.data() << "]" << std::endl;
}
} catch (const boost::bad_lexical_cast&) {
std::stringstream msg;
msg << "Invalid input [" << input << "] for replica_number.";
throw std::invalid_argument(msg.str());
}
}
else if ("resource_hierarchy" == option) {
v = input.data();
}
return v;
} // get_replica_value
auto print_grid_config(const char* _namespace, const char* _option_name) -> int
{
try {
namespace adm = irods::experimental::administration;
const auto user_type =
adm::client::type(*Conn, adm::user{Conn->clientUser.userName, Conn->clientUser.rodsZone});
if (!user_type) {
rodsLogError(
LOG_ERROR, CAT_INVALID_USER_TYPE, "Could not determine if user has permission to perform operation.");
return 1;
}
if (*user_type != adm::user_type::rodsadmin) {
rodsLogError(LOG_ERROR, CAT_INSUFFICIENT_PRIVILEGE_LEVEL, "Operation requires rodsadmin level privileges.");
return 1;
}
}
catch (const irods::exception& e) {
rodsLogError(LOG_ERROR, e.code(), e.client_display_what());
return 1;
}
catch (const std::exception& e) {
rodsLogError(LOG_ERROR, SYS_UNKNOWN_ERROR, e.what());
return 1;
}
GridConfigurationInput input{};
// This is the maximum size for the namespace (minus 1 for null terminator).
constexpr auto namespace_max_size = sizeof(input.name_space) - 1;
if (const auto len = strnlen(_namespace, namespace_max_size + 1); 0 == len || len > namespace_max_size) {
std::cerr << fmt::format("Error: namespace must be between 1 and {} characters.\n", namespace_max_size);
return 1;
}
// This is the maximum size for the option name (minus 1 for null terminator).
constexpr auto option_name_max_size = sizeof(input.option_name) - 1;
if (const auto len = strnlen(_option_name, option_name_max_size + 1); 0 == len || len > option_name_max_size) {
std::cerr << fmt::format("Error: option name must be between 1 and {} characters.\n", option_name_max_size);
return 1;
}
std::strncpy(input.name_space, _namespace, namespace_max_size);
std::strncpy(input.option_name, _option_name, option_name_max_size);
GridConfigurationOutput* output{};
irods::at_scope_exit free_output{[&output] { std::free(output); }};
if (const auto ec = rc_get_grid_configuration_value(Conn, &input, &output); ec != 0) {
const auto msg = fmt::format("Failed to get grid configuration for namespace [{}] and option [{}] [ec={}]",
_namespace,
_option_name,
ec);
std::cerr << msg << "\n";
return 1;
}
std::cout << output->option_value << '\n';
return 0;
} // print_grid_config
auto set_grid_config(const char* _namespace, const char* _option_name, const char* _option_value) -> int
{
if (!_namespace || !_option_name || !_option_value) {
std::cerr << "One or more parameters to set_grid_configuration are null.\n";
return 1;
}
try {
namespace adm = irods::experimental::administration;
const auto user_type =
adm::client::type(*Conn, adm::user{Conn->clientUser.userName, Conn->clientUser.rodsZone});
if (!user_type) {
rodsLogError(LOG_ERROR,
CAT_INVALID_USER_TYPE,
"Could not determine if user has permission to set grid configuration values.");
return 1;
}
if (*user_type != adm::user_type::rodsadmin) {
rodsLogError(LOG_ERROR, CAT_INSUFFICIENT_PRIVILEGE_LEVEL, "Operation requires rodsadmin level privileges.");
return 1;
}
}
catch (const irods::exception& e) {
rodsLogError(LOG_ERROR, e.code(), e.client_display_what());
return 1;
}
catch (const std::exception& e) {
rodsLogError(LOG_ERROR, SYS_UNKNOWN_ERROR, e.what());
return 1;
}
GridConfigurationInput input{};
// This is the maximum size for the namespace (minus 1 for null terminator).
constexpr auto namespace_max_size = sizeof(input.name_space) - 1;
if (const auto len = strnlen(_namespace, namespace_max_size + 1); 0 == len || len > namespace_max_size) {
std::cerr << fmt::format("Error: namespace must be between 1 and {} characters.\n", namespace_max_size);
return 1;
}
// This is the maximum size for the option name (minus 1 for null terminator).
constexpr auto option_name_max_size = sizeof(input.option_name) - 1;
if (const auto len = strnlen(_option_name, option_name_max_size + 1); 0 == len || len > option_name_max_size) {
std::cerr << fmt::format("Error: option name must be between 1 and {} characters.\n", option_name_max_size);
return 1;
}
// This is the maximum size for the option value (minus 1 for null terminator).
constexpr auto option_value_max_size = sizeof(input.option_value) - 1;
if (const auto len = strnlen(_option_value, option_value_max_size + 1); 0 == len || len > option_value_max_size) {
std::cerr << fmt::format("Error: option value must be between 1 and {} characters.\n", option_value_max_size);
return 1;
}
std::strncpy(input.name_space, _namespace, namespace_max_size);
std::strncpy(input.option_name, _option_name, option_name_max_size);
std::strncpy(input.option_value, _option_value, option_value_max_size);
if (const auto ec = rc_set_grid_configuration_value(Conn, &input); ec != 0) {
const auto msg = fmt::format("Failed to set grid configuration for namespace [{}] and option [{}] [ec={}]",
_namespace,
_option_name,
ec);
std::cerr << msg << "\n";
return 1;
}
return 0;
} // set_grid_config
auto print_delay_server_info() -> int
{
try {
namespace adm = irods::experimental::administration;
const auto user_type = adm::client::type(*Conn, adm::user{Conn->clientUser.userName, Conn->clientUser.rodsZone});
if (!user_type) {
rodsLogError(LOG_ERROR, CAT_INVALID_USER_TYPE, "Could not determine if user has permission to view information.");
return 1;
}
if (*user_type != adm::user_type::rodsadmin) {
rodsLogError(LOG_ERROR, CAT_INSUFFICIENT_PRIVILEGE_LEVEL, "Operation requires rodsadmin level privileges.");
return 1;
}
}
catch (const irods::exception& e) {
rodsLogError(LOG_ERROR, e.code(), e.client_display_what());
return 1;
}
catch (const std::exception& e) {
rodsLogError(LOG_ERROR, SYS_UNKNOWN_ERROR, e.what());
return 1;
}
//
// At this point, we know the user is a rodsadmin.
//
nlohmann::json delay_server_info;
GridConfigurationInput input{};
std::strcpy(input.name_space, "delay_server");
std::strcpy(input.option_name, "leader");
GridConfigurationOutput* output{};
{
irods::at_scope_exit free_output{[&output] {
std::free(output);
}};
if (const auto ec = rc_get_grid_configuration_value(Conn, &input, &output); ec != 0) {
rodsLogError(LOG_ERROR, ec, "Failed to get delay server information.");
return 1;
}
delay_server_info["leader"] = output->option_value;
}
std::strcpy(input.option_name, "successor");
output = nullptr;
irods::at_scope_exit free_output{[&output] {
std::free(output);
}};
if (const auto ec = rc_get_grid_configuration_value(Conn, &input, &output); ec != 0) {
rodsLogError(LOG_ERROR, ec, "Failed to get delay server information.");
return 1;
}
delay_server_info["successor"] = output->option_value;
std::cout << delay_server_info.dump(4) << '\n';
return 0;
} // print_delay_server_info
auto modify_replica(
char** tokens) -> int
{
const std::vector<std::string_view> genquery_attrs_denylist = {
"COLL_ID",
"DATA_ID",
"DATA_MAP_ID",
"DATA_NAME",
"DATA_RESC_HIER",
"DATA_RESC_NAME"
};
const auto get_attribute_to_modify_from_input{
[&genquery_attrs_denylist](const std::string_view attr) -> std::string_view
{
const auto attr_pair = std::find_if(
std::cbegin(genquery_attrs),
std::cend(genquery_attrs),
[&attr](const auto& a) {
return 0 == attr.compare(a.first);
});
const auto attr_in_genquery_attrs_and_not_in_denylist{
std::cend(genquery_attrs) != attr_pair &&
std::none_of(
std::cbegin(genquery_attrs_denylist),
std::cend(genquery_attrs_denylist),
[&attr](const auto& a) {
return 0 == attr.compare(a);
})
};
if (!attr_in_genquery_attrs_and_not_in_denylist) {
throw std::invalid_argument("Invalid attribute specified.");
}
return attr_pair->second;
}
};
try {
dataObjInfo_t info{};
const auto args = get_args_vector(tokens, 7);
const auto data_object_option = get_data_object_value(args[1], args[2]);
if (const auto id = std::get_if<data_id_t>(&data_object_option)) {
info.dataId = *id;
}
else if (const auto path = std::get_if<logical_path_t>(&data_object_option)) {
rstrcpy(info.objPath, path->string().c_str(), MAX_NAME_LEN);
}
else {
std::cerr << "Invalid data object option specified." << std::endl;
return -2;
}
const auto replica_option = get_replica_value(args[3], args[4]);
if (const auto num = std::get_if<replica_number_t>(&replica_option)) {
info.replNum = *num;
}
else if (const auto hier = std::get_if<resource_hierarchy_t>(&replica_option)) {
rstrcpy(info.rescHier, hier->data(), MAX_NAME_LEN);
}
else {
std::cerr << "Invalid replica option specified." << std::endl;
return -2;
}
const auto key = get_attribute_to_modify_from_input(args[5]);
auto [kvp, lm] = irods::experimental::make_key_value_proxy();
kvp[key.data()] = args[6].data();
kvp[ADMIN_KW] = "";
modDataObjMeta_t inp{};
inp.regParam = kvp.get();
inp.dataObjInfo = &info;
if(const int status = rcModDataObjMeta(Conn, &inp); status) {
char* sub_error_name{};
const char* error_name = rodsErrorName(status, &sub_error_name);
std::cerr << "rcModDataObjMeta failed when modifying replica: [" << error_name << " (" << status << ")]" << std::endl;
printErrorStack(Conn->rError);
return -2;
}
}
catch (const std::exception& e) {
std::cerr << "An error occurred:\n" << e.what() << std::endl;
return -2;
}
return 0;
} // modify_replica
auto ls_replica(
char** tokens) -> int
{
try {
const auto args = get_args_vector(tokens, 5);
std::stringstream q_str;
q_str << "select";
for (auto c = std::begin(genquery_attrs); c != std::end(genquery_attrs); ++c) {
q_str << " " << c->first;
if (std::next(c) != std::end(genquery_attrs)) {
q_str << ",";
}
}
const auto data_object_option = get_data_object_value(args[1], args[2]);
if (const auto id = std::get_if<data_id_t>(&data_object_option)) {
q_str << " where DATA_ID = '" << *id << "'";
}
else if (const auto path = std::get_if<logical_path_t>(&data_object_option)) {
const auto dirname = path->parent_path().string();
q_str << " where COLL_NAME = '" << dirname << "'";
const auto basename = path->object_name().string();
q_str << " and DATA_NAME = '" << basename << "'";
}
else {
std::cerr << "Invalid data object option specified." << std::endl;
return -2;
}
const auto replica_option = get_replica_value(args[3], args[4]);
if (const auto num = std::get_if<replica_number_t>(&replica_option)) {
q_str << " and DATA_REPL_NUM = '" << *num << "'";
}
else if (const auto hier = std::get_if<resource_hierarchy_t>(&replica_option)) {
q_str << " and DATA_RESC_HIER = '" << *hier << "'";
}
else {
std::cerr << "Invalid replica option specified." << std::endl;
return -2;
}
if (veryVerbose) {
std::cout << "query:[" << q_str.str() << "]" << std::endl;
}
auto q = irods::experimental::query_builder{}
.zone_hint(Conn->clientUser.rodsZone)
.build<rcComm_t>(*Conn, q_str.str());
if (std::begin(q) == std::end(q)) {
std::cout << "No results found." << std::endl;
return -3;
}
size_t index = 0;
for (auto&& result : q) {
for (auto&& r : result) {
std::cout << genquery_attrs[index++].first << ": ";
std::cout << r << std::endl;
}
}
}
catch (const std::exception& e) {
std::cerr << "An error occurred:\n" << e.what() << std::endl;
return -2;
}
return 0;
} // ls_replica
constexpr auto is_timestamp_label(std::string_view _label) -> bool
{
constexpr std::array timestamp_labels{"create_ts", "modify_ts"};
return std::any_of(
std::cbegin(timestamp_labels), std::cend(timestamp_labels), [&_label](const auto& _l) { return _l == _label; });
} // is_timestamp_label
auto print_genquery_result(std::string_view _label, std::string_view _value) -> void
{
constexpr const char* fmt_str = "{}: {}\n";
if (!is_timestamp_label(_label)) {
fmt::print(fmt::runtime(fmt_str), _label, _value);
}
else {
char timestamp[TIME_LEN] = "";
if (const int ec = getLocalTimeFromRodsTime(_value.data(), timestamp); 0 == ec) {
fmt::print(fmt::runtime(fmt_str), _label, timestamp);
}
}
} // print_genquery_result
auto print_genquery_results(const std::string_view _query_string, const std::vector<const char*>& _labels) -> void
{
auto q = irods::query(Conn, _query_string.data());
if (q.empty()) {
fmt::print("No rows found\n");
return;
}
for (auto&& result : q) {
if (result.size() != _labels.size()) {
THROW(SYS_INVALID_INPUT_PARAM, "Selected columns and labels must be a one-to-one mapping");
}
for (std::size_t l = 0; l < _labels.size(); ++l) {
print_genquery_result(_labels.at(l), result.at(l));
}
}
} // print_genquery_results
} // anonymous namespace
auto set_local_zone_global() -> int
{
if (!std::string_view{localZone}.empty()) {
return 0;
}
try {
const auto q = irods::query(Conn, "select ZONE_NAME where ZONE_TYPE = 'local'");
if (q.empty()) {
fmt::print(stderr, "Error getting local zone\n");
return 1;
}
const auto& local_zone_name = q.front();
if (local_zone_name.empty()) {
fmt::print(stderr, "Error getting local zone\n");
return 2;
}
std::strncpy(localZone, q.front().at(0).data(), BIG_STR);
}
catch (const irods::exception& e) {
fmt::print(stderr, "Error getting local zone\n{}\n", e.client_display_what());
return 1;
}
catch (...) {
return 1;
}
return 0;
} // set_local_zone_global
auto show_token(const char* _token_namespace = nullptr, const char* _token = nullptr) -> int
{
try {
constexpr const char* default_namespace = "token_namespace";
const char* token_namespace =
!_token_namespace || std::string_view{_token_namespace}.empty() ? default_namespace : _token_namespace;
if (!_token || std::string_view{_token}.empty()) {
const auto list_token_namespaces =
fmt::format(fmt::runtime("select TOKEN_NAME where TOKEN_NAMESPACE = '{}'"), token_namespace);
const auto query_string = fmt::format(fmt::runtime(list_token_namespaces), _token_namespace);
auto q = irods::query(Conn, query_string);
if (q.empty()) {
fmt::print("No rows found");
return 0;
}
for (auto&& result : q) {
fmt::print("{}\n", result.at(0));
}
return 0;
}
constexpr std::array columns{
"TOKEN_NAMESPACE",
"TOKEN_ID",
"TOKEN_NAME",
"TOKEN_VALUE",
"TOKEN_VALUE2",
"TOKEN_VALUE3",
"TOKEN_COMMENT",
"TOKEN_CREATE_TIME",
"TOKEN_MODIFY_TIME"};
static const std::vector labels{
"token_namespace",
"token_id",
"token_name",
"token_value",
"token_value2",
"token_value3",
"r_comment",
"create_ts",
"modify_ts"};
const auto query_string_template = fmt::format(
FMT_COMPILE("select {} where TOKEN_NAMESPACE = '{{}}' and TOKEN_NAME like '{{}}'"),
fmt::join(columns, ", "));
print_genquery_results(fmt::format(fmt::runtime(query_string_template), token_namespace, _token), labels);
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_token
auto show_group(const char* _group = nullptr) -> int
{
try {
if (_group && !std::string_view{_group}.empty()) {
fmt::print("Members of group {}:\n", _group);
constexpr const char* list_group_members =
"select USER_NAME, USER_ZONE where USER_GROUP_NAME = '{}' and USER_TYPE != 'rodsgroup'";
const auto query_string = fmt::format(fmt::runtime(list_group_members), _group);
auto q = irods::query(Conn, query_string);
if (q.empty()) {
fmt::print("No rows found\n");
return 0;
}
for (auto&& result : q) {
const auto& user = result.at(0);
const auto& zone = result.at(1);
fmt::print(fmt::runtime("{}#{}\n"), user, zone);
}
}
else {
constexpr const char* query_string = "select USER_NAME where USER_TYPE = 'rodsgroup'";
auto q = irods::query(Conn, query_string);
if (q.empty()) {
fmt::print("No rows found\n");
return 0;
}
for (auto&& result : q) {
fmt::print("{}\n", result.at(0));
}
}
return 0;
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_group
auto show_resource(const char* _resc = nullptr) -> int
{
try {
if (!_resc || std::string_view{_resc}.empty()) {
auto q = irods::query(Conn, "select RESC_NAME");
if (q.empty()) {
fmt::print("No rows found\n");
return 0;
}
for (auto&& result : q) {
fmt::print(fmt::runtime("{}\n"), result.at(0));
}
return 0;
}
constexpr std::array columns{
"RESC_ID",
"RESC_NAME",
"RESC_ZONE_NAME",
"RESC_TYPE_NAME",
"RESC_LOC",
"RESC_VAULT_PATH",
"RESC_FREE_SPACE",
"RESC_FREE_SPACE_TIME",
"RESC_INFO",
"RESC_COMMENT",
"RESC_STATUS",
"RESC_CREATE_TIME",
"RESC_MODIFY_TIME",
"RESC_CHILDREN",
"RESC_CONTEXT",
"RESC_PARENT",
"RESC_PARENT_CONTEXT"};
static const std::vector labels{
"resc_id",
"resc_name",
"zone_name",
"resc_type_name",
"resc_net",
"resc_def_path",
"free_space",
"free_space_ts",
"resc_info",
"r_comment",
"resc_status",
"create_ts",
"modify_ts",
"resc_children",
"resc_context",
"resc_parent",
"resc_parent_context"};
const auto query_string_template =
fmt::format(FMT_COMPILE("select {} where RESC_NAME = '{{}}'"), fmt::join(columns, ", "));
print_genquery_results(fmt::format(fmt::runtime(query_string_template), _resc), labels);
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_resource
auto show_zone(const char* _zone = nullptr) -> int
{
try {
if (!_zone || std::string_view{_zone}.empty()) {
auto q = irods::query(Conn, "select ZONE_NAME");
if (q.empty()) {
fmt::print("No rows found\n");
return 0;
}
for (auto&& result : q) {
fmt::print(fmt::runtime("{}\n"), result.at(0));
}
return 0;
}
constexpr std::array columns{
"ZONE_ID",
"ZONE_NAME",
"ZONE_TYPE",
"ZONE_CONNECTION",
"ZONE_COMMENT",
"ZONE_CREATE_TIME",
"ZONE_MODIFY_TIME"};
static const std::vector labels{
"zone_id", "zone_name", "zone_type_name", "zone_conn_string", "r_comment", "create_ts", "modify_ts"};
const auto query_string_template =
fmt::format(FMT_COMPILE("select {} where ZONE_NAME = '{{}}'"), fmt::join(columns, ", "));
print_genquery_results(fmt::format(fmt::runtime(query_string_template), _zone), labels);
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_zone
auto show_user(const char* _user, const char* _zone = nullptr) -> int
{
try {
if (!_user || std::string_view{_user}.empty()) {
constexpr const char* query_string = "select USER_NAME, USER_ZONE where USER_TYPE != 'rodsgroup'";
for (auto&& result : irods::query(Conn, query_string)) {
const auto& user = result.at(0);
const auto& zone = result.at(1);
fmt::print(fmt::runtime("{}#{}\n"), user, zone);
}
return 0;
}
constexpr std::array columns{
"USER_ID",
"USER_NAME",
"USER_TYPE",
"USER_ZONE",
"USER_INFO",
"USER_COMMENT",
"USER_CREATE_TIME",
"USER_MODIFY_TIME"};
static const std::vector labels{
"user_id", "user_name", "user_type_name", "zone_name", "user_info", "r_comment", "create_ts", "modify_ts"};
static const auto user_query =
fmt::format(FMT_COMPILE("select {} where USER_NAME = '{{}}'"), fmt::join(columns, ", "));
static const auto user_query_with_zone = fmt::format(
FMT_COMPILE("select {} where USER_NAME = '{{}}' and USER_ZONE = '{{}}'"), fmt::join(columns, ", "));
const auto query_string = _zone && !std::string_view{_zone}.empty()
? fmt::format(fmt::runtime(user_query_with_zone), _user, _zone)
: fmt::format(fmt::runtime(user_query), _user);
print_genquery_results(query_string, labels);
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_user
auto show_user_auth(const char* _user, const char* _zone = nullptr) -> int
{
try {
std::string query_string;
if (!_user || std::string_view{_user}.empty()) {
query_string = "select USER_NAME, USER_DN";
}
else {
constexpr std::array columns{"USER_NAME", "USER_DN"};
static const auto user_query =
fmt::format(FMT_COMPILE("select {} where USER_NAME = '{{}}'"), fmt::join(columns, ", "));
static const auto user_query_with_zone = fmt::format(
FMT_COMPILE("select {} where USER_NAME = '{{}}' and USER_ZONE = '{{}}'"), fmt::join(columns, ", "));
query_string = _zone && !std::string_view{_zone}.empty()
? fmt::format(fmt::runtime(user_query_with_zone), _user, _zone)
: fmt::format(fmt::runtime(user_query), _user);
}
auto q = irods::query(Conn, query_string);
if (q.empty()) {
fmt::print("No rows found\n");
return 0;
}
for (auto&& result : q) {
fmt::print("{} {}\n", result.at(0), result.at(1));
}
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_user_auth
auto show_user_auth_name(const char* _auth_name) -> int
{
if (!_auth_name || std::string_view{_auth_name}.empty()) {
fmt::print("Provided user auth name is empty.\n");
return 1;
}
try {
auto q =
irods::query(Conn, fmt::format(fmt::runtime("select USER_NAME, USER_DN where USER_DN = '{}'"), _auth_name));
if (q.empty()) {
fmt::print("No rows found\n");
return 0;
}
for (auto&& result : q) {
fmt::print("{} {}\n", result.at(0), result.at(1));
}
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_user_auth_name
auto show_global_quotas(const char* _user_or_group = nullptr) -> int
{
try {
constexpr std::array columns{
"QUOTA_USER_NAME", "QUOTA_USER_ZONE", "QUOTA_LIMIT", "QUOTA_OVER", "QUOTA_MODIFY_TIME"};
static const std::vector labels{"user_name", "zone_name", "quota_limit", "quota_over", "modify_ts"};
const auto user_provided = _user_or_group && !std::string_view{_user_or_group}.empty();
if (!user_provided) {
fmt::print("\nGlobal (total usage) quotas (if any) for users/groups:\n");
const auto query_string =
fmt::format(FMT_COMPILE("select {} where QUOTA_RESC_ID = '0'"), fmt::join(columns, ", "));
print_genquery_results(query_string, labels);
return 0;
}
if (const int ec = set_local_zone_global(); ec) {
return ec;
}
char user_name[NAME_LEN]{};
char zone_name[NAME_LEN]{};
if (const int ec = parseUserName(_user_or_group, user_name, zone_name); ec) {
return ec;
}
if (std::string_view{zone_name}.empty()) {
std::strncpy(zone_name, localZone, sizeof(zone_name));
}
fmt::print("\nGlobal (total usage) quotas (if any) for user/group {}:\n", _user_or_group);
const auto query_string_template = fmt::format(
FMT_COMPILE(
"select {} where QUOTA_USER_NAME = '{{}}' and QUOTA_USER_ZONE = '{{}}' and QUOTA_RESC_ID = '0'"),
fmt::join(columns, ", "));
print_genquery_results(fmt::format(fmt::runtime(query_string_template), user_name, zone_name), labels);
}
catch (const irods::exception& e) {
fmt::print(stderr, fmt::runtime(e.client_display_what()));
return 1;
}
catch (...) {
return 1;
}
return 0;
} // show_global_quotas
auto show_resource_quotas(const char* _user_or_group = nullptr) -> int