Files
libguestfs/fish/prep-fs.c
Richard W.M. Jones 129e4938ba 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`
2016-04-04 13:14:26 +01:00

55 lines
1.7 KiB
C

/* guestfish - guest filesystem shell
* Copyright (C) 2010-2016 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <libintl.h>
#include "fish.h"
#include "prepopts.h"
void
prep_prelaunch_fs (const char *filename, prep_data *data)
{
if (alloc_disk (filename, data->params[1], 0, 1) == -1)
prep_error (data, filename, _("failed to allocate disk"));
}
void
prep_postlaunch_fs (const char *filename, prep_data *data, const char *device)
{
if (guestfs_part_disk (g, device, data->params[2]) == -1)
prep_error (data, filename, _("failed to partition disk: %s"),
guestfs_last_error (g));
CLEANUP_FREE char *part;
if (asprintf (&part, "%s1", device) == -1)
error (EXIT_FAILURE, errno, "asprintf");
if (guestfs_mkfs (g, data->params[0], part) == -1)
prep_error (data, filename, _("failed to create filesystem (%s): %s"),
data->params[0], guestfs_last_error (g));
}