- `registry` and `repository` doesn't need to be passed in again;

 - `ctx->auth_header` can be assigned directly.

Cc: Chengyu Zhu <hudson...@tencent.com>
Signed-off-by: Gao Xiang <hsiang...@linux.alibaba.com>
---
 lib/remotes/oci.c | 46 +++++++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/lib/remotes/oci.c b/lib/remotes/oci.c
index f2b08b2..189b634 100644
--- a/lib/remotes/oci.c
+++ b/lib/remotes/oci.c
@@ -511,9 +511,8 @@ static char *ocierofs_discover_auth_endpoint(struct 
ocierofs_ctx *ctx,
        return result;
 }
 
-static char *ocierofs_get_auth_token(struct ocierofs_ctx *ctx, const char 
*registry,
-                                    const char *repository, const char 
*username,
-                                    const char *password)
+static int ocierofs_get_auth_token(struct ocierofs_ctx *ctx, const char 
*username,
+                                  const char *password)
 {
        static const char * const auth_patterns[] = {
                "https://%s/v2/auth";,
@@ -521,8 +520,9 @@ static char *ocierofs_get_auth_token(struct ocierofs_ctx 
*ctx, const char *regis
                "https://%s/token";,
                NULL,
        };
-       char *auth_header = NULL;
-       char *discovered_auth_url = NULL;
+       const char *registry = ctx->registry;
+       const char *repository = ctx->repository;
+       char *auth_header, *discovered_auth_url;
        char *discovered_service = NULL;
        const char *service = registry;
        bool docker_reg;
@@ -535,8 +535,10 @@ static char *ocierofs_get_auth_token(struct ocierofs_ctx 
*ctx, const char *regis
                auth_header = ocierofs_get_auth_token_with_url(ctx,
                                "https://auth.docker.io/token";, service, 
repository,
                                username, password);
-               if (!IS_ERR(auth_header))
-                       return auth_header;
+               if (!IS_ERR(auth_header)) {
+                       ctx->auth_header = auth_header;
+                       return 0;
+               }
        }
 
        discovered_auth_url = ocierofs_discover_auth_endpoint(ctx, registry, 
repository);
@@ -579,8 +581,10 @@ static char *ocierofs_get_auth_token(struct ocierofs_ctx 
*ctx, const char *regis
                                                               username, 
password);
                free(discovered_auth_url);
                free(discovered_service);
-               if (!IS_ERR(auth_header))
-                       return auth_header;
+               if (!IS_ERR(auth_header)) {
+                       ctx->auth_header = auth_header;
+                       return 0;
+               }
        }
 
        for (i = 0; auth_patterns[i]; i++) {
@@ -594,12 +598,16 @@ static char *ocierofs_get_auth_token(struct ocierofs_ctx 
*ctx, const char *regis
                                                               username, 
password);
                free(auth_url);
 
-               if (!IS_ERR(auth_header))
-                       return auth_header;
-               if (!docker_reg)
-                       return NULL;
+               if (!IS_ERR(auth_header)) {
+                       ctx->auth_header = auth_header;
+                       return 0;
+               }
+               if (!docker_reg) {
+                       ctx->auth_header = NULL;
+                       return 0;
+               }
        }
-       return ERR_PTR(-ENOENT);
+       return -ENOENT;
 }
 
 static char *ocierofs_get_manifest_digest(struct ocierofs_ctx *ctx,
@@ -863,19 +871,15 @@ static int ocierofs_prepare_auth(struct ocierofs_ctx *ctx,
                                 const char *password)
 {
        char *auth_header = NULL;
-       int ret = 0;
+       int ret;
 
        ctx->using_basic = false;
        free(ctx->auth_header);
        ctx->auth_header = NULL;
 
-       auth_header = ocierofs_get_auth_token(ctx, ctx->registry,
-                                             ctx->repository,
-                                             username, password);
-       if (!IS_ERR(auth_header)) {
-               ctx->auth_header = auth_header;
+       ret = ocierofs_get_auth_token(ctx, username, password);
+       if (!ret)
                return 0;
-       }
 
        if (username && password && *username && *password) {
                ret = ocierofs_curl_setup_basic_auth(ctx->curl,
-- 
2.43.5


Reply via email to