1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

cleanup: on stack structure instead of allocation

This commit is contained in:
Zdenek Kabelac 2021-03-04 18:09:58 +01:00
parent dac990ae03
commit f4200acac2

View File

@ -1040,62 +1040,53 @@ static int _text_vg_export(struct formatter *f,
int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp) int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp)
{ {
struct formatter *f;
int r; int r;
struct formatter f = {
.indent = 0,
.header = 1,
.out_with_comment = &_out_with_comment_file,
.nl = &_nl_file,
.data.fp = fp,
};
_init(); _init();
if (!(f = zalloc(sizeof(*f)))) if ((r = _text_vg_export(&f, vg, desc)))
return_0; r = !ferror(f.data.fp);
f->data.fp = fp;
f->indent = 0;
f->header = 1;
f->out_with_comment = &_out_with_comment_file;
f->nl = &_nl_file;
r = _text_vg_export(f, vg, desc);
if (r)
r = !ferror(f->data.fp);
free(f);
return r; return r;
} }
/* Returns amount of buffer used incl. terminating NUL */ /* Returns amount of buffer used incl. terminating NUL */
size_t text_vg_export_raw(struct volume_group *vg, const char *desc, char **buf, uint32_t *buf_size) size_t text_vg_export_raw(struct volume_group *vg, const char *desc, char **buf, uint32_t *buf_size)
{ {
struct formatter *f; size_t r;
size_t r = 0; struct formatter f = {
.indent = 0,
.header = 0,
.out_with_comment = &_out_with_comment_raw,
.nl = &_nl_raw,
.data.buf.size = 65536, /* Initial metadata limit */
};
_init(); _init();
if (!(f = zalloc(sizeof(*f)))) if (!(f.data.buf.start = zalloc(f.data.buf.size))) {
return_0;
f->data.buf.size = 65536; /* Initial metadata limit */
if (!(f->data.buf.start = zalloc(f->data.buf.size))) {
log_error("text_export buffer allocation failed"); log_error("text_export buffer allocation failed");
goto out; return 0;
} }
f->indent = 0; if (!_text_vg_export(&f, vg, desc)) {
f->header = 0; free(f.data.buf.start);
f->out_with_comment = &_out_with_comment_raw; return 0;
f->nl = &_nl_raw;
if (!_text_vg_export(f, vg, desc)) {
free(f->data.buf.start);
goto_out;
} }
r = f->data.buf.used + 1; r = f.data.buf.used + 1;
*buf = f->data.buf.start; *buf = f.data.buf.start;
if (buf_size) if (buf_size)
*buf_size = f->data.buf.size; *buf_size = f.data.buf.size;
out:
free(f);
return r; return r;
} }