diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2015-03-30 17:11:50 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2015-03-30 17:11:50 +0000 |
commit | 541f3b912c47a0fb6b5fb9f1b1e2f5748dddee53 (patch) | |
tree | 9f96081a94ff25d4e3946f5ef536a3de2a6ddcad | |
parent | 686342425694c618617627d676c3bc27a01f6c98 (diff) |
Even better -- readsector() and writesector() become DISK_readsector() and
DISK_writesector() and live in disk.[ch].
-rw-r--r-- | sbin/fdisk/disk.c | 67 | ||||
-rw-r--r-- | sbin/fdisk/disk.h | 4 | ||||
-rw-r--r-- | sbin/fdisk/mbr.c | 16 | ||||
-rw-r--r-- | sbin/fdisk/misc.c | 64 | ||||
-rw-r--r-- | sbin/fdisk/misc.h | 4 |
5 files changed, 79 insertions, 76 deletions
diff --git a/sbin/fdisk/disk.c b/sbin/fdisk/disk.c index de9fd77c85e..c2a8045d21a 100644 --- a/sbin/fdisk/disk.c +++ b/sbin/fdisk/disk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.c,v 1.46 2015/03/27 16:06:00 krw Exp $ */ +/* $OpenBSD: disk.c,v 1.47 2015/03/30 17:11:49 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -24,6 +24,7 @@ #include <sys/stat.h> #include <sys/disklabel.h> #include <err.h> +#include <errno.h> #include <util.h> #include <stdio.h> #include <stdlib.h> @@ -108,3 +109,67 @@ DISK_printgeometry(char *units) return (0); } + +/* + * Read the sector at 'where' from the file descriptor 'fd' into newly + * calloc'd memory. Return a pointer to the memory if it contains the + * requested data, or NULL if it does not. + * + * The caller must free() the memory it gets. + */ +char * +DISK_readsector(int fd, off_t where) +{ + int secsize; + char *secbuf; + ssize_t len; + off_t off; + + secsize = dl.d_secsize; + + where *= secsize; + off = lseek(fd, where, SEEK_SET); + if (off != where) + return (NULL); + + secbuf = calloc(1, secsize); + if (secbuf == NULL) + return (NULL); + + len = read(fd, secbuf, secsize); + if (len == -1 || len != secsize) { + free(secbuf); + return (NULL); + } + + return (secbuf); +} + +/* + * Write the sector-sized 'secbuf' to the sector 'where' on the file + * descriptor 'fd'. Return 0 if the write works. Return -1 and set + * errno if the write fails. + */ +int +DISK_writesector(int fd, char *secbuf, off_t where) +{ + int secsize; + ssize_t len; + off_t off; + + len = -1; + secsize = dl.d_secsize; + + where *= secsize; + off = lseek(fd, where, SEEK_SET); + if (off == where) + len = write(fd, secbuf, secsize); + + if (len == -1 || len != secsize) { + /* short read or write */ + errno = EIO; + return (-1); + } + + return (0); +} diff --git a/sbin/fdisk/disk.h b/sbin/fdisk/disk.h index 64cb9a92024..c77dc16c2f6 100644 --- a/sbin/fdisk/disk.h +++ b/sbin/fdisk/disk.h @@ -1,4 +1,4 @@ -/* $OpenBSD: disk.h,v 1.18 2015/03/16 23:51:50 krw Exp $ */ +/* $OpenBSD: disk.h,v 1.19 2015/03/30 17:11:49 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -30,6 +30,8 @@ struct disk { int DISK_open(char *, int); void DISK_getlabelgeometry(void); int DISK_printgeometry(char *); +char *DISK_readsector(int, off_t); +int DISK_writesector(int, char *, off_t); extern struct disk disk; extern struct disklabel dl; diff --git a/sbin/fdisk/mbr.c b/sbin/fdisk/mbr.c index 87f201e02f3..352635a5e75 100644 --- a/sbin/fdisk/mbr.c +++ b/sbin/fdisk/mbr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mbr.c,v 1.50 2015/03/29 21:16:39 krw Exp $ */ +/* $OpenBSD: mbr.c,v 1.51 2015/03/30 17:11:49 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -173,7 +173,7 @@ MBR_read(int fd, off_t where, struct dos_mbr *dos_mbr) { char *secbuf; - secbuf = readsector(fd, where); + secbuf = DISK_readsector(fd, where); if (secbuf == NULL) return (-1); @@ -188,7 +188,7 @@ MBR_write(int fd, off_t where, struct dos_mbr *dos_mbr) { char *secbuf; - secbuf = readsector(fd, where); + secbuf = DISK_readsector(fd, where); if (secbuf == NULL) return (-1); @@ -197,7 +197,7 @@ MBR_write(int fd, off_t where, struct dos_mbr *dos_mbr) * write the sector back to "disk". */ memcpy(secbuf, dos_mbr, sizeof(*dos_mbr)); - writesector(fd, secbuf, where); + DISK_writesector(fd, secbuf, where); ioctl(fd, DIOCRLDINFO, 0); free(secbuf); @@ -250,25 +250,25 @@ MBR_zapgpt(int fd, struct dos_mbr *dos_mbr, uint64_t lastsec) (dos_parts[i].dp_typ == DOSPTYP_EFISYS)) return; - secbuf = readsector(fd, GPTSECTOR); + secbuf = DISK_readsector(fd, GPTSECTOR); if (secbuf == NULL) return; memcpy(&sig, secbuf, sizeof(sig)); if (letoh64(sig) == GPTSIGNATURE) { memset(secbuf, 0, sizeof(sig)); - writesector(fd, secbuf, GPTSECTOR); + DISK_writesector(fd, secbuf, GPTSECTOR); } free(secbuf); - secbuf = readsector(fd, lastsec); + secbuf = DISK_readsector(fd, lastsec); if (secbuf == NULL) return; memcpy(&sig, secbuf, sizeof(sig)); if (letoh64(sig) == GPTSIGNATURE) { memset(secbuf, 0, sizeof(sig)); - writesector(fd, secbuf, lastsec); + DISK_writesector(fd, secbuf, lastsec); } free(secbuf); } diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index cc851825768..4b32da58391 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.50 2015/03/29 21:16:39 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.51 2015/03/30 17:11:49 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -22,7 +22,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <err.h> #include <errno.h> @@ -365,64 +364,3 @@ crc32(const u_char *buf, const u_int32_t size) return ~crc; } - -/* - * Read the sector at 'where' from the file descriptor 'fd' into newly - * calloc'd memory. Return a pointer to the memory if it contains the - * requested data, or NULL if it does not. - * - * The caller must free() the memory it gets. - */ -char * -readsector(int fd, off_t where) -{ - const int secsize = unit_types[SECTORS].conversion; - char *secbuf; - ssize_t len; - off_t off; - - where *= secsize; - off = lseek(fd, where, SEEK_SET); - if (off != where) - return (NULL); - - secbuf = calloc(1, secsize); - if (secbuf == NULL) - return (NULL); - - len = read(fd, secbuf, secsize); - if (len == -1 || len != secsize) { - free(secbuf); - return (NULL); - } - - return (secbuf); -} - -/* - * Write the sector-sized 'secbuf' to the sector 'where' on the file - * descriptor 'fd'. Return 0 if the write works. Return -1 and set - * errno if the write fails. - */ -int -writesector(int fd, char *secbuf, off_t where) -{ - const int secsize = unit_types[SECTORS].conversion; - ssize_t len; - off_t off; - - len = -1; - - where *= secsize; - off = lseek(fd, where, SEEK_SET); - if (off == where) - len = write(fd, secbuf, secsize); - - if (len == -1 || len != secsize) { - /* short read or write */ - errno = EIO; - return (-1); - } - - return (0); -} diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index e451e50d9b8..04d95c5865d 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.29 2015/03/29 21:16:39 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.30 2015/03/30 17:11:49 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -36,7 +36,5 @@ char *ask_string(const char *, const char *); int ask_yn(const char *); u_int64_t getuint64(char *, u_int64_t, u_int64_t); u_int32_t crc32(const u_char *, const u_int32_t); -char *readsector(int, off_t); -int writesector(int, char *, off_t); #endif /* _MISC_H */ |