Add comments to the code describing the changes.

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2020-02-22 15:17:24 +01:00
parent 81d4531e10
commit 8131b5961c
No known key found for this signature in database
GPG key ID: FB60471F0575164A

View file

@ -400,7 +400,7 @@ gravity_DownloadBlocklists() {
lineno="${warning#*:}" lineno="${warning#*:}"
if [[ -n "${file}" && -n "${lineno}" ]]; then if [[ -n "${file}" && -n "${lineno}" ]]; then
echo -n " Line contains: " echo -n " Line contains: "
awk "NR==${lineno}" < ${file} awk "NR==${lineno}" < "${file}"
fi fi
done <<< "${output}" done <<< "${output}"
echo "" echo ""
@ -415,17 +415,22 @@ gravity_DownloadBlocklists() {
total_num=0 total_num=0
parseList() { parseList() {
local adlistID="${1}" src="${2}" target="${3}" incorrect_lines local adlistID="${1}" src="${2}" target="${3}" incorrect_lines
#Append ,${arg} to every line and then remove blank lines before import # This sed does the following things:
# /.$/a\\ ensures there is a newline on the last line # 1. Remove all domains containing invalid characters. Valid are: a-z, A-Z, 0-9, dot (.), minus (-), underscore (_)
sed -e "/[^a-zA-Z0-9.\_-]/d;s/$/,${adlistID}/;/^$/d;/.$/a\\" "${src}" >> "${target}" # 2. Append ,adlistID to every line
# 3. Ensures there is a newline on the last line
sed -e "/[^a-zA-Z0-9.\_-]/d;s/$/,${adlistID}/;/.$/a\\" "${src}" >> "${target}"
# Find (up to) five domains containing invalid characters (see above)
incorrect_lines="$(sed -e "/[^a-zA-Z0-9.\_-]/!d" "${src}" | head -n 5)" incorrect_lines="$(sed -e "/[^a-zA-Z0-9.\_-]/!d" "${src}" | head -n 5)"
local num_lines num_target_lines num_correct_lines percentage percentage_fraction local num_lines num_target_lines num_correct_lines percentage percentage_fraction
# Get number of lines in source file
num_lines="$(grep -c "^" "${src}")" num_lines="$(grep -c "^" "${src}")"
#num_correct_lines="$(grep -c "^[a-zA-Z0-9.-]*$" "${src}")" # Get number of lines in destination file
num_target_lines="$(grep -c "^" "${target}")" num_target_lines="$(grep -c "^" "${target}")"
num_correct_lines="$(( num_target_lines-total_num ))" num_correct_lines="$(( num_target_lines-total_num ))"
total_num="$num_target_lines" total_num="$num_target_lines"
# Compute percentage of valid lines
percentage=100 percentage=100
percentage_fraction=0 percentage_fraction=0
if [[ "${num_lines}" -gt 0 ]]; then if [[ "${num_lines}" -gt 0 ]]; then
@ -433,15 +438,15 @@ parseList() {
percentage_fraction="$(( percentage%10 ))" percentage_fraction="$(( percentage%10 ))"
percentage="$(( percentage/10 ))" percentage="$(( percentage/10 ))"
fi fi
echo " ${INFO} List quality: ${num_correct_lines} of ${num_lines} lines importable (${percentage}.${percentage_fraction}%)" echo " ${INFO} ${num_correct_lines} of ${num_lines} domains imported (${percentage}.${percentage_fraction}%)"
# Display sample of invalid lines if we found some
if [[ -n "${incorrect_lines}" ]]; then if [[ -n "${incorrect_lines}" ]]; then
echo " Example for invalid domains (showing only the first five):" echo " Sample of invalid domains (showing up to five):"
while IFS= read -r line; do while IFS= read -r line; do
echo " - ${line}" echo " - ${line}"
done <<< "${incorrect_lines}" done <<< "${incorrect_lines}"
fi fi
} }
# Download specified URL and perform checks on HTTP status and file content # Download specified URL and perform checks on HTTP status and file content