Re: [PATCH] mkfs.minix: remove die()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2011-04-12 at 13:05 +0200, Karel Zak wrote:
> On Tue, Apr 12, 2011 at 12:29:07PM +0200, Sami Kerola wrote:
> > On Tue, Apr 12, 2011 at 12:08, Karel Zak <kzak@xxxxxxxxxx> wrote:
> > >> -     die(_("%s is mounted; will not make a filesystem here!"));
> > >> +     errx(8, _("%s is mounted; will not make a filesystem here!"), device_name);
> > >
> > >  Please, define and use EXIT_SOMETHING instead of the magic number '8'.
> > 
> > The disc-utils/mkfs.cramfs.c has
> > 
> > #define MKFS_OK          0     /* No errors */
> > #define MKFS_ERROR       8     /* Operational error */
> > #define MKFS_USAGE       16    /* Usage or syntax error */
> > 
> > and the return value 8 seems to be somehow special for mkfs* I wonder
> > if the definition of return values should be in mkfs.h
> 
>  Good point, disk-utils/mkfs.h makes sense.

Ok, so here's v2 with the requested changes. Karel, regarding the
minixfs v3 specific patches, I will send you them once we cleanup
mkfs.minix in general.


From: Davidlohr Bueso <dave@xxxxxxx>
Date: Wed, 13 Apr 2011 10:33:19 -0300
Subject: [PATCH] mkfs.minix: remove die()

Get rid of this function and use errx(3) instead. This patch also introduces a mkfs.h header and defines general purpose mkfs related exit codes.

Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>
---
 disk-utils/mkfs.cramfs.c |    6 +---
 disk-utils/mkfs.h        |    9 ++++++
 disk-utils/mkfs.minix.c  |   73 +++++++++++++++++++++-------------------------
 3 files changed, 43 insertions(+), 45 deletions(-)
 create mode 100644 disk-utils/mkfs.h

diff --git a/disk-utils/mkfs.cramfs.c b/disk-utils/mkfs.cramfs.c
index c56aab4..5fc66b7 100644
--- a/disk-utils/mkfs.cramfs.c
+++ b/disk-utils/mkfs.cramfs.c
@@ -41,11 +41,7 @@
 #include "cramfs_common.h"
 #include "md5.h"
 #include "nls.h"
-
-/* Exit codes used by mkfs-type programs */
-#define MKFS_OK          0     /* No errors */
-#define MKFS_ERROR       8     /* Operational error */
-#define MKFS_USAGE       16    /* Usage or syntax error */
+#include "mkfs.h"
 
 /* The kernel only supports PAD_SIZE of 0 and 512. */
 #define PAD_SIZE 512
diff --git a/disk-utils/mkfs.h b/disk-utils/mkfs.h
new file mode 100644
index 0000000..35b88d1
--- /dev/null
+++ b/disk-utils/mkfs.h
@@ -0,0 +1,9 @@
+#ifndef __MKFS_H__
+#define __MKFS_H__
+
+/* Exit codes used by mkfs-type programs */
+#define MKFS_OK          0     /* No errors */
+#define MKFS_ERROR       8     /* Operational error */
+#define MKFS_USAGE       16    /* Usage or syntax error */
+
+#endif
diff --git a/disk-utils/mkfs.minix.c b/disk-utils/mkfs.minix.c
index 807a571..5941ba8 100644
--- a/disk-utils/mkfs.minix.c
+++ b/disk-utils/mkfs.minix.c
@@ -77,6 +77,7 @@
 #include "nls.h"
 #include "pathnames.h"
 #include "bitops.h"
+#include "mkfs.h"
 
 #define MINIX_ROOT_INO 1
 #define MINIX_BAD_INO 2
@@ -141,17 +142,10 @@ static unsigned long req_nr_inodes = 0;
 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
 
-static void
-die(char *str) {
-	fprintf(stderr, "%s: ", program_name);
-	fprintf(stderr, str, device_name);
-	fprintf(stderr, "\n");
-	exit(8);
-}
 
 static void __attribute__((__noreturn__))
 usage(void) {
-	errx(16, _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]"),
+	errx(MKFS_USAGE, _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]"),
 	     program_name);
 }
 
@@ -174,7 +168,7 @@ check_mount(void) {
 	if (!mnt)
 		return;
 
-	die(_("%s is mounted; will not make a filesystem here!"));
+	errx(MKFS_ERROR, _("%s is mounted; will not make a filesystem here!"), device_name);
 }
 
 static void
