Serialize rule writes and add tests

This commit is contained in:
2026-04-17 10:21:37 +08:00
parent 66c25b06a7
commit eb8c76492c
7 changed files with 682 additions and 35 deletions

View File

@@ -34,20 +34,30 @@ storage_init() {
chmod 600 "${lock}"
}
storage_add() {
local line=${1-}
local db lock
[[ -n ${line} ]] || return 1
storage_with_lock() {
local lock
storage_init
db=$(storage_db_path)
lock=$(storage_lock_path)
(
flock -x 9
printf '%s\n' "${line}" >>"${db}"
"$@"
) 9>>"${lock}"
}
storage_add_unlocked() {
local line=${1-}
local db
[[ -n ${line} ]] || return 1
storage_init
db=$(storage_db_path)
printf '%s\n' "${line}" >>"${db}"
}
storage_add() {
storage_with_lock storage_add_unlocked "$@"
}
storage_list() {
local db
db=$(storage_db_path)
@@ -83,34 +93,34 @@ storage_get() {
return 1
}
storage_delete() {
storage_delete_unlocked() {
local uuid=${1-}
local db lock tmp found=0 line current
local db tmp found=0 line current
[[ -n ${uuid} ]] || return 1
storage_init
db=$(storage_db_path)
lock=$(storage_lock_path)
tmp="${db}.tmp.$$"
(
flock -x 9
: >"${tmp}"
while IFS= read -r line || [[ -n ${line} ]]; do
current=$(storage_parse "${line}" uuid || true)
if [[ ${current} == "${uuid}" ]]; then
found=1
continue
fi
printf '%s\n' "${line}" >>"${tmp}"
done <"${db}"
if (( found == 0 )); then
rm -f "${tmp}"
exit 1
: >"${tmp}"
while IFS= read -r line || [[ -n ${line} ]]; do
current=$(storage_parse "${line}" uuid || true)
if [[ ${current} == "${uuid}" ]]; then
found=1
continue
fi
printf '%s\n' "${line}" >>"${tmp}"
done <"${db}"
mv "${tmp}" "${db}"
) 9>>"${lock}"
if (( found == 0 )); then
rm -f "${tmp}"
return 1
fi
mv "${tmp}" "${db}"
}
storage_delete() {
storage_with_lock storage_delete_unlocked "$@"
}
storage_count() {