]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mkswap: various minor improvement
authorSami Kerola <kerolasa@iki.fi>
Sun, 2 Nov 2014 20:26:31 +0000 (20:26 +0000)
committerKarel Zak <kzak@redhat.com>
Fri, 7 Nov 2014 12:21:06 +0000 (13:21 +0100)
Use correct data type in page_bad(), and add information to error message
how many bad pages were seen.

In check_blocks() move initialization to variable introduction, fix typo,
and avoid memset() when array initializer can do the job.

In main() use correct initializer for pointer.  Move swap file specific
actions to same location, and add warning when request too --check is
dismissed by command.  At the end remove momory leak.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
disk-utils/mkswap.c

index 280d8902501a2449b50c7e5d1456ebf99c93f212..71f62b471e114f7c83ae05c7bc1d6bd9964cbb28 100644 (file)
@@ -167,13 +167,13 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
 }
 
 static void
-page_bad(struct mkswap_control *ctl, int page)
+page_bad(struct mkswap_control *ctl, unsigned int page)
 {
        struct swap_header_v1_2 *p = (struct swap_header_v1_2 *)ctl->signature_page;
        const unsigned long max_badpages = (ctl->pagesize - 1024 - 128 * sizeof(int) - 10) / sizeof(int);
 
        if (ctl->badpages == max_badpages)
-               errx(EXIT_FAILURE, _("too many bad pages"));
+               errx(EXIT_FAILURE, _("too many bad pages: %lu"), max_badpages);
        p->badpages[ctl->badpages] = page;
        ctl->badpages++;
 }
@@ -181,12 +181,11 @@ page_bad(struct mkswap_control *ctl, int page)
 static void
 check_blocks(struct mkswap_control *ctl)
 {
-       unsigned int current_page;
+       unsigned int current_page = 0;
        int do_seek = 1;
        char *buffer;
 
        buffer = xmalloc(ctl->pagesize);
-       current_page = 0;
        while (current_page < ctl->nr_pages) {
                ssize_t rc;
 
@@ -265,14 +264,13 @@ wipe_device(struct mkswap_control *ctl)
 
        if (zap) {
                /*
-                * Wipe boodbits
+                * Wipe bootbits
                 */
-               char buf[1024];
+               char buf[1024] = { '\0' };
 
                if (lseek(ctl->fd, 0, SEEK_SET) != 0)
                        errx(EXIT_FAILURE, _("unable to rewind swap-device"));
 
-               memset(buf, 0, sizeof(buf));
                if (write_all(ctl->fd, buf, sizeof(buf)))
                        errx(EXIT_FAILURE, _("unable to erase bootbits sectors"));
 #ifdef HAVE_LIBBLKID
@@ -317,7 +315,7 @@ main(int argc, char **argv) {
        unsigned long long sz;
        off_t offset;
        int version = SWAP_VERSION;
-       char *block_count = 0;
+       char *block_count = NULL;
 #ifdef HAVE_LIBUUID
        const char *opt_uuid = NULL;
        uuid_t uuid_dat;
@@ -435,16 +433,19 @@ main(int argc, char **argv) {
                err(EXIT_FAILURE, _("stat failed %s"), ctl.device_name);
        if (S_ISBLK(statbuf.st_mode))
                ctl.fd = open(ctl.device_name, O_RDWR | O_EXCL);
-       else
+       else {
+               if (ctl.check) {
+                       ctl.check = 0;
+                       warnx(_("warning: checking bad blocks from swap file is not supported: %s"),
+                               ctl.device_name);
+               }
                ctl.fd = open(ctl.device_name, O_RDWR);
+       }
        if (ctl.fd < 0)
                err(EXIT_FAILURE, _("cannot open %s"), ctl.device_name);
-
-       if (!S_ISBLK(statbuf.st_mode))
-               ctl.check = 0;
-       else if (blkdev_is_misaligned(ctl.fd))
-               warnx(_("warning: %s is misaligned"), ctl.device_name);
-
+       if (S_ISBLK(statbuf.st_mode))
+               if (blkdev_is_misaligned(ctl.fd))
+                       warnx(_("warning: %s is misaligned"), ctl.device_name);
        if (ctl.check)
                check_blocks(&ctl);
 
@@ -473,7 +474,7 @@ main(int argc, char **argv) {
                err(EXIT_FAILURE,
                        _("%s: unable to write signature page"),
                        ctl.device_name);
-
+       free(ctl.signature_page);
 #ifdef HAVE_LIBSELINUX
        if (S_ISREG(statbuf.st_mode) && is_selinux_enabled() > 0) {
                security_context_t context_string;