image.c 43.6 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
 * iSulad licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * 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.
 * See the Mulan PSL v1 for more details.
 * 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"
O
overweight 已提交
26 27 28 29
#include "log.h"
#include "utils.h"

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

#ifdef ENABLE_OCI_IMAGE
D
dogsheng 已提交
34 35
#include "isula_image.h"
#include "oci_images_store.h"
O
overweight 已提交
36 37 38 39 40 41 42 43 44
#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 已提交
45
    .clean_resource = NULL,
O
overweight 已提交
46 47 48 49 50 51
    .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 已提交
52
    .export_rf = NULL,
O
overweight 已提交
53 54 55 56 57

    .merge_conf = embedded_merge_conf,
    .get_user_conf = embedded_get_user_conf,

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

    .login = NULL,
    .logout = NULL,

    .health_check = NULL,
O
overweight 已提交
73 74 75
};
#endif

D
dogsheng 已提交
76
/* isula image server */
O
overweight 已提交
77
#ifdef ENABLE_OCI_IMAGE
D
dogsheng 已提交
78 79 80
static const struct bim_ops g_isula_ops = {
    .init = isula_init,
    .clean_resource = isula_exit,
O
overweight 已提交
81 82
    .detect = oci_detect,

D
dogsheng 已提交
83 84 85 86 87
    .prepare_rf = isula_prepare_rf,
    .mount_rf = isula_mount_rf,
    .umount_rf = isula_umount_rf,
    .delete_rf = isula_delete_rf,
    .export_rf = isula_export_rf,
O
overweight 已提交
88

D
dogsheng 已提交
89
    .merge_conf = isula_merge_conf_rf,
O
overweight 已提交
90 91 92
    .get_user_conf = oci_get_user_conf,

    .list_ims = oci_list_images,
D
dogsheng 已提交
93 94
    .get_image_count = oci_images_store_size,
    .rm_image = isula_rmi,
O
overweight 已提交
95 96
    .inspect_image = oci_inspect_image,
    .resolve_image_name = oci_resolve_image_name,
D
dogsheng 已提交
97 98 99 100 101 102 103 104 105 106
    .container_fs_usage = isula_container_filesystem_usage,
    .get_filesystem_info = isula_get_filesystem_info,
    .get_storage_status = isula_get_storage_status,
    .image_status = oci_status_image,
    .load_image = isual_load_image,
    .pull_image = isula_pull_rf,
    .login = isula_login,
    .logout = isula_logout,

    .health_check = isula_health_check,
O
overweight 已提交
107 108 109 110 111 112
};
#endif

/* external */
static const struct bim_ops g_ext_ops = {
    .init = ext_init,
D
dogsheng 已提交
113
    .clean_resource = NULL,
O
overweight 已提交
114 115 116 117 118 119
    .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 已提交
120
    .export_rf = NULL,
O
overweight 已提交
121 122 123 124 125

    .merge_conf = ext_merge_conf,
    .get_user_conf = ext_get_user_conf,

    .list_ims = ext_list_images,
D
dogsheng 已提交
126
    .get_image_count = NULL,
O
overweight 已提交
127 128 129
    .rm_image = ext_remove_image,
    .inspect_image = ext_inspect_image,
    .resolve_image_name = ext_resolve_image_name,
D
dogsheng 已提交
130 131 132 133
    .container_fs_usage = ext_filesystem_usage,
    .image_status = NULL,
    .get_filesystem_info = NULL,
    .get_storage_status = NULL,
O
overweight 已提交
134 135 136 137
    .load_image = ext_load_image,
    .pull_image = NULL,
    .login = ext_login,
    .logout = ext_logout,
D
dogsheng 已提交
138 139

    .health_check = NULL,
O
overweight 已提交
140 141 142 143 144 145
};

