From 1fc41b39dac877ccec1284da8bb14baa4df368b8 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Tue, 19 May 2009 12:05:43 +0100 Subject: [PATCH] Generated code for 'find' command. --- daemon/actions.h | 1 + daemon/stubs.c | 31 +++ fish/cmds.c | 24 ++ fish/completion.c | 1 + guestfish-actions.pod | 29 +++ guestfs-actions.pod | 34 +++ java/com/redhat/et/libguestfs/GuestFS.java | 41 +++ java/com_redhat_et_libguestfs_GuestFS.c | 33 +++ ocaml/guestfs.ml | 1 + ocaml/guestfs.mli | 3 + ocaml/guestfs_c_actions.c | 26 ++ perl/Guestfs.xs | 19 ++ perl/lib/Sys/Guestfs.pm | 27 ++ python/guestfs-py.c | 26 ++ python/guestfs.py | 31 +++ ruby/ext/guestfs/_guestfs.c | 31 +++ src/guestfs-actions.c | 97 ++++++++ src/guestfs-actions.h | 1 + src/guestfs_protocol.c | 21 ++ src/guestfs_protocol.h | 20 +- src/guestfs_protocol.x | 9 + tests.c | 387 ++++++++++++++++++++++++++++- 22 files changed, 891 insertions(+), 2 deletions(-) diff --git a/daemon/actions.h b/daemon/actions.h index 79ec62a..8aed57c 100644 --- a/daemon/actions.h +++ b/daemon/actions.h @@ -127,3 +127,4 @@ extern int do_vg_activate_all (int activate); extern int do_vg_activate (int activate, char * const* const volgroups); extern int do_lvresize (const char *device, int mbytes); extern int do_resize2fs (const char *device); +extern char **do_find (const char *directory); diff --git a/daemon/stubs.c b/daemon/stubs.c index 2fdd5c1..2df8bba 100644 --- a/daemon/stubs.c +++ b/daemon/stubs.c @@ -2667,6 +2667,34 @@ done: xdr_free ((xdrproc_t) xdr_guestfs_resize2fs_args, (char *) &args); } +static void find_stub (XDR *xdr_in) +{ + char **r; + struct guestfs_find_args args; + const char *directory; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_find_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "find"); + return; + } + directory = args.directory; + + r = do_find (directory); + if (r == NULL) + /* do_find has already called reply_with_error */ + goto done; + + struct guestfs_find_ret ret; + ret.names.names_len = count_strings (r); + ret.names.names_val = r; + reply ((xdrproc_t) &xdr_guestfs_find_ret, (char *) &ret); + free_strings (r); +done: + xdr_free ((xdrproc_t) xdr_guestfs_find_args, (char *) &args); +} + void dispatch_incoming_message (XDR *xdr_in) { switch (proc_nr) { @@ -2988,6 +3016,9 @@ void dispatch_incoming_message (XDR *xdr_in) case GUESTFS_PROC_RESIZE2FS: resize2fs_stub (xdr_in); break; + case GUESTFS_PROC_FIND: + find_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 77b5d43..0f5b65d 100644 --- a/fish/cmds.c +++ b/fish/cmds.c @@ -72,6 +72,7 @@ void list_commands (void) printf ("%-20s %s\n", "equal", "test if two files have equal contents"); printf ("%-20s %s\n", "exists", "test if file or directory exists"); printf ("%-20s %s\n", "file", "determine file type"); + printf ("%-20s %s\n", "find", "find all files and directories"); printf ("%-20s %s\n", "fsck", "run the filesystem checker"); printf ("%-20s %s\n", "get-append", "get the additional kernel options"); printf ("%-20s %s\n", "get-autosync", "get autosync mode"); @@ -540,6 +541,9 @@ void display_command (const char *cmd) if (strcasecmp (cmd, "resize2fs") == 0) pod2text ("resize2fs - resize an ext2/ext3 filesystem", " resize2fs \n\nThis resizes an ext2 or ext3 filesystem to match the size of\nthe underlying device."); else + if (strcasecmp (cmd, "find") == 0) + pod2text ("find - find all files and directories", " find \n\nThis command lists out all files and directories, recursively,\nstarting at C. It is essentially equivalent to\nrunning the shell command C but some\npost-processing happens on the output, described below.\n\nThis returns a list of strings I. Thus\nif the directory structure was:\n\n /tmp/a\n /tmp/b\n /tmp/c/d\n\nthen the returned list from C C would be\n4 elements:\n\n a\n b\n c\n c/d\n\nIf C is not a directory, then this command returns\nan error.\n\nThe returned list is sorted."); + else display_builtin_command (cmd); } @@ -2641,6 +2645,23 @@ static int run_resize2fs (const char *cmd, int argc, char *argv[]) return r; } +static int run_find (const char *cmd, int argc, char *argv[]) +{ + char **r; + const char *directory; + 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; + } + directory = argv[0]; + r = guestfs_find (g, directory); + if (r == NULL) return -1; + print_strings (r); + free_strings (r); + return 0; +} + int run_action (const char *cmd, int argc, char *argv[]) { if (strcasecmp (cmd, "launch") == 0 || strcasecmp (cmd, "run") == 0) @@ -3021,6 +3042,9 @@ int run_action (const char *cmd, int argc, char *argv[]) if (strcasecmp (cmd, "resize2fs") == 0) return run_resize2fs (cmd, argc, argv); else + if (strcasecmp (cmd, "find") == 0) + return run_find (cmd, argc, argv); + else { fprintf (stderr, "%s: unknown command\n", cmd); return -1; diff --git a/fish/completion.c b/fish/completion.c index 33a5663..d0e8050 100644 --- a/fish/completion.c +++ b/fish/completion.c @@ -79,6 +79,7 @@ static const char *const commands[] = { "equal", "exists", "file", + "find", "fsck", "get-append", "get-autosync", diff --git a/guestfish-actions.pod b/guestfish-actions.pod index 3945fb1..ee8e824 100644 --- a/guestfish-actions.pod +++ b/guestfish-actions.pod @@ -514,6 +514,35 @@ The exact command which runs is C. Note in particular that the filename is not prepended to the output (the C<-b> option). +=head2 find + + find directory + +This command lists out all files and directories, recursively, +starting at C. It is essentially equivalent to +running the shell command C but some +post-processing happens on the output, described below. + +This returns a list of strings I. Thus +if the directory structure was: + + /tmp/a + /tmp/b + /tmp/c/d + +then the returned list from C C would be +4 elements: + + a + b + c + c/d + +If C is not a directory, then this command returns +an error. + +The returned list is sorted. + =head2 fsck fsck fstype device diff --git a/guestfs-actions.pod b/guestfs-actions.pod index 6f33d80..0d5efb4 100644 --- a/guestfs-actions.pod +++ b/guestfs-actions.pod @@ -677,6 +677,40 @@ particular that the filename is not prepended to the output This function returns a string, or NULL on error. I. +=head2 guestfs_find + + char **guestfs_find (guestfs_h *handle, + const char *directory); + +This command lists out all files and directories, recursively, +starting at C. It is essentially equivalent to +running the shell command C but some +post-processing happens on the output, described below. + +This returns a list of strings I. Thus +if the directory structure was: + + /tmp/a + /tmp/b + /tmp/c/d + +then the returned list from C C would be +4 elements: + + a + b + c + c/d + +If C is not a directory, then this command returns +an error. + +The returned list is sorted. + +This function returns a NULL-terminated array of strings +(like L), or NULL if there was an error. +I. + =head2 guestfs_fsck int guestfs_fsck (guestfs_h *handle, diff --git a/java/com/redhat/et/libguestfs/GuestFS.java b/java/com/redhat/et/libguestfs/GuestFS.java index 2846a80..f4cec79 100644 --- a/java/com/redhat/et/libguestfs/GuestFS.java +++ b/java/com/redhat/et/libguestfs/GuestFS.java @@ -2951,4 +2951,45 @@ public class GuestFS { private native void _resize2fs (long g, String device) throws LibGuestFSException; + /** + * find all files and directories + * + * This command lists out all files and directories, + * recursively, starting at "directory". It is essentially + * equivalent to running the shell command "find directory + * -print" but some post-processing happens on the output, + * described below. + * + * This returns a list of strings *without any prefix*. + * Thus if the directory structure was: + * + * /tmp/a + * /tmp/b + * /tmp/c/d + * + * then the returned list from "g.find" "/tmp" would be 4 + * elements: + * + * a + * b + * c + * c/d + * + * If "directory" is not a directory, then this command + * returns an error. + * + * The returned list is sorted. + * + * @throws LibGuestFSException + */ + public String[] find (String directory) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("find: handle is closed"); + return _find (g, directory); + } + private native String[] _find (long g, String directory) + throws LibGuestFSException; + } diff --git a/java/com_redhat_et_libguestfs_GuestFS.c b/java/com_redhat_et_libguestfs_GuestFS.c index 1850f21..1e7f9de 100644 --- a/java/com_redhat_et_libguestfs_GuestFS.c +++ b/java/com_redhat_et_libguestfs_GuestFS.c @@ -2930,3 +2930,36 @@ Java_com_redhat_et_libguestfs_GuestFS__1resize2fs } } +JNIEXPORT jobjectArray JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1find + (JNIEnv *env, jobject obj, jlong jg, jstring jdirectory) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + jobjectArray jr; + int r_len; + jclass cl; + jstring jstr; + char **r; + const char *directory; + int i; + + directory = (*env)->GetStringUTFChars (env, jdirectory, NULL); + r = guestfs_find (g, directory); + (*env)->ReleaseStringUTFChars (env, jdirectory, directory); + if (r == NULL) { + throw_exception (env, guestfs_last_error (g)); + return NULL; + } + for (r_len = 0; r[r_len] != NULL; ++r_len) ; + cl = (*env)->FindClass (env, "java/lang/String"); + jstr = (*env)->NewStringUTF (env, ""); + jr = (*env)->NewObjectArray (env, r_len, cl, jstr); + for (i = 0; i < r_len; ++i) { + jstr = (*env)->NewStringUTF (env, r[i]); + (*env)->SetObjectArrayElement (env, jr, i, jstr); + free (r[i]); + } + free (r); + return jr; +} + diff --git a/ocaml/guestfs.ml b/ocaml/guestfs.ml index 561b93c..0fe2d82 100644 --- a/ocaml/guestfs.ml +++ b/ocaml/guestfs.ml @@ -245,3 +245,4 @@ external vg_activate_all : t -> bool -> unit = "ocaml_guestfs_vg_activate_all" external vg_activate : t -> bool -> string array -> unit = "ocaml_guestfs_vg_activate" external lvresize : t -> string -> int -> unit = "ocaml_guestfs_lvresize" external resize2fs : t -> string -> unit = "ocaml_guestfs_resize2fs" +external find : t -> string -> string array = "ocaml_guestfs_find" diff --git a/ocaml/guestfs.mli b/ocaml/guestfs.mli index d9ce4af..f758552 100644 --- a/ocaml/guestfs.mli +++ b/ocaml/guestfs.mli @@ -514,3 +514,6 @@ val lvresize : t -> string -> int -> unit val resize2fs : t -> string -> unit (** resize an ext2/ext3 filesystem *) +val find : t -> string -> string array +(** find all files and directories *) + diff --git a/ocaml/guestfs_c_actions.c b/ocaml/guestfs_c_actions.c index c64bcf0..0dcaf3f 100644 --- a/ocaml/guestfs_c_actions.c +++ b/ocaml/guestfs_c_actions.c @@ -3426,3 +3426,29 @@ ocaml_guestfs_resize2fs (value gv, value devicev) CAMLreturn (rv); } +CAMLprim value +ocaml_guestfs_find (value gv, value directoryv) +{ + CAMLparam2 (gv, directoryv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("find: used handle after closing it"); + + const char *directory = String_val (directoryv); + int i; + char **r; + + caml_enter_blocking_section (); + r = guestfs_find (g, directory); + caml_leave_blocking_section (); + if (r == NULL) + ocaml_guestfs_raise_error (g, "find"); + + rv = caml_copy_string_array ((const char **) r); + for (i = 0; r[i] != NULL; ++i) free (r[i]); + free (r); + CAMLreturn (rv); +} + diff --git a/perl/Guestfs.xs b/perl/Guestfs.xs index 5a6cf36..0cc124c 100644 --- a/perl/Guestfs.xs +++ b/perl/Guestfs.xs @@ -1917,3 +1917,22 @@ PREINIT: if (r == -1) croak ("resize2fs: %s", guestfs_last_error (g)); +void +find (g, directory) + guestfs_h *g; + char *directory; +PREINIT: + char **names; + int i, n; + PPCODE: + names = guestfs_find (g, directory); + if (names == NULL) + croak ("find: %s", guestfs_last_error (g)); + for (n = 0; names[n] != NULL; ++n) /**/; + EXTEND (SP, n); + for (i = 0; i < n; ++i) { + PUSHs (sv_2mortal (newSVpv (names[i], 0))); + free (names[i]); + } + free (names); + diff --git a/perl/lib/Sys/Guestfs.pm b/perl/lib/Sys/Guestfs.pm index 62f3e7b..e7f8924 100644 --- a/perl/lib/Sys/Guestfs.pm +++ b/perl/lib/Sys/Guestfs.pm @@ -531,6 +531,33 @@ The exact command which runs is C. Note in particular that the filename is not prepended to the output (the C<-b> option). +=item @names = $h->find ($directory); + +This command lists out all files and directories, recursively, +starting at C. It is essentially equivalent to +running the shell command C but some +post-processing happens on the output, described below. + +This returns a list of strings I. Thus +if the directory structure was: + + /tmp/a + /tmp/b + /tmp/c/d + +then the returned list from C<$h-Efind> C would be +4 elements: + + a + b + c + c/d + +If C is not a directory, then this command returns +an error. + +The returned list is sorted. + =item $status = $h->fsck ($fstype, $device); This runs the filesystem checker (fsck) on C which diff --git a/python/guestfs-py.c b/python/guestfs-py.c index cd1014f..bb64104 100644 --- a/python/guestfs-py.c +++ b/python/guestfs-py.c @@ -3663,6 +3663,31 @@ py_guestfs_resize2fs (PyObject *self, PyObject *args) return py_r; } +static PyObject * +py_guestfs_find (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + char **r; + const char *directory; + + if (!PyArg_ParseTuple (args, (char *) "Os:guestfs_find", + &py_g, &directory)) + return NULL; + g = get_handle (py_g); + + r = guestfs_find (g, directory); + if (r == NULL) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + py_r = put_string_list (r); + free_strings (r); + return py_r; +} + static PyMethodDef methods[] = { { (char *) "create", py_guestfs_create, METH_VARARGS, NULL }, { (char *) "close", py_guestfs_close, METH_VARARGS, NULL }, @@ -3796,6 +3821,7 @@ static PyMethodDef methods[] = { { (char *) "vg_activate", py_guestfs_vg_activate, METH_VARARGS, NULL }, { (char *) "lvresize", py_guestfs_lvresize, METH_VARARGS, NULL }, { (char *) "resize2fs", py_guestfs_resize2fs, METH_VARARGS, NULL }, + { (char *) "find", py_guestfs_find, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/python/guestfs.py b/python/guestfs.py index 77c2989..237e62d 100644 --- a/python/guestfs.py +++ b/python/guestfs.py @@ -1436,3 +1436,34 @@ class GuestFS: """ return libguestfsmod.resize2fs (self._o, device) + def find (self, directory): + u"""This command lists out all files and directories, + recursively, starting at "directory". It is essentially + equivalent to running the shell command "find directory + -print" but some post-processing happens on the output, + described below. + + This returns a list of strings *without any prefix*. + Thus if the directory structure was: + + /tmp/a + /tmp/b + /tmp/c/d + + then the returned list from "g.find" "/tmp" would be 4 + elements: + + a + b + c + c/d + + If "directory" is not a directory, then this command + returns an error. + + The returned list is sorted. + + This function returns a list of strings. + """ + return libguestfsmod.find (self._o, directory) + diff --git a/ruby/ext/guestfs/_guestfs.c b/ruby/ext/guestfs/_guestfs.c index d7a4aa3..2deda5d 100644 --- a/ruby/ext/guestfs/_guestfs.c +++ b/ruby/ext/guestfs/_guestfs.c @@ -3115,6 +3115,35 @@ static VALUE ruby_guestfs_resize2fs (VALUE gv, VALUE devicev) return Qnil; } +static VALUE ruby_guestfs_find (VALUE gv, VALUE directoryv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "find"); + + const char *directory = StringValueCStr (directoryv); + if (!directory) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "directory", "find"); + + char **r; + + r = guestfs_find (g, directory); + if (r == NULL) + rb_raise (e_Error, "%s", guestfs_last_error (g)); + + int i, len = 0; + for (i = 0; r[i] != NULL; ++i) len++; + VALUE rv = rb_ary_new2 (len); + for (i = 0; r[i] != NULL; ++i) { + rb_ary_push (rv, rb_str_new2 (r[i])); + free (r[i]); + } + free (r); + return rv; +} + /* Initialize the module. */ void Init__guestfs () { @@ -3385,4 +3414,6 @@ void Init__guestfs () ruby_guestfs_lvresize, 2); rb_define_method (c_guestfs, "resize2fs", ruby_guestfs_resize2fs, 1); + rb_define_method (c_guestfs, "find", + ruby_guestfs_find, 1); } diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index f7f0fb9..0f603d6 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -9709,3 +9709,100 @@ int guestfs_resize2fs (guestfs_h *g, return 0; } +struct find_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_find_ret ret; +}; + +static void find_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct find_ctx *ctx = (struct find_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_find"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_find"); + 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_find"); + return; + } + goto done; + } + if (!xdr_guestfs_find_ret (xdr, &ctx->ret)) { + error (g, "%s: failed to parse reply", "guestfs_find"); + return; + } + done: + ctx->cb_sequence = 1; +} + +char **guestfs_find (guestfs_h *g, + const char *directory) +{ + struct guestfs_find_args args; + struct find_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_find") == -1) return NULL; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.directory = (char *) directory; + serial = guestfs__send_sync (g, GUESTFS_PROC_FIND, + (xdrproc_t) xdr_guestfs_find_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, find_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_find"); + guestfs_end_busy (g); + return NULL; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_FIND, 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); + /* caller will free this, but we need to add a NULL entry */ + ctx.ret.names.names_val = + safe_realloc (g, ctx.ret.names.names_val, + sizeof (char *) * (ctx.ret.names.names_len + 1)); + ctx.ret.names.names_val[ctx.ret.names.names_len] = NULL; + return ctx.ret.names.names_val; +} + diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h index 0e9be17..76472e8 100644 --- a/src/guestfs-actions.h +++ b/src/guestfs-actions.h @@ -149,3 +149,4 @@ extern int guestfs_vg_activate_all (guestfs_h *handle, int activate); extern int guestfs_vg_activate (guestfs_h *handle, int activate, char * const* const volgroups); extern int guestfs_lvresize (guestfs_h *handle, const char *device, int mbytes); extern int guestfs_resize2fs (guestfs_h *handle, const char *device); +extern char **guestfs_find (guestfs_h *handle, const char *directory); diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c index c352cb4..c2e96da 100644 --- a/src/guestfs_protocol.c +++ b/src/guestfs_protocol.c @@ -1808,6 +1808,27 @@ xdr_guestfs_resize2fs_args (XDR *xdrs, guestfs_resize2fs_args *objp) } bool_t +xdr_guestfs_find_args (XDR *xdrs, guestfs_find_args *objp) +{ + register int32_t *buf; + + if (!xdr_string (xdrs, &objp->directory, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_guestfs_find_ret (XDR *xdrs, guestfs_find_ret *objp) +{ + register int32_t *buf; + + if (!xdr_array (xdrs, (char **)&objp->names.names_val, (u_int *) &objp->names.names_len, ~0, + sizeof (str), (xdrproc_t) xdr_str)) + 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 a34395a..aef554b 100644 --- a/src/guestfs_protocol.h +++ b/src/guestfs_protocol.h @@ -912,6 +912,19 @@ struct guestfs_resize2fs_args { }; typedef struct guestfs_resize2fs_args guestfs_resize2fs_args; +struct guestfs_find_args { + char *directory; +}; +typedef struct guestfs_find_args guestfs_find_args; + +struct guestfs_find_ret { + struct { + u_int names_len; + str *names_val; + } names; +}; +typedef struct guestfs_find_ret guestfs_find_ret; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -1019,7 +1032,8 @@ enum guestfs_procedure { GUESTFS_PROC_VG_ACTIVATE = 104, GUESTFS_PROC_LVRESIZE = 105, GUESTFS_PROC_RESIZE2FS = 106, - GUESTFS_PROC_NR_PROCS = 106 + 1, + GUESTFS_PROC_FIND = 107, + GUESTFS_PROC_NR_PROCS = 107 + 1, }; typedef enum guestfs_procedure guestfs_procedure; #define GUESTFS_MESSAGE_MAX 4194304 @@ -1214,6 +1228,8 @@ extern bool_t xdr_guestfs_vg_activate_all_args (XDR *, guestfs_vg_activate_all_ extern bool_t xdr_guestfs_vg_activate_args (XDR *, guestfs_vg_activate_args*); extern bool_t xdr_guestfs_lvresize_args (XDR *, guestfs_lvresize_args*); extern bool_t xdr_guestfs_resize2fs_args (XDR *, guestfs_resize2fs_args*); +extern bool_t xdr_guestfs_find_args (XDR *, guestfs_find_args*); +extern bool_t xdr_guestfs_find_ret (XDR *, guestfs_find_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*); @@ -1367,6 +1383,8 @@ extern bool_t xdr_guestfs_vg_activate_all_args (); extern bool_t xdr_guestfs_vg_activate_args (); extern bool_t xdr_guestfs_lvresize_args (); extern bool_t xdr_guestfs_resize2fs_args (); +extern bool_t xdr_guestfs_find_args (); +extern bool_t xdr_guestfs_find_ret (); 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 6090810..6c1f84e 100644 --- a/src/guestfs_protocol.x +++ b/src/guestfs_protocol.x @@ -707,6 +707,14 @@ struct guestfs_resize2fs_args { string device<>; }; +struct guestfs_find_args { + string directory<>; +}; + +struct guestfs_find_ret { + str names<>; +}; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -814,6 +822,7 @@ enum guestfs_procedure { GUESTFS_PROC_VG_ACTIVATE = 104, GUESTFS_PROC_LVRESIZE = 105, GUESTFS_PROC_RESIZE2FS = 106, + GUESTFS_PROC_FIND = 107, GUESTFS_PROC_NR_PROCS }; diff --git a/tests.c b/tests.c index f551e98..19a3775 100644 --- a/tests.c +++ b/tests.c @@ -126,6 +126,373 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_resize2fs\" has no tests\n"); } +static int test_find_0 (void) +{ + /* InitBasicFS for test_find_0: create ext2 on /dev/sda1 */ + { + char device[] = "/dev/sda"; + device[5] = devchar; + 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"; + device[5] = devchar; + 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"; + device[5] = devchar; + int r; + suppress_error = 0; + r = guestfs_mkfs (g, fstype, device); + if (r == -1) + return -1; + } + { + char device[] = "/dev/sda1"; + device[5] = devchar; + char mountpoint[] = "/"; + int r; + suppress_error = 0; + r = guestfs_mount (g, device, mountpoint); + if (r == -1) + return -1; + } + /* TestOutputList for find (0) */ + { + char directory[] = "/"; + char **r; + int i; + suppress_error = 0; + r = guestfs_find (g, directory); + if (r == NULL) + return -1; + if (!r[0]) { + fprintf (stderr, "test_find_0: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "lost+found"; + if (strcmp (r[0], expected) != 0) { + fprintf (stderr, "test_find_0: expected \"%s\" but got \"%s\"\n", expected, r[0]); + return -1; + } + } + if (r[1] != NULL) { + fprintf (stderr, "test_find_0: extra elements returned from command\n"); + print_strings (r); + return -1; + } + for (i = 0; r[i] != NULL; ++i) + free (r[i]); + free (r); + } + return 0; +} + +static int test_find_1 (void) +{ + /* InitBasicFS for test_find_1: create ext2 on /dev/sda1 */ + { + char device[] = "/dev/sda"; + device[5] = devchar; + 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"; + device[5] = devchar; + 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"; + device[5] = devchar; + int r; + suppress_error = 0; + r = guestfs_mkfs (g, fstype, device); + if (r == -1) + return -1; + } + { + char device[] = "/dev/sda1"; + device[5] = devchar; + char mountpoint[] = "/"; + int r; + suppress_error = 0; + r = guestfs_mount (g, device, mountpoint); + if (r == -1) + return -1; + } + /* TestOutputList for find (1) */ + { + char path[] = "/a"; + int r; + suppress_error = 0; + r = guestfs_touch (g, path); + if (r == -1) + return -1; + } + { + char path[] = "/b"; + int r; + suppress_error = 0; + r = guestfs_mkdir (g, path); + if (r == -1) + return -1; + } + { + char path[] = "/b/c"; + int r; + suppress_error = 0; + r = guestfs_touch (g, path); + if (r == -1) + return -1; + } + { + char directory[] = "/"; + char **r; + int i; + suppress_error = 0; + r = guestfs_find (g, directory); + if (r == NULL) + return -1; + if (!r[0]) { + fprintf (stderr, "test_find_1: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "a"; + if (strcmp (r[0], expected) != 0) { + fprintf (stderr, "test_find_1: expected \"%s\" but got \"%s\"\n", expected, r[0]); + return -1; + } + } + if (!r[1]) { + fprintf (stderr, "test_find_1: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "b"; + if (strcmp (r[1], expected) != 0) { + fprintf (stderr, "test_find_1: expected \"%s\" but got \"%s\"\n", expected, r[1]); + return -1; + } + } + if (!r[2]) { + fprintf (stderr, "test_find_1: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "b/c"; + if (strcmp (r[2], expected) != 0) { + fprintf (stderr, "test_find_1: expected \"%s\" but got \"%s\"\n", expected, r[2]); + return -1; + } + } + if (!r[3]) { + fprintf (stderr, "test_find_1: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "lost+found"; + if (strcmp (r[3], expected) != 0) { + fprintf (stderr, "test_find_1: expected \"%s\" but got \"%s\"\n", expected, r[3]); + return -1; + } + } + if (r[4] != NULL) { + fprintf (stderr, "test_find_1: extra elements returned from command\n"); + print_strings (r); + return -1; + } + for (i = 0; r[i] != NULL; ++i) + free (r[i]); + free (r); + } + return 0; +} + +static int test_find_2 (void) +{ + /* InitBasicFS for test_find_2: create ext2 on /dev/sda1 */ + { + char device[] = "/dev/sda"; + device[5] = devchar; + 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"; + device[5] = devchar; + 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"; + device[5] = devchar; + int r; + suppress_error = 0; + r = guestfs_mkfs (g, fstype, device); + if (r == -1) + return -1; + } + { + char device[] = "/dev/sda1"; + device[5] = devchar; + char mountpoint[] = "/"; + int r; + suppress_error = 0; + r = guestfs_mount (g, device, mountpoint); + if (r == -1) + return -1; + } + /* TestOutputList for find (2) */ + { + char path[] = "/a/b/c"; + int r; + suppress_error = 0; + r = guestfs_mkdir_p (g, path); + if (r == -1) + return -1; + } + { + char path[] = "/a/b/c/d"; + int r; + suppress_error = 0; + r = guestfs_touch (g, path); + if (r == -1) + return -1; + } + { + char directory[] = "/a/b/"; + char **r; + int i; + suppress_error = 0; + r = guestfs_find (g, directory); + if (r == NULL) + return -1; + if (!r[0]) { + fprintf (stderr, "test_find_2: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "c"; + if (strcmp (r[0], expected) != 0) { + fprintf (stderr, "test_find_2: expected \"%s\" but got \"%s\"\n", expected, r[0]); + return -1; + } + } + if (!r[1]) { + fprintf (stderr, "test_find_2: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "c/d"; + if (strcmp (r[1], expected) != 0) { + fprintf (stderr, "test_find_2: expected \"%s\" but got \"%s\"\n", expected, r[1]); + return -1; + } + } + if (r[2] != NULL) { + fprintf (stderr, "test_find_2: extra elements returned from command\n"); + print_strings (r); + return -1; + } + for (i = 0; r[i] != NULL; ++i) + free (r[i]); + free (r); + } + return 0; +} + static int test_lvresize_0 (void) { /* InitNone|InitEmpty for test_lvresize_0 */ @@ -13298,9 +13665,27 @@ int main (int argc, char *argv[]) free (devs[i]); free (devs); - nr_tests = 137; + nr_tests = 140; test_num++; + printf ("%3d/%3d test_find_0\n", test_num, nr_tests); + if (test_find_0 () == -1) { + printf ("test_find_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_find_1\n", test_num, nr_tests); + if (test_find_1 () == -1) { + printf ("test_find_1 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_find_2\n", test_num, nr_tests); + if (test_find_2 () == -1) { + printf ("test_find_2 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_lvresize_0\n", test_num, nr_tests); if (test_lvresize_0 () == -1) { printf ("test_lvresize_0 FAILED\n"); -- 1.8.3.1