Browse Source

libdpkg: Refactor modstatdb init/done sequence into new functions

Move variable path initialization and destruction code to the new
modstatdb_init() and modstatdb_done(). Add a boolean variable to protect
the funtions from reiterated calls.
Guillem Jover 15 years ago
parent
commit
be596facc4
3 changed files with 45 additions and 17 deletions
  1. 42 17
      lib/dpkg/dbmodify.c
  2. 2 0
      lib/dpkg/dpkg-db.h
  3. 1 0
      lib/dpkg/libdpkg.Versions

+ 42 - 17
lib/dpkg/dbmodify.c

@@ -35,6 +35,7 @@
 #include <fcntl.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
 
@@ -45,6 +46,8 @@
 #include <dpkg/dir.h>
 #include <dpkg/triglib.h>
 
+static bool db_initialized;
+
 static enum modstatdb_rw cstatus=-1, cflags=0;
 static char *statusfile, *availablefile;
 static char *importanttmpfile=NULL;
@@ -141,6 +144,43 @@ static const struct fni {
   {   NULL, NULL                                      }
 };
 
+void
+modstatdb_init(const char *admindir)
+{
+  const struct fni *fnip;
+
+  if (db_initialized)
+    return;
+
+  for (fnip = fnis; fnip->suffix; fnip++) {
+    free(*fnip->store);
+    m_asprintf(fnip->store, "%s/%s", admindir, fnip->suffix);
+  }
+
+  updatefnbuf = m_malloc(strlen(updatesdir) + IMPORTANTMAXLEN + 5);
+  strcpy(updatefnbuf, updatesdir);
+  updatefnrest = updatefnbuf + strlen(updatefnbuf);
+
+  db_initialized = true;
+}
+
+void
+modstatdb_done(void)
+{
+  const struct fni *fnip;
+
+  if (!db_initialized)
+    return;
+
+  for (fnip = fnis; fnip->suffix; fnip++) {
+    free(*fnip->store);
+    *fnip->store = NULL;
+  }
+  free(updatefnbuf);
+
+  db_initialized = false;
+}
+
 static int dblockfd = -1;
 
 bool
@@ -203,12 +243,7 @@ modstatdb_unlock(void)
 enum modstatdb_rw
 modstatdb_open(const char *admindir, enum modstatdb_rw readwritereq)
 {
-  const struct fni *fnip;
-
-  for (fnip=fnis; fnip->suffix; fnip++) {
-    free(*fnip->store);
-    m_asprintf(fnip->store, "%s/%s", admindir, fnip->suffix);
-  }
+  modstatdb_init(admindir);
 
   cflags = readwritereq & msdbrw_available_mask;
   readwritereq &= ~msdbrw_available_mask;
@@ -239,10 +274,6 @@ modstatdb_open(const char *admindir, enum modstatdb_rw readwritereq)
     internerr("unknown modstatdb_rw '%d'", readwritereq);
   }
 
-  updatefnbuf = m_malloc(strlen(updatesdir) + IMPORTANTMAXLEN + 5);
-  strcpy(updatefnbuf, updatesdir);
-  updatefnrest= updatefnbuf+strlen(updatefnbuf);
-
   if (cstatus != msdbrw_needsuperuserlockonly) {
     cleanupdates();
     if (cflags >= msdbrw_available_readonly)
@@ -282,8 +313,6 @@ void modstatdb_checkpoint(void) {
 }
 
 void modstatdb_shutdown(void) {
-  const struct fni *fnip;
-
   if (cflags >= msdbrw_available_write)
     writedb(availablefile, 1, 0);
 
@@ -301,11 +330,7 @@ void modstatdb_shutdown(void) {
     break;
   }
 
-  for (fnip=fnis; fnip->suffix; fnip++) {
-    free(*fnip->store);
-    *fnip->store= NULL;
-  }
-  free(updatefnbuf);
+  modstatdb_done();
 }
 
 static void

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

@@ -222,6 +222,8 @@ enum modstatdb_rw {
   msdbrw_available_write = 0200,
 };
 
+void modstatdb_init(const char *admindir);
+void modstatdb_done(void);
 bool modstatdb_is_locked(const char *admindir);
 void modstatdb_lock(const char *admindir);
 void modstatdb_unlock(void);

+ 1 - 0
lib/dpkg/libdpkg.Versions

@@ -232,6 +232,7 @@ LIBDPKG_PRIVATE {
 	modstatdb_note_ifwrite;
 	modstatdb_checkpoint;
 	modstatdb_shutdown;
+	modstatdb_done;
 
 	# Triggers support
 	trig_name_is_illegal;