image.c 46.1 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide image functions
 ******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <sys/utsname.h>
#include <ctype.h>

#include "image.h"
L
LiuHao 已提交
25
#include "libisulad.h"
H
haozi007 已提交
26
#include "isula_libutils/log.h"
O
overweight 已提交
27 28 29
#include "utils.h"

#include "ext_image.h"
D
dogsheng 已提交
30
#include "filters.h"
W
wujing 已提交
31
#include "collector.h"
32 33
#include "driver.h"
#include "storage.h"
O
overweight 已提交
34 35

#ifdef ENABLE_OCI_IMAGE
36
#include "oci_image.h"
O
overweight 已提交
37 38 39 40 41 42 43 44 45
#endif

#ifdef ENABLE_EMBEDDED_IMAGE
#include "embedded_image.h"
#include "db_all.h"

/* embedded */
static const struct bim_ops g_embedded_ops = {
    .init = embedded_init,
D
dogsheng 已提交
46
    .clean_resource = NULL,
O
overweight 已提交
47 48 49 50 51 52
    .detect = embedded_detect,

    .prepare_rf = embedded_prepare_rf,
    .mount_rf = embedded_mount_rf,
    .umount_rf = embedded_umount_rf,
    .delete_rf = embedded_delete_rf,
D
dogsheng 已提交
53
    .export_rf = NULL,
O
overweight 已提交
54 55 56 57 58

    .merge_conf = embedded_merge_conf,
    .get_user_conf = embedded_get_user_conf,

    .list_ims = embedded_list_images,
D
dogsheng 已提交
59
    .get_image_count = NULL,
O
overweight 已提交
60 61 62
    .rm_image = embedded_remove_image,
    .inspect_image = embedded_inspect_image,
    .resolve_image_name = embedded_resolve_image_name,
D
dogsheng 已提交
63 64 65
    .container_fs_usage = embedded_filesystem_usage,
    .get_filesystem_info = NULL,
    .image_status = NULL,
O
overweight 已提交
66 67
    .load_image = embedded_load_image,
    .pull_image = NULL,
D
dogsheng 已提交
68 69 70

    .login = NULL,
    .logout = NULL,
W
WangFengTu 已提交
71
    .tag_image = NULL,
W
WangFengTu 已提交
72
    .import = NULL,
O
overweight 已提交
73 74 75 76
};
#endif

#ifdef ENABLE_OCI_IMAGE
77 78
static const struct bim_ops g_oci_ops = {
    .init = oci_init,
79
    .clean_resource = NULL,
O
overweight 已提交
80 81
    .detect = oci_detect,

82 83 84 85 86
    .prepare_rf = oci_prepare_rf,
    .mount_rf = oci_mount_rf,
    .umount_rf = oci_umount_rf,
    .delete_rf = oci_delete_rf,
    .export_rf = oci_export_rf,
O
overweight 已提交
87

88
    .merge_conf = oci_merge_conf_rf,
O
overweight 已提交
89 90 91
    .get_user_conf = oci_get_user_conf,

    .list_ims = oci_list_images,
L
LiFeng 已提交
92
    .get_image_count = oci_get_images_count,
93
    .rm_image = oci_rmi,
O
overweight 已提交
94 95
    .inspect_image = oci_inspect_image,
    .resolve_image_name = oci_resolve_image_name,
96 97
    .container_fs_usage = oci_container_filesystem_usage,
    .get_filesystem_info = oci_get_filesystem_info,
D
dogsheng 已提交
98
    .image_status = oci_status_image,
99 100 101 102
    .load_image = oci_load_image,
    .pull_image = oci_pull_rf,
    .login = oci_login,
    .logout = oci_logout,
L
LiFeng 已提交
103
    .tag_image = oci_tag,
W
WangFengTu 已提交
104
    .import = isula_import,
O
overweight 已提交
105 106 107 108 109 110
};
#endif

/* external */
static const struct bim_ops g_ext_ops = {
    .init = ext_init,
D
dogsheng 已提交
111
    .clean_resource = NULL,
O
overweight 已提交
112 113 114 115 116 117
    .detect = ext_detect,

    .prepare_rf = ext_prepare_rf,
    .mount_rf = ext_mount_rf,
    .umount_rf = ext_umount_rf,
    .delete_rf = ext_delete_rf,
D
dogsheng 已提交
118
    .export_rf = NULL,
O
overweight 已提交
119 120 121 122 123

    .merge_conf = ext_merge_conf,
    .get_user_conf = ext_get_user_conf,

    .list_ims = ext_list_images,
D
dogsheng 已提交
124
    .get_image_count = NULL,
O
overweight 已提交
125 126 127
    .rm_image = ext_remove_image,
    .inspect_image = ext_inspect_image,
    .resolve_image_name = ext_resolve_image_name,
D
dogsheng 已提交
128 129 130
    .container_fs_usage = ext_filesystem_usage,
    .image_status = NULL,
    .get_filesystem_info = NULL,
O
overweight 已提交
131 132 133 134
    .load_image = ext_load_image,
    .pull_image = NULL,
    .login = ext_login,
    .logout = ext_logout,
W
WangFengTu 已提交
135
    .tag_image = NULL,
W
WangFengTu 已提交
136
    .import = NULL,
O
overweight 已提交
137 138 139 140 141 142
};

static const struct bim_type g_bims[] = {
#ifdef ENABLE_OCI_IMAGE
    {
        .image_type = IMAGE_TYPE_OCI,
143
        .ops = &g_oci_ops,
O
overweight 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    },
#endif
    { .image_type = IMAGE_TYPE_EXTERNAL, .ops = &g_ext_ops },
#ifdef ENABLE_EMBEDDED_IMAGE
    { .image_type = IMAGE_TYPE_EMBEDDED, .ops = &g_embedded_ops },
#endif
};

