summaryrefslogtreecommitdiffstats
path: root/scripts/repo-add.sh.in
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/repo-add.sh.in')
-rw-r--r--scripts/repo-add.sh.in66
1 files changed, 42 insertions, 24 deletions
diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in
index 5724022a..006672a0 100644
--- a/scripts/repo-add.sh.in
+++ b/scripts/repo-add.sh.in
@@ -25,8 +25,8 @@ shopt -s extglob
export TEXTDOMAIN='pacman-scripts'
export TEXTDOMAINDIR='@localedir@'
-myver='@PACKAGE_VERSION@'
-confdir='@sysconfdir@'
+declare -r myver='@PACKAGE_VERSION@'
+declare -r confdir='@sysconfdir@'
QUIET=0
DELTA=0
@@ -176,6 +176,11 @@ db_remove_delta() {
if grep -q "$filename" "$deltas"; then
sed -i.backup "/$filename/d" "$deltas" && rm -f "$deltas.backup"
msg2 "$(gettext "Removing existing entry '%s'...")" "$filename"
+ # empty deltas file contains only "%DELTAS%"
+ if (( $(wc -l < "$deltas") == 1 )); then
+ msg2 "$(gettext "Removing empty deltas file ...")"
+ rm "$deltas"
+ fi
return 0
fi
@@ -203,7 +208,7 @@ create_signature() {
gpg --detach-sign --use-agent ${SIGNWITHKEY} "$dbfile" &>/dev/null || ret=$?
if (( ! ret )); then
- msg2 "$(gettext "Created signature file %s.")" "${dbfile##*/}.sig"
+ msg2 "$(gettext "Created signature file %s.")" "${dbfile##*/.tmp.}.sig"
else
warning "$(gettext "Failed to sign package database.")"
fi
@@ -424,13 +429,8 @@ elephant() {
check_repo_db() {
local repodir
- # ensure the path to the DB exists
- if [[ "$LOCKFILE" == /* ]]; then
- repodir=${LOCKFILE%/*}/
- else
- repodir=$PWD/$LOCKFILE
- repodir=${repodir%/*}/
- fi
+ # ensure the path to the DB exists; $LOCKFILE is always an absolute path
+ repodir=${LOCKFILE%/*}/
if [[ ! -d $repodir ]]; then
error "$(gettext "%s does not exist or is not a directory.")" "$repodir"
@@ -579,7 +579,7 @@ if [[ $cmd != "repo-add" && $cmd != "repo-remove" ]]; then
exit 1
fi
-tmpdir=$(mktemp -d /tmp/repo-tools.XXXXXXXXXX) || (\
+tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/repo-tools.XXXXXXXXXX") || (\
error "$(gettext "Cannot create temp directory for database building.")"; \
exit 1)
mkdir "$tmpdir/tree"
@@ -637,7 +637,11 @@ if [[ -z $REPO_DB_FILE ]]; then
exit 1
fi
-LOCKFILE=$REPO_DB_FILE.lck
+if [[ $REPO_DB_FILE == /* ]]; then
+ LOCKFILE=$REPO_DB_FILE.lck
+else
+ LOCKFILE=$PWD/$REPO_DB_FILE.lck
+fi
verify_repo_extension "$REPO_DB_FILE" >/dev/null
check_repo_db
@@ -654,37 +658,51 @@ if (( success )); then
msg "$(gettext "Creating updated database file '%s'")" "$REPO_DB_FILE"
TAR_OPT=$(verify_repo_extension "$REPO_DB_FILE")
+ # $LOCKFILE is already guaranteed to be absolute so this is safe
+ dirname=${LOCKFILE%/*}
filename=${REPO_DB_FILE##*/}
+ # this ensures we create it on the same filesystem, making moves atomic
+ tempname=$dirname/.tmp.$filename
pushd "$tmpdir/tree" >/dev/null
if ( shopt -s nullglob; files=(*); (( ${#files[*]} )) ); then
- bsdtar -c${TAR_OPT}f "$tmpdir/$filename" *
+ bsdtar -c${TAR_OPT}f "$tempname" *
else
# we have no packages remaining? zip up some emptyness
warning "$(gettext "No packages remain, creating empty database.")"
- bsdtar -c${TAR_OPT}f "$tmpdir/$filename" -T /dev/null
+ bsdtar -c${TAR_OPT}f "$tempname" -T /dev/null
fi
popd >/dev/null
- create_signature "$tmpdir/$filename"
+ create_signature "$tempname"
- [[ -f $REPO_DB_FILE ]] && mv -f "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
+ # hardlink or move the previous version of the database and signature to .old
+ # extension as a backup measure
+ if [[ -f $REPO_DB_FILE ]]; then
+ ln -f "$REPO_DB_FILE" "$REPO_DB_FILE.old" 2>/dev/null || \
+ mv -f "$REPO_DB_FILE" "$REPO_DB_FILE.old"
+ fi
if [[ -f $REPO_DB_FILE.sig ]]; then
- mv -f "$REPO_DB_FILE.sig" "$REPO_DB_FILE.old.sig"
+ ln -f "$REPO_DB_FILE.sig" "$REPO_DB_FILE.old.sig" 2>/dev/null || \
+ mv -f "$REPO_DB_FILE.sig" "$REPO_DB_FILE.old.sig"
else
rm -f "$REPO_DB_FILE.old.sig"
fi
- [[ -f $tmpdir/$filename ]] && mv "$tmpdir/$filename" "$REPO_DB_FILE"
- [[ -f $tmpdir/$filename.sig ]] && mv "$tmpdir/$filename.sig" "$REPO_DB_FILE.sig"
+
+ # rotate the newly-created database and signature into place
+ mv "$tempname" "$REPO_DB_FILE"
+ if [[ -f $tempname.sig ]]; then
+ mv "$tempname.sig" "$REPO_DB_FILE.sig"
+ fi
+
dblink=${REPO_DB_FILE%.tar*}
- target=${REPO_DB_FILE##*/}
rm -f "$dblink" "$dblink.sig"
- ln -s "$target" "$dblink" 2>/dev/null || \
- ln "$target" "$dblink" 2>/dev/null || \
+ ln -s "$filename" "$dblink" 2>/dev/null || \
+ ln "$filename" "$dblink" 2>/dev/null || \
cp "$REPO_DB_FILE" "$dblink"
if [[ -f "$REPO_DB_FILE.sig" ]]; then
- ln -s "$target.sig" "$dblink.sig" 2>/dev/null || \
- ln "$target.sig" "$dblink.sig" 2>/dev/null || \
+ ln -s "$filename.sig" "$dblink.sig" 2>/dev/null || \
+ ln "$filename.sig" "$dblink.sig" 2>/dev/null || \
cp "$REPO_DB_FILE.sig" "$dblink.sig"
fi
else