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
This commit is contained in:
Denis Vlasenko 2008-06-29 05:10:47 +00:00
parent ce13b76002
commit 6eaf0a9883
4 changed files with 66 additions and 68 deletions

View File

@ -20,21 +20,22 @@
# define USE_FEATURE_FDISK_BLKSIZE(a) # define USE_FEATURE_FDISK_BLKSIZE(a)
#endif #endif
#define DEFAULT_SECTOR_SIZE 512 #define DEFAULT_SECTOR_SIZE 512
#define MAX_SECTOR_SIZE 2048 #define DEFAULT_SECTOR_SIZE_STR "512"
#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */ #define MAX_SECTOR_SIZE 2048
#define MAXIMUM_PARTS 60 #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 EXTENDED 0x05
#define WIN98_EXTENDED 0x0f #define WIN98_EXTENDED 0x0f
#define LINUX_PARTITION 0x81 #define LINUX_PARTITION 0x81
#define LINUX_SWAP 0x82 #define LINUX_SWAP 0x82
#define LINUX_NATIVE 0x83 #define LINUX_NATIVE 0x83
#define LINUX_EXTENDED 0x85 #define LINUX_EXTENDED 0x85
#define LINUX_LVM 0x8e #define LINUX_LVM 0x8e
#define LINUX_RAID 0xfd #define LINUX_RAID 0xfd
enum { enum {
@ -290,12 +291,25 @@ struct globals {
unsigned user_set_sector_size; unsigned user_set_sector_size;
unsigned sector_offset; // = 1; unsigned sector_offset; // = 1;
unsigned g_heads, g_sectors, g_cylinders; 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; smallint display_in_cyl_units; // = 1;
#if ENABLE_FEATURE_OSF_LABEL #if ENABLE_FEATURE_OSF_LABEL
smallint possibly_osf_label; smallint possibly_osf_label;
#endif #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; jmp_buf listingbuf;
char line_buffer[80]; char line_buffer[80];
char partname_buffer[80]; char partname_buffer[80];
@ -319,6 +333,19 @@ struct globals {
#define current_label_type (G.current_label_type ) #define current_label_type (G.current_label_type )
#define display_in_cyl_units (G.display_in_cyl_units) #define display_in_cyl_units (G.display_in_cyl_units)
#define possibly_osf_label (G.possibly_osf_label ) #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 listingbuf (G.listingbuf)
#define line_buffer (G.line_buffer) #define line_buffer (G.line_buffer)
#define partname_buffer (G.partname_buffer) #define partname_buffer (G.partname_buffer)
@ -331,6 +358,7 @@ struct globals {
g_partitions = 4; \ g_partitions = 4; \
display_in_cyl_units = 1; \ display_in_cyl_units = 1; \
units_per_sector = 1; \ units_per_sector = 1; \
dos_compatible_flag = 1; \
} while (0) } while (0)
@ -661,21 +689,6 @@ get_nr_sects(const struct partition *p)
return read4_little_endian(p->size4); 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) static void fdisk_fatal(const char *why)
{ {
if (listing) { if (listing) {
@ -706,8 +719,7 @@ static void
write_sector(ullong secno, char *buf) write_sector(ullong secno, char *buf)
{ {
seek_sector(secno); seek_sector(secno);
if (write(dev_fd, buf, sector_size) != sector_size) xwrite(dev_fd, buf, sector_size);
fdisk_fatal(unable_to_write);
} }
#endif #endif
@ -716,9 +728,10 @@ static void
read_pte(struct pte *pe, ullong offset) read_pte(struct pte *pe, ullong offset)
{ {
pe->offset = offset; pe->offset = offset;
pe->sectorbuffer = xmalloc(sector_size); pe->sectorbuffer = xzalloc(sector_size);
seek_sector(offset); 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); fdisk_fatal(unable_to_read);
#if ENABLE_FEATURE_FDISK_WRITABLE #if ENABLE_FEATURE_FDISK_WRITABLE
pe->changed = 0; pe->changed = 0;
@ -1167,8 +1180,9 @@ get_sectorsize(void)
if (ioctl(dev_fd, BLKSSZGET, &arg) == 0) if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
sector_size = arg; sector_size = arg;
if (sector_size != DEFAULT_SECTOR_SIZE) if (sector_size != DEFAULT_SECTOR_SIZE)
printf("Note: sector size is %d (not %d)\n", printf("Note: sector size is %d "
sector_size, DEFAULT_SECTOR_SIZE); "(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); printf("'%s' is opened for read only\n", disk_device);
} }
xmove_fd(fd, dev_fd); xmove_fd(fd, dev_fd);
if (512 != read(dev_fd, MBRbuffer, 512)) { if (512 != full_read(dev_fd, MBRbuffer, 512)) {
if (what == TRY_ONLY) { if (what == TRY_ONLY) {
close_dev_fd(); close_dev_fd();
return 1; return 1;
@ -1313,7 +1327,7 @@ static int get_boot(void)
fd = open(disk_device, O_RDONLY); fd = open(disk_device, O_RDONLY);
if (fd < 0) if (fd < 0)
return 1; return 1;
if (512 != read(fd, MBRbuffer, 512)) { if (512 != full_read(fd, MBRbuffer, 512)) {
close(fd); close(fd);
return 1; return 1;
} }

View File

@ -688,13 +688,12 @@ xbsd_get_bootstrap(char *path, void *ptr, int size)
{ {
int fdb; int fdb;
fdb = open(path, O_RDONLY); fdb = open_or_warn(path, O_RDONLY);
if (fdb < 0) { if (fdb < 0) {
perror(path);
return 0; return 0;
} }
if (read(fdb, ptr, size) < 0) { if (full_read(fdb, ptr, size) < 0) {
perror(path); bb_simple_perror_msg(path);
close(fdb); close(fdb);
return 0; return 0;
} }
@ -765,10 +764,8 @@ xbsd_write_bootstrap(void)
sector = get_start_sect(xbsd_part); sector = get_start_sect(xbsd_part);
#endif #endif
if (lseek(dev_fd, sector * SECTOR_SIZE, SEEK_SET) == -1) seek_sector(sector);
fdisk_fatal(unable_to_seek); xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
if (BSD_BBSIZE != write(dev_fd, disklabelbuffer, BSD_BBSIZE))
fdisk_fatal(unable_to_write);
#if defined(__alpha__) #if defined(__alpha__)
printf("Bootstrap installed on %s\n", disk_device); printf("Bootstrap installed on %s\n", disk_device);
@ -938,9 +935,8 @@ xbsd_readlabel(struct partition *p)
sector = 0; sector = 0;
#endif #endif
if (lseek(dev_fd, sector * SECTOR_SIZE, SEEK_SET) == -1) seek_sector(sector);
fdisk_fatal(unable_to_seek); if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
if (BSD_BBSIZE != read(dev_fd, disklabelbuffer, BSD_BBSIZE))
fdisk_fatal(unable_to_read); fdisk_fatal(unable_to_read);
memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
@ -984,15 +980,12 @@ xbsd_writelabel(struct partition *p)
#if defined(__alpha__) && BSD_LABELSECTOR == 0 #if defined(__alpha__) && BSD_LABELSECTOR == 0
alpha_bootblock_checksum(disklabelbuffer); alpha_bootblock_checksum(disklabelbuffer);
if (lseek(dev_fd, 0, SEEK_SET) == -1) seek_sector(0);
fdisk_fatal(unable_to_seek); xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
if (BSD_BBSIZE != write(dev_fd, disklabelbuffer, BSD_BBSIZE))
fdisk_fatal(unable_to_write);
#else #else
if (lseek(dev_fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1) seek_sector(sector);
fdisk_fatal(unable_to_seek); lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
if (sizeof(struct xbsd_disklabel) != write(dev_fd, d, sizeof(struct xbsd_disklabel))) xwrite(dev_fd, d, sizeof(*d));
fdisk_fatal(unable_to_write);
#endif #endif
sync_disks(); sync_disks();
return 1; return 1;

View File

@ -439,10 +439,7 @@ sgi_write_table(void)
assert(two_s_complement_32bit_sum( assert(two_s_complement_32bit_sum(
(unsigned int*)sgilabel, sizeof(*sgilabel)) == 0); (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
if (lseek(dev_fd, 0, SEEK_SET) < 0) write_sector(0, sgilabel);
fdisk_fatal(unable_to_seek);
if (write(dev_fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
fdisk_fatal(unable_to_write);
if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) { if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
/* /*
* keep this habit of first writing the "sgilabel". * keep this habit of first writing the "sgilabel".
@ -450,10 +447,7 @@ sgi_write_table(void)
*/ */
sgiinfo *info = fill_sgiinfo(); sgiinfo *info = fill_sgiinfo();
int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start); int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
if (lseek(dev_fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0) write_sector(infostartblock, info);
fdisk_fatal(unable_to_seek);
if (write(dev_fd, info, SECTOR_SIZE) != SECTOR_SIZE)
fdisk_fatal(unable_to_write);
free(info); free(info);
} }
} }

View File

@ -722,9 +722,6 @@ sun_write_table(void)
while (ush < (unsigned short *)(&sunlabel->csum)) while (ush < (unsigned short *)(&sunlabel->csum))
csum ^= *ush++; csum ^= *ush++;
sunlabel->csum = csum; sunlabel->csum = csum;
if (lseek(dev_fd, 0, SEEK_SET) < 0) write_sector(0, sunlabel);
fdisk_fatal(unable_to_seek);
if (write(dev_fd, sunlabel, SECTOR_SIZE) != SECTOR_SIZE)
fdisk_fatal(unable_to_write);
} }
#endif /* SUN_LABEL */ #endif /* SUN_LABEL */