image.c 44.8 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
    .container_fs_usage = embedded_filesystem_usage,
    .get_filesystem_info = NULL,
    .get_storage_status = NULL,
65
    .get_storage_metadata = NULL,
D
dogsheng 已提交
66
    .image_status = NULL,
O
overweight 已提交
67 68
    .load_image = embedded_load_image,
    .pull_image = NULL,
D
dogsheng 已提交
69 70 71 72 73

    .login = NULL,
    .logout = NULL,

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

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

D
dogsheng 已提交
84 85 86 87 88
    .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 已提交
89

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

    .list_ims = oci_list_images,
D
dogsheng 已提交
94 95
    .get_image_count = oci_images_store_size,
    .rm_image = isula_rmi,
O
overweight 已提交
96 97
    .inspect_image = oci_inspect_image,
    .resolve_image_name = oci_resolve_image_name,
D
dogsheng 已提交
98 99 100
    .container_fs_usage = isula_container_filesystem_usage,
    .get_filesystem_info = isula_get_filesystem_info,
    .get_storage_status = isula_get_storage_status,
101
    .get_storage_metadata = isula_get_storage_metadata,
D
dogsheng 已提交
102 103 104 105 106 107 108
    .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 已提交
109 110 111 112 113 114
};
#endif

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

    .merge_conf = ext_merge_conf,
    .get_user_conf = ext_get_user_conf,

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

    .health_check = NULL,
O
overweight 已提交
143 144 145 146 147 148
};

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

O
overweight 已提交
253 254 255
    if (image_name != NULL) {
        bim->image_name = bim->ops->resolve_image_name(image_name);
        if (bim->image_name == NULL) {
L
LiuHao 已提交
256
            isulad_append_error_message("Failed to resovle image name%s", image_name);
O
overweight 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269
            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 已提交
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
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;
}

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
int im_get_storage_metadata(const char *image_type, char *id, im_storage_metadata_response **response)
{
    int ret = -1;
    const struct bim_type *q = NULL;

    if (image_type == NULL || response == NULL) {
        ERROR("Image type or response is NULL");
        goto out;
    }
    q = get_bim_by_type(image_type);
    if (q == NULL) {
        goto out;
    }
    if (q->ops->get_storage_metadata == NULL) {
        ERROR("Get storage metadata umimplements");
        goto out;
    }

    ret = q->ops->get_storage_metadata(id, response);
    if (ret != 0) {
        ERROR("Get storage metadata failed");
        free_im_storage_metadata_response(*response);
        *response = NULL;
        goto out;
    }

out:
    return ret;
}

D
dogsheng 已提交
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
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 已提交
420 421 422 423 424
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 已提交
425
    im_container_fs_usage_request *request = NULL;
O
overweight 已提交
426 427 428 429 430 431 432 433 434 435 436 437

    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 已提交
438 439 440 441
    if (q->ops->container_fs_usage == NULL) {
        ERROR("Unimplements filesystem usage in %s", image_type);
        goto out;
    }
O
overweight 已提交
442

D
dogsheng 已提交
443 444 445
    request = util_common_calloc_s(sizeof(im_container_fs_usage_request));
    if (request == NULL) {
        ERROR("Out of memory");
O
overweight 已提交
446 447 448 449 450
        ret = -1;
        goto out;
    }

    if (id != NULL) {
D
dogsheng 已提交
451
        request->name_id = util_strdup_s(id);
O
overweight 已提交
452 453
    }

D
dogsheng 已提交
454 455
    EVENT("Event: {Object: container \'%s\' filesystem info, Type: inspecting}", id != NULL ? id : "");
    ret = q->ops->container_fs_usage(request, &filesystemusage);
O
overweight 已提交
456 457 458 459 460 461 462
    if (ret != 0) {
        ERROR("Failed to get filesystem usage for container %s", id);
        ret = -1;
        goto out;
    }

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

out:
D
dogsheng 已提交
466
    free_im_container_fs_usage_request(request);
O
overweight 已提交
467 468 469 470 471 472
    return ret;
}