static const struct bim_type g_bims[] = {
#ifdef ENABLE_OCI_IMAGE
    {
        .image_type = IMAGE_TYPE_OCI,
D
dogsheng 已提交
146
        .ops = &g_isula_ops,
O
overweight 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    },
#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 已提交
163 164 165 166
        if (g_bims[i].ops->resolve_image_name == NULL) {
            WARN("Unimplements resolve image name in %s", g_bims[i].image_type);
            continue;
        }
L
LiFeng 已提交
167 168 169 170
        if (g_bims[i].ops->detect == NULL) {
            WARN("Unimplements detect in %s", g_bims[i].image_type);
            continue;
        }
O
overweight 已提交
171 172
        temp = g_bims[i].ops->resolve_image_name(image_name);
        if (temp == NULL) {
L
LiuHao 已提交
173
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
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 241 242 243
            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 已提交
244 245 246 247 248 249
    if (bim->ops->resolve_image_name == NULL) {
        ERROR("Unimplements resolve image name");
        bim_put(bim);
        return NULL;
    }

O
overweight 已提交
250 251 252
    if (image_name != NULL) {
        bim->image_name = bim->ops->resolve_image_name(image_name);
        if (bim->image_name == NULL) {
L
LiuHao 已提交
253
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
            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 已提交
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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
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_storage_status(const char *image_type, im_storage_status_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_storage_status == NULL) {
        ERROR("Get storage status umimplements");
        goto out;
    }

    ret = q->ops->get_storage_status(response);
    if (ret != 0) {
        ERROR("Get storage status failed");
        free_im_storage_status_response(*response);
        *response = NULL;
        goto out;
    }

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;
}

int im_health_check(const char *image_type)
{
    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) {
        ERROR("Get bim failed");
        goto out;
    }

    if (q->ops->health_check == NULL) {
        ERROR("Health check umimplement");
        goto out;
    }

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

out:
    return ret;
}

O
overweight 已提交
387 388 389 390 391
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 已提交
392
    im_container_fs_usage_request *request = NULL;
O
overweight 已提交
393 394 395 396 397 398 399 400 401 402 403 404

    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 已提交
405 406 407 408
    if (q->ops->container_fs_usage == NULL) {
        ERROR("Unimplements filesystem usage in %s", image_type);
        goto out;
    }
O
overweight 已提交
409

D
dogsheng 已提交
410 411 412
    request = util_common_calloc_s(sizeof(im_container_fs_usage_request));
    if (request == NULL) {
        ERROR("Out of memory");
O
overweight 已提交
413 414 415 416 417
        ret = -1;
        goto out;
    }

    if (id != NULL) {
D
dogsheng 已提交
418
        request->name_id = util_strdup_s(id);
O
overweight 已提交
419 420
    }

D
dogsheng 已提交
421 422
    EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspecting}", id != NULL ? id : "");
    ret = q->ops->container_fs_usage(request, &filesystemusage);
O
overweight 已提交
423 424 425 426 427 428 429
    if (ret != 0) {
        ERROR("Failed to get filesystem usage for container %s", id);
        ret = -1;
        goto out;
    }

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

out:
D
dogsheng 已提交
433
    free_im_container_fs_usage_request(request);
O
overweight 已提交
434 435 436 437 438 439
    return ret;
}

