The following commit has been merged in the master branch:
commit 4c9d2d0eeed8b077a19da5bac5f2e8183e27e850
Author: Guillem Jover <[email protected]>
Date:   Mon Feb 22 03:59:28 2010 +0100

    libdpkg: Add new tempfile template generator function
    
    Use it instead of ad-hoc code in dpkg-deb.

diff --git a/dpkg-deb/build.c b/dpkg-deb/build.c
index 87f4ec1..f06a277 100644
--- a/dpkg-deb/build.c
+++ b/dpkg-deb/build.c
@@ -218,7 +218,6 @@ void do_build(const char *const *argv) {
   char *m;
   const char *debar, *directory, *const *mscriptp, *versionstring, *arch;
   char *controlfile, *tfbuf;
-  const char *envbuf;
   struct pkginfo *checkedinfo;
   struct arbitraryfield *field;
   FILE *ar, *cf;
@@ -235,12 +234,6 @@ void do_build(const char *const *argv) {
   directory = *argv++;
   if (!directory)
     badusage(_("--%s needs a <directory> argument"), cipaction->olong);
-  /* template for our tempfiles */
-  if ((envbuf= getenv("TMPDIR")) == NULL)
-    envbuf= P_tmpdir;
-  tfbuf = m_malloc(strlen(envbuf) + 13);
-  strcpy(tfbuf,envbuf);
-  strcat(tfbuf,"/dpkg.XXXXXX");
   subdir= 0;
   debar = *argv++;
   if (debar != NULL) {
@@ -421,13 +414,13 @@ void do_build(const char *const *argv) {
   /* Create a temporary file to store the control data in. Immediately unlink
    * our temporary file so others can't mess with it.
    */
+  tfbuf = path_make_temp_template("dpkg");
   if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile 
(control)"));
   /* make sure it's gone, the fd will remain until we close it */
   if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (control), %s"),
       tfbuf);
-  /* reset this, so we can use it elsewhere */
-  strcpy(tfbuf,envbuf);
-  strcat(tfbuf,"/dpkg.XXXXXX");
+  free(tfbuf);
+
   /* And run gzip to compress our control archive */
   c2 = subproc_fork();
   if (!c2) {
@@ -470,13 +463,12 @@ void do_build(const char *const *argv) {
    * can't mess with it. */
   if (!oldformatflag) {
     close(gzfd);
+    tfbuf = path_make_temp_template("dpkg");
     if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile 
(data)"));
     /* make sure it's gone, the fd will remain until we close it */
     if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (data), %s"),
         tfbuf);
-    /* reset these, in case we want to use the later */
-    strcpy(tfbuf,envbuf);
-    strcat(tfbuf,"/dpkg.XXXXXX");
+    free(tfbuf);
   }
   /* Fork off a tar. We will feed it a list of filenames on stdin later.
    */
diff --git a/lib/dpkg/path.c b/lib/dpkg/path.c
index febc1c9..0b384be 100644
--- a/lib/dpkg/path.c
+++ b/lib/dpkg/path.c
@@ -3,7 +3,7 @@
  * path.c - path handling functions
  *
  * Copyright © 1995 Ian Jackson <[email protected]>
- * Copyright © 2008 Guillem Jover <[email protected]>
+ * Copyright © 2008, 2009 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,9 +22,12 @@
 #include <config.h>
 #include <compat.h>
 
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 
+#include <dpkg/varbuf.h>
 #include <dpkg/path.h>
 
 size_t
@@ -54,6 +57,32 @@ path_skip_slash_dotslash(const char *path)
        return path;
 }
 
+/**
+ * Create a template for a temporary pathname.
+ *
+ * @param suffix The suffix to use for the template string.
+ *
+ * @return An allocated string with the created template.
+ */
+char *
+path_make_temp_template(const char *suffix)
+{
+       const char *tmpdir;
+       struct varbuf template = VARBUF_INIT;
+
+       tmpdir = getenv("TMPDIR");
+#ifdef P_tmpdir
+       if (!tmpdir)
+               tmpdir = P_tmpdir;
+#endif
+       if (!tmpdir)
+               tmpdir = "/tmp";
+
+       varbufprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
+
+       return varbuf_detach(&template);
+}
+
 /*
  * snprintf(3) doesn't work if format contains %.<nnn>s and an argument has
  * invalid char for locale, then it returns -1.
diff --git a/lib/dpkg/path.h b/lib/dpkg/path.h
index b8e0523..88a0c8d 100644
--- a/lib/dpkg/path.h
+++ b/lib/dpkg/path.h
@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * path.h - path handling routines
  *
- * Copyright © 2008 Guillem Jover <[email protected]>
+ * Copyright © 2008, 2009 Guillem Jover <[email protected]>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -33,6 +33,8 @@ size_t path_rtrim_slash_slashdot(char *path);
 const char *path_skip_slash_dotslash(const char *path);
 char *path_quote_filename(char *dst, const char *src, size_t size);
 
+char *path_make_temp_template(const char *suffix);
+
 DPKG_END_DECLS
 
 #endif /* LIBDPKG_PATH_H */

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to