import-dkr.c revision ff2670ad11a1a29d483b7a246c95b159ddc1137c
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "set.h"
#include "json.h"
#include "strv.h"
#include "btrfs-util.h"
#include "utf8.h"
#include "mkdir.h"
#include "curl-util.h"
#include "aufs-util.h"
#include "import-util.h"
#include "import-job.h"
#include "import-dkr.h"
struct DkrImport {
char *index_url;
char *image_root;
char *name;
char *tag;
char *id;
char *response_token;
char **response_registries;
char **ancestry;
unsigned current_ancestry;
void *userdata;
char *local;
bool force_local;
char *temp_path;
char *final_path;
};
#define PROTOCOL_PREFIX "https://"
#define LAYERS_MAX 2048
static void dkr_import_job_on_finished(ImportJob *j);
if (!i)
return NULL;
if (i->tar_pid > 1) {
}
import_job_unref(i->tags_job);
import_job_unref(i->json_job);
curl_glue_unref(i->glue);
sd_event_unref(i->event);
if (i->temp_path) {
(void) btrfs_subvol_remove(i->temp_path);
(void) rm_rf_dangerous(i->temp_path, false, true, false);
}
free(i->response_token);
free(i->response_registries);
free(i->final_path);
free(i->image_root);
free(i);
return NULL;
}
int dkr_import_new(
const char *index_url,
const char *image_root,
void *userdata) {
char *e;
int r;
if (!http_url_is_valid(index_url))
return -EINVAL;
if (!i)
return -ENOMEM;
i->on_finished = on_finished;
if (!i->image_root)
return -ENOMEM;
if (!i->index_url)
return -ENOMEM;
if (e)
*e = 0;
if (event)
else {
r = sd_event_default(&i->event);
if (r < 0)
return r;
}
if (r < 0)
return r;
*ret = i;
i = NULL;
return 0;
}
union json_value v = {};
void *json_state = NULL;
const char *p;
int t;
if (size <= 0)
return -EBADMSG;
return -EBADMSG;
if (!buf)
return -ENOMEM;
p = buf;
if (t < 0)
return t;
if (t != JSON_STRING)
return -EBADMSG;
if (t < 0)
return t;
if (t != JSON_END)
return -EBADMSG;
if (!dkr_id_is_valid(id))
return -EBADMSG;
return 0;
}
void *json_state = NULL;
const char *p;
enum {
} state = STATE_BEGIN;
_cleanup_strv_free_ char **l = NULL;
if (size <= 0)
return -EBADMSG;
return -EBADMSG;
if (!buf)
return -ENOMEM;
p = buf;
for (;;) {
_cleanup_free_ char *str;
union json_value v = {};
int t;
if (t < 0)
return t;
switch (state) {
case STATE_BEGIN:
if (t == JSON_ARRAY_OPEN)
state = STATE_ITEM;
else
return -EBADMSG;
break;
case STATE_ITEM:
if (t == JSON_STRING) {
if (!dkr_id_is_valid(str))
return -EBADMSG;
if (n+1 > LAYERS_MAX)
return -EFBIG;
return -ENOMEM;
l[n++] = str;
l[n] = NULL;
state = STATE_COMMA;
} else if (t == JSON_ARRAY_CLOSE)
else
return -EBADMSG;
break;
case STATE_COMMA:
if (t == JSON_COMMA)
state = STATE_ITEM;
else if (t == JSON_ARRAY_CLOSE)
else
return -EBADMSG;
break;
case STATE_END:
if (t == JSON_END) {
if (strv_isempty(l))
return -EBADMSG;
if (!strv_is_uniq(l))
return -EBADMSG;
l = strv_reverse(l);
*ret = l;
l = NULL;
return 0;
} else
return -EBADMSG;
}
}
}
static const char *dkr_import_current_layer(DkrImport *i) {
assert(i);
if (strv_isempty(i->ancestry))
return NULL;
return i->ancestry[i->current_ancestry];
}
static const char *dkr_import_current_base_layer(DkrImport *i) {
assert(i);
if (strv_isempty(i->ancestry))
return NULL;
if (i->current_ancestry <= 0)
return NULL;
}
const char *t;
assert(i);
assert(j);
if (i->response_token)
else
t = HEADER_TOKEN " true";
if (!j->request_header)
return -ENOMEM;
return 0;
}
static bool dkr_import_is_done(DkrImport *i) {
assert(i);
assert(i->images_job);
return false;
return false;
return false;
return false;
return false;
if (dkr_import_current_layer(i))
return false;
return true;
}
static int dkr_import_make_local_copy(DkrImport *i) {
int r;
assert(i);
if (!i->local)
return 0;
if (!i->final_path) {
if (!i->final_path)
return log_oom();
}
if (r < 0)
return r;
return 0;
}
static int dkr_import_job_on_open_disk(ImportJob *j) {
const char *base;
DkrImport *i;
int r;
assert(j);
i = j->userdata;
assert(i->final_path);
if (r < 0)
return log_oom();
if (base) {
const char *base_path;
} else
r = btrfs_subvol_make(i->temp_path);
if (r < 0)
if (i->tar_pid < 0)
if (i->tar_pid == 0) {
int null_fd;
/* Child */
}
if (pipefd[0] != STDIN_FILENO)
safe_close(pipefd[0]);
if (null_fd < 0) {
}
}
if (null_fd != STDOUT_FILENO)
}
return 0;
}
static int dkr_import_pull_layer(DkrImport *i) {
int r;
assert(i);
assert(!i->final_path);
for (;;) {
if (!layer)
return 0; /* no more layers */
if (!path)
return log_oom();
break;
}
i->current_ancestry++;
}
i->final_path = path;
if (r < 0)
return log_error_errno(r, "Failed to allocate layer job: %m");
r = dkr_import_add_token(i, i->layer_job);
if (r < 0)
return log_oom();
r = import_job_begin(i->layer_job);
if (r < 0)
return log_error_errno(r, "Failed to start layer job: %m");
return 0;
}
static void dkr_import_job_on_finished(ImportJob *j) {
DkrImport *i;
int r;
assert(j);
i = j->userdata;
if (j->error != 0) {
if (j == i->images_job)
else if (j == i->tags_job)
else if (j == i->ancestry_job)
else if (j == i->json_job)
else
r = j->error;
goto finish;
}
if (i->images_job == j) {
const char *url;
assert(!i->ancestry_job);
if (strv_isempty(i->response_registries)) {
r = -EBADMSG;
log_error("Didn't get registry information.");
goto finish;
}
url = strappenda(PROTOCOL_PREFIX, i->response_registries[0], "/v1/repositories/", i->name, "/tags/", i->tag);
if (r < 0) {
log_error_errno(r, "Failed to allocate tags job: %m");
goto finish;
}
r = dkr_import_add_token(i, i->tags_job);
if (r < 0) {
log_oom();
goto finish;
}
r = import_job_begin(i->tags_job);
if (r < 0) {
log_error_errno(r, "Failed to start tags job: %m");
goto finish;
}
} else if (i->tags_job == j) {
const char *url;
assert(!i->ancestry_job);
if (r < 0) {
log_error_errno(r, "Failed to parse JSON id.");
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to allocate ancestry job: %m");
goto finish;
}
r = dkr_import_add_token(i, i->ancestry_job);
if (r < 0) {
log_oom();
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to allocate json job: %m");
goto finish;
}
r = dkr_import_add_token(i, i->json_job);
if (r < 0) {
log_oom();
goto finish;
}
r = import_job_begin(i->ancestry_job);
if (r < 0) {
log_error_errno(r, "Failed to start ancestry job: %m");
goto finish;
}
r = import_job_begin(i->json_job);
if (r < 0) {
log_error_errno(r, "Failed to start json job: %m");
goto finish;
}
} else if (i->ancestry_job == j) {
unsigned n;
if (r < 0) {
log_error_errno(r, "Failed to parse JSON id.");
goto finish;
}
n = strv_length(ancestry);
log_error("Ancestry doesn't end in main layer.");
r = -EBADMSG;
goto finish;
}
log_info("Ancestor lookup succeeded, requires layers:\n");
STRV_FOREACH(k, ancestry)
log_info("\t%s", *k);
i->current_ancestry = 0;
r = dkr_import_pull_layer(i);
if (r < 0)
goto finish;
} else if (i->layer_job == j) {
assert(i->final_path);
if (i->tar_pid > 0) {
i->tar_pid = 0;
if (r < 0)
goto finish;
}
r = aufs_resolve(i->temp_path);
if (r < 0) {
log_error_errno(r, "Failed to resolve aufs whiteouts: %m");
goto finish;
}
r = btrfs_subvol_set_read_only(i->temp_path, true);
if (r < 0) {
log_error_errno(r, "Failed to mark snapshort read-only: %m");
goto finish;
}
goto finish;
}
free(i->final_path);
i->final_path = NULL;
i->current_ancestry ++;
r = dkr_import_pull_layer(i);
if (r < 0)
goto finish;
} else if (i->json_job != j)
assert_not_reached("Got finished event for unknown curl object");
if (!dkr_import_is_done(i))
return;
r = dkr_import_make_local_copy(i);
if (r < 0)
goto finish;
r = 0;
if (i->on_finished)
i->on_finished(i, r, i->userdata);
else
sd_event_exit(i->event, r);
}
char *token;
DkrImport *i;
int r;
assert(j);
i = j->userdata;
if (r < 0)
return log_oom();
if (r > 0) {
free(i->response_token);
i->response_token = token;
return 0;
}
if (r < 0)
return log_oom();
if (r > 0) {
char **l, **k;
if (!l)
return log_oom();
STRV_FOREACH(k, l) {
if (!hostname_is_valid(*k)) {
log_error("Registry hostname is not valid.");
strv_free(l);
return -EBADMSG;
}
}
i->response_registries = l;
}
return 0;
}
int dkr_import_pull(DkrImport *i, const char *name, const char *tag, const char *local, bool force_local) {
const char *url;
int r;
assert(i);
if (!dkr_name_is_valid(name))
return -EINVAL;
return -EINVAL;
return -EINVAL;
if (i->images_job)
return -EBUSY;
if (!tag)
tag = "latest";
if (r < 0)
return r;
i->force_local = force_local;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = dkr_import_add_token(i, i->images_job);
if (r < 0)
return r;
return import_job_begin(i->images_job);
}
bool dkr_name_is_valid(const char *name) {
const char *slash, *p;
return false;
if (!slash)
return false;
return false;
if (!filename_is_valid(p))
return false;
return true;
}
bool dkr_id_is_valid(const char *id) {
if (!filename_is_valid(id))
return false;
return false;
return true;
}