libfdisk: add generic label code

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2012-12-05 15:07:19 +01:00
parent 9475cc78ff
commit 8adbcf0ce9
6 changed files with 132 additions and 123 deletions

View File

@ -73,8 +73,6 @@ extern const struct fdisk_label gpt_label;
extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly);
extern int fdisk_dev_has_topology(struct fdisk_context *cxt);
extern int fdisk_dev_has_disklabel(struct fdisk_context *cxt);
extern int fdisk_dev_is_disklabel(struct fdisk_context *cxt, enum fdisk_labeltype l);
extern int fdisk_dev_sectsz_is_default(struct fdisk_context *cxt);
extern void fdisk_free_context(struct fdisk_context *cxt);
extern void fdisk_zeroize_firstsector(struct fdisk_context *cxt);
@ -82,10 +80,6 @@ extern int fdisk_context_force_sector_size(struct fdisk_context *cxt, sector_t s
extern int fdisk_context_set_user_geometry(struct fdisk_context *cxt,
unsigned int cylinders, unsigned int heads,
unsigned int sectors);
extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum);
extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, struct fdisk_parttype *t);
extern int fdisk_write_disklabel(struct fdisk_context *cxt);
extern int fdisk_verify_disklabel(struct fdisk_context *cxt);
extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name);
extern int fdisk_reset_alignment(struct fdisk_context *cxt);
extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum);

View File

@ -49,85 +49,6 @@ static const struct fdisk_label *labels[] =
&mac_label,
};
/**
* fdisk_write_disklabel:
* @cxt: fdisk context
*
* Write in-memory changes to disk
*
* Returns 0 on success, otherwise, a corresponding error.
*/
int fdisk_write_disklabel(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->write)
return -ENOSYS;
return cxt->label->write(cxt);
}
/**
* fdisk_verify_disklabel:
* @cxt: fdisk context
*
* Verifies the partition table.
*
* Returns 0.
*/
int fdisk_verify_disklabel(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->verify)
return -ENOSYS;
return cxt->label->verify(cxt);
}
/**
* fdisk_add_partition:
* @cxt: fdisk context
* @partnum: partition number to create
* @t: partition type to create or NULL for label-specific default
*
* Creates a new partition, with number @partnum and type @parttype.
*
* Returns 0.
*/
int fdisk_add_partition(struct fdisk_context *cxt, int partnum,
struct fdisk_parttype *t)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->part_add)
return -ENOSYS;
DBG(LABEL, dbgprint("adding new partition number %d", partnum));
cxt->label->part_add(cxt, partnum, t);
return 0;
}
/**
* fdisk_delete_partition:
* @cxt: fdisk context
* @partnum: partition number to delete
*
* Deletes a @partnum partition.
*
* Returns 0 on success, otherwise, a corresponding error.
*/
int fdisk_delete_partition(struct fdisk_context *cxt, int partnum)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->part_delete)
return -ENOSYS;
DBG(LABEL, dbgprint("deleting %s partition number %d",
cxt->label->name, partnum));
return cxt->label->part_delete(cxt, partnum);
}
static int __probe_labels(struct fdisk_context *cxt)
{
@ -474,28 +395,6 @@ int fdisk_reset_alignment(struct fdisk_context *cxt)
return rc;
}
/**
* fdisk_dev_has_disklabel:
* @cxt: fdisk context
*
* Returns: return 1 if there is label on the device.
*/
int fdisk_dev_has_disklabel(struct fdisk_context *cxt)
{
return cxt && cxt->disklabel != FDISK_DISKLABEL_ANY;
}
/**
* fdisk_dev_is_disklabel:
* @cxt: fdisk context
* @l: disklabel type
*
* Returns: return 1 if there is @l disklabel on the device.
*/
int fdisk_dev_is_disklabel(struct fdisk_context *cxt, enum fdisk_labeltype l)
{
return cxt && cxt->disklabel == l;
}
/**
* fdisk_create_disklabel:

View File

@ -10,6 +10,7 @@ libfdisk_la_SOURCES = \
\
libfdisk/src/init.c \
libfdisk/src/alignment.c \
libfdisk/src/label.c \
libfdisk/src/parttype.c

View File

@ -88,20 +88,6 @@ extern int fdisk_debug_mask;
typedef unsigned long long sector_t;
/*
* Supported partition table types (labels)
*/
enum fdisk_labeltype {
FDISK_DISKLABEL_DOS = 1,
FDISK_DISKLABEL_SUN = 2,
FDISK_DISKLABEL_SGI = 4,
FDISK_DISKLABEL_AIX = 8,
FDISK_DISKLABEL_OSF = 16,
FDISK_DISKLABEL_MAC = 32,
FDISK_DISKLABEL_GPT = 64,
FDISK_DISKLABEL_ANY = -1
};
/*
* Partition types
*/
@ -159,8 +145,6 @@ struct fdisk_context {
const struct fdisk_label *label;
};
#define fdisk_is_disklabel(c, x) fdisk_dev_is_disklabel(c, FDISK_DISKLABEL_ ## x)
/*
* Label specific operations
*/