int im_remove_container_rootfs(const char *image_type, const char *container_id)
{
    int ret = 0;
D
dogsheng 已提交
440
    im_delete_request *request = NULL;
O
overweight 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454
    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 已提交
455 456 457 458 459 460 461 462 463 464 465 466
    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 已提交
467

D
dogsheng 已提交
468 469
    EVENT("Event: {Object: %s, Type: removeing rootfs}", container_id);
    ret = bim->ops->delete_rf(request);
O
overweight 已提交
470 471 472 473 474
    if (ret != 0) {
        ERROR("Failed to delete rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
475
    EVENT("Event: {Object: %s, Type: removed rootfs}", container_id);
O
overweight 已提交
476 477 478

out:
    bim_put(bim);
D
dogsheng 已提交
479
    free_im_delete_request(request);
O
overweight 已提交
480 481 482
    return ret;
}

D
dogsheng 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
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;
    free(request->ext_config_image);
    request->ext_config_image = NULL;

    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 已提交
546 547 548 549
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 已提交
550
    im_umount_request *request = NULL;
O
overweight 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563

    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 已提交
564 565 566 567
    if (bim->ops->umount_rf == NULL) {
        ERROR("Unimplements umount in %s", bim->type);
        goto out;
    }
O
overweight 已提交
568

D
dogsheng 已提交
569 570 571 572 573 574 575 576 577 578 579 580
    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 已提交
581 582 583 584 585
    if (ret != 0) {
        ERROR("Failed to umount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
586
    EVENT("Event: {Object: %s, Type: umounted rootfs}", container_id);
O
overweight 已提交
587 588 589

out:
    bim_put(bim);
D
dogsheng 已提交
590
    free_im_umount_request(request);
O
overweight 已提交
591 592 593 594 595 596 597
    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 已提交
598
    im_mount_request *request = NULL;
O
overweight 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611

    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 已提交
612 613 614 615 616 617 618 619 620 621 622 623
    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 已提交
624

D
dogsheng 已提交
625 626
    EVENT("Event: {Object: %s, Type: mounting rootfs}", container_id);
    ret = bim->ops->mount_rf(request);
O
overweight 已提交
627 628 629 630 631
    if (ret != 0) {
        ERROR("Failed to mount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
632
    EVENT("Event: {Object: %s, Type: mounted rootfs}", container_id);
O
overweight 已提交
633 634 635

out:
    bim_put(bim);
D
dogsheng 已提交
636
    free_im_mount_request(request);
O
overweight 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    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 已提交
654
        isulad_set_error_message("No such image:%s", image_name);
O
overweight 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667
        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 已提交
668
        isulad_set_error_message("Image %s not exist", image_name);
O
overweight 已提交
669 670 671 672 673 674 675
        return false;
    }

    return true;
}

int im_merge_image_config(const char *id, const char *image_type, const char *image_name,
676 677
                          const char *ext_config_image,
                          host_config *host_spec, container_config *container_spec,
O
overweight 已提交
678 679 680 681
                          char **real_rootfs)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
682
    im_prepare_request *request = NULL;
O
overweight 已提交
683

684
    if (real_rootfs == NULL || image_type == NULL) {
O
overweight 已提交
685 686 687 688 689 690 691 692 693 694 695
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    bim = bim_get(image_type, image_name, ext_config_image, id);
    if (bim == NULL) {
        ERROR("Failed to init bim of image %s", image_name);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    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);
    request->ext_config_image = util_strdup_s(ext_config_image);
    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 已提交
715

716
    ret = bim->ops->merge_conf(host_spec, container_spec, request, real_rootfs);
D
dogsheng 已提交
717
    request->storage_opt = NULL;
O
overweight 已提交
718 719 720 721 722
    if (ret != 0) {
        ERROR("Failed to merge image %s config, config image is %s", image_name, ext_config_image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
723 724
    EVENT("Event: {Object: %s, Type: prepared rootfs with image %s}", id, image_name);

O
overweight 已提交
725 726 727 728
    INFO("Use real rootfs: %s with type: %s", *real_rootfs, image_type);

out:
    bim_put(bim);
D
dogsheng 已提交
729
    free_im_prepare_request(request);
O
overweight 已提交
730 731 732 733
    return ret;
}

int im_get_user_conf(const char *image_type, const char *basefs, host_config *hc, const char *userstr,
734
                     defs_process_user *puser)
O
overweight 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
{
    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 已提交
751 752 753 754
    if (bim->ops->get_user_conf == NULL) {
        ERROR("Unimplements get user config in %s", bim->type);
        goto out;
    }
O
overweight 已提交
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824

    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 已提交
825
int im_list_images(const im_list_request *ctx, im_list_response **response)
O
overweight 已提交
826 827 828 829 830 831 832 833 834 835
{
    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 已提交
836
    EVENT("Event: {Object: list images, Type: listing}");
O
overweight 已提交
837 838

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
839 840 841 842 843
        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 已提交
844 845 846 847 848 849 850 851 852 853 854 855
        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 已提交
856
    EVENT("Event: {Object: list images, Type: listed}");
O
overweight 已提交
857

L
LiuHao 已提交
858 859
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872
    }

    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 已提交
873 874
    filters_args_free(ptr->image_filters);
    ptr->image_filters = NULL;
O
overweight 已提交
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    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 已提交
899
        isulad_set_error_message("Empty image required");
O
overweight 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
        return false;
    }
    return true;
}

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

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

    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 已提交
921
        ERROR("Unimplements pull image in %s", bim->type);
O
overweight 已提交
922 923 924
        goto out;
    }

D
dogsheng 已提交
925
    EVENT("Event: {Object: %s, Type: Pulling}", request->image);
O
overweight 已提交
926 927 928 929 930 931
    ret = bim->ops->pull_image(request, response);
    if (ret != 0) {
        ERROR("Pull image %s failed", request->image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
932
    EVENT("Event: {Object: %s, Type: Pulled}", request->image);
W
wujing 已提交
933
    (void)isulad_monitor_send_image_event(request->image, IM_PULL);
O
overweight 已提交
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

out:
    bim_put(bim);
    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);
}

D
dogsheng 已提交
976
int im_load_image(const im_load_request *request, im_load_response **response)
O
overweight 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
{
    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 已提交
994
        isulad_set_error_message("Load image requires image tarball file path");
O
overweight 已提交
995 996 997 998 999
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
L
LiuHao 已提交
1000
        isulad_set_error_message("Missing image type");
O
overweight 已提交
1001 1002 1003 1004 1005 1006 1007 1008
        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 已提交
1009 1010 1011 1012
    if (bim->ops->load_image == NULL) {
        ERROR("Unimplements load image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

    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 已提交
1026 1027
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    }

    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 已提交
1064
static bool check_login_request(const im_login_request *request)
O
overweight 已提交
1065
{
L
LiFeng 已提交
1066
    if (request == NULL) {
O
overweight 已提交
1067
        ERROR("Invalid input arguments");
L
LiFeng 已提交
1068
        return false;
O
overweight 已提交
1069 1070 1071 1072
    }

    if (request->server == NULL) {
        ERROR("Login requires server address");
L
LiuHao 已提交
1073
        isulad_set_error_message("Login requires server address");
L
LiFeng 已提交
1074
        return false;
O
overweight 已提交
1075 1076 1077 1078
    }

    if (request->type == NULL) {
        ERROR("Login requires image type");
L
LiuHao 已提交
1079
        isulad_set_error_message("Login requires image type");
L
LiFeng 已提交
1080
        return false;
O
overweight 已提交
1081 1082 1083 1084
    }

    if (request->username == NULL || request->password == NULL) {
        ERROR("Missing username or password");
L
LiuHao 已提交
1085
        isulad_set_error_message("Missing username or password");
L
LiFeng 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        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 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116
        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 已提交
1117 1118 1119 1120 1121
    if (bim->ops->login == NULL) {
        ERROR("Unimplements login in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    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 已提交
1134 1135
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    }

    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 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
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 已提交
1196
int im_logout(const im_logout_request *request, im_logout_response **response)
O
overweight 已提交
1197 1198 1199 1200
{
    int ret = -1;
    struct bim *bim = NULL;

L
LiFeng 已提交
1201 1202
    if (response == NULL) {
        ERROR("Empty response");
O
overweight 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211
        return -1;
    }

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

L
LiFeng 已提交
1212
    if (!check_logout_request(request)) {
O
overweight 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221
        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 已提交
1222 1223 1224 1225 1226
    if (bim->ops->logout == NULL) {
        ERROR("Unimplements logout in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    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 已提交
1239 1240
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    }

    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 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
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 已提交
1294
        isulad_set_error_message("get image status requires image ref");
D
dogsheng 已提交
1295 1296 1297 1298 1299 1300 1301 1302
        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 已提交
1303
        isulad_set_error_message("No such image:%s", image_ref);
D
dogsheng 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
        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 已提交
1324
    if (g_isulad_errmsg != NULL) {
D
dogsheng 已提交
1325
        free((*response)->errmsg);
L
LiuHao 已提交
1326
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
D
dogsheng 已提交
1327 1328 1329 1330 1331 1332 1333
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

int im_rm_image(const im_remove_request *request, im_remove_response **response)
O
overweight 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
{
    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 已提交
1353
        isulad_set_error_message("remove image requires image ref");
O
overweight 已提交
1354 1355 1356 1357 1358
        goto pack_response;
    }

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

D
dogsheng 已提交
1359
    EVENT("Event: {Object: %s, Type: image removing}", image_ref);
O
overweight 已提交
1360 1361 1362 1363

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1364
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1365 1366 1367 1368 1369 1370 1371 1372
        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 已提交
1373 1374 1375 1376
    if (bim->ops->rm_image == NULL) {
        ERROR("Unimplements rm image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1377 1378 1379 1380 1381 1382 1383 1384

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

D
dogsheng 已提交
1385
    EVENT("Event: {Object: %s, Type: image removed}", image_ref);
W
wujing 已提交
1386
    (void)isulad_monitor_send_image_event(image_ref, IM_REMOVE);
O
overweight 已提交
1387 1388

pack_response:
L
LiuHao 已提交
1389 1390
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

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);
}

D
dogsheng 已提交
1420 1421 1422 1423 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
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 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
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 已提交
1471
        isulad_set_error_message("inspect image requires image ref");
O
overweight 已提交
1472 1473 1474 1475 1476 1477
        ret = -1;
        goto pack_response;
    }

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

D
dogsheng 已提交
1478
    EVENT("Event: {Object: %s, Type: image inspecting}", image_ref);
O
overweight 已提交
1479 1480 1481 1482

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1483
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
        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 已提交
1495
    ret = do_im_inspect_image(bim, &inspected_json);
O
overweight 已提交
1496 1497 1498 1499
    if (ret != 0) {
        goto pack_response;
    }

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

pack_response:
L
LiuHao 已提交
1503 1504
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
    }
    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 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 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 1654 1655 1656 1657 1658 1659 1660 1661 1662
/*
 * 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);
}

void free_im_configs(struct im_configs *conf)
{
    if (conf == NULL) {
        return;
    }
    free(conf->rootpath);
    conf->rootpath = NULL;
    free(conf->server_sock);
    conf->server_sock = NULL;
    free(conf);
}

static int bims_init(const struct im_configs *conf)
O
overweight 已提交
1663 1664 1665 1666 1667
{
    int ret = 0;
    size_t i;

    for (i = 0; i < g_numbims; i++) {
L
LiFeng 已提交
1668 1669 1670 1671
        if (g_bims[i].ops->init == NULL) {
            WARN("Unimplements init in %s", g_bims[i].image_type);
            continue;
        }
D
dogsheng 已提交
1672
        ret = g_bims[i].ops->init(conf);
O
overweight 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
        if (ret != 0) {
            ERROR("Failed to init bim %s", g_bims[i].image_type);
            break;
        }
    }

    return ret;
}

int image_module_init(const char *rootpath)
{
D
dogsheng 已提交
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
    struct im_configs *conf;
    int ret = -1;

    conf = (struct im_configs *)util_common_calloc_s(sizeof(struct im_configs));
    if (conf == NULL) {
        ERROR("Out of memory");
        return ret;
    }
    conf->rootpath = util_strdup_s(rootpath);
    ret = bims_init(conf);

    free_im_configs(conf);
    return ret;
}

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 已提交
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
}

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 已提交
1734 1735
        ret = snprintf(str, len, "%s=%s", map->keys[i], map->values[i]);
        if (ret < 0 || (size_t)ret >= len) {
O
overweight 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
            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 已提交
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
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);
}

void free_im_storage_status_response(im_storage_status_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->backing_fs);
    ptr->backing_fs = NULL;
W
WangFengTu 已提交
1801 1802
    free(ptr->status);
    ptr->status = NULL;
D
dogsheng 已提交
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
    free(ptr);
}

void im_sync_containers_isuladkit(void)
{
    DEBUG("Sync containers...");
#ifdef ENABLE_OCI_IMAGE
    if (isula_sync_containers() != 0) {
        WARN("Sync containers with remote failed!!");
    }
#endif
}