Improved input validation

This commit is contained in:
PedoDeveloper 2024-11-11 12:07:49 +00:00
parent bb075ed369
commit 4a236a56cf

View file

@ -31,29 +31,31 @@ check_input() {
exit 2
fi
echo "Archiving file/directory '${input_file}'" >&2
}
set_volumes() {
volumes=
if [[ "$size" =~ ^[1-9][0-9]*$ ]]; then
volumes="-v${size}m"
elif [[ -n "$size" ]]; then
echo "Invalid size $size, must be a number"
if [[ -z "${archive_pwd}" ]]; then
echo "ERROR: No password given"
exit 2
fi
if ! [[ "$size" =~ ^([1-9][0-9]*)?$ ]]; then
echo "Invalid size $size, must be a positive number"
exit 2
fi
echo "Archiving file/directory '${input_file}' with password '${archive_pwd}'" >&2
}
set_volumes() {
[[ -n "$size" ]] && volumes="-v${size}m" || volumes=
echo "Volumes set to '$volumes'" >&2
}
generate_name() {
archive="$archive_dir/$(base64 /dev/urandom | tr -dc [:alnum:] | head -c 20).7z"
echo "Generated archive name $archive" >&2
echo "Generated archive name '$archive'" >&2
}
create_archive() {
echo "Packing ${input_file} into archive $archive with password ${archive_pwd}"
echo "Packing '${input_file}' into archive '$archive' with password '${archive_pwd}'"
[[ -n "$volumes" ]] && echo "and volumes of size $size MB"
7z a -mhe=on "-p${archive_pwd}" "$volumes" "$archive" "${input_file}" >&2