summaryrefslogtreecommitdiff
path: root/src/lib/config.validate.source.sh
blob: 3ca720a558e5391ee55ff3f96e603587afebb09c (plain)
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
config_error() {
    local -r message="$1"; shift

    printf 'ERROR: %s\n' "$message" >&2
    return 1
}

require_config_var() {
    local -r name="$1"; shift

    if [ -z "${!name+x}" ] || [ -z "${!name}" ]; then
        config_error "$name must be set in shuriken configuration"
        return 1
    fi
}

validate_positive_integer_config_var() {
    local -r name="$1"; shift
    local -r value="${!name}"

    if [[ ! "$value" =~ ^[0-9]+$ ]] || (( value < 1 )); then
        config_error "$name must be a positive integer"
        return 1
    fi
}

validate_optional_positive_integer_config_var() {
    local -r name="$1"; shift

    if [ -n "${!name:-}" ]; then
        validate_positive_integer_config_var "$name" || return
    fi
}

# A percentage config var: an integer in the inclusive range 0..100. Unlike
# validate_positive_integer_config_var, 0 is allowed (it is the natural "off"
# value, e.g. THUMB_SUBDIVIDE_PERCENT=0 disables tile subdivision entirely).
validate_percentage_config_var() {
    local -r name="$1"; shift
    local -r value="${!name}"

    if [[ ! "$value" =~ ^[0-9]+$ ]] || (( value > 100 )); then
        config_error "$name must be an integer between 0 and 100"
        return 1
    fi
}

validate_yes_no_config_var() {
    local -r name="$1"; shift
    local -r value="${!name}"

    case "$value" in
        yes|no)
            ;;
        *)
            config_error "$name must be yes or no"
            return 1
            ;;
    esac
}

validate_dist_dir() {
    local existing_parent

    if [ -e "$DIST_DIR" ]; then
        if [ ! -d "$DIST_DIR" ]; then
            config_error "DIST_DIR $DIST_DIR must be a directory"
            return 1
        fi
        if [[ ! -w "$DIST_DIR" || ! -x "$DIST_DIR" ]]; then
            config_error "DIST_DIR $DIST_DIR must be writable"
            return 1
        fi

        return
    fi

    existing_parent=$(existing_parent_dir "$DIST_DIR")

    if [ ! -d "$existing_parent" ]; then
        config_error "DIST_DIR parent $existing_parent must be a directory"
        return 1
    fi
    if [[ ! -w "$existing_parent" || ! -x "$existing_parent" ]]; then
        config_error "DIST_DIR parent $existing_parent must be writable"
        return 1
    fi
}

# The destructive --clean path guard (resolve_dist_dir_path,
# validate_clean_dist_dir, and the dangerous-path blocklist) lives in
# config.clean-guard.source.sh (task tr0). It is kept in its own module because
# it gates an unconditional rm -rf and warrants an isolated test surface; the
# functions there still reuse the generic validators below at runtime.

validate_template_dir_access() {
    if [[ ! -d "$TEMPLATE_DIR" || ! -r "$TEMPLATE_DIR" \
        || ! -x "$TEMPLATE_DIR" ]]; then
        config_error "TEMPLATE_DIR $TEMPLATE_DIR must be a readable directory"
        return 1
    fi
}

validate_template_file() {
    local -r template_name="$1"; shift

    if [ ! -r "$TEMPLATE_DIR/$template_name.tmpl" ]; then
        config_error \
            "template file $TEMPLATE_DIR/$template_name.tmpl must be readable"
        return 1
    fi
}

validate_template_dir() {
    local template_name
    local -a required_templates=(
        details
        footer
        header
        next
        prev
        preview
        previewpage
        redirect
        view
    )

    validate_template_dir_access || return

    if [ "$SPLASH_PAGE" = yes ]; then
        required_templates+=(splash)
    fi

    for template_name in "${required_templates[@]}"; do
        validate_template_file "$template_name" || return
    done
}

validate_refresh_splash_config() {
    local required_var
    local -a required_vars=(
        TITLE
        DIST_DIR
        TEMPLATE_DIR
    )

    for required_var in "${required_vars[@]}"; do
        require_config_var "$required_var" || return
    done

    validate_yes_no_config_var SPLASH_PAGE || return

    if [ "$SPLASH_PAGE" != yes ]; then
        config_error 'SPLASH_PAGE must be yes to refresh the splash page'
        return 1
    fi

    validate_dist_dir || return

    validate_template_dir_access || return
    validate_template_file splash || return

    if [ ! -d "$DIST_DIR/photos" ]; then
        config_error "DIST_DIR photos directory $DIST_DIR/photos must exist"
        return 1
    fi

    if [ ! -d "$DIST_DIR/blurs" ]; then
        config_error "DIST_DIR blurs directory $DIST_DIR/blurs must exist"
        return 1
    fi
}

validate_imagemagick() {
    # Reuse the canonical detection in resolve_imagemagick_command instead of
    # duplicating the magick/convert probing here. Its own error message is
    # suppressed so we report the failure through config_error, keeping the
    # validation output consistent (single "ERROR: ..." line, return code 1).
    # imagemagick_command is a nameref output filled by the resolver; we only
    # care about the exit status here, not the resolved command.
    # shellcheck disable=SC2034
    local -a imagemagick_command=()

    if resolve_imagemagick_command convert imagemagick_command 2>/dev/null; then
        return
    fi

    config_error 'ImageMagick is required; install magick or convert'
    return 1
}

