From: Paolo Bonzini Date: Thu, 16 Jul 2009 14:40:08 +0000 (+0200) Subject: add cd and pwd commands to virsh X-Git-Tag: v0.7.0~189 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4951f11b7dac86c211969c3d14a88a1f55e8d31;p=thirdparty%2Flibvirt.git add cd and pwd commands to virsh * 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 --- diff --git a/AUTHORS b/AUTHORS index dd537e6d75..1571ff15db 100644 --- a/AUTHORS +++ b/AUTHORS @@ -77,6 +77,7 @@ Patches have also been contributed by: Amy Griffis Henrik Persson E Satoru SATOH + Paolo Bonzini [....send patches to get your name here....] diff --git a/docs/virsh.pod b/docs/virsh.pod index 6434d78432..9530ba68e5 100644 --- a/docs/virsh.pod +++ b/docs/virsh.pod @@ -82,6 +82,18 @@ Running hypervisor: Xen 3.0.0 =back +=item B I optional + +Will change current directory to I. The default directory +for the B command is the home directory or, if there is no I +variable in the environment, the root directory. + +This command is only available in interactive mode. + +=item B + +Will print the current directory. + =item B I optional I<--readonly> (Re)-Connect to the hypervisor. This is a build-in command after shell diff --git a/src/virsh.c b/src/virsh.c index 5623499b6f..94f2b51182 100644 --- a/src/virsh.c +++ b/src/virsh.c @@ -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 45ea614365..ca03cc177a 100644 --- a/virsh.1 +++ b/virsh.1 @@ -132,7 +132,7 @@ .\" ======================================================================== .\" .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