From ad8a256f54a6cb99f89bb444c8597a152a793dce Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 22 Jun 2009 08:20:42 +0100 Subject: [PATCH 1/1] Generated code for 'glob-expand'. --- capitests/tests.c | 419 ++++++++++++++++++++++++++++- daemon/actions.h | 1 + daemon/stubs.c | 31 +++ fish/cmds.c | 24 ++ fish/completion.c | 1 + guestfish-actions.pod | 15 ++ guestfs-actions.pod | 20 ++ java/com/redhat/et/libguestfs/GuestFS.java | 26 ++ 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 | 13 + python/guestfs-py.c | 26 ++ python/guestfs.py | 16 ++ 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 + 22 files changed, 851 insertions(+), 2 deletions(-) diff --git a/capitests/tests.c b/capitests/tests.c index e5466b5..309bac3 100644 --- a/capitests/tests.c +++ b/capitests/tests.c @@ -152,6 +152,405 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_sh_lines\" has no tests\n"); } +static int test_glob_expand_0_skip (void) +{ + const char *str; + + str = getenv ("SKIP_TEST_GLOB_EXPAND_0"); + if (str && strcmp (str, "1") == 0) return 1; + str = getenv ("SKIP_TEST_GLOB_EXPAND"); + if (str && strcmp (str, "1") == 0) return 1; + return 0; +} + +static int test_glob_expand_0 (void) +{ + if (test_glob_expand_0_skip ()) { + printf ("%s skipped (reason: SKIP_TEST_* variable set)\n", "test_glob_expand_0"); + return 0; + } + + /* InitBasicFS for test_glob_expand_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; + } + /* TestOutputList for glob_expand (0) */ + { + 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 path[] = "/a/b/c/e"; + int r; + suppress_error = 0; + r = guestfs_touch (g, path); + if (r == -1) + return -1; + } + { + char pattern[] = "/a/b/c/*"; + char **r; + int i; + suppress_error = 0; + r = guestfs_glob_expand (g, pattern); + if (r == NULL) + return -1; + if (!r[0]) { + fprintf (stderr, "test_glob_expand_0: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "/a/b/c/d"; + if (strcmp (r[0], expected) != 0) { + fprintf (stderr, "test_glob_expand_0: expected \"%s\" but got \"%s\"\n", expected, r[0]); + return -1; + } + } + if (!r[1]) { + fprintf (stderr, "test_glob_expand_0: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "/a/b/c/e"; + if (strcmp (r[1], expected) != 0) { + fprintf (stderr, "test_glob_expand_0: expected \"%s\" but got \"%s\"\n", expected, r[1]); + return -1; + } + } + if (r[2] != NULL) { + fprintf (stderr, "test_glob_expand_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_glob_expand_1_skip (void) +{ + const char *str; + + str = getenv ("SKIP_TEST_GLOB_EXPAND_1"); + if (str && strcmp (str, "1") == 0) return 1; + str = getenv ("SKIP_TEST_GLOB_EXPAND"); + if (str && strcmp (str, "1") == 0) return 1; + return 0; +} + +static int test_glob_expand_1 (void) +{ + if (test_glob_expand_1_skip ()) { + printf ("%s skipped (reason: SKIP_TEST_* variable set)\n", "test_glob_expand_1"); + return 0; + } + + /* InitBasicFS for test_glob_expand_1: 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; + } + /* TestOutputList for glob_expand (1) */ + { + 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 path[] = "/a/b/c/e"; + int r; + suppress_error = 0; + r = guestfs_touch (g, path); + if (r == -1) + return -1; + } + { + char pattern[] = "/a/*/c/*"; + char **r; + int i; + suppress_error = 0; + r = guestfs_glob_expand (g, pattern); + if (r == NULL) + return -1; + if (!r[0]) { + fprintf (stderr, "test_glob_expand_1: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "/a/b/c/d"; + if (strcmp (r[0], expected) != 0) { + fprintf (stderr, "test_glob_expand_1: expected \"%s\" but got \"%s\"\n", expected, r[0]); + return -1; + } + } + if (!r[1]) { + fprintf (stderr, "test_glob_expand_1: short list returned from command\n"); + print_strings (r); + return -1; + } + { + char expected[] = "/a/b/c/e"; + if (strcmp (r[1], expected) != 0) { + fprintf (stderr, "test_glob_expand_1: expected \"%s\" but got \"%s\"\n", expected, r[1]); + return -1; + } + } + if (r[2] != NULL) { + fprintf (stderr, "test_glob_expand_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_glob_expand_2_skip (void) +{ + const char *str; + + str = getenv ("SKIP_TEST_GLOB_EXPAND_2"); + if (str && strcmp (str, "1") == 0) return 1; + str = getenv ("SKIP_TEST_GLOB_EXPAND"); + if (str && strcmp (str, "1") == 0) return 1; + return 0; +} + +static int test_glob_expand_2 (void) +{ + if (test_glob_expand_2_skip ()) { + printf ("%s skipped (reason: SKIP_TEST_* variable set)\n", "test_glob_expand_2"); + return 0; + } + + /* InitBasicFS for test_glob_expand_2: 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; + } + /* TestOutputList for glob_expand (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 path[] = "/a/b/c/e"; + int r; + suppress_error = 0; + r = guestfs_touch (g, path); + if (r == -1) + return -1; + } + { + char pattern[] = "/a/*/x/*"; + char **r; + int i; + suppress_error = 0; + r = guestfs_glob_expand (g, pattern); + if (r == NULL) + return -1; + if (r[0] != NULL) { + fprintf (stderr, "test_glob_expand_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_ntfs_3g_probe_0_skip (void) { const char *str; @@ -15767,9 +16166,27 @@ int main (int argc, char *argv[]) /* Cancel previous alarm. */ alarm (0); - nr_tests = 146; + nr_tests = 149; test_num++; + printf ("%3d/%3d test_glob_expand_0\n", test_num, nr_tests); + if (test_glob_expand_0 () == -1) { + printf ("test_glob_expand_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_glob_expand_1\n", test_num, nr_tests); + if (test_glob_expand_1 () == -1) { + printf ("test_glob_expand_1 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_glob_expand_2\n", test_num, nr_tests); + if (test_glob_expand_2 () == -1) { + printf ("test_glob_expand_2 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_ntfs_3g_probe_0\n", test_num, nr_tests); if (test_ntfs_3g_probe_0 () == -1) { printf ("test_ntfs_3g_probe_0 FAILED\n"); diff --git a/daemon/actions.h b/daemon/actions.h index a76498f..cc8bc78 100644 --- a/daemon/actions.h +++ b/daemon/actions.h @@ -133,3 +133,4 @@ extern int do_sleep (int secs); extern int do_ntfs_3g_probe (int rw, char *device); extern char *do_sh (char *command); extern char **do_sh_lines (char *command); +extern char **do_glob_expand (char *pattern); diff --git a/daemon/stubs.c b/daemon/stubs.c index dcdc51c..2a6035c 100644 --- a/daemon/stubs.c +++ b/daemon/stubs.c @@ -2826,6 +2826,34 @@ done: xdr_free ((xdrproc_t) xdr_guestfs_sh_lines_args, (char *) &args); } +static void glob_expand_stub (XDR *xdr_in) +{ + char **r; + struct guestfs_glob_expand_args args; + char *pattern; + + memset (&args, 0, sizeof args); + + if (!xdr_guestfs_glob_expand_args (xdr_in, &args)) { + reply_with_error ("%s: daemon failed to decode procedure arguments", "glob_expand"); + return; + } + pattern = args.pattern; + + r = do_glob_expand (pattern); + if (r == NULL) + /* do_glob_expand has already called reply_with_error */ + goto done; + + struct guestfs_glob_expand_ret ret; + ret.paths.paths_len = count_strings (r); + ret.paths.paths_val = r; + reply ((xdrproc_t) &xdr_guestfs_glob_expand_ret, (char *) &ret); + free_strings (r); +done: + xdr_free ((xdrproc_t) xdr_guestfs_glob_expand_args, (char *) &args); +} + void dispatch_incoming_message (XDR *xdr_in) { switch (proc_nr) { @@ -3165,6 +3193,9 @@ void dispatch_incoming_message (XDR *xdr_in) case GUESTFS_PROC_SH_LINES: sh_lines_stub (xdr_in); break; + case GUESTFS_PROC_GLOB_EXPAND: + glob_expand_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 fe1ef92..8c35eea 100644 --- a/fish/cmds.c +++ b/fish/cmds.c @@ -84,6 +84,7 @@ void list_commands (void) printf ("%-20s %s\n", "get-qemu", "get the qemu binary"); printf ("%-20s %s\n", "get-state", "get the current state"); printf ("%-20s %s\n", "get-verbose", "get verbose mode"); + printf ("%-20s %s\n", "glob-expand", "expand a wildcard path"); printf ("%-20s %s\n", "grub-install", "install GRUB"); printf ("%-20s %s\n", "hexdump", "dump a file in hexadecimal"); printf ("%-20s %s\n", "is-busy", "is busy processing a command"); @@ -568,6 +569,9 @@ void display_command (const char *cmd) if (strcasecmp (cmd, "sh_lines") == 0 || strcasecmp (cmd, "sh-lines") == 0) pod2text ("sh-lines - run a command via the shell returning lines", " sh-lines \n\nThis is the same as C, but splits the result\ninto a list of lines.\n\nSee also: C"); else + if (strcasecmp (cmd, "glob_expand") == 0 || strcasecmp (cmd, "glob-expand") == 0) + pod2text ("glob-expand - expand a wildcard path", " glob-expand \n\nThis command searches for all the pathnames matching\nC according to the wildcard expansion rules\nused by the shell.\n\nIf no paths match, then this returns an empty list\n(note: not an error).\n\nIt is just a wrapper around the C L function\nwith flags C.\nSee that manual page for more details."); + else display_builtin_command (cmd); } @@ -2780,6 +2784,23 @@ static int run_sh_lines (const char *cmd, int argc, char *argv[]) return 0; } +static int run_glob_expand (const char *cmd, int argc, char *argv[]) +{ + char **r; + const char *pattern; + 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; + } + pattern = argv[0]; + r = guestfs_glob_expand (g, pattern); + 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) @@ -3181,6 +3202,9 @@ int run_action (const char *cmd, int argc, char *argv[]) if (strcasecmp (cmd, "sh_lines") == 0 || strcasecmp (cmd, "sh-lines") == 0) return run_sh_lines (cmd, argc, argv); else + if (strcasecmp (cmd, "glob_expand") == 0 || strcasecmp (cmd, "glob-expand") == 0) + return run_glob_expand (cmd, argc, argv); + else { fprintf (stderr, "%s: unknown command\n", cmd); return -1; diff --git a/fish/completion.c b/fish/completion.c index 4ac0fad..e8d8cea 100644 --- a/fish/completion.c +++ b/fish/completion.c @@ -179,6 +179,7 @@ static const char *const commands[] = { "ntfs-3g-probe", "sh", "sh-lines", + "glob-expand", NULL }; diff --git a/guestfish-actions.pod b/guestfish-actions.pod index 3217d92..4845990 100644 --- a/guestfish-actions.pod +++ b/guestfish-actions.pod @@ -683,6 +683,21 @@ For more information on states, see L. This returns the verbose messages flag. +=head2 glob-expand + + glob-expand pattern + +This command searches for all the pathnames matching +C according to the wildcard expansion rules +used by the shell. + +If no paths match, then this returns an empty list +(note: not an error). + +It is just a wrapper around the C L function +with flags C. +See that manual page for more details. + =head2 grub-install grub-install root device diff --git a/guestfs-actions.pod b/guestfs-actions.pod index 93a9fbb..d35d1aa 100644 --- a/guestfs-actions.pod +++ b/guestfs-actions.pod @@ -884,6 +884,26 @@ This returns the verbose messages flag. This function returns a C truth value on success or -1 on error. +=head2 guestfs_glob_expand + + char **guestfs_glob_expand (guestfs_h *handle, + const char *pattern); + +This command searches for all the pathnames matching +C according to the wildcard expansion rules +used by the shell. + +If no paths match, then this returns an empty list +(note: not an error). + +It is just a wrapper around the C L function +with flags C. +See that manual page for more details. + +This function returns a NULL-terminated array of strings +(like L), or NULL if there was an error. +I. + =head2 guestfs_grub_install int guestfs_grub_install (guestfs_h *handle, diff --git a/java/com/redhat/et/libguestfs/GuestFS.java b/java/com/redhat/et/libguestfs/GuestFS.java index 4075420..cdc0f09 100644 --- a/java/com/redhat/et/libguestfs/GuestFS.java +++ b/java/com/redhat/et/libguestfs/GuestFS.java @@ -3435,4 +3435,30 @@ public HashMap test0rhashtableerr () private native String[] _sh_lines (long g, String command) throws LibGuestFSException; + /** + * expand a wildcard path + *

+ * This command searches for all the pathnames matching + * "pattern" according to the wildcard expansion rules used + * by the shell. + *

+ * If no paths match, then this returns an empty list + * (note: not an error). + *

+ * It is just a wrapper around the C glob(3) function with + * flags "GLOB_MARK|GLOB_BRACE". See that manual page for + * more details. + *

+ * @throws LibGuestFSException + */ + public String[] glob_expand (String pattern) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("glob_expand: handle is closed"); + return _glob_expand (g, pattern); + } + private native String[] _glob_expand (long g, String pattern) + throws LibGuestFSException; + } diff --git a/java/com_redhat_et_libguestfs_GuestFS.c b/java/com_redhat_et_libguestfs_GuestFS.c index 631e48f..8a5afe9 100644 --- a/java/com_redhat_et_libguestfs_GuestFS.c +++ b/java/com_redhat_et_libguestfs_GuestFS.c @@ -4040,3 +4040,36 @@ Java_com_redhat_et_libguestfs_GuestFS__1sh_1lines return jr; } +JNIEXPORT jobjectArray JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1glob_1expand + (JNIEnv *env, jobject obj, jlong jg, jstring jpattern) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + jobjectArray jr; + int r_len; + jclass cl; + jstring jstr; + char **r; + const char *pattern; + int i; + + pattern = (*env)->GetStringUTFChars (env, jpattern, NULL); + r = guestfs_glob_expand (g, pattern); + (*env)->ReleaseStringUTFChars (env, jpattern, pattern); + 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 f102459..fb83545 100644 --- a/ocaml/guestfs.ml +++ b/ocaml/guestfs.ml @@ -279,3 +279,4 @@ external sleep : t -> int -> unit = "ocaml_guestfs_sleep" external ntfs_3g_probe : t -> bool -> string -> int = "ocaml_guestfs_ntfs_3g_probe" external sh : t -> string -> string = "ocaml_guestfs_sh" external sh_lines : t -> string -> string array = "ocaml_guestfs_sh_lines" +external glob_expand : t -> string -> string array = "ocaml_guestfs_glob_expand" diff --git a/ocaml/guestfs.mli b/ocaml/guestfs.mli index 8983a16..345c7ec 100644 --- a/ocaml/guestfs.mli +++ b/ocaml/guestfs.mli @@ -616,3 +616,6 @@ val sh : t -> string -> string val sh_lines : t -> string -> string array (** run a command via the shell returning lines *) +val glob_expand : t -> string -> string array +(** expand a wildcard path *) + diff --git a/ocaml/guestfs_c_actions.c b/ocaml/guestfs_c_actions.c index e4dc509..1ce55f0 100644 --- a/ocaml/guestfs_c_actions.c +++ b/ocaml/guestfs_c_actions.c @@ -4248,3 +4248,29 @@ ocaml_guestfs_sh_lines (value gv, value commandv) CAMLreturn (rv); } +CAMLprim value +ocaml_guestfs_glob_expand (value gv, value patternv) +{ + CAMLparam2 (gv, patternv); + CAMLlocal1 (rv); + + guestfs_h *g = Guestfs_val (gv); + if (g == NULL) + caml_failwith ("glob_expand: used handle after closing it"); + + const char *pattern = String_val (patternv); + int i; + char **r; + + caml_enter_blocking_section (); + r = guestfs_glob_expand (g, pattern); + caml_leave_blocking_section (); + if (r == NULL) + ocaml_guestfs_raise_error (g, "glob_expand"); + + 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 8a6c4bb..5905dc5 100644 --- a/perl/Guestfs.xs +++ b/perl/Guestfs.xs @@ -2583,3 +2583,22 @@ PREINIT: } free (lines); +void +glob_expand (g, pattern) + guestfs_h *g; + char *pattern; +PREINIT: + char **paths; + int i, n; + PPCODE: + paths = guestfs_glob_expand (g, pattern); + if (paths == NULL) + croak ("glob_expand: %s", guestfs_last_error (g)); + for (n = 0; paths[n] != NULL; ++n) /**/; + EXTEND (SP, n); + for (i = 0; i < n; ++i) { + PUSHs (sv_2mortal (newSVpv (paths[i], 0))); + free (paths[i]); + } + free (paths); + diff --git a/perl/lib/Sys/Guestfs.pm b/perl/lib/Sys/Guestfs.pm index 9329b76..436f218 100644 --- a/perl/lib/Sys/Guestfs.pm +++ b/perl/lib/Sys/Guestfs.pm @@ -676,6 +676,19 @@ For more information on states, see L. This returns the verbose messages flag. +=item @paths = $h->glob_expand ($pattern); + +This command searches for all the pathnames matching +C according to the wildcard expansion rules +used by the shell. + +If no paths match, then this returns an empty list +(note: not an error). + +It is just a wrapper around the C L function +with flags C. +See that manual page for more details. + =item $h->grub_install ($root, $device); This command installs GRUB (the Grand Unified Bootloader) on diff --git a/python/guestfs-py.c b/python/guestfs-py.c index f5bc109..60e20d2 100644 --- a/python/guestfs-py.c +++ b/python/guestfs-py.c @@ -4506,6 +4506,31 @@ py_guestfs_sh_lines (PyObject *self, PyObject *args) return py_r; } +static PyObject * +py_guestfs_glob_expand (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_r; + char **r; + const char *pattern; + + if (!PyArg_ParseTuple (args, (char *) "Os:guestfs_glob_expand", + &py_g, &pattern)) + return NULL; + g = get_handle (py_g); + + r = guestfs_glob_expand (g, pattern); + 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 }, @@ -4673,6 +4698,7 @@ static PyMethodDef methods[] = { { (char *) "ntfs_3g_probe", py_guestfs_ntfs_3g_probe, METH_VARARGS, NULL }, { (char *) "sh", py_guestfs_sh, METH_VARARGS, NULL }, { (char *) "sh_lines", py_guestfs_sh_lines, METH_VARARGS, NULL }, + { (char *) "glob_expand", py_guestfs_glob_expand, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/python/guestfs.py b/python/guestfs.py index c9658cd..3976610 100644 --- a/python/guestfs.py +++ b/python/guestfs.py @@ -1651,3 +1651,19 @@ class GuestFS: """ return libguestfsmod.sh_lines (self._o, command) + def glob_expand (self, pattern): + u"""This command searches for all the pathnames matching + "pattern" according to the wildcard expansion rules used + by the shell. + + If no paths match, then this returns an empty list + (note: not an error). + + It is just a wrapper around the C glob(3) function with + flags "GLOB_MARK|GLOB_BRACE". See that manual page for + more details. + + This function returns a list of strings. + """ + return libguestfsmod.glob_expand (self._o, pattern) + diff --git a/ruby/ext/guestfs/_guestfs.c b/ruby/ext/guestfs/_guestfs.c index 81e3691..bcb6fef 100644 --- a/ruby/ext/guestfs/_guestfs.c +++ b/ruby/ext/guestfs/_guestfs.c @@ -4055,6 +4055,35 @@ static VALUE ruby_guestfs_sh_lines (VALUE gv, VALUE commandv) return rv; } +static VALUE ruby_guestfs_glob_expand (VALUE gv, VALUE patternv) +{ + guestfs_h *g; + Data_Get_Struct (gv, guestfs_h, g); + if (!g) + rb_raise (rb_eArgError, "%s: used handle after closing it", "glob_expand"); + + const char *pattern = StringValueCStr (patternv); + if (!pattern) + rb_raise (rb_eTypeError, "expected string for parameter %s of %s", + "pattern", "glob_expand"); + + char **r; + + r = guestfs_glob_expand (g, pattern); + 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 () { @@ -4393,4 +4422,6 @@ void Init__guestfs () ruby_guestfs_sh, 1); rb_define_method (c_guestfs, "sh_lines", ruby_guestfs_sh_lines, 1); + rb_define_method (c_guestfs, "glob_expand", + ruby_guestfs_glob_expand, 1); } diff --git a/src/guestfs-actions.c b/src/guestfs-actions.c index f36fcf0..e1ec158 100644 --- a/src/guestfs-actions.c +++ b/src/guestfs-actions.c @@ -10263,3 +10263,100 @@ char **guestfs_sh_lines (guestfs_h *g, return ctx.ret.lines.lines_val; } +struct glob_expand_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_glob_expand_ret ret; +}; + +static void glob_expand_reply_cb (guestfs_h *g, void *data, XDR *xdr) +{ + guestfs_main_loop *ml = guestfs_get_main_loop (g); + struct glob_expand_ctx *ctx = (struct glob_expand_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_glob_expand"); + return; + } + + ml->main_loop_quit (ml, g); + + if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) { + error (g, "%s: failed to parse reply header", "guestfs_glob_expand"); + 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_glob_expand"); + return; + } + goto done; + } + if (!xdr_guestfs_glob_expand_ret (xdr, &ctx->ret)) { + error (g, "%s: failed to parse reply", "guestfs_glob_expand"); + return; + } + done: + ctx->cb_sequence = 1; +} + +char **guestfs_glob_expand (guestfs_h *g, + const char *pattern) +{ + struct guestfs_glob_expand_args args; + struct glob_expand_ctx ctx; + guestfs_main_loop *ml = guestfs_get_main_loop (g); + int serial; + + if (check_state (g, "guestfs_glob_expand") == -1) return NULL; + guestfs_set_busy (g); + + memset (&ctx, 0, sizeof ctx); + + args.pattern = (char *) pattern; + serial = guestfs__send_sync (g, GUESTFS_PROC_GLOB_EXPAND, + (xdrproc_t) xdr_guestfs_glob_expand_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, glob_expand_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_glob_expand"); + guestfs_end_busy (g); + return NULL; + } + + if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_GLOB_EXPAND, 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.paths.paths_val = + safe_realloc (g, ctx.ret.paths.paths_val, + sizeof (char *) * (ctx.ret.paths.paths_len + 1)); + ctx.ret.paths.paths_val[ctx.ret.paths.paths_len] = NULL; + return ctx.ret.paths.paths_val; +} + diff --git a/src/guestfs-actions.h b/src/guestfs-actions.h index fe9967f..19996e8 100644 --- a/src/guestfs-actions.h +++ b/src/guestfs-actions.h @@ -183,3 +183,4 @@ extern int guestfs_sleep (guestfs_h *handle, int secs); extern int guestfs_ntfs_3g_probe (guestfs_h *handle, int rw, const char *device); extern char *guestfs_sh (guestfs_h *handle, const char *command); extern char **guestfs_sh_lines (guestfs_h *handle, const char *command); +extern char **guestfs_glob_expand (guestfs_h *handle, const char *pattern); diff --git a/src/guestfs_protocol.c b/src/guestfs_protocol.c index 96951d9..b2d435e 100644 --- a/src/guestfs_protocol.c +++ b/src/guestfs_protocol.c @@ -1912,6 +1912,27 @@ xdr_guestfs_sh_lines_ret (XDR *xdrs, guestfs_sh_lines_ret *objp) } bool_t +xdr_guestfs_glob_expand_args (XDR *xdrs, guestfs_glob_expand_args *objp) +{ + register int32_t *buf; + + if (!xdr_string (xdrs, &objp->pattern, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_guestfs_glob_expand_ret (XDR *xdrs, guestfs_glob_expand_ret *objp) +{ + register int32_t *buf; + + if (!xdr_array (xdrs, (char **)&objp->paths.paths_val, (u_int *) &objp->paths.paths_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 a9863cc..8fdb664 100644 --- a/src/guestfs_protocol.h +++ b/src/guestfs_protocol.h @@ -969,6 +969,19 @@ struct guestfs_sh_lines_ret { }; typedef struct guestfs_sh_lines_ret guestfs_sh_lines_ret; +struct guestfs_glob_expand_args { + char *pattern; +}; +typedef struct guestfs_glob_expand_args guestfs_glob_expand_args; + +struct guestfs_glob_expand_ret { + struct { + u_int paths_len; + str *paths_val; + } paths; +}; +typedef struct guestfs_glob_expand_ret guestfs_glob_expand_ret; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -1082,7 +1095,8 @@ enum guestfs_procedure { GUESTFS_PROC_NTFS_3G_PROBE = 110, GUESTFS_PROC_SH = 111, GUESTFS_PROC_SH_LINES = 112, - GUESTFS_PROC_NR_PROCS = 112 + 1, + GUESTFS_PROC_GLOB_EXPAND = 113, + GUESTFS_PROC_NR_PROCS = 113 + 1, }; typedef enum guestfs_procedure guestfs_procedure; #define GUESTFS_MESSAGE_MAX 4194304 @@ -1287,6 +1301,8 @@ extern bool_t xdr_guestfs_sh_args (XDR *, guestfs_sh_args*); extern bool_t xdr_guestfs_sh_ret (XDR *, guestfs_sh_ret*); extern bool_t xdr_guestfs_sh_lines_args (XDR *, guestfs_sh_lines_args*); extern bool_t xdr_guestfs_sh_lines_ret (XDR *, guestfs_sh_lines_ret*); +extern bool_t xdr_guestfs_glob_expand_args (XDR *, guestfs_glob_expand_args*); +extern bool_t xdr_guestfs_glob_expand_ret (XDR *, guestfs_glob_expand_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*); @@ -1450,6 +1466,8 @@ extern bool_t xdr_guestfs_sh_args (); extern bool_t xdr_guestfs_sh_ret (); extern bool_t xdr_guestfs_sh_lines_args (); extern bool_t xdr_guestfs_sh_lines_ret (); +extern bool_t xdr_guestfs_glob_expand_args (); +extern bool_t xdr_guestfs_glob_expand_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 94d2e84..d662726 100644 --- a/src/guestfs_protocol.x +++ b/src/guestfs_protocol.x @@ -748,6 +748,14 @@ struct guestfs_sh_lines_ret { str lines<>; }; +struct guestfs_glob_expand_args { + string pattern<>; +}; + +struct guestfs_glob_expand_ret { + str paths<>; +}; + enum guestfs_procedure { GUESTFS_PROC_MOUNT = 1, GUESTFS_PROC_SYNC = 2, @@ -861,6 +869,7 @@ enum guestfs_procedure { GUESTFS_PROC_NTFS_3G_PROBE = 110, GUESTFS_PROC_SH = 111, GUESTFS_PROC_SH_LINES = 112, + GUESTFS_PROC_GLOB_EXPAND = 113, GUESTFS_PROC_NR_PROCS }; -- 1.8.3.1