Esempio n. 1
0
/**
 * fdisk_list_disklabel:
 * @cxt: fdisk context
 *
 * Lists details about disklabel, but no partitions.
 *
 * This function is based on fdisk_get_disklabel_item() and prints all label
 * specific information by ASK interface (FDISK_ASKTYPE_INFO, aka fdisk_info()).
 * The function requires enabled "details" by fdisk_enable_details().
 *
 * It's recommended to use fdisk_get_disklabel_item() if you need better
 * control on output and formatting.
 *
 * Returns: 0 on success, otherwise, a corresponding error.
 */
int fdisk_list_disklabel(struct fdisk_context *cxt)
{
	int id = 0, rc = 0;
	struct fdisk_labelitem item = { .id = id };

	if (!cxt || !cxt->label)
		return -EINVAL;

	if (!cxt->display_details)
		return 0;

	/* List all label items */
	do {
		/* rc: < 0 error, 0 success, 1 unknown item, 2 out of range */
		rc = fdisk_get_disklabel_item(cxt, id++, &item);
		if (rc != 0)
			continue;
		switch (item.type) {
		case 'j':
			fdisk_info(cxt, "%s: %ju", item.name, item.data.num64);
			break;
		case 's':
			if (item.data.str && item.name)
				fdisk_info(cxt, "%s: %s", item.name, item.data.str);
			break;
		}
		fdisk_reset_labelitem(&item);
	} while (rc == 0 || rc == 1);

	return rc < 0 ? rc : 0;
}
Esempio n. 2
0
/**
 * fdisk_unref_labelitem:
 * @li: label item
 *
 * Decrements reference counter, on zero the @li is automatically
 * deallocated.
 *
 * Since: 2.29
 */
void fdisk_unref_labelitem(struct fdisk_labelitem *li)
{
	if (!li)
		return;

	li->refcount--;
	if (li->refcount <= 0) {
		DBG(ITEM, ul_debugobj(li, "free"));
		fdisk_reset_labelitem(li);
		free(li);
	}
}
Esempio n. 3
0
/**
 * fdisk_get_disklabel_item:
 * @cxt: fdisk context
 * @id: item ID (FDISK_LABELITEM_* or {GPT,MBR,...}_LABELITEM_*)
 * @item: specifies and returns the item
 *
 * Note that @id is always in range 0..N. It's fine to use the function in loop
 * until it returns error or 2, the result in @item should be ignored when
 * function returns 1. Don't forget to use fdisk_reset_labelitem() or fdisk_unref_labelitem().
 *
 * Returns: 0 on success, < 0 on error, 1 on unsupported item, 2 @id out of range
 */
int fdisk_get_disklabel_item(struct fdisk_context *cxt, int id, struct fdisk_labelitem *item)
{
	if (!cxt || !cxt->label || !item)
		return -EINVAL;

	fdisk_reset_labelitem(item);
	item->id = id;
	DBG(CXT, ul_debugobj(cxt, "asking for disk %s item %d", cxt->label->name, item->id));

	if (!cxt->label->op->get_item)
		return -ENOSYS;

	return cxt->label->op->get_item(cxt, item);
}
Esempio n. 4
0
/**
 * fdisk_unref_labelitem:
 * @li: label item
 *
 * Decrements reference counter, on zero the @li is automatically
 * deallocated.
 *
 * Since: 2.29
 */
void fdisk_unref_labelitem(struct fdisk_labelitem *li)
{
	if (!li)
		return;

	/* me sure we do not use refcouting for static items */
	assert(li->refcount > 0);

	li->refcount--;
	if (li->refcount <= 0) {
		DBG(ITEM, ul_debugobj(li, "free"));
		fdisk_reset_labelitem(li);
		free(li);
	}
}
Esempio n. 5
0
/**
 * fdisk_get_disklabel_id:
 * @cxt: fdisk context
 * @id: returns pointer to allocated string (MBR Id or GPT dirk UUID)
 *
 * Returns: 0 on success, otherwise, a corresponding error.
 */
int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id)
{
	struct fdisk_labelitem item = FDISK_LABELITEM_INIT;
	int rc;

	if (!cxt || !cxt->label || !id)
		return -EINVAL;

	DBG(CXT, ul_debugobj(cxt, "asking for disk %s ID", cxt->label->name));

	rc = fdisk_get_disklabel_item(cxt, FDISK_LABELITEM_ID, &item);
	if (rc == 0) {
		*id = item.data.str;
		item.data.str = NULL;
	}
	fdisk_reset_labelitem(&item);
	if (rc > 0)
		rc = 0;
	return rc;
}