-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-contract.sh
More file actions
executable file
·304 lines (288 loc) · 8.96 KB
/
Copy pathcore-contract.sh
File metadata and controls
executable file
·304 lines (288 loc) · 8.96 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
#!/usr/bin/env bash
set -uo pipefail
export LC_ALL=C
umask 077
assembly_error() {
case "$1" in
E_USAGE|E_RUNTIME) printf '%s\n' "$1" >&2 ;;
*) printf '%s\n' E_RUNTIME >&2 ;;
esac
return 1
}
assembly_fd3_writable() {
local fdinfo_line
local fdinfo_key
local fdinfo_value
local flags_value=''
local flags_count=0
local record_count=0
local access_mode
case "${OSTYPE:-}" in
linux*)
[ -r /proc/self/fdinfo/3 ] || return 1
while IFS= read -r fdinfo_line || [ -n "$fdinfo_line" ]; do
record_count=$((record_count + 1))
[[ "$fdinfo_line" =~ ^([A-Za-z0-9_]+):[[:space:]]+([^[:cntrl:]]*)$ ]] ||
return 1
fdinfo_key="${BASH_REMATCH[1]}"
fdinfo_value="${BASH_REMATCH[2]}"
if [ "$fdinfo_key" = flags ]; then
flags_count=$((flags_count + 1))
flags_value="$fdinfo_value"
fi
done < /proc/self/fdinfo/3
[ "$record_count" -gt 0 ] && [ "$flags_count" -eq 1 ] &&
[[ "$flags_value" =~ ^[0-7]+$ ]] &&
[ "${#flags_value}" -le 20 ] || return 1
access_mode=$((8#$flags_value & 3))
case "$access_mode" in 1|2) return 0 ;; *) return 1 ;; esac
;;
darwin*)
[ -e /dev/fd/3 ] && [ -w /dev/fd/3 ]
;;
*) return 1 ;;
esac
}
assembly_accounted=false
assembly_scratch_root=''
assembly_byte_budget=0
assembly_finalizing=false
assembly_receipt_emitted=false
assembly_finalization_signal=''
assembly_cleaning=false
assembly_cleanup_signal=''
assembly_error_emitted=false
unset PORTABLE_CORE_INGRESS_ACCOUNTED \
PORTABLE_CORE_INGRESS_BUFFER_ERRORS \
PORTABLE_CORE_INGRESS_PENDING_ERROR \
PORTABLE_CORE_INGRESS_WRITTEN_BYTES \
PORTABLE_CORE_INGRESS_RESERVED_BYTES \
PORTABLE_CORE_INGRESS_DEFERRED_SIGNAL \
PORTABLE_CORE_INGRESS_CREATING_TEMP \
PORTABLE_CORE_INGRESS_OWNS_TEMP \
PORTABLE_CORE_INGRESS_TEMP \
PORTABLE_CORE_INGRESS_SCRATCH_ROOT \
PORTABLE_CORE_INGRESS_OWNER_PID \
PORTABLE_CORE_INGRESS_INITIAL_BYTES \
PORTABLE_CORE_INGRESS_REMAINING_BYTES \
PORTABLE_CORE_INGRESS_RM
# shellcheck disable=SC2034
declare PORTABLE_CORE_INGRESS_ACCOUNTED=false \
PORTABLE_CORE_INGRESS_BUFFER_ERRORS=false \
PORTABLE_CORE_INGRESS_PENDING_ERROR='' \
PORTABLE_CORE_INGRESS_WRITTEN_BYTES=0 \
PORTABLE_CORE_INGRESS_RESERVED_BYTES='' \
PORTABLE_CORE_INGRESS_DEFERRED_SIGNAL='' \
PORTABLE_CORE_INGRESS_CREATING_TEMP=false \
PORTABLE_CORE_INGRESS_OWNS_TEMP=false \
PORTABLE_CORE_INGRESS_TEMP='' \
PORTABLE_CORE_INGRESS_SCRATCH_ROOT='' \
PORTABLE_CORE_INGRESS_OWNER_PID='' \
PORTABLE_CORE_INGRESS_INITIAL_BYTES=0 \
PORTABLE_CORE_INGRESS_REMAINING_BYTES=0 \
PORTABLE_CORE_INGRESS_RM=''
assembly_emit_receipt() {
local receipt_status=0
[ "$assembly_accounted" = true ] || return 0
[ "$assembly_receipt_emitted" = false ] || return 0
assembly_finalizing=true
printf 'written-bytes:%s\n' \
"${PORTABLE_CORE_INGRESS_WRITTEN_BYTES:-0}" \
2>/dev/null >&3 || receipt_status=$?
if [ "$receipt_status" -eq 0 ]; then
assembly_receipt_emitted=true
fi
assembly_finalizing=false
[ "$receipt_status" -eq 0 ] || return 1
[ -z "$assembly_finalization_signal" ] || return 2
}
assembly_emit_pending_error() {
local pending_error="${PORTABLE_CORE_INGRESS_PENDING_ERROR:-}"
[ "$assembly_accounted" = true ] || return 0
[ "$assembly_error_emitted" = false ] || return 0
[ -n "$pending_error" ] || return 0
case "$pending_error" in
E_RUNTIME|E_PARSE|E_CANONICAL|E_LIMIT|E_SHAPE|E_REF|E_RELATION) ;;
*) pending_error=E_RUNTIME ;;
esac
printf '%s\n' "$pending_error" >&2
assembly_error_emitted=true
}
assembly_cleanup() {
local status=$?
local receipt_status=0
local close_status=0
trap - EXIT
if [ "$(type -t portable_core_ingress_close 2>/dev/null)" = function ]; then
assembly_cleaning=true
portable_core_ingress_close >/dev/null 2>&1 || close_status=$?
if [ "$assembly_accounted" = true ] && [ "$close_status" -ne 0 ]; then
close_status=0
portable_core_ingress_close >/dev/null 2>&1 || close_status=$?
fi
assembly_cleaning=false
fi
if [ "$assembly_accounted" = true ] && [ "$close_status" -ne 0 ]; then
PORTABLE_CORE_INGRESS_PENDING_ERROR=E_RUNTIME
status=1
fi
assembly_emit_pending_error || status=1
assembly_emit_receipt || receipt_status=$?
[ "$receipt_status" -eq 0 ] || status=1
[ -z "$assembly_finalization_signal" ] || status=1
[ -z "$assembly_cleanup_signal" ] || status=1
trap - HUP INT TERM
exit "$status"
}
assembly_signal() {
if [ "${PORTABLE_CORE_INGRESS_CREATING_TEMP:-false}" = true ] ||
[ -n "${PORTABLE_CORE_INGRESS_RESERVED_BYTES:-}" ]; then
# shellcheck disable=SC2034
PORTABLE_CORE_INGRESS_DEFERRED_SIGNAL="$1"
return 0
fi
if [ "$assembly_cleaning" = true ]; then
assembly_cleanup_signal="$1"
return 0
fi
if [ "$assembly_finalizing" = true ]; then
assembly_finalization_signal="$1"
return 0
fi
exit 1
}
if [ "${1:-}" = --accounted-validation ]; then
if [ "$#" -lt 4 ] ||
[[ ! "${3:-}" =~ ^(0|[1-9][0-9]*)$ ]] ||
[ "${#3}" -gt 9 ] || [ "$3" -gt 536870912 ] ||
! assembly_fd3_writable; then
assembly_error E_USAGE
exit 1
fi
assembly_accounted=true
assembly_scratch_root="$2"
assembly_byte_budget="$3"
shift 3
trap assembly_cleanup EXIT
trap 'assembly_signal HUP' HUP
trap 'assembly_signal INT' INT
trap 'assembly_signal TERM' TERM
fi
case "${1:-}" in
validate-document)
[ "$#" -eq 2 ] || { assembly_error E_USAGE; exit 1; }
assembly_mode=document
;;
validate-profile-set)
[ "$#" -ge 4 ] && [ "$#" -le 11 ] || {
assembly_error E_USAGE
exit 1
}
assembly_mode=profile-set
;;
validate-stage-run)
[ "$#" -eq 4 ] || { assembly_error E_USAGE; exit 1; }
assembly_mode=stage-run
;;
*)
assembly_error E_USAGE
exit 1
;;
esac
shift
assembly_inputs=("$@")
PORTABLE_CORE_SCHEMA_MAJOR='2'
PORTABLE_CORE_GENERATION='g-c83c940afd16550a4f8a4dbee2b9a6f37e429063d277962ba81c141ba5303b43'
assembly_source="${BASH_SOURCE[0]}"
case "$assembly_source" in
/*) ;;
*)
assembly_cwd="$(pwd -P 2>/dev/null)" || {
assembly_error E_RUNTIME
exit 1
}
assembly_source="$assembly_cwd/$assembly_source"
;;
esac
assembly_name="${assembly_source##*/}"
assembly_parent="${assembly_source%/*}"
assembly_dir="$(CDPATH='' cd -P -- "$assembly_parent" 2>/dev/null && pwd -P)" || {
assembly_error E_RUNTIME
exit 1
}
assembly_source="$assembly_dir/$assembly_name"
assembly_repo="$(CDPATH='' cd -P -- "$assembly_dir/.." 2>/dev/null && pwd -P)" || {
assembly_error E_RUNTIME
exit 1
}
if [ "$assembly_source" != "$assembly_repo/scripts/core-contract.sh" ] ||
[ ! -f "$assembly_source" ] || [ -L "$assembly_source" ]; then
assembly_error E_RUNTIME
exit 1
fi
assembly_schema_root="$assembly_repo/core/v$PORTABLE_CORE_SCHEMA_MAJOR"
assembly_generation_root="$assembly_schema_root/generations/$PORTABLE_CORE_GENERATION"
assembly_ingress="$assembly_generation_root/core-ingress.sh"
assembly_program="$assembly_generation_root/contracts.jq"
assembly_modules="$assembly_generation_root/modules"
for assembly_required_dir in \
"$assembly_repo" "$assembly_repo/core" "$assembly_schema_root" \
"$assembly_schema_root/generations" "$assembly_generation_root" \
"$assembly_modules"; do
if [ ! -d "$assembly_required_dir" ] || [ -L "$assembly_required_dir" ]; then
assembly_error E_RUNTIME
exit 1
fi
done
for assembly_required_file in \
"$assembly_ingress" "$assembly_program" \
"$assembly_modules/schema.jq" "$assembly_modules/profile_graph.jq" \
"$assembly_modules/stage_request.jq" "$assembly_modules/result_facts.jq" \
"$assembly_modules/result_truth.jq"; do
if [ ! -f "$assembly_required_file" ] || [ -L "$assembly_required_file" ]; then
assembly_error E_RUNTIME
exit 1
fi
done
trap assembly_cleanup EXIT
trap 'assembly_signal HUP' HUP
trap 'assembly_signal INT' INT
trap 'assembly_signal TERM' TERM
# shellcheck source=/dev/null
if ! source "$assembly_ingress" 2>/dev/null; then
assembly_error E_RUNTIME
exit 1
fi
if [ "$assembly_accounted" = true ]; then
portable_core_ingress_open "$assembly_scratch_root" \
"$assembly_byte_budget" || exit 1
else
portable_core_ingress_open || exit 1
fi
if [ "$PORTABLE_CORE_INGRESS_GENERATION" != "$PORTABLE_CORE_GENERATION" ] ||
[ "$PORTABLE_CORE_INGRESS_ROOT" != "$assembly_program" ] ||
[ "$PORTABLE_CORE_INGRESS_MODULE_DIR" != "$assembly_modules" ]; then
assembly_error E_RUNTIME
exit 1
fi
portable_core_ingress_begin "$assembly_mode" || exit 1
for assembly_input in "${assembly_inputs[@]}"; do
portable_core_ingress_snapshot "$assembly_input" || exit 1
done
portable_core_ingress_finish_driver || exit 1
portable_core_ingress_validate || exit 1
assembly_cleaning=true
portable_core_ingress_close || exit 1
assembly_cleaning=false
assembly_receipt_status=0
assembly_emit_receipt || assembly_receipt_status=$?
case "$assembly_receipt_status" in
0) ;;
1)
assembly_error E_RUNTIME
exit 1
;;
2) exit 1 ;;
esac
[ -z "$assembly_cleanup_signal" ] || exit 1
trap - EXIT HUP INT TERM