import: use structured initializers

This commit is contained in:
Yu Watanabe 2018-11-24 03:52:35 +09:00
parent c3e658004a
commit 0d94088e4e
10 changed files with 141 additions and 86 deletions

View File

@ -222,24 +222,31 @@ CurlGlue *curl_glue_unref(CurlGlue *g) {
int curl_glue_new(CurlGlue **glue, sd_event *event) {
_cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
_cleanup_(curl_multi_cleanupp) CURL *c = NULL;
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
int r;
g = new0(CurlGlue, 1);
if (!g)
return -ENOMEM;
if (event)
g->event = sd_event_ref(event);
e = sd_event_ref(event);
else {
r = sd_event_default(&g->event);
r = sd_event_default(&e);
if (r < 0)
return r;
}
g->curl = curl_multi_init();
if (!g->curl)
c = curl_multi_init();
if (!c)
return -ENOMEM;
g = new(CurlGlue, 1);
if (!g)
return -ENOMEM;
*g = (CurlGlue) {
.event = TAKE_PTR(e),
.curl = TAKE_PTR(c),
};
if (curl_multi_setopt(g->curl, CURLMOPT_SOCKETDATA, g) != CURLM_OK)
return -EINVAL;

View File

@ -35,4 +35,5 @@ int curl_header_strdup(const void *contents, size_t sz, const char *field, char
int curl_parse_http_time(const char *t, usec_t *ret);
DEFINE_TRIVIAL_CLEANUP_FUNC(CURL*, curl_easy_cleanup);
DEFINE_TRIVIAL_CLEANUP_FUNC(CURL*, curl_multi_cleanup);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct curl_slist*, curl_slist_free_all);

View File

@ -86,16 +86,19 @@ int raw_export_new(
assert(ret);
e = new0(RawExport, 1);
e = new(RawExport, 1);
if (!e)
return -ENOMEM;
e->output_fd = e->input_fd = -1;
e->on_finished = on_finished;
e->userdata = userdata;
*e = (RawExport) {
.output_fd = -1,
.input_fd = -1,
.on_finished = on_finished,
.userdata = userdata,
.last_percent = (unsigned) -1,
};
RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
e->last_percent = (unsigned) -1;
if (event)
e->event = sd_event_ref(event);

View File

@ -88,17 +88,20 @@ int tar_export_new(
assert(ret);
e = new0(TarExport, 1);
e = new(TarExport, 1);
if (!e)
return -ENOMEM;
e->output_fd = e->tar_fd = -1;
e->on_finished = on_finished;
e->userdata = userdata;
e->quota_referenced = (uint64_t) -1;
*e = (TarExport) {
.output_fd = -1,
.tar_fd = -1,
.on_finished = on_finished,
.userdata = userdata,
.quota_referenced = (uint64_t) -1,
.last_percent = (unsigned) -1,
};
RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
e->last_percent = (unsigned) -1;
if (event)
e->event = sd_event_ref(event);

View File

@ -94,26 +94,33 @@ int raw_import_new(
void *userdata) {
_cleanup_(raw_import_unrefp) RawImport *i = NULL;
_cleanup_free_ char *root = NULL;
bool grow;
int r;
assert(ret);
i = new0(RawImport, 1);
root = strdup(image_root ?: "/var/lib/machines");
if (!root)
return -ENOMEM;
grow = path_startswith(root, "/var/lib/machines");
i = new(RawImport, 1);
if (!i)
return -ENOMEM;
i->input_fd = i->output_fd = -1;
i->on_finished = on_finished;
i->userdata = userdata;
*i = (RawImport) {
.input_fd = -1,
.output_fd = -1,
.on_finished = on_finished,
.userdata = userdata,
.last_percent = (unsigned) -1,
.image_root = TAKE_PTR(root),
.grow_machine_directory = grow,
};
RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
i->last_percent = (unsigned) -1;
i->image_root = strdup(image_root ?: "/var/lib/machines");
if (!i->image_root)
return -ENOMEM;
i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
if (event)
i->event = sd_event_ref(event);

View File

@ -101,26 +101,33 @@ int tar_import_new(
void *userdata) {
_cleanup_(tar_import_unrefp) TarImport *i = NULL;
_cleanup_free_ char *root = NULL;
bool grow;
int r;
assert(ret);
i = new0(TarImport, 1);
root = strdup(image_root ?: "/var/lib/machines");
if (!root)
return -ENOMEM;
grow = path_startswith(root, "/var/lib/machines");
i = new(TarImport, 1);
if (!i)
return -ENOMEM;
i->input_fd = i->tar_fd = -1;
i->on_finished = on_finished;
i->userdata = userdata;
*i = (TarImport) {
.input_fd = -1,
.tar_fd = -1,
.on_finished = on_finished,
.userdata = userdata,
.last_percent = (unsigned) -1,
.image_root = TAKE_PTR(root),
.grow_machine_directory = grow,
};
RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
i->last_percent = (unsigned) -1;
i->image_root = strdup(image_root ?: "/var/lib/machines");
if (!i->image_root)
return -ENOMEM;
i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
if (event)
i->event = sd_event_ref(event);

View File

@ -146,15 +146,17 @@ static int transfer_new(Manager *m, Transfer **ret) {
if (r < 0)
return r;
t = new0(Transfer, 1);
t = new(Transfer, 1);
if (!t)
return -ENOMEM;
t->type = _TRANSFER_TYPE_INVALID;
t->log_fd = -1;
t->stdin_fd = -1;
t->stdout_fd = -1;
t->verify = _IMPORT_VERIFY_INVALID;
*t = (Transfer) {
.type = _TRANSFER_TYPE_INVALID,
.log_fd = -1,
.stdin_fd = -1,
.stdout_fd = -1,
.verify = _IMPORT_VERIFY_INVALID,
};
id = m->current_transfer_id + 1;

View File

@ -537,27 +537,32 @@ static int pull_job_progress_callback(void *userdata, curl_off_t dltotal, curl_o
int pull_job_new(PullJob **ret, const char *url, CurlGlue *glue, void *userdata) {
_cleanup_(pull_job_unrefp) PullJob *j = NULL;
_cleanup_free_ char *u = NULL;
assert(url);
assert(glue);
assert(ret);
j = new0(PullJob, 1);
u = strdup(url);
if (u)
return -ENOMEM;
j = new(PullJob, 1);
if (!j)
return -ENOMEM;
j->state = PULL_JOB_INIT;
j->disk_fd = -1;
j->userdata = userdata;
j->glue = glue;
j->content_length = (uint64_t) -1;
j->start_usec = now(CLOCK_MONOTONIC);
j->compressed_max = j->uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU; /* 64GB safety limit */
j->style = VERIFICATION_STYLE_UNSET;
j->url = strdup(url);
if (!j->url)
return -ENOMEM;
*j = (PullJob) {
.state = PULL_JOB_INIT,
.disk_fd = -1,
.userdata = userdata,
.glue = glue,
.content_length = (uint64_t) -1,
.start_usec = now(CLOCK_MONOTONIC),
.compressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
.uncompressed_max = 64LLU * 1024LLU * 1024LLU * 1024LLU, /* 64GB safety limit */
.style = VERIFICATION_STYLE_UNSET,
.url = TAKE_PTR(u),
};
*ret = TAKE_PTR(j);

View File

@ -115,36 +115,46 @@ int raw_pull_new(
RawPullFinished on_finished,
void *userdata) {
_cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_(raw_pull_unrefp) RawPull *i = NULL;
_cleanup_free_ char *root = NULL;
bool grow;
int r;
assert(ret);
i = new0(RawPull, 1);
if (!i)
root = strdup(image_root ?: "/var/lib/machines");
if (!root)
return -ENOMEM;
i->on_finished = on_finished;
i->userdata = userdata;
i->image_root = strdup(image_root ?: "/var/lib/machines");
if (!i->image_root)
return -ENOMEM;
i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
grow = path_startswith(root, "/var/lib/machines");
if (event)
i->event = sd_event_ref(event);
e = sd_event_ref(event);
else {
r = sd_event_default(&i->event);
r = sd_event_default(&e);
if (r < 0)
return r;
}
r = curl_glue_new(&i->glue, i->event);
r = curl_glue_new(&g, e);
if (r < 0)
return r;
i = new(RawPull, 1);
if (!i)
return -ENOMEM;
*i = (RawPull) {
.on_finished = on_finished,
.userdata = userdata,
.image_root = TAKE_PTR(root),
.grow_machine_directory = grow,
.event = TAKE_PTR(e),
.glue = TAKE_PTR(g),
};
i->glue->on_finished = pull_job_curl_on_finished;
i->glue->userdata = i;

View File

@ -108,36 +108,46 @@ int tar_pull_new(
TarPullFinished on_finished,
void *userdata) {
_cleanup_(curl_glue_unrefp) CurlGlue *g = NULL;
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_(tar_pull_unrefp) TarPull *i = NULL;
_cleanup_free_ char *root = NULL;
bool grow;
int r;
assert(ret);
i = new0(TarPull, 1);
if (!i)
root = strdup(image_root ?: "/var/lib/machines");
if (!root)
return -ENOMEM;
i->on_finished = on_finished;
i->userdata = userdata;
i->image_root = strdup(image_root ?: "/var/lib/machines");
if (!i->image_root)
return -ENOMEM;
i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
grow = path_startswith(root, "/var/lib/machines");
if (event)
i->event = sd_event_ref(event);
e = sd_event_ref(event);
else {
r = sd_event_default(&i->event);
r = sd_event_default(&e);
if (r < 0)
return r;
}
r = curl_glue_new(&i->glue, i->event);
r = curl_glue_new(&g, e);
if (r < 0)
return r;
i = new(TarPull, 1);
if (!i)
return -ENOMEM;
*i = (TarPull) {
.on_finished = on_finished,
.userdata = userdata,
.image_root = TAKE_PTR(root),
.grow_machine_directory = grow,
.event = TAKE_PTR(e),
.glue = TAKE_PTR(g),
};
i->glue->on_finished = pull_job_curl_on_finished;
i->glue->userdata = i;