X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=fish%2Ffish.c;h=a57472f8c6eb0c5c856f1a1739fac7750c6b1839;hb=470e373eea218c8d46b0c63dda93ee9ece48940f;hp=1419c895355e7a5172970b470336d63d50e4b350;hpb=b8be128caa27fa5e1636e9e4caff3e23a6dc761f;p=libguestfs.git diff --git a/fish/fish.c b/fish/fish.c index 1419c89..a57472f 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -220,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. * @@ -1376,10 +1363,13 @@ xwrite (int fd, const void *v_buf, size_t len) } /* Resolve the special "win:..." form for Windows-specific paths. The - * generated code calls this for all device or path arguments. The - * function must return a newly allocated string (caller frees) or - * display an error and return NULL. + * 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 * win_prefix (const char *path) { @@ -1396,97 +1386,113 @@ win_prefix (const char *path) path += 4; - /* Is there a drive letter? */ + /* If there is a drive letter, rewrite the path. */ if (c_isalpha (path[0]) && path[1] == ':') { - char drive_letter; - char **roots, **drives, **mountpoints, *device; - size_t i; - - drive_letter = c_tolower (path[0]); - path += 2; - - /* Resolve the drive letter using the drive mappings table. */ - roots = guestfs_inspect_get_roots (g); - if (roots == NULL) + 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; - 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); - free_strings (roots); + } + else if (!*path) { + ret = strdup ("/"); + if (ret == NULL) { + perror ("strdup"); return NULL; } - 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); - free_strings (roots); - free_strings (drives); + } + else { + ret = strdup (path); + if (ret == NULL) { + perror ("strdup"); return NULL; } + } - 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; - } - } + /* Blindly convert any backslashes into forward slashes. Is this good? */ + for (i = 0; i < strlen (ret); ++i) + if (ret[i] == '\\') + ret[i] = '/'; - 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]); - free_strings (roots); - free_strings (drives); - return NULL; - } + char *t = guestfs_case_sensitive_path (g, ret); + free (ret); + ret = t; - /* This drive letter must be mounted on / (we won't do it). */ - mountpoints = guestfs_mountpoints (g); - if (mountpoints == NULL) { - free_strings (roots); - free_strings (drives); - return NULL; - } + return ret; +} - for (i = 0; mountpoints[i] != NULL; i += 2) { - if (STREQ (mountpoints[i+1], "/")) { - if (STRNEQ (mountpoints[i], device)) { - fprintf (stderr, _("%s: to access '%c:', mount %s on / first. One way to do this is:\n umount-all\n mount %s /\n"), - program_name, drive_letter, device, device); - free_strings (roots); - free_strings (drives); - free_strings (mountpoints); - return NULL; - } - } +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; } + } - free_strings (roots); - free_strings (drives); - free_strings (mountpoints); + 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; } - if (!*path) { - ret = strdup ("/"); - if (ret == NULL) - perror ("strdup"); - return ret; + /* 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; + } } - ret = strdup (path); - if (ret == NULL) { - perror ("strdup"); - return NULL; + 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; } - /* Blindly convert any backslashes into forward slashes. Is this good? */ - for (i = 0; i < strlen (ret); ++i) - if (ret[i] == '\\') - ret[i] = '/'; + /* 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; + } - char *t = guestfs_case_sensitive_path (g, ret); - free (ret); - ret = t; + out: + if (roots) + free_strings (roots); + if (drives) + free_strings (drives); + if (mountpoints) + free_strings (mountpoints); return ret; }