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

libdpkg: Add new tempfile template generator function

Use it instead of ad-hoc code in dpkg-deb.
Guillem Jover лет назад: 16
Родитель
Сommit
4c9d2d0eee
3 измененных файлов с 38 добавлено и 15 удалено
  1. 5 13
      dpkg-deb/build.c
  2. 30 1
      lib/dpkg/path.c
  3. 3 1
      lib/dpkg/path.h

+ 5 - 13
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.
    */

+ 30 - 1
lib/dpkg/path.c

@@ -3,7 +3,7 @@
  * path.c - path handling functions
  *
  * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
  *
  * 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.

+ 3 - 1
lib/dpkg/path.h

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * path.h - path handling routines
  *
- * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ * Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
  *
  * 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 */