regressions: Enable both tests for bug 576879 (not fixed).
[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     if (asprintf (&drv->device, "/dev/sd%c", next_drive) == -1) {
45       perror ("asprintf");
46       exit (EXIT_FAILURE);
47     }
48
49     switch (drv->type) {
50     case drv_a:
51       ad_optargs.bitmask = 0;
52       if (read_only) {
53         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
54         ad_optargs.readonly = 1;
55       }
56       if (drv->a.format) {
57         ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
58         ad_optargs.format = drv->a.format;
59       }
60       r = guestfs_add_drive_opts_argv (g, drv->a.filename, &ad_optargs);
61       if (r == -1)
62         exit (EXIT_FAILURE);
63
64       drv->nr_drives = 1;
65       next_drive++;
66       break;
67
68     case drv_d:
69       r = add_libvirt_drives (drv->d.guest);
70       if (r == -1)
71         exit (EXIT_FAILURE);
72
73       drv->nr_drives = r;
74       next_drive += r;
75       break;
76
77     case drv_N:
78       /* guestfs_add_drive (ie. autodetecting) should be safe here
79        * since we have just created the prepared disk.  At the moment
80        * it will always be "raw" but in a theoretical future we might
81        * create other formats.
82        */
83       /* -N option is not affected by --ro */
84       r = guestfs_add_drive (g, drv->N.filename);
85       if (r == -1)
86         exit (EXIT_FAILURE);
87
88       drv->nr_drives = 1;
89       next_drive++;
90       break;
91
92     default: /* keep GCC happy */
93       abort ();
94     }
95   }
96
97   return next_drive;
98 }
99
100 /* List is built in reverse order, so mount them in reverse order. */
101 void
102 mount_mps (struct mp *mp)
103 {
104   int r;
105
106   if (mp) {
107     mount_mps (mp->next);
108
109     /* Don't use guestfs_mount here because that will default to mount
110      * options -o sync,noatime.  For more information, see guestfs(3)
111      * section "LIBGUESTFS GOTCHAS".
112      */
113     const char *options = read_only ? "ro" : "";
114     r = guestfs_mount_options (g, options, mp->device, mp->mountpoint);
115     if (r == -1) {
116       /* Display possible mountpoints before exiting. */
117       char **fses = guestfs_list_filesystems (g);
118       if (fses == NULL || fses[0] == NULL)
119         goto out;
120       fprintf (stderr,
121                _("%s: '%s' could not be mounted.  Did you mean one of these?\n"),
122                program_name, mp->device);
123       size_t i;
124       for (i = 0; fses[i] != NULL; i += 2)
125         fprintf (stderr, "\t%s (%s)\n", fses[i], fses[i+1]);
126
127     out:
128       exit (EXIT_FAILURE);
129     }
130   }
131 }
132
133 void
134 free_drives (struct drv *drv)
135 {
136   if (!drv) return;
137   free_drives (drv->next);
138
139   free (drv->device);
140
141   switch (drv->type) {
142   case drv_a: /* a.filename and a.format are optargs, don't free them */ break;
143   case drv_d: /* d.filename is optarg, don't free it */ break;
144   case drv_N:
145     free (drv->N.filename);
146     drv->N.data_free (drv->N.data);
147     break;
148   default: ;                    /* keep GCC happy */
149   }
150   free (drv);
151 }
152
153 void
154 free_mps (struct mp *mp)
155 {
156   if (!mp) return;
157   free_mps (mp->next);
158
159   /* The drive and mountpoint fields are not allocated
160    * from the heap, so we should not free them here.
161    */
162
163   free (mp);
164 }