static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
{
struct fdisk_partition *n = fdisk_new_partition();
+ int rc;
if (!n)
return NULL;
+
memcpy(n, o, sizeof(*n));
+
+ /* do not copy reference to lists, etc.*/
+ n->refcount = 1;
+ INIT_LIST_HEAD(&n->parts);
+
if (n->type)
fdisk_ref_parttype(n->type);
- if (o->name)
- n->name = strdup(o->name);
- if (o->uuid)
- n->uuid = strdup(o->uuid);
- if (o->attrs)
- n->attrs = strdup(o->attrs);
- if (o->fstype)
- n->fstype = strdup(o->fstype);
- if (o->fsuuid)
- n->fsuuid = strdup(o->fsuuid);
- if (o->fslabel)
- n->fslabel = strdup(o->fslabel);
+ rc = strdup_between_structs(n, o, name);
+ if (!rc)
+ rc = strdup_between_structs(n, o, uuid);
+ if (!rc)
+ rc = strdup_between_structs(n, o, attrs);
+ if (!rc)
+ rc = strdup_between_structs(n, o, fstype);
+ if (!rc)
+ rc = strdup_between_structs(n, o, fsuuid);
+ if (!rc)
+ rc = strdup_between_structs(n, o, fslabel);
+ if (!rc)
+ rc = strdup_between_structs(n, o, start_chs);
+ if (!rc)
+ rc = strdup_between_structs(n, o, end_chs);
+
+ if (rc) {
+ fdisk_unref_partition(n);
+ n = NULL;
+ }
return n;
}
int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
{
- char *p = NULL;
-
if (!pa)
return -EINVAL;
- if (name) {
- p = strdup(name);
- if (!p)
- return -ENOMEM;
- }
- free(pa->name);
- pa->name = p;
- return 0;
+ return strdup_to_struct_member(pa, name, name);
}
const char *fdisk_partition_get_name(struct fdisk_partition *pa)
int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
{
- char *p = NULL;
-
if (!pa)
return -EINVAL;
- if (uuid) {
- p = strdup(uuid);
- if (!p)
- return -ENOMEM;
- }
- free(pa->uuid);
- pa->uuid = p;
- return 0;
+ return strdup_to_struct_member(pa, uuid, uuid);
}
/**
*/
int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs)
{
- char *p = NULL;
-
if (!pa)
return -EINVAL;
- if (attrs) {
- p = strdup(attrs);
- if (!p)
- return -ENOMEM;
- }
- free(pa->attrs);
- pa->attrs = p;
- return 0;
+ return strdup_to_struct_member(pa, attrs, attrs);
}
/**
FDISK_INIT_UNDEF(size);
rc = fdisk_get_partitions(cxt, &tb);
- if (!rc)
+ if (!rc) {
+ /* For resize we do not follow grain to detect free-space, but
+ * we allow to resize with very small granulation. */
+ unsigned long org = cxt->grain;
+
+ cxt->grain = cxt->sector_size;
rc = fdisk_get_freespaces(cxt, &tb);
- if (rc)
+ cxt->grain = org;
+ }
+ if (rc) {
+ fdisk_unref_table(tb);
return rc;
+ }
fdisk_table_sort_partitions(tb, fdisk_partition_cmp_start);
goto erange;
}
- if (!FDISK_IS_UNDEF(size)) {
+ if (FDISK_IS_UNDEF(size)) {
DBG(PART, ul_debugobj(tpl, "resize: size unchanged (undefined)"));
}