mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
Use 'error' function for fprintf followed by exit.
Like with the previous commit, this replaces instances of:
if (something_bad) {
fprintf (stderr, "%s: error message\n", guestfs_int_program_name);
exit (EXIT_FAILURE);
}
with:
if (something_bad)
error (EXIT_FAILURE, 0, "error message");
(except in a few cases were errno was incorrectly being ignored, in
which case I have fixed that).
It's slightly more complex than the previous commit because we must be
careful to:
- Remove the program name (since error(3) prints it).
- Remove any trailing \n character from the message.
Candidates for replacement were found using:
pcregrep --buffer-size 10M -M '\bfprintf\b.*\n.*\bexit\b' `git ls-files`
This commit is contained in:
@@ -207,10 +207,8 @@ main (int argc, char *argv[])
|
||||
sigaction (SIGPIPE, &sa, NULL);
|
||||
|
||||
g = guestfs_create ();
|
||||
if (g == NULL) {
|
||||
fprintf (stderr, _("guestfs_create: failed to create handle\n"));
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (g == NULL)
|
||||
error (EXIT_FAILURE, errno, "guestfs_create");
|
||||
|
||||
for (;;) {
|
||||
c = getopt_long (argc, argv, options, long_options, &option_index);
|
||||
@@ -242,17 +240,13 @@ main (int argc, char *argv[])
|
||||
} else if (STREQ (long_options[option_index].name, "no-fork")) {
|
||||
do_fork = 0;
|
||||
} else if (STREQ (long_options[option_index].name, "fd")) {
|
||||
if (sscanf (optarg, "%d", &pipe_fd) != 1 || pipe_fd < 0) {
|
||||
fprintf (stderr, _("%s: unable to parse --fd option value: %s\n"),
|
||||
guestfs_int_program_name, optarg);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
|
||||
guestfs_int_program_name,
|
||||
long_options[option_index].name, option_index);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (sscanf (optarg, "%d", &pipe_fd) != 1 || pipe_fd < 0)
|
||||
error (EXIT_FAILURE, 0,
|
||||
_("unable to parse --fd option value: %s"), optarg);
|
||||
} else
|
||||
error (EXIT_FAILURE, 0,
|
||||
_("unknown long option: %s (%d)"),
|
||||
long_options[option_index].name, option_index);
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
@@ -317,29 +311,18 @@ main (int argc, char *argv[])
|
||||
|
||||
/* Check we have the right options. */
|
||||
if (!live) {
|
||||
if (!drvs || !(mps || inspector)) {
|
||||
fprintf (stderr,
|
||||
_("%s: must have at least one -a/-d and at least one -m/-i option\n"),
|
||||
guestfs_int_program_name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (!drvs || !(mps || inspector))
|
||||
error (EXIT_FAILURE, 0,
|
||||
_("must have at least one -a/-d and at least one -m/-i option"));
|
||||
} else {
|
||||
size_t count_d = 0, count_other = 0;
|
||||
struct drv *drv;
|
||||
|
||||
if (read_only) {
|
||||
fprintf (stderr,
|
||||
_("%s: --live is not compatible with --ro option\n"),
|
||||
guestfs_int_program_name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (read_only)
|
||||
error (EXIT_FAILURE, 0, _("--live is not compatible with --ro option"));
|
||||
|
||||
if (inspector) {
|
||||
fprintf (stderr,
|
||||
_("%s: --live is not compatible with -i option\n"),
|
||||
guestfs_int_program_name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (inspector)
|
||||
error (EXIT_FAILURE, 0, _("--live is not compatible with -i option"));
|
||||
|
||||
/* --live: make sure there was one -d option and no -a options */
|
||||
for (drv = drvs; drv; drv = drv->next) {
|
||||
@@ -349,28 +332,18 @@ main (int argc, char *argv[])
|
||||
count_other++;
|
||||
}
|
||||
|
||||
if (count_d != 1) {
|
||||
fprintf (stderr,
|
||||
_("%s: with --live, you must use exactly one -d option\n"),
|
||||
guestfs_int_program_name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (count_d != 1)
|
||||
error (EXIT_FAILURE, 0,
|
||||
_("with --live, you must use exactly one -d option"));
|
||||
|
||||
if (count_other != 0) {
|
||||
fprintf (stderr,
|
||||
_("%s: --live is not compatible with -a option\n"),
|
||||
guestfs_int_program_name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (count_other != 0)
|
||||
error (EXIT_FAILURE, 0, _("--live is not compatible with -a option"));
|
||||
}
|
||||
|
||||
/* We'd better have a mountpoint. */
|
||||
if (optind+1 != argc) {
|
||||
fprintf (stderr,
|
||||
_("%s: you must specify a mountpoint in the host filesystem\n"),
|
||||
guestfs_int_program_name);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (optind+1 != argc)
|
||||
error (EXIT_FAILURE, 0,
|
||||
_("you must specify a mountpoint in the host filesystem"));
|
||||
|
||||
/* If we're forking, we can't use the recovery process. */
|
||||
if (guestfs_set_recovery_proc (g, !do_fork) == -1)
|
||||
|
||||
Reference in New Issue
Block a user