int im_remove_container_rootfs(const char *image_type, const char *container_id)
{
    int ret = 0;
D
dogsheng 已提交
473
    im_delete_request *request = NULL;
O
overweight 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487
    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 已提交
488 489 490 491 492 493 494 495 496 497 498 499
    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 已提交
500

D
dogsheng 已提交
501 502
    EVENT("Event: {Object: %s, Type: removeing rootfs}", container_id);
    ret = bim->ops->delete_rf(request);
O
overweight 已提交
503 504 505 506 507
    if (ret != 0) {
        ERROR("Failed to delete rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
508
    EVENT("Event: {Object: %s, Type: removed rootfs}", container_id);
O
overweight 已提交
509 510 511

out:
    bim_put(bim);
D
dogsheng 已提交
512
    free_im_delete_request(request);
O
overweight 已提交
513 514 515
    return ret;
}

D
dogsheng 已提交
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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
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 已提交
579 580 581 582
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 已提交
583
    im_umount_request *request = NULL;
O
overweight 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596

    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 已提交
597 598 599 600
    if (bim->ops->umount_rf == NULL) {
        ERROR("Unimplements umount in %s", bim->type);
        goto out;
    }
O
overweight 已提交
601

D
dogsheng 已提交
602 603 604 605 606 607 608 609 610 611 612 613
    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 已提交
614 615 616 617 618
    if (ret != 0) {
        ERROR("Failed to umount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
619
    EVENT("Event: {Object: %s, Type: umounted rootfs}", container_id);
O
overweight 已提交
620 621 622

out:
    bim_put(bim);
D
dogsheng 已提交
623
    free_im_umount_request(request);
O
overweight 已提交
624 625 626 627 628 629 630
    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 已提交
631
    im_mount_request *request = NULL;
O
overweight 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644

    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 已提交
645 646 647 648 649 650 651 652 653 654 655 656
    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 已提交
657

D
dogsheng 已提交
658 659
    EVENT("Event: {Object: %s, Type: mounting rootfs}", container_id);
    ret = bim->ops->mount_rf(request);
O
overweight 已提交
660 661 662 663 664
    if (ret != 0) {
        ERROR("Failed to mount rootfs for container %s", container_id);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
665
    EVENT("Event: {Object: %s, Type: mounted rootfs}", container_id);
O
overweight 已提交
666 667 668

out:
    bim_put(bim);
D
dogsheng 已提交
669
    free_im_mount_request(request);
O
overweight 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    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 已提交
687
        isulad_set_error_message("No such image:%s", image_name);
O
overweight 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700
        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 已提交
701
        isulad_set_error_message("Image %s not exist", image_name);
O
overweight 已提交
702 703 704 705 706 707 708
        return false;
    }

    return true;
}

int im_merge_image_config(const char *id, const char *image_type, const char *image_name,
709 710
                          const char *ext_config_image,
                          host_config *host_spec, container_config *container_spec,
O
overweight 已提交
711 712 713 714
                          char **real_rootfs)
{
    int ret = 0;
    struct bim *bim = NULL;
D
dogsheng 已提交
715
    im_prepare_request *request = NULL;
O
overweight 已提交
716

717
    if (real_rootfs == NULL || image_type == NULL) {
O
overweight 已提交
718 719 720 721 722 723 724 725 726 727 728
        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 已提交
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
    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 已提交
748

749
    ret = bim->ops->merge_conf(host_spec, container_spec, request, real_rootfs);
D
dogsheng 已提交
750
    request->storage_opt = NULL;
O
overweight 已提交
751 752 753 754 755
    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 已提交
756 757
    EVENT("Event: {Object: %s, Type: prepared rootfs with image %s}", id, image_name);

O
overweight 已提交
758 759 760 761
    INFO("Use real rootfs: %s with type: %s", *real_rootfs, image_type);

out:
    bim_put(bim);
D
dogsheng 已提交
762
    free_im_prepare_request(request);
O
overweight 已提交
763 764 765 766
    return ret;
}

int im_get_user_conf(const char *image_type, const char *basefs, host_config *hc, const char *userstr,
767
                     defs_process_user *puser)
O
overweight 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
{
    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 已提交
784 785 786 787
    if (bim->ops->get_user_conf == NULL) {
        ERROR("Unimplements get user config in %s", bim->type);
        goto out;
    }
O
overweight 已提交
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 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

    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 已提交
858
int im_list_images(const im_list_request *ctx, im_list_response **response)
O
overweight 已提交
859 860 861 862 863 864 865 866 867 868
{
    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 已提交
869
    EVENT("Event: {Object: list images, Type: listing}");
O
overweight 已提交
870 871

    for (i = 0; i < g_numbims; i++) {
D
dogsheng 已提交
872 873 874 875 876
        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 已提交
877 878 879 880 881 882 883 884 885 886 887 888
        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 已提交
889
    EVENT("Event: {Object: list images, Type: listed}");
O
overweight 已提交
890

L
LiuHao 已提交
891 892
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905
    }

    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 已提交
906 907
    filters_args_free(ptr->image_filters);
    ptr->image_filters = NULL;
O
overweight 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
    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 已提交
932
        isulad_set_error_message("Empty image required");
O
overweight 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
        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 已提交
954
        ERROR("Unimplements pull image in %s", bim->type);
O
overweight 已提交
955 956 957
        goto out;
    }

D
dogsheng 已提交
958
    EVENT("Event: {Object: %s, Type: Pulling}", request->image);
O
overweight 已提交
959 960 961 962 963 964
    ret = bim->ops->pull_image(request, response);
    if (ret != 0) {
        ERROR("Pull image %s failed", request->image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
965
    EVENT("Event: {Object: %s, Type: Pulled}", request->image);
W
wujing 已提交
966
    (void)isulad_monitor_send_image_event(request->image, IM_PULL);
O
overweight 已提交
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 1003 1004 1005 1006 1007 1008

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 已提交
1009
int im_load_image(const im_load_request *request, im_load_response **response)
O
overweight 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
{
    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 已提交
1027
        isulad_set_error_message("Load image requires image tarball file path");
O
overweight 已提交
1028 1029 1030 1031 1032
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
L
LiuHao 已提交
1033
        isulad_set_error_message("Missing image type");
O
overweight 已提交
1034 1035 1036 1037 1038 1039 1040 1041
        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 已提交
1042 1043 1044 1045
    if (bim->ops->load_image == NULL) {
        ERROR("Unimplements load image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

    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 已提交
1059 1060
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
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 1091 1092 1093 1094 1095 1096
    }

    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 已提交
1097
static bool check_login_request(const im_login_request *request)
O
overweight 已提交
1098
{
L
LiFeng 已提交
1099
    if (request == NULL) {
O
overweight 已提交
1100
        ERROR("Invalid input arguments");
L
LiFeng 已提交
1101
        return false;
O
overweight 已提交
1102 1103 1104 1105
    }

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

    if (request->type == NULL) {
        ERROR("Login requires image type");
L
LiuHao 已提交
1112
        isulad_set_error_message("Login requires image type");
L
LiFeng 已提交
1113
        return false;
O
overweight 已提交
1114 1115 1116 1117
    }

    if (request->username == NULL || request->password == NULL) {
        ERROR("Missing username or password");
L
LiuHao 已提交
1118
        isulad_set_error_message("Missing username or password");
L
LiFeng 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
        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 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149
        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 已提交
1150 1151 1152 1153 1154
    if (bim->ops->login == NULL) {
        ERROR("Unimplements login in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    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 已提交
1167 1168
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
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 1202 1203 1204 1205 1206 1207
    }

    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 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
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 已提交
1229
int im_logout(const im_logout_request *request, im_logout_response **response)
O
overweight 已提交
1230 1231 1232 1233
{
    int ret = -1;
    struct bim *bim = NULL;

L
LiFeng 已提交
1234 1235
    if (response == NULL) {
        ERROR("Empty response");
O
overweight 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244
        return -1;
    }

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

L
LiFeng 已提交
1245
    if (!check_logout_request(request)) {
O
overweight 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254
        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 已提交
1255 1256 1257 1258 1259
    if (bim->ops->logout == NULL) {
        ERROR("Unimplements logout in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
    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 已提交
1272 1273
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
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 1301 1302 1303 1304 1305 1306
    }

    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 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
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 已提交
1327
        isulad_set_error_message("get image status requires image ref");
D
dogsheng 已提交
1328 1329 1330 1331 1332 1333 1334 1335
        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 已提交
1336
        isulad_set_error_message("No such image:%s", image_ref);
D
dogsheng 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
        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 已提交
1357
    if (g_isulad_errmsg != NULL) {
D
dogsheng 已提交
1358
        free((*response)->errmsg);
L
LiuHao 已提交
1359
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
D
dogsheng 已提交
1360 1361 1362 1363 1364 1365 1366
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

int im_rm_image(const im_remove_request *request, im_remove_response **response)
O
overweight 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
{
    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 已提交
1386
        isulad_set_error_message("remove image requires image ref");
O
overweight 已提交
1387 1388 1389 1390 1391
        goto pack_response;
    }

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

D
dogsheng 已提交
1392
    EVENT("Event: {Object: %s, Type: image removing}", image_ref);
O
overweight 已提交
1393 1394 1395 1396

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1397
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1398 1399 1400 1401 1402 1403 1404 1405
        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 已提交
1406 1407 1408 1409
    if (bim->ops->rm_image == NULL) {
        ERROR("Unimplements rm image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1410 1411 1412 1413 1414 1415 1416 1417

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

D
dogsheng 已提交
1418
    EVENT("Event: {Object: %s, Type: image removed}", image_ref);
W
wujing 已提交
1419
    (void)isulad_monitor_send_image_event(image_ref, IM_REMOVE);
O
overweight 已提交
1420 1421

pack_response:
L
LiuHao 已提交
1422 1423
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
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
    }
    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 已提交
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
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 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
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 已提交
1504
        isulad_set_error_message("inspect image requires image ref");
O
overweight 已提交
1505 1506 1507 1508 1509 1510
        ret = -1;
        goto pack_response;
    }

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

D
dogsheng 已提交
1511
    EVENT("Event: {Object: %s, Type: image inspecting}", image_ref);
O
overweight 已提交
1512 1513 1514 1515

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1516
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
        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 已提交
1528
    ret = do_im_inspect_image(bim, &inspected_json);
O
overweight 已提交
1529 1530 1531 1532
    if (ret != 0) {
        goto pack_response;
    }

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

pack_response:
L
LiuHao 已提交
1536 1537
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
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 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    }
    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 已提交
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 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
/*
 * 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 已提交
1696 1697 1698 1699 1700
{
    int ret = 0;
    size_t i;

    for (i = 0; i < g_numbims; i++) {
L
LiFeng 已提交
1701 1702 1703 1704
        if (g_bims[i].ops->init == NULL) {
            WARN("Unimplements init in %s", g_bims[i].image_type);
            continue;
        }
D
dogsheng 已提交
1705
        ret = g_bims[i].ops->init(conf);
O
overweight 已提交
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
        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 已提交
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
    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 已提交
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
}

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 已提交
1767 1768
        ret = snprintf(str, len, "%s=%s", map->keys[i], map->values[i]);
        if (ret < 0 || (size_t)ret >= len) {
O
overweight 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
            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 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 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 1828 1829 1830 1831 1832 1833
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 已提交
1834 1835
    free(ptr->status);
    ptr->status = NULL;
D
dogsheng 已提交
1836 1837 1838
    free(ptr);
}

1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
void free_im_storage_metadata_response(im_storage_metadata_response *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free_json_map_string_string(ptr->metadata);
    ptr->metadata = NULL;
    free(ptr->name);
    ptr->name = NULL;
    free(ptr->errmsg);
    ptr->errmsg = NULL;
    free(ptr);
}

D
dogsheng 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861
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
}