Пример #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;
}
Пример #2
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;
	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;
	if (rc > 0)
		rc = 0;
	return rc;
}
Пример #3
0
static int test_listitems(struct fdisk_test *ts, int argc, char *argv[])
{
	const char *disk = argv[1];
	struct fdisk_context *cxt;
	struct fdisk_labelitem *item;
	int i = 0, rc;

	cxt = fdisk_new_context();
	item = fdisk_new_labelitem();

	fdisk_assign_device(cxt, disk, 1);

	do {
		rc = fdisk_get_disklabel_item(cxt, i++, item);
		switch (rc) {
		case 0:	/* success */
		{
			const char *name = fdisk_labelitem_get_name(item);
			const char *str;
			uint64_t num;

			if (fdisk_labelitem_is_string(item)
			    && fdisk_labelitem_get_data_string(item, &str) == 0)
				printf("%s: %s\n", name, str);
			else if (fdisk_labelitem_get_data_u64(item, &num) == 0)
				printf("%s: %ju\n", name, num);
			break;
		}
		case 1: /* item unsuported by label -- ignore */
			rc = 0;
			break;
		case 2:	/* end (out of range) */
			break;
		default: /* error */
			break;
		}
	} while (rc == 0);

	fdisk_unref_labelitem(item);
	fdisk_unref_context(cxt);
	return rc < 0 ? rc : 0;
}