#include "loopdev.h"
#include "fdiskP.h"
+#include "strutils.h"
/**
* SECTION: context
parent = cxt->parent;
+ INIT_LIST_HEAD(&cxt->wipes);
+
cxt->alignment_offset = parent->alignment_offset;
cxt->ask_cb = parent->ask_cb;
cxt->ask_data = parent->ask_data;
cxt->dev_model = NULL;
cxt->dev_model_probed = 0;
- free(cxt->dev_path);
- cxt->dev_path = NULL;
-
- if (parent->dev_path) {
- cxt->dev_path = strdup(parent->dev_path);
- if (!cxt->dev_path)
- return -ENOMEM;
- }
-
- INIT_LIST_HEAD(&cxt->wipes);
-
- return 0;
+ return strdup_between_structs(cxt, parent, dev_path);
}
/**
#endif
#include "fdiskP.h"
+#include "strutils.h"
/**
* SECTION: partition
static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
{
struct fdisk_partition *n = fdisk_new_partition();
+ int rc;
if (!n)
return NULL;
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);
- if (o->start_chs)
- n->start_chs = strdup(o->start_chs);
- if (o->end_chs)
- n->end_chs = strdup(o->end_chs);
+ 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);
}
/**
#include <ctype.h>
#include "fdiskP.h"
+#include "strutils.h"
/**
* SECTION: parttype
*/
int fdisk_parttype_set_name(struct fdisk_parttype *t, const char *str)
{
- char *p = NULL;
-
if (!t || !fdisk_parttype_is_allocated(t))
return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
-
- free(t->name);
- t->name = p;
- return 0;
+ return strdup_to_struct_member(t, name, str);
}
/**
*/
int fdisk_parttype_set_typestr(struct fdisk_parttype *t, const char *str)
{
- char *p = NULL;
-
if (!t || !fdisk_parttype_is_allocated(t))
return -EINVAL;
- if (str) {
- p = strdup(str);
- if (!p)
- return -ENOMEM;
- }
-
- free(t->typestr);
- t->typestr = p;
- return 0;
+ return strdup_to_struct_member(t, typestr, str);
}
/**
}
if (!fi) {
+ int rc;
+
DBG(SCRIPT, ul_debugobj(dp, "setting new header %s='%s'", name, data));
/* new header */
if (!fi)
return -ENOMEM;
INIT_LIST_HEAD(&fi->headers);
- fi->name = strdup(name);
- fi->data = strdup(data);
- if (!fi->data || !fi->name) {
+
+ rc = strdup_to_struct_member(fi, name, name);
+ if (!rc)
+ rc = strdup_to_struct_member(fi, data, data);
+ if (rc) {
fdisk_script_free_header(fi);
- return -ENOMEM;
+ return rc;
}
list_add_tail(&fi->headers, &dp->headers);
} else {