Просмотр исходного кода

Refactor status-fd duplicate output code into a new function

Based on a patch by Ian Jackson <ian@davenant.greenend.org.uk>.
Guillem Jover лет назад: 18
Родитель
Сommit
ce0455f7c4
8 измененных файлов с 59 добавлено и 58 удалено
  1. 14 0
      ChangeLog
  2. 0 1
      TODO
  3. 1 17
      lib/dbmodify.c
  4. 0 6
      lib/dpkg-db.h
  5. 9 0
      lib/dpkg.h
  6. 33 0
      lib/log.c
  7. 1 17
      src/configure.c
  8. 1 17
      src/errors.c

+ 14 - 0
ChangeLog

@@ -1,3 +1,17 @@
+2008-03-25  Ian Jackson  <ian@davenant.greenend.org.uk>,
+            Guillem Jover  <guillem@debian.org>
+
+	* lib/dbmodify.c (status_pipes): Move definitions to ...
+	* lib/log.c: ... here.
+	* lib/dpkg-db.h (struct pipef, status_pipes): Move declarations to ...
+	* lib/dpkg.h: ... here.
+	(statusfd_send): New function protoype.
+	* lib/log.c: Include <assert.h> and <unistd.h>.
+	(statusfd_send): New function.
+	* lib/dbmodify.c: Use statusfd_send instead of duplicate code.
+	* src/configure.c: Likewise.
+	* src/errors.c: Likewise.
+
 2008-03-25  Guillem Jover  <guillem@debian.org>
 
 	* lib/dbmodify.c (log_file, log_message): Move to ...

+ 0 - 1
TODO

@@ -20,7 +20,6 @@ lenny
 ------
 
  * Code cleanup:
-   - Coalesce status_pipe handling.
    - blankfoo embedded code.
    - static variables inside functions.
    - Coalesce admindir / infodir / foodir generation.

+ 1 - 17
lib/dbmodify.c

@@ -234,30 +234,14 @@ void modstatdb_shutdown(void) {
   free(updatefnbuf);
 }
 
-struct pipef *status_pipes= NULL;
-
 void modstatdb_note(struct pkginfo *pkg) {
   assert(cstatus >= msdbrw_write);
 
   onerr_abort++;
 
-  if (status_pipes) {
-    static struct varbuf *status= NULL;
-    struct pipef *pipef= status_pipes;
-    int r;
-    if (status == NULL) {
-      status = nfmalloc(sizeof(struct varbuf));
-      varbufinit(status);
-    } else
-      varbufreset(status);
-    r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status].name);
-    while (pipef) {
-      write(pipef->fd, status->buf, r);
-      pipef= pipef->next;
-    }
-  }
   log_message("status %s %s %s", statusinfos[pkg->status].name, pkg->name,
 	      versiondescribe(&pkg->installed.version, vdew_nonambig));
+  statusfd_send("status: %s: %s", pkg->name, statusinfos[pkg->status].name);
 
   varbufreset(&uvb);
   varbufrecord(&uvb, pkg, &pkg->installed);

+ 0 - 6
lib/dpkg-db.h

@@ -166,12 +166,6 @@ enum modstatdb_rw {
   msdbrw_noavail= 0100,
 };
 
-struct pipef {
-  int fd;
-  struct pipef *next;
-};
-extern struct pipef *status_pipes;
-
 enum modstatdb_rw modstatdb_init(const char *admindir, enum modstatdb_rw reqrwflags);
 void modstatdb_note(struct pkginfo *pkg);
 void modstatdb_checkpoint(void);

+ 9 - 0
lib/dpkg.h

@@ -212,6 +212,15 @@ void warningf(const char *fmt, ...) PRINTFFORMAT(1, 2);
 extern const char *log_file;
 void log_message(const char *fmt, ...) PRINTFFORMAT(1, 2);
 
+/* FIXME: pipef and status_pipes should not be publicly exposed. */
+struct pipef {
+  int fd;
+  struct pipef *next;
+};
+extern struct pipef *status_pipes;
+
+void statusfd_send(const char *fmt, ...);
+
 /*** cleanup.c ***/
 
 void cu_closefile(int argc, void **argv);

+ 33 - 0
lib/log.c

@@ -21,8 +21,10 @@
 
 #include <config.h>
 
+#include <assert.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <time.h>
 #include <errno.h>
 
@@ -72,3 +74,34 @@ log_message(const char *fmt, ...)
 	fprintf(logfd, "%s %s\n", time_str, log->buf);
 }
 
+struct pipef *status_pipes = NULL;
+
+void
+statusfd_send(const char *fmt, ...)
+{
+	static struct varbuf vb;
+	struct pipef *pipef;
+	const char *p;
+	int r, l;
+	va_list al;
+
+	if (!status_pipes)
+		return;
+
+	va_start(al, fmt);
+	varbufreset(&vb);
+	varbufvprintf(&vb, fmt, al);
+	varbufaddc(&vb, '\n');
+	va_end(al);
+
+	for (pipef = status_pipes; pipef; pipef = pipef->next) {
+		for (p = vb.buf, l = vb.used; l;  p += r, l -= r) {
+			r = write(pipef->fd, vb.buf, vb.used);
+			if (r < 0)
+				ohshite("unable to write to status fd %d",
+				        pipef->fd);
+			assert(r && r <= l);
+		}
+	}
+}
+

+ 1 - 17
src/configure.c

@@ -531,25 +531,9 @@ static enum conffopt promptconfaction(const char* cfgfile, const char* realold,
 	if (!(what&cfof_prompt))
 		return what;
 
-	/* if there is a status pipe, send conffile-prompt there */
-	if (status_pipes) {
-	   static struct varbuf *status= NULL;
-	   struct pipef *pipef= status_pipes;
-	   int r;
-	   if (status == NULL) {
-	      status = nfmalloc(sizeof(struct varbuf));
-	      varbufinit(status);
-	   } else
-	      varbufreset(status);
-	   
-	   r= varbufprintf(status, "status: %s : %s : '%s' '%s' %i %i \n", 
+	statusfd_send("status: %s : %s : '%s' '%s' %i %i ",
 			   cfgfile, "conffile-prompt", 
 			   realold, realnew, useredited, distedited);
-	   while (pipef) {
-	      write(pipef->fd, status->buf, r);
-	      pipef= pipef->next;
-	   }
-	}
 
 	do {
 		/* Flush the terminal's input in case the user

+ 1 - 17
src/errors.c

@@ -56,23 +56,7 @@ void print_error_perpackage(const char *emsg, const char *arg) {
   fprintf(stderr, _("%s: error processing %s (--%s):\n %s\n"),
           DPKG, arg, cipaction->olong, emsg);
 
-  if (status_pipes) {
-     static struct varbuf *status= NULL;
-     struct pipef *pipef= status_pipes;
-     int r;
-     if (status == NULL) {
-	status = nfmalloc(sizeof(struct varbuf));
-	varbufinit(status);
-     } else
-	varbufreset(status);
-
-     r= varbufprintf(status, "status: %s : %s : %s\n", arg, "error",emsg);
-     while (pipef) {
-	write(pipef->fd, status->buf, r);
-	pipef= pipef->next;
-     }
-  }
-
+  statusfd_send("status: %s : %s : %s", arg, "error", emsg);
 
   nr= malloc(sizeof(struct error_report));
   if (!nr) {