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

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

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

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

    .merge_conf = embedded_merge_conf,
    .get_user_conf = embedded_get_user_conf,

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

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

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

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

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

    .list_ims = oci_list_images,
L
LiFeng 已提交
92
    .get_image_count = oci_get_images_count,
93
    .rm_image = oci_rmi,
O
overweight 已提交
94 95
    .inspect_image = oci_inspect_image,
    .resolve_image_name = oci_resolve_image_name,
96 97 98 99
    .container_fs_usage = oci_container_filesystem_usage,
    .get_filesystem_info = oci_get_filesystem_info,
    .get_storage_status = oci_get_storage_status,
    .get_storage_metadata = oci_get_storage_metadata,
D
dogsheng 已提交
100
    .image_status = oci_status_image,
101 102 103 104
    .load_image = oci_load_image,
    .pull_image = oci_pull_rf,
    .login = oci_login,
    .logout = oci_logout,
L
LiFeng 已提交
105
    .tag_image = oci_tag,
W
WangFengTu 已提交
106
    .import = isula_import,
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,
134
    .get_storage_metadata = NULL,
O
overweight 已提交
135 136 137 138
    .load_image = ext_load_image,
    .pull_image = NULL,
    .login = ext_login,
    .logout = ext_logout,
W
WangFengTu 已提交
139
    .tag_image = NULL,
W
WangFengTu 已提交
140
    .import = NULL,
O
overweight 已提交
141 142 143 144 145 146
};

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

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

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
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 已提交
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 387 388 389 390
int im_get_filesystem_info(const char *image_type, im_fs_info_response **response)
{
    int ret = -1;
    const struct bim_type *q = NULL;

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

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

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

out:
    return ret;
}

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

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

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

    if (id != NULL) {
D
dogsheng 已提交
422
        request->name_id = util_strdup_s(id);
O
overweight 已提交
423 424
    }

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

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

out:
D
dogsheng 已提交
437
    free_im_container_fs_usage_request(request);
O
overweight 已提交
438 439 440 441 442 443
    return ret;
}

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

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

out:
    bim_put(bim);
D
dogsheng 已提交
483
    free_im_delete_request(request);
O
overweight 已提交
484 485 486
    return ret;
}

D
dogsheng 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
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;
508 509
    free(request->rootfs);
    request->rootfs = NULL;
D
dogsheng 已提交
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 546 547 548 549

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

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

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

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

    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 已提交
616 617 618 619 620 621 622 623 624 625 626 627
    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 已提交
628

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

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

    return true;
}

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

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

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

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

O
overweight 已提交
728 729 730 731
    INFO("Use real rootfs: %s with type: %s", *real_rootfs, image_type);

out:
    bim_put(bim);
D
dogsheng 已提交
732
    free_im_prepare_request(request);
O
overweight 已提交
733 734 735 736
    return ret;
}

int im_get_user_conf(const char *image_type, const char *basefs, host_config *hc, const char *userstr,
737
                     defs_process_user *puser)
O
overweight 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
{
    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 已提交
754 755 756 757
    if (bim->ops->get_user_conf == NULL) {
        ERROR("Unimplements get user config in %s", bim->type);
        goto out;
    }
O
overweight 已提交
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 825 826 827

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

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

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

    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 已提交
876 877
    filters_args_free(ptr->image_filters);
    ptr->image_filters = NULL;
O
overweight 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    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 已提交
902
        isulad_set_error_message("Empty image required");
O
overweight 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
        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 已提交
924
        ERROR("Unimplements pull image in %s", bim->type);
O
overweight 已提交
925 926 927
        goto out;
    }

D
dogsheng 已提交
928
    EVENT("Event: {Object: %s, Type: Pulling}", request->image);
O
overweight 已提交
929 930 931 932 933 934
    ret = bim->ops->pull_image(request, response);
    if (ret != 0) {
        ERROR("Pull image %s failed", request->image);
        ret = -1;
        goto out;
    }
D
dogsheng 已提交
935
    EVENT("Event: {Object: %s, Type: Pulled}", request->image);
W
wujing 已提交
936
    (void)isulad_monitor_send_image_event(request->image, IM_PULL);
