-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.cpp
1624 lines (1384 loc) · 70.6 KB
/
handler.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 "handler.hpp"
#include "logical_quotas_error.hpp"
#include "utilities.hpp"
#include <irods/client_connection.hpp>
#include <irods/execCmd.h>
#include <irods/irods_re_plugin.hpp>
#include <irods/irods_state_table.h>
#include <irods/msParam.h>
#include <irods/genQuery.h>
#include <irods/objDesc.hpp>
#include <irods/rodsDef.h>
#include <irods/irods_query.hpp>
#include <irods/irods_logger.hpp>
#include <irods/query_builder.hpp>
#include <irods/irods_get_l1desc.hpp>
#include <irods/modAVUMetadata.h>
#include <irods/rodsErrorTable.h>
#include <irods/replica.hpp>
#include <irods/scoped_client_identity.hpp>
#include <irods/scoped_permission.hpp>
#include <irods/filesystem.hpp>
#define IRODS_FILESYSTEM_ENABLE_SERVER_SIDE_API
#include <irods/filesystem.hpp>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <string_view>
#include <vector>
#include <tuple>
#include <functional>
#include <stdexcept>
#include <algorithm>
namespace
{
// clang-format off
namespace fs = irods::experimental::filesystem;
namespace log = irods::experimental::log;
using size_type = irods::handler::size_type;
using quotas_info_type = std::unordered_map<std::string, size_type>;
using file_position_map_type = std::unordered_map<std::string, irods::handler::file_position_type>;
// clang-format on
//
// Classes
//
class parent_path
{
public:
explicit parent_path(const fs::path& _p)
: p_{_p}
{
}
parent_path(const parent_path&) = delete;
auto operator=(const parent_path&) -> parent_path& = delete;
auto of(const fs::path& _child) -> bool
{
if (p_ == _child) {
return false;
}
auto p_iter = std::begin(p_);
auto p_last = std::end(p_);
auto c_iter = std::begin(_child);
auto c_last = std::end(_child);
for (; p_iter != p_last && c_iter != c_last && *p_iter == *c_iter; ++p_iter, ++c_iter);
return (p_iter == p_last);
}
private:
const fs::path& p_;
}; // class parent_path
//
// Function Prototypes
//
auto get_collection_username(rsComm_t& _conn, fs::path _p) -> std::optional<std::string>;
auto get_monitored_collection_info(rsComm_t& _conn,
const irods::attributes& _attrs,
const fs::path& _p) -> quotas_info_type;
auto throw_if_maximum_number_of_data_objects_violation(const irods::attributes& _attrs,
const quotas_info_type& _tracking_info,
size_type _delta) -> void;
auto throw_if_maximum_size_in_bytes_violation(const irods::attributes& _attrs,
const quotas_info_type& _tracking_info,
size_type _delta) -> void;
auto is_monitored_collection(rsComm_t& _conn,
const irods::attributes& _attrs,
const fs::path& _p) -> bool;
auto get_monitored_parent_collection(rsComm_t& _conn,
const irods::attributes& _attrs,
fs::path _p) -> std::optional<fs::path>;
auto compute_data_object_count_and_size(rsComm_t& _conn, fs::path _p) -> std::tuple<size_type, size_type>;
auto update_data_object_count_and_size(rsComm_t& _conn,
const irods::attributes& _attrs,
const fs::path& _collection,
const quotas_info_type& _info,
size_type _data_objects_delta,
size_type _size_in_bytes_delta) -> void;
auto unset_metadata_impl(const std::string& _instance_name,
std::list<boost::any>& _rule_arguments,
irods::callback& _effect_handler,
std::unordered_map<std::string, irods::instance_configuration>& _instance_configs,
std::function<std::vector<const std::string*> (const irods::attributes& _attrs)> _func) -> irods::error;
template <typename T>
auto get_pointer(std::list<boost::any>& _rule_arguments, int _index = 2) -> T*;
template <typename Function>
auto for_each_monitored_collection(rsComm_t& _conn,
const irods::attributes& _attrs,
fs::path _collection,
Function _func) -> void;
template <typename Value, typename Map>
auto get_attribute_value(const Map& _map, std::string_view _key) -> Value;
auto get_instance_config(const irods::instance_configuration_map& _map,
std::string_view _key) -> const irods::instance_configuration&;
auto make_unique_id(fs::path _p) -> std::string;
auto throw_if_string_cannot_be_cast_to_an_integer(const std::string& s, const std::string& error_msg) -> void;
auto is_group(rsComm_t& _conn, const std::string_view _entity_name) -> bool;
auto log_logical_quotas_exception(const irods::logical_quotas_error& e, irods::callback& _effect_handler) -> irods::error;
auto log_irods_exception(const irods::exception& e, irods::callback& _effect_handler) -> irods::error;
auto log_exception(const std::exception& e, irods::callback& _effect_handler) -> irods::error;
//
// Function Implementations
//
auto get_collection_username(rsComm_t& _conn, fs::path _p) -> std::optional<std::string>
{
const auto gql = fmt::format("select COLL_OWNER_NAME where COLL_NAME = '{}'", _p.c_str());
for (auto&& row : irods::query{&_conn, gql}) {
return row[0];
}
return std::nullopt;
}
auto get_monitored_collection_info(rsComm_t& _conn, const irods::attributes& _attrs, const fs::path& _p) -> quotas_info_type
{
quotas_info_type info;
const auto gql = fmt::format("select META_COLL_ATTR_NAME, META_COLL_ATTR_VALUE where COLL_NAME = '{}'", _p.c_str());
for (auto&& row : irods::query{&_conn, gql}) {
// clang-format off
if (_attrs.maximum_number_of_data_objects() == row[0]) { info[_attrs.maximum_number_of_data_objects()] = std::stoll(row[1]); }
else if (_attrs.maximum_size_in_bytes() == row[0]) { info[_attrs.maximum_size_in_bytes()] = std::stoll(row[1]); }
else if (_attrs.total_number_of_data_objects() == row[0]) { info[_attrs.total_number_of_data_objects()] = std::stoll(row[1]); }
else if (_attrs.total_size_in_bytes() == row[0]) { info[_attrs.total_size_in_bytes()] = std::stoll(row[1]); }
// clang-format on
}
return info;
}
auto throw_if_maximum_number_of_data_objects_violation(const irods::attributes& _attrs,
const quotas_info_type& _tracking_info,
size_type _delta) -> void
{
const auto& max_attr_name = _attrs.maximum_number_of_data_objects();
if (const auto iter = _tracking_info.find(max_attr_name); iter != std::end(_tracking_info)) {
const auto total = get_attribute_value<size_type>(_tracking_info, _attrs.total_number_of_data_objects());
if (total + _delta > iter->second) {
throw irods::logical_quotas_error{"Logical Quotas Policy Violation: Adding object exceeds maximum number of objects limit",
SYS_NOT_ALLOWED};
}
}
}
auto throw_if_maximum_size_in_bytes_violation(const irods::attributes& _attrs,
const quotas_info_type& _tracking_info,
size_type _delta) -> void
{
const auto& max_attr_name = _attrs.maximum_size_in_bytes();
if (const auto iter = _tracking_info.find(max_attr_name); iter != std::end(_tracking_info)) {
const auto total = get_attribute_value<size_type>(_tracking_info, _attrs.total_size_in_bytes());
if (total + _delta > iter->second) {
throw irods::logical_quotas_error{"Logical Quotas Policy Violation: Adding object exceeds maximum data size in bytes limit",
SYS_NOT_ALLOWED};
}
}
}
auto is_monitored_collection(rsComm_t& _conn, const irods::attributes& _attrs, const fs::path& _p) -> bool
{
const auto gql = fmt::format("select META_COLL_ATTR_NAME where COLL_NAME = '{}' and META_COLL_ATTR_NAME = '{}' || = '{}'",
_p.c_str(),
_attrs.total_number_of_data_objects(),
_attrs.total_size_in_bytes());
for (auto&& row : irods::query{&_conn, gql}) {
return true;
}
return false;
}
auto get_monitored_parent_collection(rsComm_t& _conn, const irods::attributes& _attrs, fs::path _p) -> std::optional<fs::path>
{
for (; !_p.empty(); _p = _p.parent_path()) {
if (is_monitored_collection(_conn, _attrs, _p)) {
return _p;
}
else if ("/" == _p) {
break;
}
}
return std::nullopt;
}
auto compute_data_object_count_and_size(rsComm_t& _conn, fs::path _p) -> std::tuple<size_type, size_type>
{
size_type objects = 0;
size_type bytes = 0;
const auto gql = fmt::format("select count(DATA_NAME), sum(DATA_SIZE) where COLL_NAME = '{0}' || like '{0}/%'", _p.c_str());
for (auto&& row : irods::query{&_conn, gql}) {
objects = !row[0].empty() ? std::stoll(row[0]) : 0;
bytes = !row[1].empty() ? std::stoll(row[1]) : 0;
}
return {objects, bytes};
}
auto update_data_object_count_and_size(rsComm_t& _conn,
const irods::attributes& _attrs,
const fs::path& _collection,
const quotas_info_type& _info,
size_type _data_objects_delta,
size_type _size_in_bytes_delta) -> void
{
if (0 != _data_objects_delta) {
const auto& objects_attr = _attrs.total_number_of_data_objects();
if (const auto iter = _info.find(objects_attr); std::end(_info) != iter) {
const auto new_object_count = std::to_string(iter->second + _data_objects_delta);
fs::server::set_metadata(_conn, _collection, {objects_attr, new_object_count});
}
}
if (0 != _size_in_bytes_delta) {
const auto& size_attr = _attrs.total_size_in_bytes();
if (const auto iter = _info.find(size_attr); std::end(_info) != iter) {
const auto new_size_in_bytes = std::to_string(iter->second + _size_in_bytes_delta);
fs::server::set_metadata(_conn, _collection, {size_attr, new_size_in_bytes});
}
}
}
auto unset_metadata_impl(const std::string& _instance_name,
const irods::instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
irods::callback& _effect_handler,
std::function<std::vector<const std::string*> (const irods::attributes& _attrs)> _func) -> irods::error
{
try {
auto args_iter = std::begin(_rule_arguments);
const auto& path = *boost::any_cast<std::string*>(*args_iter);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
const auto info = get_monitored_collection_info(conn, attrs, path);
irods::experimental::client_connection client_conn;
for (auto&& attribute_name : _func(attrs)) {
if (const auto iter = info.find(*attribute_name); iter != std::end(info)) {
const auto value = get_attribute_value<size_type>(info, *attribute_name);
fs::client::remove_metadata(fs::admin, client_conn, path, {*attribute_name, std::to_string(value)});
}
}
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return SUCCESS();
}
template <typename T>
auto get_pointer(std::list<boost::any>& _rule_arguments, int _index) -> T*
{
return boost::any_cast<T*>(*std::next(std::begin(_rule_arguments), _index));
}
template <typename Function>
auto for_each_monitored_collection(rsComm_t& _conn,
const irods::attributes& _attrs,
fs::path _collection,
Function _func) -> void
{
for (auto collection = get_monitored_parent_collection(_conn, _attrs, _collection.parent_path());
collection;
collection = get_monitored_parent_collection(_conn, _attrs, collection->parent_path()))
{
auto info = get_monitored_collection_info(_conn, _attrs, *collection);
_func(*collection, info);
}
}
template <typename Value, typename Map>
auto get_attribute_value(const Map& _map, std::string_view _key) -> Value
{
if (const auto iter = _map.find(_key.data()); std::end(_map) != iter) {
return iter->second;
}
throw std::runtime_error{fmt::format("Logical Quotas Policy: Failed to find metadata [{}]", _key)};
}
auto get_instance_config(const irods::instance_configuration_map& _map,
std::string_view _key) -> const irods::instance_configuration&
{
try {
return _map.at(_key.data());
}
catch (const std::out_of_range&) {
throw std::runtime_error{fmt::format("Logical Quotas Policy: Failed to find configuration for "
"rule engine plugin instance [{}]", _key)};
}
}
auto make_unique_id(fs::path _p) -> std::string
{
std::string id = "irods_logical_quotas-";
id += std::to_string(std::hash<std::string>{}(_p.c_str()));
id += "-";
id += std::to_string(getpid());
return id;
}
auto throw_if_string_cannot_be_cast_to_an_integer(const std::string& s, const std::string& error_msg) -> void
{
try {
std::stoll(s); // TODO Could be replaced with std::from_chars when it is available.
}
catch (const std::invalid_argument&) {
throw std::invalid_argument{error_msg};
}
catch (const std::out_of_range&) {
throw std::out_of_range{error_msg};
}
}
auto is_group(rsComm_t& _conn, const std::string_view _entity_name) -> bool
{
const auto gql = fmt::format("select USER_TYPE where USER_NAME = '{}'", _entity_name);
for (auto&& row : irods::query{&_conn, gql}) {
return "rodsgroup" == row[0];
}
return false;
}
auto log_logical_quotas_exception(const irods::logical_quotas_error& e, irods::callback& _effect_handler) -> irods::error
{
log::rule_engine::error(e.what());
addRErrorMsg(&get_rei(_effect_handler).rsComm->rError, e.error_code(), e.what());
return ERROR(e.error_code(), e.what());
}
auto log_irods_exception(const irods::exception& e, irods::callback& _effect_handler) -> irods::error
{
log::rule_engine::error(e.what());
addRErrorMsg(&get_rei(_effect_handler).rsComm->rError, e.code(), e.client_display_what());
return e;
}
auto log_exception(const std::exception& e, irods::callback& _effect_handler) -> irods::error
{
log::rule_engine::error(e.what());
addRErrorMsg(&get_rei(_effect_handler).rsComm->rError, RE_RUNTIME_ERROR, e.what());
return ERROR(RE_RUNTIME_ERROR, e.what());
}
auto get_quota_value_for_collection(RcComm& _conn, const std::string& _coll_path, const std::string& _quota_name)
-> std::tuple<std::string, irods::error>
{
auto ret_error = SUCCESS();
std::string value_out;
// Query will be performed using client connection, ie. with administrative privilege.
// Initialize query conditions and column for selection.
GenQueryInp input{};
GenQueryOut* output{};
addInxIval(&input.selectInp, COL_META_COLL_ATTR_VALUE, 0);
addInxVal(&input.sqlCondInp, COL_COLL_NAME, fmt::format("= '{}'", _coll_path).c_str());
addInxVal(&input.sqlCondInp, COL_META_COLL_ATTR_NAME, fmt::format("= '{}'", _quota_name).c_str());
input.maxRows = MAX_SQL_ROWS;
while (true) {
if (const int ec = rcGenQuery(&_conn, &input, &output); ec < 0) {
if (ec != CAT_NO_ROWS_FOUND) {
ret_error = ERROR(ec, "rcGenQuery failed.");
}
break;
}
for (int row = 0; row < output->rowCnt; ++row) {
for (int attr = 0; attr < output->attriCnt; ++attr) {
const SqlResult* sql_result = &output->sqlResult[attr];
const char* value = sql_result->value + (row * sql_result->len);
value_out = value;
}
}
if (output->continueInx <= 0) {
break;
}
input.continueInx = output->continueInx;
clearGenQueryOut(output);
}
clearGenQueryInp(&input);
freeGenQueryOut(&output);
return std::make_tuple(value_out, ret_error);
}
} // anonymous namespace
namespace irods::handler
{
auto logical_quotas_get_collection_status(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
auto args_iter = std::begin(_rule_arguments);
const auto& path = *boost::any_cast<std::string*>(*args_iter);
if (!is_monitored_collection(conn, attrs, path)) {
THROW(SYS_INVALID_INPUT_PARAM, fmt::format("Logical Quotas Policy: [{}] is not a monitored collection.", path));
}
auto quota_status = nlohmann::json::object(); // Holds the current quota values.
// Fetch the current quota values for the collection.
irods::experimental::client_connection client_conn;
for (const auto& quota_name : {attrs.maximum_number_of_data_objects(),
attrs.maximum_size_in_bytes(),
attrs.total_number_of_data_objects(),
attrs.total_size_in_bytes()})
{
auto [result, err] =
get_quota_value_for_collection(static_cast<RcComm&>(client_conn), path, quota_name);
if (!err.ok()) {
return err;
}
quota_status[quota_name] = std::move(result);
}
// "_ms_param_array" points to a valid object depending on how the rule is invoked. If the implementation
// is invoked via exec_rule, then this parameter will be null. If invoked via exec_rule_text or
// exec_rule_expression, this parameter will point to a valid object. The exec_rule_text/expression
// functions reply on this parameter to return information back to the client.
if (_ms_param_array) {
if (auto* msp = getMsParamByLabel(_ms_param_array, "ruleExecOut"); msp) {
// Free any resources previously associated with the parameter.
if (msp->type) { std::free(msp->type); }
if (msp->inOutStruct) { std::free(msp->inOutStruct); }
// Set the correct type information and allocate enough memory for that type.
msp->type = strdup(ExecCmdOut_MS_T);
msp->inOutStruct = std::malloc(sizeof(ExecCmdOut));
auto* out = static_cast<ExecCmdOut*>(msp->inOutStruct);
std::memset(out, 0, sizeof(ExecCmdOut));
// Copy the JSON string into the output object.
const auto json_string = quota_status.dump();
const auto buffer_size = json_string.size() + 1;
out->stdoutBuf.len = buffer_size;
out->stdoutBuf.buf = std::malloc(sizeof(char) * buffer_size);
std::memcpy(out->stdoutBuf.buf, json_string.data(), buffer_size);
}
else {
auto* out = static_cast<ExecCmdOut*>(std::malloc(sizeof(ExecCmdOut)));
std::memset(out, 0, sizeof(ExecCmdOut));
// Copy the JSON string into the output object.
const auto json_string = quota_status.dump();
const auto buffer_size = json_string.size() + 1;
out->stdoutBuf.len = buffer_size;
out->stdoutBuf.buf = std::malloc(sizeof(char) * buffer_size);
std::memcpy(out->stdoutBuf.buf, json_string.data(), buffer_size);
addMsParamToArray(_ms_param_array, "ruleExecOut", ExecCmdOut_MS_T, out, nullptr, 0);
}
}
// If "_ms_param_array" is not set, then the rule must have been invoked via exec_rule. The client must provide
// a second variable so that the results can be returned.
else if (_rule_arguments.size() == 2) {
*boost::any_cast<std::string*>(*std::next(args_iter)) = quota_status.dump();
}
else {
return ERROR(RE_UNABLE_TO_WRITE_VAR, "Logical Quotas Policy: Missing output variable for status.");
}
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return SUCCESS();
}
auto logical_quotas_start_monitoring_collection(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
return logical_quotas_recalculate_totals(_instance_name, _instance_configs, _rule_arguments, _ms_param_array, _effect_handler);
}
auto logical_quotas_stop_monitoring_collection(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
return unset_metadata_impl(_instance_name, _instance_configs, _rule_arguments, _effect_handler, [](const auto& _attrs) {
return std::vector{&_attrs.total_number_of_data_objects(), &_attrs.total_size_in_bytes()};
});
}
auto logical_quotas_count_total_number_of_data_objects(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto args_iter = std::begin(_rule_arguments);
const auto& path = *boost::any_cast<std::string*>(*args_iter);
auto& rei = get_rei(_effect_handler);
std::vector args{path + '%'};
auto query = irods::experimental::query_builder{}
.type(irods::experimental::query_type::specific)
.bind_arguments(args)
.build<RsComm>(*rei.rsComm, "logical_quotas_count_data_objects_recursive");
std::string objects;
for (auto&& row : query) {
objects = row[0];
}
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
irods::experimental::client_connection conn;
fs::client::set_metadata(
fs::admin, conn, path, {attrs.total_number_of_data_objects(), objects.empty() ? "0" : objects});
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return SUCCESS();
}
auto logical_quotas_count_total_size_in_bytes(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto args_iter = std::begin(_rule_arguments);
const auto& path = *boost::any_cast<std::string*>(*args_iter);
auto& rei = get_rei(_effect_handler);
std::vector args{path + '%'};
auto query = irods::experimental::query_builder{}
.type(irods::experimental::query_type::specific)
.bind_arguments(args)
.build<RsComm>(*rei.rsComm, "logical_quotas_sum_data_object_sizes_recursive");
std::string bytes;
for (auto&& row : query) {
bytes = row[0];
}
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
irods::experimental::client_connection conn;
fs::client::set_metadata(fs::admin, conn, path, {attrs.total_size_in_bytes(), bytes.empty() ? "0" : bytes});
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return SUCCESS();
}
auto logical_quotas_recalculate_totals(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
auto functions = {logical_quotas_count_total_number_of_data_objects,
logical_quotas_count_total_size_in_bytes};
for (auto&& f : functions) {
if (const auto error = f(_instance_name, _instance_configs, _rule_arguments, _ms_param_array, _effect_handler); !error.ok()) {
return error;
}
}
return SUCCESS();
}
auto logical_quotas_set_maximum_number_of_data_objects(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto args_iter = std::begin(_rule_arguments);
const auto& path = *boost::any_cast<std::string*>(*args_iter);
const auto& max_objects = *boost::any_cast<std::string*>(*++args_iter);
const auto msg = fmt::format(
"Logical Quotas Policy: Invalid value for maximum number of data objects [{}]", max_objects);
throw_if_string_cannot_be_cast_to_an_integer(max_objects, msg);
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
irods::experimental::client_connection client_conn;
fs::client::set_metadata(
fs::admin, client_conn, path, {attrs.maximum_number_of_data_objects(), max_objects});
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return SUCCESS();
}
auto logical_quotas_set_maximum_size_in_bytes(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto args_iter = std::begin(_rule_arguments);
const auto& path = *boost::any_cast<std::string*>(*args_iter);
const auto& max_bytes = *boost::any_cast<std::string*>(*++args_iter);
const auto msg =
fmt::format("Logical Quotas Policy: Invalid value for maximum size in bytes [{}]", max_bytes);
throw_if_string_cannot_be_cast_to_an_integer(max_bytes, msg);
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
irods::experimental::client_connection client_conn;
fs::client::set_metadata(fs::admin, client_conn, path, {attrs.maximum_size_in_bytes(), max_bytes});
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return SUCCESS();
}
auto logical_quotas_unset_maximum_number_of_data_objects(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
return unset_metadata_impl(_instance_name, _instance_configs, _rule_arguments, _effect_handler, [](const auto& _attrs) {
return std::vector{&_attrs.maximum_number_of_data_objects()};
});
}
auto logical_quotas_unset_maximum_size_in_bytes(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
return unset_metadata_impl(_instance_name, _instance_configs, _rule_arguments, _effect_handler, [](const auto& _attrs) {
return std::vector{&_attrs.maximum_size_in_bytes()};
});
}
auto logical_quotas_unset_total_number_of_data_objects(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
return unset_metadata_impl(_instance_name, _instance_configs, _rule_arguments, _effect_handler, [](const auto& _attrs) {
return std::vector{&_attrs.total_number_of_data_objects()};
});
}
auto logical_quotas_unset_total_size_in_bytes(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
return unset_metadata_impl(_instance_name, _instance_configs, _rule_arguments, _effect_handler, [](const auto& _attrs) {
return std::vector{&_attrs.total_size_in_bytes()};
});
}
auto pep_api_data_obj_copy::reset() noexcept -> void
{
data_objects_ = 0;
size_in_bytes_ = 0;
}
auto pep_api_data_obj_copy::pre(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
reset(); // Not needed necessarily, but here for completeness.
try {
auto* input = get_pointer<dataObjCopyInp_t>(_rule_arguments);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
if (const auto status = fs::server::status(conn, input->srcDataObjInp.objPath); fs::server::is_data_object(status)) {
data_objects_ = 1;
size_in_bytes_ = fs::server::data_object_size(conn, input->srcDataObjInp.objPath);
}
else if (fs::server::is_collection(status)) {
std::tie(data_objects_, size_in_bytes_) = compute_data_object_count_and_size(conn, input->srcDataObjInp.objPath);
}
else {
throw logical_quotas_error{"Logical Quotas Policy: Invalid object type", INVALID_OBJECT_TYPE};
}
for_each_monitored_collection(conn, attrs, input->destDataObjInp.objPath, [&conn, &attrs](auto& _collection, const auto& _info) {
throw_if_maximum_number_of_data_objects_violation(attrs, _info, data_objects_);
throw_if_maximum_size_in_bytes_violation(attrs, _info, size_in_bytes_);
});
}
catch (const logical_quotas_error& e) {
return log_logical_quotas_exception(e, _effect_handler);
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return CODE(RULE_ENGINE_CONTINUE);
}
auto pep_api_data_obj_copy::post(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto* input = get_pointer<dataObjCopyInp_t>(_rule_arguments);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
for_each_monitored_collection(conn, attrs, input->destDataObjInp.objPath, [&conn, &attrs](const auto& _collection, const auto& _info) {
update_data_object_count_and_size(conn, attrs, _collection, _info, data_objects_, size_in_bytes_);
});
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return CODE(RULE_ENGINE_CONTINUE);
}
auto pep_api_data_obj_create_pre(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto* input = get_pointer<dataObjInp_t>(_rule_arguments);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
for_each_monitored_collection(conn, attrs, input->objPath, [&attrs, input](auto&, auto& _info) {
throw_if_maximum_number_of_data_objects_violation(attrs, _info, 1);
});
}
catch (const logical_quotas_error& e) {
return log_logical_quotas_exception(e, _effect_handler);
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return CODE(RULE_ENGINE_CONTINUE);
}
auto pep_api_data_obj_create_post(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto* input = get_pointer<dataObjInp_t>(_rule_arguments);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
for_each_monitored_collection(conn, attrs, input->objPath, [&conn, &attrs, input](const auto& _collection, const auto& _info) {
update_data_object_count_and_size(conn, attrs, _collection, _info, 1, 0);
});
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return CODE(RULE_ENGINE_CONTINUE);
}
auto pep_api_data_obj_put::reset() noexcept -> void
{
size_diff_ = 0;
forced_overwrite_ = false;
}
auto pep_api_data_obj_put::pre(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
reset();
try {
auto* input = get_pointer<dataObjInp_t>(_rule_arguments);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
if (fs::server::exists(*rei.rsComm, input->objPath)) {
forced_overwrite_ = true;
const size_type existing_size = fs::server::data_object_size(conn, input->objPath);
size_diff_ = static_cast<size_type>(input->dataSize) - existing_size;
for_each_monitored_collection(conn, attrs, input->objPath, [&conn, &attrs, input](const auto& _collection, auto& _info) {
throw_if_maximum_size_in_bytes_violation(attrs, _info, size_diff_);
});
}
else {
for_each_monitored_collection(conn, attrs, input->objPath, [&attrs, input](auto&, auto& _info) {
throw_if_maximum_number_of_data_objects_violation(attrs, _info, 1);
throw_if_maximum_size_in_bytes_violation(attrs, _info, input->dataSize);
});
}
}
catch (const logical_quotas_error& e) {
return log_logical_quotas_exception(e, _effect_handler);
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return CODE(RULE_ENGINE_CONTINUE);
}
auto pep_api_data_obj_put::post(const std::string& _instance_name,
const instance_configuration_map& _instance_configs,
std::list<boost::any>& _rule_arguments,
MsParamArray* _ms_param_array,
irods::callback& _effect_handler) -> irods::error
{
try {
auto* input = get_pointer<dataObjInp_t>(_rule_arguments);
auto& rei = get_rei(_effect_handler);
auto& conn = *rei.rsComm;
const auto& attrs = get_instance_config(_instance_configs, _instance_name).attributes();
if (forced_overwrite_) {
for_each_monitored_collection(conn, attrs, input->objPath, [&conn, &attrs, input](const auto& _collection, const auto& _info) {
update_data_object_count_and_size(conn, attrs, _collection, _info, 0, size_diff_);
});
}
else {
for_each_monitored_collection(conn, attrs, input->objPath, [&conn, &attrs, input](const auto& _collection, const auto& _info) {
update_data_object_count_and_size(conn, attrs, _collection, _info, 1, input->dataSize);
});
}
}
catch (const irods::exception& e) {
return log_irods_exception(e, _effect_handler);
}
catch (const std::exception& e) {
return log_exception(e, _effect_handler);
}
return CODE(RULE_ENGINE_CONTINUE);
}
auto pep_api_data_obj_rename::reset() noexcept -> void
{
data_objects_ = 0;
size_in_bytes_ = 0;
}