From 0d94088e4e9e00f5ca9afdb8e68c94558fe23268 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 24 Nov 2018 03:52:35 +0900 Subject: [PATCH] import: use structured initializers --- src/import/curl-util.c | 23 +++++++++++++++-------- src/import/curl-util.h | 1 + src/import/export-raw.c | 13 ++++++++----- src/import/export-tar.c | 15 +++++++++------ src/import/import-raw.c | 29 ++++++++++++++++++----------- src/import/import-tar.c | 29 ++++++++++++++++++----------- src/import/importd.c | 14 ++++++++------ src/import/pull-job.c | 31 ++++++++++++++++++------------- src/import/pull-raw.c | 36 +++++++++++++++++++++++------------- src/import/pull-tar.c | 36 +++++++++++++++++++++++------------- 10 files changed, 141 insertions(+), 86 deletions(-) diff --git a/src/import/curl-util.c b/src/import/curl-util.c index 24011860f7..05b17c3c78 100644 --- a/src/import/curl-util.c +++ b/src/import/curl-util.c @@ -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; diff --git a/src/import/curl-util.h b/src/import/curl-util.h index 6626eefc56..a03e844030 100644 --- a/src/import/curl-util.h +++ b/src/import/curl-util.h @@ -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); diff --git a/src/import/export-raw.c b/src/import/export-raw.c index f3a34d40c5..04ad129ca1 100644 --- a/src/import/export-raw.c +++ b/src/import/export-raw.c @@ -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); diff --git a/src/import/export-tar.c b/src/import/export-tar.c index 9e7c0eaa9d..5acb09432a 100644 --- a/src/import/export-tar.c +++ b/src/import/export-tar.c @@ -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); diff --git a/src/import/import-raw.c b/src/import/import-raw.c index f62247cabd..e8adaae740 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -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); diff --git a/src/import/import-tar.c b/src/import/import-tar.c index 89c95af279..0399b03747 100644 --- a/src/import/import-tar.c +++ b/src/import/import-tar.c @@ -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); diff --git a/src/import/importd.c b/src/import/importd.c index 5d2a051533..2c88d353da 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -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; diff --git a/src/import/pull-job.c b/src/import/pull-job.c index 3d3a8713d7..0b7d9df32d 100644 --- a/src/import/pull-job.c +++ b/src/import/pull-job.c @@ -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); diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c index 96800ac8a6..2ceca68c07 100644 --- a/src/import/pull-raw.c +++ b/src/import/pull-raw.c @@ -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; diff --git a/src/import/pull-tar.c b/src/import/pull-tar.c index 56ec252e98..67ea8813e1 100644 --- a/src/import/pull-tar.c +++ b/src/import/pull-tar.c @@ -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;