[pacman-dev] [PATCH 2/2] pacman-optimize: fix database locking race conditions

Denis A. Altoé Falqueto denisfalqueto at gmail.com
Mon Jun 21 13:07:45 EDT 2010


On Mon, Jun 21, 2010 at 1:55 PM, Andres P <aepd87 at gmail.com> wrote:
> Instead of making the lock file with touch, use mkdir since it's the only
> portable atomic transaction available to shell scripts.

In fact, there is another way to do an atomic operation. Look at
repo-add script, in function check_repo_db (I've seen this advice in
some article some time ago too..). The first command tries to create a
file with set -o noclobber. So, if the file already exists, it will
fail and will not alter the lock file. If it doesn't exist, the lock
file will be created and the process will proceed.

if (set -o noclobber; echo "$$" > "$LOCKFILE") 2>/dev/null; then
  # We acquired the lock
else
  # The lock already exists and we can't proceed.
fi

This would avoid changing the db.lock to a directory. Maybe this could
break some kind of script or utility out there...

-- 
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

-------------------------------------------
Denis A. Altoe Falqueto
-------------------------------------------


More information about the pacman-dev mailing list