tools: Modify existing tools to use guestfs_{push,pop}_error_handler.

This is a shorter and more convenient way to disable errors
temporarily across calls.
This commit is contained in:
Richard W.M. Jones
2012-11-09 18:03:49 +01:00
parent b460d9f32e
commit 2d220f5da2
13 changed files with 81 additions and 158 deletions

View File

@@ -36,15 +36,6 @@
#include "guestfs.h"
#include "options.h"
#define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
guestfs_error_handler_cb old_error_cb; \
void *old_error_data; \
old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
guestfs_set_error_handler (g, NULL, NULL); \
stmt; \
guestfs_set_error_handler (g, old_error_cb, old_error_data); \
} while (0)
/* These globals are shared with options.c. */
guestfs_h *g;
@@ -472,9 +463,9 @@ do_output_filesystems (void)
* otherwise pass them as NULL.
*/
if ((columns & COLUMN_VFS_LABEL)) {
DISABLE_GUESTFS_ERRORS_FOR (
vfs_label = guestfs_vfs_label (g, fses[i]);
);
guestfs_push_error_handler (g, NULL, NULL);
vfs_label = guestfs_vfs_label (g, fses[i]);
guestfs_pop_error_handler (g);
if (vfs_label == NULL) {
vfs_label = strdup ("");
if (!vfs_label) {
@@ -484,9 +475,9 @@ do_output_filesystems (void)
}
}
if ((columns & COLUMN_UUID)) {
DISABLE_GUESTFS_ERRORS_FOR (
vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
);
guestfs_push_error_handler (g, NULL, NULL);
vfs_uuid = guestfs_vfs_uuid (g, fses[i]);
guestfs_pop_error_handler (g);
if (vfs_uuid == NULL) {
vfs_uuid = strdup ("");
if (!vfs_uuid) {
@@ -662,23 +653,20 @@ get_mbr_id (const char *dev, const char *parent_name)
char *parttype = NULL;
int mbr_id = -1, partnum;
DISABLE_GUESTFS_ERRORS_FOR (
parttype = guestfs_part_get_parttype (g, parent_name);
);
guestfs_push_error_handler (g, NULL, NULL);
parttype = guestfs_part_get_parttype (g, parent_name);
if (parttype && STREQ (parttype, "msdos")) {
DISABLE_GUESTFS_ERRORS_FOR (
partnum = guestfs_part_to_partnum (g, dev);
);
if (partnum >= 0) {
DISABLE_GUESTFS_ERRORS_FOR (
mbr_id = guestfs_part_get_mbr_id (g, parent_name, partnum);
);
}
partnum = guestfs_part_to_partnum (g, dev);
if (partnum >= 0)
mbr_id = guestfs_part_get_mbr_id (g, parent_name, partnum);
}
free (parttype);
guestfs_pop_error_handler (g);
return mbr_id;
}

14
df/df.c
View File

@@ -122,8 +122,6 @@ df_on_handle (const char *name, const char *uuid, char **devices, int offset)
static int
find_dev_in_devices (const char *dev, char **devices)
{
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
size_t i, len;
char *whole_disk;
int free_whole_disk;
@@ -132,12 +130,11 @@ find_dev_in_devices (const char *dev, char **devices)
/* Convert 'dev' to a whole disk name. */
len = strlen (dev);
if (len > 0 && c_isdigit (dev[len-1])) {
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
whole_disk = guestfs_part_to_dev (g, dev);
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
if (!whole_disk) /* probably an MD device or similar */
return 0;
@@ -167,8 +164,6 @@ try_df (const char *name, const char *uuid,
const char *dev, int offset)
{
struct guestfs_statvfs *stat = NULL;
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
if (verbose)
fprintf (stderr, "try_df %s %s %d\n", name, dev, offset);
@@ -176,15 +171,14 @@ try_df (const char *name, const char *uuid,
/* Try mounting and stating the device. This might reasonably fail,
* so don't show errors.
*/
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
if (guestfs_mount_ro (g, dev, "/") == 0) {
stat = guestfs_statvfs (g, "/");
guestfs_umount_all (g);
}
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
if (stat) {
print_stat (name, uuid, dev, offset, stat);

View File

@@ -536,16 +536,13 @@ copy_attributes (const char *src, const char *dest)
* attributes too?
*/
if (has_linuxxattrs) {
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
selinux_context = guestfs_getxattr (g, src, "security.selinux",
&selinux_context_size);
/* selinux_context could be NULL. This isn't an error. */
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
}
/* Set the permissions (inc. sticky and set*id bits), UID, GID. */
@@ -578,15 +575,12 @@ feature_available (guestfs_h *g, const char *feature)
/* If there's an error we should ignore it, so to do that we have to
* temporarily replace the error handler with a null one.
*/
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
const char *groups[] = { feature, NULL };
int r = guestfs_available (g, (char * const *) groups);
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
return r == 0 ? 1 : 0;
}

View File

@@ -45,8 +45,6 @@ main (int argc, char *argv[])
char tempdir[] = "/tmp/mlXXXXXX";
pid_t pid;
char *shell, *p;
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
if (argc != 2) {
usage ();
@@ -175,14 +173,13 @@ main (int argc, char *argv[])
/* We're going to hide libguestfs errors here, but in a real program
* you would probably want to log them somewhere.
*/
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
/* Now run the FUSE thread. */
if (guestfs_mount_local_run (g) == -1)
exit (EXIT_FAILURE);
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
waitpid (pid, NULL, 0);

View File

@@ -89,19 +89,6 @@ complete_dest_paths_generator (const char *text, int state)
static size_t len, index;
static struct word *words = NULL;
static size_t nr_words = 0;
guestfs_error_handler_cb old_error_cb;
void *old_error_cb_data;
/* Temporarily replace the error handler so that messages don't
* get printed to stderr while we are issuing commands.
*/
#define SAVE_ERROR_CB \
old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data); \
guestfs_set_error_handler (g, NULL, NULL);
/* Restore error handler. */
#define RESTORE_ERROR_CB \
guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
if (!state) {
char **strs;
@@ -114,7 +101,7 @@ complete_dest_paths_generator (const char *text, int state)
words = NULL;
nr_words = 0;
SAVE_ERROR_CB
guestfs_push_error_handler (g, NULL, NULL);
/* Silently do nothing if an allocation fails */
#define APPEND_STRS_AND_FREE \
@@ -220,7 +207,7 @@ complete_dest_paths_generator (const char *text, int state)
* names. At the moment we don't do that.
*/
RESTORE_ERROR_CB
guestfs_pop_error_handler (g);
}
/* This inhibits ordinary (local filename) completion. */

View File

@@ -206,16 +206,13 @@ copy_attributes (const char *src, const char *dest)
* attributes too?
*/
if (has_linuxxattrs) {
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
selinux_context = guestfs_getxattr (g, src, "security.selinux",
&selinux_context_size);
/* selinux_context could be NULL. This isn't an error. */
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
}
/* Set the permissions (inc. sticky and set*id bits), UID, GID. */

View File

@@ -1932,15 +1932,12 @@ feature_available (guestfs_h *g, const char *feature)
/* If there's an error we should ignore it, so to do that we have to
* temporarily replace the error handler with a null one.
*/
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
const char *groups[] = { feature, NULL };
int r = guestfs_available (g, (char * const *) groups);
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
return r == 0 ? 1 : 0;
}

View File

@@ -39,10 +39,7 @@ run_supported (const char *cmd, size_t argc, char *argv[])
/* Temporarily replace the error handler so that messages don't get
* printed to stderr while we are issuing commands.
*/
guestfs_error_handler_cb old_error_cb;
void *old_error_cb_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
/* Work out the max string length of any group name. */
size_t i;
@@ -76,7 +73,7 @@ run_supported (const char *cmd, size_t argc, char *argv[])
free (groups);
/* Restore error handler. */
guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
guestfs_pop_error_handler (g);
return 0;
}

View File

@@ -430,11 +430,8 @@ do_rescan (char **devices)
{
size_t i;
size_t errors = 0;
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
for (i = 0; devices[i] != NULL; ++i) {
if (guestfs_blockdev_rereadpt (g, devices[i]) == -1)
@@ -444,7 +441,7 @@ do_rescan (char **devices)
if (guestfs_vgscan (g) == -1)
errors++;
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
return errors ? 1 : 0;
}
@@ -455,15 +452,12 @@ feature_available (guestfs_h *g, const char *feature)
/* If there's an error we should ignore it, so to do that we have to
* temporarily replace the error handler with a null one.
*/
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
const char *groups[] = { feature, NULL };
int r = guestfs_available (g, (char * const *) groups);
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
return r == 0 ? 1 : 0;
}

View File

@@ -293,15 +293,6 @@ main (int argc, char *argv[])
exit (EXIT_SUCCESS);
}
#define DISABLE_GUESTFS_ERRORS_FOR(stmt) do { \
guestfs_error_handler_cb old_error_cb; \
void *old_error_data; \
old_error_cb = guestfs_get_error_handler (g, &old_error_data); \
guestfs_set_error_handler (g, NULL, NULL); \
stmt; \
guestfs_set_error_handler (g, old_error_cb, old_error_data); \
} while (0)
#define XMLERROR(code,e) do { \
if ((e) == (code)) { \
fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \
@@ -433,22 +424,20 @@ output_root (xmlTextWriterPtr xo, char *root)
* or if the systemroot could not be determined for a windows guest.
* Disable error output around this call.
*/
DISABLE_GUESTFS_ERRORS_FOR (
str = guestfs_inspect_get_windows_systemroot (g, root);
if (str)
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
BAD_CAST str));
free (str);
);
DISABLE_GUESTFS_ERRORS_FOR (
str = guestfs_inspect_get_windows_current_control_set (g, root);
if (str)
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
BAD_CAST str));
free (str);
);
guestfs_push_error_handler (g, NULL, NULL);
str = guestfs_inspect_get_windows_systemroot (g, root);
if (str)
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "windows_systemroot",
BAD_CAST str));
free (str);
str = guestfs_inspect_get_windows_current_control_set (g, root);
if (str)
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "windows_current_control_set",
BAD_CAST str));
free (str);
guestfs_pop_error_handler (g);
str = guestfs_inspect_get_hostname (g, root);
if (!str) exit (EXIT_FAILURE);
@@ -620,32 +609,30 @@ output_filesystems (xmlTextWriterPtr xo, char *root)
xmlTextWriterWriteAttribute (xo, BAD_CAST "dev", BAD_CAST str));
free (str);
DISABLE_GUESTFS_ERRORS_FOR (
str = guestfs_vfs_type (g, filesystems[i]);
if (str && str[0])
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "type",
BAD_CAST str));
free (str);
);
guestfs_push_error_handler (g, NULL, NULL);
DISABLE_GUESTFS_ERRORS_FOR (
str = guestfs_vfs_label (g, filesystems[i]);
if (str && str[0])
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "label",
BAD_CAST str));
free (str);
);
str = guestfs_vfs_type (g, filesystems[i]);
if (str && str[0])
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "type",
BAD_CAST str));
free (str);
DISABLE_GUESTFS_ERRORS_FOR (
str = guestfs_vfs_uuid (g, filesystems[i]);
if (str && str[0])
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
BAD_CAST str));
free (str);
);
str = guestfs_vfs_label (g, filesystems[i]);
if (str && str[0])
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "label",
BAD_CAST str));
free (str);
str = guestfs_vfs_uuid (g, filesystems[i]);
if (str && str[0])
XMLERROR (-1,
xmlTextWriterWriteElement (xo, BAD_CAST "uuid",
BAD_CAST str));
free (str);
guestfs_pop_error_handler (g);
XMLERROR (-1, xmlTextWriterEndElement (xo));
}
@@ -662,9 +649,9 @@ output_drive_mappings (xmlTextWriterPtr xo, char *root)
char *str;
size_t i;
DISABLE_GUESTFS_ERRORS_FOR (
drive_mappings = guestfs_inspect_get_drive_mappings (g, root);
);
guestfs_push_error_handler (g, NULL, NULL);
drive_mappings = guestfs_inspect_get_drive_mappings (g, root);
guestfs_pop_error_handler (g);
if (drive_mappings == NULL)
return;