@@ -184,28 +178,27 @@ write_tables(void) {
 	Super.s_state &= ~MINIX_ERROR_FS;
 
 	if (lseek(DEV, 0, SEEK_SET))
-		die(_("seek to boot block failed in write_tables"));
+		errx(MKFS_ERROR, _("seek to boot block failed in write_tables"), device_name);
 	if (512 != write(DEV, boot_block_buffer, 512))
-		die(_("unable to clear boot sector"));
+		errx(MKFS_ERROR, _("unable to clear boot sector"), device_name);
 	if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
-		die(_("seek failed in write_tables"));
+		errx(MKFS_ERROR, _("seek failed in write_tables"), device_name);
 	if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
-		die(_("unable to write super-block"));
+		errx(MKFS_ERROR, _("unable to write super-block"), device_name);
 	if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
-		die(_("unable to write inode map"));
+		errx(MKFS_ERROR, _("unable to write inode map"), device_name);
 	if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
-		die(_("unable to write zone map"));
+		errx(MKFS_ERROR, _("unable to write zone map"), device_name);
 	if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
-		die(_("unable to write inodes"));
-	
+		errx(MKFS_ERROR, _("unable to write inodes"), device_name);
 }
 
 static void
 write_block(int blk, char * buffer) {
 	if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET))
-		die(_("seek failed in write_block"));
+		errx(MKFS_ERROR, _("seek failed in write_block"), device_name);
 	if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
-		die(_("write failed in write_block"));
+		errx(MKFS_ERROR, _("write failed in write_block"), device_name);
 }
 
 static int
@@ -213,7 +206,7 @@ get_free_block(void) {
 	int blk;
 
 	if (used_good_blocks+1 >= MAX_GOOD_BLOCKS)
-		die(_("too many bad blocks"));
+		errx(MKFS_ERROR, _("too many bad blocks"), device_name);
 	if (used_good_blocks)
 		blk = good_blocks_table[used_good_blocks-1]+1;
 	else
@@ -221,7 +214,7 @@ get_free_block(void) {
 	while (blk < ZONES && zone_in_use(blk))
 		blk++;
 	if (blk >= ZONES)
-		die(_("not enough good blocks"));
+		errx(MKFS_ERROR, _("not enough good blocks"), device_name);
 	good_blocks_table[used_good_blocks] = blk;
 	used_good_blocks++;
 	return blk;
@@ -287,7 +280,7 @@ make_bad_inode(void) {
 				goto end_bad;
 		}
 	}
-	die(_("too many bad blocks"));
+	errx(MKFS_ERROR, _("too many bad blocks"), device_name);
 end_bad:
 	if (ind)
 		write_block(ind, (char *) ind_block);
@@ -336,7 +329,7 @@ make_bad_inode2 (void) {
 		}
 	}
 	/* Could make triple indirect block here */
-	die (_("too many bad blocks"));
+	errx(MKFS_ERROR, _("too many bad blocks"), device_name);
  end_bad:
 	if (ind)
 		write_block (ind, (char *) ind_block);
