static int _print_vg(struct formatter *f, struct volume_group *vg)
{
char buffer[4096];
+ char *tag_buffer = NULL;
if (!id_write_format(&vg->id, buffer, sizeof(buffer)))
return_0;
return_0;
if (!dm_list_empty(&vg->tags)) {
- if (!print_tags(&vg->tags, buffer, sizeof(buffer)))
+ if (!(tag_buffer = alloc_printed_tags(&vg->tags)))
return_0;
- outf(f, "tags = %s", buffer);
+ outf(f, "tags = %s", tag_buffer);
+ dm_free(tag_buffer);
}
if (vg->system_id && *vg->system_id)
struct pv_list *pvl;
struct physical_volume *pv;
char buffer[4096];
- char *buf;
+ char *buf, *tag_buffer = NULL;
const char *name;
outf(f, "physical_volumes {");
return_0;
if (!dm_list_empty(&pv->tags)) {
- if (!print_tags(&pv->tags, buffer, sizeof(buffer)))
+ if (!(tag_buffer = alloc_printed_tags(&pv->tags)))
return_0;
- outf(f, "tags = %s", buffer);
+ outf(f, "tags = %s", tag_buffer);
+ dm_free(tag_buffer);
}
outsize(f, pv->size, "dev_size = %" PRIu64, pv->size);
static int _print_segment(struct formatter *f, struct volume_group *vg,
int count, struct lv_segment *seg)
{
- char buffer[4096];
+ char *tag_buffer = NULL;
outf(f, "segment%u {", count);
_inc_indent(f);
outf(f, "type = \"%s\"", seg->segtype->name);
if (!dm_list_empty(&seg->tags)) {
- if (!print_tags(&seg->tags, buffer, sizeof(buffer)))
+ if (!(tag_buffer = alloc_printed_tags(&seg->tags)))
return_0;
- outf(f, "tags = %s", buffer);
+ outf(f, "tags = %s", tag_buffer);
+ dm_free(tag_buffer);
}
if (seg->segtype->ops->text_export &&
{
struct lv_segment *seg;
char buffer[4096];
+ char *tag_buffer = NULL;
int seg_count;
outnl(f);
return_0;
if (!dm_list_empty(&lv->tags)) {
- if (!print_tags(&lv->tags, buffer, sizeof(buffer)))
+ if (!(tag_buffer = alloc_printed_tags(&lv->tags)))
return_0;
- outf(f, "tags = %s", buffer);
+ outf(f, "tags = %s", tag_buffer);
+ dm_free(tag_buffer);
}
if (lv->alloc != ALLOC_INHERIT)
int print_flags(uint64_t status, int type, char *buffer, size_t size);
int read_flags(uint64_t *status, int type, struct config_value *cv);
-int print_tags(struct dm_list *tags, char *buffer, size_t size);
+char *alloc_printed_tags(struct dm_list *tags);
int read_tags(struct dm_pool *mem, struct dm_list *tags, struct config_value *cv);
int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp);
#include "str_list.h"
#include "lvm-string.h"
-int print_tags(struct dm_list *tags, char *buffer, size_t size)
+char *alloc_printed_tags(struct dm_list *tags)
{
struct str_list *sl;
int first = 1;
+ size_t size = 0;
+ char *buffer, *buf;
- if (!emit_to_buffer(&buffer, &size, "["))
- return_0;
+ dm_list_iterate_items(sl, tags)
+ /* '"' + tag + '"' + ',' + ' ' */
+ size += strlen(sl->str) + 4;
+ /* '[' + ']' + '\0' */
+ size += 3;
+
+ if (!(buffer = buf = dm_malloc(size))) {
+ log_error("Could not allocate memory for tag list buffer.");
+ return NULL;
+ }
+
+ if (!emit_to_buffer(&buf, &size, "["))
+ goto bad;
dm_list_iterate_items(sl, tags) {
if (!first) {
- if (!emit_to_buffer(&buffer, &size, ", "))
- return_0;
+ if (!emit_to_buffer(&buf, &size, ", "))
+ goto bad;
} else
first = 0;
- if (!emit_to_buffer(&buffer, &size, "\"%s\"", sl->str))
- return_0;
+ if (!emit_to_buffer(&buf, &size, "\"%s\"", sl->str))
+ goto bad;
}
- if (!emit_to_buffer(&buffer, &size, "]"))
- return_0;
+ if (!emit_to_buffer(&buf, &size, "]"))
+ goto bad;
- return 1;
+ return buffer;
+
+bad:
+ dm_free(buffer);
+ return_NULL;
}
int read_tags(struct dm_pool *mem, struct dm_list *tags, struct config_value *cv)