View File

@@ -448,15 +448,12 @@ feature_available (guestfs_h *g, const char *feature)
/* If there's an error we should ignore it, so to do that we have to
* temporarily replace the error handler with a null one.
*/
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
const char *groups[] = { feature, NULL };
int r = guestfs_available (g, (char * const *) groups);
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
return r == 0 ? 1 : 0;
}

View File

@@ -204,8 +204,6 @@ start_thread (void *statevp)
time_t start_t, t;
pid_t pid;
int status, r;
guestfs_error_handler_cb old_error_cb;
void *old_error_cb_data;
g = guestfs_create ();
if (g == NULL) {
@@ -264,10 +262,9 @@ start_thread (void *statevp)
/* Run the FUSE main loop. We don't really want to see libguestfs
* errors here since these are harmless.
*/
old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
r = guestfs_mount_local_run (g);
guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
guestfs_pop_error_handler (g);
/* Wait for child process to exit and catch any errors from it. */
again:

View File

@@ -121,8 +121,6 @@ start_thread (void *vi)
{
guestfs_h *g;
int r, thread_id = *(int *)vi;
guestfs_error_handler_cb old_error_cb;
void *old_error_data;
const char *error;
g = guestfs_create ();
@@ -159,8 +157,7 @@ start_thread (void *vi)
* will fail with "child process died unexpectedly". We are
* interested in other failures.
*/
old_error_cb = guestfs_get_error_handler (g, &old_error_data);
guestfs_set_error_handler (g, NULL, NULL);
guestfs_push_error_handler (g, NULL, NULL);
r = guestfs_launch (g);
error = guestfs_last_error (g);
@@ -190,7 +187,7 @@ start_thread (void *vi)
pthread_exit (vi);
}
guestfs_set_error_handler (g, old_error_cb, old_error_data);
guestfs_pop_error_handler (g);
/* Close the handle. */
guestfs_close (g);