From 6eaf0a98831960e3a4593e6630404634d747ab82 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sun, 29 Jun 2008 05:10:47 +0000 Subject: fdisk: move more data to struct globals; shrink code function old new delta nowarn 1 - -1 listing 1 - -1 dos_compatible_flag 1 - -1 warn_cylinders 44 42 -2 open_list_and_close 364 362 -2 fdisk_fatal 41 38 -3 verify 1053 1049 -4 user_sectors 4 - -4 user_heads 4 - -4 user_cylinders 4 - -4 pt_sectors 4 - -4 pt_heads 4 - -4 kern_sectors 4 - -4 kern_heads 4 - -4 ext_index 4 - -4 total_number_of_sectors 8 - -8 extended_offset 8 - -8 create_doslabel 129 120 -9 write_table 225 211 -14 delete_partition 445 431 -14 set_partition 476 459 -17 list_disk_geometry 247 229 -18 unable_to_write 19 - -19 add_partition 2515 2486 -29 get_boot 1709 1636 -73 fdisk_main 2812 2679 -133 ------------------------------------------------------------------------------ (add/remove: 0/14 grow/shrink: 0/12 up/down: 0/-388) Total: -388 bytes text data bss dec hex filename 804634 611 6852 812097 c6441 busybox_old 804298 610 6804 811712 c62c0 busybox_unstripped --- util-linux/fdisk.c | 88 +++++++++++++++++++++++++++++--------------------- util-linux/fdisk_osf.c | 31 +++++++----------- util-linux/fdisk_sgi.c | 10 ++---- util-linux/fdisk_sun.c | 5 +-- 4 files changed, 66 insertions(+), 68 deletions(-) diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c index 4034ca5..7f046ed 100644 --- a/util-linux/fdisk.c +++ b/util-linux/fdisk.c @@ -20,21 +20,22 @@ # define USE_FEATURE_FDISK_BLKSIZE(a) #endif -#define DEFAULT_SECTOR_SIZE 512 -#define MAX_SECTOR_SIZE 2048 -#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */ -#define MAXIMUM_PARTS 60 +#define DEFAULT_SECTOR_SIZE 512 +#define DEFAULT_SECTOR_SIZE_STR "512" +#define MAX_SECTOR_SIZE 2048 +#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */ +#define MAXIMUM_PARTS 60 -#define ACTIVE_FLAG 0x80 +#define ACTIVE_FLAG 0x80 -#define EXTENDED 0x05 -#define WIN98_EXTENDED 0x0f -#define LINUX_PARTITION 0x81 -#define LINUX_SWAP 0x82 -#define LINUX_NATIVE 0x83 -#define LINUX_EXTENDED 0x85 -#define LINUX_LVM 0x8e -#define LINUX_RAID 0xfd +#define EXTENDED 0x05 +#define WIN98_EXTENDED 0x0f +#define LINUX_PARTITION 0x81 +#define LINUX_SWAP 0x82 +#define LINUX_NATIVE 0x83 +#define LINUX_EXTENDED 0x85 +#define LINUX_LVM 0x8e +#define LINUX_RAID 0xfd enum { @@ -290,12 +291,25 @@ struct globals { unsigned user_set_sector_size; unsigned sector_offset; // = 1; unsigned g_heads, g_sectors, g_cylinders; - enum label_type current_label_type; + smallint /* enum label_type */ current_label_type; smallint display_in_cyl_units; // = 1; #if ENABLE_FEATURE_OSF_LABEL smallint possibly_osf_label; #endif + smallint listing; /* no aborts for fdisk -l */ + smallint dos_compatible_flag; // = 1; +#if ENABLE_FEATURE_FDISK_WRITABLE + //int dos_changed; + smallint nowarn; /* no warnings for fdisk -l/-s */ +#endif + int ext_index; /* the prime extended partition */ + unsigned user_cylinders, user_heads, user_sectors; + unsigned pt_heads, pt_sectors; + unsigned kern_heads, kern_sectors; + ullong extended_offset; /* offset of link pointers */ + ullong total_number_of_sectors; + jmp_buf listingbuf; char line_buffer[80]; char partname_buffer[80]; @@ -319,6 +333,19 @@ struct globals { #define current_label_type (G.current_label_type ) #define display_in_cyl_units (G.display_in_cyl_units) #define possibly_osf_label (G.possibly_osf_label ) +#define listing (G.listing ) +#define dos_compatible_flag (G.dos_compatible_flag ) +#define nowarn (G.nowarn ) +#define ext_index (G.ext_index ) +#define user_cylinders (G.user_cylinders ) +#define user_heads (G.user_heads ) +#define user_sectors (G.user_sectors ) +#define pt_heads (G.pt_heads ) +#define pt_sectors (G.pt_sectors ) +#define kern_heads (G.kern_heads ) +#define kern_sectors (G.kern_sectors ) +#define extended_offset (G.extended_offset ) +#define total_number_of_sectors (G.total_number_of_sectors) #define listingbuf (G.listingbuf) #define line_buffer (G.line_buffer) #define partname_buffer (G.partname_buffer) @@ -331,6 +358,7 @@ struct globals { g_partitions = 4; \ display_in_cyl_units = 1; \ units_per_sector = 1; \ + dos_compatible_flag = 1; \ } while (0) @@ -661,21 +689,6 @@ get_nr_sects(const struct partition *p) return read4_little_endian(p->size4); } -static int ext_index; /* the prime extended partition */ -static smallint listing; /* no aborts for fdisk -l */ -static smallint dos_compatible_flag = 1; -#if ENABLE_FEATURE_FDISK_WRITABLE -//static int dos_changed; -static smallint nowarn; /* no warnings for fdisk -l/-s */ -#endif - -static unsigned user_cylinders, user_heads, user_sectors; -static unsigned pt_heads, pt_sectors; -static unsigned kern_heads, kern_sectors; - -static ullong extended_offset; /* offset of link pointers */ -static ullong total_number_of_sectors; - static void fdisk_fatal(const char *why) { if (listing) { @@ -706,8 +719,7 @@ static void write_sector(ullong secno, char *buf) { seek_sector(secno); - if (write(dev_fd, buf, sector_size) != sector_size) - fdisk_fatal(unable_to_write); + xwrite(dev_fd, buf, sector_size); } #endif @@ -716,9 +728,10 @@ static void read_pte(struct pte *pe, ullong offset) { pe->offset = offset; - pe->sectorbuffer = xmalloc(sector_size); + pe->sectorbuffer = xzalloc(sector_size); seek_sector(offset); - if (read(dev_fd, pe->sectorbuffer, sector_size) != sector_size) + /* xread would make us abort - bad for fdisk -l */ + if (full_read(dev_fd, pe->sectorbuffer, sector_size) != sector_size) fdisk_fatal(unable_to_read); #if ENABLE_FEATURE_FDISK_WRITABLE pe->changed = 0; @@ -1167,8 +1180,9 @@ get_sectorsize(void) if (ioctl(dev_fd, BLKSSZGET, &arg) == 0) sector_size = arg; if (sector_size != DEFAULT_SECTOR_SIZE) - printf("Note: sector size is %d (not %d)\n", - sector_size, DEFAULT_SECTOR_SIZE); + printf("Note: sector size is %d " + "(not " DEFAULT_SECTOR_SIZE_STR ")\n", + sector_size); } } @@ -1302,7 +1316,7 @@ static int get_boot(void) printf("'%s' is opened for read only\n", disk_device); } xmove_fd(fd, dev_fd); - if (512 != read(dev_fd, MBRbuffer, 512)) { + if (512 != full_read(dev_fd, MBRbuffer, 512)) { if (what == TRY_ONLY) { close_dev_fd(); return 1; @@ -1313,7 +1327,7 @@ static int get_boot(void) fd = open(disk_device, O_RDONLY); if (fd < 0) return 1; - if (512 != read(fd, MBRbuffer, 512)) { + if (512 != full_read(fd, MBRbuffer, 512)) { close(fd); return 1; } diff --git a/util-linux/fdisk_osf.c b/util-linux/fdisk_osf.c index 24f240a..c50ee9b 100644 --- a/util-linux/fdisk_osf.c +++ b/util-linux/fdisk_osf.c @@ -688,13 +688,12 @@ xbsd_get_bootstrap(char *path, void *ptr, int size) { int fdb; - fdb = open(path, O_RDONLY); + fdb = open_or_warn(path, O_RDONLY); if (fdb < 0) { - perror(path); return 0; } - if (read(fdb, ptr, size) < 0) { - perror(path); + if (full_read(fdb, ptr, size) < 0) { + bb_simple_perror_msg(path); close(fdb); return 0; } @@ -765,10 +764,8 @@ xbsd_write_bootstrap(void) sector = get_start_sect(xbsd_part); #endif - if (lseek(dev_fd, sector * SECTOR_SIZE, SEEK_SET) == -1) - fdisk_fatal(unable_to_seek); - if (BSD_BBSIZE != write(dev_fd, disklabelbuffer, BSD_BBSIZE)) - fdisk_fatal(unable_to_write); + seek_sector(sector); + xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE); #if defined(__alpha__) printf("Bootstrap installed on %s\n", disk_device); @@ -938,9 +935,8 @@ xbsd_readlabel(struct partition *p) sector = 0; #endif - if (lseek(dev_fd, sector * SECTOR_SIZE, SEEK_SET) == -1) - fdisk_fatal(unable_to_seek); - if (BSD_BBSIZE != read(dev_fd, disklabelbuffer, BSD_BBSIZE)) + seek_sector(sector); + if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE)) fdisk_fatal(unable_to_read); memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], @@ -984,15 +980,12 @@ xbsd_writelabel(struct partition *p) #if defined(__alpha__) && BSD_LABELSECTOR == 0 alpha_bootblock_checksum(disklabelbuffer); - if (lseek(dev_fd, 0, SEEK_SET) == -1) - fdisk_fatal(unable_to_seek); - if (BSD_BBSIZE != write(dev_fd, disklabelbuffer, BSD_BBSIZE)) - fdisk_fatal(unable_to_write); + seek_sector(0); + xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE); #else - if (lseek(dev_fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1) - fdisk_fatal(unable_to_seek); - if (sizeof(struct xbsd_disklabel) != write(dev_fd, d, sizeof(struct xbsd_disklabel))) - fdisk_fatal(unable_to_write); + seek_sector(sector); + lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR); + xwrite(dev_fd, d, sizeof(*d)); #endif sync_disks(); return 1; diff --git a/util-linux/fdisk_sgi.c b/util-linux/fdisk_sgi.c index 55e77d5..5a86a68 100644 --- a/util-linux/fdisk_sgi.c +++ b/util-linux/fdisk_sgi.c @@ -439,10 +439,7 @@ sgi_write_table(void) assert(two_s_complement_32bit_sum( (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0); - if (lseek(dev_fd, 0, SEEK_SET) < 0) - fdisk_fatal(unable_to_seek); - if (write(dev_fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE) - fdisk_fatal(unable_to_write); + write_sector(0, sgilabel); if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) { /* * keep this habit of first writing the "sgilabel". @@ -450,10 +447,7 @@ sgi_write_table(void) */ sgiinfo *info = fill_sgiinfo(); int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start); - if (lseek(dev_fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0) - fdisk_fatal(unable_to_seek); - if (write(dev_fd, info, SECTOR_SIZE) != SECTOR_SIZE) - fdisk_fatal(unable_to_write); + write_sector(infostartblock, info); free(info); } } diff --git a/util-linux/fdisk_sun.c b/util-linux/fdisk_sun.c index eec319c..427efbe 100644 --- a/util-linux/fdisk_sun.c +++ b/util-linux/fdisk_sun.c @@ -722,9 +722,6 @@ sun_write_table(void) while (ush < (unsigned short *)(&sunlabel->csum)) csum ^= *ush++; sunlabel->csum = csum; - if (lseek(dev_fd, 0, SEEK_SET) < 0) - fdisk_fatal(unable_to_seek); - if (write(dev_fd, sunlabel, SECTOR_SIZE) != SECTOR_SIZE) - fdisk_fatal(unable_to_write); + write_sector(0, sunlabel); } #endif /* SUN_LABEL */ -- cgit v1.1