Generated code for 'glob-expand'.
authorRichard Jones <rjones@trick.home.annexia.org>
Mon, 22 Jun 2009 07:20:42 +0000 (08:20 +0100)
committerRichard Jones <rjones@trick.home.annexia.org>
Mon, 22 Jun 2009 07:20:42 +0000 (08:20 +0100)
22 files changed:
capitests/tests.c
daemon/actions.h
daemon/stubs.c
fish/cmds.c
fish/completion.c
guestfish-actions.pod
guestfs-actions.pod
java/com/redhat/et/libguestfs/GuestFS.java
java/com_redhat_et_libguestfs_GuestFS.c
ocaml/guestfs.ml
ocaml/guestfs.mli
ocaml/guestfs_c_actions.c
perl/Guestfs.xs
perl/lib/Sys/Guestfs.pm
python/guestfs-py.c
python/guestfs.py
ruby/ext/guestfs/_guestfs.c
src/guestfs-actions.c
src/guestfs-actions.h
src/guestfs_protocol.c
src/guestfs_protocol.h
src/guestfs_protocol.x

index e5466b5..309bac3 100644 (file)
@@ -152,6 +152,405 @@ static void no_test_warnings (void)
   fprintf (stderr, "warning: \"guestfs_sh_lines\" has no tests\n");
 }
 
   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;
 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);
 
   /* Cancel previous alarm. */
   alarm (0);
 
-  nr_tests = 146;
+  nr_tests = 149;
 
   test_num++;
 
   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");
   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");
index a76498f..cc8bc78 100644 (file)
@@ -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 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);
index dcdc51c..2a6035c 100644 (file)
@@ -2826,6 +2826,34 @@ done:
   xdr_free ((xdrproc_t) xdr_guestfs_sh_lines_args, (char *) &args);
 }
 
   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) {
 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_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);
   }
     default:
       reply_with_error ("dispatch_incoming_message: unknown procedure number %d", proc_nr);
   }
index fe1ef92..8c35eea 100644 (file)
@@ -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", "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");
   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 <command>\n\nThis is the same as C<sh>, but splits the result\ninto a list of lines.\n\nSee also: C<command_lines>");
   else
   if (strcasecmp (cmd, "sh_lines") == 0 || strcasecmp (cmd, "sh-lines") == 0)
     pod2text ("sh-lines - run a command via the shell returning lines", " sh-lines <command>\n\nThis is the same as C<sh>, but splits the result\ninto a list of lines.\n\nSee also: C<command_lines>");
   else
+  if (strcasecmp (cmd, "glob_expand") == 0 || strcasecmp (cmd, "glob-expand") == 0)
+    pod2text ("glob-expand - expand a wildcard path", " glob-expand <pattern>\n\nThis command searches for all the pathnames matching\nC<pattern> 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<glob(3)> function\nwith flags C<GLOB_MARK|GLOB_BRACE>.\nSee that manual page for more details.");
+  else
     display_builtin_command (cmd);
 }
 
     display_builtin_command (cmd);
 }
 
@@ -2780,6 +2784,23 @@ static int run_sh_lines (const char *cmd, int argc, char *argv[])
   return 0;
 }
 
   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)
 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, "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;
     {
       fprintf (stderr, "%s: unknown command\n", cmd);
       return -1;
index 4ac0fad..e8d8cea 100644 (file)
@@ -179,6 +179,7 @@ static const char *const commands[] = {
   "ntfs-3g-probe",
   "sh",
   "sh-lines",
   "ntfs-3g-probe",
   "sh",
   "sh-lines",
+  "glob-expand",
   NULL
 };
 
   NULL
 };
 
index 3217d92..4845990 100644 (file)
@@ -683,6 +683,21 @@ For more information on states, see L<guestfs(3)>.
 
 This returns the verbose messages flag.
 
 
 This returns the verbose messages flag.
 
+=head2 glob-expand
+
+ glob-expand pattern
+
+This command searches for all the pathnames matching
+C<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 L<glob(3)> function
+with flags C<GLOB_MARK|GLOB_BRACE>.
+See that manual page for more details.
+
 =head2 grub-install
 
  grub-install root device
 =head2 grub-install
 
  grub-install root device
index 93a9fbb..d35d1aa 100644 (file)
@@ -884,6 +884,26 @@ This returns the verbose messages flag.
 
 This function returns a C truth value on success or -1 on error.
 
 
 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<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 L<glob(3)> function
