svn commit: r185192 - in stable/7/sbin/geom: . class/part misc
Xin LI
delphij at FreeBSD.org
Sat Nov 22 16:16:10 PST 2008
Author: delphij
Date: Sun Nov 23 00:16:10 2008
New Revision: 185192
URL: http://svn.freebsd.org/changeset/base/185192
Log:
MFC r185038,185044,185046:
Automatically pad gptboot, style for include files,
use humanize_number() for consistency and reduce
code duplication.
Approved by: re (kib)
Modified:
stable/7/sbin/geom/ (props changed)
stable/7/sbin/geom/class/part/ (props changed)
stable/7/sbin/geom/class/part/Makefile
stable/7/sbin/geom/class/part/geom_part.c
stable/7/sbin/geom/misc/ (props changed)
Modified: stable/7/sbin/geom/class/part/Makefile
==============================================================================
--- stable/7/sbin/geom/class/part/Makefile Sun Nov 23 00:13:25 2008 (r185191)
+++ stable/7/sbin/geom/class/part/Makefile Sun Nov 23 00:16:10 2008 (r185192)
@@ -4,6 +4,8 @@
CLASS= part
+LDADD= -lutil
+
WARNS?= 4
.include <bsd.lib.mk>
Modified: stable/7/sbin/geom/class/part/geom_part.c
==============================================================================
--- stable/7/sbin/geom/class/part/geom_part.c Sun Nov 23 00:13:25 2008 (r185191)
+++ stable/7/sbin/geom/class/part/geom_part.c Sun Nov 23 00:16:10 2008 (r185192)
@@ -27,19 +27,21 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <unistd.h>
+#include <sys/stat.h>
+
+#include <assert.h>
#include <err.h>
+#include <errno.h>
#include <fcntl.h>
-#include <string.h>
-#include <strings.h>
#include <libgeom.h>
+#include <libutil.h>
#include <paths.h>
-#include <errno.h>
-#include <assert.h>
-#include <sys/stat.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
#include "core/geom.h"
#include "misc/subr.h"
@@ -202,21 +204,12 @@ find_provider(struct ggeom *gp, unsigned
}
static const char *
-fmtsize(long double rawsz)
+fmtsize(int64_t rawsz)
{
- static char buf[32];
- static const char *sfx[] = { "B", "KB", "MB", "GB", "TB" };
- long double sz;
- int sfxidx;
-
- sfxidx = 0;
- sz = (long double)rawsz;
- while (sfxidx < 4 && sz > 1099.0) {
- sz /= 1000;
- sfxidx++;
- }
+ static char buf[5];
- sprintf(buf, "%.1Lf%s", sz, sfx[sfxidx]);
+ humanize_number(buf, sizeof(buf), rawsz, "", HN_AUTOSCALE,
+ HN_B | HN_NOSPACE | HN_DECIMAL);
return (buf);
}
@@ -386,6 +379,8 @@ gpart_write_partcode(struct gctl_req *re
struct ggeom *gp;
struct gprovider *pp;
const char *s;
+ char *buf;
+ off_t bsize;
int error, fd;
s = gctl_get_ascii(req, "class");
@@ -421,8 +416,21 @@ gpart_write_partcode(struct gctl_req *re
errx(EXIT_FAILURE, "%s: not enough space", dsf);
if (lseek(fd, 0, SEEK_SET) != 0)
err(EXIT_FAILURE, "%s", dsf);
- if (write(fd, code, size) != size)
+
+ /*
+ * When writing to a disk device, the write must be
+ * sector aligned and not write to any partial sectors,
+ * so round up the buffer size to the next sector and zero it.
+ */
+ bsize = (size + pp->lg_sectorsize - 1) /
+ pp->lg_sectorsize * pp->lg_sectorsize;
+ buf = calloc(1, bsize);
+ if (buf == NULL)
+ err(EXIT_FAILURE, "%s", dsf);
+ bcopy(code, buf, size);
+ if (write(fd, buf, bsize) != bsize)
err(EXIT_FAILURE, "%s", dsf);
+ free(buf);
close(fd);
} else
errx(EXIT_FAILURE, "invalid partition index");
More information about the svn-src-stable-7
mailing list