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

dpkg-deb: Construct control components paths with printf variants

The output of dpkg-deb -I, -f and -W is not performance critical, and
code simplicity trumps over the tiny performance improvements that using
the explicit string functions or keeping the extracted control directory
in the varbuf might imply in these cases.
Guillem Jover лет назад: 15
Родитель
Сommit
9b22745f0a
1 измененных файлов с 15 добавлено и 28 удалено
  1. 15 28
      dpkg-deb/info.c

+ 15 - 28
dpkg-deb/info.c

@@ -99,10 +99,7 @@ info_spew(const char *debar, const char *dir, const char *const *argv)
 
 
   while ((component = *argv++) != NULL) {
   while ((component = *argv++) != NULL) {
     varbuf_reset(&controlfile);
     varbuf_reset(&controlfile);
-    varbuf_add_str(&controlfile, dir);
-    varbuf_add_char(&controlfile, '/');
-    varbuf_add_str(&controlfile, component);
-    varbuf_end_str(&controlfile);
+    varbuf_printf(&controlfile, "%s/%s", dir, component);
 
 
     fd = open(controlfile.buf, O_RDONLY);
     fd = open(controlfile.buf, O_RDONLY);
     if (fd >= 0) {
     if (fd >= 0) {
@@ -136,20 +133,17 @@ info_list(const char *debar, const char *dir)
   FILE *cc;
   FILE *cc;
   struct stat stab;
   struct stat stab;
   int c;
   int c;
-  size_t dirlen;
 
 
   cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
   cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
   if (cdn == -1)
   if (cdn == -1)
     ohshite(_("cannot scan directory `%.255s'"), dir);
     ohshite(_("cannot scan directory `%.255s'"), dir);
-  varbuf_add_str(&controlfile, dir);
-  varbuf_add_char(&controlfile, '/');
-  dirlen = controlfile.used;
 
 
   for (n = 0; n < cdn; n++) {
   for (n = 0; n < cdn; n++) {
     cdep = cdlist[n];
     cdep = cdlist[n];
-    varbuf_trunc(&controlfile, dirlen);
-    varbuf_add_str(&controlfile, cdep->d_name);
-    varbuf_end_str(&controlfile);
+
+    varbuf_reset(&controlfile);
+    varbuf_printf(&controlfile, "%s/%s", dir, cdep->d_name);
+
     if (stat(controlfile.buf, &stab))
     if (stat(controlfile.buf, &stab))
       ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
       ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
     if (S_ISREG(stab.st_mode)) {
     if (S_ISREG(stab.st_mode)) {
@@ -183,9 +177,8 @@ info_list(const char *debar, const char *dir)
   }
   }
   free(cdlist);
   free(cdlist);
 
 
-  varbuf_trunc(&controlfile, dirlen);
-  varbuf_add_str(&controlfile, CONTROLFILE);
-  varbuf_end_str(&controlfile);
+  varbuf_reset(&controlfile);
+  varbuf_printf(&controlfile, "%s/%s", dir, CONTROLFILE);
   cc = fopen(controlfile.buf, "r");
   cc = fopen(controlfile.buf, "r");
   if (!cc) {
   if (!cc) {
     if (errno != ENOENT)
     if (errno != ENOENT)
@@ -215,19 +208,16 @@ static void
 info_field(const char *debar, const char *dir, const char *const *fields,
 info_field(const char *debar, const char *dir, const char *const *fields,
            bool showfieldname)
            bool showfieldname)
 {
 {
-  struct varbuf controlfile = VARBUF_INIT;
   FILE *cc;
   FILE *cc;
+  char *controlfile;
   char fieldname[MAXFIELDNAME+1];
   char fieldname[MAXFIELDNAME+1];
   char *pf;
   char *pf;
   const char *const *fp;
   const char *const *fp;
   int c, lno, fnl;
   int c, lno, fnl;
   bool doing;
   bool doing;
 
 
-  varbuf_add_str(&controlfile, dir);
-  varbuf_add_char(&controlfile, '/');
-  varbuf_add_str(&controlfile, CONTROLFILE);
-  varbuf_end_str(&controlfile);
-  cc = fopen(controlfile.buf, "r");
+  m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
+  cc = fopen(controlfile, "r");
   if (!cc)
   if (!cc)
     ohshite(_("could not open the `control' component"));
     ohshite(_("could not open the `control' component"));
   doing = true;
   doing = true;
@@ -273,14 +263,14 @@ info_field(const char *debar, const char *dir, const char *const *fields,
     ohshite(_("error closing the '%s' component"), CONTROLFILE);
     ohshite(_("error closing the '%s' component"), CONTROLFILE);
   if (doing) putc('\n',stdout);
   if (doing) putc('\n',stdout);
   m_output(stdout, _("<standard output>"));
   m_output(stdout, _("<standard output>"));
-  varbuf_destroy(&controlfile);
+  free(controlfile);
 }
 }
 
 
 int
 int
 do_showinfo(const char *const *argv)
 do_showinfo(const char *const *argv)
 {
 {
-  struct varbuf controlfile = VARBUF_INIT;
   const char *debar, *dir;
   const char *debar, *dir;
+  char *controlfile;
   struct pkginfo *pkg;
   struct pkginfo *pkg;
   struct pkg_format_node *fmt = pkg_format_parse(showformat);
   struct pkg_format_node *fmt = pkg_format_parse(showformat);
 
 
@@ -289,14 +279,11 @@ do_showinfo(const char *const *argv)
 
 
   info_prepare(&argv, &debar, &dir, 1);
   info_prepare(&argv, &debar, &dir, 1);
 
 
-  varbuf_add_str(&controlfile, dir);
-  varbuf_add_char(&controlfile, '/');
-  varbuf_add_str(&controlfile, CONTROLFILE);
-  varbuf_end_str(&controlfile);
-  parsedb(controlfile.buf,
+  m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
+  parsedb(controlfile,
           pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles, &pkg);
           pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles, &pkg);
   pkg_format_show(fmt, pkg, &pkg->available);
   pkg_format_show(fmt, pkg, &pkg->available);
-  varbuf_destroy(&controlfile);
+  free(controlfile);
 
 
   return 0;
   return 0;
 }
 }