From: Karel Zak Date: Tue, 11 Jun 2013 08:41:34 +0000 (+0200) Subject: fdisk: remove ugly fatal() function X-Git-Tag: v2.24-rc1~198 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=918afd3f1a5c3be33098f3b5cb3c51189f2eb48c;p=thirdparty%2Futil-linux.git fdisk: remove ugly fatal() function Signed-off-by: Karel Zak --- diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index cf6db69f3f..d9c1753294 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -87,28 +87,6 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out) exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } -void __attribute__((__noreturn__)) -fatal(struct fdisk_context *cxt, enum failure why) -{ - close(cxt->dev_fd); - switch (why) { - case unable_to_read: - err(EXIT_FAILURE, _("unable to read %s"), cxt->dev_path); - - case unable_to_seek: - err(EXIT_FAILURE, _("unable to seek on %s"), cxt->dev_path); - - case unable_to_write: - err(EXIT_FAILURE, _("unable to write %s"), cxt->dev_path); - - case ioctl_error: - err(EXIT_FAILURE, _("BLKGETSIZE ioctl failed on %s"), cxt->dev_path); - - default: - err(EXIT_FAILURE, _("fatal error")); - } -} - struct partition * get_part_table(int i) { return ptes[i].part_table; diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index cb7f877b1e..32d6a2afbb 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -59,14 +59,6 @@ enum menutype { EXPERT_MENU, }; -enum failure { - ioctl_error, - unable_to_read, - unable_to_seek, - unable_to_write -}; - - extern int get_user_reply(struct fdisk_context *cxt, const char *prompt, char *buf, size_t bufsz); @@ -79,7 +71,6 @@ extern int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask, /* prototypes for fdisk.c */ extern char *line_ptr; -extern void fatal(struct fdisk_context *cxt, enum failure why); extern void list_partition_types(struct fdisk_context *cxt); extern int read_line(struct fdisk_context *cxt, int *asked); extern char read_char(struct fdisk_context *cxt, char *mesg); diff --git a/fdisks/fdiskbsdlabel.c b/fdisks/fdiskbsdlabel.c index eaf96dbeba..a1e6d80b4b 100644 --- a/fdisks/fdiskbsdlabel.c +++ b/fdisks/fdiskbsdlabel.c @@ -72,7 +72,7 @@ struct fdisk_bsd_label { static int xbsd_delete_part (struct fdisk_context *cxt, size_t partnum); static void xbsd_edit_disklabel (struct fdisk_context *cxt); -static void xbsd_write_bootstrap (struct fdisk_context *cxt); +static int xbsd_write_bootstrap (struct fdisk_context *cxt); static void xbsd_change_fstype (struct fdisk_context *cxt); static int xbsd_get_part_index (struct fdisk_context *cxt, int max); static int xbsd_check_new_partition (struct fdisk_context *cxt, int *i); @@ -522,7 +522,7 @@ xbsd_get_bootstrap (char *path, void *ptr, int size) return 1; } -static void +static int xbsd_write_bootstrap (struct fdisk_context *cxt) { char *bootdir = BSD_LINUX_BOOTDIR; @@ -545,7 +545,7 @@ xbsd_write_bootstrap (struct fdisk_context *cxt) } snprintf (path, sizeof(path), "%s/%sboot", bootdir, dkbasename); if (!xbsd_get_bootstrap (path, disklabelbuffer, (int) xbsd_dlabel.d_secsize)) - return; + return -1; /* We need a backup of the disklabel (xbsd_dlabel might have changed). */ d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE]; @@ -557,13 +557,13 @@ xbsd_write_bootstrap (struct fdisk_context *cxt) snprintf (path, sizeof(path), "%s/boot%s", bootdir, dkbasename); if (!xbsd_get_bootstrap (path, &disklabelbuffer[xbsd_dlabel.d_secsize], (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize)) - return; + return -1; e = d + sizeof (struct xbsd_disklabel); for (p=d; p < e; p++) if (*p) { - fprintf (stderr, _("Bootstrap overlaps with disk label!\n")); - exit ( EXIT_FAILURE ); + fdisk_warnx(cxt, _("Bootstrap overlaps with disk label!\n")); + return -EINVAL; } memmove (d, &dl, sizeof (struct xbsd_disklabel)); @@ -577,14 +577,18 @@ xbsd_write_bootstrap (struct fdisk_context *cxt) sector = get_start_sect(xbsd_part); #endif - if (lseek (cxt->dev_fd, (off_t) sector * SECTOR_SIZE, SEEK_SET) == -1) - fatal (cxt, unable_to_seek); - if (BSD_BBSIZE != write (cxt->dev_fd, disklabelbuffer, BSD_BBSIZE)) - fatal (cxt, unable_to_write); + if (lseek (cxt->dev_fd, (off_t) sector * SECTOR_SIZE, SEEK_SET) == -1) { + fdisk_warn(cxt, _("seek failed: %s"), cxt->dev_path); + return -errno; + } + if (BSD_BBSIZE != write (cxt->dev_fd, disklabelbuffer, BSD_BBSIZE)) { + fdisk_warn(cxt, _("write failed: %s"), cxt->dev_path); + return -errno; + } printf (_("Bootstrap installed on %s.\n"), cxt->dev_path); - sync_disks (); + return 0; } /* TODO: remove this, use regular change_partition_type() in fdisk.c */ @@ -799,21 +803,28 @@ xbsd_writelabel (struct fdisk_context *cxt, struct partition *p, struct xbsd_dis #if defined (__alpha__) && BSD_LABELSECTOR == 0 alpha_bootblock_checksum (disklabelbuffer); - if (lseek (cxt->dev_fd, (off_t) 0, SEEK_SET) == -1) - fatal (cxt, unable_to_seek); - if (BSD_BBSIZE != write (cxt->dev_fd, disklabelbuffer, BSD_BBSIZE)) - fatal (cxt, unable_to_write); + if (lseek (cxt->dev_fd, (off_t) 0, SEEK_SET) == -1) { + fdisk_warn(cxt, _("seek failed: %d"), cxt->dev_path); + return -errno; + } + if (BSD_BBSIZE != write (cxt->dev_fd, disklabelbuffer, BSD_BBSIZE)) { + fdisk_warn(cxt, _("write failed: %d"), cxt->dev_path); + return -errno; + } #else if (lseek (cxt->dev_fd, (off_t) sector * SECTOR_SIZE + BSD_LABELOFFSET, - SEEK_SET) == -1) - fatal (cxt, unable_to_seek); - if (sizeof (struct xbsd_disklabel) != write (cxt->dev_fd, d, sizeof (struct xbsd_disklabel))) - fatal (cxt, unable_to_write); + SEEK_SET) == -1) { + fdisk_warn(cxt, _("seek failed: %d"), cxt->dev_path); + return -errno; + } + if (sizeof (struct xbsd_disklabel) != write (cxt->dev_fd, d, sizeof (struct xbsd_disklabel))) { + fdisk_warn(cxt, _("write failed: %d"), cxt->dev_path); + return -errno; + } #endif sync_disks (); - - return 1; + return 0; } static void