]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
add cd and pwd commands to virsh
authorPaolo Bonzini <pbonzini@redhat.com>
Thu, 16 Jul 2009 14:40:08 +0000 (16:40 +0200)
committerDaniel Veillard <veillard@redhat.com>
Thu, 16 Jul 2009 14:40:08 +0000 (16:40 +0200)
* src/virsh.c: adds cd and pwd commands to virsh useful for save and
  restore commands
* docs/virsh.pod virsh.1: update the documentation
* AUTHORS: add Paolo Bonzini

AUTHORS
docs/virsh.pod
src/virsh.c
virsh.1

diff --git a/AUTHORS b/AUTHORS
index dd537e6d7509d87ae91557fc358593980a1c6d10..1571ff15db2b1ea2109d6f065ec426685cea23e4 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -77,6 +77,7 @@ Patches have also been contributed by:
   Amy Griffis          <amy.griffis@hp.com>
   Henrik Persson E     <henrik.e.persson@ericsson.com>
   Satoru SATOH         <satoru.satoh@gmail.com>
+  Paolo Bonzini        <pbonzini@redhat.com>
 
   [....send patches to get your name here....]
 
index 6434d78432a6745a74cbc16606f229c108409b41..9530ba68e590b07f128166133b4b6e18e94ef642 100644 (file)
@@ -82,6 +82,18 @@ Running hypervisor: Xen 3.0.0
 
 =back
 
+=item B<cd> I<directory> optional
+
+Will change current directory to I<directory>.  The default directory
+for the B<cd> command is the home directory or, if there is no I<HOME>
+variable in the environment, the root directory.
+
+This command is only available in interactive mode.
+
+=item B<pwd>
+
+Will print the current directory.
+
 =item B<connect> I<URI> optional I<--readonly>
 
 (Re)-Connect to the hypervisor. This is a build-in command after shell
index 5623499b6f564d7821a7e39b3fe201349420690c..94f2b51182feb512face2ba6b5d1b60753b48ff1 100644 (file)
@@ -6046,6 +6046,83 @@ editReadBackFile (vshControl *ctl, const char *filename)
     return ret;
 }
 
+/*
+ * "cd" command
+ */
+static const vshCmdInfo info_cd[] = {
+    {"help", gettext_noop("change the current directory")},
+    {"desc", gettext_noop("Change the current directory.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_cd[] = {
+    {"dir", VSH_OT_DATA, 0, gettext_noop("directory to switch to (default: home or else root)")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdCd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+    const char *dir;
+    int found;
+
+    if (!ctl->imode) {
+        vshError(ctl, FALSE, _("cd: command valid only in interactive mode"));
+        return -1;
+    }
+
+    dir = vshCommandOptString(cmd, "dir", &found);
+    if (!found) {
+        uid_t uid = geteuid();
+        dir = virGetUserDirectory(NULL, uid);
+    }
+    if (!dir)
+        dir = "/";
+
+    if (chdir (dir) == -1) {
+        vshError(ctl, FALSE, _("cd: %s: %s"), strerror (errno), dir);
+        return -1;
+    }
+
+    return 0;
+}
+
+/*
+ * "pwd" command
+ */
+static const vshCmdInfo info_pwd[] = {
+    {"help", gettext_noop("print the current directory")},
+    {"desc", gettext_noop("Print the current directory.")},
+    {NULL, NULL}
+};
+
+static int
+cmdPwd(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+    char *cwd;
+    size_t path_max;
+    int err = TRUE;
+
+    path_max = (size_t) PATH_MAX + 2;
+    cwd = vshMalloc (ctl, path_max);
+    while (cwd) {
+        err = getcwd (cwd, path_max) == NULL;
+        if (!err || errno != ERANGE)
+            break;
+
+        path_max *= 2;
+        cwd = vshRealloc (ctl, cwd, path_max);
+    }
+
+    if (err)
+        vshError(ctl, FALSE, _("pwd: cannot get current directory: %s"), strerror (errno));
+    else
+        vshPrint (ctl, _("%s\n"), cwd);
+
+    free (cwd);
+    return !err;
+}
+
 /*
  * "edit" command
  */
@@ -6209,6 +6286,7 @@ static const vshCmdDef commands[] = {
     {"attach-interface", cmdAttachInterface, opts_attach_interface, info_attach_interface},
     {"autostart", cmdAutostart, opts_autostart, info_autostart},
     {"capabilities", cmdCapabilities, NULL, info_capabilities},
+    {"cd", cmdCd, opts_cd, info_cd},
     {"connect", cmdConnect, opts_connect, info_connect},
     {"console", cmdConsole, opts_console, info_console},
     {"create", cmdCreate, opts_create, info_create},
@@ -6277,6 +6355,7 @@ static const vshCmdDef commands[] = {
     {"pool-undefine", cmdPoolUndefine, opts_pool_undefine, info_pool_undefine},
     {"pool-uuid", cmdPoolUuid, opts_pool_uuid, info_pool_uuid},
 
+    {"pwd", cmdPwd, NULL, info_pwd},
     {"quit", cmdQuit, NULL, info_quit},
     {"reboot", cmdReboot, opts_reboot, info_reboot},
     {"restore", cmdRestore, opts_restore, info_restore},
diff --git a/virsh.1 b/virsh.1
index 45ea614365e086607300642a9526d7af23b102e9..ca03cc177a9a831baa0ab16537a45f05ca44290a 100644 (file)
--- a/virsh.1
+++ b/virsh.1
 .\" ========================================================================
 .\"
 .IX Title "VIRSH 1"
-.TH VIRSH 1 "2009-04-16" "libvirt-0.6.2" "Virtualization Support"
+.TH VIRSH 1 "2009-07-02" "libvirt-0.6.4" "Virtualization Support"
 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
 .\" way too many mistakes in technical documents.
 .if n .ad l
@@ -214,6 +214,16 @@ Running hypervisor: Xen 3.0.0
 .RE
 .RS 4
 .RE
+.IP "\fBcd\fR \fIdirectory\fR optional" 4
+.IX Item "cd directory optional"
+Will change current directory to \fIdirectory\fR.  The default directory
+for the \fBcd\fR command is the home directory or, if there is no \fI\s-1HOME\s0\fR
+variable in the environment, the root directory.
+.Sp
+This command is only available in interactive mode.
+.IP "\fBpwd\fR" 4
+.IX Item "pwd"
+Will print the current directory.
 .IP "\fBconnect\fR \fI\s-1URI\s0\fR optional \fI\-\-readonly\fR" 4
 .IX Item "connect URI optional --readonly"
 (Re)\-Connect to the hypervisor. This is a build-in command after shell