@@ -395,7 +388,7 @@ setup_tables(void) {
 
 	super_block_buffer = calloc(1, BLOCK_SIZE);
 	if (!super_block_buffer)
-		die(_("unable to alloc buffer for superblock"));
+		errx(MKFS_ERROR, _("unable to alloc buffer for superblock"), device_name);
 
 	memset(boot_block_buffer,0,512);
 	Super.s_magic = magic;
@@ -436,7 +429,7 @@ setup_tables(void) {
 	inode_map = malloc(IMAPS * BLOCK_SIZE);
 	zone_map = malloc(ZMAPS * BLOCK_SIZE);
 	if (!inode_map || !zone_map)
-		die(_("unable to allocate buffers for maps"));
+		errx(MKFS_ERROR, _("unable to allocate buffers for maps"), device_name);
 	memset(inode_map,0xff,IMAPS * BLOCK_SIZE);
 	memset(zone_map,0xff,ZMAPS * BLOCK_SIZE);
 	for (i = FIRSTZONE ; i<ZONES ; i++)
@@ -445,7 +438,7 @@ setup_tables(void) {
 		unmark_inode(i);
 	inode_buffer = malloc(INODE_BUFFER_SIZE);
 	if (!inode_buffer)
-		die(_("unable to allocate buffer for inodes"));
+		errx(MKFS_ERROR, _("unable to allocate buffer for inodes"), device_name);
 	memset(inode_buffer,0,INODE_BUFFER_SIZE);
 	printf(_("%ld inodes\n"),INODES);
 	printf(_("%ld blocks\n"),ZONES);
@@ -465,7 +458,7 @@ do_check(char * buffer, int try, unsigned int current_block) {
 	/* Seek to the correct loc. */
 	if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
 		       current_block * BLOCK_SIZE ) {
-		 die(_("seek failed during testing of blocks"));
+		errx(MKFS_ERROR, _("seek failed during testing of blocks"), device_name);
 	}
 
 
@@ -504,7 +497,7 @@ check_blocks(void) {
 	while (currently_testing < ZONES) {
 		if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) !=
 		currently_testing*BLOCK_SIZE)
-			die(_("seek failed in check_blocks"));
+			errx(MKFS_ERROR, _("seek failed in check_blocks"), device_name);
 		try = TEST_BUFFER_BLOCKS;
 		if (currently_testing + try > ZONES)
 			try = ZONES-currently_testing;
@@ -513,7 +506,7 @@ check_blocks(void) {
 		if (got == try)
 			continue;
 		if (currently_testing < FIRSTZONE)
-			die(_("bad blocks before data-area: cannot make fs"));
+			errx(MKFS_ERROR, _("bad blocks before data-area: cannot make fs"), device_name);
 		mark_zone(currently_testing);
 		badblocks++;
 		currently_testing++;
@@ -531,12 +524,12 @@ get_list_blocks(char *filename) {
 
 	listfile = fopen(filename,"r");
 	if (listfile == NULL)
-		die(_("can't open file of bad blocks"));
+		errx(MKFS_ERROR, _("can't open file of bad blocks"), device_name);
 
 	while (!feof(listfile)) {
 		if (fscanf(listfile,"%ld\n", &blockno) != 1) {
 			printf(_("badblock number input error on line %d\n"), badblocks + 1);
-			die(_("cannot read badblocks file"));
+			errx(MKFS_ERROR, _("cannot read badblocks file"), device_name);
 		}
 		mark_zone(blockno);
 		badblocks++;
@@ -573,9 +566,9 @@ main(int argc, char ** argv) {
 	}
 
 	if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
-		die(_("bad inode size"));
+		errx(MKFS_ERROR, _("bad inode size"), device_name);
 	if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
-		die(_("bad inode size"));
+		errx(MKFS_ERROR, _("bad inode size"), device_name);
 
 	opterr = 0;
 	while ((i = getopt(argc, argv, "ci:l:n:v")) != -1)
@@ -635,23 +628,23 @@ main(int argc, char ** argv) {
 	*(short *)tmp = 2;
 	strcpy(tmp+2,".badblocks");
 	if (stat(device_name, &statbuf) < 0)
-		die(_("unable to stat %s"));
+		errx(MKFS_ERROR, _("unable to stat %s"), device_name);
 	if (S_ISBLK(statbuf.st_mode))
 		DEV = open(device_name,O_RDWR | O_EXCL);
 	else
 		DEV = open(device_name,O_RDWR);
 	if (DEV<0)
-		die(_("unable to open %s"));
+		errx(MKFS_ERROR, _("unable to open %s"), device_name);
 	if (S_ISBLK(statbuf.st_mode)) {
 		int sectorsize;
 
 		if (blkdev_get_sector_size(DEV, &sectorsize) == -1)
-			die(_("cannot determine sector size for %s"));
+			errx(MKFS_ERROR, _("cannot determine sector size for %s"), device_name);
 		if (BLOCK_SIZE < sectorsize)
-			die(_("block size smaller than physical sector size of %s"));
+			errx(MKFS_ERROR, _("block size smaller than physical sector size of %s"), device_name);
 		if (!BLOCKS) {
 			if (blkdev_get_size(DEV, &BLOCKS) == -1)
-				die(_("cannot determine size of %s"));
+				errx(MKFS_ERROR, _("cannot determine size of %s"), device_name);
 			BLOCKS /= BLOCK_SIZE;
 		}
 	} else if (!S_ISBLK(statbuf.st_mode)) {
@@ -659,9 +652,9 @@ main(int argc, char ** argv) {
 			BLOCKS = statbuf.st_size / BLOCK_SIZE;
 		check=0;
 	} else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
-		die(_("will not try to make filesystem on '%s'"));
+		errx(MKFS_ERROR, _("will not try to make filesystem on '%s'"), device_name);
 	if (BLOCKS < 10)
-		die(_("number of blocks too small"));
+		errx(MKFS_ERROR, _("number of blocks too small"), device_name);
 	if (version2) {
 		if (namelen == 14)
 			magic = MINIX2_SUPER_MAGIC;
-- 
1.7.1




--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux