Review comments

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2019-02-06 18:57:48 +01:00
parent 9984647ebb
commit 644ec36e64
No known key found for this signature in database
GPG Key ID: FB60471F0575164A
2 changed files with 28 additions and 15 deletions

View File

@ -1,7 +1,10 @@
CREATE TABLE whitelist (domain TEXT UNIQUE NOT NULL, enabled BOOLEAN DEFAULT 1, comment TEXT, dateadded DATETIME); CREATE TABLE whitelist (domain TEXT UNIQUE NOT NULL, enabled BOOLEAN NOT NULL DEFAULT 1, date_added DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, comment TEXT);
CREATE TABLE blacklist (domain TEXT UNIQUE NOT NULL, enabled BOOLEAN DEFAULT 1, comment TEXT, dateadded DATETIME); CREATE TABLE blacklist (domain TEXT UNIQUE NOT NULL, enabled BOOLEAN NOT NULL DEFAULT 1, date_added DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, comment TEXT);
CREATE TABLE regex (filter TEXT UNIQUE NOT NULL, enabled BOOLEAN DEFAULT 1, comment TEXT, dateadded DATETIME); CREATE TABLE regex (filter TEXT UNIQUE NOT NULL, enabled BOOLEAN NOT NULL DEFAULT 1, date_added DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, comment TEXT);
CREATE TABLE gravity (domain TEXT UNIQUE NOT NULL); CREATE TABLE gravity (domain TEXT UNIQUE NOT NULL);
CREATE TABLE info (property TEXT NOT NULL, value TEXT NOT NULL);
INSERT INTO info VALUES("version","1");
CREATE VIEW vw_gravity AS SELECT DISTINCT a.domain CREATE VIEW vw_gravity AS SELECT DISTINCT a.domain
FROM gravity a FROM gravity a

View File

@ -20,6 +20,8 @@ source "${regexconverter}"
basename="pihole" basename="pihole"
PIHOLE_COMMAND="/usr/local/bin/${basename}" PIHOLE_COMMAND="/usr/local/bin/${basename}"
PIHOLE_USER="pihole"
PIHOLE_GROUP="pihole"
piholeDir="/etc/${basename}" piholeDir="/etc/${basename}"
@ -90,6 +92,7 @@ fi
# Generate new sqlite3 file from schema template # Generate new sqlite3 file from schema template
generate_gravity_database() { generate_gravity_database() {
sqlite3 "${gravityDBfile}" < "${gravityDBschema}" sqlite3 "${gravityDBfile}" < "${gravityDBschema}"
chown $PIHOLE_USER:$PIHOLE_GROUP "${gravityDBfile}"
} }
# Import domains from file and store them in the specified database table # Import domains from file and store them in the specified database table
@ -113,25 +116,32 @@ gravity_store_in_database() {
gravity_Cleanup "error" gravity_Cleanup "error"
fi fi
# Store domains in gravity database table ${table} local tmpFile=$(mktemp -p "/tmp" --suffix=".gravity")
output=$( { sqlite3 "${gravityDBfile}" <<< ".import \"${source}\" ${table}"; } 2>&1 )
status="$?"
if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to create ${table} in database ${gravityDBfile}\\n ${output}"
gravity_Cleanup "error"
fi
if [ "$table" == "whitelist" ] || [ "$table" == "blacklist" ] || [ "$table" == "regex" ]; then if [ "$table" == "whitelist" ] || [ "$table" == "blacklist" ] || [ "$table" == "regex" ]; then
# Set enabled to true where it is unspecified # Apply format for white-, blacklist, and regex tables, prevent globbing
output=$( { sqlite3 "${gravityDBfile}" <<< "UPDATE ${table} SET enabled = 1 WHERE enabled IS NULL;"; } 2>&1 ) set -f
for domain in $(cat < "${source}"); do
echo "\"${domain}\",1,$(date --utc +'%s')," >> "${tmpFile}"
done
set +f
inputfile="${tmpFile}"
else
# No need to modify the input data for the gravity table
inputfile="${source}"
fi
# Store domains in gravity database table ${table}
# Use printf as .mode and .import need to be on separate lines
# see https://unix.stackexchange.com/a/445615/83260
output=$( { printf ".mode csv\n.import \"${inputfile}\" ${table}\n" | sqlite3 "${gravityDBfile}"; } 2>&1 )
status="$?" status="$?"
if [[ "${status}" -ne 0 ]]; then if [[ "${status}" -ne 0 ]]; then
echo -e "\\n ${CROSS} Unable to set enabled states (${table}) in database ${gravityDBfile}\\n ${output}" echo -e "\\n ${CROSS} Unable to fill table ${table} in database ${gravityDBfile}\\n ${output}"
gravity_Cleanup "error" gravity_Cleanup "error"
fi fi
fi
# Delete tmpfile
#rm "$tmpFile" > /dev/null 2>&1 || true
# Only create template file if asked to # Only create template file if asked to
if [ "${template}" != "-" ]; then if [ "${template}" != "-" ]; then