[PATCH] Add OCI register operation

Gao Xiang hsiangkao at linux.alibaba.com
Tue Jul 9 16:05:57 AEST 2024


Hi,


On 2024/7/5 14:15, saz97 wrote:

Thanks for the patch!

it would be better to write a commit message for this, and
write your SoB here, see:
https://erofs.docs.kernel.org/en/latest/developers.html

> ---
>   include/erofs/io.h |   1 +
>   lib/oci_registry.c | 511 +++++++++++++++++++++++++++++++++++++++++++++
>   2 files changed, 512 insertions(+)
>   create mode 100644 lib/oci_registry.c
> 
> diff --git a/include/erofs/io.h b/include/erofs/io.h
> index f53abed..e8b6008 100644
> --- a/include/erofs/io.h
> +++ b/include/erofs/io.h
> @@ -16,6 +16,7 @@ extern "C"
>   #define _GNU_SOURCE
>   #endif
>   #include <unistd.h>
> +
>   #include "defs.h"

It's an unnecessary change, I think you could just drop this.

>   
>   #ifndef O_BINARY
> diff --git a/lib/oci_registry.c b/lib/oci_registry.c
> new file mode 100644
> index 0000000..37fe357
> --- /dev/null
> +++ b/lib/oci_registry.c
> @@ -0,0 +1,511 @@
> +#include <stdio.h>
> +#include <curl/curl.h>
> +#include <json-c/json.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include "erofs/io.h"
> +
> +#define TOKEN_MODE 1
> +#define IMAGE_INDEX_MODE 2
> +#define MANIFEST_MODE 3
> +#define BLOB_MODE 4



> +
> +struct MemoryStruct {

erofs-utils doesn't use this coding style, please also
see the web page above.

> +    char *memory;
> +    size_t size;
> +};
> +
> +CURLM *get_multi_handle() {

please add erofs_ prefix at least, also I think it could
be

CURLM *erofs_oci_registry_get_multi_handle(void)
{
}

> +    static CURLM *multi_handle = NULL;
> +    if (multi_handle == NULL) {
> +        multi_handle = curl_multi_init();
> +    }
> +    return multi_handle;
> +}
> +
> +static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {

naming needs to be changed too.

> +    size_t realSize = size * nmemb;
> +    struct MemoryStruct *mem = (struct MemoryStruct *)userp;
> +
> +    char *ptr = realloc(mem->memory, mem->size + realSize + 1); // +1 for null terminator
> +    if (ptr == NULL) {
> +        fprintf(stderr, "realloc failed\n");
> +        return 0;
> +    }
> +
> +    mem->memory = ptr;
> +    memcpy(&(mem->memory[mem->size]), contents, realSize);
> +    mem->size += realSize;
> +    mem->memory[mem->size] = 0; // Null terminator
> +    return realSize;
> +}
> +
> +ssize_t oci_registry_read(struct erofs_vfile *vf, void *buf, size_t len) {
> +    // 取出指向 MemoryStruct 的指针

We don't use Chinese comment in the whole project.

> +    struct MemoryStruct *memoryStruct = (struct MemoryStruct *)(vf->payload);
> +
> +    // 检查读取长度是否超出 memory 的大小


// comment should be avoided, /* */ is preferred.

> +    if (len > memoryStruct->size) {
> +        len = memoryStruct->size; // 限制读取长度为 memory 的大小
> +    }

if the block contains only one statement, the brace pair is unneeded.


> +
> +    // 将 memoryStruct->memory 中的数据拷贝到 buf 中
> +    memcpy(buf, memoryStruct->memory, len);
> +
> +    // 返回实际读取的字节数
> +    return len;
> +}
> +
> +ssize_t oci_registry_pread(struct erofs_vfile *vf, void *buf, u64 offset, size_t len) {
> +    // 取出指向 MemoryStruct 的指针
> +    struct MemoryStruct *memoryStruct = (struct MemoryStruct *)(vf->payload);
> +
> +    // 检查 offset 是否超出 memory 的大小
> +    if (offset >= memoryStruct->size) {
> +        return 0; // 如果 offset 超出大小,返回0表示没有读取任何数据
> +    }
> +
> +    // 检查读取长度是否超出 memory 剩余的大小
> +    if (offset + len > memoryStruct->size) {
> +        len = memoryStruct->size - offset; // 限制读取长度为 memory 剩余的大小
> +    }
> +
> +    // 将 memoryStruct->memory 中从 offset 开始的数据拷贝到 buf 中
> +    memcpy(buf, memoryStruct->memory + offset, len);
> +
> +    // 返回实际读取的字节数
> +    return len;
> +}
> +
> +off_t oci_registry_lseek(struct erofs_vfile *vf, u64 offset, int whence) {
> +    // 取出指向 MemoryStruct 的指针
> +    struct MemoryStruct *memoryStruct = (struct MemoryStruct *)(vf->payload);
> +
> +    u64 new_offset = 0;
> +
> +    // 根据 whence 参数计算新的偏移量
> +    switch (whence) {
> +        case SEEK_SET:
> +            new_offset = offset;
> +            break;
> +        case SEEK_CUR:
> +            new_offset = vf->offset + offset;
> +            break;
> +        case SEEK_END:
> +            new_offset = memoryStruct->size + offset;
> +            break;
> +        default:
> +            return -1; // 无效的 whence 参数
> +    }
> +
> +    // 检查新的偏移量是否超出文件大小
> +    if (new_offset > memoryStruct->size) {
> +        return -1; // 超出文件大小,返回错误
> +    }
> +
> +    // 更新结构体中的偏移量
> +    vf->offset = new_offset;
> +
> +    // 返回新的偏移量
> +    return new_offset;
> +}
> +
> +char *get_token(struct MemoryStruct *data) {

erofs_oci_registry_get_token...

Please fix all similar cases, anyway..

Thanks,
Gao Xiang



More information about the Linux-erofs mailing list