mkswap, readahead: stop using fdlength, it is reported to be unreliable
authorDenis Vlasenko <vda.linux@googlemail.com>
Sun, 27 Jan 2008 23:41:34 +0000 (23:41 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sun, 27 Jan 2008 23:41:34 +0000 (23:41 -0000)
libbb/xfuncs.c
miscutils/readahead.c
util-linux/mkswap.c

index 25c36bd0743c8e390bcbdc143792ba226787720a..445e077170e162d6bfcc64a01a211c800d77414d 100644 (file)
@@ -475,6 +475,7 @@ void xsetuid(uid_t uid)
 }
 
 // Return how long the file at fd is, if there's any way to determine it.
+#ifdef UNUSED
 off_t fdlength(int fd)
 {
        off_t bottom = 0, top = 0, pos;
@@ -513,6 +514,7 @@ off_t fdlength(int fd)
 
        return pos + 1;
 }
+#endif
 
 int bb_putchar(int ch)
 {
index 7b375cffff7c31f7df5b7b8f52f7f674420b77c5..fb71ce85f010b3f3097b265e09e8f7ba383b58ee 100644 (file)
@@ -22,9 +22,16 @@ int readahead_main(int argc, char **argv)
        while (*++argv) {
                int fd = open_or_warn(*argv, O_RDONLY);
                if (fd >= 0) {
-                       int r = readahead(fd, 0, fdlength(fd));
+                       off_t len;
+                       int r;
+
+                       /* fdlength was reported to be unreliable - use seek */
+                       len = xlseek(fd, 0, SEEK_END);
+                       xlseek(fd, 0, SEEK_SET);
+                       r = readahead(fd, 0, len);
                        close(fd);
-                       if (r >= 0) continue;
+                       if (r >= 0)
+                               continue;
                }
                retval = EXIT_FAILURE;
        }
index 8e1fbc38460d666223e0ed60b1a0c8acbd601c63..f047cce26a2761e275e923cebde7d6ecc079bbbb 100644 (file)
@@ -64,9 +64,11 @@ int mkswap_main(int argc, char **argv)
        // Figure out how big the device is and announce our intentions.
 
        fd = xopen(argv[1], O_RDWR);
-       len = fdlength(fd);
+       /* fdlength was reported to be unreliable - use seek */
+       len = xlseek(fd, 0, SEEK_END);
+       xlseek(fd, 0, SEEK_SET);
        pagesize = getpagesize();
-       printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
+       printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
                        len - pagesize);
        mkswap_selinux_setcontext(fd, argv[1]);