Version 1.7.1.
[libguestfs.git] / fish / inspect.c
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "guestfs.h"
26
27 #include "options.h"
28
29 /* Global that saves the root device between inspect_mount and
30  * print_inspect_prompt.
31  */
32 static char *root = NULL;
33
34 static void
35 free_strings (char **argv)
36 {
37   int argc;
38
39   for (argc = 0; argv[argc] != NULL; ++argc)
40     free (argv[argc]);
41   free (argv);
42 }
43
44 static int
45 count_strings (char *const *argv)
46 {
47   int c;
48
49   for (c = 0; argv[c]; ++c)
50     ;
51   return c;
52 }
53
54 static int
55 compare_keys_len (const void *p1, const void *p2)
56 {
57   const char *key1 = * (char * const *) p1;
58   const char *key2 = * (char * const *) p2;
59   return strlen (key1) - strlen (key2);
60 }
61
62 static int
63 compare_keys (const void *p1, const void *p2)
64 {
65   const char *key1 = * (char * const *) p1;
66   const char *key2 = * (char * const *) p2;
67   return strcasecmp (key1, key2);
68 }
69
70 /* This function implements the -i option. */
71 void
72 inspect_mount (void)
73 {
74   char **roots = guestfs_inspect_os (g);
75   if (roots == NULL)
76     exit (EXIT_FAILURE);
77
78   if (roots[0] == NULL) {
79     fprintf (stderr, _("%s: no operating system was found on this disk\n"),
80              program_name);
81     exit (EXIT_FAILURE);
82   }
83
84   if (roots[1] != NULL) {
85     fprintf (stderr, _("%s: multi-boot operating systems are not supported by the -i option\n"),
86              program_name);
87     exit (EXIT_FAILURE);
88   }
89
90   root = roots[0];
91   free (roots);
92
93   char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
94   if (mountpoints == NULL)
95     exit (EXIT_FAILURE);
96
97   /* Sort by key length, shortest key first, so that we end up
98    * mounting the filesystems in the correct order.
99    */
100   qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
101          compare_keys_len);
102
103   size_t i;
104   for (i = 0; mountpoints[i] != NULL; i += 2) {
105     int r;
106     if (!read_only)
107       r = guestfs_mount_options (g, "", mountpoints[i+1], mountpoints[i]);
108     else
109       r = guestfs_mount_ro (g, mountpoints[i+1], mountpoints[i]);
110     if (r == -1)
111       exit (EXIT_FAILURE);
112   }
113
114   free_strings (mountpoints);
115 }
116
117 /* This function is called only if the above function was called,
118  * and only after we've printed the prompt in interactive mode.
119  */
120 void
121 print_inspect_prompt (void)
122 {
123   char *name = guestfs_inspect_get_product_name (g, root);
124   if (STRNEQ (name, "unknown"))
125     printf (_("Operating system: %s\n"), name);
126   free (name);
127
128   char **mountpoints = guestfs_inspect_get_mountpoints (g, root);
129   if (mountpoints == NULL)
130     return;
131
132   /* Sort by key. */
133   qsort (mountpoints, count_strings (mountpoints) / 2, 2 * sizeof (char *),
134          compare_keys);
135
136   size_t i;
137   for (i = 0; mountpoints[i] != NULL; i += 2)
138     printf (_("%s mounted on %s\n"), mountpoints[i+1], mountpoints[i]);
139
140   free_strings (mountpoints);
141 }