X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=fish%2Ffish.c;h=a57472f8c6eb0c5c856f1a1739fac7750c6b1839;hb=5c3c7e8825341e18c9449976f8a321a04cc78d79;hp=22f3359ef0e1ba6c60358f9870c3ab04a0466363;hpb=0daf7e81a659bcdc1ce327151cc1207b124a35a1;p=libguestfs.git diff --git a/fish/fish.c b/fish/fish.c index 22f3359..a57472f 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -117,7 +117,7 @@ usage (int status) " --keys-from-stdin Read passphrases from stdin\n" " --listen Listen for remote commands\n" " --live Connect to a live virtual machine\n" - " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n" + " -m|--mount dev[:mnt[:opts]] Mount dev on mnt (if omitted, /)\n" " -n|--no-sync Don't autosync\n" " -N|--new type Create prepared disk (test1.img, ...)\n" " --progress-bars Enable progress bars even when not interactive\n" @@ -158,6 +158,8 @@ main (int argc, char *argv[]) bindtextdomain (PACKAGE, LOCALEBASEDIR); textdomain (PACKAGE); + parse_config (); + set_up_terminal (); enum { HELP_OPTION = CHAR_MAX + 1 }; @@ -218,19 +220,6 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } - /* If developing, add ./appliance to the path. Note that libtools - * interferes with this because uninstalled guestfish is a shell - * script that runs the real program with an absolute path. Detect - * that too. - * - * BUT if LIBGUESTFS_PATH environment variable is already set by - * the user, then don't override it. - */ - if (getenv ("LIBGUESTFS_PATH") == NULL && - argv[0] && - (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL)) - guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH); - /* CAUTION: we are careful to modify argv[0] here, only after * using it just above. * @@ -1373,16 +1362,21 @@ xwrite (int fd, const void *v_buf, size_t len) return 0; } -/* Resolve the special "win:..." form for Windows-specific paths. - * This always returns a newly allocated string which is freed by the - * caller function in "cmds.c". +/* Resolve the special "win:..." form for Windows-specific paths. The + * generated code calls this for all device or path arguments. + * + * The function returns a newly allocated string, and the caller must + * free this string; else display an error and return NULL. */ +static char *win_prefix_drive_letter (char drive_letter, const char *path); + char * -resolve_win_path (const char *path) +win_prefix (const char *path) { char *ret; size_t i; + /* If there is not a "win:..." prefix on the path, return strdup'd string. */ if (STRCASENEQLEN (path, "win:", 4)) { ret = strdup (path); if (ret == NULL) @@ -1392,21 +1386,27 @@ resolve_win_path (const char *path) path += 4; - /* Drop drive letter, if it's "C:". */ - if (STRCASEEQLEN (path, "c:", 2)) - path += 2; - - if (!*path) { - ret = strdup ("/"); + /* If there is a drive letter, rewrite the path. */ + if (c_isalpha (path[0]) && path[1] == ':') { + char drive_letter = c_tolower (path[0]); + /* This returns the newly allocated string. */ + ret = win_prefix_drive_letter (drive_letter, path + 2); if (ret == NULL) + return NULL; + } + else if (!*path) { + ret = strdup ("/"); + if (ret == NULL) { perror ("strdup"); - return ret; + return NULL; + } } - - ret = strdup (path); - if (ret == NULL) { - perror ("strdup"); - return NULL; + else { + ret = strdup (path); + if (ret == NULL) { + perror ("strdup"); + return NULL; + } } /* Blindly convert any backslashes into forward slashes. Is this good? */ @@ -1421,6 +1421,82 @@ resolve_win_path (const char *path) return ret; } +static char * +win_prefix_drive_letter (char drive_letter, const char *path) +{ + char **roots = NULL; + char **drives = NULL; + char **mountpoints = NULL; + char *device, *mountpoint, *ret = NULL; + size_t i; + + /* Resolve the drive letter using the drive mappings table. */ + roots = guestfs_inspect_get_roots (g); + if (roots == NULL) + goto out; + if (roots[0] == NULL) { + fprintf (stderr, _("%s: to use Windows drive letters, you must inspect the guest (\"-i\" option or run \"inspect-os\" command)\n"), + program_name); + goto out; + } + drives = guestfs_inspect_get_drive_mappings (g, roots[0]); + if (drives == NULL || drives[0] == NULL) { + fprintf (stderr, _("%s: to use Windows drive letters, this must be a Windows guest\n"), + program_name); + goto out; + } + + device = NULL; + for (i = 0; drives[i] != NULL; i += 2) { + if (c_tolower (drives[i][0]) == drive_letter && drives[i][1] == '\0') { + device = drives[i+1]; + break; + } + } + + if (device == NULL) { + fprintf (stderr, _("%s: drive '%c:' not found. To list available drives do:\n inspect-get-drive-mappings %s\n"), + program_name, drive_letter, roots[0]); + goto out; + } + + /* This drive letter must be mounted somewhere (we won't do it). */ + mountpoints = guestfs_mountpoints (g); + if (mountpoints == NULL) + goto out; + + mountpoint = NULL; + for (i = 0; mountpoints[i] != NULL; i += 2) { + if (STREQ (mountpoints[i], device)) { + mountpoint = mountpoints[i+1]; + break; + } + } + + if (mountpoint == NULL) { + fprintf (stderr, _("%s: to access '%c:', mount %s first. One way to do this is:\n umount-all\n mount %s /\n"), + program_name, drive_letter, device, device); + goto out; + } + + /* Rewrite the path, eg. if C: => /c then C:/foo => /c/foo */ + if (asprintf (&ret, "%s%s%s", + mountpoint, STRNEQ (mountpoint, "/") ? "/" : "", path) == -1) { + perror ("asprintf"); + goto out; + } + + out: + if (roots) + free_strings (roots); + if (drives) + free_strings (drives); + if (mountpoints) + free_strings (mountpoints); + + return ret; +} + /* Resolve the special FileIn paths ("-" or "-<