inspect: Generic parsing of MAJOR.MINOR in product names.
[libguestfs.git] / fish / options.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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "guestfs.h"
25
26 #include "options.h"
27
28 char
29 add_drives (struct drv *drv, char next_drive)
30 {
31   int r;
32   struct guestfs_add_drive_opts_argv ad_optargs;
33
34   if (next_drive > 'z') {
35     fprintf (stderr,
36              _("%s: too many drives added on the command line\n"),
37              program_name);
38     exit (EXIT_FAILURE);
39   }
40
41   if (drv) {
42     next_drive = add_drives (drv->next, next_drive);
43
44     switch (drv->type) {
45     case drv_a:
46       ad_optargs.bitmask = 0;
47       if (read_only) {
48         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
49         ad_optargs.readonly = 1;
50       }
51       if (drv->a.format) {
52         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
53         ad_optargs.format = drv->a.format;
54       }
55       r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
56       if (r == -1)
57         exit (EXIT_FAILURE);
58
59       next_drive++;
60       break;
61
62     case drv_d:
63       r = add_libvirt_drives (drv->d.guest);
64       if (r == -1)
65         exit (EXIT_FAILURE);
66
67       next_drive += r;
68       break;
69
70     case drv_N:
71       /* guestfs_add_drive (ie. autodetecting) should be safe here
72        * since we have just created the prepared disk.  At the moment
73        * it will always be "raw" but in a theoretical future we might
74        * create other formats.
75        */
76       /* -N option is not affected by --ro */
77       r = guestfs_add_drive (g, drv->N.filename);
78       if (r == -1)
79         exit (EXIT_FAILURE);
80
81       if (asprintf (&drv->N.device, "/dev/sd%c", next_drive) == -1) {
82         perror ("asprintf");
83         exit (EXIT_FAILURE);
84       }
85
86       next_drive++;
87       break;
88
89     default: /* keep GCC happy */
90       abort ();
91     }
92   }
93
94   return next_drive;
95 }
96
97 /* List is built in reverse order, so mount them in reverse order. */
98 void
99 mount_mps (struct mp *mp)
100 {
101   int r;
102
103   if (mp) {
104     mount_mps (mp->next);
105
106     /* Don't use guestfs_mount here because that will default to mount
107      * options -o sync,noatime.  For more information, see guestfs(3)
108      * section "LIBGUESTFS GOTCHAS".
109      */
110     const char *options = read_only ? "ro" : "";
111     r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
112     if (r == -1) {
113       /* Display possible mountpoints before exiting. */
114       char **fses = guestfs_list_filesystems (g);
115       if (fses == NULL || fses[0] == NULL)
116         goto out;
117       fprintf (stderr,
118                _("%s: '%s' could not be mounted.  Did you mean one of these?\n"),
119                program_name, mp->device);
120       size_t i;
121       for (i = 0; fses[i] != NULL; i += 2)
122         fprintf (stderr, "\t%s (%s)\n", fses[i], fses[i+1]);
123
124     out:
125       exit (EXIT_FAILURE);
126     }
127   }
128 }
129
130 void
131 free_drives (struct drv *drv)
132 {
133   if (!drv) return;
134   free_drives (drv->next);
135
136   switch (drv->type) {
137   case drv_a: /* a.filename and a.format are optargs, don't free them */ break;
138   case drv_d: /* d.filename is optarg, don't free it */ break;
139   case drv_N:
140     free (drv->N.filename);
141     free (drv->N.device);
142     drv->N.data_free (drv->N.data);
143     break;
144   default: ;                    /* keep GCC happy */
145   }
146   free (drv);
147 }
148
149 void
150 free_mps (struct mp *mp)
151 {
152   if (!mp) return;
153   free_mps (mp->next);
154
155   /* The drive and mountpoint fields are not allocated
156    * from the heap, so we should not free them here.
157    */
158
159   free (mp);
160 }