fish: Allow events to be processed in guestfish.
[libguestfs.git] / fish / reopen.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 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., 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 #include <string.h>
24 #include <unistd.h>
25
26 #include "fish.h"
27
28 int
29 run_reopen (const char *cmd, size_t argc, char *argv[])
30 {
31   guestfs_h *g2;
32   int r;
33   const char *p;
34   guestfs_error_handler_cb cb;
35   void *cb_data;
36
37   if (argc > 0) {
38     fprintf (stderr, _("'reopen' command takes no parameters\n"));
39     return -1;
40   }
41
42   /* Open the new handle first, so we can copy the settings from the
43    * old one to the new one, and also so if it fails we still have an
44    * open handle.
45    */
46   g2 = guestfs_create ();
47   if (g2 == NULL) {
48     fprintf (stderr, _("reopen: guestfs_create: failed to create handle\n"));
49     return -1;
50   }
51
52   /* Now copy some of the settings from the old handle.  The settings
53    * we copy are those which are set by guestfish itself.
54    */
55   cb = guestfs_get_error_handler (g, &cb_data);
56   guestfs_set_error_handler (g2, cb, cb_data);
57
58   r = guestfs_get_verbose (g);
59   if (r >= 0)
60     guestfs_set_verbose (g2, r);
61
62   r = guestfs_get_trace (g);
63   if (r >= 0)
64     guestfs_set_trace (g2, r);
65
66   r = guestfs_get_autosync (g);
67   if (r >= 0)
68     guestfs_set_autosync (g2, r);
69
70   p = guestfs_get_path (g);
71   if (p)
72     guestfs_set_path (g2, p);
73
74   r = guestfs_get_pgroup (g);
75   if (r >= 0)
76     guestfs_set_pgroup (g2, r);
77
78   if (progress_bars)
79     guestfs_set_event_callback (g2, progress_callback,
80                                 GUESTFS_EVENT_PROGRESS, 0, NULL);
81
82   /* Close the original handle. */
83   guestfs_close (g);
84   g = g2;
85
86   /* We don't bother copying event handlers over to the new handle,
87    * but we have to reset the list because they were registered
88    * against the old handle.
89    */
90   free_event_handlers ();
91   init_event_handlers ();
92
93   return 0;
94 }