Provide a simple getdisksize() api for gpt(8).

This commit is contained in:
christos 2014-12-29 16:27:43 +00:00
parent ccbdb0d177
commit 59069b7aab
2 changed files with 30 additions and 4 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: partutil.c,v 1.12 2013/04/13 22:08:57 jakllsch Exp $ */ /* $NetBSD: partutil.c,v 1.13 2014/12/29 16:27:43 christos Exp $ */
/*- /*-
* Copyright (c) 2006 The NetBSD Foundation, Inc. * Copyright (c) 2006 The NetBSD Foundation, Inc.
@ -30,9 +30,10 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: partutil.c,v 1.12 2013/04/13 22:08:57 jakllsch Exp $"); __RCSID("$NetBSD: partutil.c,v 1.13 2014/12/29 16:27:43 christos Exp $");
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h>
#include <sys/disklabel.h> #include <sys/disklabel.h>
#include <sys/disk.h> #include <sys/disk.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -135,8 +136,10 @@ getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
"DIOCGDINFO for disk device %s", s); "DIOCGDINFO for disk device %s", s);
} }
/* DIOCGDINFO didn't fail */ if (dkw == NULL)
return 0;
/* DIOCGDINFO didn't fail */
(void)memset(dkw, 0, sizeof(*dkw)); (void)memset(dkw, 0, sizeof(*dkw));
if (stat(s, &sb) == -1) if (stat(s, &sb) == -1)
@ -161,3 +164,23 @@ getdiskinfo(const char *s, int fd, const char *dt, struct disk_geom *geo,
return 0; return 0;
} }
int
getdisksize(const char *name, u_int *secsize, off_t *mediasize)
{
char buf[MAXPATHLEN];
struct disk_geom geo;
int fd, error;
if ((fd = opendisk(name, O_RDONLY, buf, sizeof(buf), 0)) == -1)
return -1;
error = getdiskinfo(name, fd, NULL, &geo, NULL);
close(fd);
if (error)
return error;
*secsize = geo.dg_secsize;
*mediasize = geo.dg_secsize * geo.dg_secperunit;
return 0;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: partutil.h,v 1.2 2008/04/28 20:23:08 martin Exp $ */ /* $NetBSD: partutil.h,v 1.3 2014/12/29 16:27:43 christos Exp $ */
/*- /*-
* Copyright (c) 2006 The NetBSD Foundation, Inc. * Copyright (c) 2006 The NetBSD Foundation, Inc.
@ -32,8 +32,11 @@
#define _PARTUTIL_H_ #define _PARTUTIL_H_
__BEGIN_DECLS __BEGIN_DECLS
struct dkwedge_info;
struct disk_geom;
int getdiskinfo(const char *, int, const char *, int getdiskinfo(const char *, int, const char *,
struct disk_geom *, struct dkwedge_info *); struct disk_geom *, struct dkwedge_info *);
int getdisksize(const char *, u_int *, off_t *);
__END_DECLS __END_DECLS
#endif /* _PARTUTIL_H_ */ #endif /* _PARTUTIL_H_ */