Signed-off-by: Li Zefan <lizf@xxxxxxxxxxxxxx> --- fdisk/sfdisk.c | 69 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 55 insertions(+), 14 deletions(-) diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c index 29f66cc..ee28750 100644 --- a/fdisk/sfdisk.c +++ b/fdisk/sfdisk.c @@ -297,41 +297,49 @@ static int save_sectors(char *dev, int fdin) { struct sector *s; char ss[516]; - int fdout; + int fdout = -1; fdout = open(save_sector_file, O_WRONLY | O_CREAT, 0444); if (fdout < 0) { perror(save_sector_file); error(_("cannot open partition sector save file (%s)\n"), save_sector_file); - return 0; + goto err; } for (s = sectorhead; s; s = s->next) if (s->to_be_written) { ulong_to_chars(s->sectornumber, ss); if (!sseek(dev, fdin, s->sectornumber)) - return 0; + goto err; if (read(fdin, ss+4, 512) != 512) { perror("read"); error(_("read error on %s - cannot read sector %lu\n"), dev, s->sectornumber); - return 0; + goto err; } if (write(fdout, ss, sizeof(ss)) != sizeof(ss)) { perror("write"); error(_("write error on %s\n"), save_sector_file); - return 0; + goto err; } } + + close(fdout); return 1; + +err: + if (fdout >= 0) + close(fdout); + return 0; } static void reread_disk_partition(char *dev, int fd); static int restore_sectors(char *dev) { - int fdin, fdout, ct; + int fdin = -1, fdout = -1; + int ct; struct stat statbuf; char *ss0, *ss; unsigned long sno; @@ -340,34 +348,34 @@ restore_sectors(char *dev) { perror(restore_sector_file); error(_("cannot stat partition restore file (%s)\n"), restore_sector_file); - return 0; + goto err; } if (statbuf.st_size % 516) { error(_("partition restore file has wrong size - not restoring\n")); - return 0; + goto err; } if (!(ss = (char *) malloc(statbuf.st_size))) { error(_("out of memory?\n")); - return 0; + goto err; } fdin = open(restore_sector_file, O_RDONLY); if (fdin < 0) { perror(restore_sector_file); error(_("cannot open partition restore file (%s)\n"), restore_sector_file); - return 0; + goto err; } if (read(fdin, ss, statbuf.st_size) != statbuf.st_size) { perror("read"); error(_("error reading %s\n"), restore_sector_file); - return 0; + goto err; } fdout = open(dev, O_WRONLY); if (fdout < 0) { perror(dev); error(_("cannot open device %s for writing\n"), dev); - return 0; + goto err; } ss0 = ss; @@ -375,11 +383,11 @@ restore_sectors(char *dev) { while(ct--) { sno = chars_to_ulong(ss); if (!sseek(dev, fdout, sno)) - return 0; + goto err; if (write(fdout, ss+4, 512) != 512) { perror(dev); error(_("error writing sector %lu on %s\n"), sno, dev); - return 0; + goto err; } ss += 516; } @@ -387,7 +395,18 @@ restore_sectors(char *dev) { reread_disk_partition(dev, fdout); + close(fdin); + close(fdout); + return 1; + +err: + if (fdin >= 0) + close(fdin); + if (fdout >= 0) + close(fdout); + + return 0; } /* @@ -2708,6 +2727,11 @@ my_open (char *dev, int rw, int silent) { } static void +my_close (int fd) { + close(fd); +} + +static void do_list (char *dev, int silent) { int fd; struct disk_desc *z; @@ -2731,6 +2755,8 @@ do_list (char *dev, int silent) { else exit_status = 1; } + + my_close(fd); } static void @@ -2746,6 +2772,8 @@ do_geom (char *dev, int silent) { if (R.cylinders) printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"), dev, R.cylinders, R.heads, R.sectors); + + my_close(fd); } static void @@ -2777,6 +2805,8 @@ do_pt_geom (char *dev, int silent) { if (R.cylinders) printf(_("%s: %ld cylinders, %ld heads, %ld sectors/track\n"), dev, R.cylinders, R.heads, R.sectors); + + my_close(fd); } /* for compatibility with earlier fdisk: provide option -s */ @@ -2809,6 +2839,8 @@ do_size (char *dev, int silent) { printf("%llu\n", size); total_size += size; + + my_close(fd); } /* @@ -2899,6 +2931,8 @@ do_activate (char **av, int ac, char *arg) { if (i != 1) warn(_("You have %d active primary partitions. This does not matter for LILO,\n" "but the DOS MBR will only boot a disk with 1 active partition.\n"), i); + + my_close(fd); } static void @@ -2944,6 +2978,8 @@ do_unhide (char **av, int ac, char *arg) { warn(_("Done\n\n")); else exit_status = 1; + + my_close(fd); } static void @@ -2975,6 +3011,8 @@ do_change_id(char *dev, char *pnam, char *id) { warn(_("Done\n\n")); else exit_status = 1; + + my_close(fd); } static void @@ -2984,6 +3022,8 @@ do_reread(char *dev) { fd = my_open(dev, 0, 0); if (reread_ioctl(fd)) do_warn(_("This disk is currently in use.\n")); + + my_close(fd); } /* @@ -3088,5 +3128,6 @@ do_fdisk(char *dev){ sync(); /* superstition */ sleep(3); + my_close(fd); exit(exit_status); } -- 1.5.3.rc7 - To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html