Explorar o código

Use m_asprintf() instead varbuf_printf() for one-off strings

When the string is only created once, used several times and then
freed, there's no much point in using a varbuf instead of the simpler
m_asprintf(). The former needs more state and makes using the string
slightly more cumbersome. Using m_asprintf() should be slightly faster
too, but not significantly enough to justify this change by itself.
Guillem Jover %!s(int64=15) %!d(string=hai) anos
pai
achega
f4b0dd0456
Modificáronse 7 ficheiros con 66 adicións e 70 borrados
  1. 10 10
      dpkg-deb/build.c
  2. 5 6
      dpkg-split/split.c
  3. 6 6
      lib/dpkg/dbmodify.c
  4. 7 8
      lib/dpkg/dir.c
  5. 4 4
      lib/dpkg/path.c
  6. 6 6
      lib/dpkg/triglib.c
  7. 28 30
      src/divertcmd.c

+ 10 - 10
dpkg-deb/build.c

@@ -324,28 +324,28 @@ check_new_pkg(const char *dir)
 {
   struct pkginfo *pkg;
   struct arbitraryfield *field;
-  struct varbuf controlfile = VARBUF_INIT;
+  char *controlfile;
   int warns;
 
   /* Start by reading in the control file so we can check its contents. */
-  varbuf_printf(&controlfile, "%s/%s/%s", dir, BUILDCONTROLDIR, CONTROLFILE);
-  parsedb(controlfile.buf, pdb_recordavailable | pdb_rejectstatus, &pkg);
+  m_asprintf(&controlfile, "%s/%s/%s", dir, BUILDCONTROLDIR, CONTROLFILE);
+  parsedb(controlfile, pdb_recordavailable | pdb_rejectstatus, &pkg);
 
   if (strspn(pkg->name, "abcdefghijklmnopqrstuvwxyz0123456789+-.") !=
       strlen(pkg->name))
     ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
   if (pkg->priority == pri_other)
     warning(_("'%s' contains user-defined Priority value '%s'"),
-            controlfile.buf, pkg->otherpriority);
+            controlfile, pkg->otherpriority);
   for (field = pkg->available.arbs; field; field = field->next) {
     if (known_arbitrary_field(field))
       continue;
 
-    warning(_("'%s' contains user-defined field '%s'"), controlfile.buf,
+    warning(_("'%s' contains user-defined field '%s'"), controlfile,
             field->name);
   }
 
-  varbuf_destroy(&controlfile);
+  free(controlfile);
 
   check_file_perms(dir);
   check_conffiles(dir);
@@ -367,15 +367,15 @@ check_new_pkg(const char *dir)
 static char *
 pkg_get_pathname(const char *dir, struct pkginfo *pkg)
 {
-  struct varbuf path = VARBUF_INIT;
+  char *path;
   const char *versionstring, *arch;
 
   versionstring = versiondescribe(&pkg->available.version, vdew_never);
   arch = pkg->available.architecture;
-  varbuf_printf(&path, "%s/%s_%s%s%s%s", dir, pkg->name, versionstring,
-                arch ? "_" : "", arch ? arch : "", DEBEXT);
+  m_asprintf(&path, "%s/%s_%s%s%s%s", dir, pkg->name, versionstring,
+             arch ? "_" : "", arch ? arch : "", DEBEXT);
 
-  return varbuf_detach(&path);
+  return path;
 }
 
 /**

+ 5 - 6
dpkg-split/split.c

@@ -171,15 +171,14 @@ mksplit(const char *file_src, const char *prefix, size_t maxpartsize,
 		varbuf_reset(&file_dst);
 		/* Generate output filename. */
 		if (msdos) {
-			struct varbuf refname = VARBUF_INIT;
+			char *refname;
 			int prefix_max;
 
-			varbuf_printf(&refname, "%dof%d", curpart, nparts);
-			prefix_max = max(8 - strlen(refname.buf), 0);
+			m_asprintf(&refname, "%dof%d", curpart, nparts);
+			prefix_max = max(8 - strlen(refname), 0);
 			varbuf_printf(&file_dst, "%s/%.*s%.8s.deb",
-			              prefixdir, prefix_max, prefix,
-			              refname.buf);
-			varbuf_destroy(&refname);
+			              prefixdir, prefix_max, prefix, refname);
+			free(refname);
 		} else {
 			varbuf_printf(&file_dst, "%s.%dof%d.deb",
 			              prefix, curpart, nparts);

+ 6 - 6
lib/dpkg/dbmodify.c

@@ -146,28 +146,28 @@ static int dblockfd = -1;
 bool
 modstatdb_is_locked(const char *admindir)
 {
-  struct varbuf lockfile = VARBUF_INIT;
+  char *lockfile;
   int lockfd;
   bool locked;
 
-  varbuf_printf(&lockfile, "%s/%s", admindir, LOCKFILE);
+  m_asprintf(&lockfile, "%s/%s", admindir, LOCKFILE);
 
   if (dblockfd == -1) {
-    lockfd = open(lockfile.buf, O_RDONLY);
+    lockfd = open(lockfile, O_RDONLY);
     if (lockfd == -1)
-      ohshite(_("unable to open lock file %s for testing"), lockfile.buf);
+      ohshite(_("unable to open lock file %s for testing"), lockfile);
   } else {
     lockfd = dblockfd;
   }
 
-  locked = file_is_locked(lockfd, lockfile.buf);
+  locked = file_is_locked(lockfd, lockfile);
 
   /* We only close the file if there was no lock open, otherwise we would
    * release the existing lock on close. */
   if (dblockfd == -1)
     close(lockfd);
 
-  varbuf_destroy(&lockfile);
+  free(lockfile);
 
   return locked;
 }

+ 7 - 8
lib/dpkg/dir.c

@@ -32,7 +32,6 @@
 
 #include <dpkg/dpkg.h>
 #include <dpkg/i18n.h>
-#include <dpkg/varbuf.h>
 #include <dpkg/dir.h>
 
 /**
@@ -100,20 +99,20 @@ dir_sync_path_parent(const char *path)
 static void
 dir_file_sync(const char *dir, const char *filename)
 {
-	struct varbuf path = VARBUF_INIT;
+	char *path;
 	int fd;
 
-	varbuf_printf(&path, "%s/%s", dir, filename);
+	m_asprintf(&path, "%s/%s", dir, filename);
 
-	fd = open(path.buf, O_WRONLY);
+	fd = open(path, O_WRONLY);
 	if (fd < 0)
-		ohshite(_("unable to open file '%s'"), path.buf);
+		ohshite(_("unable to open file '%s'"), path);
 	if (fsync(fd))
-		ohshite(_("unable to sync file '%s'"), path.buf);
+		ohshite(_("unable to sync file '%s'"), path);
 	if (close(fd))
-		ohshite(_("unable to close file '%s'"), path.buf);
+		ohshite(_("unable to close file '%s'"), path);
 
-	varbuf_destroy(&path);
+	free(path);
 }
 
 /**

+ 4 - 4
lib/dpkg/path.c

@@ -27,7 +27,7 @@
 #include <string.h>
 #include <stdio.h>
 
-#include <dpkg/varbuf.h>
+#include <dpkg/dpkg.h>
 #include <dpkg/path.h>
 
 /**
@@ -84,7 +84,7 @@ char *
 path_make_temp_template(const char *suffix)
 {
 	const char *tmpdir;
-	struct varbuf template = VARBUF_INIT;
+	char *template;
 
 	tmpdir = getenv("TMPDIR");
 #ifdef P_tmpdir
@@ -94,9 +94,9 @@ path_make_temp_template(const char *suffix)
 	if (!tmpdir)
 		tmpdir = "/tmp";
 
-	varbuf_printf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
+	m_asprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
 
-	return varbuf_detach(&template);
+	return template;
 }
 
 /**

+ 6 - 6
lib/dpkg/triglib.c

@@ -62,21 +62,21 @@ static char *triggersdir, *triggersfilefile, *triggersnewfilefile;
 char *
 trig_get_triggersdir(const char *admindir)
 {
-	struct varbuf path = VARBUF_INIT;
+	char *path;
 
-	varbuf_printf(&path, "%s/%s", admindir, TRIGGERSDIR);
+	m_asprintf(&path, "%s/%s", admindir, TRIGGERSDIR);
 
-	return varbuf_detach(&path);
+	return path;
 }
 
 static char *
 trig_get_filename(const char *dir, const char *filename)
 {
-	struct varbuf path = VARBUF_INIT;
+	char *path;
 
-	varbuf_printf(&path, "%s/%s", dir, filename);
+	m_asprintf(&path, "%s/%s", dir, filename);
 
-	return varbuf_detach(&path);
+	return path;
 }
 
 static struct trig_hooks trigh;

+ 28 - 30
src/divertcmd.c

@@ -157,18 +157,18 @@ file_stat(struct file *f)
 static void
 check_writable_dir(struct file *f)
 {
-	struct varbuf tmpname = VARBUF_INIT;
+	char *tmpname;
 	int tmpfd;
 
-	varbuf_printf(&tmpname, "%s%s", f->name, ".dpkg-divert.tmp");
+	m_asprintf(&tmpname, "%s%s", f->name, ".dpkg-divert.tmp");
 
-	tmpfd = creat(tmpname.buf, 0600);
+	tmpfd = creat(tmpname, 0600);
 	if (tmpfd < 0)
 		ohshite(_("error checking '%s'"), f->name);
 	close(tmpfd);
-	unlink(tmpname.buf);
+	unlink(tmpname);
 
-	varbuf_destroy(&tmpname);
+	free(tmpname);
 }
 
 static bool
@@ -238,22 +238,22 @@ file_copy(const char *src, const char *dst)
 static int
 rename_mv(const char *src, const char *dst)
 {
-	struct varbuf tmpdst = VARBUF_INIT;
+	char *tmpdst;
 
 	if (rename(src, dst) == 0)
 		return 0;
 
-	varbuf_printf(&tmpdst, "%s%s", dst, ".dpkg-divert.tmp");
+	m_asprintf(&tmpdst, "%s%s", dst, ".dpkg-divert.tmp");
 
 	/* If a simple rename didn't work try an atomic copy, rename, unlink
 	 * instead. */
-	if (file_copy(src, tmpdst.buf) != 0)
+	if (file_copy(src, tmpdst) != 0)
 		return -1;
 
-	if (rename(tmpdst.buf, dst) != 0)
+	if (rename(tmpdst, dst) != 0)
 		return -1;
 
-	varbuf_destroy(&tmpdst);
+	free(tmpdst);
 
 	return -1;
 }
