summaryrefslogtreecommitdiff
path: root/0171-registry-module-code-improve.patch
blob: 96ca3267d0b6200ea8cc8c875df7678461ef47bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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