From 4211c7a258debd236017a19c70965bc1b3658edb Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Mon, 22 Jun 2009 07:49:50 +0100 Subject: Generated code for 'sh' and 'sh-lines' commands. --- java/com/redhat/et/libguestfs/GuestFS.java | 54 +++++++++++++++++++++++++++++- java/com_redhat_et_libguestfs_GuestFS.c | 54 ++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) (limited to 'java') diff --git a/java/com/redhat/et/libguestfs/GuestFS.java b/java/com/redhat/et/libguestfs/GuestFS.java index d29ae54f..40754204 100644 --- a/java/com/redhat/et/libguestfs/GuestFS.java +++ b/java/com/redhat/et/libguestfs/GuestFS.java @@ -1998,7 +1998,9 @@ public HashMap test0rhashtableerr () * The single parameter is an argv-style list of arguments. * The first element is the name of the program to run. * Subsequent elements are parameters. The list must be - * non-empty (ie. must contain a program name). + * non-empty (ie. must contain a program name). Note that + * the command runs directly, and is *not* invoked via the + * shell (see "g.sh"). *

* The return value is anything printed to *stdout* by the * command. @@ -2040,6 +2042,8 @@ public HashMap test0rhashtableerr () * This is the same as "g.command", but splits the result * into a list of lines. *

+ * See also: "g.sh_lines" + *

* Because of the message protocol, there is a transfer * limit of somewhere between 2MB and 4MB. To transfer * large files you should use FTP. @@ -3383,4 +3387,52 @@ public HashMap test0rhashtableerr () private native int _ntfs_3g_probe (long g, boolean rw, String device) throws LibGuestFSException; + /** + * run a command via the shell + *

+ * This call runs a command from the guest filesystem via + * the guest's "/bin/sh". + *

+ * This is like "g.command", but passes the command to: + *

+ * /bin/sh -c "command" + *

+ * Depending on the guest's shell, this usually results in + * wildcards being expanded, shell expressions being + * interpolated and so on. + *

+ * All the provisos about "g.command" apply to this call. + *

+ * @throws LibGuestFSException + */ + public String sh (String command) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("sh: handle is closed"); + return _sh (g, command); + } + private native String _sh (long g, String command) + throws LibGuestFSException; + + /** + * run a command via the shell returning lines + *

+ * This is the same as "g.sh", but splits the result into a + * list of lines. + *

+ * See also: "g.command_lines" + *

+ * @throws LibGuestFSException + */ + public String[] sh_lines (String command) + throws LibGuestFSException + { + if (g == 0) + throw new LibGuestFSException ("sh_lines: handle is closed"); + return _sh_lines (g, command); + } + private native String[] _sh_lines (long g, String command) + throws LibGuestFSException; + } diff --git a/java/com_redhat_et_libguestfs_GuestFS.c b/java/com_redhat_et_libguestfs_GuestFS.c index 91e3cf19..631e48f6 100644 --- a/java/com_redhat_et_libguestfs_GuestFS.c +++ b/java/com_redhat_et_libguestfs_GuestFS.c @@ -3986,3 +3986,57 @@ Java_com_redhat_et_libguestfs_GuestFS__1ntfs_13g_1probe return (jint) r; } +JNIEXPORT jstring JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1sh + (JNIEnv *env, jobject obj, jlong jg, jstring jcommand) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + jstring jr; + char *r; + const char *command; + + command = (*env)->GetStringUTFChars (env, jcommand, NULL); + r = guestfs_sh (g, command); + (*env)->ReleaseStringUTFChars (env, jcommand, command); + if (r == NULL) { + throw_exception (env, guestfs_last_error (g)); + return NULL; + } + jr = (*env)->NewStringUTF (env, r); + free (r); + return jr; +} + +JNIEXPORT jobjectArray JNICALL +Java_com_redhat_et_libguestfs_GuestFS__1sh_1lines + (JNIEnv *env, jobject obj, jlong jg, jstring jcommand) +{ + guestfs_h *g = (guestfs_h *) (long) jg; + jobjectArray jr; + int r_len; + jclass cl; + jstring jstr; + char **r; + const char *command; + int i; + + command = (*env)->GetStringUTFChars (env, jcommand, NULL); + r = guestfs_sh_lines (g, command); + (*env)->ReleaseStringUTFChars (env, jcommand, command); + 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; +} + -- cgit