+with flags C<GLOB_MARK|GLOB_BRACE>.
+See that manual page for more details.
+
+This function returns a NULL-terminated array of strings
+(like L<environ(3)>), or NULL if there was an error.
+I<The caller must free the strings and the array after use>.
+
 =head2 guestfs_grub_install
 
  int guestfs_grub_install (guestfs_h *handle,
 =head2 guestfs_grub_install
 
  int guestfs_grub_install (guestfs_h *handle,
index 4075420..cdc0f09 100644 (file)
@@ -3435,4 +3435,30 @@ public HashMap<String,String> test0rhashtableerr ()
   private native String[] _sh_lines (long g, String command)
     throws LibGuestFSException;
 
   private native String[] _sh_lines (long g, String command)
     throws LibGuestFSException;
 
+  /**
+   * expand a wildcard path
+   * <p>
+   * This command searches for all the pathnames matching
+   * "pattern" according to the wildcard expansion rules used
+   * by the shell.
+   * <p>
+   * If no paths match, then this returns an empty list
+   * (note: not an error).
+   * <p>
+   * It is just a wrapper around the C glob(3) function with
+   * flags "GLOB_MARK|GLOB_BRACE". See that manual page for
+   * more details.
+   * <p>
+   * @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;
+
 }
 }
index 631e48f..8a5afe9 100644 (file)
@@ -4040,3 +4040,36 @@ Java_com_redhat_et_libguestfs_GuestFS__1sh_1lines
   return jr;
 }
 
   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;
+}
+
index f102459..fb83545 100644 (file)
@@ -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 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"
index 8983a16..345c7ec 100644 (file)
@@ -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 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 *)
+
index e4dc509..1ce55f0 100644 (file)
@@ -4248,3 +4248,29 @@ ocaml_guestfs_sh_lines (value gv, value commandv)
   CAMLreturn (rv);
 }
 
   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);
+}
+
index 8a6c4bb..5905dc5 100644 (file)
@@ -2583,3 +2583,22 @@ PREINIT:
       }
       free (lines);
 
       }
       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);
+
index 9329b76..436f218 100644 (file)
@@ -676,6 +676,19 @@ For more information on states, see L<guestfs(3)>.
 
 This returns the verbose messages flag.
 
 
 This returns the verbose messages flag.
 
+=item @paths = $h->glob_expand ($pattern);
+
+This command searches for all the pathnames matching
+C<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 L<glob(3)> function
+with flags C<GLOB_MARK|GLOB_BRACE>.
+See that manual page for more details.
+
 =item $h->grub_install ($root, $device);
 
 This command installs GRUB (the Grand Unified Bootloader) on
 =item $h->grub_install ($root, $device);
 
 This command installs GRUB (the Grand Unified Bootloader) on
index f5bc109..60e20d2 100644 (file)
@@ -4506,6 +4506,31 @@ py_guestfs_sh_lines (PyObject *self, PyObject *args)
   return py_r;
 }
 
   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 },
 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 *) "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 }
 };
 
   { NULL, NULL, 0, NULL }
 };
 
index c9658cd..3976610 100644 (file)
@@ -1651,3 +1651,19 @@ class GuestFS:
         """
         return libguestfsmod.sh_lines (self._o, command)
 
         """
         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)
+
index 81e3691..bcb6fef 100644 (file)
@@ -4055,6 +4055,35 @@ static VALUE ruby_guestfs_sh_lines (VALUE gv, VALUE commandv)
   return rv;
 }
 
   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 ()
 {
 /* 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);
         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);
 }
 }
index f36fcf0..e1ec158 100644 (file)
@@ -10263,3 +10263,100 @@ char **guestfs_sh_lines (guestfs_h *g,
   return ctx.ret.lines.lines_val;
 }
 
   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;
+}
+
index fe9967f..19996e8 100644 (file)
@@ -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 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);
index 96951d9..b2d435e 100644 (file)
@@ -1912,6 +1912,27 @@ xdr_guestfs_sh_lines_ret (XDR *xdrs, guestfs_sh_lines_ret *objp)
 }
 
 bool_t
 }
 
 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;
 xdr_guestfs_procedure (XDR *xdrs, guestfs_procedure *objp)
 {
        register int32_t *buf;
index a9863cc..8fdb664 100644 (file)
@@ -969,6 +969,19 @@ struct guestfs_sh_lines_ret {
 };
 typedef struct guestfs_sh_lines_ret 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,
 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_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
 };
 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_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*);
 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_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 ();
 extern bool_t xdr_guestfs_procedure ();
 extern bool_t xdr_guestfs_message_direction ();
 extern bool_t xdr_guestfs_message_status ();
index 94d2e84..d662726 100644 (file)
@@ -748,6 +748,14 @@ struct guestfs_sh_lines_ret {
   str lines<>;
 };
 
   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,
 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_NTFS_3G_PROBE = 110,
   GUESTFS_PROC_SH = 111,
   GUESTFS_PROC_SH_LINES = 112,
+  GUESTFS_PROC_GLOB_EXPAND = 113,
   GUESTFS_PROC_NR_PROCS
 };
 
   GUESTFS_PROC_NR_PROCS
 };