The following commit has been merged in the master branch:
commit 0cb6f131d85f48f0dc8a90ca1329af84c6f6ebc1
Author: Guillem Jover <[email protected]>
Date: Sun Sep 12 07:22:21 2010 +0200
libdpkg: Refactor file locking error message handling
Pass only the description of the resource being locked, and move generic
error strings inside the file lock funtions. Instead of changing locking
behaviour depending on the error strings passed, pass an explicit enum to
select it.
diff --git a/lib/dpkg/dbmodify.c b/lib/dpkg/dbmodify.c
index fb2aeb2..0366ce4 100644
--- a/lib/dpkg/dbmodify.c
+++ b/lib/dpkg/dbmodify.c
@@ -195,9 +195,7 @@ modstatdb_lock(const char *admindir)
}
}
- file_lock(&dblockfd, dblockfile,
- _("unable to lock dpkg status database"),
- _("status database area is locked by another process"));
+ file_lock(&dblockfd, FILE_LOCK_NOWAIT, dblockfile, _("dpkg status
database"));
free(dblockfile);
}
diff --git a/lib/dpkg/file.c b/lib/dpkg/file.c
index 40fbbea..3c817ee 100644
--- a/lib/dpkg/file.c
+++ b/lib/dpkg/file.c
@@ -67,6 +67,7 @@ static void
file_unlock_cleanup(int argc, void **argv)
{
int lockfd = *(int*)argv[0];
+ const char *lock_desc = argv[1];
struct flock fl;
assert(lockfd >= 0);
@@ -74,7 +75,7 @@ file_unlock_cleanup(int argc, void **argv)
file_lock_setup(&fl, F_UNLCK);
if (fcntl(lockfd, F_SETLK, &fl) == -1)
- ohshite(_("unable to unlock dpkg status database"));
+ ohshite(_("unable to unlock %s"), lock_desc);
}
void
@@ -108,21 +109,28 @@ file_is_locked(int lockfd, const char *filename)
/* lockfd must be allocated statically as its addresses is passed to
* a cleanup handler. */
void
-file_lock(int *lockfd, const char *filename,
- const char *emsg, const char *emsg_eagain)
+file_lock(int *lockfd, enum file_lock_flags flags, const char *filename,
+ const char *desc)
{
struct flock fl;
+ int lock_cmd;
setcloexec(*lockfd, filename);
file_lock_setup(&fl, F_WRLCK);
- if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
- if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
- ohshit(emsg_eagain);
- ohshite(emsg);
+ if (flags == FILE_LOCK_WAIT)
+ lock_cmd = F_SETLKW;
+ else
+ lock_cmd = F_SETLK;
+
+ if (fcntl(*lockfd, lock_cmd, &fl) == -1) {
+ if (errno == EACCES || errno == EAGAIN)
+ ohshit(_("%s is locked by another process"), desc);
+ else
+ ohshite(_("unable to lock %s"), desc);
}
- push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 1, lockfd);
+ push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 2, lockfd, desc);
}
diff --git a/lib/dpkg/file.h b/lib/dpkg/file.h
index 24dca2c..ce16c04 100644
--- a/lib/dpkg/file.h
+++ b/lib/dpkg/file.h
@@ -32,9 +32,14 @@ DPKG_BEGIN_DECLS
*/
void file_copy_perms(const char *src, const char *dst);
+enum file_lock_flags {
+ FILE_LOCK_NOWAIT,
+ FILE_LOCK_WAIT,
+};
+
bool file_is_locked(int lockfd, const char *filename);
-void file_lock(int *lockfd, const char *filename,
- const char *emsg, const char *emsg_eagain);
+void file_lock(int *lockfd, enum file_lock_flags flags, const char *filename,
+ const char *desc);
void file_unlock(void);
DPKG_END_DECLS
diff --git a/lib/dpkg/trigdeferred.l b/lib/dpkg/trigdeferred.l
index 3be1e83..7bd4e91 100644
--- a/lib/dpkg/trigdeferred.l
+++ b/lib/dpkg/trigdeferred.l
@@ -126,8 +126,7 @@ trigdef_update_start(enum trigdef_updateflags uf, const
char *admindir)
}
}
- file_lock(&lock_fd, fn.buf, _("unable to lock triggers area"),
- NULL);
+ file_lock(&lock_fd, FILE_LOCK_WAIT, fn.buf, _("triggers area"));
} else {
/* Dummy for pop_cleanups. */
push_cleanup(NULL, 0, NULL, 0, 0);
--
dpkg's main repository
--
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]