From 1593d282961bd0e20d5048eeabbd75daf8d13981 Mon Sep 17 00:00:00 2001 From: krw Date: Tue, 20 May 2014 21:11:16 +0000 Subject: [PATCH] As suggested by guenther@ and millert@, replace seek+[read|write] with p[read|write]. Makes the code much clearer by eliminating extra error checking and verbiage. No intentional functional change. Tweaks by and ok guenther@ --- bin/df/df.c | 5 ++--- sbin/dump/traverse.c | 12 +++++------- sbin/fsck_ext2fs/utilities.c | 20 +++++--------------- sbin/fsck_ffs/utilities.c | 20 +++++--------------- sbin/ncheck_ffs/ncheck_ffs.c | 12 +++++------- sbin/quotacheck/quotacheck.c | 7 +++---- sbin/tunefs/tunefs.c | 23 +++++++---------------- 7 files changed, 32 insertions(+), 67 deletions(-) diff --git a/bin/df/df.c b/bin/df/df.c index 6ae73ca3f8c..7cfe89bf7f3 100644 --- a/bin/df/df.c +++ b/bin/df/df.c @@ -1,4 +1,4 @@ -/* $OpenBSD: df.c,v 1.50 2009/10/27 23:59:21 deraadt Exp $ */ +/* $OpenBSD: df.c,v 1.51 2014/05/20 21:11:16 krw Exp $ */ /* $NetBSD: df.c,v 1.21.2.1 1995/11/01 00:06:11 jtc Exp $ */ /* @@ -445,8 +445,7 @@ bread(int rfd, off_t off, void *buf, int cnt) { int nr; - (void)lseek(rfd, off, SEEK_SET); - if ((nr = read(rfd, buf, cnt)) != cnt) { + if ((nr = pread(rfd, buf, cnt, off)) != cnt) { /* Probably a dismounted disk if errno == EIO. */ if (errno != EIO) (void)fprintf(stderr, "\ndf: %qd: %s\n", diff --git a/sbin/dump/traverse.c b/sbin/dump/traverse.c index 736922d774c..e61f0bc6d7e 100644 --- a/sbin/dump/traverse.c +++ b/sbin/dump/traverse.c @@ -1,4 +1,4 @@ -/* $OpenBSD: traverse.c,v 1.28 2013/11/12 04:59:02 deraadt Exp $ */ +/* $OpenBSD: traverse.c,v 1.29 2014/05/20 21:11:16 krw Exp $ */ /* $NetBSD: traverse.c,v 1.17 1997/06/05 11:13:27 lukem Exp $ */ /*- @@ -803,9 +803,8 @@ bread(daddr_t blkno, char *buf, int size) int cnt, i; loop: - if (lseek(diskfd, ((off_t)blkno << dev_bshift), SEEK_SET) < 0) - msg("bread: lseek fails\n"); - if ((cnt = read(diskfd, buf, size)) == size) + if ((cnt = pread(diskfd, buf, size, (off_t)blkno << dev_bshift)) == + size) return; if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_ffs1_size)) { /* @@ -843,9 +842,8 @@ loop: */ memset(buf, 0, size); for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) { - if (lseek(diskfd, ((off_t)blkno << dev_bshift), SEEK_SET) < 0) - msg("bread: lseek2 fails!\n"); - if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize) + if ((cnt = pread(diskfd, buf, dev_bsize, + (off_t)blkno << dev_bshift)) == dev_bsize) continue; if (cnt == -1) { msg("read error from %s: %s: [sector %lld]: count=%ld\n", diff --git a/sbin/fsck_ext2fs/utilities.c b/sbin/fsck_ext2fs/utilities.c index 81f74d0521b..3fe2436fcec 100644 --- a/sbin/fsck_ext2fs/utilities.c +++ b/sbin/fsck_ext2fs/utilities.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utilities.c,v 1.19 2011/03/12 17:50:47 deraadt Exp $ */ +/* $OpenBSD: utilities.c,v 1.20 2014/05/20 21:11:16 krw Exp $ */ /* $NetBSD: utilities.c,v 1.6 2001/02/04 21:19:34 christos Exp $ */ /* @@ -286,19 +286,14 @@ bread(int fd, char *buf, daddr32_t blk, long size) offset = blk; offset *= dev_bsize; - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); - else if (read(fd, buf, (int)size) == size) + if (pread(fd, buf, size, offset) == size) return (0); rwerror("READ", blk); - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); errs = 0; memset(buf, 0, (size_t)size); printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:"); for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) { - if (read(fd, cp, (int)secsize) != secsize) { - (void)lseek(fd, offset + i + secsize, SEEK_SET); + if (pread(fd, cp, secsize, offset + i) != secsize) { if (secsize != dev_bsize && dev_bsize != 1) printf(" %ld (%ld),", (blk * dev_bsize + i) / secsize, @@ -323,19 +318,14 @@ bwrite(int fd, char *buf, daddr32_t blk, long size) return; offset = blk; offset *= dev_bsize; - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); - else if (write(fd, buf, (int)size) == size) { + if (pwrite(fd, buf, size, offset) == size) { fsmodified = 1; return; } rwerror("WRITE", blk); - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"); for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize) - if (write(fd, cp, (int)dev_bsize) != dev_bsize) { - (void)lseek(fd, offset + i + dev_bsize, SEEK_SET); + if (pwrite(fd, cp, dev_bsize, offset + i) != dev_bsize) { printf(" %ld,", blk + i / dev_bsize); } printf("\n"); diff --git a/sbin/fsck_ffs/utilities.c b/sbin/fsck_ffs/utilities.c index 6793460e5a9..475b83a76f8 100644 --- a/sbin/fsck_ffs/utilities.c +++ b/sbin/fsck_ffs/utilities.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utilities.c,v 1.43 2014/05/09 13:19:34 krw Exp $ */ +/* $OpenBSD: utilities.c,v 1.44 2014/05/20 21:11:16 krw Exp $ */ /* $NetBSD: utilities.c,v 1.18 1996/09/27 22:45:20 christos Exp $ */ /* @@ -339,19 +339,14 @@ bread(int fd, char *buf, daddr_t blk, long size) offset = blk; offset *= DEV_BSIZE; - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); - else if (read(fd, buf, (int)size) == size) + if (pread(fd, buf, size, offset) == size) return (0); rwerror("READ", blk); - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); errs = 0; memset(buf, 0, (size_t)size); printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:"); for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) { - if (read(fd, cp, (int)secsize) != secsize) { - (void)lseek(fd, offset + i + secsize, SEEK_SET); + if (pread(fd, cp, secsize, offset + i) != secsize) { if (secsize != DEV_BSIZE) printf(" %lld (%lld),", (long long)((blk * DEV_BSIZE + i) / @@ -378,19 +373,14 @@ bwrite(int fd, char *buf, daddr_t blk, long size) return; offset = blk; offset *= DEV_BSIZE; - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); - else if (write(fd, buf, (int)size) == size) { + if (pwrite(fd, buf, size, offset) == size) { fsmodified = 1; return; } rwerror("WRITE", blk); - if (lseek(fd, offset, SEEK_SET) < 0) - rwerror("SEEK", blk); printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"); for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) - if (write(fd, cp, (int)secsize) != secsize) { - (void)lseek(fd, offset + i + secsize, SEEK_SET); + if (pwrite(fd, cp, secsize, offset + i) != secsize) { if (secsize != DEV_BSIZE) printf(" %lld (%lld),", (long long)((blk * DEV_BSIZE + i) / diff --git a/sbin/ncheck_ffs/ncheck_ffs.c b/sbin/ncheck_ffs/ncheck_ffs.c index b51717ee857..847fbea06c8 100644 --- a/sbin/ncheck_ffs/ncheck_ffs.c +++ b/sbin/ncheck_ffs/ncheck_ffs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ncheck_ffs.c,v 1.41 2014/05/13 05:50:24 guenther Exp $ */ +/* $OpenBSD: ncheck_ffs.c,v 1.42 2014/05/20 21:11:16 krw Exp $ */ /*- * Copyright (c) 1995, 1996 SigmaSoft, Th. Lockert @@ -267,9 +267,8 @@ bread(daddr_t blkno, char *buf, int size) int cnt, i; loop: - if (lseek(diskfd, ((off_t)blkno << dev_bshift), SEEK_SET) < 0) - warnx("bread: lseek fails"); - if ((cnt = read(diskfd, buf, size)) == size) + if ((cnt = pread(diskfd, buf, size, (off_t)blkno << dev_bshift)) == + size) return; if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_ffs1_size)) { /* @@ -299,9 +298,8 @@ loop: */ memset(buf, 0, size); for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) { - if (lseek(diskfd, ((off_t)blkno << dev_bshift), SEEK_SET) < 0) - warnx("bread: lseek2 fails!"); - if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize) + if ((cnt = pread(diskfd, buf, (int)dev_bsize, + (off_t)blkno << dev_bshift)) == dev_bsize) continue; if (cnt == -1) { warnx("read error from %s: %s: [sector %lld]: " diff --git a/sbin/quotacheck/quotacheck.c b/sbin/quotacheck/quotacheck.c index 5530085434e..a74e7c9c07e 100644 --- a/sbin/quotacheck/quotacheck.c +++ b/sbin/quotacheck/quotacheck.c @@ -1,4 +1,4 @@ -/* $OpenBSD: quotacheck.c,v 1.32 2014/05/12 21:10:35 krw Exp $ */ +/* $OpenBSD: quotacheck.c,v 1.33 2014/05/20 21:11:16 krw Exp $ */ /* $NetBSD: quotacheck.c,v 1.12 1996/03/30 22:34:25 mark Exp $ */ /* @@ -735,7 +735,6 @@ freeinodebuf(void) void bread(daddr_t bno, char *buf, long cnt) { - if (lseek(fi, (off_t)bno * DEV_BSIZE, SEEK_SET) < 0 || - read(fi, buf, cnt) != cnt) - err(1, "bread failed on block %lld", (long long)bno); + if (pread(fi, buf, cnt, bno * DEV_BSIZE) != cnt) + err(1, "read failed on block %lld", (long long)bno); } diff --git a/sbin/tunefs/tunefs.c b/sbin/tunefs/tunefs.c index 89cd7b85e48..68df1a8548c 100644 --- a/sbin/tunefs/tunefs.c +++ b/sbin/tunefs/tunefs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tunefs.c,v 1.33 2014/05/12 12:16:53 krw Exp $ */ +/* $OpenBSD: tunefs.c,v 1.34 2014/05/20 21:11:16 krw Exp $ */ /* $NetBSD: tunefs.c,v 1.33 2005/01/19 20:46:16 xtraeme Exp $ */ /* @@ -292,26 +292,17 @@ getsb(struct fs *fs, const char *file) static void bwrite(daddr_t blk, char *buffer, int size, const char *file) { - off_t offset; - - offset = (off_t)blk * DEV_BSIZE; - if (lseek(fi, offset, SEEK_SET) == -1) - err(6, "%s: seeking to %lld", file, (long long)offset); - if (write(fi, buffer, size) != size) - err(7, "%s: writing %d bytes", file, size); + if (pwrite(fi, buffer, size, blk * DEV_BSIZE) != size) + err(7, "%s: writing %d bytes @ %lld", file, size, + (long long)(blk * DEV_BSIZE)); } static void bread(daddr_t blk, char *buffer, int cnt, const char *file) { - off_t offset; - int i; - - offset = (off_t)blk * DEV_BSIZE; - if (lseek(fi, offset, SEEK_SET) == -1) - err(4, "%s: seeking to %lld", file, (long long)offset); - if ((i = read(fi, buffer, cnt)) != cnt) - errx(5, "%s: short read", file); + if ((pread(fi, buffer, cnt, (off_t)blk * DEV_BSIZE)) != cnt) + errx(5, "%s: reading %d bytes @ %lld", file, cnt, + (long long)(blk * DEV_BSIZE)); } static int -- 2.20.1