[Libguestfs] [PATCH] NEW API: mktemp

Wanlong Gao gaowanlong at cn.fujitsu.com
Sun Oct 14 06:32:36 UTC 2012


Used to create temporary directory or file with an optional suffix.

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 daemon/dir.c         | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 generator/actions.ml | 36 +++++++++++++++++++++++++++++++
 gobject/Makefile.inc |  6 ++++--
 po/POTFILES          |  2 ++
 src/MAX_PROC_NR      |  2 +-
 5 files changed, 104 insertions(+), 3 deletions(-)

diff --git a/daemon/dir.c b/daemon/dir.c
index aed45d6..b58cc2a 100644
--- a/daemon/dir.c
+++ b/daemon/dir.c
@@ -212,3 +212,64 @@ do_mkdtemp (const char *template)
 
   return r;
 }
+
+char *
+do_mktemp (const char *template,
+           int dir,
+           const char *suffix)
+{
+  char *dest_name = NULL;
+  size_t suffix_len = 0;
+  char *r;
+  int err;
+  if (!(optargs_bitmask & GUESTFS_MKTEMP_DIR_BITMASK))
+    dir = 0;
+
+  if (optargs_bitmask & GUESTFS_MKTEMP_SUFFIX_BITMASK) {
+    if (suffix) {
+      if (dir) {
+        reply_with_error ("can not support suffix with directory");
+        return NULL;
+      }
+      size_t len = strlen (template);
+      if (!len || template[len - 1] != 'X') {
+        reply_with_error ("template %s must end in X", template);
+        return NULL;
+      }
+      suffix_len = strlen (suffix);
+      dest_name = malloc (len + suffix_len + 1);
+      memcpy (dest_name, template, len);
+      memcpy (dest_name + len, suffix, suffix_len + 1);
+    }
+  }
+
+  if (dest_name == NULL) {
+    dest_name = strdup (template);
+    if (dest_name == NULL) {
+      reply_with_perror ("strdup");
+      return NULL;
+    }
+  }
+
+  CHROOT_IN;
+  if (dir)
+    r = mkdtemp (dest_name);
+  else
+    err = mkstemps (dest_name, suffix_len);
+  CHROOT_OUT;
+
+  if (dir) {
+    if (r == NULL) {
+      reply_with_perror ("%s", dest_name);
+      free (dest_name);
+    }
+    return r;
+  } else {
+    if (err == -1) {
+      reply_with_perror ("%s", dest_name);
+      free (dest_name);
+      return NULL;
+    }
+    return dest_name;
+  }
+}
diff --git a/generator/actions.ml b/generator/actions.ml
index 13e54f3..855b4d1 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -4834,6 +4834,7 @@ manual page for more details." };
     name = "mkdtemp";
     style = RString "dir", [Pathname "tmpl"], [];
     proc_nr = Some 117;
+    deprecated_by = Some "mktemp";
     tests = [
       InitScratchFS, Always, TestRun (
         [["mkdir"; "/mkdtemp"];
@@ -10013,6 +10014,41 @@ This function is used internally when hotplugging drives." };
     longdesc = "\
 This function is used internally when hotplugging drives." };
 
+  { defaults with
+    name = "mktemp";
+    style = RString "path", [Pathname "tmpl"], [OBool "dir"; OString "suffix"];
+    proc_nr = Some 373;
+    tests = [
+      InitScratchFS, Always, TestRun (
+        [["mkdir"; "/mktemp"];
+         ["mktemp"; "/mktemp/tmpXXXXXX"; "true"; "NOARG"];
+         ["mktemp"; "/mktemp/tmpXXXXXX"; "false"; "suff"]])
+    ];
+    shortdesc = "create a temporary directory or file";
+    longdesc = "\
+This command creates a temporary directory/file.  The
+C<tmpl> parameter should be a full pathname for the
+temporary directory name with the final six characters being
+\"XXXXXX\".
+
+For example: \"/tmp/myprogXXXXXX\" or \"/Temp/myprogXXXXXX\",
+the second one being suitable for Windows filesystems.
+
+The name of the temporary directory/file that was created
+is returned.
+
+The temporary directory/file is created with mode 0700
+and is owned by root.
+
+The caller is responsible for deleting the temporary
+directory/file and its contents after use.
+
+Set C<dir> to \"true\" if you want to crate a directory.
+
+C<suffix> is used to specify a suffix to append the C<tmpl>.
+
+See also: L<mkdtemp(3)> and L<mkstemps(3)>" };
+
 ]
 
 (* Non-API meta-commands available only in guestfish.
diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc
index 0a168f4..95a4b6b 100644
--- a/gobject/Makefile.inc
+++ b/gobject/Makefile.inc
@@ -81,7 +81,8 @@ guestfs_gobject_headers= \
   include/guestfs-gobject/optargs-xfs_admin.h \
   include/guestfs-gobject/optargs-hivex_open.h \
   include/guestfs-gobject/optargs-xfs_repair.h \
-  include/guestfs-gobject/optargs-mke2fs.h
+  include/guestfs-gobject/optargs-mke2fs.h \
+  include/guestfs-gobject/optargs-mktemp.h
 
 guestfs_gobject_sources= \
   src/session.c \
@@ -144,4 +145,5 @@ guestfs_gobject_sources= \
   src/optargs-xfs_admin.c \
   src/optargs-hivex_open.c \
   src/optargs-xfs_repair.c \
-  src/optargs-mke2fs.c
+  src/optargs-mke2fs.c \
+  src/optargs-mktemp.c
diff --git a/po/POTFILES b/po/POTFILES
index 3705e74..4bdee3e 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -137,6 +137,7 @@ fish/tilde.c
 fish/time.c
 format/format.c
 fuse/guestmount.c
+gobject/docs/guestfs-scan.c
 gobject/src/optargs-add_domain.c
 gobject/src/optargs-add_drive.c
 gobject/src/optargs-btrfs_filesystem_resize.c
@@ -160,6 +161,7 @@ gobject/src/optargs-mke2fs.c
 gobject/src/optargs-mkfs.c
 gobject/src/optargs-mkfs_btrfs.c
 gobject/src/optargs-mkswap.c
+gobject/src/optargs-mktemp.c
 gobject/src/optargs-mount_9p.c
 gobject/src/optargs-mount_local.c
 gobject/src/optargs-ntfsclone_out.c
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index ba30067..a5c3fde 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-372
+373
-- 
1.7.12.1.401.gb5d156c




More information about the Libguestfs mailing list