From 91c2f82ac1ae2ea23eb52c3cd1096e3f396ffae2 Mon Sep 17 00:00:00 2001 From: wujing Date: Wed, 24 Jun 2020 15:28:12 +0800 Subject: [PATCH] Code structure decoupling Signed-off-by: wujing --- src/daemon/image/oci/registry/registry.c | 1 - .../image/oci/registry/registry_common.c | 181 ------------------ .../image/oci/registry/registry_common.h | 41 ---- .../image/oci/{registry => }/registry_type.c | 0 .../image/oci/{registry => }/registry_type.h | 3 +- .../oci/storage/image_store/image_store.c | 1 - src/daemon/image/oci/utils_images.c | 159 +++++++++++++++ src/daemon/image/oci/utils_images.h | 9 + test/image/oci/storage/images/CMakeLists.txt | 3 +- 9 files changed, 170 insertions(+), 228 deletions(-) delete mode 100644 src/daemon/image/oci/registry/registry_common.c delete mode 100644 src/daemon/image/oci/registry/registry_common.h rename src/daemon/image/oci/{registry => }/registry_type.c (100%) rename src/daemon/image/oci/{registry => }/registry_type.h (98%) diff --git a/src/daemon/image/oci/registry/registry.c b/src/daemon/image/oci/registry/registry.c index cc45b0d..1e5d31c 100644 --- a/src/daemon/image/oci/registry/registry.c +++ b/src/daemon/image/oci/registry/registry.c @@ -41,7 +41,6 @@ #include "storage.h" #include "constants.h" #include "utils_images.h" -#include "registry_common.h" #define MANIFEST_BIG_DATA_KEY "manifest" diff --git a/src/daemon/image/oci/registry/registry_common.c b/src/daemon/image/oci/registry/registry_common.c deleted file mode 100644 index beb3bca..0000000 --- a/src/daemon/image/oci/registry/registry_common.c +++ /dev/null @@ -1,181 +0,0 @@ -/****************************************************************************** - * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. - * 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 - * 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 v2 for more details. - * Author: WuJing - * Create: 2020-06-13 - * Description: provide registry common functions - *******************************************************************************/ - -#define _GNU_SOURCE -#include "registry_common.h" -#include -#include - -#include "isula_libutils/log.h" -#include "utils.h" - -void free_items_not_inherit(docker_image_config_v2 *config) -{ - size_t i = 0; - - if (config == NULL) { - return; - } - free(config->id); - config->id = NULL; - free(config->parent); - config->parent = NULL; - config->size = 0; - free_docker_image_rootfs(config->rootfs); - config->rootfs = NULL; - - for (i = 0; i < config->history_len; i++) { - free_docker_image_history(config->history[i]); - config->history[i] = NULL; - } - config->history = NULL; - config->history_len = 0; - - return; -} - -static char *convert_created_by(image_manifest_v1_compatibility *config) -{ - size_t i = 0; - char *created_by = NULL; - size_t size = 0; - - if (config == NULL || config->container_config == NULL || config->container_config->cmd == NULL || - config->container_config->cmd_len == 0) { - return NULL; - } - - for (i = 0; i < config->container_config->cmd_len; i++) { - size += strlen(config->container_config->cmd[i]) + 1; // +1 for ' ' or '\0' - } - - created_by = util_common_calloc_s(size); - if (created_by == NULL) { - ERROR("out of memory"); - return NULL; - } - - for (i = 0; i < config->container_config->cmd_len; i++) { - if (i != 0) { - (void)strcat(created_by, " "); - } - (void)strcat(created_by, config->container_config->cmd[i]); - } - - return created_by; -} - -int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, - const registry_manifest_schema1 *manifest, docker_image_config_v2 *config) -{ - int i = 0; - int ret = 0; - size_t history_index = 0; - parser_error err = NULL; - image_manifest_v1_compatibility *v1config = NULL; - docker_image_history *history = NULL; - - if (layers == NULL || layers_len == 0 || config == NULL || manifest == NULL) { - ERROR("Invalid NULL param"); - return -1; - } - - config->rootfs = util_common_calloc_s(sizeof(docker_image_rootfs)); - config->history = util_common_calloc_s(sizeof(docker_image_history *) * layers_len); - if (config->rootfs == NULL || config->history == NULL) { - ERROR("out of memory"); - return -1; - } - config->rootfs->type = util_strdup_s(ROOTFS_TYPE); - - history_index = manifest->history_len - 1; - for (i = 0; i < layers_len; i++) { - v1config = image_manifest_v1_compatibility_parse_data(manifest->history[history_index]->v1compatibility, NULL, - &err); - if (v1config == NULL) { - ERROR("parse v1 compatibility config failed, err: %s", err); - ret = -1; - goto out; - } - free(err); - err = NULL; - - history = util_common_calloc_s(sizeof(docker_image_history)); - if (history == NULL) { - ERROR("out of memory"); - ret = -1; - goto out; - } - - history->created = v1config->created; - v1config->created = NULL; - history->author = v1config->author; - v1config->author = NULL; - history->created_by = convert_created_by(v1config); - history->comment = v1config->comment; - v1config->comment = NULL; - history->empty_layer = layers[i].empty_layer; - - config->history[i] = history; - history = NULL; - config->history_len++; - - free_image_manifest_v1_compatibility(v1config); - v1config = NULL; - history_index--; - if (layers[i].empty_layer) { - continue; - } - - ret = util_array_append(&config->rootfs->diff_ids, layers[i].diff_id); - if (ret != 0) { - ERROR("append diff id of layer %u to rootfs failed, diff id is %s", i, layers[i].diff_id); - ret = -1; - goto out; - } - config->rootfs->diff_ids_len++; - } - -out: - free(err); - err = NULL; - free_docker_image_history(history); - history = NULL; - free_image_manifest_v1_compatibility(v1config); - v1config = NULL; - - return ret; -} - -types_timestamp_t created_to_timestamp(char *created) -{ - int64_t nanos = 0; - types_timestamp_t timestamp = { 0 }; - - if (to_unix_nanos_from_str(created, &nanos) != 0) { - ERROR("Failed to get created time from image config"); - goto out; - } - - timestamp.has_seconds = true; - timestamp.seconds = nanos / Time_Second; - timestamp.has_nanos = true; - timestamp.nanos = nanos % Time_Second; - -out: - - return timestamp; -} - diff --git a/src/daemon/image/oci/registry/registry_common.h b/src/daemon/image/oci/registry/registry_common.h deleted file mode 100644 index 558063c..0000000 --- a/src/daemon/image/oci/registry/registry_common.h +++ /dev/null @@ -1,41 +0,0 @@ -/****************************************************************************** - * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. - * 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 - * 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 v2 for more details. - * Author: WuJing - * Create: 2020-06-13 - * Description: provide registry common functions - ********************************************************************************/ - -#ifndef __OCI_REGISTRY_COMMON_H -#define __OCI_REGISTRY_COMMON_H - -#include -#include -#include -#include "isula_libutils/docker_image_config_v2.h" -#include "isula_libutils/registry_manifest_schema1.h" -#include "isula_libutils/image_manifest_v1_compatibility.h" -#include "registry_type.h" -#include "utils_timestamp.h" - -#ifdef __cplusplus -extern "C" { -#endif - -void free_items_not_inherit(docker_image_config_v2 *config); -int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, const registry_manifest_schema1 *manifest, - docker_image_config_v2 *config); -types_timestamp_t created_to_timestamp(char *created); - -#ifdef __cplusplus -} -#endif - -#endif /* __OCI_REGISTRY_COMMON_H */ diff --git a/src/daemon/image/oci/registry/registry_type.c b/src/daemon/image/oci/registry_type.c similarity index 100% rename from src/daemon/image/oci/registry/registry_type.c rename to src/daemon/image/oci/registry_type.c diff --git a/src/daemon/image/oci/registry/registry_type.h b/src/daemon/image/oci/registry_type.h similarity index 98% rename from src/daemon/image/oci/registry/registry_type.h rename to src/daemon/image/oci/registry_type.h index 56b55ee..a54876e 100644 --- a/src/daemon/image/oci/registry/registry_type.h +++ b/src/daemon/image/oci/registry_type.h @@ -20,7 +20,6 @@ #include #include "utils_timestamp.h" -#include "utils_images.h" // 8 is enough for challenge, usually only one challenge is provided. #define CHALLENGE_MAX 8 @@ -107,4 +106,4 @@ void free_challenge(challenge *c); void free_layer_blob(layer_blob *layer); void free_pull_desc(pull_descriptor *desc); -#endif +#endif /* _IMAGE_REGISTRY_TYPE_H */ diff --git a/src/daemon/image/oci/storage/image_store/image_store.c b/src/daemon/image/oci/storage/image_store/image_store.c index 1aab7ca..f2baade 100644 --- a/src/daemon/image/oci/storage/image_store/image_store.c +++ b/src/daemon/image/oci/storage/image_store/image_store.c @@ -48,7 +48,6 @@ #include "isula_libutils/image_manifest_v1_compatibility.h" #include "registry_type.h" #include "mediatype.h" -#include "registry_common.h" #include "storage.h" // the name of the big data item whose contents we consider useful for computing a "digest" of the diff --git a/src/daemon/image/oci/utils_images.c b/src/daemon/image/oci/utils_images.c index 37bc30e..67a5ea6 100644 --- a/src/daemon/image/oci/utils_images.c +++ b/src/daemon/image/oci/utils_images.c @@ -392,3 +392,162 @@ out: } return diff_id; } + +void free_items_not_inherit(docker_image_config_v2 *config) +{ + size_t i = 0; + + if (config == NULL) { + return; + } + free(config->id); + config->id = NULL; + free(config->parent); + config->parent = NULL; + config->size = 0; + free_docker_image_rootfs(config->rootfs); + config->rootfs = NULL; + + for (i = 0; i < config->history_len; i++) { + free_docker_image_history(config->history[i]); + config->history[i] = NULL; + } + config->history = NULL; + config->history_len = 0; + + return; +} + +static char *convert_created_by(image_manifest_v1_compatibility *config) +{ + size_t i = 0; + char *created_by = NULL; + size_t size = 0; + + if (config == NULL || config->container_config == NULL || config->container_config->cmd == NULL || + config->container_config->cmd_len == 0) { + return NULL; + } + + for (i = 0; i < config->container_config->cmd_len; i++) { + size += strlen(config->container_config->cmd[i]) + 1; // +1 for ' ' or '\0' + } + + created_by = util_common_calloc_s(size); + if (created_by == NULL) { + ERROR("out of memory"); + return NULL; + } + + for (i = 0; i < config->container_config->cmd_len; i++) { + if (i != 0) { + (void)strcat(created_by, " "); + } + (void)strcat(created_by, config->container_config->cmd[i]); + } + + return created_by; +} + +int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, + const registry_manifest_schema1 *manifest, docker_image_config_v2 *config) +{ + int i = 0; + int ret = 0; + size_t history_index = 0; + parser_error err = NULL; + image_manifest_v1_compatibility *v1config = NULL; + docker_image_history *history = NULL; + + if (layers == NULL || layers_len == 0 || config == NULL || manifest == NULL) { + ERROR("Invalid NULL param"); + return -1; + } + + config->rootfs = util_common_calloc_s(sizeof(docker_image_rootfs)); + config->history = util_common_calloc_s(sizeof(docker_image_history *) * layers_len); + if (config->rootfs == NULL || config->history == NULL) { + ERROR("out of memory"); + return -1; + } + config->rootfs->type = util_strdup_s(ROOTFS_TYPE); + + history_index = manifest->history_len - 1; + for (i = 0; i < layers_len; i++) { + v1config = image_manifest_v1_compatibility_parse_data(manifest->history[history_index]->v1compatibility, NULL, + &err); + if (v1config == NULL) { + ERROR("parse v1 compatibility config failed, err: %s", err); + ret = -1; + goto out; + } + free(err); + err = NULL; + + history = util_common_calloc_s(sizeof(docker_image_history)); + if (history == NULL) { + ERROR("out of memory"); + ret = -1; + goto out; + } + + history->created = v1config->created; + v1config->created = NULL; + history->author = v1config->author; + v1config->author = NULL; + history->created_by = convert_created_by(v1config); + history->comment = v1config->comment; + v1config->comment = NULL; + history->empty_layer = layers[i].empty_layer; + + config->history[i] = history; + history = NULL; + config->history_len++; + + free_image_manifest_v1_compatibility(v1config); + v1config = NULL; + history_index--; + if (layers[i].empty_layer) { + continue; + } + + ret = util_array_append(&config->rootfs->diff_ids, layers[i].diff_id); + if (ret != 0) { + ERROR("append diff id of layer %u to rootfs failed, diff id is %s", i, layers[i].diff_id); + ret = -1; + goto out; + } + config->rootfs->diff_ids_len++; + } + +out: + free(err); + err = NULL; + free_docker_image_history(history); + history = NULL; + free_image_manifest_v1_compatibility(v1config); + v1config = NULL; + + return ret; +} + +types_timestamp_t created_to_timestamp(char *created) +{ + int64_t nanos = 0; + types_timestamp_t timestamp = { 0 }; + + if (to_unix_nanos_from_str(created, &nanos) != 0) { + ERROR("Failed to get created time from image config"); + goto out; + } + + timestamp.has_seconds = true; + timestamp.seconds = nanos / Time_Second; + timestamp.has_nanos = true; + timestamp.nanos = nanos % Time_Second; + +out: + + return timestamp; +} + diff --git a/src/daemon/image/oci/utils_images.h b/src/daemon/image/oci/utils_images.h index de0d3da..6c07708 100644 --- a/src/daemon/image/oci/utils_images.h +++ b/src/daemon/image/oci/utils_images.h @@ -19,6 +19,11 @@ #include #include #include +#include "isula_libutils/docker_image_config_v2.h" +#include "isula_libutils/registry_manifest_schema1.h" +#include "isula_libutils/image_manifest_v1_compatibility.h" +#include "registry_type.h" +#include "utils_timestamp.h" #ifdef __cplusplus extern "C" { @@ -44,6 +49,10 @@ char *oci_full_image_name(const char *host, const char *name, const char *tag); char *oci_strip_dockerio_prefix(const char *name); char *make_big_data_base_name(const char *key); char *oci_calc_diffid(const char *file); +void free_items_not_inherit(docker_image_config_v2 *config); +int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, const registry_manifest_schema1 *manifest, + docker_image_config_v2 *config); +types_timestamp_t created_to_timestamp(char *created); #ifdef __cplusplus } diff --git a/test/image/oci/storage/images/CMakeLists.txt b/test/image/oci/storage/images/CMakeLists.txt index 083bf7a..1550944 100644 --- a/test/image/oci/storage/images/CMakeLists.txt +++ b/test/image/oci/storage/images/CMakeLists.txt @@ -19,8 +19,7 @@ add_executable(${EXE} ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_timestamp.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/utils_images.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/image_store/image_type.c - ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/registry/registry_common.c - ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/registry/registry_type.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/registry_type.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/image_store/image_store.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/storage_mock.cc storage_images_ut.cpp) -- GitLab