Beispiel #1
0
/**
 * fdisk_set_partition:
 * @cxt: context
 * @partno: partition number (0 is the first partition)
 * @pa: new partition setting
 *
 * Modifies disklabel according to setting with in @pa.
 *
 * Returns: 0 on success, <0 on error.
 */
int fdisk_set_partition(struct fdisk_context *cxt, size_t partno,
			struct fdisk_partition *pa)
{
	struct fdisk_partition *xpa = pa;
	int rc;

	if (!cxt || !cxt->label || !pa)
		return -EINVAL;
	if (!cxt->label->op->set_part)
		return -ENOSYS;

	if (pa->resize || fdisk_partition_has_start(pa) || fdisk_partition_has_size(pa)) {
		xpa = __copy_partition(pa);
		xpa->movestart = 0;
		xpa->resize = 0;
		FDISK_INIT_UNDEF(xpa->size);
		FDISK_INIT_UNDEF(xpa->start);

		rc = recount_resize(cxt, partno, xpa, pa);
		if (rc)
			goto done;
	}

	DBG(CXT, ul_debugobj(cxt, "setting partition %zu %p (start=%ju, end=%ju, size=%ju)",
		    partno, xpa,
		    (uintmax_t) fdisk_partition_get_start(xpa),
		    (uintmax_t) fdisk_partition_get_end(xpa),
		    (uintmax_t) fdisk_partition_get_size(xpa)));

	rc = cxt->label->op->set_part(cxt, partno, xpa);
done:
	if (xpa != pa)
		fdisk_unref_partition(xpa);
	return rc;
}
Beispiel #2
0
/**
 * fdisk_get_partition:
 * @cxt: context
 * @partno: partition number (0 is the first partition)
 * @pa: returns data about partition
 *
 * Reads disklabel and fills in @pa with data about partition @n.
 *
 * Note that partno may address unused partition and then this function does
 * not fill anything to @pa.  See fdisk_is_partition_used(). If @pa points to
 * NULL then the function allocates a newly allocated fdisk_partition struct,
 * use fdisk_unref_partition() to deallocate.
 *
 * Returns: 0 on success, otherwise, a corresponding error.
 */
int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
			struct fdisk_partition **pa)
{
	int rc;
	struct fdisk_partition *np = NULL;

	if (!cxt || !cxt->label || !pa)
		return -EINVAL;
	if (!cxt->label->op->get_part)
		return -ENOSYS;
	if (!fdisk_is_partition_used(cxt, partno))
		return -EINVAL;

	if (!*pa) {
		np = *pa = fdisk_new_partition();
		if (!*pa)
			return -ENOMEM;
	} else
		fdisk_reset_partition(*pa);

	(*pa)->partno = partno;
	rc = cxt->label->op->get_part(cxt, partno, *pa);

	if (rc) {
		if (np) {
			fdisk_unref_partition(np);
			*pa = NULL;
		} else
			fdisk_reset_partition(*pa);
	} else
		(*pa)->size_explicit = 1;
	return rc;
}
Beispiel #3
0
/* Reads partition in tree-like order from scols
 */
static int partition_from_scols(struct fdisk_table *tb,
				struct libscols_line *ln)
{
	struct fdisk_partition *pa = scols_line_get_userdata(ln);

	fdisk_table_add_partition(tb, pa);
	fdisk_unref_partition(pa);

	if (scols_line_has_children(ln)) {
		struct libscols_line *chln;
		struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);

		if (!itr)
			return -EINVAL;
		while (scols_line_next_child(ln, itr, &chln) == 0)
			partition_from_scols(tb, chln);
	}
	return 0;
}
Beispiel #4
0
/**
 * fdisk_set_partition_type:
 * @cxt: fdisk context
 * @partnum: partition number
 * @t: new type
 *
 * Returns: 0 on success, < 0 on error.
 */
int fdisk_set_partition_type(struct fdisk_context *cxt,
			     size_t partnum,
			     struct fdisk_parttype *t)
{
	if (!cxt || !cxt->label || !t)
		return -EINVAL;


	if (cxt->label->op->set_part) {
		struct fdisk_partition *pa = fdisk_new_partition();
		int rc;

		if (!pa)
			return -ENOMEM;
		fdisk_partition_set_type(pa, t);

		DBG(CXT, ul_debugobj(cxt, "partition: %zd: set type", partnum));
		rc = cxt->label->op->set_part(cxt, partnum, pa);
		fdisk_unref_partition(pa);
		return rc;
	}

	return -ENOSYS;
}