@@ -354,21 +354,19 @@ diversion_describe(struct diversion *d)
 static void
 divertdb_write(void)
 {
+	char *dbname, *dbname_new, *dbname_old;
 	FILE *dbfile;
 	struct fileiterator *iter;
 	struct filenamenode *namenode;
-	struct varbuf dbname = VARBUF_INIT;
-	struct varbuf dbname_new = VARBUF_INIT;
-	struct varbuf dbname_old = VARBUF_INIT;
 
-	varbuf_printf(&dbname, "%s/%s", admindir, DIVERSIONSFILE);
-	varbuf_printf(&dbname_new, "%s%s", dbname.buf, NEWDBEXT);
-	varbuf_printf(&dbname_old, "%s%s", dbname.buf, OLDDBEXT);
+	m_asprintf(&dbname, "%s/%s", admindir, DIVERSIONSFILE);
+	m_asprintf(&dbname_new, "%s%s", dbname, NEWDBEXT);
+	m_asprintf(&dbname_old, "%s%s", dbname, OLDDBEXT);
 
-	dbfile = fopen(dbname_new.buf, "w");
+	dbfile = fopen(dbname_new, "w");
 	if (!dbfile)
 		ohshite(_("cannot create new %s file"), DIVERSIONSFILE);
-	chmod(dbname_new.buf, 0644);
+	chmod(dbname_new, 0644);
 
 	iter = iterfilestart();
 	while ((namenode = iterfilenext(iter))) {
@@ -385,22 +383,22 @@ divertdb_write(void)
 	iterfileend(iter);
 
 	if (fflush(dbfile))
-		ohshite(_("unable to flush file '%s'"), dbname_new.buf);
+		ohshite(_("unable to flush file '%s'"), dbname_new);
 	if (fsync(fileno(dbfile)))
-		ohshite(_("unable to sync file '%s'"), dbname_new.buf);
+		ohshite(_("unable to sync file '%s'"), dbname_new);
 	if (fclose(dbfile))
-		ohshite(_("unable to close file '%s'"), dbname_new.buf);
+		ohshite(_("unable to close file '%s'"), dbname_new);
 
-	if (unlink(dbname_old.buf) && errno != ENOENT)
+	if (unlink(dbname_old) && errno != ENOENT)
 		ohshite(_("error removing old diversions-old"));
-	if (link(dbname.buf, dbname_old.buf) && errno != ENOENT)
+	if (link(dbname, dbname_old) && errno != ENOENT)
 		ohshite(_("error creating new diversions-old"));
-	if (rename(dbname_new.buf, dbname.buf))
+	if (rename(dbname_new, dbname))
 		ohshite(_("error installing new diversions"));
 
-	varbuf_destroy(&dbname);
-	varbuf_destroy(&dbname_new);
-	varbuf_destroy(&dbname_old);
+	free(dbname);
+	free(dbname_new);
+	free(dbname_old);
 }
 
 static int
@@ -434,10 +432,10 @@ diversion_add(const char *const *argv)
 
 	/* Handle divertto. */
 	if (opt_divertto == NULL) {
-		struct varbuf str = VARBUF_INIT;
+		char *str;
 
-		varbuf_printf(&str, "%s.distrib", filename);
-		opt_divertto = varbuf_detach(&str);
+		m_asprintf(&str, "%s.distrib", filename);
+		opt_divertto = str;
 	}
 	if (opt_divertto[0] != '/')
 		badusage(_("filename \"%s\" is not absolute"), opt_divertto);