aboutsummaryrefslogtreecommitdiffstats
path: root/sbin/geom/misc
diff options
context:
space:
mode:
authorPawel Jakub Dawidek <pjd@FreeBSD.org>2004-07-05 02:06:44 +0000
committerPawel Jakub Dawidek <pjd@FreeBSD.org>2004-07-05 02:06:44 +0000
commit10fa0ebe1fa639371be77acf5ce0292131808776 (patch)
tree6c55177b2ea40eeba47a5fc3ef6e64105b5244bb /sbin/geom/misc
parentfaf1b66d1d251a45850dae0ae5d6471c1c9ae460 (diff)
downloadsrc-10fa0ebe1fa639371be77acf5ce0292131808776.tar.gz
src-10fa0ebe1fa639371be77acf5ce0292131808776.zip
MFp4: gmirror(8) is coming soon, and we need g_metadata_read() there.
Notes
Notes: svn path=/head/; revision=131603
Diffstat (limited to 'sbin/geom/misc')
-rw-r--r--sbin/geom/misc/subr.c53
-rw-r--r--sbin/geom/misc/subr.h2
2 files changed, 55 insertions, 0 deletions
diff --git a/sbin/geom/misc/subr.c b/sbin/geom/misc/subr.c
index ae639b7a9017..a0525af6aff4 100644
--- a/sbin/geom/misc/subr.c
+++ b/sbin/geom/misc/subr.c
@@ -134,6 +134,59 @@ g_get_sectorsize(const char *name)
}
int
+g_metadata_read(const char *name, u_char *md, size_t size, const char *magic)
+{
+ struct std_metadata stdmd;
+ char path[MAXPATHLEN];
+ unsigned sectorsize;
+ off_t mediasize;
+ u_char *sector;
+ int error, fd;
+
+ pathgen(name, path, sizeof(path));
+ sector = NULL;
+ error = 0;
+
+ fd = open(path, O_RDONLY);
+ if (fd == -1)
+ return (errno);
+ mediasize = g_get_mediasize(name);
+ if (mediasize == 0) {
+ error = errno;
+ goto out;
+ }
+ sectorsize = g_get_sectorsize(name);
+ if (sectorsize == 0) {
+ error = errno;
+ goto out;
+ }
+ assert(sectorsize >= size);
+ sector = malloc(sectorsize);
+ if (sector == NULL) {
+ error = ENOMEM;
+ goto out;
+ }
+ if (pread(fd, sector, sectorsize, mediasize - sectorsize) !=
+ (ssize_t)sectorsize) {
+ error = errno;
+ goto out;
+ }
+ if (magic != NULL) {
+ std_metadata_decode(sector, &stdmd);
+ if (strcmp(stdmd.md_magic, magic) != 0) {
+ error = EINVAL;
+ goto out;
+ }
+ }
+ bcopy(sector, md, size);
+out:
+ if (sector != NULL)
+ free(sector);
+ close(fd);
+ return (error);
+}
+
+int
g_metadata_store(const char *name, u_char *md, size_t size)
{
char path[MAXPATHLEN];
diff --git a/sbin/geom/misc/subr.h b/sbin/geom/misc/subr.h
index f88af01bddb2..ee319a983bc5 100644
--- a/sbin/geom/misc/subr.h
+++ b/sbin/geom/misc/subr.h
@@ -33,6 +33,8 @@ unsigned g_lcm(unsigned a, unsigned b);
off_t g_get_mediasize(const char *name);
unsigned g_get_sectorsize(const char *name);
+int g_metadata_read(const char *name, u_char *md, size_t size,
+ const char *magic);
int g_metadata_store(const char *name, u_char *md, size_t size);
int g_metadata_clear(const char *name, const char *magic);