On Sun, Oct 14, 2012 at 02:32:36PM +0800, Wanlong Gao wrote:
Used to create temporary directory or file with an optional suffix.
Signed-off-by: Wanlong Gao <gaowanlong(a)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)>" };
+
It all seems good. What would be the real killer feature would be
some sort of 'cleanup' flag which removes the file/directory when the
daemon exits ...
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines. Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v