-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata-store-fix
More file actions
executable file
·841 lines (686 loc) · 19.5 KB
/
Copy pathdata-store-fix
File metadata and controls
executable file
·841 lines (686 loc) · 19.5 KB
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
#!/usr/bin/env bash
#
# © 2021, The Arizona Board of Regents on behalf of The University of Arizona.
# For license information, see https://cyverse.org/license.
show_help() {
cat <<EOF
$EXEC_NAME version $VERSION
Usage:
$EXEC_NAME -h|--help
$EXEC_NAME -v|--version
$EXEC_NAME [-d|--debug | (-A|--age) AGE | (-U|--db-user) DB-USER |
(-H|--dbms-host) DBMS-HOST | (-P|--dbms-port) DBMS-PORT]...
This program fixes the following three common problems that happen when an
upload fails.
1. The rodsadmin group isn't given own permission on a new collection or data
object.
2. A new collection or data object doesn't receive a UUID.
3. A checksum isn't computed for a new or modified data object replica.
It generates two reports: one for collections and one for data objects. Each
report lists all collections or data objects, respectively, that had one or more
of the problems. For each problem, the report indicates if the script was able
to fix the problem. If the program wasn't able to, the error encountered while
attempting the fix will be included at the bottom of the relevant report.
Options:
-A, --age AGE how long ago a collection or data object
modification must have occurred before it will be
considered, default is 0
-U, --db-user DB-USER the account used to authorize the connection to the
ICAT database
-H, --dbms-host DBMS-HOST the domain name or IP address of the server hosting
the PostgreSQL DBMS containing the DB
-P, --dbms-port DBMS-PORT the TCP port the DBMS listens on
-d, --debug will cause the program to display messages
describing what it is doing
-h, --help show help and exit
-v, --version show version and exit
Environment Variables:
PGHOST provides the default value for the DBMS host, default is "localhost"
PGPORT provides the default value for the TCP port the DBMS listens on,
default is 5432
PGUSER provides the default value for the account used to authorize the
connection, default is the user executing this program.
EOF
}
readonly VERSION=5
set -o errexit -o nounset -o pipefail
export PGHOST PGPORT PGUSER
readonly COLL_TABLE_HEADING='Collection Repair Attempts'
readonly DATA_TABLE_HEADING='Data Object Repair Attempts'
readonly DEFAULT_AGE=0
readonly MAX_LOG_MAIL_SIZE=2097152 # 2 MiB
readonly FIX_CONCURRENCY=5
EXEC_PATH="$(realpath --canonicalize-existing "$0")"
readonly EXEC_PATH
EXEC_DIR="$(dirname "$EXEC_PATH")"
EXEC_NAME="$(basename "$EXEC_PATH")"
readonly EXEC_DIR EXEC_NAME
export EXEC_DIR
declare DEBUG
declare -a TempFiles
main() {
local opts
if ! opts="$(format_opts "$@")"; then
show_help >&2
return 1
fi
eval set -- "$opts"
local age="$DEFAULT_AGE"
local help version
while true; do
case "$1" in
-A|--age)
age="$2"
shift 2
;;
-U|--db-user)
PGUSER="$2"
shift 2
;;
-H|--dbms-host)
PGHOST="$2"
shift 2
;;
-P|--dbms-port)
PGPORT="$2"
shift 2
;;
-d|--debug)
DEBUG=debug
shift
;;
-h|--help)
help=help
shift
;;
-v|--version)
version=version
shift
;;
--)
shift
break
;;
esac
done
readonly DEBUG
if [[ -n "${help-}" ]]; then
show_help
elif [[ -n "${version-}" ]]; then
printf '%s\n' "$VERSION"
else
fix "$age"
fi
}
# formats the command-line options using getopt
# Arguments:
# It accepts the raw commandline arguments
# Output:
# the arguments formatted by getopt
format_opts() {
getopt \
--name "$EXEC_NAME" \
--longoptions age:,db-user:,dbms-host:,dbms-port:,debug,help,version \
--options A:dH:hP:U:v \
-- "$@"
}
# performs any needed fixes and generates the report
# Arguments:
# age how many days old a collection or data object must be before it will be
# considered
# Output:
# the reports
fix() {
local age="$1"
check_existing_instance
trap rm_temp_files EXIT
TempFiles+=("$(mktemp)")
local errorLog="${TempFiles[-1]}"
TempFiles+=("$(mktemp)")
local problems="${TempFiles[-1]}"
local zone
zone="$(determine_zone)"
local cutoffTime
cutoffTime="$(date --iso-8601 --date "$age days ago")"
local cutoffTS
cutoffTS="$(date --date="$cutoffTime" '+%s')"
if ! (find_problems "$zone" "$cutoffTS" 2>> "$errorLog" | strip_noise > "$problems"); then
local rc=$?
print_report_title "$cutoffTime"
printf 'The following errors occurred while accessing the ICAT DB.\n\n'
cat "$errorLog"
return $rc
fi
trap 'if [[ "${FUNCNAME[0]-}" != main ]]; then return; fi' SIGINT SIGTERM
TempFiles+=("$(mktemp)")
local fixLog="${TempFiles[-1]}"
fix_problems < "$problems" > "$fixLog" 2> "$errorLog" || true
generate_report "$cutoffTime" "$fixLog" "$errorLog"
}
check_existing_instance() {
local pid
for pid in $(pgrep --full "$EXEC_NAME"); do
if (( pid != $$ && pid != PPID )); then
printf "%s already running with PID %s\n" "$EXEC_NAME" "$pid" 1>&2
return 1
fi
done
}
determine_zone() {
"$EXEC_DIR"/quest '%s' <<< "select ZONE_NAME where ZONE_TYPE = 'local'"
}
find_problems() {
local zone="$1"
local cutoffTS="$2"
psql ICAT <<EOF
$(inject_debug_stmt '\timing on')
$(inject_debug_newline)
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
$(inject_perm_probs_tables "$zone")
$(inject_uuid_probs_tables "$zone")
$(inject_checksum_probs_table "$cutoffTS")
$(inject_debug_newline)
$(inject_heading "$COLL_TABLE_HEADING")
SELECT
coll_id IN (SELECT * FROM coll_perm_probs) AS "Permission Issue",
COALESCE((SELECT cu.uuid_count FROM coll_uuid_probs AS cu WHERE cu.coll_id = c.coll_id), 1)
AS "UUID Count",
coll_owner_name || '#' || coll_owner_zone AS "Owner",
TO_TIMESTAMP(CAST(modify_ts AS INTEGER)) AS "Modify Time",
REPLACE(REPLACE(coll_name, E'\\\\', E'\\\\\\\\'), E'\\n', E'\\\\n') AS "Collection"
FROM r_coll_main AS c
WHERE coll_id IN (SELECT * FROM coll_perm_probs UNION SELECT coll_id FROM coll_uuid_probs)
AND coll_name LIKE '/$zone/%'
AND coll_type != 'linkPoint'
ORDER BY modify_ts;
\echo ''
$(inject_heading "$DATA_TABLE_HEADING")
SELECT
d.data_id IN (SELECT data_id FROM data_perm_probs) AS "Permission Issue",
d.data_checksum IS NULL OR d.data_checksum = '' AS "Missing Checksum",
COALESCE((SELECT du.uuid_count FROM data_uuid_probs AS du WHERE du.data_id = d.data_id), 1)
AS "UUID Count",
d.data_owner_name || '#' || d.data_owner_zone AS "Owner",
d.data_repl_num AS "Replica",
TO_TIMESTAMP(CAST(d.modify_ts AS INTEGER)) AS "Modify Time",
REPLACE(REPLACE(c.coll_name || '/' || d.data_name, E'\\\\', E'\\\\\\\\'), E'\\n', E'\\\\n')
AS "Data Object"
FROM r_coll_main AS c JOIN r_data_main AS d ON d.coll_id = c.coll_id
WHERE c.coll_name LIKE '/$zone/%'
AND d.modify_ts < '0$cutoffTS'
AND d.data_is_dirty = '1'
AND (
(d.data_id, d.data_repl_num) IN (SELECT data_id, data_repl_num FROM data_chksum_probs)
OR d.data_id IN (
SELECT data_id FROM data_perm_probs UNION SELECT data_id FROM data_uuid_probs))
ORDER BY d.modify_ts;
$(inject_debug_newline)
ROLLBACK;
EOF
}
inject_checksum_probs_table() {
local cutoffTS="$1"
cat <<EOF
$(inject_debug_msg 'creating data_checksum_probs')
CREATE TEMPORARY TABLE data_chksum_probs AS
SELECT DISTINCT data_id, data_repl_num
FROM r_data_main
WHERE modify_ts < '0$cutoffTS'
AND data_is_dirty = 1
AND (data_checksum IS NULL OR data_checksum = '');
CREATE INDEX idx_data_chksum_probs ON data_chksum_probs (data_id, data_repl_num);
EOF
}
inject_perm_probs_tables() {
local zone="$1"
cat <<EOF
$(inject_debug_msg 'creating owned_by_rodsadmin')
CREATE TEMPORARY TABLE owned_by_rodsadmin AS
SELECT a.object_id
FROM r_objt_access AS a JOIN r_user_main AS u ON u.user_id = a.user_id
WHERE u.user_name = 'rodsadmin'
AND a.access_type_id = (
SELECT token_id
FROM r_tokn_main
WHERE token_namespace = 'access_type' AND token_name = 'own');
CREATE INDEX idx_owned_by_rodsadmin ON owned_by_rodsadmin (object_id);
$(inject_debug_msg 'creating coll_perm_probs')
CREATE TEMPORARY TABLE coll_perm_probs AS
SELECT coll_id
FROM r_coll_main AS c
WHERE NOT EXISTS (SELECT * FROM owned_by_rodsadmin AS o WHERE o.object_id = c.coll_id)
AND c.coll_name SIMILAR TO '/$zone(/trash)?/home/%'
AND c.coll_type != 'linkPoint';
CREATE INDEX idx_coll_perm_probs ON coll_perm_probs(coll_id);
$(inject_debug_msg 'creating data_perm_probs')
CREATE TEMPORARY TABLE data_perm_probs AS
SELECT DISTINCT data_id
FROM r_data_main AS d JOIN r_coll_main AS c ON c.coll_id = d.coll_id
WHERE c.coll_name SIMILAR TO '/$zone(/trash)?/home/%'
AND NOT EXISTS (SELECT * FROM owned_by_rodsadmin AS o WHERE o.object_id = d.data_id);
CREATE INDEX idx_data_perm_probs ON data_perm_probs(data_id);
EOF
}
inject_uuid_probs_tables() {
local zone="$1"
cat <<EOF
$(inject_debug_msg 'creating uuid_attrs')
CREATE TEMPORARY TABLE uuid_attrs AS
SELECT o.object_id
FROM r_objt_metamap AS o JOIN r_meta_main AS m ON m.meta_id = o.meta_id
WHERE m.meta_attr_name = 'ipc_UUID';
CREATE INDEX idx_uuid_attrs ON uuid_attrs (object_id);
$(inject_debug_msg 'creating coll_uuid_probs')
CREATE TEMPORARY TABLE coll_uuid_probs (coll_id, uuid_count) AS
SELECT c.coll_id, COUNT(u.object_id)
FROM r_coll_main AS c LEFT JOIN uuid_attrs AS u ON u.object_id = c.coll_id
WHERE c.coll_name LIKE '/$zone/%' AND c.coll_type != 'linkPoint'
GROUP BY c.coll_id
HAVING COUNT(u.object_id) != 1;
CREATE INDEX idx_coll_uuid_probs ON coll_uuid_probs (coll_id);
$(inject_debug_msg 'creating data_uuid_probs')
CREATE TEMPORARY TABLE data_uuid_probs (data_id, uuid_count) AS
SELECT DISTINCT d.data_id, COUNT(u.object_id)
FROM r_data_main AS d LEFT JOIN uuid_attrs AS u ON u.object_id = d.data_id
GROUP BY d.data_id, d.data_repl_num
-- data_repl_num prevents counting uuids for multiple replicas in a single count
HAVING COUNT(u.object_id) != 1;
CREATE INDEX idx_data_uuid_probs ON data_uuid_probs(data_id);
EOF
}
inject_debug_msg() {
local msg="$1"
inject_debug_newline
inject_debug_stmt "\\echo '$msg'"
}
inject_debug_newline() {
inject_debug_stmt "\\echo ''"
}
inject_debug_stmt() {
local stmt="$1"
if [[ -n "${DEBUG-}" ]]; then
printf '%s\n' "$stmt"
fi
}
inject_heading() {
local title="$1"
inject_debug_quiet on
printf $'\pset title \'%s\'\n' "$title"
inject_debug_quiet off
}
inject_debug_quiet() {
local state="$1"
printf $'\\set QUIET %s\n' "$state"
}
fix_problems() {
local line
while IFS= read -r line; do
printf '%s\n' "$line"
case "$line" in
*"$COLL_TABLE_HEADING")
fix_collection_problems
;;
*"$DATA_TABLE_HEADING")
fix_object_problems
;;
*)
;;
esac
done
}
fix_collection_problems() {
pass_hdr_thru
local collField createTime permIssue owner uuidCnt
while IFS='|' read -r permIssue uuidCnt owner createTime collField; do
if [[ -z "$collField" ]]; then
break
fi
local escColl
escColl="$(TRIM "$collField")"
local coll
coll="$(UNESCAPE "$escColl")"
local noPermIssue='false'
if ! permIssue="$(PROCESS_PERM_ISSUE "$permIssue" "$coll")"; then
noPermIssue='true'
fi
local noUUIDIssue='false'
if ! uuidCnt="$(PROCESS_UUID_ISSUE "$uuidCnt" coll "$coll")"; then
noUUIDIssue='true'
fi
if [[ "$noPermIssue" == 'false' || "$noUUIDIssue" == 'false' ]]; then
printf '%s|%s|%s|%s|%s\n' "$permIssue" "$uuidCnt" "$owner" "$createTime" "$collField"
fi
done
}
fix_object_problems() {
pass_hdr_thru
parallel --line-buffer --no-notice --max-args 1 --max-procs "$FIX_CONCURRENCY" \
FIX_SINGLE_OBJECT_PROBLEMS
}
generate_report() {
local cutoffTime="$1"
local fixLog="$2"
local errLog="$3"
printf '\t%s DATA STORE REPAIR REPORT\n\n\n' "$cutoffTime"
printf 'ITEMIZED REPAIR ATTEMPTS\n\n'
local fixLogSize
fixLogSize="$(stat --format '%s' "$fixLog")"
if (( fixLogSize > MAX_LOG_MAIL_SIZE )); then
local reportHost
reportHost="$(hostname --fqdn)"
local reportLog=/tmp/"$EXEC_NAME"@"$cutoffTime".log
mv "$fixLog" "$reportLog"
printf '\n'
printf 'The itemized lists of attempted repairs is too large to send by email. '
printf 'It may be found at %s:%s\n' "$reportHost" "$reportLog"
else
cat "$fixLog"
fi
printf '\n\nREPAIR ATTEMPT FAILURES\n\n'
local errLogSize
errLogSize="$(stat --format '%s' "$errLog")"
if (( errLogSize > MAX_LOG_MAIL_SIZE )); then
local reportHost
reportHost="$(hostname --fqdn)"
local reportErr=/tmp/"$EXEC_NAME"@"$cutoffTime".err
mv "$errLog" "$reportErr"
printf '\n'
printf 'The repair attempt failure log is too large to send by email. '
printf 'It may be found at %s:%s\n' "$reportHost" "$reportErr"
elif (( errLogSize > 0 )); then
cat "$errLog"
else
printf 'All problems were fixed.\n'
fi
}
strip_noise() {
local line
while IFS= read -r line; do
if [[ -n "${DEBUG-}" ]]; then
printf '%s\n' "$line"
else
case "$line" in
BEGIN|CREATE*|SELECT*|ROLLBACK)
;;
*)
printf '%s\n' "$line"
;;
esac
fi
done
}
pass_hdr_thru() {
local line
while read -r line; do
printf '%s\n' "$line"
if [[ "$line" =~ ^- ]]; then
break
fi
done
}
rm_temp_files() {
rm --force "${TempFiles[@]}"
}
FIX_SINGLE_OBJECT_PROBLEMS() {
local permIssue missingChksum uuidCnt owner replField createTime objField
IFS='|' read -r permIssue missingChksum uuidCnt owner replField createTime objField <<< "$@"
if [[ -z "$objField" ]]; then
return 1
fi
local escObj
escObj="$(TRIM "$objField")"
local obj
obj="$(UNESCAPE "$escObj")"
local repl
repl="$(TRIM "$replField")"
local noPermIssue='false'
if ! permIssue="$(PROCESS_PERM_ISSUE "$permIssue" "$obj")"; then
noPermIssue='true'
fi
local noChksumIssue='false'
if ! missingChksum="$(PROCESS_CHKSUM_ISSUE "$missingChksum" "$repl" "$obj")"; then
noChksumIssue='true'
fi
local noUUIDIssue='false'
if ! uuidCnt="$(PROCESS_UUID_ISSUE "$uuidCnt" obj "$obj")"; then
noUUIDIssue='true'
fi
if [[ "$noPermIssue" == 'false' || "$noChksumIssue" == 'false' || "$noUUIDIssue" == 'false' ]]
then
printf '%s|%s|%s|%s|%s|%s|%s\n' \
"$permIssue" "$missingChksum" "$uuidCnt" "$owner" "$replField" "$createTime" "$objField"
fi
}
export -f FIX_SINGLE_OBJECT_PROBLEMS
PROCESS_CHKSUM_ISSUE() {
local issue="$1"
local repl="$2"
local obj="$3"
if [[ "${issue// /}" == f ]]; then
printf '%s' "$issue"
return 1
fi
local err retCode
err="$(ichksum -M --silent -n "$repl" "$obj" 2>&1 > /dev/null)"
retCode="$?"
if [[ "$err" =~ 'does not exist' ]]; then
printf '%s' "${issue/% /👻 }"
return
elif [[ "$err" =~ HIERARCHY_ERROR ]]; then
err="$(FIX_ARCHIVE_CHKSUM "$repl" "$obj")"
retCode=$?
fi
if (( retCode == 0 )) && [[ -z "$err" ]]; then
printf '%s' "${issue/% /✓ }"
else
if [[ -n "$err" ]]; then
echo "$err" >&2
fi
printf '%s' "${issue/% /✗ }"
printf 'FAILED TO GENERATE CHECKSUM!! - %s\n' "$obj" >&2
fi
return 0
}
export -f PROCESS_CHKSUM_ISSUE
FIX_ARCHIVE_CHKSUM() {
local repl="$1"
local dataPath="$2"
local collPath dataName
collPath="$(dirname "$dataPath")"
dataName="$(basename "$dataPath")"
local parentContext
parentContext="$(GET_PARENT_RESC_CONTEXT "$repl" "$collPath" "$dataName")"
if [[ "$parentContext" != archive ]]; then
printf 'Replica %s of %s is not stored in an archival resource\n' "$repl" "$dataPath" >&2
return 1
fi
local chksum
chksum="$(GET_SIBLING_CHKSUM "$collPath" "$dataName")"
if [[ -z "$chksum" ]]; then
printf 'Cannot copy checksum from other replica to archival replica %s of %s\n' \
"$repl" "$dataPath" \
>&2
return 1
fi
iadmin modrepl logical_path "$dataPath" replica_number "$repl" DATA_CHECKSUM "$chksum" \
> /dev/null
}
export -f FIX_ARCHIVE_CHKSUM
GET_PARENT_RESC_CONTEXT() {
local repl="$1"
local collPath="$2"
local dataName="$3"
"$EXEC_DIR"/quest %s <<EOF
select RESC_PARENT_CONTEXT
where COLL_NAME = '$collPath' and DATA_NAME = '$dataName' and DATA_REPL_NUM = '$repl'
EOF
}
export -f GET_PARENT_RESC_CONTEXT
GET_SIBLING_CHKSUM() {
local collPath="$1"
local dataName="$2"
"$EXEC_DIR"/quest %s <<EOF
select DATA_CHECKSUM
where COLL_NAME = '$collPath' and DATA_NAME = '$dataName' and DATA_CHECKSUM != ''
EOF
}
export -f GET_SIBLING_CHKSUM
PROCESS_PERM_ISSUE() {
local issue="$1"
local entity="$2"
if [[ "${issue// /}" == f ]]; then
printf '%s' "$issue"
return 1
fi
local err
if err="$(ichmod -M own rodsadmin "$entity" 2>&1 > /dev/null)"; then
printf '%s' "${issue/% /✓ }"
elif [[ "$err" =~ CAT_INVALID_ARGUMENT ]]; then
printf '%s' "${issue/% /👻 }"
else
printf '%s' "${issue/% /✗ }"
echo "$err" >&2
printf 'FAILED TO ADD RODSADMIN OWN PERMISSION!! - %s\n' "$entity" >&2
fi
return 0
}
export -f PROCESS_PERM_ISSUE
PROCESS_UUID_ISSUE() {
local uuidCntField="$1"
local entityType="$2"
local entity="$3"
uuidCntField="${uuidCnt# }"
declare -i cnt=$uuidCntField
if (( cnt == 1 )); then
printf '%s ' "$uuidCntField"
return 1
fi
local imetaFlag
if [[ "$entityType" == coll ]]; then
local imetaFlag=-C
else
local imetaFlag=-d
fi
local statusMsg
if (( cnt == 0 )); then
statusMsg="$(ADD_MISSING_UUID "$entity" "$imetaFlag")"
else
statusMsg="$(RM_EXTRA_UUIDS "$entity" "$imetaFlag")"
fi
local statusFmt
case "${statusMsg-}" in
SUCCESS)
statusFmt=✓
;;
MISSING)
statusFmt=👻
;;
*)
statusFmt=✗
;;
esac
printf '%s%s ' "$uuidCntField" "$statusFmt"
return 0
}
export -f PROCESS_UUID_ISSUE
ADD_MISSING_UUID() {
local entity="$1"
local imetaFlag="$2"
local uuid
uuid="$(uuidgen -t)"
local err
if err="$(imeta set "$imetaFlag" "$entity" ipc_UUID "$uuid" 2>&1)"; then
printf SUCCESS
else
if [[ "$err" =~ (CAT_UNKNOWN_COLLECTION|CAT_UNKNOWN_FILE) ]]; then
printf MISSING
return 1
else
echo "$err" >&2
printf 'FAILED TO ADD UUID!! - %s\n' "$entity" >&2
fi
fi
}
export -f ADD_MISSING_UUID
RM_EXTRA_UUIDS() {
local entity="$1"
local imetaFlag="$2"
local failed
local uuidMap
if [[ "$imetaFlag" = '-C' ]]; then
uuidMap="$(QUERY_COLL_UUID "$entity")"
else
uuidMap="$(QUERY_DATA_UUID "$(dirname "$entity")" "$(basename "$entity")")"
fi
if [[ -z "$uuidMap" ]]; then
failed=failed
printf MISSING
else
local cnt=0
local uuid
while read -r _ uuid; do
# The first UUID created will have the first DB Id
if (( ++cnt > 1 )); then
if ! imeta rm "$imetaFlag" "$entity" ipc_UUID "$uuid" > /dev/null; then
failed=failed
fi
fi
done <<< "$uuidMap"
fi
if [[ -z "${failed-}" ]]; then
printf SUCCESS
else
printf 'FAILED TO REMOVE EXTRA UUIDS!! - %s\n' "$entity" >&2
fi
}
export -f RM_EXTRA_UUIDS
QUERY_COLL_UUID() {
local collPath="$1"
"$EXEC_DIR"/quest '%s %s' <<EOF
select order(META_COLL_ATTR_ID), META_COLL_ATTR_VALUE
where META_COLL_ATTR_NAME = 'ipc_UUID' and COLL_NAME = '$collPath'"
EOF
}
export -f QUERY_COLL_UUID
QUERY_DATA_UUID() {
local collPath="$1"
local dataName="$2"
"$EXEC_DIR"/quest '%s %s' <<EOF
select order(META_DATA_ATTR_ID), META_DATA_ATTR_VALUE
where META_DATA_ATTR_NAME = 'ipc_UUID' and COLL_NAME = '$collPath' and DATA_NAME = '$dataName'"
EOF
}
export -f QUERY_DATA_UUID
TRIM() {
local str="$*"
str="${str#"${str%%[![:space:]]*}"}"
str="${str%"${str##*[![:space:]]}"}"
printf '%s' "$str"
}
export -f TRIM
UNESCAPE() {
local escEntity="$*"
local entity=
local escaped=0
for i in $(seq 0 $(( ${#escEntity} - 1 ))); do
local curChar="${escEntity:$i:1}"
if (( escaped == 1 )); then
if [[ "$curChar" =~ (n|r) ]]; then
# shellcheck disable=SC1003
printf -v entity '%s\'"$curChar" "$entity"
else
entity="$entity$curChar"
fi
escaped=0
else
if [[ "$curChar" == \\ ]]; then
escaped=1
else
entity="$entity$curChar"
fi
fi
done
printf '%s' "$entity"
}
export -f UNESCAPE
main "$@"