105
libfdisk/src/label.c Normal file
View File

@ -0,0 +1,105 @@
#include "fdiskP.h"
/**
* fdisk_dev_has_disklabel:
* @cxt: fdisk context
*
* Returns: return 1 if there is label on the device.
*/
int fdisk_dev_has_disklabel(struct fdisk_context *cxt)
{
return cxt && cxt->disklabel != FDISK_DISKLABEL_ANY;
}
/**
* fdisk_dev_is_disklabel:
* @cxt: fdisk context
* @l: disklabel type
*
* Returns: return 1 if there is @l disklabel on the device.
*/
int fdisk_dev_is_disklabel(struct fdisk_context *cxt, enum fdisk_labeltype l)
{
return cxt && cxt->disklabel == l;
}
/**
* fdisk_write_disklabel:
* @cxt: fdisk context
*
* Write in-memory changes to disk
*
* Returns 0 on success, otherwise, a corresponding error.
*/
int fdisk_write_disklabel(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->write)
return -ENOSYS;
return cxt->label->write(cxt);
}
/**
* fdisk_verify_disklabel:
* @cxt: fdisk context
*
* Verifies the partition table.
*
* Returns 0.
*/
int fdisk_verify_disklabel(struct fdisk_context *cxt)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->verify)
return -ENOSYS;
return cxt->label->verify(cxt);
}
/**
* fdisk_add_partition:
* @cxt: fdisk context
* @partnum: partition number to create
* @t: partition type to create or NULL for label-specific default
*
* Creates a new partition, with number @partnum and type @parttype.
*
* Returns 0.
*/
int fdisk_add_partition(struct fdisk_context *cxt, int partnum,
struct fdisk_parttype *t)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->part_add)
return -ENOSYS;
DBG(LABEL, dbgprint("adding new partition number %d", partnum));
cxt->label->part_add(cxt, partnum, t);
return 0;
}
/**
* fdisk_delete_partition:
* @cxt: fdisk context
* @partnum: partition number to delete
*
* Deletes a @partnum partition.
*
* Returns 0 on success, otherwise, a corresponding error.
*/
int fdisk_delete_partition(struct fdisk_context *cxt, int partnum)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->part_delete)
return -ENOSYS;
DBG(LABEL, dbgprint("deleting %s partition number %d",
cxt->label->name, partnum));
return cxt->label->part_delete(cxt, partnum);
}

View File

@ -28,6 +28,20 @@ extern "C" {
struct fdisk_context;
struct fdisk_parttype;
/*
* Supported partition table types (labels)
*/
enum fdisk_labeltype {
FDISK_DISKLABEL_DOS = 1,
FDISK_DISKLABEL_SUN = 2,
FDISK_DISKLABEL_SGI = 4,
FDISK_DISKLABEL_AIX = 8,
FDISK_DISKLABEL_OSF = 16,
FDISK_DISKLABEL_MAC = 32,
FDISK_DISKLABEL_GPT = 64,
FDISK_DISKLABEL_ANY = -1
};
/* init.c */
extern void fdisk_init_debug(int mask);
@ -42,6 +56,18 @@ extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, cons
extern void fdisk_free_parttype(struct fdisk_parttype *type);
extern size_t fdisk_get_nparttypes(struct fdisk_context *cxt);
/* label.c */
extern int fdisk_dev_has_disklabel(struct fdisk_context *cxt);
extern int fdisk_dev_is_disklabel(struct fdisk_context *cxt, enum fdisk_labeltype l);
#define fdisk_is_disklabel(c, x) fdisk_dev_is_disklabel(c, FDISK_DISKLABEL_ ## x)
extern int fdisk_write_disklabel(struct fdisk_context *cxt);
extern int fdisk_verify_disklabel(struct fdisk_context *cxt);
extern int fdisk_add_partition(struct fdisk_context *cxt, int partnum, struct fdisk_parttype *t);
extern int fdisk_delete_partition(struct fdisk_context *cxt, int partnum);
#ifdef __cplusplus
}
#endif