From 4f030e07e99dfe996897b69c9d950f3226363afe Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Wed, 11 Dec 2024 04:04:45 +1400
Subject: [PATCH 05/11] registry module code improve

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 src/daemon/modules/image/oci/oci_pull.c       |  2 +-
 .../modules/image/oci/registry/http_request.c | 30 +++-----
 .../modules/image/oci/registry/registry.c     | 74 +++++++++----------
 .../image/oci/registry/registry_apiv1.c       | 11 +--
 .../image/oci/registry/registry_apiv2.c       | 62 +++++-----------
 5 files changed, 72 insertions(+), 107 deletions(-)

diff --git a/src/daemon/modules/image/oci/oci_pull.c b/src/daemon/modules/image/oci/oci_pull.c
index 1c486974..245d14fd 100644
--- a/src/daemon/modules/image/oci/oci_pull.c
+++ b/src/daemon/modules/image/oci/oci_pull.c
@@ -105,7 +105,7 @@ static int pull_image(const im_pull_request *request, progress_status_map *progr
     options = (registry_pull_options *)util_common_calloc_s(sizeof(registry_pull_options));
     if (options == NULL) {
         ERROR("Out of memory");
-        goto out;
+        return ret;
     }
 
     if (request->auth != NULL) {
diff --git a/src/daemon/modules/image/oci/registry/http_request.c b/src/daemon/modules/image/oci/registry/http_request.c
index 80fc2184..b9b29c39 100644
--- a/src/daemon/modules/image/oci/registry/http_request.c
+++ b/src/daemon/modules/image/oci/registry/http_request.c
@@ -16,9 +16,6 @@
 #define _GNU_SOURCE /* See feature_test_macros(7) */
 #include "http_request.h"
 #include <curl/curl.h>
-#include <isula_libutils/json_common.h>
-#include <isula_libutils/log.h>
-#include <isula_libutils/registry_token.h>
 #include <pthread.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -27,6 +24,10 @@
 #include <strings.h>
 #include <time.h>
 
+#include <isula_libutils/json_common.h>
+#include <isula_libutils/log.h>
+#include <isula_libutils/registry_token.h>
+
 #include "buffer.h"
 #include "certs.h"
 #include "err_msg.h"
@@ -128,7 +129,6 @@ static int setup_ssl_config(pull_descriptor *desc, struct http_get_options *opti
     options->ssl_verify_host = !desc->skip_tls_verify;
 
 out:
-
     free(host);
     host = NULL;
 
@@ -437,16 +437,14 @@ static int setup_common_options(pull_descriptor *desc, struct http_get_options *
     if (ret != 0) {
         ERROR("Failed setup ssl config");
         isulad_try_set_error_message("setup ssl config failed");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     if (custom_headers != NULL) {
         options->custom_headers = util_str_array_dup(custom_headers, util_array_len(custom_headers));
         if (options->custom_headers == NULL) {
             ERROR("dup headers failed");
-            ret = -1;
-            goto out;
+            return -1;
         }
     }
 
@@ -454,14 +452,10 @@ static int setup_common_options(pull_descriptor *desc, struct http_get_options *
     if (ret != 0) {
         ERROR("setup auth challenges failed");
         isulad_try_set_error_message("setup auth challenges failed");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     options->debug = false;
-
-out:
-
     return ret;
 }
 
@@ -478,21 +472,16 @@ static int setup_get_token_options(pull_descriptor *desc, struct http_get_option
     ret = setup_ssl_config(desc, options, url);
     if (ret != 0) {
         ERROR("Failed setup ssl config");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     ret = setup_auth_basic(desc, &options->custom_headers);
     if (ret != 0) {
         ERROR("dup headers failed");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     options->debug = false;
-
-out:
-
     return ret;
 }
 
@@ -526,7 +515,6 @@ static int http_request_buf_options(pull_descriptor *desc, struct http_get_optio
 
     *output = util_strdup_s(output_buffer->contents);
 out:
-
     buffer_free(output_buffer);
 
     return ret;
diff --git a/src/daemon/modules/image/oci/registry/registry.c b/src/daemon/modules/image/oci/registry/registry.c
index 2e99255a..9a3b59a1 100644
--- a/src/daemon/modules/image/oci/registry/registry.c
+++ b/src/daemon/modules/image/oci/registry/registry.c
@@ -20,13 +20,21 @@
 #include <string.h>
 #include <limits.h>
 #include <errno.h>
+#include <pthread.h>
+#include <stdlib.h>
+
 #include <isula_libutils/docker_image_rootfs.h>
 #include <isula_libutils/json_common.h>
 #include <isula_libutils/oci_image_content_descriptor.h>
 #include <isula_libutils/oci_image_manifest.h>
 #include <isula_libutils/oci_image_spec.h>
-#include <pthread.h>
-#include <stdlib.h>
+#include <isula_libutils/registry_manifest_schema2.h>
+#include <isula_libutils/registry_manifest_schema1.h>
+#include <isula_libutils/docker_image_config_v2.h>
+#include <isula_libutils/image_manifest_v1_compatibility.h>
+#ifdef ENABLE_IMAGE_SEARCH
+#include <isula_libutils/image_search_image.h>
+#endif
 
 #include "mediatype.h"
 #include "isula_libutils/log.h"
@@ -35,13 +43,6 @@
 #include "registry_apiv1.h"
 #include "certs.h"
 #include "auths.h"
-#include "isula_libutils/registry_manifest_schema2.h"
-#include "isula_libutils/registry_manifest_schema1.h"
-#include "isula_libutils/docker_image_config_v2.h"
-#include "isula_libutils/image_manifest_v1_compatibility.h"
-#ifdef ENABLE_IMAGE_SEARCH
-#include "isula_libutils/image_search_image.h"
-#endif
 #include "sha256.h"
 #include "map.h"
 #include "linked_list.h"
@@ -536,7 +537,6 @@ static char *calc_chain_id(char *parent_chain_id, char *diff_id)
     full_digest = util_full_digest(digest);
 
 out:
-
     free(digest);
     digest = NULL;
 
@@ -797,7 +797,6 @@ static int set_config(pull_descriptor *desc, char *image_id)
     }
 
 out:
-
     free(config_str);
     config_str = NULL;
 
@@ -812,17 +811,15 @@ static int set_loaded_time(pull_descriptor *desc, char *image_id)
     if (!util_get_now_time_stamp(&now)) {
         ret = -1;
         ERROR("get now time stamp failed");
-        goto out;
+        return ret;
     }
 
     ret = storage_img_set_loaded_time(image_id, &now);
     if (ret != 0) {
         ERROR("set loaded time failed");
-        goto out;
+        return ret;
     }
 
-out:
-
     return ret;
 }
 
@@ -984,7 +981,6 @@ static int parse_docker_config(pull_descriptor *desc)
     desc->config.create_time = util_to_timestamp_from_str(config->created);
 
 out:
-
     free_docker_image_config_v2(config);
     config = NULL;
     free(err);
@@ -1084,17 +1080,15 @@ static int fetch_and_parse_config(pull_descriptor *desc)
     ret = fetch_config(desc);
     if (ret != 0) {
         ERROR("fetch config failed");
-        goto out;
+        return ret;
     }
 
     ret = parse_config(desc);
     if (ret != 0) {
         ERROR("parse config failed");
-        goto out;
+        return ret;
     }
 
-out:
-
     return ret;
 }
 
@@ -1110,17 +1104,15 @@ static int fetch_and_parse_manifest(pull_descriptor *desc)
     ret = fetch_manifest(desc);
     if (ret != 0) {
         ERROR("fetch manifest failed");
-        goto out;
+        return ret;
     }
 
     ret = parse_manifest(desc);
     if (ret != 0) {
         ERROR("parse manifest failed");
-        goto out;
+        return ret;
     }
 
-out:
-
     return ret;
 }
 
@@ -2116,6 +2108,26 @@ static void cached_layers_kvfree(void *key, void *value)
     return;
 }
 
+static void free_registry_global(registry_global *registry)
+{
+    if (registry == NULL) {
+        return;
+    }
+
+    if (registry->cond_inited) {
+        pthread_cond_destroy(&registry->cond);
+    }
+    if (registry->mutex_inited) {
+        pthread_mutex_destroy(&registry->mutex);
+    }
+    if (registry->image_mutex_inited) {
+        pthread_mutex_destroy(&registry->image_mutex);
+    }
+    map_free(registry->cached_layers);
+    registry->cached_layers = NULL;
+    free(registry);
+}
+
 int registry_init(char *auths_dir, char *certs_dir)
 {
     int ret = 0;
@@ -2160,18 +2172,7 @@ int registry_init(char *auths_dir, char *certs_dir)
 out:
 
     if (ret != 0) {
-        if (g_shared->cond_inited) {
-            pthread_cond_destroy(&g_shared->cond);
-        }
-        if (g_shared->mutex_inited) {
-            pthread_mutex_destroy(&g_shared->mutex);
-        }
-        if (g_shared->image_mutex_inited) {
-            pthread_mutex_destroy(&g_shared->image_mutex);
-        }
-        map_free(g_shared->cached_layers);
-        g_shared->cached_layers = NULL;
-        free(g_shared);
+        free_registry_global(g_shared);
         g_shared = NULL;
     }
 
@@ -2221,7 +2222,6 @@ int registry_login(registry_login_options *options)
     }
 
 out:
-
     free_pull_desc(desc);
     desc = NULL;
 
diff --git a/src/daemon/modules/image/oci/registry/registry_apiv1.c b/src/daemon/modules/image/oci/registry/registry_apiv1.c
index 6da24c1d..d45f3876 100644
--- a/src/daemon/modules/image/oci/registry/registry_apiv1.c
+++ b/src/daemon/modules/image/oci/registry/registry_apiv1.c
@@ -18,12 +18,16 @@
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
-#include <isula_libutils/http_parser.h>
-#include <isula_libutils/json_common.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <strings.h>
 
+#include <isula_libutils/http_parser.h>
+#include <isula_libutils/json_common.h>
+#include <isula_libutils/oci_image_index.h>
+#include <isula_libutils/registry_manifest_list.h>
+#include <isula_libutils/imagetool_search_result.h>
+
 #include "registry_type.h"
 #include "isula_libutils/log.h"
 #include "http.h"
@@ -31,9 +35,6 @@
 #include "utils.h"
 #include "parser.h"
 #include "mediatype.h"
-#include "isula_libutils/oci_image_index.h"
-#include "isula_libutils/registry_manifest_list.h"
-#include "isula_libutils/imagetool_search_result.h"
 #include "auths.h"
 #include "err_msg.h"
 #include "sha256.h"
diff --git a/src/daemon/modules/image/oci/registry/registry_apiv2.c b/src/daemon/modules/image/oci/registry/registry_apiv2.c
index dd49fab7..5d83b425 100644
--- a/src/daemon/modules/image/oci/registry/registry_apiv2.c
+++ b/src/daemon/modules/image/oci/registry/registry_apiv2.c
@@ -18,12 +18,15 @@
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
-#include <isula_libutils/http_parser.h>
-#include <isula_libutils/json_common.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <strings.h>
 
+#include <isula_libutils/http_parser.h>
+#include <isula_libutils/json_common.h>
+#include <isula_libutils/oci_image_index.h>
+#include <isula_libutils/registry_manifest_list.h>
+
 #include "registry_type.h"
 #include "isula_libutils/log.h"
 #include "http.h"
@@ -31,8 +34,6 @@
 #include "utils.h"
 #include "parser.h"
 #include "mediatype.h"
-#include "isula_libutils/oci_image_index.h"
-#include "isula_libutils/registry_manifest_list.h"
 #include "auths.h"
 #include "err_msg.h"
 #include "sha256.h"
@@ -60,7 +61,6 @@ static void set_body_null_if_exist(char *message)
 static int parse_http_header(char *resp_buf, size_t buf_size, struct parsed_http_message *message)
 {
     char *real_message = NULL;
-    int ret = 0;
 
     if (resp_buf == NULL || message == NULL) {
         ERROR("Invalid NULL param");
@@ -70,8 +70,7 @@ static int parse_http_header(char *resp_buf, size_t buf_size, struct parsed_http
     real_message = strstr(resp_buf, "HTTP/1.1");
     if (real_message == NULL) {
         ERROR("Failed to parse response, the response do not have HTTP/1.1");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     set_body_null_if_exist(real_message);
@@ -79,13 +78,10 @@ static int parse_http_header(char *resp_buf, size_t buf_size, struct parsed_http
     ret = parse_http(real_message, strlen(real_message), message, HTTP_RESPONSE);
     if (ret != 0) {
         ERROR("Failed to parse response: %s", real_message);
-        ret = -1;
-        goto out;
+        return -1;
     }
 
-out:
-
-    return ret;
+    return 0;
 }
 
 static int parse_challenges(pull_descriptor *desc, char *schema, char *params)
@@ -225,7 +221,6 @@ static void free_parsed_http_message(struct parsed_http_message **message)
     (*message)->body = NULL;
     free(*message);
     *message = NULL;
-    return;
 }
 
 static struct parsed_http_message *get_parsed_message(char *http_head)
@@ -386,7 +381,7 @@ static int registry_ping(pull_descriptor *desc)
     ret = registry_pingv2(desc, "https");
     if (ret == 0) {
         desc->protocol = util_strdup_s("https");
-        goto out;
+        return ret;
     }
 
     if (desc->insecure_registry) {
@@ -396,15 +391,13 @@ static int registry_ping(pull_descriptor *desc)
         ret = registry_pingv2(desc, "http");
         if (ret != 0) {
             ERROR("ping %s with http failed", desc->host);
-            goto out;
+            return ret;
         }
         desc->protocol = util_strdup_s("http");
     } else {
         ERROR("ping %s with https failed", desc->host);
     }
 
-out:
-
     return ret;
 }
 
@@ -552,7 +545,6 @@ static int parse_manifest_head(char *http_head, char **content_type, char **dige
     }
 
 out:
-
     if (ret != 0) {
         free(*content_type);
         *content_type = NULL;
@@ -584,19 +576,16 @@ static int append_manifests_accepts(char ***custom_headers)
         sret = snprintf(accept, MAX_ACCEPT_LEN, "Accept: %s", mediatypes[i]);
         if (sret < 0 || (size_t)sret >= MAX_ACCEPT_LEN) {
             ERROR("Failed to sprintf accept media type %s", mediatypes[i]);
-            ret = -1;
-            goto out;
+            return -1;
         }
 
         ret = util_array_append(custom_headers, accept);
         if (ret != 0) {
             ERROR("append accepts failed");
-            goto out;
+            return ret;
         }
     }
 
-out:
-
     return ret;
 }
 
@@ -703,7 +692,6 @@ static int fetch_manifest_list(pull_descriptor *desc, char *file, char **content
     }
 
 out:
-
     free(http_head);
     http_head = NULL;
     util_free_array(custom_headers);
@@ -727,7 +715,6 @@ static void try_log_resp_body(char *path, char *file)
         ERROR("Get %s response message body: %s", path, body);
     }
     free(body);
-    return;
 }
 
 static int fetch_data(pull_descriptor *desc, char *path, char *file, char *content_type, char *digest)
@@ -1009,25 +996,22 @@ static int fetch_manifest_data(pull_descriptor *desc, char *file, char **content
             ERROR("select manifest failed, manifests:%s", manifest_text);
             free(manifest_text);
             manifest_text = NULL;
-            goto out;
+            return ret;
         }
 
         sret = snprintf(path, sizeof(path), "/v2/%s/manifests/%s", desc->name, *digest);
         if (sret < 0 || (size_t)sret >= sizeof(path)) {
             ERROR("Failed to sprintf path for manifest");
-            ret = -1;
-            goto out;
+            return -1;
         }
 
         ret = fetch_data(desc, path, file, *content_type, *digest);
         if (ret != 0) {
             ERROR("registry: Get %s failed", path);
-            goto out;
+            return ret;
         }
     }
 
-out:
-
     return ret;
 }
 
@@ -1096,20 +1080,17 @@ int fetch_config(pull_descriptor *desc)
     sret = snprintf(path, sizeof(path), "/v2/%s/blobs/%s", desc->name, desc->config.digest);
     if (sret < 0 || (size_t)sret >= sizeof(path)) {
         ERROR("Failed to sprintf path for config");
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     ret = fetch_data(desc, path, file, desc->config.media_type, desc->config.digest);
     if (ret != 0) {
         ERROR("registry: Get %s failed", path);
-        goto out;
+       return ret;
     }
 
     desc->config.file = util_strdup_s(file);
 
-out:
-
     return ret;
 }
 
@@ -1141,18 +1122,15 @@ int fetch_layer(pull_descriptor *desc, size_t index)
     sret = snprintf(path, sizeof(path), "/v2/%s/blobs/%s", desc->name, layer->digest);
     if (sret < 0 || (size_t)sret >= sizeof(path)) {
         ERROR("Failed to sprintf path for layer %zu, name %s, digest %s", index, desc->name, layer->digest);
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     ret = fetch_data(desc, path, file, layer->media_type, layer->digest);
     if (ret != 0) {
         ERROR("registry: Get %s failed", path);
-        goto out;
+        return ret;
     }
 
-out:
-
     return ret;
 }
 
@@ -1185,7 +1163,6 @@ int parse_login(char *http_head, char *host)
     }
 
 out:
-
     free_parsed_http_message(&message);
 
     return ret;
@@ -1230,7 +1207,6 @@ int login_to_registry(pull_descriptor *desc)
         goto out;
     }
 out:
-
     free(resp_buffer);
     resp_buffer = NULL;
 
-- 
2.23.0

