#!/bin/bash
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
###############################################################################
_db_key2path() {
local key="${1}"
assert isset key
print "${NETWORK_DB_DIR}/${key}"
}
db_exists() {
local key="${1}"
local path="$(_db_key2path "${key}")"
if [ -e "${path}" ]; then
return ${EXIT_TRUE}
fi
return ${EXIT_FALSE}
}
db_get() {
local key="${1}"
local path="$(_db_key2path "${key}")"
fread "${path}"
}
db_set() {
local key="${1}"
shift
local value="$@"
local path="$(_db_key2path "${key}")"
if [ -r "${path}" ]; then
log DEBUG "Setting '${key}' = '${value}'"
else
log DEBUG "Adding '${key}' = '${value}'"
fi
make_parent_dir "${path}"
print "${value}" > "${path}"
}
db_delete() {
local key="${1}"
local path="$(_db_key2path "${key}")"
log DEBUG "Deleting key = '${key}'"
if [ -d "${path}" ]; then
_db_delete_recursive "${path}"
fi
rm -f - "${path}"
}
_db_delete_recursive() {
local path="${1}"
local key
for key in $(_db_list_path "${path}"); do
db_delete "${key}"
done
rmdir "${path}"
}
db_dump() {
_db_dump_recursive "${NETWORK_DB_DIR}"
}
_db_dump_key() {
local key="${1}"
local val="$(db_get "${key}")"
printf "%-38s = %s\n" "${key}" "${val}"
}
_db_dump_recursive() {
local path="${1}"
local key
for key in $(_db_list_path "${path}"); do
path="$(_db_key2path "${key}")"
if [ -d "${path}" ]; then
_db_dump_recursive "${path}"
else
_db_dump_key "${key}"
fi
done
}
_db_list_path() {
local path="${1}"
local element
for element in ${path}/*; do
[ -e "${element}" ] || continue
print "${element#${NETWORK_DB_DIR}/}"
done
}
db_connection_init() {
if [ -e "${DB_CONNECTION_FILE}" ]; then
return ${EXIT_OK}
fi
log DEBUG "Creating connection database ${DB_CONNECTION_FILE}."
sqlite3 -batch ${DB_CONNECTION_FILE} <