From ac286b26df1aabceca26dac66c325a3676ace4cc Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 1 May 2009 11:00:46 +0100 Subject: [PATCH] Generated code for cp, cp-a and mv commands. --- daemon/actions.h | 3 + daemon/stubs.c | 87 +++++ fish/cmds.c | 69 ++++ fish/completion.c | 3 + guestfish-actions.pod | 21 ++ guestfs-actions.pod | 33 ++ java/com/redhat/et/libguestfs/GuestFS.java | 54 ++++ java/com_redhat_et_libguestfs_GuestFS.c | 60 ++++ ocaml/guestfs.ml | 3 + ocaml/guestfs.mli | 9 + ocaml/guestfs_c_actions.c | 72 +++++ perl/Guestfs.xs | 36 +++ perl/lib/Sys/Guestfs.pm | 15 + python/guestfs-py.c | 81 +++++ python/guestfs.py | 18 ++ ruby/ext/guestfs/_guestfs.c | 81 +++++ src/guestfs-actions.c | 264 ++++++++++++++++ src/guestfs-actions.h | 3 + src/guestfs_protocol.c | 36 +++ src/guestfs_protocol.h | 29 +- src/guestfs_protocol.x | 18 ++ tests.c | 489 ++++++++++++++++++++++++++++- 22 files changed, 1482 insertions(+), 2 deletions(-) diff --git a/daemon/actions.h b/daemon/actions.h index 5e2fb41..d43617b 100644 --- a/daemon/actions.h +++ b/daemon/actions.h @@ -107,3 +107,6 @@ extern char *do_get_e2uuid (const char *device); extern int do_fsck (const char *fstype, const char *device); extern int do_zero (const char *device); extern int do_grub_install (const char *root, const char *device); +extern int do_cp (const char *src, const char *dest); +extern int do_cp_a (const char *src, const char *dest); +extern int do_mv (const char *src, const char *dest); diff --git a/daemon/stubs.c b/daemon/stubs.c index ee27997..ddd3fce 100644 --- a/daemon/stubs.c +++ b/daemon/stubs.c @@ -2153,6 +2153,84 @@ done: xdr_free ((xdrproc_t) xdr_guestfs_grub_install_args, (char *) &args); } +static void cp_stub (XDR *xdr_in) +{ + int r; + struct guestfs_cp_args args; + const char *src; + const char *dest; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_cp_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "cp"); + return; + } + src = args.src; + dest = args.dest; + + r = do_cp (src, dest); + if (r == -1) + /* do_cp has already called reply_with_error */ + goto done; + + reply (NULL, NULL); +done: + xdr_free ((xdrproc_t) xdr_guestfs_cp_args, (char *) &args); +} + +static void cp_a_stub (XDR *xdr_in) +{ + int r; + struct guestfs_cp_a_args args; + const char *src; + const char *dest; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_cp_a_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "cp_a"); + return; + } + src = args.src; + dest = args.dest; + + r = do_cp_a (src, dest); + if (r == -1) + /* do_cp_a has already called reply_with_error */ + goto done; + + reply (NULL, NULL); +done: + xdr_free ((xdrproc_t) xdr_guestfs_cp_a_args, (char *) &args); +} + +static void mv_stub (XDR *xdr_in) +{ + int r; + struct guestfs_mv_args args; + const char *src; + const char *dest; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_mv_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "mv"); + return; + } + src = args.src; + dest = args.dest; + + r = do_mv (src, dest); + if (r == -1) + /* do_mv has already called reply_with_error */ + goto done; + + reply (NULL, NULL); +done: + xdr_free ((xdrproc_t) xdr_guestfs_mv_args, (char *) &args); +} + void dispatch_incoming_message (XDR *xdr_in) { switch (proc_nr) { @@ -2414,6 +2492,15 @@ void dispatch_incoming_message (XDR *xdr_in) case GUESTFS_PROC_GRUB_INSTALL: grub_install_stub (xdr_in); break; + case GUESTFS_PROC_CP: + cp_stub (xdr_in); + break; + case GUESTFS_PROC_CP_A: + cp_a_stub (xdr_in); + break; + case GUESTFS_PROC_MV: + mv_stub (xdr_in); + break; default: reply_with_error ("dispatch_incoming_message: unknown procedure number %d", proc_nr); } diff --git a/fish/cmds.c b/fish/cmds.c index 8506044..018080d 100644 --- a/fish/cmds.c +++ b/fish/cmds.c @@ -63,6 +63,8 @@ void list_commands (void) printf ("%-20s %s\n", "command", "run a command from the guest filesystem"); printf ("%-20s %s\n", "command-lines", "run a command, returning lines"); printf ("%-20s %s\n", "config", "add qemu parameters"); + printf ("%-20s %s\n", "cp", "copy a file"); + printf ("%-20s %s\n", "cp-a", "copy a file or directory recursively"); printf ("%-20s %s\n", "debug", "debugging and internals"); printf ("%-20s %s\n", "download", "download a file to the local machine"); printf ("%-20s %s\n", "exists", "test if file or directory exists"); @@ -102,6 +104,7 @@ void list_commands (void) printf ("%-20s %s\n", "mount-ro", "mount a guest disk, read-only"); printf ("%-20s %s\n", "mount-vfs", "mount a guest disk with mount options and vfstype"); printf ("%-20s %s\n", "mounts", "show mounted filesystems"); + printf ("%-20s %s\n", "mv", "move a file"); printf ("%-20s %s\n", "pvcreate", "create an LVM physical volume"); printf ("%-20s %s\n", "pvremove", "remove an LVM physical volume"); printf ("%-20s %s\n", "pvs", "list the LVM physical volumes (PVs)"); @@ -452,6 +455,15 @@ void display_command (const char *cmd) if (strcasecmp (cmd, "grub_install") == 0 || strcasecmp (cmd, "grub-install") == 0) pod2text ("grub-install - install GRUB", " grub-install \n\nThis command installs GRUB (the Grand Unified Bootloader) on\nC, with the root directory being C."); else + if (strcasecmp (cmd, "cp") == 0) + pod2text ("cp - copy a file", " cp \n\nThis copies a file from C to C where C is\neither a destination filename or destination directory."); + else + if (strcasecmp (cmd, "cp_a") == 0 || strcasecmp (cmd, "cp-a") == 0) + pod2text ("cp-a - copy a file or directory recursively", " cp-a \n\nThis copies a file or directory from C to C\nrecursively using the C command."); + else + if (strcasecmp (cmd, "mv") == 0) + pod2text ("mv - move a file", " mv \n\nThis moves a file from C to C where C is\neither a destination filename or destination directory."); + else display_builtin_command (cmd); } @@ -2202,6 +2214,54 @@ static int run_grub_install (const char *cmd, int argc, char *argv[]) return r; } +static int run_cp (const char *cmd, int argc, char *argv[]) +{ + int r; + const char *src; + const char *dest; + if (argc != 2) { + fprintf (stderr, "%s should have 2 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + src = argv[0]; + dest = argv[1]; + r = guestfs_cp (g, src, dest); + return r; +} + +static int run_cp_a (const char *cmd, int argc, char *argv[]) +{ + int r; + const char *src; + const char *dest; + if (argc != 2) { + fprintf (stderr, "%s should have 2 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + src = argv[0]; + dest = argv[1]; + r = guestfs_cp_a (g, src, dest); + return r; +} + +static int run_mv (const char *cmd, int argc, char *argv[]) +{ + int r; + const char *src; + const char *dest; + if (argc != 2) { + fprintf (stderr, "%s should have 2 parameter(s)\n", cmd); + fprintf (stderr, "type 'help %s' for help on %s\n", cmd, cmd); + return -1; + } + src = argv[0]; + dest = argv[1]; + r = guestfs_mv (g, src, dest); + return r; +} + int run_action (const char *cmd, int argc, char *argv[]) { if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0) @@ -2516,6 +2576,15 @@ int run_action (const char *cmd, int argc, char *argv[]) if (strcasecmp (cmd, "grub_install") == 0 || strcasecmp (cmd, "grub-install") == 0) return run_grub_install (cmd, argc, argv); else + if (strcasecmp (cmd, "cp") == 0) + return run_cp (cmd, argc, argv); + else + if (strcasecmp (cmd, "cp_a") == 0 || strcasecmp (cmd, "cp-a") == 0) + return run_cp_a (cmd, argc, argv); + else + if (strcasecmp (cmd, "mv") == 0) + return run_mv (cmd, argc, argv); + else { fprintf (stderr, "%s: unknown command\n", cmd); return -1; diff --git a/fish/completion.c b/fish/completion.c index 665abd2..b2f21ff 100644 --- a/fish/completion.c +++ b/fish/completion.c @@ -69,6 +69,8 @@ static const char *const commands[] = { "command", "command-lines", "config", + "cp", + "cp-a", "debug", "download", "exists", @@ -108,6 +110,7 @@ static const char *const commands[] = { "mount-ro", "mount-vfs", "mounts", + "mv", "path", "pvcreate", "pvremove", diff --git a/guestfish-actions.pod b/guestfish-actions.pod index 74242e3..c7b9fa4 100644 --- a/guestfish-actions.pod +++ b/guestfish-actions.pod @@ -403,6 +403,20 @@ The first character of C string must be a C<-> (dash). C can be NULL. +=head2 cp + + cp src dest + +This copies a file from C to C where C is +either a destination filename or destination directory. + +=head2 cp-a + + cp-a src dest + +This copies a file or directory from C to C +recursively using the C command. + =head2 debug debug subcmd 'extraargs ...' @@ -785,6 +799,13 @@ the list of devices (eg. C, C). Some internal mounts are not shown. +=head2 mv + + mv src dest + +This moves a file from C to C where C is +either a destination filename or destination directory. + =head2 pvcreate pvcreate device diff --git a/guestfs-actions.pod b/guestfs-actions.pod index a05436c..1cd4701 100644 --- a/guestfs-actions.pod +++ b/guestfs-actions.pod @@ -522,6 +522,28 @@ C can be NULL. This function returns 0 on success or -1 on error. +=head2 guestfs_cp + + int guestfs_cp (guestfs_h *handle, + const char *src, + const char *dest); + +This copies a file from C to C where C is +either a destination filename or destination directory. + +This function returns 0 on success or -1 on error. + +=head2 guestfs_cp_a + + int guestfs_cp_a (guestfs_h *handle, + const char *src, + const char *dest); + +This copies a file or directory from C to C +recursively using the C command. + +This function returns 0 on success or -1 on error. + =head2 guestfs_debug char *guestfs_debug (guestfs_h *handle, @@ -1039,6 +1061,17 @@ This function returns a NULL-terminated array of strings (like L), or NULL if there was an error. I. +=head2 guestfs_mv + + int guestfs_mv (guestfs_h *handle, + const char *src, + const char *dest); + +This moves a file from C to C where C is +either a destination filename or destination directory. + +This function returns 0 on success or -1 on error. + =head2 guestfs_pvcreate int guestfs_pvcreate (guestfs_h *handle, diff --git a/java/com/redhat/et/libguestfs/GuestFS.java b/java/com/redhat/et/libguestfs/GuestFS.java index dc414d6..515fc77 100644 --- a/java/com/redhat/et/libguestfs/GuestFS.java +++ b/java/com/redhat/et/libguestfs/GuestFS.java @@ -2433,4 +2433,58 @@ public class GuestFS { private native void _grub_install (long g, String root, String device) throws LibGuestFSException; + /** + * copy a file + * + * This copies a file from "src" to "dest" where "dest" is + * either a destination filename or destination directory. + * + * @throws LibGuestFSException + */ + public void cp (String src, String dest) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("cp: handle is closed"); + _cp (g, src, dest); + } + private native void _cp (long g, String src, String dest) + throws LibGuestFSException; + + /** + * copy a file or directory recursively + * + * This copies a file or directory from "src" to "dest" + * recursively using the "cp -a" command. + * + * @throws LibGuestFSException + */ + public void cp_a (String src, String dest) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("cp_a: handle is closed"); + _cp_a (g, src, dest); + } + private native void _cp_a (long g, String src, String dest) + throws LibGuestFSException; + + /** + * move a file + * + * This moves a file from "src" to "dest" where "dest" is + * either a destination filename or destination directory. + * + * @throws LibGuestFSException + */ + public void mv (String src, String dest) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("mv: handle is closed"); + _mv (g, src, dest); + } + private native void _mv (long g, String src, String dest) + throws LibGuestFSException; + } diff --git a/java/com_redhat_et_libguestfs_GuestFS.c b/java/com_redhat_et_libguestfs_GuestFS.c index d70f88a..0f20d84 100644 --- a/java/com_redhat_et_libguestfs_GuestFS.c +++ b/java/com_redhat_et_libguestfs_GuestFS.c @@ -2457,3 +2457,63 @@ Java_com_redhat_et_libguestfs_GuestFS__1grub_1install } } +JNIEXPORT void JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1cp + (JNIEnv *env, jobject obj, jlong jg, jstring jsrc, jstring jdest) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + int r; + const char *src; + const char *dest; + + src = (*env)->GetStringUTFChars (env, jsrc, NULL); + dest = (*env)->GetStringUTFChars (env, jdest, NULL); + r = guestfs_cp (g, src, dest); + (*env)->ReleaseStringUTFChars (env, jsrc, src); + (*env)->ReleaseStringUTFChars (env, jdest, dest); + if (r == -1) { + throw_exception (env, guestfs_last_error (g)); + return ; + } +} + +JNIEXPORT void JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1cp_1a + (JNIEnv *env, jobject obj, jlong jg, jstring jsrc, jstring jdest) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + int r; + const char *src; + const char *dest; + + src = (*env)->GetStringUTFChars (env, jsrc, NULL); + dest = (*env)->GetStringUTFChars (env, jdest, NULL); + r = guestfs_cp_a (g, src, dest); + (*env)->ReleaseStringUTFChars (env, jsrc, src); + (*env)->ReleaseStringUTFChars (env, jdest, dest); + if (r == -1) { + throw_exception (env, guestfs_last_error (g)); + return ; + } +} + +JNIEXPORT void JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1mv + (JNIEnv *env, jobject obj, jlong jg, jstring jsrc, jstring jdest) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + int r; + const char *src; + const char *dest; + + src = (*env)->GetStringUTFChars (env, jsrc, NULL); + dest = (*env)->GetStringUTFChars (env, jdest, NULL); + r = guestfs_mv (g, src, dest); + (*env)->ReleaseStringUTFChars (env, jsrc, src); + (*env)->ReleaseStringUTFChars (env, jdest, dest); + if (r == -1) { + throw_exception (env, guestfs_last_error (g)); + return ; + } +} + diff --git a/ocaml/guestfs.ml b/ocaml/guestfs.ml index b61931e..f196ea1 100644 --- a/ocaml/guestfs.ml +++ b/ocaml/guestfs.ml @@ -222,3 +222,6 @@ external get_e2uuid : t -> string -> string = "ocaml_guestfs_get_e2uuid" external fsck : t -> string -> string -> int = "ocaml_guestfs_fsck" external zero : t -> string -> unit = "ocaml_guestfs_zero" external grub_install : t -> string -> string -> unit = "ocaml_guestfs_grub_install" +external cp : t -> string -> string -> unit = "ocaml_guestfs_cp" +external cp_a : t -> string -> string -> unit = "ocaml_guestfs_cp_a" +external mv : t -> string -> string -> unit = "ocaml_guestfs_mv" diff --git a/ocaml/guestfs.mli b/ocaml/guestfs.mli index c8b6795..60ec056 100644 --- a/ocaml/guestfs.mli +++ b/ocaml/guestfs.mli @@ -445,3 +445,12 @@ val zero : t -> string -> unit val grub_install : t -> string -> string -> unit (** install GRUB *) +val cp : t -> string -> string -> unit +(** copy a file *) + +val cp_a : t -> string -> string -> unit +(** copy a file or directory recursively *) + +val mv : t -> string -> string -> unit +(** move a file *) + diff --git a/ocaml/guestfs_c_actions.c b/ocaml/guestfs_c_actions.c index 0656e72..f4c1808 100644 --- a/ocaml/guestfs_c_actions.c +++ b/ocaml/guestfs_c_actions.c @@ -2870,3 +2870,75 @@ ocaml_guestfs_grub_install (value gv, value rootv, value devicev) CAMLreturn (rv); } +CAMLprim value +ocaml_guestfs_cp (value gv, value srcv, value destv) +{ + CAMLparam3 (gv, srcv, destv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("cp: used handle after closing it"); + + const char *src = String_val (srcv); + const char *dest = String_val (destv); + int r; + + caml_enter_blocking_section (); + r = guestfs_cp (g, src, dest); + caml_leave_blocking_section (); + if (r == -1) + ocaml_guestfs_raise_error (g, "cp"); + + rv = Val_unit; + CAMLreturn (rv); +} + +CAMLprim value +ocaml_guestfs_cp_a (value gv, value srcv, value destv) +{ + CAMLparam3 (gv, srcv, destv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("cp_a: used handle after closing it"); + + const char *src = String_val (srcv); + const char *dest = String_val (destv); + int r; + + caml_enter_blocking_section (); + r = guestfs_cp_a (g, src, dest); + caml_leave_blocking_section (); + if (r == -1) + ocaml_guestfs_raise_error (g, "cp_a"); + + rv = Val_unit; + CAMLreturn (rv); +} + +CAMLprim value +ocaml_guestfs_mv (value gv, value srcv, value destv) +{ + CAMLparam3 (gv, srcv, destv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("mv: used handle after closing it"); + + const char *src = String_val (srcv); + const char *dest = String_val (destv); + int r; + + caml_enter_blocking_section (); + r = guestfs_mv (g, src, dest); + caml_leave_blocking_section (); + if (r == -1) + ocaml_guestfs_raise_error (g, "mv"); + + rv = Val_unit; + CAMLreturn (rv); +} + diff --git a/perl/Guestfs.xs b/perl/Guestfs.xs index f19b9d0..3c5f713 100644 --- a/perl/Guestfs.xs +++ b/perl/Guestfs.xs @@ -1611,3 +1611,39 @@ PREINIT: if (r == -1) croak ("grub_install: %s", guestfs_last_error (g)); +void +cp (g, src, dest) + guestfs_h *g; + char *src; + char *dest; +PREINIT: + int r; + PPCODE: + r = guestfs_cp (g, src, dest); + if (r == -1) + croak ("cp: %s", guestfs_last_error (g)); + +void +cp_a (g, src, dest) + guestfs_h *g; + char *src; + char *dest; +PREINIT: + int r; + PPCODE: + r = guestfs_cp_a (g, src, dest); + if (r == -1) + croak ("cp_a: %s", guestfs_last_error (g)); + +void +mv (g, src, dest) + guestfs_h *g; + char *src; + char *dest; +PREINIT: + int r; + PPCODE: + r = guestfs_mv (g, src, dest); + if (r == -1) + croak ("mv: %s", guestfs_last_error (g)); + diff --git a/perl/lib/Sys/Guestfs.pm b/perl/lib/Sys/Guestfs.pm index 3f00394..9501a83 100644 --- a/perl/lib/Sys/Guestfs.pm +++ b/perl/lib/Sys/Guestfs.pm @@ -432,6 +432,16 @@ The first character of C string must be a C<-> (dash). C can be NULL. +=item $h->cp ($src, $dest); + +This copies a file from C to C where C is +either a destination filename or destination directory. + +=item $h->cp_a ($src, $dest); + +This copies a file or directory from C to C +recursively using the C command. + =item $result = $h->debug ($subcmd, \@extraargs); The C<$h-Edebug> command exposes some internals of @@ -734,6 +744,11 @@ the list of devices (eg. C, C). Some internal mounts are not shown. +=item $h->mv ($src, $dest); + +This moves a file from C to C where C is +either a destination filename or destination directory. + =item $h->pvcreate ($device); This creates an LVM physical volume on the named C, diff --git a/python/guestfs-py.c b/python/guestfs-py.c index 5afcee3..b85ba1a 100644 --- a/python/guestfs-py.c +++ b/python/guestfs-py.c @@ -3078,6 +3078,84 @@ py_guestfs_grub_install (PyObject *self, PyObject *args) return py_r; } +static PyObject * +py_guestfs_cp (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + int r; + const char *src; + const char *dest; + + if (!PyArg_ParseTuple (args, (char *) "Oss:guestfs_cp", + &py_g, &src, &dest)) + return NULL; + g = get_handle (py_g); + + r = guestfs_cp (g, src, dest); + if (r == -1) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + Py_INCREF (Py_None); + py_r = Py_None; + return py_r; +} + +static PyObject * +py_guestfs_cp_a (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + int r; + const char *src; + const char *dest; + + if (!PyArg_ParseTuple (args, (char *) "Oss:guestfs_cp_a", + &py_g, &src, &dest)) + return NULL; + g = get_handle (py_g); + + r = guestfs_cp_a (g, src, dest); + if (r == -1) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + Py_INCREF (Py_None); + py_r = Py_None; + return py_r; +} + +static PyObject * +py_guestfs_mv (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + int r; + const char *src; + const char *dest; + + if (!PyArg_ParseTuple (args, (char *) "Oss:guestfs_mv", + &py_g, &src, &dest)) + return NULL; + g = get_handle (py_g); + + r = guestfs_mv (g, src, dest); + if (r == -1) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + Py_INCREF (Py_None); + py_r = Py_None; + return py_r; +} + static PyMethodDef methods[] = { { (char *) "create", py_guestfs_create, METH_VARARGS, NULL }, { (char *) "close", py_guestfs_close, METH_VARARGS, NULL }, @@ -3188,6 +3266,9 @@ static PyMethodDef methods[] = { { (char *) "fsck", py_guestfs_fsck, METH_VARARGS, NULL }, { (char *) "zero", py_guestfs_zero, METH_VARARGS, NULL }, { (char *) "grub_install", py_guestfs_grub_install, METH_VARARGS, NULL }, + { (char *) "cp", py_guestfs_cp, METH_VARARGS, NULL }, + { (char *) "cp_a", py_guestfs_cp_a, METH_VARARGS, NULL }, + { (char *) "mv", py_guestfs_mv, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/python/guestfs.py b/python/guestfs.py index 4a23cdb..291af5d 100644 --- a/python/guestfs.py +++ b/python/guestfs.py @@ -1190,3 +1190,21 @@ class GuestFS: """ return libguestfsmod.grub_install (self._o, root, device) + def cp (self, src, dest): + u"""This copies a file from "src" to "dest" where "dest" is + either a destination filename or destination directory. + """ + return libguestfsmod.cp (self._o, src, dest) + + def cp_a (self, src, dest): + u"""This copies a file or directory from "src" to "dest" + recursively using the "cp -a" command. + """ + return libguestfsmod.cp_a (self._o, src, dest) + + def mv (self, src, dest): + u"""This moves a file from "src" to "dest" where "dest" is + either a destination filename or destination directory. + """ + return libguestfsmod.mv (self._o, src, dest) + diff --git a/ruby/ext/guestfs/_guestfs.c b/ruby/ext/guestfs/_guestfs.c index f911750..9d97f96 100644 --- a/ruby/ext/guestfs/_guestfs.c +++ b/ruby/ext/guestfs/_guestfs.c @@ -2586,6 +2586,81 @@ static VALUE ruby_guestfs_grub_install (VALUE gv, VALUE rootv, VALUE devicev) return Qnil; } +static VALUE ruby_guestfs_cp (VALUE gv, VALUE srcv, VALUE destv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "cp"); + + const char *src = StringValueCStr (srcv); + if (!src) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "src", "cp"); + const char *dest = StringValueCStr (destv); + if (!dest) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "dest", "cp"); + + int r; + + r = guestfs_cp (g, src, dest); + if (r == -1) + rb_raise (e_Error, "%s", guestfs_last_error (g)); + + return Qnil; +} + +static VALUE ruby_guestfs_cp_a (VALUE gv, VALUE srcv, VALUE destv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "cp_a"); + + const char *src = StringValueCStr (srcv); + if (!src) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "src", "cp_a"); + const char *dest = StringValueCStr (destv); + if (!dest) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "dest", "cp_a"); + + int r; + + r = guestfs_cp_a (g, src, dest); + if (r == -1) + rb_raise (e_Error, "%s", guestfs_last_error (g)); + + return Qnil; +} + +static VALUE ruby_guestfs_mv (VALUE gv, VALUE srcv, VALUE destv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "mv"); + + const char *src = StringValueCStr (srcv); + if (!src) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "src", "mv"); + const char *dest = StringValueCStr (destv); + if (!dest) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "dest", "mv"); + + int r; + + r = guestfs_mv (g, src, dest); + if (r == -1) + rb_raise (e_Error, "%s", guestfs_last_error (g)); + + return Qnil; +} + /* Initialize the module. */ void Init__guestfs () { @@ -2810,4 +2885,10 @@ void Init__guestfs () ruby_guestfs_zero, 1); rb_define_method (c_guestfs, "grub_install", ruby_guestfs_grub_install, 2); + rb_define_method (c_guestfs, "cp", + ruby_guestfs_cp, 2); + rb_define_method (c_guestfs, "cp_a", + ruby_guestfs_cp_a, 2); + rb_define_method (c_guestfs, "mv", + ruby_guestfs_mv, 2); } diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index c20301b..4992355 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -7816,3 +7816,267 @@ int guestfs_grub_install (guestfs_h *g, return 0; } +struct cp_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; +}; + +static void cp_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct cp_ctx *ctx = (struct cp_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_cp"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_cp"); + 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_cp"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_cp (guestfs_h *g, + const char *src, + const char *dest) +{ + struct guestfs_cp_args args; + struct cp_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_cp") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.src = (char *) src; + args.dest = (char *) dest; + serial = guestfs__send_sync (g, GUESTFS_PROC_CP, + (xdrproc_t) xdr_guestfs_cp_args, (char *) &args); + if (serial == -1) { + guestfs_set_ready (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, cp_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_cp"); + guestfs_set_ready (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CP, serial) == -1) { + guestfs_set_ready (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + guestfs_set_ready (g); + return -1; + } + + guestfs_set_ready (g); + return 0; +} + +struct cp_a_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; +}; + +static void cp_a_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct cp_a_ctx *ctx = (struct cp_a_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_cp_a"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_cp_a"); + 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_cp_a"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_cp_a (guestfs_h *g, + const char *src, + const char *dest) +{ + struct guestfs_cp_a_args args; + struct cp_a_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_cp_a") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.src = (char *) src; + args.dest = (char *) dest; + serial = guestfs__send_sync (g, GUESTFS_PROC_CP_A, + (xdrproc_t) xdr_guestfs_cp_a_args, (char *) &args); + if (serial == -1) { + guestfs_set_ready (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, cp_a_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_cp_a"); + guestfs_set_ready (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_CP_A, serial) == -1) { + guestfs_set_ready (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + guestfs_set_ready (g); + return -1; + } + + guestfs_set_ready (g); + return 0; +} + +struct mv_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; +}; + +static void mv_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct mv_ctx *ctx = (struct mv_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_mv"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_mv"); + 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_mv"); + return; + } + goto done; + } + done: + ctx->cb_sequence = 1; +} + +int guestfs_mv (guestfs_h *g, + const char *src, + const char *dest) +{ + struct guestfs_mv_args args; + struct mv_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_mv") == -1) return -1; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.src = (char *) src; + args.dest = (char *) dest; + serial = guestfs__send_sync (g, GUESTFS_PROC_MV, + (xdrproc_t) xdr_guestfs_mv_args, (char *) &args); + if (serial == -1) { + guestfs_set_ready (g); + return -1; + } + + guestfs__switch_to_receiving (g); + ctx.cb_sequence = 0; + guestfs_set_reply_callback (g, mv_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_mv"); + guestfs_set_ready (g); + return -1; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_MV, serial) == -1) { + guestfs_set_ready (g); + return -1; + } + + if (ctx.hdr.status == GUESTFS_STATUS_ERROR) { + error (g, "%s", ctx.err.error_message); + guestfs_set_ready (g); + return -1; + } + + guestfs_set_ready (g); + return 0; +} + diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h index 843968c..56088ba 100644 --- a/src/guestfs-actions.h +++ b/src/guestfs-actions.h @@ -126,3 +126,6 @@ extern char *guestfs_get_e2uuid (guestfs_h *handle, const char *device); extern int guestfs_fsck (guestfs_h *handle, const char *fstype, const char *device); extern int guestfs_zero (guestfs_h *handle, const char *device); extern int guestfs_grub_install (guestfs_h *handle, const char *root, const char *device); +extern int guestfs_cp (guestfs_h *handle, const char *src, const char *dest); +extern int guestfs_cp_a (guestfs_h *handle, const char *src, const char *dest); +extern int guestfs_mv (guestfs_h *handle, const char *src, const char *dest); diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c index 2f08afd..6c00afc 100644 --- a/src/guestfs_protocol.c +++ b/src/guestfs_protocol.c @@ -1472,6 +1472,42 @@ xdr_guestfs_grub_install_args (XDR *xdrs, guestfs_grub_install_args *objp) } bool_t +xdr_guestfs_cp_args (XDR *xdrs, guestfs_cp_args *objp) +{ + register int32_t *buf; + + if (!xdr_string (xdrs, &objp->src, ~0)) + return FALSE; + if (!xdr_string (xdrs, &objp->dest, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_guestfs_cp_a_args (XDR *xdrs, guestfs_cp_a_args *objp) +{ + register int32_t *buf; + + if (!xdr_string (xdrs, &objp->src, ~0)) + return FALSE; + if (!xdr_string (xdrs, &objp->dest, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_guestfs_mv_args (XDR *xdrs, guestfs_mv_args *objp) +{ + register int32_t *buf; + + if (!xdr_string (xdrs, &objp->src, ~0)) + return FALSE; + if (!xdr_string (xdrs, &objp->dest, ~0)) + return FALSE; + return TRUE; +} + +bool_t xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp) { register int32_t *buf; diff --git a/src/guestfs_protocol.h b/src/guestfs_protocol.h index 69ae268..d2c93b9 100644 --- a/src/guestfs_protocol.h +++ b/src/guestfs_protocol.h @@ -761,6 +761,24 @@ struct guestfs_grub_install_args { }; typedef struct guestfs_grub_install_args guestfs_grub_install_args; +struct guestfs_cp_args { + char *src; + char *dest; +}; +typedef struct guestfs_cp_args guestfs_cp_args; + +struct guestfs_cp_a_args { + char *src; + char *dest; +}; +typedef struct guestfs_cp_a_args guestfs_cp_a_args; + +struct guestfs_mv_args { + char *src; + char *dest; +}; +typedef struct guestfs_mv_args guestfs_mv_args; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -848,7 +866,10 @@ enum guestfs_procedure { GUESTFS_PROC_FSCK = 84, GUESTFS_PROC_ZERO = 85, GUESTFS_PROC_GRUB_INSTALL = 86, - GUESTFS_PROC_NR_PROCS = 86 + 1, + GUESTFS_PROC_CP = 87, + GUESTFS_PROC_CP_A = 88, + GUESTFS_PROC_MV = 89, + GUESTFS_PROC_NR_PROCS = 89 + 1, }; typedef enum guestfs_procedure guestfs_procedure; #define GUESTFS_MESSAGE_MAX 4194304 @@ -1017,6 +1038,9 @@ extern bool_t xdr_guestfs_fsck_args (XDR *, guestfs_fsck_args*); extern bool_t xdr_guestfs_fsck_ret (XDR *, guestfs_fsck_ret*); extern bool_t xdr_guestfs_zero_args (XDR *, guestfs_zero_args*); extern bool_t xdr_guestfs_grub_install_args (XDR *, guestfs_grub_install_args*); +extern bool_t xdr_guestfs_cp_args (XDR *, guestfs_cp_args*); +extern bool_t xdr_guestfs_cp_a_args (XDR *, guestfs_cp_a_args*); +extern bool_t xdr_guestfs_mv_args (XDR *, guestfs_mv_args*); 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*); @@ -1144,6 +1168,9 @@ extern bool_t xdr_guestfs_fsck_args (); extern bool_t xdr_guestfs_fsck_ret (); extern bool_t xdr_guestfs_zero_args (); extern bool_t xdr_guestfs_grub_install_args (); +extern bool_t xdr_guestfs_cp_args (); +extern bool_t xdr_guestfs_cp_a_args (); +extern bool_t xdr_guestfs_mv_args (); extern bool_t xdr_guestfs_procedure (); extern bool_t xdr_guestfs_message_direction (); extern bool_t xdr_guestfs_message_status (); diff --git a/src/guestfs_protocol.x b/src/guestfs_protocol.x index 8183330..1a6a2d7 100644 --- a/src/guestfs_protocol.x +++ b/src/guestfs_protocol.x @@ -591,6 +591,21 @@ struct guestfs_grub_install_args { string device<>; }; +struct guestfs_cp_args { + string src<>; + string dest<>; +}; + +struct guestfs_cp_a_args { + string src<>; + string dest<>; +}; + +struct guestfs_mv_args { + string src<>; + string dest<>; +}; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -678,6 +693,9 @@ enum guestfs_procedure { GUESTFS_PROC_FSCK = 84, GUESTFS_PROC_ZERO = 85, GUESTFS_PROC_GRUB_INSTALL = 86, + GUESTFS_PROC_CP = 87, + GUESTFS_PROC_CP_A = 88, + GUESTFS_PROC_MV = 89, GUESTFS_PROC_NR_PROCS }; diff --git a/tests.c b/tests.c index 49c62df..b47a864 100644 --- a/tests.c +++ b/tests.c @@ -112,6 +112,457 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_get_e2uuid\" has no tests\n"); } +static int test_mv_0 (void) +{ + /* InitBasicFS for mv (0): create ext2 on /dev/sda1 */ + { + 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 *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for mv (0) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/old", "file content", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mv (g, "/old", "/new"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "file content") != 0) { + fprintf (stderr, "test_mv_0: expected \"file content\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_mv_1 (void) +{ + /* InitBasicFS for mv (1): create ext2 on /dev/sda1 */ + { + 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 *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutputFalse for mv (1) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/old", "file content", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mv (g, "/old", "/new"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_is_file (g, "/old"); + if (r == -1) + return -1; + if (r) { + fprintf (stderr, "test_mv_1: expected false, got true\n"); + return -1; + } + } + return 0; +} + +static int test_cp_a_0 (void) +{ + /* InitBasicFS for cp_a (0): create ext2 on /dev/sda1 */ + { + 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 *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for cp_a (0) */ + { + int r; + suppress_error = 0; + r = guestfs_mkdir (g, "/olddir"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkdir (g, "/newdir"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/olddir/file", "file content", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_cp_a (g, "/olddir", "/newdir"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/newdir/olddir/file"); + if (r == NULL) + return -1; + if (strcmp (r, "file content") != 0) { + fprintf (stderr, "test_cp_a_0: expected \"file content\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_cp_0 (void) +{ + /* InitBasicFS for cp (0): create ext2 on /dev/sda1 */ + { + 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 *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for cp (0) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/old", "file content", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_cp (g, "/old", "/new"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "file content") != 0) { + fprintf (stderr, "test_cp_0: expected \"file content\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_cp_1 (void) +{ + /* InitBasicFS for cp (1): create ext2 on /dev/sda1 */ + { + 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 *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutputTrue for cp (1) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/old", "file content", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_cp (g, "/old", "/new"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_is_file (g, "/old"); + if (r == -1) + return -1; + if (!r) { + fprintf (stderr, "test_cp_1: expected true, got false\n"); + return -1; + } + } + return 0; +} + +static int test_cp_2 (void) +{ + /* InitBasicFS for cp (2): create ext2 on /dev/sda1 */ + { + 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 *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for cp (2) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/old", "file content", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkdir (g, "/dir"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_cp (g, "/old", "/dir/new"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/dir/new"); + if (r == NULL) + return -1; + if (strcmp (r, "file content") != 0) { + fprintf (stderr, "test_cp_2: expected \"file content\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + static int test_grub_install_0 (void) { /* InitBasicFS for grub_install (0): create ext2 on /dev/sda1 */ @@ -6784,9 +7235,45 @@ int main (int argc, char *argv[]) exit (1); } - nr_tests = 95; + nr_tests = 101; test_num++; + printf ("%3d/%3d test_mv_0\n", test_num, nr_tests); + if (test_mv_0 () == -1) { + printf ("test_mv_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_mv_1\n", test_num, nr_tests); + if (test_mv_1 () == -1) { + printf ("test_mv_1 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_cp_a_0\n", test_num, nr_tests); + if (test_cp_a_0 () == -1) { + printf ("test_cp_a_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_cp_0\n", test_num, nr_tests); + if (test_cp_0 () == -1) { + printf ("test_cp_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_cp_1\n", test_num, nr_tests); + if (test_cp_1 () == -1) { + printf ("test_cp_1 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_cp_2\n", test_num, nr_tests); + if (test_cp_2 () == -1) { + printf ("test_cp_2 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_grub_install_0\n", test_num, nr_tests); if (test_grub_install_0 () == -1) { printf ("test_grub_install_0 FAILED\n"); -- 1.8.3.1