-
Notifications
You must be signed in to change notification settings - Fork 141
/
filesystem.cpp
1254 lines (984 loc) · 43 KB
/
filesystem.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/filesystem.hpp"
#include "irods/filesystem/path.hpp"
#include "irods/filesystem/path_utilities.hpp"
#include "irods/filesystem/collection_iterator.hpp"
// clang-format off
#ifdef IRODS_FILESYSTEM_ENABLE_SERVER_SIDE_API
# define IRODS_QUERY_ENABLE_SERVER_SIDE_API
# include "irods/rods.h"
# include "irods/apiHeaderAll.h"
# include "irods/rsSpecificQuery.hpp"
# include "irods/rsObjStat.hpp"
# include "irods/rsDataObjCopy.hpp"
# include "irods/rsDataObjRename.hpp"
# include "irods/rsDataObjUnlink.hpp"
# include "irods/rsDataObjChksum.hpp"
# include "irods/rsModAccessControl.hpp"
# include "irods/rsCollCreate.hpp"
# include "irods/rsModColl.hpp"
# include "irods/rsRmColl.hpp"
# include "irods/rsModAVUMetadata.hpp"
# include "irods/rsModDataObjMeta.hpp"
#else
# include "irods/rodsClient.h"
# include "irods/specificQuery.h"
# include "irods/objStat.h"
# include "irods/dataObjCopy.h"
# include "irods/dataObjRename.h"
# include "irods/dataObjUnlink.h"
# include "irods/dataObjChksum.h"
# include "irods/modAccessControl.h"
# include "irods/collCreate.h"
# include "irods/modColl.h"
# include "irods/rmColl.h"
# include "irods/modAVUMetadata.h"
# include "irods/data_object_modify_info.h"
#endif // IRODS_FILESYSTEM_ENABLE_SERVER_SIDE_API
// clang-format on
#include "irods/rcMisc.h"
#include "irods/rodsErrorTable.h"
#include "irods/irods_log.hpp"
#include "irods/irods_error.hpp"
#include "irods/irods_exception.hpp"
#include "irods/irods_query.hpp"
#include "irods/irods_at_scope_exit.hpp"
#include "irods/query_builder.hpp"
#include <fmt/format.h>
#include <cctype>
#include <cstring>
#include <string>
#include <iterator>
#include <exception>
#include <sstream>
#include <iomanip>
#include <algorithm>
namespace irods::experimental::filesystem::NAMESPACE_IMPL
{
namespace
{
#ifdef IRODS_FILESYSTEM_ENABLE_SERVER_SIDE_API
int rsDataObjCopy(rsComm_t* _comm, dataObjCopyInp_t* _dataObjCopyInp)
{
transferStat_t* ts_ptr{};
const auto ec = rsDataObjCopy(_comm, _dataObjCopyInp, &ts_ptr);
if (ts_ptr) {
std::free(ts_ptr);
}
return ec;
}
const auto rsRmColl = [](rsComm_t* _comm, collInp_t* _rmCollInp, bool _track_progress) -> int
{
collOprStat_t* stat{};
return ::rsRmColl(_comm, _rmCollInp, _track_progress ? &stat : nullptr);
};
#endif // IRODS_FILESYSTEM_ENABLE_SERVER_SIDE_API
struct stat
{
int error;
long long size;
int type;
int mode;
long long id;
std::string owner_name;
std::string owner_zone;
long long ctime;
long long mtime;
std::vector<entity_permission> prms;
bool inheritance;
};
auto to_permission_enum(const std::string& _perm) -> perms
{
// clang-format off
if (_perm == "null") { return perms::null; }
if (_perm == "read_metadata") { return perms::read_metadata; }
if (_perm == "read_object") { return perms::read; }
if (_perm == "read object") { return perms::read; }
if (_perm == "read") { return perms::read; }
if (_perm == "create_metadata") { return perms::create_metadata; }
if (_perm == "modify_metadata") { return perms::modify_metadata; }
if (_perm == "delete_metadata") { return perms::delete_metadata; }
if (_perm == "create_object") { return perms::create_object; }
if (_perm == "modify_object") { return perms::write; }
if (_perm == "modify object") { return perms::write; }
if (_perm == "write") { return perms::write; }
if (_perm == "delete_object") { return perms::delete_object; }
if (_perm == "own") { return perms::own; }
// clang-format on
throw filesystem_error{
"stat error: cannot convert string to permission enum", make_error_code(SYS_INVALID_INPUT_PARAM)};
}
auto set_permissions(rxComm& _comm, const path& _p, stat& _s) -> void
{
if (DATA_OBJ_T == _s.type) {
std::string sql = "select DATA_USER_NAME, DATA_ZONE_NAME, DATA_ACCESS_NAME, USER_TYPE "
"where COLL_NAME = '";
sql += _p.parent_path();
sql += "' and DATA_NAME = '";
sql += _p.object_name();
sql += "' and DATA_TOKEN_NAMESPACE = 'access_type'";
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
for (const auto& row : qb.build(_comm, sql)) {
_s.prms.push_back({row[0], row[1], to_permission_enum(row[2]), row[3]});
}
}
else if (COLL_OBJ_T == _s.type) {
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
try {
std::vector<std::string> args{_p.string()};
qb.type(irods::experimental::query_type::specific)
.bind_arguments(args);
for (const auto& row : qb.build(_comm, "ShowCollAcls")) {
_s.prms.push_back({row[0], row[1], to_permission_enum(row[2]), row[3]});
}
}
catch (...) {
// Fallback to GenQuery if the specific query fails (does not exist).
//
// In the case where the specific query, "ShowCollAcls", does not exist,
// this implementation is required to fallback to GenQuery. The following
// code does not require any information from the exception object, therefore
// it is ignored.
qb.type(irods::experimental::query_type::general);
std::string sql = "select COLL_USER_NAME, COLL_ZONE_NAME, COLL_ACCESS_NAME, USER_TYPE "
"where COLL_TOKEN_NAMESPACE = 'access_type' and COLL_NAME = '";
sql += _p.c_str();
sql += "'";
for (const auto& row : qb.build(_comm, sql)) {
_s.prms.push_back({row[0], row[1], to_permission_enum(row[2]), row[3]});
}
}
}
}
auto get_inheritance(rxComm& _comm, const path& _p, int _object_type) -> bool
{
if (COLL_OBJ_T != _object_type) {
return false;
}
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
qb.type(irods::experimental::query_type::general);
const auto gql = fmt::format("select COLL_INHERITANCE where COLL_NAME = '{}'", _p.c_str());
for (const auto& row : qb.build(_comm, gql)) {
return std::atoi(row[0].data());
}
return false;
}
auto stat(rxComm& _comm, const path& _p) -> stat
{
dataObjInp_t input{};
std::strncpy(input.objPath, _p.c_str(), std::strlen(_p.c_str()));
rodsObjStat_t* output{};
struct stat s{};
if (s.error = rxObjStat(&_comm, &input, &output); s.error >= 0) {
irods::at_scope_exit at_scope_exit{[output] {
freeRodsObjStat(output);
}};
try {
s.id = std::stoll(output->dataId);
s.ctime = std::stoll(output->createTime);
s.mtime = std::stoll(output->modifyTime);
}
catch (...) {
throw filesystem_error{"stat error: cannot convert string to integer", _p, make_error_code(SYS_INTERNAL_ERR)};
}
s.size = output->objSize;
s.type = output->objType;
s.mode = static_cast<int>(output->dataMode);
s.owner_name = output->ownerName;
s.owner_zone = output->ownerZone;
s.inheritance = get_inheritance(_comm, _p, s.type);
set_permissions(_comm, _p, s);
}
else if (USER_FILE_DOES_NOT_EXIST == s.error) {
s.error = 0;
s.type = UNKNOWN_OBJ_T;
}
return s;
}
auto is_collection_empty(rxComm& _comm, const path& _p) -> bool
{
return collection_iterator{} == collection_iterator{_comm, _p};
}
auto remove_impl(rxComm& _comm, const path& _p, extended_remove_options _opts) -> bool
{
throw_if_path_length_exceeds_limit(_p);
const auto s = status(_comm, _p);
if (!exists(s)) {
return false;
}
if (is_data_object(s)) {
dataObjInp_t input{};
at_scope_exit free_memory{[&input] { clearKeyVal(&input.condInput); }};
input.oprType = _opts.unregister ? UNREG_OPR : 0;
std::strncpy(input.objPath, _p.c_str(), std::strlen(_p.c_str()));
if (_opts.no_trash) {
addKeyVal(&input.condInput, FORCE_FLAG_KW, "");
}
return rxDataObjUnlink(&_comm, &input) == 0;
}
if (is_collection(s)) {
if (!_opts.recursive && !is_collection_empty(_comm, _p)) {
throw filesystem_error{"cannot remove non-empty collection", _p, make_error_code(SYS_COLLECTION_NOT_EMPTY)};
}
collInp_t input{};
at_scope_exit free_memory{[&input] { clearKeyVal(&input.condInput); }};
std::strncpy(input.collName, _p.c_str(), std::strlen(_p.c_str()));
if (_opts.no_trash) {
addKeyVal(&input.condInput, FORCE_FLAG_KW, "");
}
if (_opts.recursive) {
addKeyVal(&input.condInput, RECURSIVE_OPR__KW, "");
}
return rxRmColl(&_comm, &input, _opts.progress) >= 0;
}
throw filesystem_error{"cannot remove: unknown object type", _p, make_error_code(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION)};
}
auto set_permissions(bool _add_admin_flag,
rxComm& _comm,
const path& _p,
const std::string& _user_or_group,
perms _prms) -> void
{
throw_if_path_length_exceeds_limit(_p);
char username[NAME_LEN]{};
char zone[NAME_LEN]{};
auto ec = parseUserName(_user_or_group.c_str(), username, zone);
if (ec != 0) {
throw filesystem_error{"cannot parse user/group name", _p, make_error_code(ec)};
}
modAccessControlInp_t input{};
input.userName = username;
input.zone = zone;
char path[MAX_NAME_LEN]{};
std::strncpy(path, _p.c_str(), std::strlen(_p.c_str()));
input.path = path;
char access[16]{};
if (_add_admin_flag) {
std::strcpy(access, "admin:");
}
switch (_prms) {
case perms::null:
std::strncat(access, "null", 4);
break;
case perms::read_metadata:
std::strncat(access, "read_metadata", 13);
break;
case perms::read_object:
case perms::read:
std::strncat(access, "read", 4);
break;
case perms::create_metadata:
std::strncat(access, "create_metadata", 15);
break;
case perms::modify_metadata:
std::strncat(access, "modify_metadata", 15);
break;
case perms::delete_metadata:
std::strncat(access, "delete_metadata", 15);
break;
case perms::create_object:
std::strncat(access, "create_object", 13);
break;
case perms::modify_object:
case perms::write:
std::strncat(access, "write", 5);
break;
case perms::delete_object:
std::strncat(access, "delete_object", 13);
break;
case perms::own:
std::strncat(access, "own", 3);
break;
}
input.accessLevel = access;
ec = rxModAccessControl(&_comm, &input);
if (ec != 0) {
throw filesystem_error{"cannot set permissions", _p, make_error_code(ec)};
}
}
auto set_inheritance(bool _add_admin_flag, rxComm& _comm, const path& _p, bool _value) -> void
{
throw_if_path_is_empty(_p);
throw_if_path_length_exceeds_limit(_p);
if (!is_collection(_comm, _p)) {
throw filesystem_error{"existing path is not a collection", _p, make_error_code(NOT_A_COLLECTION)};
}
modAccessControlInp_t input{};
input.userName = "";
input.zone = "";
char path[MAX_NAME_LEN]{};
std::strncpy(path, _p.c_str(), std::strlen(_p.c_str()));
input.path = path;
char access[16]{};
if (_add_admin_flag) {
std::strcpy(access, "admin:");
}
std::strcat(access, _value ? "inherit" : "noinherit");
input.accessLevel = access;
const auto ec = rxModAccessControl(&_comm, &input);
if (ec != 0) {
throw filesystem_error{"cannot set inheritance", _p, make_error_code(ec)};
}
}
auto has_prefix(const path& _p, const path& _prefix) -> bool
{
if (_p == _prefix) {
return false;
}
auto p_iter = std::begin(_prefix);
auto p_last = std::end(_prefix);
auto c_iter = std::begin(_p);
auto c_last = std::end(_p);
for (; p_iter != p_last && c_iter != c_last && *p_iter == *c_iter; ++p_iter, ++c_iter);
return (p_iter == p_last);
}
auto do_metadata_op(bool _add_admin_flag,
rxComm& _comm,
const path& _p,
const metadata& _metadata,
std::string_view op) -> void
{
throw_if_path_is_empty(_p);
throw_if_path_length_exceeds_limit(_p);
modAVUMetadataInp_t input{};
char* command = const_cast<char*>(op.data());
input.arg0 = command;
char type[3]{};
if (const auto s = status(_comm, _p); is_data_object(s)) {
std::strncpy(type, "-d", std::strlen("-d"));
}
else if (is_collection(s)) {
std::strncpy(type, "-C", std::strlen("-C"));
}
else {
std::string_view op_full_name = (op == "rm") ? "remove" : op;
throw filesystem_error{fmt::format("cannot {} metadata: unknown object type", op_full_name), _p,
make_error_code(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION)};
}
input.arg1 = type;
char path_buf[MAX_NAME_LEN]{};
std::strncpy(path_buf, _p.c_str(), std::strlen(_p.c_str()));
input.arg2 = path_buf;
char attr_buf[MAX_NAME_LEN]{};
std::strncpy(attr_buf, _metadata.attribute.c_str(), _metadata.attribute.size());
input.arg3 = attr_buf;
char value_buf[MAX_NAME_LEN]{};
std::strncpy(value_buf, _metadata.value.c_str(), _metadata.value.size());
input.arg4 = value_buf;
char units_buf[MAX_NAME_LEN]{};
std::strncpy(units_buf, _metadata.units.c_str(), _metadata.units.size());
input.arg5 = units_buf;
at_scope_exit free_memory{[&input] { clearKeyVal(&input.condInput); }};
if (_add_admin_flag) {
addKeyVal(&input.condInput, ADMIN_KW, "");
}
if (const auto ec = rxModAVUMetadata(&_comm, &input); ec != 0) {
std::string_view op_full_name = (op == "rm") ? "remove" : op;
throw filesystem_error{fmt::format("cannot {} metadata", op_full_name), _p, make_error_code(ec)};
}
}
} // anonymous namespace
// Operational functions
auto copy(rxComm& _comm, const path& _from, const path& _to, copy_options _options) -> void
{
const auto from_status = status(_comm, _from);
if (!exists(from_status)) {
throw filesystem_error{"path does not exist", _from, make_error_code(OBJ_PATH_DOES_NOT_EXIST)};
}
const auto to_status = status(_comm, _to);
if (exists(to_status) && equivalent(_comm, _from, _to)) {
throw filesystem_error{"paths cannot point to the same object", _from, _to, make_error_code(SAME_SRC_DEST_PATHS_ERR)};
}
if (is_other(from_status)) {
throw filesystem_error{"cannot copy: unknown object type", _from, make_error_code(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION)};
}
if (is_other(to_status)) {
throw filesystem_error{"cannot copy: unknown object type", _to, make_error_code(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION)};
}
if (is_collection(from_status) && is_data_object(to_status)) {
throw filesystem_error{"cannot copy a collection into a data object", _from, _to, make_error_code(SYS_INVALID_INPUT_PARAM)};
}
if (is_data_object(from_status)) {
if (copy_options::collections_only == (copy_options::collections_only & _options)) {
return;
}
if (is_collection(to_status)) {
copy_data_object(_comm, _from, _to / _from.object_name(), _options);
return;
}
copy_data_object(_comm, _from, _to, _options);
}
else if (is_collection(from_status)) {
if (copy_options::recursive == (copy_options::recursive & _options) ||
copy_options::none == _options)
{
if (!exists(to_status)) {
if (!create_collection(_comm, _to, _from)) {
throw filesystem_error{"cannot create collection", _to, make_error_code(FILE_CREATE_ERROR)};
}
}
for (const auto& e : collection_iterator{_comm, _from}) {
copy(_comm, e.path(), _to / e.path().object_name(), _options | copy_options::in_recursive_copy);
}
}
}
}
auto copy_data_object(rxComm& _comm, const path& _from, const path& _to, copy_options _options) -> bool
{
throw_if_path_length_exceeds_limit(_from);
throw_if_path_length_exceeds_limit(_to);
if (!is_data_object(_comm, _from)) {
throw filesystem_error{"path does not point to a data object", _from, make_error_code(INVALID_OBJECT_TYPE)};
}
dataObjCopyInp_t input{};
at_scope_exit free_memory{[&input] {
clearKeyVal(&input.srcDataObjInp.condInput);
clearKeyVal(&input.destDataObjInp.condInput);
}};
if (const auto s = status(_comm, _to); exists(s)) {
if (equivalent(_comm, _from, _to)) {
throw filesystem_error{"paths cannot point to the same object", _from, _to, make_error_code(SAME_SRC_DEST_PATHS_ERR)};
}
if (!is_data_object(s)) {
throw filesystem_error{"path does not point to a data object", _to, make_error_code(INVALID_OBJECT_TYPE)};
}
if (copy_options::skip_existing == _options) {
return false;
}
if (copy_options::overwrite_existing == _options) {
addKeyVal(&input.destDataObjInp.condInput, FORCE_FLAG_KW, "");
}
else if (copy_options::update_existing == _options) {
if (last_write_time(_comm, _from) <= last_write_time(_comm, _to)) {
return false;
}
addKeyVal(&input.destDataObjInp.condInput, FORCE_FLAG_KW, "");
}
}
std::strncpy(input.srcDataObjInp.objPath, _from.c_str(), std::strlen(_from.c_str()));
std::strncpy(input.destDataObjInp.objPath, _to.c_str(), std::strlen(_to.c_str()));
if (const auto ec = rxDataObjCopy(&_comm, &input); ec < 0) {
throw filesystem_error{"cannot copy data object", _from, _to, make_error_code(ec)};
}
return true;
}
auto create_collection(rxComm& _comm, const path& _p) -> bool // Implies perms::all
{
throw_if_path_length_exceeds_limit(_p);
if (!exists(_comm, _p.parent_path())) {
throw filesystem_error{"path does not exist", _p.parent_path(), make_error_code(OBJ_PATH_DOES_NOT_EXIST)};
}
if (exists(_comm, _p)) {
return false;
}
collInp_t input{};
std::strncpy(input.collName, _p.c_str(), std::strlen(_p.c_str()));
if (const auto ec = rxCollCreate(&_comm, &input); ec != 0) {
throw filesystem_error{"cannot create collection", _p, make_error_code(ec)};
}
return true;
}
auto create_collection(rxComm& _comm, const path& _p, const path& _existing_p) -> bool
{
throw_if_path_is_empty(_p);
throw_if_path_is_empty(_existing_p);
throw_if_path_length_exceeds_limit(_p);
throw_if_path_length_exceeds_limit(_existing_p);
const auto s = status(_comm, _existing_p);
if (!is_collection(s)) {
throw filesystem_error{"existing path is not a collection", _existing_p, make_error_code(INVALID_OBJECT_TYPE)};
}
create_collection(_comm, _p);
for (auto&& p : s.permissions()) {
permissions(_comm, _p, p.name, p.prms);
}
return true;
}
auto create_collections(rxComm& _comm, const path& _p) -> bool
{
throw_if_path_length_exceeds_limit(_p);
if (exists(_comm, _p)) {
return false;
}
collInp_t input{};
at_scope_exit free_memory{[&input] { clearKeyVal(&input.condInput); }};
std::strncpy(input.collName, _p.c_str(), std::strlen(_p.c_str()));
addKeyVal(&input.condInput, RECURSIVE_OPR__KW, "");
return rxCollCreate(&_comm, &input) == 0;
}
auto exists(const object_status& _s) noexcept -> bool
{
return status_known(_s) && _s.type() != object_type::not_found;
}
auto exists(rxComm& _comm, const path& _p) -> bool
{
return exists(status(_comm, _p));
}
auto is_collection_registered(rxComm& _comm, const path& _p) -> bool
{
throw_if_path_is_empty(_p);
throw_if_path_length_exceeds_limit(_p);
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
const auto gql = fmt::format("select COLL_ID where COLL_NAME = '{}'", _p.c_str());
return qb.build(_comm, gql).size() > 0;
}
auto is_data_object_registered(rxComm& _comm, const path& _p) -> bool
{
throw_if_path_is_empty(_p);
throw_if_path_length_exceeds_limit(_p);
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
const auto gql = fmt::format("select DATA_ID where COLL_NAME = '{}' and DATA_NAME = '{}'",
_p.parent_path().c_str(),
_p.object_name().c_str());
return qb.build(_comm, gql).size() > 0;
}
auto equivalent(rxComm& _comm, const path& _p1, const path& _p2) -> bool
{
throw_if_path_is_empty(_p1);
throw_if_path_is_empty(_p2);
const auto p1_info = stat(_comm, _p1);
if (p1_info.error < 0) {
throw filesystem_error{"cannot stat path", _p1, make_error_code(p1_info.error)};
}
if (p1_info.type == UNKNOWN_OBJ_T) {
throw filesystem_error{"path does not exist", _p1, make_error_code(OBJ_PATH_DOES_NOT_EXIST)};
}
const auto p2_info = stat(_comm, _p2);
if (p2_info.error < 0) {
throw filesystem_error{"cannot stat path", _p2, make_error_code(p2_info.error)};
}
if (p2_info.type == UNKNOWN_OBJ_T) {
throw filesystem_error{"path does not exist", _p2, make_error_code(OBJ_PATH_DOES_NOT_EXIST)};
}
return p1_info.id == p2_info.id;
}
auto data_object_size(rxComm& _comm, const path& _p) -> std::uintmax_t
{
throw_if_path_is_empty(_p);
throw_if_path_length_exceeds_limit(_p);
if (!is_data_object(_comm, _p)) {
throw filesystem_error{"path does not point to a data object", _p, make_error_code(SYS_INVALID_INPUT_PARAM)};
}
// Fetch information for good replicas only (i.e. DATA_REPL_STATUS = '1').
const auto gql = fmt::format("select DATA_SIZE, DATA_MODIFY_TIME "
"where"
" COLL_NAME = '{}' and"
" DATA_NAME = '{}' and"
" DATA_REPL_STATUS = '1'",
_p.parent_path().c_str(),
_p.object_name().c_str());
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
auto query = qb.build(_comm, gql);
if (query.size() == 0) {
throw filesystem_error{"no good replica found", _p, make_error_code(SYS_NO_GOOD_REPLICA)};
}
// This implementation assumes that any good replica will always satisfy
// the requirement within the loop, therefore the first iteration always causes
// the size to be captured. The size object should be empty if and only if there
// are no good replicas.
std::uint64_t latest_mtime = 0;
std::uintmax_t size = 0;
for (auto&& row : query) {
// As we iterate over the replicas, compare the mtimes and capture the size
// of the latest good replica.
if (const auto current_mtime = std::stoull(row[1]); current_mtime > latest_mtime) {
latest_mtime = current_mtime;
size = std::stoull(row[0]);
}
}
return size;
}
auto is_collection(const object_status& _s) noexcept -> bool
{
return _s.type() == object_type::collection;
}
auto is_collection(rxComm& _comm, const path& _p) -> bool
{
return is_collection(status(_comm, _p));
}
auto is_special_collection(rxComm& _comm, const path& _p) -> bool
{
throw_if_path_is_empty(_p);
throw_if_path_length_exceeds_limit(_p);
const auto gql = fmt::format("select COLL_TYPE, COLL_INFO_1, COLL_INFO_2 "
"where COLL_NAME = '{}'", _p.c_str());
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
for (auto&& row : qb.build(_comm, gql)) {
return !row[0].empty() && (!row[1].empty() || !row[2].empty());
}
return false;
}
auto is_empty(rxComm& _comm, const path& _p) -> bool
{
const auto s = status(_comm, _p);
if (is_collection(s)) {
return is_collection_empty(_comm, _p);
}
throw filesystem_error{"cannot check emptiness: unknown object type", _p, make_error_code(CAT_NOT_A_DATAOBJ_AND_NOT_A_COLLECTION)};
}
auto is_other(const object_status& _s) noexcept -> bool
{
return _s.type() == object_type::unknown;
}
auto is_other(rxComm& _comm, const path& _p) -> bool
{
return is_other(status(_comm, _p));
}
auto is_data_object(const object_status& _s) noexcept -> bool
{
return _s.type() == object_type::data_object;
}
auto is_data_object(rxComm& _comm, const path& _p) -> bool
{
return is_data_object(status(_comm, _p));
}
auto last_write_time(rxComm& _comm, const path& _p) -> object_time_type
{
std::string gql;
if (const auto s = status(_comm, _p); is_data_object(s)) {
// Fetch information for good replicas only (i.e. DATA_REPL_STATUS = '1').
gql = fmt::format("select max(DATA_MODIFY_TIME) "
"where"
" COLL_NAME = '{}' and"
" DATA_NAME = '{}' and"
" DATA_REPL_STATUS = '1'",
_p.parent_path().c_str(),
_p.object_name().c_str());
}
else if (is_collection(s)) {
gql = fmt::format("select COLL_MODIFY_TIME where COLL_NAME = '{}'", _p.c_str());
}
else {
throw filesystem_error{"cannot get mtime", _p, make_error_code(INVALID_OBJECT_TYPE)};
}
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
for (auto&& row : qb.build(_comm, gql)) {
return object_time_type{std::chrono::seconds{std::stoull(row[0])}};
}
throw filesystem_error{"cannot get mtime", _p, make_error_code(CAT_NO_ROWS_FOUND)};
}
auto last_write_time(rxComm& _comm, const path& _p, object_time_type _new_time) -> void
{
throw_if_path_length_exceeds_limit(_p);
if (!is_collection(_comm, _p)) {
throw filesystem_error{"path does not point to a collection", _p, make_error_code(SYS_INVALID_INPUT_PARAM)};
}
const auto timestamp = fmt::format("{:011}", _new_time.time_since_epoch().count());
collInp_t input{};
at_scope_exit free_memory{[&input] { clearKeyVal(&input.condInput); }};
std::strncpy(input.collName, _p.c_str(), std::strlen(_p.c_str()));
addKeyVal(&input.condInput, COLLECTION_MTIME_KW, timestamp.c_str());
if (const auto ec = rxModColl(&_comm, &input); ec != 0) {
throw filesystem_error{"cannot set mtime", _p, make_error_code(ec)};
}
}
auto remove(rxComm& _comm, const path& _p, remove_options _opts) -> bool
{
extended_remove_options opts{};
opts.no_trash = (remove_options::no_trash == _opts);
opts.recursive = false;
return remove_impl(_comm, _p, opts);
}
auto remove(rxComm& _comm, const path& _p, extended_remove_options _opts) -> bool
{
return remove_impl(_comm, _p, _opts);
}
auto remove_all(rxComm& _comm, const path& _p, remove_options _opts) -> std::uintmax_t
{
std::string data_obj_sql = "select count(DATA_ID) where COLL_NAME like '";
data_obj_sql += _p;
data_obj_sql += "%'";
std::string colls_sql = "select count(COLL_ID) where COLL_NAME like '";
colls_sql += _p;
colls_sql += "%'";
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
std::uintmax_t count = 0;
for (const auto& sql : {data_obj_sql, colls_sql}) {
for (const auto& row : qb.build(_comm, sql)) {
count += std::stoull(row[0]);
}
}
if (0 == count) {
return 0;
}
extended_remove_options opts{};
opts.no_trash = (remove_options::no_trash == _opts);
opts.recursive = true;
if (remove_impl(_comm, _p, opts)) {
return count;
}
return 0;
}
auto remove_all(rxComm& _comm, const path& _p, extended_remove_options _opts) -> std::uintmax_t
{
std::string data_obj_sql = "select count(DATA_ID) where COLL_NAME like '";
data_obj_sql += _p;
data_obj_sql += "%'";
std::string colls_sql = "select count(COLL_ID) where COLL_NAME like '";
colls_sql += _p;
colls_sql += "%'";
irods::experimental::query_builder qb;
if (const auto zone = zone_name(_p); zone) {
qb.zone_hint(*zone);
}
std::uintmax_t count = 0;
for (const auto& sql : {data_obj_sql, colls_sql}) {
for (const auto& row : qb.build(_comm, sql)) {
count += std::stoull(row[0]);
}
}
if (0 == count) {
return 0;
}
_opts.recursive = true;
if (remove_impl(_comm, _p, _opts)) {
return count;
}
return 0;
}
auto permissions(rxComm& _comm, const path& _p, const std::string& _user_or_group, perms _prms) -> void
{
constexpr auto add_admin_flag = false;
set_permissions(add_admin_flag, _comm, _p, _user_or_group, _prms);
}
auto permissions(admin_tag, rxComm& _comm, const path& _p, const std::string& _user_or_group, perms _prms) -> void
{
constexpr auto add_admin_flag = true;
set_permissions(add_admin_flag, _comm, _p, _user_or_group, _prms);
}