]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: remove ugly fatal() function
authorKarel Zak <kzak@redhat.com>
Tue, 11 Jun 2013 08:41:34 +0000 (10:41 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:46:59 +0000 (16:46 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
fdisks/fdisk.h
fdisks/fdiskbsdlabel.c

index cf6db69f3f531fc115980c40a60f9fde1edd51ab..d9c1753294eb8819f39217cd31ee338314b9120d 100644 (file)
@@ -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;
index cb7f877b1e6ee98c537ceb97ea19abd7903c59f9..32d6a2afbbcaadb8476708cd4606d6d6ecc0262c 100644 (file)
@@ -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);
index eaf96dbeba552ebc042901eea502c313ad60ed05..a1e6d80b4b70063b82beb5ab84d6f6ae2788d620 100644 (file)
@@ -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