static const size_t g_numbims = sizeof(g_bims) / sizeof(struct bim_type);

static const struct bim_type *bim_query(const char *image_name)
{
    size_t i;
    char *temp = NULL;

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
160 161 162 163
        if (g_bims[i].ops->resolve_image_name == NULL) {
            WARN("Unimplements resolve image name in %s", g_bims[i].image_type);
            continue;
        }
L
LiFeng 已提交
164 165 166 167
        if (g_bims[i].ops->detect == NULL) {
            WARN("Unimplements detect in %s", g_bims[i].image_type);
            continue;
        }
O
overweight 已提交
168 169
        temp = g_bims[i].ops->resolve_image_name(image_name);
        if (temp == NULL) {
L
LiuHao 已提交
170
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
            return NULL;
        }
        int r = g_bims[i].ops->detect(temp);

        free(temp);
        temp = NULL;

        if (r != 0) {
            break;
        }
    }

    if (i == g_numbims) {
        return NULL;
    }
    return &g_bims[i];
}

static const struct bim_type *get_bim_by_type(const char *image_type)
{
    size_t i;

    for (i = 0; i < g_numbims; i++) {
        if (strcmp(g_bims[i].image_type, image_type) == 0) {
            return &g_bims[i];
        }
    }

    ERROR("Backing store %s unknown but not caught earlier\n", image_type);
    return NULL;
}

static void bim_put(struct bim *bim)
{
    if (bim == NULL) {
        return;
    }

    free(bim->image_name);
    bim->image_name = NULL;
    free(bim->ext_config_image);
    bim->ext_config_image = NULL;
    free(bim->container_id);
    bim->container_id = NULL;
    free(bim);
}

static struct bim *bim_get(const char *image_type, const char *image_name, const char *ext_config_image,
                           const char *container_id)
{
    struct bim *bim = NULL;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        return NULL;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        return NULL;
    }

    bim = util_common_calloc_s(sizeof(struct bim));
    if (bim == NULL) {
        return NULL;
    }

    bim->ops = q->ops;
    bim->type = q->image_type;

L
LiFeng 已提交
241 242 243 244 245 246
    if (bim->ops->resolve_image_name == NULL) {
        ERROR("Unimplements resolve image name");
        bim_put(bim);
        return NULL;
    }

O
overweight 已提交
247 248 249
    if (image_name != NULL) {
        bim->image_name = bim->ops->resolve_image_name(image_name);
        if (bim->image_name == NULL) {
L
LiuHao 已提交
250
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
            bim_put(bim);
            return NULL;
        }
    }
    if (ext_config_image != NULL) {
        bim->ext_config_image = util_strdup_s(ext_config_image);
    }
    if (container_id != NULL) {
        bim->container_id = util_strdup_s(container_id);
    }
    return bim;
}

D
dogsheng 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
int im_resolv_image_name(const char *image_type, const char *image_name, char **resolved_name)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        ERROR("Image type is required");
        goto out;
    }
    q = get_bim_by_type(image_type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->resolve_image_name == NULL) {
        ERROR("Get resolve image name umimplements");
        goto out;
    }

    *resolved_name = q->ops->resolve_image_name(image_name);
    if (*resolved_name == NULL) {
        goto out;
    }

    ret = 0;
out:
    return ret;
}

int im_get_filesystem_info(const char *image_type, im_fs_info_response **response)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL) {
        ERROR("Image type is required");
        goto out;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->get_filesystem_info == NULL) {
        ERROR("Get filesystem info umimplements");
        goto out;
    }

    EVENT("Event: {Object: get image filesystem info, Type: inspecting}");
    ret = q->ops->get_filesystem_info(response);
    if (ret != 0) {
        if (response != NULL && *response != NULL) {
            ERROR("Get filesystem info failed: %s", (*response)->errmsg);
        } else {
            ERROR("Get filesystem info failed");
        }
        goto out;
    }
    EVENT("Event: {Object: get image filesystem info, Type: inspected}");

out:
    return ret;
}

O
overweight 已提交
327 328 329 330 331
int im_get_container_filesystem_usage(const char *image_type, const char *id, imagetool_fs_info **fs_usage)
{
    int ret = 0;
    imagetool_fs_info *filesystemusage = NULL;
    const struct bim_type *q = NULL;
D
dogsheng 已提交
332
    im_container_fs_usage_request *request = NULL;
O
overweight 已提交
333 334 335 336 337 338 339 340 341 342 343 344

    if (image_type == NULL || id == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    q = get_bim_by_type(image_type);
    if (q == NULL) {
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
345 346 347 348
    if (q->ops->container_fs_usage == NULL) {
        ERROR("Unimplements filesystem usage in %s", image_type);
        goto out;
    }
O
overweight 已提交
349

D
dogsheng 已提交
350 351 352
    request = util_common_calloc_s(sizeof(im_container_fs_usage_request));
    if (request == NULL) {
        ERROR("Out of memory");
O
overweight 已提交
353 354 355 356 357
        ret = -1;
        goto out;
    }

    if (id != NULL) {
D
dogsheng 已提交
358
        request->name_id = util_strdup_s(id);
O
overweight 已提交
359 360
    }

D
dogsheng 已提交
361 362
    EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspecting}", id != NULL ? id : "");
    ret = q->ops->container_fs_usage(request, &filesystemusage);
O
overweight 已提交
363 364 365 366 367 368 369
    if (ret != 0) {
        ERROR("Failed to get filesystem usage for container %s", id);
        ret = -1;
        goto out;
    }

    *fs_usage = filesystemusage;
D
dogsheng 已提交
370
    EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspected}", id != NULL ? id : "");
O
overweight 已提交
371 372

out:
D
dogsheng 已提交
373
    free_im_container_fs_usage_request(request);
O
overweight 已提交
374 375 376 377 378 379
    return ret;
}

