Bläddra i källkod

Make modstatdb locking functions not take an admindir argument

Initialize the lockfile on modstatdb_init() via the fnis array. Make
sure we call modstatdb_init() and modstatdb_done() in case we are not
calling modstatdb_open() and modstatdb_shutdown().
Guillem Jover 15 år sedan
förälder
incheckning
6c9093777f
4 ändrade filer med 15 tillägg och 20 borttagningar
  1. 7 16
      lib/dpkg/dbmodify.c
  2. 2 2
      lib/dpkg/dpkg-db.h
  3. 1 1
      src/enquiry.c
  4. 5 1
      src/update.c

+ 7 - 16
lib/dpkg/dbmodify.c

@@ -49,6 +49,7 @@
 static bool db_initialized;
 
 static enum modstatdb_rw cstatus=-1, cflags=0;
+static char *lockfile;
 static char *statusfile, *availablefile;
 static char *importanttmpfile=NULL;
 static FILE *importanttmp;
@@ -136,6 +137,7 @@ static const struct fni {
   const char *suffix;
   char **store;
 } fnis[] = {
+  {   LOCKFILE,                   &lockfile           },
   {   STATUSFILE,                 &statusfile         },
   {   AVAILFILE,                  &availablefile      },
   {   UPDATESDIR,                 &updatesdir         },
@@ -184,14 +186,11 @@ modstatdb_done(void)
 static int dblockfd = -1;
 
 bool
-modstatdb_is_locked(const char *admindir)
+modstatdb_is_locked(void)
 {
-  char *lockfile;
   int lockfd;
   bool locked;
 
-  m_asprintf(&lockfile, "%s/%s", admindir, LOCKFILE);
-
   if (dblockfd == -1) {
     lockfd = open(lockfile, O_RDONLY);
     if (lockfd == -1)
@@ -207,20 +206,14 @@ modstatdb_is_locked(const char *admindir)
   if (dblockfd == -1)
     close(lockfd);
 
-  free(lockfile);
-
   return locked;
 }
 
 void
-modstatdb_lock(const char *admindir)
+modstatdb_lock(void)
 {
-  char *dblockfile = NULL;
-
-  m_asprintf(&dblockfile, "%s/%s", admindir, LOCKFILE);
-
   if (dblockfd == -1) {
-    dblockfd = open(dblockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
+    dblockfd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
     if (dblockfd == -1) {
       if (errno == EPERM)
         ohshit(_("you do not have permission to lock the dpkg status database"));
@@ -228,9 +221,7 @@ modstatdb_lock(const char *admindir)
     }
   }
 
-  file_lock(&dblockfd, FILE_LOCK_NOWAIT, dblockfile, _("dpkg status database"));
-
-  free(dblockfile);
+  file_lock(&dblockfd, FILE_LOCK_NOWAIT, lockfile, _("dpkg status database"));
 }
 
 void
@@ -262,7 +253,7 @@ modstatdb_open(const char *admindir, enum modstatdb_rw readwritereq)
         ohshit(_("operation requires read/write access to dpkg status area"));
       cstatus= msdbrw_readonly;
     } else {
-      modstatdb_lock(admindir);
+      modstatdb_lock();
       cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
                 msdbrw_needsuperuserlockonly :
                 msdbrw_write);

+ 2 - 2
lib/dpkg/dpkg-db.h

@@ -224,8 +224,8 @@ enum modstatdb_rw {
 
 void modstatdb_init(const char *admindir);
 void modstatdb_done(void);
-bool modstatdb_is_locked(const char *admindir);
-void modstatdb_lock(const char *admindir);
+bool modstatdb_is_locked(void);
+void modstatdb_lock(void);
 void modstatdb_unlock(void);
 enum modstatdb_rw modstatdb_open(const char *admindir, enum modstatdb_rw reqrwflags);
 void modstatdb_note(struct pkginfo *pkg);

+ 1 - 1
src/enquiry.c

@@ -145,7 +145,7 @@ void audit(const char *const *argv) {
     while ((pkg = pkg_db_iter_next(it))) {
       if (!bsi->yesno(pkg,bsi)) continue;
       if (!head_running) {
-        if (modstatdb_is_locked(admindir))
+        if (modstatdb_is_locked())
           puts(_(
 "Another process has locked the database for writing, and might currently be\n"
 "modifying it, some of the following problems might just be due to that.\n"));

+ 5 - 1
src/update.c

@@ -39,6 +39,8 @@ void updateavailable(const char *const *argv) {
   int count= 0;
   static struct varbuf vb;
 
+  modstatdb_init(admindir);
+
   switch (cipaction->arg_int) {
   case act_avclear:
     if (sourcefile) badusage(_("--%s takes no arguments"),cipaction->olong);
@@ -58,7 +60,7 @@ void updateavailable(const char *const *argv) {
       else
         ohshit(_("bulk available update requires write access to dpkg status area"));
     }
-    modstatdb_lock(admindir);
+    modstatdb_lock();
   }
 
   switch (cipaction->arg_int) {
@@ -95,6 +97,8 @@ void updateavailable(const char *const *argv) {
   if (cipaction->arg_int != act_avclear)
     printf(P_("Information about %d package was updated.\n",
               "Information about %d packages was updated.\n", count), count);
+
+  modstatdb_done();
 }
 
 void forgetold(const char *const *argv) {