Repository navigation
Expand file tree
/
Copy pathfm-pr-lib.sh
More file actions
executable file
·1596 lines (1523 loc) · 66.4 KB
/
Copy pathfm-pr-lib.sh
File metadata and controls
executable file
·1596 lines (1523 loc) · 66.4 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
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
#!/usr/bin/env bash
# Shared PR/MR record reads, validation, and atomic artifact helpers for merge
# polling on the supported forges. Callers must validate task IDs and raw PR/MR
# URLs before constructing task paths or performing any side effect.
#
# The stored identity is provider-tagged: provider, url, host, path, number.
# "path" is the full project path, which is owner/repository on GitHub and
# Forgejo, an arbitrarily nested group/subgroup/project namespace on GitLab, and
# an arbitrarily nested project name on Gerrit, where "number" is the change
# number. A GitLab or Gerrit project can sit at any depth, so no
# owner/repository pair can address one and the sidecar carries the whole path
# instead. GitLab, Gerrit, and Forgejo all run on self-hosted instances, and
# Gerrit runs nowhere else, so the host is part of that identity rather than a
# constant. GitHub runs on github.com and on GitHub Enterprise (Cloud *.ghe.com
# and Enterprise Server), so its host is part of that identity too. Every
# consumer re-derives the identity
# from the stored URL and refuses any record whose parts do not reconstruct that
# exact URL.
#
# A validated exact merged result is retired through a private receipt only
# after its durable wake is appended.
# The receipt binds the terminal observation to the canonical registration and
# lets a restart finish fixed-path removal without executing state-file bytes.
_FM_PR_LIB_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
FM_PR_PROVIDER=
FM_PR_URL=
FM_PR_HOST=
FM_PR_PATH=
FM_PR_OWNER=
FM_PR_REPO=
FM_PR_NUMBER=
FM_PR_DATA_PROVIDER=
FM_PR_DATA_URL=
FM_PR_DATA_HOST=
FM_PR_DATA_PATH=
FM_PR_DATA_NUMBER=
FM_PR_META_PROVIDER=
FM_PR_META_URL=
FM_PR_META_HOST=
FM_PR_META_PATH=
FM_PR_META_NUMBER=
FM_PR_REG_ID=
FM_PR_REG_PROVIDER=
FM_PR_REG_URL=
FM_PR_REG_HOST=
FM_PR_REG_PATH=
FM_PR_REG_NUMBER=
FM_PR_REG_DATA_HASH=
FM_PR_REG_TEMPLATE_HASH=
FM_PR_REG_DATA_IDENTITY=
FM_PR_REG_CHECK_IDENTITY=
FM_PR_POLL_DATA_TMP=
FM_PR_POLL_CHECK_TMP=
FM_PR_POLL_REG_TMP=
FM_PR_POLL_DATA_DEST=
FM_PR_POLL_CHECK_DEST=
FM_PR_POLL_REG_DEST=
FM_PR_POLL_EXPECT_ID=
FM_PR_POLL_EXPECT_PROVIDER=
FM_PR_POLL_EXPECT_URL=
FM_PR_POLL_EXPECT_HOST=
FM_PR_POLL_EXPECT_PATH=
FM_PR_POLL_EXPECT_NUMBER=
FM_PR_POLL_EXPECT_DATA_HASH=
FM_PR_POLL_EXPECT_TEMPLATE_HASH=
FM_PR_POLL_EXPECT_DATA_IDENTITY=
FM_PR_POLL_EXPECT_CHECK_IDENTITY=
FM_PR_POLL_TEMPLATE=
FM_PR_POLL_STATE_DEVICE=
FM_PR_POLL_SNAPSHOT_ID=
FM_PR_POLL_SNAPSHOT_PROVIDER=
FM_PR_POLL_SNAPSHOT_URL=
FM_PR_POLL_SNAPSHOT_HOST=
FM_PR_POLL_SNAPSHOT_PATH=
FM_PR_POLL_SNAPSHOT_NUMBER=
FM_PR_POLL_SNAPSHOT_DATA_HASH=
FM_PR_POLL_SNAPSHOT_TEMPLATE_HASH=
FM_PR_POLL_SNAPSHOT_DATA_IDENTITY=
FM_PR_POLL_SNAPSHOT_CHECK_IDENTITY=
FM_PR_POLL_SNAPSHOT_REG_HASH=
FM_PR_POLL_SNAPSHOT_REG_IDENTITY=
FM_PR_POLL_REARM_DATA_IDENTITY=
FM_PR_POLL_REARM_CHECK_IDENTITY=
FM_PR_RETIRE_ID=
FM_PR_RETIRE_PROVIDER=
FM_PR_RETIRE_URL=
FM_PR_RETIRE_HOST=
FM_PR_RETIRE_PATH=
FM_PR_RETIRE_NUMBER=
FM_PR_RETIRE_DATA_HASH=
FM_PR_RETIRE_TEMPLATE_HASH=
FM_PR_RETIRE_DATA_IDENTITY=
FM_PR_RETIRE_CHECK_IDENTITY=
FM_PR_RETIRE_REG_HASH=
FM_PR_RETIRE_REG_IDENTITY=
FM_PR_RETIRE_RECEIPT_HASH=
FM_PR_RETIRE_RECEIPT_IDENTITY=
FM_PR_RECORD_STATE=
FM_PR_RECORD_MERGED=
FM_PR_POLL_RETIREMENT_REJECTED=
fm_task_id_path_safe() {
local id=${1-}
local LC_ALL=C
case "$id" in
''|.*|*[!A-Za-z0-9._-]*) return 1 ;;
esac
}
fm_pr_task_id_valid() {
local id=${1-}
fm_task_id_path_safe "$id"
}
fm_task_id_creation_valid() {
local id=${1-}
fm_pr_task_id_valid "$id" || return 1
[ "${#id}" -le 64 ]
}
# GitLab, Gerrit, and Forgejo all serve self-hosted instances, so the host is part of the
# identity rather than a constant. It is accepted only as a lowercase DNS name
# with no userinfo, port, or trailing dot, which keeps one canonical spelling per
# change. github.com is refused here even though its shape is otherwise valid:
# it is GitHub's own host and never another forge's instance, so a URL like
# https://github.com/o/r/-/merge_requests/1 (a typo'd or spoofed GitHub URL)
# would otherwise be armed as a watch that can never succeed.
fm_pr_forge_host_valid() {
local host=${1-} label
local LC_ALL=C
local -a labels
[ "${#host}" -ge 1 ] && [ "${#host}" -le 253 ] || return 1
[ "$host" != github.com ] || return 1
case "$host" in
.*|*.|*..*|*[!a-z0-9.-]*) return 1 ;;
esac
IFS=. read -ra labels <<< "$host"
for label in "${labels[@]}"; do
[ "${#label}" -ge 1 ] && [ "${#label}" -le 63 ] || return 1
case "$label" in
-*|*-) return 1 ;;
esac
done
}
# A GitLab project path is group[/subgroup...]/project, so at least two
# segments and no fixed depth. GitLab reserves "-" as its route separator and
# forbids a leading hyphen, ".git", and ".atom", so none of those can name a
# real namespace and each is refused here.
fm_pr_gitlab_path_valid() {
local path=${1-} segment
local LC_ALL=C
local -a segments
[ "${#path}" -ge 3 ] && [ "${#path}" -le 1024 ] || return 1
case "$path" in
/*|*/|*//*) return 1 ;;
esac
IFS=/ read -ra segments <<< "$path"
[ "${#segments[@]}" -ge 2 ] && [ "${#segments[@]}" -le 20 ] || return 1
for segment in "${segments[@]}"; do
[ "${#segment}" -ge 1 ] && [ "${#segment}" -le 255 ] || return 1
case "$segment" in
.|..|-*|*.git|*.atom|*[!A-Za-z0-9._-]*) return 1 ;;
esac
done
}
# A Gerrit project name is itself a path at no fixed depth, and it needs no
# enclosing group, so a single segment is canonical here where GitLab needs at
# least two. Gerrit reserves no route segment inside the name, so nothing
# corresponds to GitLab's "-": the change URL's literal "/+/" is what ends the
# project instead. A ".git" suffix is refused because Gerrit strips it and the
# stripped name is the canonical one, and a leading hyphen is refused because a
# project path is what names the project to any CLI that takes one, where a
# leading hyphen reads as an option instead.
fm_pr_gerrit_path_valid() {
local path=${1-} segment
local LC_ALL=C
local -a segments
[ "${#path}" -ge 1 ] && [ "${#path}" -le 1024 ] || return 1
case "$path" in
/*|*/|*//*) return 1 ;;
esac
IFS=/ read -ra segments <<< "$path"
[ "${#segments[@]}" -ge 1 ] && [ "${#segments[@]}" -le 20 ] || return 1
for segment in "${segments[@]}"; do
[ "${#segment}" -ge 1 ] && [ "${#segment}" -le 255 ] || return 1
case "$segment" in
.|..|-*|*.git|*[!A-Za-z0-9._-]*) return 1 ;;
esac
done
}
# Unlike GitLab, a Forgejo/Gitea project has no subgroup nesting: it is always
# exactly owner/repository, so each segment is validated the same way GitHub's
# owner and repo are, minus GitHub's specific hyphen-run and length rules.
fm_pr_forgejo_segment_valid() {
local segment=${1-}
local LC_ALL=C
[ "${#segment}" -ge 1 ] && [ "${#segment}" -le 100 ] || return 1
case "$segment" in
.|..|*.git|*[!A-Za-z0-9._-]*) return 1 ;;
esac
}
# A GitHub pull request URL names its host, and github.com is the only GitHub
# host accepted without being declared: every other host must be listed in this
# home's config/github-hosts (fm_github_host_allowed), so a look-alike such as
# github.com.evil or evilgithub.com is refused rather than armed as a watch that
# can never succeed. That list is the captain's own statement that a host is a
# GitHub host this home uses, which no URL shape can prove on its own.
# docs/configuration.md "GitHub Enterprise hosts" owns the file format.
# An unreadable list, an absent one, and a host it does not name all refuse.
# Only an exact line match authorizes, so a malformed or comment line can never
# authorize anything and needs no separate validation.
fm_github_host_allowed() { # <host>
local host=${1-} file line
local LC_ALL=C
[ -n "$host" ] || return 1
[ "$host" != github.com ] || return 0
fm_pr_forge_host_valid "$host" || return 1
file="${FM_CONFIG_OVERRIDE:-${FM_HOME:-$_FM_PR_LIB_ROOT}/config}/github-hosts"
[ -f "$file" ] && [ ! -L "$file" ] && [ -r "$file" ] || return 1
while IFS= read -r line || [ -n "$line" ]; do
[ "$line" = "$host" ] && return 0
done < "$file"
return 1
}
# gh and gh-axi address the host in GH_HOST, from the pull request URL, or from
# the clone's own origin, in that order of specificity. A GitHub Enterprise host
# is not any tool's default one, so every GitHub call on a pull request path
# exports it once through here and its children inherit it. github.com keeps the
# ambient behavior it always had, byte for byte.
fm_pr_github_host_export() { # <host>
case ${1-} in
''|github.com) return 0 ;;
esac
GH_HOST=$1
export GH_HOST
}
# Parse a canonical pull request, merge request, or Gerrit change URL into the
# provider-tagged identity. Validation is strict and per provider: the GitHub
# username and repository rules are unchanged, and GitLab, Gerrit, and Forgejo
# each get their own namespace rules rather than a loosened GitHub rule.
#
# FM_PR_OWNER and FM_PR_REPO are additionally set for github and forgejo
# because bin/fm-pr-merge.sh addresses both by owner/repository. A gitlab or
# gerrit URL leaves them empty, and those paths address the project by FM_PR_HOST and
# FM_PR_PATH instead, so a change on any instance resolves without a hardcoded
# host.
fm_pr_url_parse() {
local raw=${1-} pattern host path
local LC_ALL=C
FM_PR_PROVIDER=
FM_PR_URL=
FM_PR_HOST=
FM_PR_PATH=
FM_PR_OWNER=
FM_PR_REPO=
FM_PR_NUMBER=
pattern='^https://([a-z0-9.-]{1,253})/([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]{0,37}[A-Za-z0-9])/([A-Za-z0-9._-]{1,100})/pull/([1-9][0-9]*)$'
if [[ "$raw" =~ $pattern ]]; then
host=${BASH_REMATCH[1]}
fm_github_host_allowed "$host" || return 1
[[ "${BASH_REMATCH[2]}" != *--* ]] || return 1
[ "${BASH_REMATCH[3]}" != . ] && [ "${BASH_REMATCH[3]}" != .. ] || return 1
FM_PR_PROVIDER=github
FM_PR_URL=$raw
FM_PR_HOST=$host
FM_PR_PATH="${BASH_REMATCH[2]}/${BASH_REMATCH[3]}"
# Consumed by bin/fm-pr-merge.sh, which addresses GitHub by owner/repository.
# shellcheck disable=SC2034
FM_PR_OWNER=${BASH_REMATCH[2]}
# shellcheck disable=SC2034
FM_PR_REPO=${BASH_REMATCH[3]}
FM_PR_NUMBER=${BASH_REMATCH[4]}
return 0
fi
# The path class contains "/" and "-", so this match is greedy to the last
# "/-/merge_requests/". Any earlier separator therefore lands inside the
# captured path, where the reserved "-" segment is refused.
pattern='^https://([a-z0-9.-]{1,253})/([A-Za-z0-9._/-]+)/-/merge_requests/([1-9][0-9]*)$'
if [[ "$raw" =~ $pattern ]]; then
host=${BASH_REMATCH[1]}
path=${BASH_REMATCH[2]}
fm_pr_forge_host_valid "$host" || return 1
fm_pr_gitlab_path_valid "$path" || return 1
FM_PR_PROVIDER=gitlab
FM_PR_URL=$raw
FM_PR_HOST=$host
FM_PR_PATH=$path
FM_PR_NUMBER=${BASH_REMATCH[3]}
return 0
fi
# A Gerrit change URL is https://<host>/c/<project>/+/<number>. "+" is outside
# the path class, so the project can never contain the "/+/" separator and this
# match needs no greediness argument: a second "/+/" makes the URL match
# nothing rather than splitting somewhere else. The project keeps its whole
# nested path for the same reason GitLab's does, so it is never flattened into
# an owner/repository pair that cannot address it.
pattern='^https://([a-z0-9.-]{1,253})/c/([A-Za-z0-9._/-]+)/\+/([1-9][0-9]*)$'
if [[ "$raw" =~ $pattern ]]; then
host=${BASH_REMATCH[1]}
path=${BASH_REMATCH[2]}
fm_pr_forge_host_valid "$host" || return 1
fm_pr_gerrit_path_valid "$path" || return 1
FM_PR_PROVIDER=gerrit
FM_PR_URL=$raw
FM_PR_HOST=$host
FM_PR_PATH=$path
FM_PR_NUMBER=${BASH_REMATCH[3]}
return 0
fi
# Forgejo/Gitea serve pulls at owner/repo/pulls/<n>: plural "pulls", no "/-/"
# route separator and no "/+/" change separator, so this shape never collides
# with the GitLab or Gerrit patterns above.
pattern='^https://([a-z0-9.-]{1,253})/([A-Za-z0-9._-]{1,100})/([A-Za-z0-9._-]{1,100})/pulls/([1-9][0-9]*)$'
[[ "$raw" =~ $pattern ]] || return 1
host=${BASH_REMATCH[1]}
fm_pr_forge_host_valid "$host" || return 1
fm_pr_forgejo_segment_valid "${BASH_REMATCH[2]}" || return 1
fm_pr_forgejo_segment_valid "${BASH_REMATCH[3]}" || return 1
FM_PR_PROVIDER=forgejo
FM_PR_URL=$raw
FM_PR_HOST=$host
FM_PR_PATH="${BASH_REMATCH[2]}/${BASH_REMATCH[3]}"
# Consumed by bin/fm-pr-merge.sh, which addresses Forgejo by owner/repository.
# shellcheck disable=SC2034
FM_PR_OWNER=${BASH_REMATCH[2]}
# shellcheck disable=SC2034
FM_PR_REPO=${BASH_REMATCH[3]}
FM_PR_NUMBER=${BASH_REMATCH[4]}
}
fm_pr_head_valid() {
local head=${1-}
local LC_ALL=C
[[ "$head" =~ ^[0-9a-f]{40}$|^[0-9a-f]{64}$ ]]
}
# Resolve the PR number for a task copy's branch via gh-axi, run inside that
# copy so gh-axi resolves the copy's own repository. Echoes the number on a
# single match and returns 0; returns non-zero on no match or any lookup
# failure, so callers treat it as "no PR found" (fail-safe). bin/fm-teardown.sh
# reads any state for its landed check; bin/fm-pr-health.sh reads open PRs to
# enroll a branch whose PR was never recorded.
fm_pr_number_from_branch() { # <worktree> <branch> [open|closed|all]
local wt=$1 branch=$2 state=${3:-all} out n
[ -n "$branch" ] && [ "$branch" != HEAD ] || return 1
out=$( cd "$wt" && gh-axi pr list --state "$state" --head "$branch" --limit 1 2>/dev/null ) || return 1
n=$(printf '%s\n' "$out" | sed -n 's/^[[:space:]]*\([0-9][0-9]*\),.*/\1/p' | head -1)
[ -n "$n" ] || return 1
printf '%s' "$n"
}
# The one reading of a GitHub pull request's draft state. Prints "true" or
# "false" for a boolean isDraft and nothing for anything else, so a caller can
# tell a positive draft from an unreadable payload. bin/fm-pr-merge.sh refuses
# a merge unless this prints "false"; bin/fm-pr-check.sh refuses to arm a merge
# poll only when it prints "true".
fm_pr_json_draft_state() { # <pull-request-json>
printf '%s' "${1-}" | jq -r '
if type == "object" and (.isDraft | type) == "boolean" then (.isDraft | tostring) else "" end
' 2>/dev/null || true
}
fm_pr_file_mode() {
if [ "$(uname)" = Darwin ]; then
/usr/bin/stat -f %Lp "$1" 2>/dev/null
else
stat -c %a "$1" 2>/dev/null
fi
}
fm_pr_file_device() {
if [ "$(uname)" = Darwin ]; then
/usr/bin/stat -f %d "$1" 2>/dev/null
else
stat -c %d "$1" 2>/dev/null
fi
}
fm_pr_file_link_count() {
if [ "$(uname)" = Darwin ]; then
/usr/bin/stat -f %l "$1" 2>/dev/null
else
stat -c %h "$1" 2>/dev/null
fi
}
fm_pr_file_inode() {
if [ "$(uname)" = Darwin ]; then
/usr/bin/stat -f %i "$1" 2>/dev/null
else
stat -c %i "$1" 2>/dev/null
fi
}
# device:inode names one file object, since an inode number is unique only
# within its filesystem. The device part is not stable across a volume remount:
# APFS can renumber st_dev on reboot while every inode and byte is unchanged, so
# an identity persisted before the remount no longer matches the live file
# (fm_pr_poll_registration_rerecord_device).
fm_pr_file_identity() {
local device inode
device=$(fm_pr_file_device "$1") || return 1
inode=$(fm_pr_file_inode "$1") || return 1
[ -n "$device" ] && [ -n "$inode" ] || return 1
printf '%s:%s\n' "$device" "$inode"
}
fm_pr_sha256() {
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" 2>/dev/null | awk '{print $1}'
elif command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" 2>/dev/null | awk '{print $1}'
else
return 1
fi
}
# Callers pass the containing directory's device read in the same invocation,
# never a persisted one, so this compares two live readings and survives a
# remount that renumbers the volume. It refuses a file that is not on that
# directory's own filesystem, such as one bind-mounted over the name, which is
# also what keeps same-directory rename publication atomic.
fm_pr_private_file_valid() {
local path=$1 mode=$2 device=$3
[ -f "$path" ] && [ ! -L "$path" ] || return 1
[ "$(fm_pr_file_mode "$path")" = "$mode" ] || return 1
[ "$(fm_pr_file_device "$path")" = "$device" ] || return 1
[ "$(fm_pr_file_link_count "$path")" = 1 ]
}
fm_pr_regular_destination_or_absent() {
local path=$1
[ ! -L "$path" ] || return 1
if [ -e "$path" ]; then
[ -f "$path" ] && [ "$(fm_pr_file_link_count "$path")" = 1 ]
fi
}
fm_pr_regular_destination_on_device_or_absent() {
local path=$1 device=$2
fm_pr_regular_destination_or_absent "$path" || return 1
[ ! -e "$path" ] || [ "$(fm_pr_file_device "$path")" = "$device" ]
}
fm_pr_metadata_identity_parse() {
local file=$1 line value pr_count=0 seen_pr=0 post_pr_invalid=0
FM_PR_META_PROVIDER=
FM_PR_META_URL=
FM_PR_META_HOST=
FM_PR_META_PATH=
FM_PR_META_NUMBER=
[ -f "$file" ] && [ ! -L "$file" ] || return 1
[ "$(fm_pr_file_link_count "$file")" = 1 ] || return 1
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
pr=*)
pr_count=$((pr_count + 1))
[ "$pr_count" -eq 1 ] || continue
value=${line#pr=}
if fm_pr_url_parse "$value"; then
FM_PR_META_PROVIDER=$FM_PR_PROVIDER
FM_PR_META_URL=$FM_PR_URL
FM_PR_META_HOST=$FM_PR_HOST
FM_PR_META_PATH=$FM_PR_PATH
FM_PR_META_NUMBER=$FM_PR_NUMBER
fi
seen_pr=1
;;
pr_head=*)
if [ "$seen_pr" -eq 1 ]; then
value=${line#pr_head=}
fm_pr_head_valid "$value" || post_pr_invalid=1
fi
;;
x_request=*|x_request_ts=*|x_followups=*|x_platform=*|x_reply_max_chars=*)
;;
control_relaunch_tx=*)
# fm-spawn appends this after pr= on a control relaunch (robots-paf4m).
;;
*)
[ "$seen_pr" -eq 0 ] || post_pr_invalid=1
;;
esac
done < "$file"
[ "$pr_count" -eq 1 ] || return 1
[ "$post_pr_invalid" -eq 0 ] || return 1
[ -n "$FM_PR_META_URL" ]
}
# Sidecar layout: provider, url, host, path, number, one per line. A sidecar
# written before the provider tag existed has a URL on its first line and one
# line fewer, so it fails both the field count and the provider comparison and
# is refused rather than misread as a provider-tagged record.
fm_pr_poll_data_parse() {
local file=$1 provider url host path number
FM_PR_DATA_PROVIDER=
FM_PR_DATA_URL=
FM_PR_DATA_HOST=
FM_PR_DATA_PATH=
FM_PR_DATA_NUMBER=
[ -f "$file" ] && [ ! -L "$file" ] || return 1
exec 8< "$file" || return 1
IFS= read -r provider <&8 || { exec 8<&-; return 1; }
IFS= read -r url <&8 || { exec 8<&-; return 1; }
IFS= read -r host <&8 || { exec 8<&-; return 1; }
IFS= read -r path <&8 || { exec 8<&-; return 1; }
IFS= read -r number <&8 || { exec 8<&-; return 1; }
if IFS= read -r _extra <&8; then
exec 8<&-
return 1
fi
exec 8<&-
fm_pr_url_parse "$url" || return 1
[ "$provider" = "$FM_PR_PROVIDER" ] || return 1
[ "$host" = "$FM_PR_HOST" ] || return 1
[ "$path" = "$FM_PR_PATH" ] || return 1
[ "$number" = "$FM_PR_NUMBER" ] || return 1
FM_PR_DATA_PROVIDER=$FM_PR_PROVIDER
FM_PR_DATA_URL=$FM_PR_URL
FM_PR_DATA_HOST=$FM_PR_HOST
FM_PR_DATA_PATH=$FM_PR_PATH
FM_PR_DATA_NUMBER=$FM_PR_NUMBER
}
# Registration layout: version tag, task id, then the same provider-tagged
# identity as the sidecar, then the two hashes and the two file identities.
# The version tag moved to v2 with the provider tag, so a registration written
# by the previous release is recognised as old and refused.
fm_pr_poll_registration_parse() {
local file=$1 version id provider url host path number data_hash template_hash data_identity check_identity
FM_PR_REG_ID=
FM_PR_REG_PROVIDER=
FM_PR_REG_URL=
FM_PR_REG_HOST=
FM_PR_REG_PATH=
FM_PR_REG_NUMBER=
FM_PR_REG_DATA_HASH=
FM_PR_REG_TEMPLATE_HASH=
FM_PR_REG_DATA_IDENTITY=
FM_PR_REG_CHECK_IDENTITY=
[ -f "$file" ] && [ ! -L "$file" ] || return 1
exec 7< "$file" || return 1
IFS= read -r version <&7 || { exec 7<&-; return 1; }
IFS= read -r id <&7 || { exec 7<&-; return 1; }
IFS= read -r provider <&7 || { exec 7<&-; return 1; }
IFS= read -r url <&7 || { exec 7<&-; return 1; }
IFS= read -r host <&7 || { exec 7<&-; return 1; }
IFS= read -r path <&7 || { exec 7<&-; return 1; }
IFS= read -r number <&7 || { exec 7<&-; return 1; }
IFS= read -r data_hash <&7 || { exec 7<&-; return 1; }
IFS= read -r template_hash <&7 || { exec 7<&-; return 1; }
IFS= read -r data_identity <&7 || { exec 7<&-; return 1; }
IFS= read -r check_identity <&7 || { exec 7<&-; return 1; }
if IFS= read -r _extra <&7; then
exec 7<&-
return 1
fi
exec 7<&-
[ "$version" = fm-pr-poll-registration-v2 ] || return 1
fm_pr_task_id_valid "$id" || return 1
fm_pr_url_parse "$url" || return 1
[ "$provider" = "$FM_PR_PROVIDER" ] || return 1
[ "$host" = "$FM_PR_HOST" ] || return 1
[ "$path" = "$FM_PR_PATH" ] || return 1
[ "$number" = "$FM_PR_NUMBER" ] || return 1
[[ "$data_hash" =~ ^[0-9a-f]{64}$ ]] || return 1
[[ "$template_hash" =~ ^[0-9a-f]{64}$ ]] || return 1
[[ "$data_identity" =~ ^[0-9]+:[0-9]+$ ]] || return 1
[[ "$check_identity" =~ ^[0-9]+:[0-9]+$ ]] || return 1
FM_PR_REG_ID=$id
FM_PR_REG_PROVIDER=$FM_PR_PROVIDER
FM_PR_REG_URL=$FM_PR_URL
FM_PR_REG_HOST=$FM_PR_HOST
FM_PR_REG_PATH=$FM_PR_PATH
FM_PR_REG_NUMBER=$FM_PR_NUMBER
FM_PR_REG_DATA_HASH=$data_hash
FM_PR_REG_TEMPLATE_HASH=$template_hash
FM_PR_REG_DATA_IDENTITY=$data_identity
FM_PR_REG_CHECK_IDENTITY=$check_identity
}
fm_pr_poll_cleanup() {
[ -z "$FM_PR_POLL_DATA_TMP" ] || rm -f -- "$FM_PR_POLL_DATA_TMP"
[ -z "$FM_PR_POLL_CHECK_TMP" ] || rm -f -- "$FM_PR_POLL_CHECK_TMP"
[ -z "$FM_PR_POLL_REG_TMP" ] || rm -f -- "$FM_PR_POLL_REG_TMP"
FM_PR_POLL_DATA_TMP=
FM_PR_POLL_CHECK_TMP=
FM_PR_POLL_REG_TMP=
}
fm_pr_poll_revoke_final() {
local failed=0
# Neutralize the runnable name first so a failed rearm cannot consume state
# whose transactional registration did not commit successfully.
if [ -e "$FM_PR_POLL_CHECK_DEST" ] || [ -L "$FM_PR_POLL_CHECK_DEST" ]; then
rm -f -- "$FM_PR_POLL_CHECK_DEST" || failed=1
fi
if [ -e "$FM_PR_POLL_REG_DEST" ] || [ -L "$FM_PR_POLL_REG_DEST" ]; then
rm -f -- "$FM_PR_POLL_REG_DEST" || failed=1
fi
if [ -e "$FM_PR_POLL_DATA_DEST" ] || [ -L "$FM_PR_POLL_DATA_DEST" ]; then
rm -f -- "$FM_PR_POLL_DATA_DEST" || failed=1
fi
[ ! -e "$FM_PR_POLL_CHECK_DEST" ] && [ ! -L "$FM_PR_POLL_CHECK_DEST" ] || failed=1
[ ! -e "$FM_PR_POLL_REG_DEST" ] && [ ! -L "$FM_PR_POLL_REG_DEST" ] || failed=1
[ ! -e "$FM_PR_POLL_DATA_DEST" ] && [ ! -L "$FM_PR_POLL_DATA_DEST" ] || failed=1
return "$failed"
}
fm_pr_poll_prepare() {
local state=$1 id=$2 provider=$3 url=$4 host=$5 path=$6 number=$7 template=$8
fm_pr_task_id_valid "$id" || return 1
fm_pr_url_parse "$url" || return 1
[ "$provider" = "$FM_PR_PROVIDER" ] || return 1
[ "$host" = "$FM_PR_HOST" ] || return 1
[ "$path" = "$FM_PR_PATH" ] || return 1
[ "$number" = "$FM_PR_NUMBER" ] || return 1
[ -f "$template" ] || return 1
[ ! -L "$state" ] || return 1
mkdir -p "$state" || return 1
[ -d "$state" ] && [ ! -L "$state" ] || return 1
umask 077
FM_PR_POLL_DATA_DEST="$state/$id.pr-poll"
FM_PR_POLL_CHECK_DEST="$state/$id.check.sh"
FM_PR_POLL_REG_DEST="$state/$id.pr-poll-registration"
FM_PR_POLL_EXPECT_ID=$id
FM_PR_POLL_EXPECT_PROVIDER=$provider
FM_PR_POLL_EXPECT_URL=$url
FM_PR_POLL_EXPECT_HOST=$host
FM_PR_POLL_EXPECT_PATH=$path
FM_PR_POLL_EXPECT_NUMBER=$number
FM_PR_POLL_TEMPLATE=$template
FM_PR_POLL_STATE_DEVICE=$(fm_pr_file_device "$state") || return 1
[ -n "$FM_PR_POLL_STATE_DEVICE" ] || return 1
FM_PR_POLL_DATA_TMP=$(mktemp "$state/.fm-pr-poll-data.XXXXXX") || return 1
FM_PR_POLL_CHECK_TMP=$(mktemp "$state/.fm-pr-poll-check.XXXXXX") || {
fm_pr_poll_cleanup
return 1
}
FM_PR_POLL_REG_TMP=$(mktemp "$state/.fm-pr-poll-registration.XXXXXX") || {
fm_pr_poll_cleanup
return 1
}
if ! printf '%s\n%s\n%s\n%s\n%s\n' "$provider" "$url" "$host" "$path" "$number" > "$FM_PR_POLL_DATA_TMP" \
|| ! chmod 0600 "$FM_PR_POLL_DATA_TMP" \
|| ! fm_pr_private_file_valid "$FM_PR_POLL_DATA_TMP" 600 "$FM_PR_POLL_STATE_DEVICE" \
|| ! fm_pr_poll_data_parse "$FM_PR_POLL_DATA_TMP" \
|| [ "$FM_PR_DATA_PROVIDER" != "$provider" ] \
|| [ "$FM_PR_DATA_URL" != "$url" ] \
|| [ "$FM_PR_DATA_HOST" != "$host" ] \
|| [ "$FM_PR_DATA_PATH" != "$path" ] \
|| [ "$FM_PR_DATA_NUMBER" != "$number" ] \
|| ! cp "$template" "$FM_PR_POLL_CHECK_TMP" \
|| ! chmod 0600 "$FM_PR_POLL_CHECK_TMP" \
|| ! fm_pr_private_file_valid "$FM_PR_POLL_CHECK_TMP" 600 "$FM_PR_POLL_STATE_DEVICE" \
|| ! cmp -s "$template" "$FM_PR_POLL_CHECK_TMP"; then
fm_pr_poll_cleanup
return 1
fi
FM_PR_POLL_EXPECT_DATA_HASH=$(fm_pr_sha256 "$FM_PR_POLL_DATA_TMP") || { fm_pr_poll_cleanup; return 1; }
FM_PR_POLL_EXPECT_TEMPLATE_HASH=$(fm_pr_sha256 "$FM_PR_POLL_CHECK_TMP") || { fm_pr_poll_cleanup; return 1; }
FM_PR_POLL_EXPECT_DATA_IDENTITY=$(fm_pr_file_identity "$FM_PR_POLL_DATA_TMP") || { fm_pr_poll_cleanup; return 1; }
FM_PR_POLL_EXPECT_CHECK_IDENTITY=$(fm_pr_file_identity "$FM_PR_POLL_CHECK_TMP") || { fm_pr_poll_cleanup; return 1; }
if ! printf '%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n' \
fm-pr-poll-registration-v2 "$id" "$provider" "$url" "$host" "$path" "$number" \
"$FM_PR_POLL_EXPECT_DATA_HASH" "$FM_PR_POLL_EXPECT_TEMPLATE_HASH" \
"$FM_PR_POLL_EXPECT_DATA_IDENTITY" "$FM_PR_POLL_EXPECT_CHECK_IDENTITY" \
> "$FM_PR_POLL_REG_TMP" \
|| ! chmod 0600 "$FM_PR_POLL_REG_TMP" \
|| ! fm_pr_private_file_valid "$FM_PR_POLL_REG_TMP" 600 "$FM_PR_POLL_STATE_DEVICE" \
|| ! fm_pr_poll_registration_parse "$FM_PR_POLL_REG_TMP" \
|| [ "$FM_PR_REG_ID" != "$id" ] \
|| [ "$FM_PR_REG_DATA_HASH" != "$FM_PR_POLL_EXPECT_DATA_HASH" ] \
|| [ "$FM_PR_REG_TEMPLATE_HASH" != "$FM_PR_POLL_EXPECT_TEMPLATE_HASH" ]; then
fm_pr_poll_cleanup
return 1
fi
}
# The caller holds the task's poll publication lock while publishing this
# prepared generation, so no registration can name another generation's files.
fm_pr_poll_publish_prepared() {
[ -n "$FM_PR_POLL_DATA_TMP" ] && [ -n "$FM_PR_POLL_CHECK_TMP" ] \
&& [ -n "$FM_PR_POLL_REG_TMP" ] || return 1
fm_pr_regular_destination_on_device_or_absent "$FM_PR_POLL_DATA_DEST" "$FM_PR_POLL_STATE_DEVICE" || return 1
fm_pr_regular_destination_on_device_or_absent "$FM_PR_POLL_REG_DEST" "$FM_PR_POLL_STATE_DEVICE" || return 1
fm_pr_regular_destination_on_device_or_absent "$FM_PR_POLL_CHECK_DEST" "$FM_PR_POLL_STATE_DEVICE" || return 1
if ! mv -f -- "$FM_PR_POLL_DATA_TMP" "$FM_PR_POLL_DATA_DEST"; then
fm_pr_poll_revoke_final || true
return 1
fi
FM_PR_POLL_DATA_TMP=
if ! fm_pr_private_file_valid "$FM_PR_POLL_DATA_DEST" 600 "$FM_PR_POLL_STATE_DEVICE" \
|| [ "$(fm_pr_file_identity "$FM_PR_POLL_DATA_DEST")" != "$FM_PR_POLL_EXPECT_DATA_IDENTITY" ] \
|| [ "$(fm_pr_sha256 "$FM_PR_POLL_DATA_DEST")" != "$FM_PR_POLL_EXPECT_DATA_HASH" ] \
|| ! fm_pr_poll_data_parse "$FM_PR_POLL_DATA_DEST" \
|| [ "$FM_PR_DATA_PROVIDER" != "$FM_PR_POLL_EXPECT_PROVIDER" ] \
|| [ "$FM_PR_DATA_URL" != "$FM_PR_POLL_EXPECT_URL" ] \
|| [ "$FM_PR_DATA_HOST" != "$FM_PR_POLL_EXPECT_HOST" ] \
|| [ "$FM_PR_DATA_PATH" != "$FM_PR_POLL_EXPECT_PATH" ] \
|| [ "$FM_PR_DATA_NUMBER" != "$FM_PR_POLL_EXPECT_NUMBER" ]; then
fm_pr_poll_revoke_final || true
return 1
fi
if ! mv -f -- "$FM_PR_POLL_REG_TMP" "$FM_PR_POLL_REG_DEST"; then
fm_pr_poll_revoke_final || true
return 1
fi
FM_PR_POLL_REG_TMP=
if ! fm_pr_private_file_valid "$FM_PR_POLL_REG_DEST" 600 "$FM_PR_POLL_STATE_DEVICE" \
|| ! fm_pr_poll_registration_parse "$FM_PR_POLL_REG_DEST" \
|| [ "$FM_PR_REG_ID" != "$FM_PR_POLL_EXPECT_ID" ] \
|| [ "$FM_PR_REG_PROVIDER" != "$FM_PR_POLL_EXPECT_PROVIDER" ] \
|| [ "$FM_PR_REG_URL" != "$FM_PR_POLL_EXPECT_URL" ] \
|| [ "$FM_PR_REG_HOST" != "$FM_PR_POLL_EXPECT_HOST" ] \
|| [ "$FM_PR_REG_PATH" != "$FM_PR_POLL_EXPECT_PATH" ] \
|| [ "$FM_PR_REG_NUMBER" != "$FM_PR_POLL_EXPECT_NUMBER" ] \
|| [ "$FM_PR_REG_DATA_HASH" != "$FM_PR_POLL_EXPECT_DATA_HASH" ] \
|| [ "$FM_PR_REG_TEMPLATE_HASH" != "$FM_PR_POLL_EXPECT_TEMPLATE_HASH" ] \
|| [ "$FM_PR_REG_DATA_IDENTITY" != "$FM_PR_POLL_EXPECT_DATA_IDENTITY" ] \
|| [ "$FM_PR_REG_CHECK_IDENTITY" != "$FM_PR_POLL_EXPECT_CHECK_IDENTITY" ]; then
fm_pr_poll_revoke_final || true
return 1
fi
if ! fm_pr_regular_destination_on_device_or_absent "$FM_PR_POLL_CHECK_DEST" "$FM_PR_POLL_STATE_DEVICE" \
|| ! mv -f -- "$FM_PR_POLL_CHECK_TMP" "$FM_PR_POLL_CHECK_DEST"; then
fm_pr_poll_revoke_final || true
return 1
fi
FM_PR_POLL_CHECK_TMP=
if ! fm_pr_poll_artifacts_valid "${FM_PR_POLL_CHECK_DEST%/*}" "$FM_PR_POLL_EXPECT_ID" "$FM_PR_POLL_TEMPLATE"; then
fm_pr_poll_revoke_final || true
return 1
fi
}
# Records which predicate refused a poll in FM_PR_POLL_FAILED_PREDICATE and
# returns 1, so the watcher's rejection record can name it. Cleared on entry to
# fm_pr_poll_artifacts_valid; read only after that returns nonzero.
fm_pr_poll_fail() { # <predicate-slug>
export FM_PR_POLL_FAILED_PREDICATE=$1
return 1
}
fm_pr_poll_artifacts_valid() {
local state=$1 id=$2 template=$3 data_identity check_identity
export FM_PR_POLL_FAILED_PREDICATE=
fm_pr_poll_artifacts_content_valid "$state" "$id" "$template" || return 1
data_identity=$(fm_pr_file_identity "$state/$id.pr-poll") || { fm_pr_poll_fail data-identity-unreadable; return 1; }
check_identity=$(fm_pr_file_identity "$state/$id.check.sh") || { fm_pr_poll_fail check-identity-unreadable; return 1; }
# The recorded identities bind the registration to the exact sidecar and
# check file objects published in its own transaction, so a byte-identical
# replacement or a torn re-arm pairing one generation's check with another's
# registration is refused.
[ "$FM_PR_REG_DATA_IDENTITY" = "$data_identity" ] || { fm_pr_poll_fail registration-data-identity-mismatch; return 1; }
[ "$FM_PR_REG_CHECK_IDENTITY" = "$check_identity" ] || { fm_pr_poll_fail registration-check-identity-mismatch; return 1; }
}
# Everything fm_pr_poll_artifacts_valid proves except that the registration's
# recorded file identities name the live sidecar and check. Success alone is
# never authentication. On success FM_PR_DATA_*, FM_PR_REG_*, and FM_PR_META_*
# hold the parsed records.
fm_pr_poll_artifacts_content_valid() {
local state=$1 id=$2 template=$3 state_device check data registration meta data_hash template_hash
fm_pr_task_id_valid "$id" || { fm_pr_poll_fail task-id-invalid; return 1; }
[ -d "$state" ] && [ ! -L "$state" ] || { fm_pr_poll_fail state-dir-invalid; return 1; }
state_device=$(fm_pr_file_device "$state") || { fm_pr_poll_fail state-device-unreadable; return 1; }
check="$state/$id.check.sh"
data="$state/$id.pr-poll"
registration="$state/$id.pr-poll-registration"
meta="$state/$id.meta"
fm_pr_private_file_valid "$check" 600 "$state_device" || { fm_pr_poll_fail check-file-not-private; return 1; }
fm_pr_private_file_valid "$data" 600 "$state_device" || { fm_pr_poll_fail data-file-not-private; return 1; }
fm_pr_private_file_valid "$registration" 600 "$state_device" || { fm_pr_poll_fail registration-file-not-private; return 1; }
[ -f "$meta" ] && [ ! -L "$meta" ] || { fm_pr_poll_fail meta-file-missing; return 1; }
[ "$(fm_pr_file_link_count "$meta")" = 1 ] || { fm_pr_poll_fail meta-file-linked; return 1; }
cmp -s "$template" "$check" || { fm_pr_poll_fail check-differs-from-template; return 1; }
fm_pr_poll_data_parse "$data" || { fm_pr_poll_fail data-unparseable; return 1; }
data_hash=$(fm_pr_sha256 "$data") || { fm_pr_poll_fail data-hash-unreadable; return 1; }
template_hash=$(fm_pr_sha256 "$check") || { fm_pr_poll_fail template-hash-unreadable; return 1; }
fm_pr_poll_registration_parse "$registration" || { fm_pr_poll_fail registration-unparseable; return 1; }
[ "$FM_PR_REG_ID" = "$id" ] || { fm_pr_poll_fail registration-id-mismatch; return 1; }
[ "$FM_PR_REG_PROVIDER" = "$FM_PR_DATA_PROVIDER" ] || { fm_pr_poll_fail registration-provider-mismatch; return 1; }
[ "$FM_PR_REG_URL" = "$FM_PR_DATA_URL" ] || { fm_pr_poll_fail registration-url-mismatch; return 1; }
[ "$FM_PR_REG_HOST" = "$FM_PR_DATA_HOST" ] || { fm_pr_poll_fail registration-host-mismatch; return 1; }
[ "$FM_PR_REG_PATH" = "$FM_PR_DATA_PATH" ] || { fm_pr_poll_fail registration-path-mismatch; return 1; }
[ "$FM_PR_REG_NUMBER" = "$FM_PR_DATA_NUMBER" ] || { fm_pr_poll_fail registration-number-mismatch; return 1; }
[ "$FM_PR_REG_DATA_HASH" = "$data_hash" ] || { fm_pr_poll_fail registration-data-hash-mismatch; return 1; }
[ "$FM_PR_REG_TEMPLATE_HASH" = "$template_hash" ] || { fm_pr_poll_fail registration-template-hash-mismatch; return 1; }
fm_pr_metadata_identity_parse "$meta" || { fm_pr_poll_fail meta-pr-identity-invalid; return 1; }
[ "$FM_PR_META_PROVIDER" = "$FM_PR_DATA_PROVIDER" ] || { fm_pr_poll_fail meta-provider-mismatch; return 1; }
[ "$FM_PR_META_URL" = "$FM_PR_DATA_URL" ] || { fm_pr_poll_fail meta-url-mismatch; return 1; }
[ "$FM_PR_META_HOST" = "$FM_PR_DATA_HOST" ] || { fm_pr_poll_fail meta-host-mismatch; return 1; }
[ "$FM_PR_META_PATH" = "$FM_PR_DATA_PATH" ] || { fm_pr_poll_fail meta-path-mismatch; return 1; }
[ "$FM_PR_META_NUMBER" = "$FM_PR_DATA_NUMBER" ] || { fm_pr_poll_fail meta-number-mismatch; return 1; }
}
# A registration armed before a volume remount can name a device number the
# kernel has since reassigned (fm_pr_file_identity). This proves that is the
# only difference: every artifact passes fm_pr_poll_artifacts_content_valid, so
# the check is byte-identical to the template, both hashes match, and the three
# poll artifacts are private, single-link, and on the state directory's live
# device; both recorded identities name one device; each recorded inode equals
# its live inode; and that recorded device differs from the live one. A
# replaced, altered, re-moded, relinked, or foreign-device artifact fails a proof
# here and stays refused. A pending retirement receipt owns its artifacts, so
# none is re-recorded while one exists. On success
# FM_PR_POLL_REARM_DATA_IDENTITY and FM_PR_POLL_REARM_CHECK_IDENTITY hold the
# live identities.
fm_pr_poll_registration_device_shifted() { # <state> <id> <template>
local state=$1 id=$2 template=$3 state_device recorded_device receipt data_identity check_identity
FM_PR_POLL_REARM_DATA_IDENTITY=
FM_PR_POLL_REARM_CHECK_IDENTITY=
fm_pr_task_id_valid "$id" || return 1
[ -f "$state/$id.pr-poll-registration" ] || return 1
receipt="$state/$id.pr-poll-retirement"
[ ! -e "$receipt" ] && [ ! -L "$receipt" ] || return 1
fm_pr_poll_artifacts_content_valid "$state" "$id" "$template" || return 1
state_device=$(fm_pr_file_device "$state") || return 1
data_identity=$(fm_pr_file_identity "$state/$id.pr-poll") || return 1
check_identity=$(fm_pr_file_identity "$state/$id.check.sh") || return 1
recorded_device=${FM_PR_REG_DATA_IDENTITY%%:*}
[ "${FM_PR_REG_CHECK_IDENTITY%%:*}" = "$recorded_device" ] || return 1
[ "$recorded_device" != "$state_device" ] || return 1
[ "$data_identity" = "$state_device:${FM_PR_REG_DATA_IDENTITY#*:}" ] || return 1
[ "$check_identity" = "$state_device:${FM_PR_REG_CHECK_IDENTITY#*:}" ] || return 1
FM_PR_POLL_REARM_DATA_IDENTITY=$data_identity
FM_PR_POLL_REARM_CHECK_IDENTITY=$check_identity
}
# Rewrite a device-shifted registration (fm_pr_poll_registration_device_shifted)
# so it names the live device, changing no other line. The caller holds the
# task's control lock and poll publication lock, which serialize this with the
# watcher's validated check and retirement, teardown, bin/fm-pr-merge.sh, and
# direct bin/fm-pr-check.sh publication. The proof is repeated just before the
# rename, which proceeds only while the registration is still the exact file
# object and bytes first proven.
# Success means the strict fm_pr_poll_artifacts_valid accepts the result.
fm_pr_poll_registration_rerecord_device() { # <state> <id> <template>
local state=$1 id=$2 template=$3 state_device registration tmp reg_hash reg_identity
local id_line provider url host path number data_hash template_hash data_identity check_identity
fm_pr_poll_registration_device_shifted "$state" "$id" "$template" || return 1
registration="$state/$id.pr-poll-registration"
id_line=$FM_PR_REG_ID
provider=$FM_PR_REG_PROVIDER
url=$FM_PR_REG_URL
host=$FM_PR_REG_HOST
path=$FM_PR_REG_PATH
number=$FM_PR_REG_NUMBER
data_hash=$FM_PR_REG_DATA_HASH
template_hash=$FM_PR_REG_TEMPLATE_HASH
data_identity=$FM_PR_POLL_REARM_DATA_IDENTITY
check_identity=$FM_PR_POLL_REARM_CHECK_IDENTITY
state_device=$(fm_pr_file_device "$state") || return 1
reg_hash=$(fm_pr_sha256 "$registration") || return 1
reg_identity=$(fm_pr_file_identity "$registration") || return 1
tmp=$(mktemp "$state/.fm-pr-poll-registration.XXXXXX") || return 1
if ! printf '%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n' \
fm-pr-poll-registration-v2 "$id_line" "$provider" "$url" "$host" "$path" "$number" \
"$data_hash" "$template_hash" "$data_identity" "$check_identity" > "$tmp" \
|| ! chmod 0600 "$tmp" \
|| ! fm_pr_private_file_valid "$tmp" 600 "$state_device" \
|| ! fm_pr_poll_registration_parse "$tmp" \
|| [ "$FM_PR_REG_ID" != "$id" ] \
|| [ "$FM_PR_REG_URL" != "$url" ] \
|| [ "$FM_PR_REG_DATA_HASH" != "$data_hash" ] \
|| [ "$FM_PR_REG_TEMPLATE_HASH" != "$template_hash" ] \
|| [ "$FM_PR_REG_DATA_IDENTITY" != "$data_identity" ] \
|| [ "$FM_PR_REG_CHECK_IDENTITY" != "$check_identity" ] \
|| ! fm_pr_poll_registration_device_shifted "$state" "$id" "$template" \
|| [ "$FM_PR_POLL_REARM_DATA_IDENTITY" != "$data_identity" ] \
|| [ "$FM_PR_POLL_REARM_CHECK_IDENTITY" != "$check_identity" ] \
|| [ "$(fm_pr_sha256 "$registration")" != "$reg_hash" ] \
|| [ "$(fm_pr_file_identity "$registration")" != "$reg_identity" ] \
|| ! fm_pr_regular_destination_on_device_or_absent "$registration" "$state_device" \
|| ! mv -f -- "$tmp" "$registration"; then
rm -f -- "$tmp"
return 1
fi
fm_pr_poll_artifacts_valid "$state" "$id" "$template"
}
fm_pr_poll_snapshot_capture() {
local state=$1 id=$2 template=$3 registration
fm_pr_poll_artifacts_valid "$state" "$id" "$template" || return 1
registration="$state/$id.pr-poll-registration"
FM_PR_POLL_SNAPSHOT_REG_HASH=$(fm_pr_sha256 "$registration") || return 1
FM_PR_POLL_SNAPSHOT_REG_IDENTITY=$(fm_pr_file_identity "$registration") || return 1
FM_PR_POLL_SNAPSHOT_ID=$id
FM_PR_POLL_SNAPSHOT_PROVIDER=$FM_PR_DATA_PROVIDER
FM_PR_POLL_SNAPSHOT_URL=$FM_PR_DATA_URL
FM_PR_POLL_SNAPSHOT_HOST=$FM_PR_DATA_HOST
FM_PR_POLL_SNAPSHOT_PATH=$FM_PR_DATA_PATH
FM_PR_POLL_SNAPSHOT_NUMBER=$FM_PR_DATA_NUMBER
FM_PR_POLL_SNAPSHOT_DATA_HASH=$FM_PR_REG_DATA_HASH
FM_PR_POLL_SNAPSHOT_TEMPLATE_HASH=$FM_PR_REG_TEMPLATE_HASH
FM_PR_POLL_SNAPSHOT_DATA_IDENTITY=$FM_PR_REG_DATA_IDENTITY
FM_PR_POLL_SNAPSHOT_CHECK_IDENTITY=$FM_PR_REG_CHECK_IDENTITY
}
fm_pr_poll_snapshot_matches() {
local state=$1 id=$2 template=$3 registration reg_hash reg_identity
[ -n "$FM_PR_POLL_SNAPSHOT_ID" ] && [ "$id" = "$FM_PR_POLL_SNAPSHOT_ID" ] || return 1
fm_pr_poll_artifacts_valid "$state" "$id" "$template" || return 1
registration="$state/$id.pr-poll-registration"
reg_hash=$(fm_pr_sha256 "$registration") || return 1
reg_identity=$(fm_pr_file_identity "$registration") || return 1
[ "$FM_PR_DATA_PROVIDER" = "$FM_PR_POLL_SNAPSHOT_PROVIDER" ] || return 1
[ "$FM_PR_DATA_URL" = "$FM_PR_POLL_SNAPSHOT_URL" ] || return 1
[ "$FM_PR_DATA_HOST" = "$FM_PR_POLL_SNAPSHOT_HOST" ] || return 1
[ "$FM_PR_DATA_PATH" = "$FM_PR_POLL_SNAPSHOT_PATH" ] || return 1
[ "$FM_PR_DATA_NUMBER" = "$FM_PR_POLL_SNAPSHOT_NUMBER" ] || return 1
[ "$FM_PR_REG_DATA_HASH" = "$FM_PR_POLL_SNAPSHOT_DATA_HASH" ] || return 1
[ "$FM_PR_REG_TEMPLATE_HASH" = "$FM_PR_POLL_SNAPSHOT_TEMPLATE_HASH" ] || return 1
[ "$FM_PR_REG_DATA_IDENTITY" = "$FM_PR_POLL_SNAPSHOT_DATA_IDENTITY" ] || return 1
[ "$FM_PR_REG_CHECK_IDENTITY" = "$FM_PR_POLL_SNAPSHOT_CHECK_IDENTITY" ] || return 1
[ "$reg_hash" = "$FM_PR_POLL_SNAPSHOT_REG_HASH" ] || return 1
[ "$reg_identity" = "$FM_PR_POLL_SNAPSHOT_REG_IDENTITY" ]
}
fm_pr_poll_retirement_parse() {
local file=$1 version id provider url host path number data_hash template_hash
local data_identity check_identity reg_hash reg_identity result _extra
FM_PR_RETIRE_ID=
FM_PR_RETIRE_PROVIDER=
FM_PR_RETIRE_URL=
FM_PR_RETIRE_HOST=
FM_PR_RETIRE_PATH=
FM_PR_RETIRE_NUMBER=
FM_PR_RETIRE_DATA_HASH=
FM_PR_RETIRE_TEMPLATE_HASH=
FM_PR_RETIRE_DATA_IDENTITY=
FM_PR_RETIRE_CHECK_IDENTITY=
FM_PR_RETIRE_REG_HASH=
FM_PR_RETIRE_REG_IDENTITY=
[ -f "$file" ] && [ ! -L "$file" ] || return 1
exec 9< "$file" || return 1
IFS= read -r version <&9 || { exec 9<&-; return 1; }
IFS= read -r id <&9 || { exec 9<&-; return 1; }
IFS= read -r provider <&9 || { exec 9<&-; return 1; }
IFS= read -r url <&9 || { exec 9<&-; return 1; }
IFS= read -r host <&9 || { exec 9<&-; return 1; }
IFS= read -r path <&9 || { exec 9<&-; return 1; }
IFS= read -r number <&9 || { exec 9<&-; return 1; }
IFS= read -r data_hash <&9 || { exec 9<&-; return 1; }
IFS= read -r template_hash <&9 || { exec 9<&-; return 1; }
IFS= read -r data_identity <&9 || { exec 9<&-; return 1; }
IFS= read -r check_identity <&9 || { exec 9<&-; return 1; }
IFS= read -r reg_hash <&9 || { exec 9<&-; return 1; }
IFS= read -r reg_identity <&9 || { exec 9<&-; return 1; }
IFS= read -r result <&9 || { exec 9<&-; return 1; }
if IFS= read -r _extra <&9; then
exec 9<&-
return 1
fi
exec 9<&-
[ "$version" = fm-pr-poll-retirement-v1 ] || return 1
fm_pr_task_id_valid "$id" || return 1
fm_pr_url_parse "$url" || return 1
[ "$provider" = "$FM_PR_PROVIDER" ] || return 1
[ "$host" = "$FM_PR_HOST" ] || return 1
[ "$path" = "$FM_PR_PATH" ] || return 1
[ "$number" = "$FM_PR_NUMBER" ] || return 1
[[ "$data_hash" =~ ^[0-9a-f]{64}$ ]] || return 1
[[ "$template_hash" =~ ^[0-9a-f]{64}$ ]] || return 1
[[ "$data_identity" =~ ^[0-9]+:[0-9]+$ ]] || return 1
[[ "$check_identity" =~ ^[0-9]+:[0-9]+$ ]] || return 1
[[ "$reg_hash" =~ ^[0-9a-f]{64}$ ]] || return 1
[[ "$reg_identity" =~ ^[0-9]+:[0-9]+$ ]] || return 1
[ "$result" = merged ] || return 1
FM_PR_RETIRE_ID=$id
FM_PR_RETIRE_PROVIDER=$provider
FM_PR_RETIRE_URL=$url
FM_PR_RETIRE_HOST=$host
FM_PR_RETIRE_PATH=$path
FM_PR_RETIRE_NUMBER=$number
FM_PR_RETIRE_DATA_HASH=$data_hash
FM_PR_RETIRE_TEMPLATE_HASH=$template_hash
FM_PR_RETIRE_DATA_IDENTITY=$data_identity
FM_PR_RETIRE_CHECK_IDENTITY=$check_identity
FM_PR_RETIRE_REG_HASH=$reg_hash
FM_PR_RETIRE_REG_IDENTITY=$reg_identity
}
fm_pr_poll_retirement_receipt_valid() {
local state=$1 id=$2 receipt state_device meta
fm_pr_task_id_valid "$id" || return 1