int im_remove_container_rootfs(const char *image_type, const char *container_id)
{
    int ret = 0;
D
dogsheng 已提交
380
    im_delete_request *request = NULL;
O
overweight 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394
    struct bim *bim = NULL;

    if (container_id == NULL || image_type == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, NULL, NULL, container_id);
    if (bim == NULL) {
        ERROR("Failed to init bim for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
395 396 397 398 399 400 401 402 403 404 405 406
    if (bim->ops->delete_rf == NULL) {
        ERROR("Unimplements delete in %s", bim->type);
        goto out;
    }

    request = util_common_calloc_s(sizeof(im_delete_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->name_id = util_strdup_s(container_id);
O
overweight 已提交
407

D
dogsheng 已提交
408 409
    EVENT("Event: {Object: %s, Type: removeing rootfs}", container_id);
    ret = bim->ops->delete_rf(request);
O
overweight 已提交
410 411 412 413 414
    if (ret != 0) {
        ERROR("Failed to delete rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
415
    EVENT("Event: {Object: %s, Type: removed rootfs}", container_id);
O
overweight 已提交
416 417 418

out:
    bim_put(bim);
D
dogsheng 已提交
419
    free_im_delete_request(request);
O
overweight 已提交
420 421 422
    return ret;
}

D
dogsheng 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
void free_im_container_fs_usage_request(im_container_fs_usage_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

void free_im_prepare_request(im_prepare_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->image_name);
    request->image_name = NULL;
    free(request->container_id);
    request->container_id = NULL;
444 445
    free(request->rootfs);
    request->rootfs = NULL;
D
dogsheng 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

    free_json_map_string_string(request->storage_opt);
    request->storage_opt = NULL;

    free(request);
}

void free_im_mount_request(im_mount_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

void free_im_delete_request(im_delete_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

void free_im_umount_request(im_umount_request *request)
{
    if (request == NULL) {
        return;
    }

    free(request->name_id);
    request->name_id = NULL;
    free(request);
}

O
overweight 已提交
486 487 488 489
int im_umount_container_rootfs(const char *image_type, const char *image_name, const char *container_id)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
490
    im_umount_request *request = NULL;
O
overweight 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503

    if (container_id == NULL || image_type == NULL || image_name == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, image_name, NULL, container_id);
    if (bim == NULL) {
        ERROR("Failed to init bim for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
504 505 506 507
    if (bim->ops->umount_rf == NULL) {
        ERROR("Unimplements umount in %s", bim->type);
        goto out;
    }
O
overweight 已提交
508

D
dogsheng 已提交
509 510 511 512 513 514 515 516 517 518 519 520
    request = (im_umount_request *)util_common_calloc_s(sizeof(im_umount_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->force = false;
    request->name_id = bim->container_id;
    bim->container_id = NULL;

    EVENT("Event: {Object: %s, Type: umounting rootfs}", container_id);
    ret = bim->ops->umount_rf(request);
O
overweight 已提交
521 522 523 524 525
    if (ret != 0) {
        ERROR("Failed to umount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
526
    EVENT("Event: {Object: %s, Type: umounted rootfs}", container_id);
O
overweight 已提交
527 528 529

out:
    bim_put(bim);
D
dogsheng 已提交
530
    free_im_umount_request(request);
O
overweight 已提交
531 532 533 534 535 536 537
    return ret;
}

int im_mount_container_rootfs(const char *image_type, const char *image_name, const char *container_id)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
538
    im_mount_request *request = NULL;
O
overweight 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551

    if (image_name == NULL || container_id == NULL || image_type == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, image_name, NULL, container_id);
    if (bim == NULL) {
        ERROR("Failed to init bim for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
552 553 554 555 556 557 558 559 560 561 562 563
    if (bim->ops->mount_rf == NULL) {
        ERROR("Unimplements mount in %s", bim->type);
        goto out;
    }

    request = util_common_calloc_s(sizeof(im_mount_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->name_id = util_strdup_s(container_id);
O
overweight 已提交
564

D
dogsheng 已提交
565 566
    EVENT("Event: {Object: %s, Type: mounting rootfs}", container_id);
    ret = bim->ops->mount_rf(request);
O
overweight 已提交
567 568 569 570 571
    if (ret != 0) {
        ERROR("Failed to mount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
572
    EVENT("Event: {Object: %s, Type: mounted rootfs}", container_id);
O
overweight 已提交
573 574 575

out:
    bim_put(bim);
D
dogsheng 已提交
576
    free_im_mount_request(request);
O
overweight 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    return ret;
}

char *im_get_image_type(const char *image, const char *external_rootfs)
{
    const char *image_name = NULL;
    const struct bim_type *bim_type = NULL;

    image_name = (external_rootfs != NULL) ? external_rootfs : image;
    if (image_name == NULL) {
        ERROR("Should specify the image name or external rootfs");
        return NULL;
    }

    bim_type = bim_query(image_name);
    if (bim_type == NULL) {
        ERROR("Failed to query type of image %s", image_name);
L
LiuHao 已提交
594
        isulad_set_error_message("No such image:%s", image_name);
O
overweight 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607
        return NULL;
    }

    return util_strdup_s(bim_type->image_type);
}

bool im_config_image_exist(const char *image_name)
{
    const struct bim_type *bim_type = NULL;

    bim_type = bim_query(image_name);
    if (bim_type == NULL) {
        ERROR("Config image %s not exist", image_name);
L
LiuHao 已提交
608
        isulad_set_error_message("Image %s not exist", image_name);
O
overweight 已提交
609 610 611 612 613 614 615
        return false;
    }

    return true;
}

int im_merge_image_config(const char *id, const char *image_type, const char *image_name,
616
                          const char *rootfs, host_config *host_spec, container_config *container_spec,
O
overweight 已提交
617 618 619 620
                          char **real_rootfs)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
621
    im_prepare_request *request = NULL;
O
overweight 已提交
622

623
    if (real_rootfs == NULL || image_type == NULL) {
O
overweight 已提交
624 625 626 627 628
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

629
    bim = bim_get(image_type, image_name, rootfs, id);
O
overweight 已提交
630 631 632 633 634
    if (bim == NULL) {
        ERROR("Failed to init bim of image %s", image_name);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647
    if (bim->ops->merge_conf == NULL) {
        ERROR("Unimplements merge config in %s", bim->type);
        goto out;
    }

    request = util_common_calloc_s(sizeof(im_prepare_request));
    if (request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    request->container_id = util_strdup_s(id);
    request->image_name = util_strdup_s(image_name);
648
    request->rootfs = util_strdup_s(rootfs);
D
dogsheng 已提交
649 650 651 652 653
    if (host_spec != NULL) {
        request->storage_opt = host_spec->storage_opt;
    }

    EVENT("Event: {Object: %s, Type: preparing rootfs with image %s}", id, image_name);
O
overweight 已提交
654

655
    ret = bim->ops->merge_conf(host_spec, container_spec, request, real_rootfs);
D
dogsheng 已提交
656
    request->storage_opt = NULL;
O
overweight 已提交
657
    if (ret != 0) {
658
        ERROR("Failed to merge image %s config", image_name);
O
overweight 已提交
659 660 661
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
662 663
    EVENT("Event: {Object: %s, Type: prepared rootfs with image %s}", id, image_name);

O
overweight 已提交
664 665 666 667
    INFO("Use real rootfs: %s with type: %s", *real_rootfs, image_type);

out:
    bim_put(bim);
D
dogsheng 已提交
668
    free_im_prepare_request(request);
O
overweight 已提交
669 670 671 672
    return ret;
}

int im_get_user_conf(const char *image_type, const char *basefs, host_config *hc, const char *userstr,
673
                     defs_process_user *puser)
O
overweight 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
{
    int ret = 0;
    struct bim *bim = NULL;

    if (basefs == NULL || hc == NULL || image_type == NULL || puser == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image type: %s", image_type);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
690 691 692 693
    if (bim->ops->get_user_conf == NULL) {
        ERROR("Unimplements get user config in %s", bim->type);
        goto out;
    }
O
overweight 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

    ret = bim->ops->get_user_conf(basefs, hc, userstr, puser);
    if (ret != 0) {
        ERROR("Failed to get user config");
        ret = -1;
        goto out;
    }

out:
    bim_put(bim);
    return ret;
}

static int append_images_to_response(im_list_response *response, imagetool_images_list *images_in)
{
    int ret = 0;
    size_t images_num = 0;
    size_t old_num = 0;
    imagetool_image **tmp = NULL;
    size_t i = 0;
    size_t new_size = 0;
    size_t old_size = 0;

    if (images_in == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    if (response->images == NULL) {
        response->images = util_common_calloc_s(sizeof(imagetool_images_list));
        if (response->images == NULL) {
            ERROR("Memeory out");
            ret = -1;
            goto out;
        }
    }

    images_num = images_in->images_len;
    // no images need to append
    if (images_num == 0) {
        goto out;
    }
    if (images_num > SIZE_MAX / sizeof(imagetool_image *) - response->images->images_len) {
        ERROR("Too many images to append!");
        ret = -1;
        goto out;
    }

    old_num = response->images->images_len;

    new_size = (old_num + images_num) * sizeof(imagetool_image *);
    old_size = old_num * sizeof(imagetool_image *);
    ret = mem_realloc((void **)(&tmp), new_size, response->images->images, old_size);
    if (ret != 0) {
        ERROR("Failed to realloc memory for append images");
        ret = -1;
        goto out;
    }
    response->images->images = tmp;
    for (i = 0; i < images_num; i++) {
        response->images->images[old_num + i] = images_in->images[i];
        images_in->images[i] = NULL;
        images_in->images_len--;
        response->images->images_len++;
    }

out:
    return ret;
}
D
dogsheng 已提交
764
int im_list_images(const im_list_request *ctx, im_list_response **response)
O
overweight 已提交
765 766 767 768 769 770 771 772 773 774
{
    size_t i;
    imagetool_images_list *images_tmp = NULL;

    *response = util_common_calloc_s(sizeof(im_list_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

D
dogsheng 已提交
775
    EVENT("Event: {Object: list images, Type: listing}");
O
overweight 已提交
776 777

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
778 779 780 781 782
        if (g_bims[i].ops->list_ims == NULL) {
            DEBUG("bim %s umimplements list images operator", g_bims[i].image_type);
            continue;
        }
        int ret = g_bims[i].ops->list_ims(ctx, &images_tmp);
O
overweight 已提交
783 784 785 786 787 788 789 790 791 792 793 794
        if (ret != 0) {
            ERROR("Failed to list all images with type:%s", g_bims[i].image_type);
            continue;
        }
        ret = append_images_to_response(*response, images_tmp);
        if (ret != 0) {
            ERROR("Failed to append images with type:%s", g_bims[i].image_type);
        }
        free_imagetool_images_list(images_tmp);
        images_tmp = NULL;
    }

D
dogsheng 已提交
795
    EVENT("Event: {Object: list images, Type: listed}");
O
overweight 已提交
796

L
LiuHao 已提交
797 798
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811
    }

    return 0;
}

void free_im_list_request(im_list_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->filter.image.image);
    ptr->filter.image.image = NULL;

D
dogsheng 已提交
812 813
    filters_args_free(ptr->image_filters);
    ptr->image_filters = NULL;
O
overweight 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    free(ptr);
}

void free_im_list_response(im_list_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free_imagetool_images_list(ptr->images);
    ptr->images = NULL;
    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

static bool check_im_pull_args(const im_pull_request *req, im_pull_response * const *resp)
{
    if (req == NULL || resp == NULL) {
        ERROR("Request or response is NULL");
        return false;
    }
    if (req->image == NULL) {
        ERROR("Empty image required");
L
LiuHao 已提交
838
        isulad_set_error_message("Empty image required");
O
overweight 已提交
839 840 841 842 843 844 845 846 847
        return false;
    }
    return true;
}

int im_pull_image(const im_pull_request *request, im_pull_response **response)
{
    int ret = -1;
    struct bim *bim = NULL;
848 849 850
    im_pull_response *tmp_res = NULL;

    DAEMON_CLEAR_ERRMSG();
O
overweight 已提交
851 852 853 854 855

    if (!check_im_pull_args(request, response)) {
        return ret;
    }

856 857 858 859 860 861
    tmp_res = (im_pull_response *)util_common_calloc_s(sizeof(im_pull_response));
    if (tmp_res == NULL) {
        ERROR("Out of memory");
        goto out;
    }

O
overweight 已提交
862 863 864 865 866 867 868
    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type: %s", request->type);
        goto out;
    }

    if (bim->ops->pull_image == NULL) {
D
dogsheng 已提交
869
        ERROR("Unimplements pull image in %s", bim->type);
O
overweight 已提交
870 871 872
        goto out;
    }

D
dogsheng 已提交
873
    EVENT("Event: {Object: %s, Type: Pulling}", request->image);
874
    ret = bim->ops->pull_image(request, tmp_res);
O
overweight 已提交
875 876 877 878 879
    if (ret != 0) {
        ERROR("Pull image %s failed", request->image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
880
    EVENT("Event: {Object: %s, Type: Pulled}", request->image);
W
wujing 已提交
881
    (void)isulad_monitor_send_image_event(request->image, IM_PULL);
O
overweight 已提交
882 883 884

out:
    bim_put(bim);
885 886 887 888 889
    if (ret != 0 && tmp_res != NULL && g_isulad_errmsg != NULL) {
        tmp_res->errmsg = util_strdup_s(g_isulad_errmsg);
    }
    DAEMON_CLEAR_ERRMSG();
    *response = tmp_res;
O
overweight 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
    return ret;
}

void free_im_pull_request(im_pull_request *req)
{
    if (req == NULL) {
        return;
    }
    free(req->type);
    req->type = NULL;
    free(req->image);
    req->image = NULL;
    free_sensitive_string(req->username);
    req->username = NULL;
    free_sensitive_string(req->password);
    req->password = NULL;
    free_sensitive_string(req->auth);
    req->auth = NULL;
    free_sensitive_string(req->server_address);
    req->server_address = NULL;
    free_sensitive_string(req->registry_token);
    req->registry_token = NULL;
    free_sensitive_string(req->identity_token);
    req->identity_token = NULL;
    free(req);
}

void free_im_pull_response(im_pull_response *resp)
{
    if (resp == NULL) {
        return;
    }
    free(resp->image_ref);
    resp->image_ref = NULL;
    free(resp->errmsg);
    resp->errmsg = NULL;
    free(resp);
}

W
WangFengTu 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
int im_import_image(const im_import_request *request, char **id)
{
    int ret = -1;
    struct bim *bim = NULL;

    if (request == NULL || id == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    if (request->file == NULL) {
        ERROR("Import image requires image tarball file path");
        isulad_set_error_message("Import image requires image tarball file path");
        goto pack_response;
    }

    // Support image type oci only currently.
    bim = bim_get(IMAGE_TYPE_OCI, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", IMAGE_TYPE_OCI);
        goto pack_response;
    }
    if (bim->ops->import == NULL) {
        ERROR("Unimplements import in %s", IMAGE_TYPE_OCI);
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: importing}", request->file);

    ret = bim->ops->import(request, id);
    if (ret != 0) {
        ERROR("Failed to import from %s with tag %s", request->file, request->tag);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: imported}", request->file);

pack_response:

    bim_put(bim);
    return ret;
}

void free_im_import_request(im_import_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->file);
    ptr->file = NULL;

    free(ptr->tag);
    ptr->file = NULL;

    free(ptr);
}

void free_im_import_response(im_import_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->id);
    ptr->id = NULL;

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1003
int im_load_image(const im_load_request *request, im_load_response **response)
O
overweight 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
{
    int ret = -1;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_load_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->file == NULL) {
        ERROR("Load image requires image tarball file path");
L
LiuHao 已提交
1021
        isulad_set_error_message("Load image requires image tarball file path");
O
overweight 已提交
1022 1023 1024 1025 1026
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
L
LiuHao 已提交
1027
        isulad_set_error_message("Missing image type");
O
overweight 已提交
1028 1029 1030 1031 1032 1033 1034 1035
        goto pack_response;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        goto pack_response;
    }
D
dogsheng 已提交
1036 1037 1038 1039
    if (bim->ops->load_image == NULL) {
        ERROR("Unimplements load image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

    EVENT("Event: {Object: %s, Type: loading}", request->file);

    ret = bim->ops->load_image(request);
    if (ret != 0) {
        ERROR("Failed to load image from %s with tag %s and type %s", request->file, request->tag, request->type);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: loaded}", request->file);

pack_response:
L
LiuHao 已提交
1053 1054
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    }

    bim_put(bim);
    return ret;
}

void free_im_load_request(im_load_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->file);
    ptr->file = NULL;

    free(ptr->tag);
    ptr->file = NULL;

    free(ptr->type);
    ptr->type = NULL;

    free(ptr);
}

void free_im_load_response(im_load_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

L
LiFeng 已提交
1091
static bool check_login_request(const im_login_request *request)
O
overweight 已提交
1092
{
L
LiFeng 已提交
1093
    if (request == NULL) {
O
overweight 已提交
1094
        ERROR("Invalid input arguments");
L
LiFeng 已提交
1095
        return false;
O
overweight 已提交
1096 1097 1098 1099
    }

    if (request->server == NULL) {
        ERROR("Login requires server address");
L
LiuHao 已提交
1100
        isulad_set_error_message("Login requires server address");
L
LiFeng 已提交
1101
        return false;
O
overweight 已提交
1102 1103 1104 1105
    }

    if (request->type == NULL) {
        ERROR("Login requires image type");
L
LiuHao 已提交
1106
        isulad_set_error_message("Login requires image type");
L
LiFeng 已提交
1107
        return false;
O
overweight 已提交
1108 1109 1110 1111
    }

    if (request->username == NULL || request->password == NULL) {
        ERROR("Missing username or password");
L
LiuHao 已提交
1112
        isulad_set_error_message("Missing username or password");
L
LiFeng 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
        return false;
    }
    return true;
}

int im_login(const im_login_request *request, im_login_response **response)
{
    int ret = -1;
    struct bim *bim = NULL;

    if (response == NULL) {
        ERROR("Empty response");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_login_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (!check_login_request(request)) {
O
overweight 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143
        goto pack_response;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        goto pack_response;
    }

L
LiFeng 已提交
1144 1145 1146 1147 1148
    if (bim->ops->login == NULL) {
        ERROR("Unimplements login in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    EVENT("Event: {Object: %s, Type: logining}", request->server);

    ret = bim->ops->login(request);
    if (ret != 0) {
        ERROR("Failed to login %s", request->server);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: logined}", request->server);

pack_response:
L
LiuHao 已提交
1161 1162
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
    }

    bim_put(bim);
    return ret;
}

void free_im_login_request(im_login_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free_sensitive_string(ptr->username);
    ptr->username = NULL;

    free_sensitive_string(ptr->password);
    ptr->password = NULL;

    free(ptr->type);
    ptr->type = NULL;

    free_sensitive_string(ptr->server);
    ptr->server = NULL;

    free(ptr);
}

void free_im_login_response(im_login_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

L
LiFeng 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
static bool check_logout_request(const im_logout_request *request)
{
    if (request == NULL) {
        ERROR("Invalid input arguments");
        return false;
    }

    if (request->server == NULL) {
        ERROR("Logout requires server address");
        isulad_set_error_message("Logout requires server address");
        return false;
    }

    if (request->type == NULL) {
        ERROR("Logout requires image type");
        isulad_set_error_message("Logout requires image type");
        return false;
    }
    return true;
}

D
dogsheng 已提交
1223
int im_logout(const im_logout_request *request, im_logout_response **response)
O
overweight 已提交
1224 1225 1226 1227
{
    int ret = -1;
    struct bim *bim = NULL;

L
LiFeng 已提交
1228 1229
    if (response == NULL) {
        ERROR("Empty response");
O
overweight 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_logout_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

L
LiFeng 已提交
1239
    if (!check_logout_request(request)) {
O
overweight 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248
        goto pack_response;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        goto pack_response;
    }

L
LiFeng 已提交
1249 1250 1251 1252 1253
    if (bim->ops->logout == NULL) {
        ERROR("Unimplements logout in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    EVENT("Event: {Object: %s, Type: logouting}", request->server);

    ret = bim->ops->logout(request);
    if (ret != 0) {
        ERROR("Failed to logout %s", request->server);
        ret = -1;
        goto pack_response;
    }

    EVENT("Event: {Object: %s, Type: logouted}", request->server);

pack_response:
L
LiuHao 已提交
1266 1267
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
    }

    bim_put(bim);
    return ret;
}

void free_im_logout_request(im_logout_request *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->type);
    ptr->type = NULL;

    free(ptr->server);
    ptr->server = NULL;

    free(ptr);
}

void free_im_logout_response(im_logout_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
int im_image_status(im_status_request *request, im_status_response **response)
{
    int ret = -1;
    char *image_ref = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = (im_status_response *)util_common_calloc_s(sizeof(im_status_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->image.image == NULL) {
        ERROR("get image status requires image ref");
L
LiuHao 已提交
1321
        isulad_set_error_message("get image status requires image ref");
D
dogsheng 已提交
1322 1323 1324 1325 1326 1327 1328 1329
        goto pack_response;
    }

    image_ref = util_strdup_s(request->image.image);

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1330
        isulad_set_error_message("No such image:%s", image_ref);
D
dogsheng 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, image_ref, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", image_ref);
        goto pack_response;
    }
    if (bim->ops->image_status == NULL) {
        ERROR("Unimplements image status in %s", bim->type);
        goto pack_response;
    }

    ret = bim->ops->image_status(request, response);
    if (ret != 0) {
        ERROR("Failed to get status of image %s", image_ref);
        ret = -1;
    }

pack_response:
L
LiuHao 已提交
1351
    if (g_isulad_errmsg != NULL) {
D
dogsheng 已提交
1352
        free((*response)->errmsg);
L
LiuHao 已提交
1353
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
D
dogsheng 已提交
1354 1355 1356 1357 1358 1359 1360
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

int im_rm_image(const im_remove_request *request, im_remove_response **response)
O
overweight 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
{
    int ret = -1;
    char *image_ref = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_remove_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->image.image == NULL) {
        ERROR("remove image requires image ref");
L
LiuHao 已提交
1380
        isulad_set_error_message("remove image requires image ref");
O
overweight 已提交
1381 1382 1383 1384 1385
        goto pack_response;
    }

    image_ref = util_strdup_s(request->image.image);

D
dogsheng 已提交
1386
    EVENT("Event: {Object: %s, Type: image removing}", image_ref);
O
overweight 已提交
1387 1388 1389 1390

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1391
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1392 1393 1394 1395 1396 1397 1398 1399
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, image_ref, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", image_ref);
        goto pack_response;
    }
D
dogsheng 已提交
1400 1401 1402 1403
    if (bim->ops->rm_image == NULL) {
        ERROR("Unimplements rm image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1404 1405 1406 1407 1408 1409 1410 1411

    ret = bim->ops->rm_image(request);
    if (ret != 0) {
        ERROR("Failed to remove image %s", image_ref);
        ret = -1;
        goto pack_response;
    }

D
dogsheng 已提交
1412
    EVENT("Event: {Object: %s, Type: image removed}", image_ref);
W
wujing 已提交
1413
    (void)isulad_monitor_send_image_event(image_ref, IM_REMOVE);
O
overweight 已提交
1414 1415

pack_response:
L
LiuHao 已提交
1416 1417
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1418 1419 1420 1421 1422 1423
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

W
WangFengTu 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
int im_tag_image(const im_tag_request *request, im_tag_response **response)
{
    int ret = -1;
    char *src_name = NULL;
    char *dest_name = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_remove_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->src_name.image == NULL || request->dest_name.image == NULL) {
        ERROR("remove image requires source image ref and dest image ref");
        isulad_set_error_message("remove image requires source image ref and dest image ref");
        goto pack_response;
    }

    src_name = util_strdup_s(request->src_name.image);
    dest_name = util_strdup_s(request->dest_name.image);

    bim_type = bim_query(src_name);
    if (bim_type == NULL) {
        ERROR("No such image:%s", src_name);
        isulad_set_error_message("No such image:%s", src_name);
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, src_name, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", src_name);
        goto pack_response;
    }
    if (bim->ops->tag_image == NULL) {
        ERROR("Unimplements tag image in %s", bim->type);
        goto pack_response;
    }

    ret = bim->ops->tag_image(request);
    if (ret != 0) {
        ERROR("Failed to tag image %s to %s", src_name, dest_name);
        ret = -1;
        goto pack_response;
    }

pack_response:
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
    }
    free(src_name);
    free(dest_name);
    bim_put(bim);
    return ret;
}

O
overweight 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
void free_im_remove_request(im_remove_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->image.image);
    ptr->image.image = NULL;

    free(ptr);
}

void free_im_remove_response(im_remove_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

W
WangFengTu 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
void free_im_tag_request(im_tag_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->src_name.image);
    ptr->src_name.image = NULL;
    free(ptr->dest_name.image);
    ptr->dest_name.image = NULL;
    free(ptr);
}

void free_im_tag_response(im_tag_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
static int do_im_inspect_image(struct bim *bim, char **inspected_json)
{
    int ret = 0;
    im_inspect_request *update_request = NULL;

    if (bim->ops->inspect_image == NULL) {
        ERROR("Unimplements inspect image in %s", bim->type);
        ret = -1;
        goto out;
    }
    update_request = util_common_calloc_s(sizeof(im_inspect_request));
    if (update_request == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }
    update_request->image.image = bim->image_name;
    bim->image_name = NULL;

    ret = bim->ops->inspect_image(update_request, inspected_json);
    if (ret != 0) {
        ERROR("Failed to inspect image %s", update_request->image.image);
        ret = -1;
    }

out:
    free_im_inspect_request(update_request);
    return ret;
}

O
overweight 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
int im_inspect_image(const im_inspect_request *request, im_inspect_response **response)
{
    int ret = 0;
    char *image_ref = NULL;
    char *inspected_json = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

    if (request == NULL || response == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    *response = util_common_calloc_s(sizeof(im_inspect_response));
    if (*response == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    if (request->image.image == NULL) {
        ERROR("inspect image requires image ref");
L
LiuHao 已提交
1584
        isulad_set_error_message("inspect image requires image ref");
O
overweight 已提交
1585 1586 1587 1588 1589 1590
        ret = -1;
        goto pack_response;
    }

    image_ref = util_strdup_s(request->image.image);

D
dogsheng 已提交
1591
    EVENT("Event: {Object: %s, Type: image inspecting}", image_ref);
O
overweight 已提交
1592 1593 1594 1595

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1596
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
        ret = -1;
        goto pack_response;
    }

    bim = bim_get(bim_type->image_type, image_ref, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim for image %s", image_ref);
        ret = -1;
        goto pack_response;
    }

D
dogsheng 已提交
1608
    ret = do_im_inspect_image(bim, &inspected_json);
O
overweight 已提交
1609 1610 1611 1612
    if (ret != 0) {
        goto pack_response;
    }

D
dogsheng 已提交
1613
    EVENT("Event: {Object: %s, Type: image inspected}", image_ref);
O
overweight 已提交
1614 1615

pack_response:
L
LiuHao 已提交
1616 1617
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
    }
    if (inspected_json != NULL) {
        (*response)->im_inspect_json = util_strdup_s(inspected_json);
    }
    free(image_ref);
    free(inspected_json);
    bim_put(bim);
    return ret;
}

void free_im_inspect_request(im_inspect_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->image.image);
    ptr->image.image = NULL;

    free(ptr);
}

void free_im_inspect_response(im_inspect_response *ptr)
{
    if (ptr == NULL) {
        return;
    }

    free(ptr->im_inspect_json);
    ptr->im_inspect_json = NULL;

    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}

D
dogsheng 已提交
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
/*
 * parameters: image type
 * return: error return 0, success return image count
 * */
size_t im_get_image_count(const im_image_count_request *request)
{
    size_t ret = 0;
    const struct bim_type *q = NULL;

    if (request == NULL || request->type == NULL) {
        ERROR("Image type is required");
        goto out;
    }
    q = get_bim_by_type(request->type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->get_image_count == NULL) {
        ERROR("Get image count umimplements");
        goto out;
    }

    ret = q->ops->get_image_count();

out:
    return ret;
}

void free_im_image_count_request(im_image_count_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->type);
    ptr->type = NULL;
    free(ptr);
}

int im_container_export(const im_export_request *request)
{
    int ret = 0;
    int nret = 0;
    struct bim *bim = NULL;

    if (request == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    if (request->file == NULL) {
        ERROR("Container export requires output file path");
        ret = -1;
        goto out;
    }
    if (request->name_id == NULL) {
        ERROR("Container export requires container id");
        ret = -1;
        goto out;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
        ret = -1;
        goto out;
    }

    bim = bim_get(request->type, NULL, NULL, NULL);
    if (bim == NULL) {
        ERROR("Failed to init bim, image type:%s", request->type);
        ret = -1;
        goto out;
    }
    if (bim->ops->export_rf == NULL) {
        ERROR("Unimplements container export in %s", bim->type);
        ret = -1;
        goto out;
    }

    EVENT("Event: {Object: %s, Type: exporting}", request->file);

    nret = bim->ops->export_rf(request);
    if (nret != 0) {
        ERROR("Failed to export container from %s into file %s and type %s",
              request->name_id, request->file, request->type);
        ret = -1;
        goto out;
    }

    EVENT("Event: {Object: %s, Type: exported}", request->name_id);

out:
    bim_put(bim);
    return ret;
}

void free_im_export_request(im_export_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->type);
    ptr->type = NULL;
    free(ptr->file);
    ptr->file = NULL;
    free(ptr->name_id);
    ptr->name_id = NULL;
    free(ptr);
}

1763
static int bims_init(const struct service_arguments *args)
O
overweight 已提交
1764 1765 1766 1767 1768
{
    int ret = 0;
    size_t i;

    for (i = 0; i < g_numbims; i++) {
L
LiFeng 已提交
1769 1770 1771 1772
        if (g_bims[i].ops->init == NULL) {
            WARN("Unimplements init in %s", g_bims[i].image_type);
            continue;
        }
1773
        ret = g_bims[i].ops->init(args);
O
overweight 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782
        if (ret != 0) {
            ERROR("Failed to init bim %s", g_bims[i].image_type);
            break;
        }
    }

    return ret;
}

1783
int image_module_init(const struct service_arguments *args)
O
overweight 已提交
1784
{
1785 1786 1787
    if (args == NULL) {
        ERROR("Invalid input arguments");
        return -1;
D
dogsheng 已提交
1788 1789
    }

1790
    return bims_init(args);
D
dogsheng 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
}

void image_module_exit()
{
    size_t i;

    for (i = 0; i < g_numbims; i++) {
        if (g_bims[i].ops->clean_resource == NULL) {
            continue;
        }
        g_bims[i].ops->clean_resource();
    }
O
overweight 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
}

int map_to_key_value_string(const json_map_string_string *map, char ***array, size_t *array_len)
{
    char **strings = NULL;
    size_t strings_len = 0;
    size_t i;
    int ret;

    if (map == NULL) {
        return 0;
    }
    for (i = 0; i < map->len; i++) {
        char *str = NULL;
        size_t len;
        if (strlen(map->keys[i]) > (SIZE_MAX - strlen(map->values[i])) - 2) {
            ERROR("Invalid keys/values");
            goto cleanup;
        }
        len = strlen(map->keys[i]) + strlen(map->values[i]) + 2;
        str = util_common_calloc_s(len);
        if (str == NULL) {
            ERROR("Out of memory");
            goto cleanup;
        }
O
openeuler-iSula 已提交
1828 1829
        ret = snprintf(str, len, "%s=%s", map->keys[i], map->values[i]);
        if (ret < 0 || (size_t)ret >= len) {
O
overweight 已提交
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
            ERROR("Failed to print string");
            free(str);
            goto cleanup;
        }
        ret = util_array_append(&strings, str);
        free(str);
        if (ret != 0) {
            ERROR("Failed to append array");
            goto cleanup;
        }
        strings_len++;
    }
    *array = strings;
    *array_len = strings_len;
    return 0;

cleanup:
    util_free_array(strings);
    return -1;
}

D
dogsheng 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
void free_im_status_request(im_status_request *req)
{
    if (req == NULL) {
        return;
    }
    free(req->image.image);
    req->image.image = NULL;

    free(req);
}

void free_im_status_response(im_status_response *req)
{
    if (req == NULL) {
        return;
    }
    free_imagetool_image_status(req->image_info);
    req->image_info = NULL;
    free(req->errmsg);
    req->errmsg = NULL;

    free(req);
}

void free_im_fs_info_response(im_fs_info_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free_imagetool_fs_info(ptr->fs_info);
    ptr->fs_info = NULL;
    free(ptr->errmsg);
    ptr->errmsg = NULL;

    free(ptr);
}
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911

container_inspect_graph_driver *im_graphdriver_get_metadata(const char *id)
{
    if (id == NULL) {
        ERROR("Invalid input arguments for get driver metadata of container:%s", id);
        return NULL;
    }

    return graphdriver_get_metadata(id);
}

struct graphdriver_status *im_graphdriver_get_status(void)
{
    return graphdriver_get_status();
}

bool im_storage_image_exist(const char *image_or_id)
{
    return storage_image_exist(image_or_id);
}

void im_free_graphdriver_status(struct graphdriver_status *status)
{
    free_graphdriver_status(status);
}