mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
Use 'error' function consistently throughout.
Wherever we had code which did:
if (something_bad) {
perror (...);
exit (EXIT_FAILURE);
}
replace this with use of the error(3) function:
if (something_bad)
error (EXIT_FAILURE, errno, ...);
The error(3) function is supplied by glibc, or by gnulib on platforms
which don't have it, and is much more flexible than perror(3). Since
we already use error(3), there seems to be no downside to mandating it
everywhere.
Note there is one nasty catch with error(3): error (EXIT_SUCCESS, ...)
does *not* exit! This is also the reason why error(3) cannot be
marked as __attribute__((noreturn)).
Because the examples can't use gnulib, I did not change them.
To search for multiline patterns of the above form, pcregrep -M turns
out to be very useful:
pcregrep --buffer-size 10M -M '\bperror\b.*\n.*\bexit\b' `git ls-files`
This commit is contained in:
@@ -26,6 +26,8 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
@@ -366,10 +368,8 @@ set_qemu (guestfs_h *g, const char *path, int use_wrapper)
|
||||
}
|
||||
|
||||
/* This should be a source directory, so check it. */
|
||||
if (asprintf (&buffer, "%s/pc-bios", path) == -1) {
|
||||
perror ("asprintf");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (asprintf (&buffer, "%s/pc-bios", path) == -1)
|
||||
error (EXIT_FAILURE, errno, "asprintf");
|
||||
if (stat (buffer, &statbuf) == -1 ||
|
||||
!S_ISDIR (statbuf.st_mode)) {
|
||||
fprintf (stderr,
|
||||
@@ -382,10 +382,8 @@ set_qemu (guestfs_h *g, const char *path, int use_wrapper)
|
||||
|
||||
/* Make a wrapper script. */
|
||||
fd = mkstemp (qemuwrapper);
|
||||
if (fd == -1) {
|
||||
perror (qemuwrapper);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (fd == -1)
|
||||
error (EXIT_FAILURE, errno, "mkstemp: %s", qemuwrapper);
|
||||
|
||||
fchmod (fd, 0700);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user