Bläddra i källkod

libdpkg: Use new writedb flags instead of independent bool variables

Using bool to pass flags to a function already taking several arguments
is generally a bad interface, as it's not clear from the call sites what
each of them refer to. Switch to a proper enum with named flags instead.
Guillem Jover 15 år sedan
förälder
incheckning
f65316a5ec
4 ändrade filer med 17 tillägg och 10 borttagningar
  1. 3 3
      lib/dpkg/dbmodify.c
  2. 8 1
      lib/dpkg/dpkg-db.h
  3. 5 5
      lib/dpkg/dump.c
  4. 1 1
      src/update.c

+ 3 - 3
lib/dpkg/dbmodify.c

@@ -95,7 +95,7 @@ static void cleanupdates(void) {
     }
 
     if (cstatus >= msdbrw_write) {
-      writedb(statusfile,0,1);
+      writedb(statusfile, wdb_must_sync);
 
       for (i=0; i<cdn; i++) {
         strcpy(updatefnrest, cdlist[i]->d_name);
@@ -301,7 +301,7 @@ void modstatdb_checkpoint(void) {
   int i;
 
   assert(cstatus >= msdbrw_write);
-  writedb(statusfile,0,1);
+  writedb(statusfile, wdb_must_sync);
 
   for (i=0; i<nextupdate; i++) {
     sprintf(updatefnrest, IMPORTANTFMT, i);
@@ -318,7 +318,7 @@ void modstatdb_checkpoint(void) {
 
 void modstatdb_shutdown(void) {
   if (cflags >= msdbrw_available_write)
-    writedb(availablefile, 1, 0);
+    writedb(availablefile, wdb_dump_available);
 
   switch (cstatus) {
   case msdbrw_write:

+ 8 - 1
lib/dpkg/dpkg-db.h

@@ -299,7 +299,14 @@ const char *versiondescribe(const struct versionrevision*,
 void writerecord(FILE*, const char*,
                  const struct pkginfo *, const struct pkgbin *);
 
-void writedb(const char *filename, bool available, bool mustsync);
+enum writedb_flags {
+  /* Dump ‘available’ in-core structures, not ‘status’. */
+  wdb_dump_available = 001,
+  /* Must sync the written file. */
+  wdb_must_sync = 002,
+};
+
+void writedb(const char *filename, enum writedb_flags flags);
 
 /* Note: The varbufs must have been initialized and will not be
  * NUL-terminated. */

+ 5 - 5
lib/dpkg/dump.c

@@ -399,7 +399,7 @@ writerecord(FILE *file, const char *filename,
 }
 
 void
-writedb(const char *filename, bool available, bool mustsync)
+writedb(const char *filename, enum writedb_flags flags)
 {
   static char writebuf[8192];
 
@@ -412,7 +412,7 @@ writedb(const char *filename, bool available, bool mustsync)
   struct varbuf vb = VARBUF_INIT;
   int old_umask;
 
-  which = available ? "available" : "status";
+  which = (flags & wdb_dump_available) ? "available" : "status";
   m_asprintf(&oldfn, "%s%s", filename, OLDDBEXT);
   m_asprintf(&newfn, "%s%s", filename, NEWDBEXT);
 
@@ -427,7 +427,7 @@ writedb(const char *filename, bool available, bool mustsync)
 
   it = pkg_db_iter_new();
   while ((pigp = pkg_db_iter_next(it)) != NULL) {
-    pifp= available ? &pigp->available : &pigp->installed;
+    pifp = (flags & wdb_dump_available) ? &pigp->available : &pigp->installed;
     /* Don't dump records which have no useful content. */
     if (!pkg_is_informative(pigp, pifp))
       continue;
@@ -441,7 +441,7 @@ writedb(const char *filename, bool available, bool mustsync)
   }
   pkg_db_iter_free(it);
   varbuf_destroy(&vb);
-  if (mustsync) {
+  if (flags & wdb_must_sync) {
     if (fflush(file))
       ohshite(_("failed to flush %s database to '%.250s'"), which, filename);
     if (fsync(fileno(file)))
@@ -458,7 +458,7 @@ writedb(const char *filename, bool available, bool mustsync)
     ohshite(_("failed to install '%.250s' as '%.250s' containing %s database"),
             newfn, filename, which);
 
-  if (mustsync)
+  if (flags & wdb_must_sync)
     dir_sync_path_parent(filename);
 
   free(newfn);

+ 1 - 1
src/update.c

@@ -90,7 +90,7 @@ updateavailable(const char *const *argv)
                      NULL);
 
   if (!f_noact) {
-    writedb(availfile, 1, 0);
+    writedb(availfile, wdb_dump_available);
     modstatdb_unlock();
   }