# Look up a config field's validation facet (index 4) from CONFIG_SPECS. Prints
# the validation token (e.g. required-posint, percentage, yesno) or empty if the
# field is unknown / has no validation rule. The single place validate_*
# dispatchers learn which rule a field uses (task mr0).
config_spec_validation() {
    local -r name="$1"; shift
    local spec
    local -a fields=()

    for spec in "${CONFIG_SPECS[@]}"; do
        config_spec_split "$spec" fields
        if [ "${fields[0]}" = "$name" ]; then
            printf '%s\n' "${fields[4]}"
            return 0
        fi
    done

    return 1
}

# Run a config field's "required" check based on its registry validation facet.
# Only required / required-posint fields are required-to-be-set; everything else
# is a no-op here. Used by validate_common_config's required pass so the set of
# required vars derives from the registry instead of a separate hand-kept list.
validate_config_field_required() {
    local -r name="$1"; shift
    local validation

    validation=$(config_spec_validation "$name")
    case "$validation" in
        required|required-posint)
            require_config_var "$name" || return 1
            ;;
    esac
}

# Run a config field's "kind" check based on its registry validation facet,
# dispatching to the matching validator. This is the single source for "which
# rule validates which field" (task mr0): validate_common_config calls this per
# field in its historical order, so the rule lives in CONFIG_SPECS while the
# error-reporting order stays byte-identical. The required-only facet has no kind
# check (the required pass covers it); required-posint additionally enforces a
# positive integer here.
validate_config_field_kind() {
    local -r name="$1"; shift
    local validation

    validation=$(config_spec_validation "$name")
    case "$validation" in
        required)
            ;;
        required-posint|posint)
            validate_positive_integer_config_var "$name" || return 1
            ;;
        opt-posint)
            validate_optional_positive_integer_config_var "$name" || return 1
            ;;
        percentage)
            validate_percentage_config_var "$name" || return 1
            ;;
        yesno)
            validate_yes_no_config_var "$name" || return 1
            ;;
        favicon)
            validate_favicon_config || return 1
            ;;
    esac
}

# Validate the config fields shared by every action that needs a loaded config.
# The set of required vars and each field's validation rule come from CONFIG_SPECS
# via the dispatchers above (task mr0). The two phases (all required checks, then
# all kind checks) are preserved deliberately: a missing required var must be
# reported before a malformed one (test_config_validators_fail_fast_without_errexit
# proves an unset TITLE is reported while a bad THUMBHEIGHT is not). The per-field
# call order below is the historical reporting order, so error messages for a
# config with several problems appear in the same sequence as before.
validate_common_config() {
    local required_var
    local kind_var
    local -a required_vars=(
        TITLE
        THUMBHEIGHT
        MAXPREVIEWS
        IMAGE_JOBS
        INCOMING_DIR
        DIST_DIR
        TEMPLATE_DIR
    )
    local -a kind_vars=(
        HEIGHT
        THUMBHEIGHT
        MAXPREVIEWS
        THUMB_SUBDIVIDE_PERCENT
        THUMB_FEATURE_PERCENT
        IMAGE_JOBS
        IMAGEMAGICK_TIMEOUT
        TAR_TIMEOUT
        SYNC_TIMEOUT
        SHUFFLE
        SPLASH_PAGE
        STATS_PAGE
        TARBALL_INCLUDE
        FAVICON
    )

    for required_var in "${required_vars[@]}"; do
        validate_config_field_required "$required_var" || return
    done

    for kind_var in "${kind_vars[@]}"; do
        validate_config_field_kind "$kind_var" || return
    done
}

# A custom FAVICON (when set) must be a readable file; empty means the bundled
# default favicon is used.
validate_favicon_config() {
    if [ -z "${FAVICON:-}" ]; then
        return
    fi
    if [ ! -f "$FAVICON" ] || [ ! -r "$FAVICON" ]; then
        config_error "FAVICON file $FAVICON must be a readable file"
        return 1
    fi
}

validate_generation_config() {
    local -r require_imagemagick="${1:-yes}"

    validate_common_config || return

    if [ ! -d "$INCOMING_DIR" ]; then
        config_error "You have to create $INCOMING_DIR first"
        return 1
    fi
    if [[ ! -r "$INCOMING_DIR" || ! -x "$INCOMING_DIR" ]]; then
        config_error "INCOMING_DIR $INCOMING_DIR must be readable"
        return 1
    fi

    validate_dist_dir || return
    validate_template_dir || return
    if [ "$require_imagemagick" = yes ]; then
        validate_imagemagick || return
    fi
}

validate_print_config() {
    # Passed by name to resolve_tar_opts.
    # shellcheck disable=SC2034
    local -a tar_opts=()
    local -a sync_destinations=()

    validate_common_config || return
    resolve_tar_opts tar_opts
    resolve_sync_destinations sync_destinations
    validate_yes_no_config_var SYNC_DELETE || return
}

validate_sync_destinations() {
    local -a sync_destinations=()

    resolve_sync_destinations sync_destinations

    if (( ${#sync_destinations[@]} == 0 )); then
        config_error 'SYNC_DESTINATIONS must contain at least one destination'
        return 1
    fi
}

validate_rsync() {
    if command -v rsync >/dev/null 2>&1; then
        return
    fi

    config_error 'rsync is required to sync generated output'
    return 1
}

validate_sync_config() {
    require_config_var DIST_DIR || return
    validate_yes_no_config_var SYNC_DELETE || return
    # SYNC_TIMEOUT bounds each per-destination rsync in sync_dist, so it must be
    # a positive integer on the sync path too (the generate path validates it via
    # validate_config). Without this, a bogus value would only surface as a
    # confusing "timeout: invalid time interval" at run time.
    validate_positive_integer_config_var SYNC_TIMEOUT || return
    validate_sync_destinations || return

    if [[ ! -d "$DIST_DIR" || ! -r "$DIST_DIR" || ! -x "$DIST_DIR" ]]; then
        config_error "DIST_DIR $DIST_DIR must be a readable directory"
        return 1
    fi

    validate_rsync || return
}