import.c revision 8af3cf74df03f7528f9e2605ec7896a5daf0f711
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek This file is part of systemd.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek (at your option) any later version.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
613b411c947635136637f8cdd66b94512f761eabLennart Poetteringstatic bool arg_force = false;
613b411c947635136637f8cdd66b94512f761eabLennart Poetteringstatic const char *arg_image_root = "/var/lib/machines";
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmekstatic ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
613b411c947635136637f8cdd66b94512f761eabLennart Poetteringstatic const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmekstatic void on_tar_finished(TarImport *import, int error, void *userdata) {
613b411c947635136637f8cdd66b94512f761eabLennart Poettering log_info("Operation completed successfully.");
613b411c947635136637f8cdd66b94512f761eabLennart Poetteringstatic int strip_tar_suffixes(const char *name, char **ret) {
613b411c947635136637f8cdd66b94512f761eabLennart Poettering const char *e;
613b411c947635136637f8cdd66b94512f761eabLennart Poetteringstatic int pull_tar(int argc, char *argv[], void *userdata) {
613b411c947635136637f8cdd66b94512f761eabLennart Poettering _cleanup_(tar_import_unrefp) TarImport *import = NULL;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering _cleanup_free_ char *l = NULL, *ll = NULL;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering log_error("URL '%s' is not valid.", url);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering return log_error_errno(r, "Failed get final component of URL: %m");
613b411c947635136637f8cdd66b94512f761eabLennart Poettering log_error("Local image name '%s' is not valid.", local);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering else if (r > 0) {
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek log_error_errno(EEXIST, "Image '%s' already exists.", local);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering log_info("Pulling '%s', saving as '%s'.", url, local);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering return log_error_errno(r, "Failed to allocate importer: %m");
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering r = tar_import_pull(import, url, local, arg_force, arg_verify);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering return log_error_errno(r, "Failed to pull image: %m");
if (error == 0)
static const char suffixes[] =
q = strdup(p);
return -ENOMEM;
const char *sfx;
bool changed = false;
changed = true;
if (!changed)
*ret = q;
q = NULL;
return -EINVAL;
local = l;
if (local) {
return log_oom();
return -EINVAL;
if (!arg_force) {
return -EEXIST;
if (error == 0)
if (!arg_dkr_index_url) {
return -EINVAL;
return -EINVAL;
if (tag) {
tag++;
return -EINVAL;
return -EINVAL;
if (local)
local++;
if (local) {
return -EINVAL;
if (!arg_force) {
case ARG_VERSION:
case ARG_FORCE:
arg_force = true;
case ARG_DKR_INDEX_URL:
return -EINVAL;
case ARG_IMAGE_ROOT:
case ARG_VERIFY:
if (arg_verify < 0) {
return -EINVAL;
return -EINVAL;
log_open();
goto finish;