O
overweight 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978

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

W
WangFengTu 已提交
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 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 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
int im_import_image(const im_import_request *request, char **id)
{
    int ret = -1;
    struct bim *bim = NULL;

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

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

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

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

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

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

pack_response:

    bim_put(bim);
    return ret;
}

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

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

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

    free(ptr);
}

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

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

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

    free(ptr);
}

D
dogsheng 已提交
1053
int im_load_image(const im_load_request *request, im_load_response **response)
O
overweight 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
{
    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 已提交
1071
        isulad_set_error_message("Load image requires image tarball file path");
O
overweight 已提交
1072 1073 1074 1075 1076
        goto pack_response;
    }

    if (request->type == NULL) {
        ERROR("Missing image type");
L
LiuHao 已提交
1077
        isulad_set_error_message("Missing image type");
O
overweight 已提交
1078 1079 1080 1081 1082 1083 1084 1085
        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 已提交
1086 1087 1088 1089
    if (bim->ops->load_image == NULL) {
        ERROR("Unimplements load image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102

    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 已提交
1103 1104
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    }

    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 已提交
1141
static bool check_login_request(const im_login_request *request)
O
overweight 已提交
1142
{
L
LiFeng 已提交
1143
    if (request == NULL) {
O
overweight 已提交
1144
        ERROR("Invalid input arguments");
L
LiFeng 已提交
1145
        return false;
O
overweight 已提交
1146 1147 1148 1149
    }

    if (request->server == NULL) {
        ERROR("Login requires server address");
L
LiuHao 已提交
1150
        isulad_set_error_message("Login requires server address");
L
LiFeng 已提交
1151
        return false;
O
overweight 已提交
1152 1153 1154 1155
    }

    if (request->type == NULL) {
        ERROR("Login requires image type");
L
LiuHao 已提交
1156
        isulad_set_error_message("Login requires image type");
L
LiFeng 已提交
1157
        return false;
O
overweight 已提交
1158 1159 1160 1161
    }

    if (request->username == NULL || request->password == NULL) {
        ERROR("Missing username or password");
L
LiuHao 已提交
1162
        isulad_set_error_message("Missing username or password");
L
LiFeng 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        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 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193
        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 已提交
1194 1195 1196 1197 1198
    if (bim->ops->login == NULL) {
        ERROR("Unimplements login in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
    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 已提交
1211 1212
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    }

    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 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
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 已提交
1273
int im_logout(const im_logout_request *request, im_logout_response **response)
O
overweight 已提交
1274 1275 1276 1277
{
    int ret = -1;
    struct bim *bim = NULL;

L
LiFeng 已提交
1278 1279
    if (response == NULL) {
        ERROR("Empty response");
O
overweight 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288
        return -1;
    }

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

L
LiFeng 已提交
1289
    if (!check_logout_request(request)) {
O
overweight 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298
        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 已提交
1299 1300 1301 1302 1303
    if (bim->ops->logout == NULL) {
        ERROR("Unimplements logout in %s", bim->type);
        goto pack_response;
    }

O
overweight 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    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 已提交
1316 1317
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
    }

    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 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
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 已提交
1371
        isulad_set_error_message("get image status requires image ref");
D
dogsheng 已提交
1372 1373 1374 1375 1376 1377 1378 1379
        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 已提交
1380
        isulad_set_error_message("No such image:%s", image_ref);
D
dogsheng 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
        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 已提交
1401
    if (g_isulad_errmsg != NULL) {
D
dogsheng 已提交
1402
        free((*response)->errmsg);
L
LiuHao 已提交
1403
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
D
dogsheng 已提交
1404 1405 1406 1407 1408 1409 1410
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

int im_rm_image(const im_remove_request *request, im_remove_response **response)
O
overweight 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
{
    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 已提交
1430
        isulad_set_error_message("remove image requires image ref");
O
overweight 已提交
1431 1432 1433 1434 1435
        goto pack_response;
    }

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

D
dogsheng 已提交
1436
    EVENT("Event: {Object: %s, Type: image removing}", image_ref);
O
overweight 已提交
1437 1438 1439 1440

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1441
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1442 1443 1444 1445 1446 1447 1448 1449
        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 已提交
1450 1451 1452 1453
    if (bim->ops->rm_image == NULL) {
        ERROR("Unimplements rm image in %s", bim->type);
        goto pack_response;
    }
O
overweight 已提交
1454 1455 1456 1457 1458 1459 1460 1461

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

D
dogsheng 已提交
1462
    EVENT("Event: {Object: %s, Type: image removed}", image_ref);
W
wujing 已提交
1463
    (void)isulad_monitor_send_image_event(image_ref, IM_REMOVE);
O
overweight 已提交
1464 1465

pack_response:
L
LiuHao 已提交
1466 1467
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1468 1469 1470 1471 1472 1473
    }
    free(image_ref);
    bim_put(bim);
    return ret;
}

W
WangFengTu 已提交
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 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
int im_tag_image(const im_tag_request *request, im_tag_response **response)
{
    int ret = -1;
    char *src_name = NULL;
    char *dest_name = NULL;
    const struct bim_type *bim_type = NULL;
    struct bim *bim = NULL;

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

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

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

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

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

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

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

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

O
overweight 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
void free_im_remove_request(im_remove_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->image.image);
    ptr->image.image = NULL;

    free(ptr);
}

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

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

    free(ptr);
}

W
WangFengTu 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
void free_im_tag_request(im_tag_request *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->src_name.image);
    ptr->src_name.image = NULL;
    free(ptr->dest_name.image);
    ptr->dest_name.image = NULL;
    free(ptr);
}

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

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

    free(ptr);
}

D
dogsheng 已提交
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
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 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
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 已提交
1634
        isulad_set_error_message("inspect image requires image ref");
O
overweight 已提交
1635 1636 1637 1638 1639 1640
        ret = -1;
        goto pack_response;
    }

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

D
dogsheng 已提交
1641
    EVENT("Event: {Object: %s, Type: image inspecting}", image_ref);
O
overweight 已提交
1642 1643 1644 1645

    bim_type = bim_query(image_ref);
    if (bim_type == NULL) {
        ERROR("No such image:%s", image_ref);
L
LiuHao 已提交
1646
        isulad_set_error_message("No such image:%s", image_ref);
O
overweight 已提交
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
        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 已提交
1658
    ret = do_im_inspect_image(bim, &inspected_json);
O
overweight 已提交
1659 1660 1661 1662
    if (ret != 0) {
        goto pack_response;
    }

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

pack_response:
L
LiuHao 已提交
1666 1667
    if (g_isulad_errmsg != NULL) {
        (*response)->errmsg = util_strdup_s(g_isulad_errmsg);
O
overweight 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
    }
    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 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 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 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
/*
 * 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);
}

1813
static int bims_init(const struct service_arguments *args)
O
overweight 已提交
1814 1815 1816 1817 1818
{
    int ret = 0;
    size_t i;

    for (i = 0; i < g_numbims; i++) {
L
LiFeng 已提交
1819 1820 1821 1822
        if (g_bims[i].ops->init == NULL) {
            WARN("Unimplements init in %s", g_bims[i].image_type);
            continue;
        }
1823
        ret = g_bims[i].ops->init(args);
O
overweight 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832
        if (ret != 0) {
            ERROR("Failed to init bim %s", g_bims[i].image_type);
            break;
        }
    }

    return ret;
}

1833
int image_module_init(const struct service_arguments *args)
O
overweight 已提交
1834
{
1835 1836 1837
    if (args == NULL) {
        ERROR("Invalid input arguments");
        return -1;
D
dogsheng 已提交
1838 1839
    }

1840
    return bims_init(args);
D
dogsheng 已提交
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
}

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 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
}

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 已提交
1878 1879
        ret = snprintf(str, len, "%s=%s", map->keys[i], map->values[i]);
        if (ret < 0 || (size_t)ret >= len) {
O
overweight 已提交
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
            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 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
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 已提交
1945 1946
    free(ptr->status);
    ptr->status = NULL;
D
dogsheng 已提交
1947 1948 1949
    free(ptr);
}

1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
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);
}