fprintf (stderr, "warning: \"guestfs_scrub_freespace\" has no tests\n");
}
+static int test_mkdtemp_0_skip (void)
+{
+ const char *str;
+
+ str = getenv ("SKIP_TEST_MKDTEMP_0");
+ if (str && strcmp (str, "1") == 0) return 1;
+ str = getenv ("SKIP_TEST_MKDTEMP");
+ if (str && strcmp (str, "1") == 0) return 1;
+ return 0;
+}
+
+static int test_mkdtemp_0 (void)
+{
+ if (test_mkdtemp_0_skip ()) {
+ printf ("%s skipped (reason: SKIP_TEST_* variable set)\n", "test_mkdtemp_0");
+ return 0;
+ }
+
+ /* InitBasicFS for test_mkdtemp_0: create ext2 on /dev/sda1 */
+ {
+ char device[] = "/dev/sda";
+ int r;
+ suppress_error = 0;
+ r = guestfs_blockdev_setrw (g, device);
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_umount_all (g);
+ if (r == -1)
+ return -1;
+ }
+ {
+ int r;
+ suppress_error = 0;
+ r = guestfs_lvm_remove_all (g);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char device[] = "/dev/sda";
+ char lines_0[] = ",";
+ char *lines[] = {
+ lines_0,
+ NULL
+ };
+ int r;
+ suppress_error = 0;
+ r = guestfs_sfdisk (g, device, 0, 0, 0, lines);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char fstype[] = "ext2";
+ char device[] = "/dev/sda1";
+ int r;
+ suppress_error = 0;
+ r = guestfs_mkfs (g, fstype, device);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char device[] = "/dev/sda1";
+ char mountpoint[] = "/";
+ int r;
+ suppress_error = 0;
+ r = guestfs_mount (g, device, mountpoint);
+ if (r == -1)
+ return -1;
+ }
+ /* TestRun for mkdtemp (0) */
+ {
+ char path[] = "/tmp";
+ int r;
+ suppress_error = 0;
+ r = guestfs_mkdir (g, path);
+ if (r == -1)
+ return -1;
+ }
+ {
+ char template[] = "/tmp/tmpXXXXXX";
+ char *r;
+ suppress_error = 0;
+ r = guestfs_mkdtemp (g, template);
+ if (r == NULL)
+ return -1;
+ free (r);
+ }
+ return 0;
+}
+
static int test_scrub_file_0_skip (void)
{
const char *str;
/* Cancel previous alarm. */
alarm (0);
- nr_tests = 151;
+ nr_tests = 152;
test_num++;
+ printf ("%3d/%3d test_mkdtemp_0\n", test_num, nr_tests);
+ if (test_mkdtemp_0 () == -1) {
+ printf ("test_mkdtemp_0 FAILED\n");
+ failed++;
+ }
+ test_num++;
printf ("%3d/%3d test_scrub_file_0\n", test_num, nr_tests);
if (test_scrub_file_0 () == -1) {
printf ("test_scrub_file_0 FAILED\n");
extern int do_scrub_device (char *device);
extern int do_scrub_file (char *file);
extern int do_scrub_freespace (char *dir);
+extern char *do_mkdtemp (char *template);
xdr_free ((xdrproc_t) xdr_guestfs_scrub_freespace_args, (char *) &args);
}
+static void mkdtemp_stub (XDR *xdr_in)
+{
+ char *r;
+ struct guestfs_mkdtemp_args args;
+ char *template;
+
+ memset (&args, 0, sizeof args);
+
+ if (!xdr_guestfs_mkdtemp_args (xdr_in, &args)) {
+ reply_with_error ("%s: daemon failed to decode procedure arguments", "mkdtemp");
+ return;
+ }
+ template = args.template;
+
+ r = do_mkdtemp (template);
+ if (r == NULL)
+ /* do_mkdtemp has already called reply_with_error */
+ goto done;
+
+ struct guestfs_mkdtemp_ret ret;
+ ret.dir = r;
+ reply ((xdrproc_t) &xdr_guestfs_mkdtemp_ret, (char *) &ret);
+ free (r);
+done:
+ xdr_free ((xdrproc_t) xdr_guestfs_mkdtemp_args, (char *) &args);
+}
+
void dispatch_incoming_message (XDR *xdr_in)
{
switch (proc_nr) {
case GUESTFS_PROC_SCRUB_FREESPACE:
scrub_freespace_stub (xdr_in);
break;
+ case GUESTFS_PROC_MKDTEMP:
+ mkdtemp_stub (xdr_in);
+ break;
default:
reply_with_error ("dispatch_incoming_message: unknown procedure number %d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory", proc_nr);
}
printf ("%-20s %s\n", "lvs-full", "list the LVM logical volumes (LVs)");
printf ("%-20s %s\n", "mkdir", "create a directory");
printf ("%-20s %s\n", "mkdir-p", "create a directory and parents");
+ printf ("%-20s %s\n", "mkdtemp", "create a temporary directory");
printf ("%-20s %s\n", "mkfs", "make a filesystem");
printf ("%-20s %s\n", "mount", "mount a guest disk at a position in the filesystem");
printf ("%-20s %s\n", "mount-options", "mount a guest disk with mount options");
if (strcasecmp (cmd, "scrub_freespace") == 0 || strcasecmp (cmd, "scrub-freespace") == 0)
pod2text ("scrub-freespace - scrub (securely wipe) free space", " scrub-freespace <dir>\n\nThis command creates the directory C<dir> and then fills it\nwith files until the filesystem is full, and scrubs the files\nas for C<scrub_file>, and deletes them.\nThe intention is to scrub any free space on the partition\ncontaining C<dir>.\n\nIt is an interface to the L<scrub(1)> program. See that\nmanual page for more details.");
else
+ if (strcasecmp (cmd, "mkdtemp") == 0)
+ pod2text ("mkdtemp - create a temporary directory", " mkdtemp <template>\n\nThis command creates a temporary directory. The\nC<template> parameter should be a full pathname for the\ntemporary directory with the six characters being\n\"XXXXXX\".\n\nFor example: \"/tmp/tmpXXXXXX\" or \"/Temp/tmpXXXXXX\",\nthe second one being suitable for Windows.\n\nThe name of the temporary directory that was created\nis returned.\n\nThe caller is responsible for deleting the temporary\ndirectory and its contents after use.\n\nSee also: L<mkdtemp(3)>");
+ else
display_builtin_command (cmd);
}
return r;
}
+static int run_mkdtemp (const char *cmd, int argc, char *argv[])
+{
+ char *r;
+ const char *template;
+ if (argc != 1) {
+ fprintf (stderr, "%s should have 1 parameter(s)\n", cmd);
+ fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd);
+ return -1;
+ }
+ template = argv[0];
+ r = guestfs_mkdtemp (g, template);
+ if (r == NULL) return -1;
+ printf ("%s\n", r);
+ free (r);
+ return 0;
+}
+
int run_action (const char *cmd, int argc, char *argv[])
{
if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0)
if (strcasecmp (cmd, "scrub_freespace") == 0 || strcasecmp (cmd, "scrub-freespace") == 0)
return run_scrub_freespace (cmd, argc, argv);
else
+ if (strcasecmp (cmd, "mkdtemp") == 0)
+ return run_mkdtemp (cmd, argc, argv);
+ else
{
fprintf (stderr, "%s: unknown command\n", cmd);
return -1;
"scrub-device",
"scrub-file",
"scrub-freespace",
+ "mkdtemp",
NULL
};
Create a directory named C<path>, creating any parent directories
as necessary. This is like the C<mkdir -p> shell command.
+=head2 mkdtemp
+
+ mkdtemp template
+
+This command creates a temporary directory. The
+C<template> parameter should be a full pathname for the
+temporary directory with the six characters being
+"XXXXXX".
+
+For example: "/tmp/tmpXXXXXX" or "/Temp/tmpXXXXXX",
+the second one being suitable for Windows.
+
+The name of the temporary directory that was created
+is returned.
+
+The caller is responsible for deleting the temporary
+directory and its contents after use.
+
+See also: L<mkdtemp(3)>
+
=head2 mkfs
mkfs fstype device
This function returns 0 on success or -1 on error.
+=head2 guestfs_mkdtemp
+
+ char *guestfs_mkdtemp (guestfs_h *handle,
+ const char *template);
+
+This command creates a temporary directory. The
+C<template> parameter should be a full pathname for the
+temporary directory with the six characters being
+"XXXXXX".
+
+For example: "/tmp/tmpXXXXXX" or "/Temp/tmpXXXXXX",
+the second one being suitable for Windows.
+
+The name of the temporary directory that was created
+is returned.
+
+The caller is responsible for deleting the temporary
+directory and its contents after use.
+
+See also: L<mkdtemp(3)>
+
+This function returns a string, or NULL on error.
+I<The caller must free the returned string after use>.
+
=head2 guestfs_mkfs
int guestfs_mkfs (guestfs_h *handle,
private native void _scrub_freespace (long g, String dir)
throws LibGuestFSException;
+ /**
+ * create a temporary directory
+ * <p>
+ * This command creates a temporary directory. The
+ * "template" parameter should be a full pathname for the
+ * temporary directory with the six characters being
+ * "XXXXXX".
+ * <p>
+ * For example: "/tmp/tmpXXXXXX" or "/Temp/tmpXXXXXX", the
+ * second one being suitable for Windows.
+ * <p>
+ * The name of the temporary directory that was created is
+ * returned.
+ * <p>
+ * The caller is responsible for deleting the temporary
+ * directory and its contents after use.
+ * <p>
+ * See also: mkdtemp(3)
+ * <p>
+ * @throws LibGuestFSException
+ */
+ public String mkdtemp (String template)
+ throws LibGuestFSException
+ {
+ if (g == 0)
+ throw new LibGuestFSException ("mkdtemp: handle is closed");
+ return _mkdtemp (g, template);
+ }
+ private native String _mkdtemp (long g, String template)
+ throws LibGuestFSException;
+
}
}
}
+JNIEXPORT jstring JNICALL
+Java_com_redhat_et_libguestfs_GuestFS__1mkdtemp
+ (JNIEnv *env, jobject obj, jlong jg, jstring jtemplate)
+{
+ guestfs_h *g = (guestfs_h *) (long) jg;
+ jstring jr;
+ char *r;
+ const char *template;
+
+ template = (*env)->GetStringUTFChars (env, jtemplate, NULL);
+ r = guestfs_mkdtemp (g, template);
+ (*env)->ReleaseStringUTFChars (env, jtemplate, template);
+ if (r == NULL) {
+ throw_exception (env, guestfs_last_error (g));
+ return NULL;
+ }
+ jr = (*env)->NewStringUTF (env, r);
+ free (r);
+ return jr;
+}
+
external scrub_device : t -> string -> unit = "ocaml_guestfs_scrub_device"
external scrub_file : t -> string -> unit = "ocaml_guestfs_scrub_file"
external scrub_freespace : t -> string -> unit = "ocaml_guestfs_scrub_freespace"
+external mkdtemp : t -> string -> string = "ocaml_guestfs_mkdtemp"
val scrub_freespace : t -> string -> unit
(** scrub (securely wipe) free space *)
+val mkdtemp : t -> string -> string
+(** create a temporary directory *)
+
CAMLreturn (rv);
}
+CAMLprim value
+ocaml_guestfs_mkdtemp (value gv, value templatev)
+{
+ CAMLparam2 (gv, templatev);
+ CAMLlocal1 (rv);
+
+ guestfs_h *g = Guestfs_val (gv);
+ if (g == NULL)
+ caml_failwith ("mkdtemp: used handle after closing it");
+
+ const char *template = String_val (templatev);
+ char *r;
+
+ caml_enter_blocking_section ();
+ r = guestfs_mkdtemp (g, template);
+ caml_leave_blocking_section ();
+ if (r == NULL)
+ ocaml_guestfs_raise_error (g, "mkdtemp");
+
+ rv = caml_copy_string (r);
+ free (r);
+ CAMLreturn (rv);
+}
+
if (r == -1)
croak ("scrub_freespace: %s", guestfs_last_error (g));
+SV *
+mkdtemp (g, template)
+ guestfs_h *g;
+ char *template;
+PREINIT:
+ char *dir;
+ CODE:
+ dir = guestfs_mkdtemp (g, template);
+ if (dir == NULL)
+ croak ("mkdtemp: %s", guestfs_last_error (g));
+ RETVAL = newSVpv (dir, 0);
+ free (dir);
+ OUTPUT:
+ RETVAL
+
Create a directory named C<path>, creating any parent directories
as necessary. This is like the C<mkdir -p> shell command.
+=item $dir = $h->mkdtemp ($template);
+
+This command creates a temporary directory. The
+C<template> parameter should be a full pathname for the
+temporary directory with the six characters being
+"XXXXXX".
+
+For example: "/tmp/tmpXXXXXX" or "/Temp/tmpXXXXXX",
+the second one being suitable for Windows.
+
+The name of the temporary directory that was created
+is returned.
+
+The caller is responsible for deleting the temporary
+directory and its contents after use.
+
+See also: L<mkdtemp(3)>
+
=item $h->mkfs ($fstype, $device);
This creates a filesystem on C<device> (usually a partition
return py_r;
}
+static PyObject *
+py_guestfs_mkdtemp (PyObject *self, PyObject *args)
+{
+ PyObject *py_g;
+ guestfs_h *g;
+ PyObject *py_r;
+ char *r;
+ const char *template;
+
+ if (!PyArg_ParseTuple (args, (char *) "Os:guestfs_mkdtemp",
+ &py_g, &template))
+ return NULL;
+ g = get_handle (py_g);
+
+ r = guestfs_mkdtemp (g, template);
+ if (r == NULL) {
+ PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
+ return NULL;
+ }
+
+ py_r = PyString_FromString (r);
+ free (r);
+ return py_r;
+}
+
static PyMethodDef methods[] = {
{ (char *) "create", py_guestfs_create, METH_VARARGS, NULL },
{ (char *) "close", py_guestfs_close, METH_VARARGS, NULL },
{ (char *) "scrub_device", py_guestfs_scrub_device, METH_VARARGS, NULL },
{ (char *) "scrub_file", py_guestfs_scrub_file, METH_VARARGS, NULL },
{ (char *) "scrub_freespace", py_guestfs_scrub_freespace, METH_VARARGS, NULL },
+ { (char *) "mkdtemp", py_guestfs_mkdtemp, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
"""
return libguestfsmod.scrub_freespace (self._o, dir)
+ def mkdtemp (self, template):
+ u"""This command creates a temporary directory. The
+ "template" parameter should be a full pathname for the
+ temporary directory with the six characters being
+ "XXXXXX".
+
+ For example: "/tmp/tmpXXXXXX" or "/Temp/tmpXXXXXX", the
+ second one being suitable for Windows.
+
+ The name of the temporary directory that was created is
+ returned.
+
+ The caller is responsible for deleting the temporary
+ directory and its contents after use.
+
+ See also: mkdtemp(3)
+ """
+ return libguestfsmod.mkdtemp (self._o, template)
+
return Qnil;
}
+static VALUE ruby_guestfs_mkdtemp (VALUE gv, VALUE templatev)
+{
+ guestfs_h *g;
+ Data_Get_Struct (gv, guestfs_h, g);
+ if (!g)
+ rb_raise (rb_eArgError, "%s: used handle after closing it", "mkdtemp");
+
+ Check_Type (templatev, T_STRING);
+ const char *template = StringValueCStr (templatev);
+ if (!template)
+ rb_raise (rb_eTypeError, "expected string for parameter %s of %s",
+ "template", "mkdtemp");
+
+ char *r;
+
+ r = guestfs_mkdtemp (g, template);
+ if (r == NULL)
+ rb_raise (e_Error, "%s", guestfs_last_error (g));
+
+ VALUE rv = rb_str_new2 (r);
+ free (r);
+ return rv;
+}
+
/* Initialize the module. */
void Init__guestfs ()
{
ruby_guestfs_scrub_file, 1);
rb_define_method (c_guestfs, "scrub_freespace",
ruby_guestfs_scrub_freespace, 1);
+ rb_define_method (c_guestfs, "mkdtemp",
+ ruby_guestfs_mkdtemp, 1);
}
return 0;
}
+struct mkdtemp_ctx {
+ /* This flag is set by the callbacks, so we know we've done
+ * the callbacks as expected, and in the right sequence.
+ * 0 = not called, 1 = reply_cb called.
+ */
+ int cb_sequence;
+ struct guestfs_message_header hdr;
+ struct guestfs_message_error err;
+ struct guestfs_mkdtemp_ret ret;
+};
+
+static void mkdtemp_reply_cb (guestfs_h *g, void *data, XDR *xdr)
+{
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ struct mkdtemp_ctx *ctx = (struct mkdtemp_ctx *) data;
+
+ /* This should definitely not happen. */
+ if (ctx->cb_sequence != 0) {
+ ctx->cb_sequence = 9999;
+ error (g, "%s: internal error: reply callback called twice", "guestfs_mkdtemp");
+ return;
+ }
+
+ ml->main_loop_quit (ml, g);
+
+ if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {
+ error (g, "%s: failed to parse reply header", "guestfs_mkdtemp");
+ return;
+ }
+ if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {
+ if (!xdr_guestfs_message_error (xdr, &ctx->err)) {
+ error (g, "%s: failed to parse reply error", "guestfs_mkdtemp");
+ return;
+ }
+ goto done;
+ }
+ if (!xdr_guestfs_mkdtemp_ret (xdr, &ctx->ret)) {
+ error (g, "%s: failed to parse reply", "guestfs_mkdtemp");
+ return;
+ }
+ done:
+ ctx->cb_sequence = 1;
+}
+
+char *guestfs_mkdtemp (guestfs_h *g,
+ const char *template)
+{
+ struct guestfs_mkdtemp_args args;
+ struct mkdtemp_ctx ctx;
+ guestfs_main_loop *ml = guestfs_get_main_loop (g);
+ int serial;
+
+ if (check_state (g, "guestfs_mkdtemp") == -1) return NULL;
+ guestfs_set_busy (g);
+
+ memset (&ctx, 0, sizeof ctx);
+
+ args.template = (char *) template;
+ serial = guestfs__send_sync (g, GUESTFS_PROC_MKDTEMP,
+ (xdrproc_t) xdr_guestfs_mkdtemp_args, (char *) &args);
+ if (serial == -1) {
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ guestfs__switch_to_receiving (g);
+ ctx.cb_sequence = 0;
+ guestfs_set_reply_callback (g, mkdtemp_reply_cb, &ctx);
+ (void) ml->main_loop_run (ml, g);
+ guestfs_set_reply_callback (g, NULL, NULL);
+ if (ctx.cb_sequence != 1) {
+ error (g, "%s reply failed, see earlier error messages", "guestfs_mkdtemp");
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MKDTEMP, serial) == -1) {
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {
+ error (g, "%s", ctx.err.error_message);
+ free (ctx.err.error_message);
+ guestfs_end_busy (g);
+ return NULL;
+ }
+
+ guestfs_end_busy (g);
+ return ctx.ret.dir; /* caller will free */
+}
+
extern int guestfs_scrub_device (guestfs_h *handle, const char *device);
extern int guestfs_scrub_file (guestfs_h *handle, const char *file);
extern int guestfs_scrub_freespace (guestfs_h *handle, const char *dir);
+extern char *guestfs_mkdtemp (guestfs_h *handle, const char *template);
}
bool_t
+xdr_guestfs_mkdtemp_args (XDR *xdrs, guestfs_mkdtemp_args *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->template, ~0))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
+xdr_guestfs_mkdtemp_ret (XDR *xdrs, guestfs_mkdtemp_ret *objp)
+{
+ register int32_t *buf;
+
+ if (!xdr_string (xdrs, &objp->dir, ~0))
+ return FALSE;
+ return TRUE;
+}
+
+bool_t
xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp)
{
register int32_t *buf;
};
typedef struct guestfs_scrub_freespace_args guestfs_scrub_freespace_args;
+struct guestfs_mkdtemp_args {
+ char *template;
+};
+typedef struct guestfs_mkdtemp_args guestfs_mkdtemp_args;
+
+struct guestfs_mkdtemp_ret {
+ char *dir;
+};
+typedef struct guestfs_mkdtemp_ret guestfs_mkdtemp_ret;
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
GUESTFS_PROC_SCRUB_DEVICE = 114,
GUESTFS_PROC_SCRUB_FILE = 115,
GUESTFS_PROC_SCRUB_FREESPACE = 116,
- GUESTFS_PROC_NR_PROCS = 116 + 1,
+ GUESTFS_PROC_MKDTEMP = 117,
+ GUESTFS_PROC_NR_PROCS = 117 + 1,
};
typedef enum guestfs_procedure guestfs_procedure;
#define GUESTFS_MESSAGE_MAX 4194304
extern bool_t xdr_guestfs_scrub_device_args (XDR *, guestfs_scrub_device_args*);
extern bool_t xdr_guestfs_scrub_file_args (XDR *, guestfs_scrub_file_args*);
extern bool_t xdr_guestfs_scrub_freespace_args (XDR *, guestfs_scrub_freespace_args*);
+extern bool_t xdr_guestfs_mkdtemp_args (XDR *, guestfs_mkdtemp_args*);
+extern bool_t xdr_guestfs_mkdtemp_ret (XDR *, guestfs_mkdtemp_ret*);
extern bool_t xdr_guestfs_procedure (XDR *, guestfs_procedure*);
extern bool_t xdr_guestfs_message_direction (XDR *, guestfs_message_direction*);
extern bool_t xdr_guestfs_message_status (XDR *, guestfs_message_status*);
extern bool_t xdr_guestfs_scrub_device_args ();
extern bool_t xdr_guestfs_scrub_file_args ();
extern bool_t xdr_guestfs_scrub_freespace_args ();
+extern bool_t xdr_guestfs_mkdtemp_args ();
+extern bool_t xdr_guestfs_mkdtemp_ret ();
extern bool_t xdr_guestfs_procedure ();
extern bool_t xdr_guestfs_message_direction ();
extern bool_t xdr_guestfs_message_status ();
string dir<>;
};
+struct guestfs_mkdtemp_args {
+ string template<>;
+};
+
+struct guestfs_mkdtemp_ret {
+ string dir<>;
+};
+
enum guestfs_procedure {
GUESTFS_PROC_MOUNT = 1,
GUESTFS_PROC_SYNC = 2,
GUESTFS_PROC_SCRUB_DEVICE = 114,
GUESTFS_PROC_SCRUB_FILE = 115,
GUESTFS_PROC_SCRUB_FREESPACE = 116,
+ GUESTFS_PROC_MKDTEMP = 117,
GUESTFS_PROC_NR_PROCS
};