summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWanlong Gao <gaowanlong@cn.fujitsu.com>2012-10-14 14:32:36 +0800
committerRichard W.M. Jones <rjones@redhat.com>2012-10-17 11:12:07 +0100
commit773fa611411d457d4b18dcb171b333b7356c20ba (patch)
treeff1055646a9c9da48f0ea885ec824928e3ae4075
parent67e483689c086e22e9a3e038ea4e7285d9a0c4d2 (diff)
downloadlibguestfs-773fa611411d457d4b18dcb171b333b7356c20ba.tar.gz
libguestfs-773fa611411d457d4b18dcb171b333b7356c20ba.tar.xz
libguestfs-773fa611411d457d4b18dcb171b333b7356c20ba.zip
NEW API: mktemp
Used to create temporary directory or file with an optional suffix. Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com> RWMJ: - Split this out into a new file (daemon/mktemp.c). - I don't see a reason to deprecate the mkdtemp function which works fine. Instead remove complex dir-making code from the new function. - Test and fix the patch (missing close(fd)).
-rw-r--r--daemon/Makefile.am1
-rw-r--r--daemon/dir.c21
-rw-r--r--daemon/mktemp.c97
-rw-r--r--generator/actions.ml34
-rw-r--r--gobject/Makefile.inc6
-rw-r--r--po/POTFILES2
-rw-r--r--src/MAX_PROC_NR2
7 files changed, 139 insertions, 24 deletions
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 9ffff152..a57e6f4f 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -142,6 +142,7 @@ guestfsd_SOURCES = \
md.c \
mkfs.c \
mknod.c \
+ mktemp.c \
modprobe.c \
mount.c \
names.c \
diff --git a/daemon/dir.c b/daemon/dir.c
index aed45d60..cf90a0d6 100644
--- a/daemon/dir.c
+++ b/daemon/dir.c
@@ -191,24 +191,3 @@ do_mkdir_p (const char *path)
return 0;
}
-
-char *
-do_mkdtemp (const char *template)
-{
- char *writable = strdup (template);
- if (writable == NULL) {
- reply_with_perror ("strdup");
- return NULL;
- }
-
- CHROOT_IN;
- char *r = mkdtemp (writable);
- CHROOT_OUT;
-
- if (r == NULL) {
- reply_with_perror ("%s", template);
- free (writable);
- }
-
- return r;
-}
diff --git a/daemon/mktemp.c b/daemon/mktemp.c
new file mode 100644
index 00000000..cacb48d5
--- /dev/null
+++ b/daemon/mktemp.c
@@ -0,0 +1,97 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2012 FUJITSU LTD.
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "guestfs_protocol.h"
+#include "daemon.h"
+#include "actions.h"
+
+char *
+do_mkdtemp (const char *template)
+{
+ char *writable = strdup (template);
+ if (writable == NULL) {
+ reply_with_perror ("strdup");
+ return NULL;
+ }
+
+ CHROOT_IN;
+ char *r = mkdtemp (writable);
+ CHROOT_OUT;
+
+ if (r == NULL) {
+ reply_with_perror ("%s", template);
+ free (writable);
+ }
+
+ return r;
+}
+
+char *
+do_mktemp (const char *template,
+ const char *suffix)
+{
+ char *dest_name = NULL;
+ size_t suffix_len = 0;
+ int fd;
+ size_t len;
+
+ if (optargs_bitmask & GUESTFS_MKTEMP_SUFFIX_BITMASK) {
+ if (suffix) {
+ len = strlen (template);
+ if (len == 0 || template[len - 1] != 'X') {
+ reply_with_error ("template %s must end in X", template);
+ return NULL;
+ }
+
+ /* Append suffix to template. */
+ suffix_len = strlen (suffix);
+ if (asprintf (&dest_name, "%s%s", template, suffix) == -1) {
+ reply_with_perror ("asprintf");
+ return NULL;
+ }
+ }
+ }
+
+ if (dest_name == NULL) {
+ dest_name = strdup (template);
+ if (dest_name == NULL) {
+ reply_with_perror ("strdup");
+ return NULL;
+ }
+ }
+
+ CHROOT_IN;
+ fd = mkstemps (dest_name, (int) suffix_len);
+ CHROOT_OUT;
+
+ if (fd == -1) {
+ reply_with_perror ("%s", dest_name);
+ free (dest_name);
+ return NULL;
+ }
+
+ close (fd);
+
+ return dest_name;
+}
diff --git a/generator/actions.ml b/generator/actions.ml
index e7f4fe64..71aee379 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -10207,6 +10207,40 @@ 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"], [OString "suffix"];
+ proc_nr = Some 373;
+ tests = [
+ InitScratchFS, Always, TestRun (
+ [["mkdir"; "/mktemp"];
+ ["mktemp"; "/mktemp/tmpXXXXXX"; "NOARG"];
+ ["mktemp"; "/mktemp/tmpXXXXXX"; "suff"]])
+ ];
+ shortdesc = "create a temporary file";
+ longdesc = "\
+This command creates a temporary 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 file that was created
+is returned.
+
+The temporary file is created with mode 0600
+and is owned by root.
+
+The caller is responsible for deleting the temporary
+file after use.
+
+If the optional C<suffix> parameter is given, then the suffix
+(eg. C<.txt>) is appended to the temporary name.
+
+See also: C<guestfs_mkdtemp>." };
+
]
(* Non-API meta-commands available only in guestfish.
diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc
index 0a168f4f..95a4b6b6 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 815b541d..1de4fc05 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -58,6 +58,7 @@ daemon/lvm.c
daemon/md.c
daemon/mkfs.c
daemon/mknod.c
+daemon/mktemp.c
daemon/modprobe.c
daemon/mount.c
daemon/names.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 ba300673..a5c3fde3 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-372
+373