From 2b6d3ed4c39cd7cb47244fd76937af9e36ad7321 Mon Sep 17 00:00:00 2001 From: Rong Zhou Date: Thu, 11 Apr 2024 08:25:53 +0000 Subject: [PATCH] now sdk could store multiple regions cache entries --- CMakeLists.txt | 7 +- docs/README.md | 70 +- docs/gist/client.c | 20 +- docs/gist/server.c | 114 ++- examples/cdn_get_bandwidth_data.c | 51 +- examples/cdn_get_flux_data.c | 51 +- examples/cdn_get_log_list.c | 40 +- examples/cdn_prefetch_urls.c | 27 +- examples/cdn_refresh_dirs.c | 28 +- examples/cdn_refresh_urls.c | 30 +- examples/fop_video_avthumb.c | 16 +- examples/rs_batch_change_mime.c | 42 +- examples/rs_batch_change_type.c | 44 +- examples/rs_batch_copy.c | 44 +- examples/rs_batch_delete.c | 38 +- examples/rs_batch_delete_after_days.c | 44 +- examples/rs_batch_move.c | 46 +- examples/rs_batch_stat.c | 40 +- examples/rs_change_mime.c | 14 +- examples/rs_change_type.c | 14 +- examples/rs_copy.c | 17 +- examples/rs_delete.c | 14 +- examples/rs_delete_after_days.c | 14 +- examples/rs_fetch.c | 28 +- examples/rs_move.c | 17 +- examples/rs_prefetch.c | 14 +- examples/rs_stat.c | 19 +- examples/rsf_list_bucket.c | 37 +- examples/rsf_list_files.c | 28 +- examples/up_chunk_upload.c | 20 +- examples/up_form_upload.c | 4 +- examples/up_multipart_sample.c | 14 +- examples/up_resumeable_upload.c | 4 +- gtests/auth_test.cc | 10 +- gtests/base_io_test.cc | 8 +- gtests/equal.c | 2 +- gtests/fop_test.cc | 2 +- gtests/io_put_test.cc | 6 +- gtests/multipart_test.cc | 22 +- gtests/recorder_test.cc | 22 +- gtests/region_test.cc | 22 +- gtests/resumable_io_test.cc | 6 +- gtests/rs_ops_test.cc | 36 +- gtests/rsf_test.cc | 2 +- hashmap/hashmap.c | 1154 +++++++++++++++++++++++++ hashmap/hashmap.h | 62 ++ qiniu/auth_mac.c | 6 +- qiniu/base.c | 3 +- qiniu/cdn.c | 358 +++++--- qiniu/fop.c | 4 +- qiniu/http.c | 20 +- qiniu/http.h | 5 +- qiniu/io.c | 20 +- qiniu/multipart_upload.c | 48 +- qiniu/qetag.c | 713 ++++++++------- qiniu/reader.c | 17 +- qiniu/recorder.c | 4 +- qiniu/recorder_utils.c | 8 +- qiniu/region.c | 332 ++++--- qiniu/resumable_io.c | 36 +- qiniu/rs.c | 56 +- qiniu/rsf.c | 6 +- 62 files changed, 2825 insertions(+), 1175 deletions(-) create mode 100644 hashmap/hashmap.c create mode 100644 hashmap/hashmap.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 18b8ac3d..00aff70f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,16 +19,17 @@ ENDIF() AUX_SOURCE_DIRECTORY(b64 DIR_SRCS_B64) AUX_SOURCE_DIRECTORY(cJSON DIR_SRCS_CJSON) +AUX_SOURCE_DIRECTORY(hashmap DIR_SRCS_HASHMAP) AUX_SOURCE_DIRECTORY(qiniu DIR_SRCS_QINIU) -MESSAGE(STATUS "Src file: ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_QINIU}") +MESSAGE(STATUS "Src file: ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_HASHMAP} ${DIR_SRCS_QINIU}") # 编译可执行程序 # ADD_EXECUTABLE(${PROJECT_NAME} ${DIR_SRCS}) # 如果要生成静态链接库 -ADD_LIBRARY(${PROJECT_NAME}_static STATIC ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_QINIU}) +ADD_LIBRARY(${PROJECT_NAME}_static STATIC ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_HASHMAP} ${DIR_SRCS_QINIU}) # 如果要生成动态链接库 -ADD_LIBRARY(${PROJECT_NAME} SHARED ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_QINIU}) +ADD_LIBRARY(${PROJECT_NAME} SHARED ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_HASHMAP} ${DIR_SRCS_QINIU}) TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") diff --git a/docs/README.md b/docs/README.md index acd00ac0..a375cce8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -68,6 +68,26 @@ C-SDK 使用 [cURL](http://curl.haxx.se/) 进行网络相关操作。无论是 如果在项目构建过程中出现环境相关的编译错误和链接错误,请确认这些选项是否都已经正确配置,以及所依赖的库是否都已经正确的安装。 +#### 通过 CMake 编译 + +如果想在 CMake 项目里使用 C-SDK,可以直接在 CMake 里导入 C-SDK: + +```cmake +INCLUDE(FetchContent) +FetchContent_Declare( + qiniu + GIT_REPOSITORY https://github.com/qiniu/c-sdk.git + GIT_TAG v7.7.0 +) +FetchContent_MakeAvailable(qiniu) + +FIND_PACKAGE(CURL REQUIRED) +FIND_PACKAGE(OpenSSL REQUIRED) + +TARGET_LINK_LIBRARIES(${PROJECT_NAME} PRIVATE qiniu m) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} PRIVATE ${CURL_LIBRARIES}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} PRIVATE ${OPENSSL_LIBRARIES}) +``` @@ -146,7 +166,7 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key) { Qiniu_RS_StatRet ret; Qiniu_Error err = Qiniu_RS_Stat(client, &ret, bucket, key); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -169,7 +189,7 @@ typedef struct _Qiniu_Error { typedef struct _Qiniu_RS_StatRet { const char* hash; const char* mimeType; - Qiniu_Int64 fsize; + Qiniu_Int64 fsize; Qiniu_Int64 putTime; } Qiniu_RS_StatRet; ``` @@ -259,7 +279,7 @@ char* upload(Qiniu_Client* client, char* uptoken, const char* key, const char* l Qiniu_Error err; Qiniu_Io_PutRet putRet; err = Qiniu_Io_PutFile(client, &putRet, uptoken, key, localFile, NULL); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return NULL; } @@ -413,7 +433,7 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key) { Qiniu_RS_StatRet ret; Qiniu_Error err = Qiniu_RS_Stat(client, &ret, bucket, key); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -427,7 +447,7 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key) typedef struct _Qiniu_RS_StatRet { const char* hash; const char* mimeType; - Qiniu_Int64 fsize; + Qiniu_Int64 fsize; Qiniu_Int64 putTime; } Qiniu_RS_StatRet; ``` @@ -442,7 +462,7 @@ typedef struct _Qiniu_RS_StatRet { void delete(Qiniu_Client* client, const char* bucket, const char* key) { Qiniu_Error err = Qiniu_RS_Delete(client, bucket, key); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -456,12 +476,12 @@ void delete(Qiniu_Client* client, const char* bucket, const char* key) 复制和移动操作,需要指定源路径和目标路径。 ```{c} -void copy(Qiniu_Client* client, - const char* bucketSrc, const char* keySrc, +void copy(Qiniu_Client* client, + const char* bucketSrc, const char* keySrc, const char* bucketDest, const char* keyDest) { Qiniu_Error err = Qiniu_RS_Copy(client, bucketSrc, keySrc, bucketDest, keyDest); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -470,12 +490,12 @@ void copy(Qiniu_Client* client, ``` ```{c} -void move(Qiniu_Client* client, - const char* bucketSrc, const char* keySrc, +void move(Qiniu_Client* client, + const char* bucketSrc, const char* keySrc, const char* bucketDest, const char* keyDest) { Qiniu_Error err = Qiniu_RS_Move(client, bucketSrc, keySrc, bucketDest, keyDest); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -494,7 +514,7 @@ void move(Qiniu_Client* client, 调用`Qiniu_RS_BatchStat`可以批量查看多个文件的属性信息。 ```{c} -void batchStat(Qiniu_Client* client, +void batchStat(Qiniu_Client* client, Qiniu_RS_EntryPath* entries, Qiniu_ItemCount entryCount) { Qiniu_RS_BatchStatRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchStatRet)); @@ -504,7 +524,7 @@ void batchStat(Qiniu_Client* client, while (curr < entryCount) { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) { printf("error: %s\n", rets[curr].error); } else { printf("hash: %s\n", rets[curr].data.hash); @@ -517,7 +537,7 @@ void batchStat(Qiniu_Client* client, free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -551,7 +571,7 @@ typedef struct _Qiniu_RS_BatchStatRet { typedef struct _Qiniu_RS_StatRet { const char* hash; const char* mimeType; - Qiniu_Int64 fsize; + Qiniu_Int64 fsize; Qiniu_Int64 putTime; } Qiniu_RS_StatRet; ``` @@ -563,7 +583,7 @@ typedef struct _Qiniu_RS_StatRet { 调用`Qiniu_RS_BatchDelete`可以批量删除多个文件。 ```{c} -void batchDelete(Qiniu_Client* client, +void batchDelete(Qiniu_Client* client, Qiniu_RS_EntryPath* entries, Qiniu_ItemCount entryCount) { Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); @@ -573,7 +593,7 @@ void batchDelete(Qiniu_Client* client, while (curr < entryCount) { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) { printf("error: %s\n", rets[curr].error); } curr++; @@ -581,7 +601,7 @@ void batchDelete(Qiniu_Client* client, free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -604,7 +624,7 @@ typedef struct _Qiniu_RS_BatchItemRet { 调用`Qiniu_RS_BatchCopy`可以批量复制多个文件。 ```{c} -void batchCopy(Qiniu_Client* client, +void batchCopy(Qiniu_Client* client, Qiniu_RS_EntryPathPair* entryPairs, Qiniu_ItemCount entryCount) { Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); @@ -614,14 +634,14 @@ void batchCopy(Qiniu_Client* client, while (curr < entryCount) { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) { printf("error: %s\n", rets[curr].error); } curr++; } free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } @@ -644,7 +664,7 @@ typedef struct _Qiniu_RS_EntryPathPair { 批量移动和批量复制很类似,唯一的区别就是调用`Qiniu_RS_BatchMove`。 ```{c} -void batchMove(Qiniu_Client* client, +void batchMove(Qiniu_Client* client, Qiniu_RS_EntryPathPair* entryPairs, Qiniu_ItemCount entryCount) { Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); @@ -654,7 +674,7 @@ void batchMove(Qiniu_Client* client, while (curr < entryCount) { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) { printf("error: %s\n", rets[curr].error); } curr++; @@ -662,7 +682,7 @@ void batchMove(Qiniu_Client* client, free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) { debug(client, err); return; } diff --git a/docs/gist/client.c b/docs/gist/client.c index 8e35d7ae..20aa6342 100644 --- a/docs/gist/client.c +++ b/docs/gist/client.c @@ -4,7 +4,7 @@ const char bucket[] = "a"; /* @gist debug */ -void debug(Qiniu_Client* client, Qiniu_Error err) +void debug(Qiniu_Client *client, Qiniu_Error err) { printf("error code: %d, message: %s\n", err.code, err.message); printf("respose header:\n%s", Qiniu_Buffer_CStr(&client->respHeader)); @@ -13,12 +13,13 @@ void debug(Qiniu_Client* client, Qiniu_Error err) /* @endgist */ /* @gist upload */ -char* upload(Qiniu_Client* client, char* uptoken, const char* key, const char* localFile) +char *upload(Qiniu_Client *client, char *uptoken, const char *key, const char *localFile) { Qiniu_Error err; Qiniu_Io_PutRet putRet; err = Qiniu_Io_PutFile(client, &putRet, uptoken, key, localFile, NULL); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return NULL; } @@ -27,7 +28,7 @@ char* upload(Qiniu_Client* client, char* uptoken, const char* key, const char* l /* @endgist */ /* @gist simple-upload */ -int simple_upload(Qiniu_Client* client, char* uptoken, const char* key, const char* localFile) +int simple_upload(Qiniu_Client *client, char *uptoken, const char *key, const char *localFile) { Qiniu_Error err; err = Qiniu_Io_PutFile(client, NULL, uptoken, key, localFile, NULL); @@ -36,7 +37,7 @@ int simple_upload(Qiniu_Client* client, char* uptoken, const char* key, const ch /* @endgist */ /* @gist resumable-upload */ -int resumable_upload(Qiniu_Client* client, char* uptoken, const char* key, const char* localFile) +int resumable_upload(Qiniu_Client *client, char *uptoken, const char *key, const char *localFile) { Qiniu_Error err; Qiniu_Rio_PutExtra extra; @@ -52,13 +53,12 @@ int main() /* @gist init */ Qiniu_Client client; - Qiniu_Global_Init(-1); /* 全局初始化函数,整个进程只需要调用一次 */ + Qiniu_Global_Init(-1); /* 全局初始化函数,整个进程只需要调用一次 */ Qiniu_Client_InitNoAuth(&client, 1024); /* HTTP客户端初始化。HTTP客户端是线程不安全的,不要在多个线程间共用 */ /* @endgist */ /* @gist init */ - Qiniu_Client_Cleanup(&client); /* 每个HTTP客户端使用完后释放 */ - Qiniu_Global_Cleanup(); /* 全局清理函数,只需要在进程退出时调用一次 */ - /* @endgist */ + Qiniu_Client_Cleanup(&client); /* 每个HTTP客户端使用完后释放 */ + Qiniu_Global_Cleanup(); /* 全局清理函数,只需要在进程退出时调用一次 */ + /* @endgist */ } - diff --git a/docs/gist/server.c b/docs/gist/server.c index f04f20dd..b50c56fb 100644 --- a/docs/gist/server.c +++ b/docs/gist/server.c @@ -1,7 +1,7 @@ #include "../../qiniu/rs.h" /* @gist debug */ -void debug(Qiniu_Client* client, Qiniu_Error err) +void debug(Qiniu_Client *client, Qiniu_Error err) { printf("\nerror code: %d, message: %s\n", err.code, err.message); printf("respose header:\n%s", Qiniu_Buffer_CStr(&client->respHeader)); @@ -10,11 +10,12 @@ void debug(Qiniu_Client* client, Qiniu_Error err) /* @endgist */ /* @gist stat */ -void stat(Qiniu_Client* client, const char* bucket, const char* key) +void stat(Qiniu_Client *client, const char *bucket, const char *key) { Qiniu_RS_StatRet ret; Qiniu_Error err = Qiniu_RS_Stat(client, &ret, bucket, key); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -23,10 +24,11 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key) /* @endgist */ /* @gist delete */ -void delete(Qiniu_Client* client, const char* bucket, const char* key) +void delete(Qiniu_Client *client, const char *bucket, const char *key) { Qiniu_Error err = Qiniu_RS_Delete(client, bucket, key); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -35,12 +37,13 @@ void delete(Qiniu_Client* client, const char* bucket, const char* key) /* @endgist */ /* @gist copy */ -void copy(Qiniu_Client* client, - const char* bucketSrc, const char* keySrc, - const char* bucketDest, const char* keyDest) +void copy(Qiniu_Client *client, + const char *bucketSrc, const char *keySrc, + const char *bucketDest, const char *keyDest) { Qiniu_Error err = Qiniu_RS_Copy(client, bucketSrc, keySrc, bucketDest, keyDest); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -49,12 +52,13 @@ void copy(Qiniu_Client* client, /* @endgist */ /* @gist move */ -void move(Qiniu_Client* client, - const char* bucketSrc, const char* keySrc, - const char* bucketDest, const char* keyDest) +void move(Qiniu_Client *client, + const char *bucketSrc, const char *keySrc, + const char *bucketDest, const char *keyDest) { Qiniu_Error err = Qiniu_RS_Move(client, bucketSrc, keySrc, bucketDest, keyDest); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -63,19 +67,23 @@ void move(Qiniu_Client* client, /* @endgist */ /* @gist batchStat */ -void batchStat(Qiniu_Client* client, - Qiniu_RS_EntryPath* entries, Qiniu_ItemCount entryCount) +void batchStat(Qiniu_Client *client, + Qiniu_RS_EntryPath *entries, Qiniu_ItemCount entryCount) { - Qiniu_RS_BatchStatRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchStatRet)); + Qiniu_RS_BatchStatRet *rets = calloc(entryCount, sizeof(Qiniu_RS_BatchStatRet)); Qiniu_Error err = Qiniu_RS_BatchStat(client, rets, entries, entryCount); int curr = 0; - while (curr < entryCount) { + while (curr < entryCount) + { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) + { printf("error: %s\n", rets[curr].error); - } else { + } + else + { printf("hash: %s\n", rets[curr].data.hash); printf("mimeType: %s\n", rets[curr].data.mimeType); printf("fsize: %lld\n", rets[curr].data.fsize); @@ -86,7 +94,8 @@ void batchStat(Qiniu_Client* client, free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -94,17 +103,19 @@ void batchStat(Qiniu_Client* client, /* @endgist */ /* @gist batchDelete */ -void batchDelete(Qiniu_Client* client, - Qiniu_RS_EntryPath* entries, Qiniu_ItemCount entryCount) +void batchDelete(Qiniu_Client *client, + Qiniu_RS_EntryPath *entries, Qiniu_ItemCount entryCount) { - Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); + Qiniu_RS_BatchItemRet *rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); Qiniu_Error err = Qiniu_RS_BatchDelete(client, rets, entries, entryCount); int curr = 0; - while (curr < entryCount) { + while (curr < entryCount) + { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) + { printf("error: %s\n", rets[curr].error); } curr++; @@ -112,7 +123,8 @@ void batchDelete(Qiniu_Client* client, free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -120,17 +132,19 @@ void batchDelete(Qiniu_Client* client, /* @endgist */ /* @gist batchMove */ -void batchMove(Qiniu_Client* client, - Qiniu_RS_EntryPathPair* entryPairs, Qiniu_ItemCount entryCount) +void batchMove(Qiniu_Client *client, + Qiniu_RS_EntryPathPair *entryPairs, Qiniu_ItemCount entryCount) { - Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); + Qiniu_RS_BatchItemRet *rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); Qiniu_Error err = Qiniu_RS_BatchMove(client, rets, entryPairs, entryCount); int curr = 0; - while (curr < entryCount) { + while (curr < entryCount) + { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) + { printf("error: %s\n", rets[curr].error); } curr++; @@ -138,7 +152,8 @@ void batchMove(Qiniu_Client* client, free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -146,24 +161,27 @@ void batchMove(Qiniu_Client* client, /* @endgist */ /* @gist batchCopy */ -void batchCopy(Qiniu_Client* client, - Qiniu_RS_EntryPathPair* entryPairs, Qiniu_ItemCount entryCount) +void batchCopy(Qiniu_Client *client, + Qiniu_RS_EntryPathPair *entryPairs, Qiniu_ItemCount entryCount) { - Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); + Qiniu_RS_BatchItemRet *rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet)); Qiniu_Error err = Qiniu_RS_BatchCopy(client, rets, entryPairs, entryCount); int curr = 0; - while (curr < entryCount) { + while (curr < entryCount) + { printf("\ncode: %d\n", rets[curr].code); - if (rets[curr].code != 200) { + if (rets[curr].code != Qiniu_OK.code) + { printf("error: %s\n", rets[curr].error); } curr++; } free(rets); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { debug(client, err); return; } @@ -171,7 +189,7 @@ void batchCopy(Qiniu_Client* client, /* @endgist */ /* @gist uptoken */ -char* uptoken(Qiniu_Client* client, const char* bucket) +char *uptoken(Qiniu_Client *client, const char *bucket) { Qiniu_RS_PutPolicy putPolicy; Qiniu_Zero(putPolicy); @@ -181,10 +199,10 @@ char* uptoken(Qiniu_Client* client, const char* bucket) /* @endgist */ /* @gist downloadUrl */ -char* downloadUrl(Qiniu_Client* client, const char* domain, const char* key) +char *downloadUrl(Qiniu_Client *client, const char *domain, const char *key) { - char* url; - char* baseUrl; + char *url; + char *baseUrl; Qiniu_RS_GetPolicy getPolicy; Qiniu_Zero(getPolicy); @@ -192,7 +210,7 @@ char* downloadUrl(Qiniu_Client* client, const char* domain, const char* key) url = Qiniu_RS_GetPolicy_MakeRequest(&getPolicy, baseUrl, NULL); Qiniu_Free(baseUrl); - return url; // When url is no longer being used, free it by Qiniu_Free. + return url; // When url is no longer being used, free it by Qiniu_Free. } /* @endgist */ @@ -204,12 +222,12 @@ int main() QINIU_ACCESS_KEY = ""; QINIU_SECRET_KEY = ""; - Qiniu_Servend_Init(-1); /* 全局初始化函数,整个进程只需要调用一次 */ + Qiniu_Servend_Init(-1); /* 全局初始化函数,整个进程只需要调用一次 */ Qiniu_Client_InitMacAuth(&client, 1024, NULL); /* HTTP客户端初始化。HTTP客户端是线程不安全的,不要在多个线程间共用 */ /* @endgist */ stat(&client, "bucket", "key"); - delete(&client, "bucket", "key"); + delete (&client, "bucket", "key"); copy(&client, "bucket1", "key1", "bucket2", "key2"); move(&client, "bucket1", "key1", "bucket2", "key2"); @@ -239,7 +257,7 @@ int main() batchMove(&client, entryPairs, 2); /* @gist init */ - Qiniu_Client_Cleanup(&client); /* 每个HTTP客户端使用完后释放 */ - Qiniu_Servend_Cleanup(); /* 全局清理函数,只需要在进程退出时调用一次 */ - /* @endgist */ + Qiniu_Client_Cleanup(&client); /* 每个HTTP客户端使用完后释放 */ + Qiniu_Servend_Cleanup(); /* 全局清理函数,只需要在进程退出时调用一次 */ + /* @endgist */ } diff --git a/examples/cdn_get_bandwidth_data.c b/examples/cdn_get_bandwidth_data.c index 04f6b05e..9c5671fe 100644 --- a/examples/cdn_get_bandwidth_data.c +++ b/examples/cdn_get_bandwidth_data.c @@ -1,7 +1,8 @@ #include "../qiniu/cdn.h" #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Client client; Qiniu_CDN_BandwidthRet ret; Qiniu_Error error; @@ -14,25 +15,26 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - - //urls to refresh + // urls to refresh char *domains[] = { - "csdk.qiniudn.com", - "javasdk.qiniudn.com" - }; + "csdk.qiniudn.com", + "javasdk.qiniudn.com"}; int domainsCount = 2; char *startDate = "2017-08-01"; char *endDate = "2017-08-09"; char *g = "day"; - //init + // init Qiniu_Zero(ret); Qiniu_Client_InitMacAuth(&client, 1024, &mac); error = Qiniu_CDN_GetBandwidthData(&client, &ret, startDate, endDate, g, domains, domainsCount); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("get domain bandwidth error.\n"); debug_log(&client, error); - } else { + } + else + { printf("get domain bandwidth success.\n\n"); printf("Code: %d\n", ret.code); printf("Error: %s\n", ret.error); @@ -40,30 +42,40 @@ int main(int argc, char **argv) { printf("-----------\n"); - for (i = 0; i < ret.timeCount; i++) { + for (i = 0; i < ret.timeCount; i++) + { printf("%s\t", ret.time[i]); } printf("\n"); - //data - for (i = 0; i < ret.domainsCount; i++) { + // data + for (i = 0; i < ret.domainsCount; i++) + { printf("Domain:%s\n", domains[i]); Qiniu_CDN_BandwidthData bandData = ret.data[i]; - if (bandData.chinaCount == 0) { + if (bandData.chinaCount == 0) + { printf("China: no bandwidth data\n"); - } else { + } + else + { printf("China: bandwidth data\n"); - for (j = 0; j < bandData.chinaCount; j++) { + for (j = 0; j < bandData.chinaCount; j++) + { printf("%lld\t", bandData.china[j]); } printf("\n"); } - if (bandData.overseaCount == 0) { + if (bandData.overseaCount == 0) + { printf("Oversea: no bandwidth data\n"); - } else { + } + else + { printf("Oversea: bandwidth data\n"); - for (j = 0; j < bandData.overseaCount; j++) { + for (j = 0; j < bandData.overseaCount; j++) + { printf("%lld\t", bandData.oversea[j]); } printf("\n"); @@ -71,8 +83,7 @@ int main(int argc, char **argv) { printf("-----------\n"); } - Qiniu_Free_CDNBandwidthRet(&ret); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/cdn_get_flux_data.c b/examples/cdn_get_flux_data.c index 1a79f2ab..fdc74de3 100644 --- a/examples/cdn_get_flux_data.c +++ b/examples/cdn_get_flux_data.c @@ -1,7 +1,8 @@ #include "../qiniu/cdn.h" #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Client client; Qiniu_CDN_FluxRet ret; Qiniu_Error error; @@ -14,25 +15,26 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - - //urls to refresh + // urls to refresh char *domains[] = { - "csdk.qiniudn.com", - "javasdk.qiniudn.com" - }; + "csdk.qiniudn.com", + "javasdk.qiniudn.com"}; int domainsCount = 2; char *startDate = "2017-08-01"; char *endDate = "2017-08-09"; char *g = "day"; - //init + // init Qiniu_Zero(ret); Qiniu_Client_InitMacAuth(&client, 1024, &mac); error = Qiniu_CDN_GetFluxData(&client, &ret, startDate, endDate, g, domains, domainsCount); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("get domain flux error.\n"); debug_log(&client, error); - } else { + } + else + { printf("get domain flux success.\n\n"); printf("Code: %d\n", ret.code); printf("Error: %s\n", ret.error); @@ -40,30 +42,40 @@ int main(int argc, char **argv) { printf("-----------\n"); - for (i = 0; i < ret.timeCount; i++) { + for (i = 0; i < ret.timeCount; i++) + { printf("%s\t", ret.time[i]); } printf("\n"); - //data - for (i = 0; i < ret.domainsCount; i++) { + // data + for (i = 0; i < ret.domainsCount; i++) + { printf("Domain:%s\n", domains[i]); Qiniu_CDN_FluxData fluxData = ret.data[i]; - if (fluxData.chinaCount == 0) { + if (fluxData.chinaCount == 0) + { printf("China: no flux data\n"); - } else { + } + else + { printf("China: flux data\n"); - for (j = 0; j < fluxData.chinaCount; j++) { + for (j = 0; j < fluxData.chinaCount; j++) + { printf("%lld\t", fluxData.china[j]); } printf("\n"); } - if (fluxData.overseaCount == 0) { + if (fluxData.overseaCount == 0) + { printf("Oversea: no flux data\n"); - } else { + } + else + { printf("Oversea: flux data\n"); - for (j = 0; j < fluxData.overseaCount; j++) { + for (j = 0; j < fluxData.overseaCount; j++) + { printf("%lld\t", fluxData.oversea[j]); } printf("\n"); @@ -71,8 +83,7 @@ int main(int argc, char **argv) { printf("-----------\n"); } - Qiniu_Free_CDNFluxRet(&ret); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/cdn_get_log_list.c b/examples/cdn_get_log_list.c index bfe42dc5..2919047f 100644 --- a/examples/cdn_get_log_list.c +++ b/examples/cdn_get_log_list.c @@ -1,7 +1,8 @@ #include "../qiniu/cdn.h" #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Client client; Qiniu_CDN_LogListRet ret; Qiniu_Error error; @@ -14,23 +15,24 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - - //urls to refresh + // urls to refresh char *domains[] = { - "csdk.qiniudn.com", - "javasdk.qiniudn.com" - }; + "csdk.qiniudn.com", + "javasdk.qiniudn.com"}; int domainsCount = 2; char *day = "2017-08-07"; - //init + // init Qiniu_Zero(ret); Qiniu_Client_InitMacAuth(&client, 1024, &mac); - error = Qiniu_CDN_GetLogList(&client, &ret, domains, domainsCount,day); - if (error.code != 200) { + error = Qiniu_CDN_GetLogList(&client, &ret, domains, domainsCount, day); + if (error.code != Qiniu_OK.code) + { printf("get domain logs error.\n"); debug_log(&client, error); - } else { + } + else + { printf("get domain logs success.\n\n"); printf("Code: %d\n", ret.code); printf("Error: %s\n", ret.error); @@ -38,15 +40,20 @@ int main(int argc, char **argv) { printf("-----------\n"); - //data - for (i = 0; i < ret.domainsCount; i++) { + // data + for (i = 0; i < ret.domainsCount; i++) + { Qiniu_CDN_LogListData logData = ret.data[i]; - if (logData.itemsCount == 0) { + if (logData.itemsCount == 0) + { printf("domain: %s, no log data\n", logData.domain); printf("-----------\n"); - } else { + } + else + { printf("domain: %s have %d log files\n", logData.domain, logData.itemsCount); - for (j = 0; j < logData.itemsCount; j++) { + for (j = 0; j < logData.itemsCount; j++) + { printf("name: %s\n", logData.items[j].name); } printf("-----------\n"); @@ -54,7 +61,6 @@ int main(int argc, char **argv) { } Qiniu_Free_CDNLogListRet(&ret); - } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/cdn_prefetch_urls.c b/examples/cdn_prefetch_urls.c index 084dad21..d8c13d12 100644 --- a/examples/cdn_prefetch_urls.c +++ b/examples/cdn_prefetch_urls.c @@ -1,7 +1,8 @@ #include "../qiniu/cdn.h" #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Client client; Qiniu_CDN_PrefetchRet ret; Qiniu_Error error; @@ -15,29 +16,31 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - - //urls to refresh + // urls to refresh char *urls[] = { - "http://csdk.qiniudn.com/qiniu1.png", - "http://csdk.qiniudn.com/qiniu2.png", - "http://csdk.qiniudn.com/qiniu3.png" - }; + "http://csdk.qiniudn.com/qiniu1.png", + "http://csdk.qiniudn.com/qiniu2.png", + "http://csdk.qiniudn.com/qiniu3.png"}; - //init + // init Qiniu_Zero(ret); Qiniu_Client_InitMacAuth(&client, 1024, &mac); error = Qiniu_CDN_PrefetchUrls(&client, &ret, urls, 3); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("prefetch urls error.\n"); debug_log(&client, error); - } else { + } + else + { printf("prefetch urls success.\n"); printf("Code: %d\n", ret.code); printf("Error: %s\n", ret.error); printf("RequestId: %s\n", ret.requestId); p = ret.invalidUrls; - for (i = 0; i < ret.invalidUrlsCount; i++) { + for (i = 0; i < ret.invalidUrlsCount; i++) + { printf("InvalidUrl: %s\n", *p); ++p; } @@ -48,4 +51,4 @@ int main(int argc, char **argv) { Qiniu_Free_CDNPrefetchRet(&ret); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/cdn_refresh_dirs.c b/examples/cdn_refresh_dirs.c index 7bdf6f2a..1cc6b64a 100644 --- a/examples/cdn_refresh_dirs.c +++ b/examples/cdn_refresh_dirs.c @@ -1,7 +1,8 @@ #include "../qiniu/cdn.h" #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Client client; Qiniu_CDN_RefreshRet ret; Qiniu_Error error; @@ -15,34 +16,37 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - - //urls to refresh + // urls to refresh char *dirs[] = { - "http://csdk.qiniudn.com/image1/", - "http://csdk.qiniudn.com/image2/", - "http://csdk.qiniudn.com/image3/" - }; + "http://csdk.qiniudn.com/image1/", + "http://csdk.qiniudn.com/image2/", + "http://csdk.qiniudn.com/image3/"}; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); error = Qiniu_CDN_RefreshDirs(&client, &ret, dirs, 3); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("refresh dirs error.\n"); debug_log(&client, error); - } else { + } + else + { printf("refresh dirs success.\n"); printf("Code: %d\n", ret.code); printf("Error: %s\n", ret.error); printf("RequestId: %s\n", ret.requestId); p = ret.invalidUrls; - for (i = 0; i < ret.invalidUrlsCount; i++) { + for (i = 0; i < ret.invalidUrlsCount; i++) + { printf("InvalidUrl: %s\n", *p); ++p; } p = ret.invalidDirs; - for (i = 0; i < ret.invalidDirsCount; i++) { + for (i = 0; i < ret.invalidDirsCount; i++) + { printf("InvalidDir: %s\n", *p); ++p; } diff --git a/examples/cdn_refresh_urls.c b/examples/cdn_refresh_urls.c index 3f09a5d4..e0f6f65b 100644 --- a/examples/cdn_refresh_urls.c +++ b/examples/cdn_refresh_urls.c @@ -1,7 +1,8 @@ #include "../qiniu/cdn.h" #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Client client; Qiniu_CDN_RefreshRet ret; Qiniu_Error error; @@ -15,35 +16,38 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - - //urls to refresh + // urls to refresh char *urls[] = { - "http://csdk.qiniudn.com/qiniu1.png", - "http://csdk.qiniudn.com/qiniu2.png", - "http://csdk.qiniudn.com/qiniu3.png" - }; + "http://csdk.qiniudn.com/qiniu1.png", + "http://csdk.qiniudn.com/qiniu2.png", + "http://csdk.qiniudn.com/qiniu3.png"}; - //init + // init Qiniu_Zero(ret); Qiniu_Client_InitMacAuth(&client, 1024, &mac); error = Qiniu_CDN_RefreshUrls(&client, &ret, urls, 3); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("refresh urls error.\n"); debug_log(&client, error); - } else { + } + else + { printf("refresh urls success.\n"); printf("Code: %d\n", ret.code); printf("Error: %s\n", ret.error); printf("RequestId: %s\n", ret.requestId); p = ret.invalidUrls; - for (i = 0; i < ret.invalidUrlsCount; i++) { + for (i = 0; i < ret.invalidUrlsCount; i++) + { printf("InvalidUrl: %s\n", *p); ++p; } p = ret.invalidDirs; - for (i = 0; i < ret.invalidDirsCount; i++) { + for (i = 0; i < ret.invalidDirsCount; i++) + { printf("InvalidDir: %s\n", *p); ++p; } @@ -56,4 +60,4 @@ int main(int argc, char **argv) { Qiniu_Free_CDNRefreshRet(&ret); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/fop_video_avthumb.c b/examples/fop_video_avthumb.c index 36857c14..2da568c4 100644 --- a/examples/fop_video_avthumb.c +++ b/examples/fop_video_avthumb.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_FOP_PfopRet pfopRet; @@ -24,7 +25,7 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); char *saveMp4Entry = Qiniu_String_Concat3(bucket, ":", "avthumb_test_target.mp4"); @@ -44,15 +45,18 @@ int main(int argc, char **argv) { char *fops[] = {avthumbMp4Fop, vframeJpgFop}; Qiniu_Error error = Qiniu_FOP_Pfop(&client, &pfopRet, bucket, key, fops, 2, pipeline, notifyURL, force); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("video file pfop %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过pfopRet变量查询任务ID*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过pfopRet变量查询任务ID*/ printf("video file pfop %s:%s success, persistentId: %s .\n\n", bucket, key, pfopRet.persistentId); } Qiniu_Free(avthumbMp4Fop); Qiniu_Free(vframeJpgFop); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_change_mime.c b/examples/rs_batch_change_mime.c index 408d6fdf..0980e134 100644 --- a/examples/rs_batch_change_mime.c +++ b/examples/rs_batch_change_mime.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchItemRet *itemRets; @@ -23,14 +24,15 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryChangeMime *entries = (Qiniu_RS_EntryChangeMime *) malloc( - sizeof(Qiniu_RS_EntryChangeMime) * entryCount); - for (i = 0; i < entryCount; i++) { + Qiniu_RS_EntryChangeMime *entries = (Qiniu_RS_EntryChangeMime *)malloc( + sizeof(Qiniu_RS_EntryChangeMime) * entryCount); + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryChangeMime entry; entry.bucket = bucket; size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); entry.key = Qiniu_String_Concat2(key, indexStr); Qiniu_Free(indexStr); @@ -38,34 +40,42 @@ int main(int argc, char **argv) { entries[i] = entry; } - itemRets = (Qiniu_RS_BatchItemRet *) malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); + itemRets = (Qiniu_RS_BatchItemRet *)malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchChangeMime(&client, itemRets, entries, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch change file mime error.\n"); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过itemRets变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过itemRets变量查询一些关于这个文件的信息*/ printf("batch change file mime success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { int code = itemRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("success: %d\n", code); - } else { + } + else + { printf("code: %d, error: %s\n", code, itemRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryChangeMime entry = entries[i]; - Qiniu_Free((void *) entry.key); + Qiniu_Free((void *)entry.key); } Qiniu_Free(entries); Qiniu_Free(itemRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_change_type.c b/examples/rs_batch_change_type.c index 80231ed7..8be8a499 100644 --- a/examples/rs_batch_change_type.c +++ b/examples/rs_batch_change_type.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchItemRet *itemRets; @@ -23,49 +24,58 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryChangeType *entries = (Qiniu_RS_EntryChangeType *) malloc( - sizeof(Qiniu_RS_EntryChangeType) * entryCount); - for (i = 0; i < entryCount; i++) { + Qiniu_RS_EntryChangeType *entries = (Qiniu_RS_EntryChangeType *)malloc( + sizeof(Qiniu_RS_EntryChangeType) * entryCount); + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryChangeType entry; entry.bucket = bucket; size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); entry.key = Qiniu_String_Concat2(key, indexStr); Qiniu_Free(indexStr); - entry.fileType = 1;//改为低频存储 + entry.fileType = 1; // 改为低频存储 entries[i] = entry; } - itemRets = (Qiniu_RS_BatchItemRet *) malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); + itemRets = (Qiniu_RS_BatchItemRet *)malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchChangeType(&client, itemRets, entries, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch change file type error.\n"); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过itemRets变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过itemRets变量查询一些关于这个文件的信息*/ printf("batch change file type success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { int code = itemRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("success: %d\n", code); - } else { + } + else + { printf("code: %d, error: %s\n", code, itemRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryChangeType entry = entries[i]; - Qiniu_Free((void *) entry.key); + Qiniu_Free((void *)entry.key); } Qiniu_Free(entries); Qiniu_Free(itemRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_copy.c b/examples/rs_batch_copy.c index c893be6c..edfc46a5 100644 --- a/examples/rs_batch_copy.c +++ b/examples/rs_batch_copy.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchItemRet *itemRets; @@ -23,23 +24,24 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryPathPair *entryPairs = (Qiniu_RS_EntryPathPair *) malloc(sizeof(Qiniu_RS_EntryPathPair) * entryCount); - for (i = 0; i < entryCount; i++) { - //src + Qiniu_RS_EntryPathPair *entryPairs = (Qiniu_RS_EntryPathPair *)malloc(sizeof(Qiniu_RS_EntryPathPair) * entryCount); + for (i = 0; i < entryCount; i++) + { + // src Qiniu_RS_EntryPath srcEntry; srcEntry.bucket = bucket; srcEntry.key = key; - //dest + // dest Qiniu_RS_EntryPath destEntry; destEntry.bucket = bucket; size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); destEntry.key = Qiniu_String_Concat2(key, indexStr); Qiniu_Free(indexStr); - //pack + // pack Qiniu_RS_EntryPathPair entryPair; entryPair.src = srcEntry; entryPair.dest = destEntry; @@ -47,33 +49,41 @@ int main(int argc, char **argv) { entryPairs[i] = entryPair; } - itemRets = (Qiniu_RS_BatchItemRet *) malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); + itemRets = (Qiniu_RS_BatchItemRet *)malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchCopy(&client, itemRets, entryPairs, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch copy file error.\n"); debug_log(&client, error); - } else { + } + else + { printf("batch copy file success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { int code = itemRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("success: %d\n", code); - } else { + } + else + { printf("code: %d, error: %s\n", code, itemRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryPathPair entryPair = entryPairs[i]; - Qiniu_Free((void *) entryPair.dest.key); + Qiniu_Free((void *)entryPair.dest.key); } Qiniu_Free(entryPairs); Qiniu_Free(itemRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_delete.c b/examples/rs_batch_delete.c index d117e2d7..2099282e 100644 --- a/examples/rs_batch_delete.c +++ b/examples/rs_batch_delete.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchItemRet *itemRets; @@ -23,45 +24,54 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryPath *entries = (Qiniu_RS_EntryPath *) malloc(sizeof(Qiniu_RS_EntryPath) * entryCount); - for (i = 0; i < entryCount; i++) { + Qiniu_RS_EntryPath *entries = (Qiniu_RS_EntryPath *)malloc(sizeof(Qiniu_RS_EntryPath) * entryCount); + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryPath entry; entry.bucket = bucket; size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); entry.key = Qiniu_String_Concat2(key, indexStr); entries[i] = entry; } - itemRets = (Qiniu_RS_BatchItemRet *) malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); + itemRets = (Qiniu_RS_BatchItemRet *)malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchDelete(&client, itemRets, entries, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch delete file error.\n"); debug_log(&client, error); - } else { + } + else + { printf("batch delete file success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { int code = itemRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("success: %d\n", code); - } else { + } + else + { printf("code: %d, error: %s\n", code, itemRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryPath entry = entries[i]; - Qiniu_Free((void *) entry.key); + Qiniu_Free((void *)entry.key); } Qiniu_Free(entries); Qiniu_Free(itemRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_delete_after_days.c b/examples/rs_batch_delete_after_days.c index 78eb5b76..201d3af6 100644 --- a/examples/rs_batch_delete_after_days.c +++ b/examples/rs_batch_delete_after_days.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchItemRet *itemRets; @@ -23,49 +24,58 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryDeleteAfterDays *entries = (Qiniu_RS_EntryDeleteAfterDays *) malloc( - sizeof(Qiniu_RS_EntryDeleteAfterDays) * entryCount); - for (i = 0; i < entryCount; i++) { + Qiniu_RS_EntryDeleteAfterDays *entries = (Qiniu_RS_EntryDeleteAfterDays *)malloc( + sizeof(Qiniu_RS_EntryDeleteAfterDays) * entryCount); + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryDeleteAfterDays entry; entry.bucket = bucket; size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); entry.key = Qiniu_String_Concat2(key, indexStr); Qiniu_Free(indexStr); - entry.days = 7;//设置7天有效期 + entry.days = 7; // 设置7天有效期 entries[i] = entry; } - itemRets = (Qiniu_RS_BatchItemRet *) malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); + itemRets = (Qiniu_RS_BatchItemRet *)malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchDeleteAfterDays(&client, itemRets, entries, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch deleteAfterDays file error.\n"); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过itemRets变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过itemRets变量查询一些关于这个文件的信息*/ printf("batch deleteAfterDays file success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { int code = itemRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("success: %d\n", code); - } else { + } + else + { printf("code: %d, error: %s\n", code, itemRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryDeleteAfterDays entry = entries[i]; - Qiniu_Free((void *) entry.key); + Qiniu_Free((void *)entry.key); } Qiniu_Free(entries); Qiniu_Free(itemRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_move.c b/examples/rs_batch_move.c index b9f9c16d..dfaa9710 100644 --- a/examples/rs_batch_move.c +++ b/examples/rs_batch_move.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchItemRet *itemRets; @@ -23,26 +24,27 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryPathPair *entryPairs = (Qiniu_RS_EntryPathPair *) malloc(sizeof(Qiniu_RS_EntryPathPair) * entryCount); - for (i = 0; i < entryCount; i++) { + Qiniu_RS_EntryPathPair *entryPairs = (Qiniu_RS_EntryPathPair *)malloc(sizeof(Qiniu_RS_EntryPathPair) * entryCount); + for (i = 0; i < entryCount; i++) + { size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); - //src + // src Qiniu_RS_EntryPath srcEntry; srcEntry.bucket = bucket; srcEntry.key = Qiniu_String_Concat2(key, indexStr); - //dest + // dest Qiniu_RS_EntryPath destEntry; destEntry.bucket = bucket; destEntry.key = Qiniu_String_Concat3(key, indexStr, "-move"); Qiniu_Free(indexStr); - //pack + // pack Qiniu_RS_EntryPathPair entryPair; entryPair.src = srcEntry; entryPair.dest = destEntry; @@ -50,34 +52,42 @@ int main(int argc, char **argv) { entryPairs[i] = entryPair; } - itemRets = (Qiniu_RS_BatchItemRet *) malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); + itemRets = (Qiniu_RS_BatchItemRet *)malloc(sizeof(Qiniu_RS_BatchItemRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchMove(&client, itemRets, entryPairs, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch move file error.\n"); debug_log(&client, error); - } else { + } + else + { printf("batch move file success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { int code = itemRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("success: %d\n", code); - } else { + } + else + { printf("code: %d, error: %s\n", code, itemRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryPathPair entryPair = entryPairs[i]; - Qiniu_Free((void *) entryPair.src.key); - Qiniu_Free((void *) entryPair.dest.key); + Qiniu_Free((void *)entryPair.src.key); + Qiniu_Free((void *)entryPair.dest.key); } Qiniu_Free(entryPairs); Qiniu_Free(itemRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_batch_stat.c b/examples/rs_batch_stat.c index 1bec6fba..4bd15a00 100644 --- a/examples/rs_batch_stat.c +++ b/examples/rs_batch_stat.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_BatchStatRet *statRets; @@ -23,52 +24,61 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_ItemCount entryCount = 10; - Qiniu_RS_EntryPath *entries = (Qiniu_RS_EntryPath *) malloc(sizeof(Qiniu_RS_EntryPath) * entryCount); - for (i = 0; i < entryCount; i++) { + Qiniu_RS_EntryPath *entries = (Qiniu_RS_EntryPath *)malloc(sizeof(Qiniu_RS_EntryPath) * entryCount); + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryPath entry; entry.bucket = bucket; size_t indexLen = snprintf(NULL, 0, "%d", i) + 1; - char *indexStr = (char *) calloc(sizeof(char), indexLen); + char *indexStr = (char *)calloc(sizeof(char), indexLen); snprintf(indexStr, indexLen, "%d", i); entry.key = Qiniu_String_Concat2(key, indexStr); Qiniu_Free(indexStr); entries[i] = entry; } - statRets = (Qiniu_RS_BatchStatRet *) malloc(sizeof(Qiniu_RS_BatchStatRet) * entryCount); + statRets = (Qiniu_RS_BatchStatRet *)malloc(sizeof(Qiniu_RS_BatchStatRet) * entryCount); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_BatchStat(&client, statRets, entries, entryCount); - if (error.code / 100 != 2) { + if (error.code / 100 != 2) + { printf("batch stat file error.\n"); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过statRets变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过statRets变量查询一些关于这个文件的信息*/ printf("batch stat file success.\n\n"); - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_StatRet data = statRets[i].data; int code = statRets[i].code; - if (code == 200) { + if (code == Qiniu_OK.code) + { printf("file hash: \t%s\n", data.hash); printf("file size: \t%lld\n", data.fsize); printf("file put time: \t%lld\n", data.putTime); printf("file mime type: \t%s\n", data.mimeType); printf("file type: \t%lld\n", data.type); - } else { + } + else + { printf("error: %s\n", statRets[i].error); } } } - for (i = 0; i < entryCount; i++) { + for (i = 0; i < entryCount; i++) + { Qiniu_RS_EntryPath entry = entries[i]; - Qiniu_Free((void *) entry.key); + Qiniu_Free((void *)entry.key); } Qiniu_Free(entries); Qiniu_Free(statRets); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_change_mime.c b/examples/rs_change_mime.c index 1506c94d..48944c2d 100644 --- a/examples/rs_change_mime.c +++ b/examples/rs_change_mime.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; @@ -21,15 +22,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_ChangeMime(&client, bucket, key, newMime); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("change file mime %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { + } + else + { printf("change file mime %s:%s success.\n\n", bucket, key); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_change_type.c b/examples/rs_change_type.c index c037e518..ef4b5e20 100644 --- a/examples/rs_change_type.c +++ b/examples/rs_change_type.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; @@ -21,15 +22,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_ChangeType(&client, bucket, key, fileType); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("change file type %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { + } + else + { printf("change file type %s:%s success.\n\n", bucket, key); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_copy.c b/examples/rs_copy.c index 719c63a3..acd45f79 100644 --- a/examples/rs_copy.c +++ b/examples/rs_copy.c @@ -6,14 +6,16 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; char *accessKey = getenv("QINIU_ACCESS_KEY"); char *secretKey = getenv("QINIU_SECRET_KEY"); - char *srcBucket = getenv("QINIU_TEST_BUCKET");; + char *srcBucket = getenv("QINIU_TEST_BUCKET"); + char *srcKey = "qiniu.png"; char *destBucket = srcBucket; char *destKey = "qiniu_copy.png"; @@ -23,15 +25,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_Copy(&client, srcBucket, srcKey, destBucket, destKey, force); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("copy file %s:%s -> %s:%s error.\n", srcBucket, srcKey, destBucket, destKey); debug_log(&client, error); - } else { + } + else + { printf("copy file %s:%s -> %s:%s success.\n", srcBucket, srcKey, destBucket, destKey); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_delete.c b/examples/rs_delete.c index 93b16346..578d1b4f 100644 --- a/examples/rs_delete.c +++ b/examples/rs_delete.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; @@ -20,15 +21,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_Delete(&client, bucket, key); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("delete file %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { + } + else + { printf("delete file %s:%s file success.\n", bucket, key); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_delete_after_days.c b/examples/rs_delete_after_days.c index e9c5b9d6..04edae68 100644 --- a/examples/rs_delete_after_days.c +++ b/examples/rs_delete_after_days.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; @@ -21,15 +22,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_DeleteAfterDays(&client, bucket, key, days); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("deleteAfterDays file %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { + } + else + { printf("deleteAfterDays file %s:%s file success.\n", bucket, key); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_fetch.c b/examples/rs_fetch.c index 7e3c15f4..19a7266b 100644 --- a/examples/rs_fetch.c +++ b/examples/rs_fetch.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RS_FetchRet fetchRet; @@ -25,13 +26,16 @@ int main(int argc, char **argv) { Qiniu_Client_InitMacAuth(&client, 1024, &mac); - //fetch with key + // fetch with key Qiniu_Error error1 = Qiniu_RS_Fetch(&client, &fetchRet, resURL, bucket, key); - if (error1.code != 200) { + if (error1.code != Qiniu_OK.code) + { printf("fetch file %s -> %s:%s error.\n", resURL, bucket, key); debug_log(&client, error1); - } else { - /*200, 正确返回了, 你可以通过fetchRet变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过fetchRet变量查询一些关于这个文件的信息*/ printf("fetch file %s -> %s:%s success.\n", resURL, bucket, key); printf("file key: \t%s\n", fetchRet.key); printf("file hash: \t%s\n", fetchRet.hash); @@ -39,13 +43,16 @@ int main(int argc, char **argv) { printf("file mime type: \t%s\n", fetchRet.mimeType); } - //fetch without key + // fetch without key Qiniu_Error error2 = Qiniu_RS_Fetch(&client, &fetchRet, resURL, bucket, 0); - if (error2.code != 200) { + if (error2.code != Qiniu_OK.code) + { printf("fetch file %s -> %s error.\n", resURL, bucket); debug_log(&client, error2); - } else { - /*200, 正确返回了, 你可以通过fetchRet变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过fetchRet变量查询一些关于这个文件的信息*/ printf("fetch file %s -> %s success.\n", resURL, bucket); printf("file key: \t%s\n", fetchRet.key); printf("file hash: \t%s\n", fetchRet.hash); @@ -53,6 +60,5 @@ int main(int argc, char **argv) { printf("file mime type: \t%s\n", fetchRet.mimeType); } - Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_move.c b/examples/rs_move.c index d00717c1..3690a5ec 100644 --- a/examples/rs_move.c +++ b/examples/rs_move.c @@ -6,14 +6,16 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; char *accessKey = getenv("QINIU_ACCESS_KEY"); char *secretKey = getenv("QINIU_SECRET_KEY"); - char *srcBucket = getenv("QINIU_TEST_BUCKET");; + char *srcBucket = getenv("QINIU_TEST_BUCKET"); + char *srcKey = "qiniu.png"; char *destBucket = srcBucket; char *destKey = "qiniu_move.png"; @@ -23,15 +25,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_Move(&client, srcBucket, srcKey, destBucket, destKey, force); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("move file %s:%s -> %s:%s error.\n", srcBucket, srcKey, destBucket, destKey); debug_log(&client, error); - } else { + } + else + { printf("move file %s:%s -> %s:%s success.\n", srcBucket, srcKey, destBucket, destKey); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_prefetch.c b/examples/rs_prefetch.c index 0764524b..97cdea6c 100644 --- a/examples/rs_prefetch.c +++ b/examples/rs_prefetch.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_Client client; @@ -19,15 +20,18 @@ int main(int argc, char **argv) { mac.accessKey = accessKey; mac.secretKey = secretKey; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_Prefetch(&client, bucket, key); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("prefetch file %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { + } + else + { printf("prefetch file \t%s:%s success.\n\n", bucket, key); } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rs_stat.c b/examples/rs_stat.c index ff9dc906..ed30b829 100644 --- a/examples/rs_stat.c +++ b/examples/rs_stat.c @@ -7,7 +7,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_RS_StatRet statRet; Qiniu_Client client; @@ -15,7 +16,8 @@ int main(int argc, char **argv) { char *secretKey = getenv("QINIU_SECRET_KEY"); char *bucket = getenv("QINIU_TEST_BUCKET"); - if (str_empty(accessKey) || str_empty(accessKey) || str_empty(bucket)) { + if (str_empty(accessKey) || str_empty(accessKey) || str_empty(bucket)) + { printf("please fill `test-env.sh` and then run `source test-env.sh` first\n"); return -1; } @@ -27,14 +29,17 @@ int main(int argc, char **argv) { mac.secretKey = secretKey; Qiniu_Use_Zone_Huadong(Qiniu_True); - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RS_Stat(&client, &statRet, bucket, key); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("stat file %s:%s error.\n", bucket, key); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过statRet变量查询一些关于这个文件的信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过statRet变量查询一些关于这个文件的信息*/ printf("stat file \t%s:%s success.\n\n", bucket, key); printf("file hash: \t%s\n", statRet.hash); printf("file size: \t%lld\n", statRet.fsize); @@ -44,4 +49,4 @@ int main(int argc, char **argv) { } Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/rsf_list_bucket.c b/examples/rsf_list_bucket.c index cec87051..aa31ad41 100644 --- a/examples/rsf_list_bucket.c +++ b/examples/rsf_list_bucket.c @@ -7,7 +7,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RSF_ListRet listRet; @@ -33,48 +34,56 @@ int main(int argc, char **argv) { Qiniu_Client_InitMacAuth(&client, 1024, &mac); - do { + do + { Qiniu_Error error = Qiniu_RSF_ListFiles(&client, &listRet, bucket, prefix, delimiter, nextMarker, limit); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("list files of bucket %s error.\n", bucket); debug_log(&client, error); nextMarker = ""; } - else { - /*200, 正确返回了, 你可以通过listRet变量查询文件列表信息*/ + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过listRet变量查询文件列表信息*/ printf("list files of bucket %s success.\n\n", bucket); - //check next marker - if (isNewMarker == Qiniu_True) { + // check next marker + if (isNewMarker == Qiniu_True) + { free(nextMarker); } - if (!str_empty(listRet.marker)) { + if (!str_empty(listRet.marker)) + { nextMarker = strdup(listRet.marker); isNewMarker = Qiniu_True; } - else { + else + { nextMarker = NULL; } printf("next marker: %s\n", nextMarker); - //common prefixes + // common prefixes commonPrefixes = listRet.commonPrefixes; - for (i = 0; i < listRet.commonPrefixesCount; i++) { + for (i = 0; i < listRet.commonPrefixesCount; i++) + { printf("commonPrefix: %s\n", *commonPrefixes); ++commonPrefixes; } - //items + // items items = listRet.items; - for (i = 0; i < listRet.itemsCount; i++) { + for (i = 0; i < listRet.itemsCount; i++) + { Qiniu_RSF_ListItem item = listRet.items[i]; printf("key: %s, hash: %s, fsize: %lld, mime: %s, putTime: %lld, endUser: %s, type: %lld\n", item.key, item.hash, item.fsize, item.mimeType, item.putTime, item.endUser, item.type); } - //free + // free Qiniu_RSF_ListRet_Cleanup(&listRet); } diff --git a/examples/rsf_list_files.c b/examples/rsf_list_files.c index e615c461..4389c7d2 100644 --- a/examples/rsf_list_files.c +++ b/examples/rsf_list_files.c @@ -6,7 +6,8 @@ #include #include "debug.h" -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ Qiniu_Global_Init(-1); Qiniu_RSF_ListRet listRet; @@ -28,34 +29,39 @@ int main(int argc, char **argv) { char **commonPrefixes = NULL; Qiniu_RSF_ListItem *items = NULL; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_RSF_ListFiles(&client, &listRet, bucket, prefix, delimiter, marker, limit); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) + { printf("list files of bucket %s error.\n", bucket); debug_log(&client, error); - } else { - /*200, 正确返回了, 你可以通过listRet变量查询文件列表信息*/ + } + else + { + /*Qiniu_OK.code, 正确返回了, 你可以通过listRet变量查询文件列表信息*/ printf("list files of bucket %s success.\n\n", bucket); - //marker + // marker printf("next marker: %s\n", listRet.marker); - //common prefixes + // common prefixes commonPrefixes = listRet.commonPrefixes; - for (i = 0; i < listRet.commonPrefixesCount; i++) { + for (i = 0; i < listRet.commonPrefixesCount; i++) + { printf("commonPrefix: %s\n", *commonPrefixes); ++commonPrefixes; } - //items + // items items = listRet.items; - for (i = 0; i < listRet.itemsCount; i++) { + for (i = 0; i < listRet.itemsCount; i++) + { Qiniu_RSF_ListItem item = listRet.items[i]; printf("key: %s, hash: %s, fsize: %lld, mime: %s, putTime: %lld, endUser: %s, type: %lld\n", item.key, item.hash, item.fsize, item.mimeType, item.putTime, item.endUser, item.type); } - //free + // free Qiniu_RSF_ListRet_Cleanup(&listRet); } diff --git a/examples/up_chunk_upload.c b/examples/up_chunk_upload.c index cacb5cd5..aa2edb57 100644 --- a/examples/up_chunk_upload.c +++ b/examples/up_chunk_upload.c @@ -31,27 +31,27 @@ int main(int argc, char **argv) putPolicy.scope = bucket; char *uptoken = Qiniu_RS_PutPolicy_Token(&putPolicy, &mac); - //设置机房域名 - //Qiniu_Use_Zone_Beimei(Qiniu_False); - //Qiniu_Use_Zone_Huabei(Qiniu_True); + // 设置机房域名 + // Qiniu_Use_Zone_Beimei(Qiniu_False); + // Qiniu_Use_Zone_Huabei(Qiniu_True); Qiniu_Use_Zone_Huadong(Qiniu_False); - //Qiniu_Use_Zone_Huanan(Qiniu_True); + // Qiniu_Use_Zone_Huanan(Qiniu_True); - //put extra - //putExtra.upHost="http://nbxs-gate-up.qiniu.com"; + // put extra + // putExtra.upHost="http://nbxs-gate-up.qiniu.com"; putExtra.mimeType = "video/x-mp4"; - //init + // init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_Rio_PutFile(&client, &putRet, uptoken, key, localFile, &putExtra); - if (error.code != 200) + if (error.code != Qiniu_OK.code) { printf("upload file %s:%s error.\n", bucket, key); debug_log(&client, error); } else { - /*200, 正确返回了, 你可以通过statRet变量查询一些关于这个文件的信息*/ + /*Qiniu_OK.code, 正确返回了, 你可以通过statRet变量查询一些关于这个文件的信息*/ printf("upload file %s:%s success.\n\n", bucket, key); printf("key:\t%s\n", putRet.key); printf("hash:\t%s\n", putRet.hash); @@ -59,4 +59,4 @@ int main(int argc, char **argv) Qiniu_Free(uptoken); Qiniu_Client_Cleanup(&client); -} \ No newline at end of file +} diff --git a/examples/up_form_upload.c b/examples/up_form_upload.c index a593b308..2756300c 100644 --- a/examples/up_form_upload.c +++ b/examples/up_form_upload.c @@ -61,11 +61,11 @@ int main(int argc, char **argv) { //init Qiniu_Client_InitMacAuth(&client, 1024, &mac); Qiniu_Error error = Qiniu_Io_PutFile(&client, &putRet, uptoken, key, localFile, &putExtra); - if (error.code != 200) { + if (error.code != Qiniu_OK.code) { printf("upload file %s:%s error.\n", bucket, key); debug_log(&client, error); } else { - /*200, 正确返回了, 你可以通过statRet变量查询一些关于这个文件的信息*/ + /*Qiniu_OK.code, 正确返回了, 你可以通过statRet变量查询一些关于这个文件的信息*/ printf("upload file %s:%s success.\n\n", bucket, key); printf("key:\t%s\n",putRet.key); printf("hash:\t%s\n", putRet.hash); diff --git a/examples/up_multipart_sample.c b/examples/up_multipart_sample.c index 9bd84b6a..8b9310f0 100644 --- a/examples/up_multipart_sample.c +++ b/examples/up_multipart_sample.c @@ -16,13 +16,13 @@ void testNotifyErr(int partNum, Qiniu_Error err) int main(int argc, char **argv) { - setLocalHost(); //using localhost for debug + setLocalHost(); // using localhost for debug Qiniu_Global_Init(-1); Qiniu_MultipartUpload_Result putRet; - Qiniu_Client client; //client不支持并发 + Qiniu_Client client; // client不支持并发 Qiniu_RS_PutPolicy putPolicy; Qiniu_Multipart_PutExtra putExtra; - Qiniu_Zero(client); //must initial memory,otherwise will use random ptr; + Qiniu_Zero(client); // must initial memory,otherwise will use random ptr; Qiniu_Zero(putRet); char *accessKey = getenv("QINIU_ACCESS_KEY"); @@ -38,7 +38,7 @@ int main(int argc, char **argv) Qiniu_Error error; Qiniu_Recorder recorder; error = Qiniu_FileSystem_Recorder_New("/tmp", &recorder); - if (error.code != 200) + if (error.code != Qiniu_OK.code) { printf("create filesystem recorder %s:%s error.\n", bucket, key); debug_log(&client, error); @@ -62,10 +62,10 @@ int main(int argc, char **argv) char *uptoken = Qiniu_RS_PutPolicy_Token(&putPolicy, &mac); Qiniu_Client_InitMacAuth(&client, 1024, &mac); - Qiniu_RS_Delete(&client, bucket, key); //to avoid "file exist" err + Qiniu_RS_Delete(&client, bucket, key); // to avoid "file exist" err error = Qiniu_Multipart_PutFile(&client, uptoken, key, localFile, &putExtra, &putRet); - if (error.code != 200) + if (error.code != Qiniu_OK.code) { printf("upload file %s:%s error.\n", bucket, key); debug_log(&client, error); @@ -84,7 +84,7 @@ int main(int argc, char **argv) void setLocalHost() { -#ifdef LOCAL_DEBUG_MODE //dedicated for qiniu maintainer +#ifdef LOCAL_DEBUG_MODE // dedicated for qiniu maintainer QINIU_RS_HOST = "http://127.0.0.1:9400"; QINIU_UP_HOST = "http://127.0.0.1:11200"; #else diff --git a/examples/up_resumeable_upload.c b/examples/up_resumeable_upload.c index a08a3faf..e3fd2442 100644 --- a/examples/up_resumeable_upload.c +++ b/examples/up_resumeable_upload.c @@ -32,7 +32,7 @@ static void resumableUploadWithKey(Qiniu_Mac *mac, const char *bucket, const cha Qiniu_Recorder recorder; Qiniu_Zero(recorder); error = Qiniu_FileSystem_Recorder_New("/tmp", &recorder); - if (error.code != 200) + if (error.code != Qiniu_OK.code) { fprintf(stderr, "code: %d\n", error.code); fprintf(stderr, "message: %s\n", error.message); @@ -43,7 +43,7 @@ static void resumableUploadWithKey(Qiniu_Mac *mac, const char *bucket, const cha putExtra.recorder = &recorder; error = Qiniu_Rio_PutFile(&client, &putRet, upToken, key, localFile, &putExtra); - if (error.code != 200) + if (error.code != Qiniu_OK.code) { fprintf(stderr, "code: %d\n", error.code); fprintf(stderr, "message: %s\n", error.message); diff --git a/gtests/auth_test.cc b/gtests/auth_test.cc index ee138eda..ddaee41b 100644 --- a/gtests/auth_test.cc +++ b/gtests/auth_test.cc @@ -27,7 +27,7 @@ TEST(UnitTest, TestQiniuMacAuthV2) const char *body = "{\"name\": \"test\"}"; Qiniu_MacAuth_Enable_Qiniu_Timestamp_Signature(); err = auth.itbl->AuthV2(&mac, "GET", &header, "http://upload.qiniup.com/", body, strlen(body)); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(match_header_prefix(header, "Authorization: Qiniu ak:"), Qiniu_True); EXPECT_EQ(match_header_prefix(header, "X-Qiniu-Date:"), Qiniu_True); curl_slist_free_all(header); @@ -36,13 +36,13 @@ TEST(UnitTest, TestQiniuMacAuthV2) Qiniu_MacAuth_Disable_Qiniu_Timestamp_Signature(); err = auth.itbl->AuthV2(&mac, "GET", &header, "http://upload.qiniup.com/", body, strlen(body)); Qiniu_MacAuth_Enable_Qiniu_Timestamp_Signature(); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(match_header_prefix(header, "X-Qiniu-Date:"), Qiniu_False); curl_slist_free_all(header); header = NULL; err = auth.itbl->AuthV2(&mac, "GET", &header, "http://upload.qiniup.com/", body, strlen(body)); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(match_header_prefix(header, "X-Qiniu-Date:"), Qiniu_True); curl_slist_free_all(header); @@ -55,14 +55,14 @@ TEST(UnitTest, TestQiniuMacAuthV2) Qiniu_MacAuth_Disable_Qiniu_Timestamp_Signature(); err = auth.itbl->AuthV2(&mac, "GET", &header, "http://upload.qiniup.com/mkfile/sdf.jpg", body, strlen(body)); Qiniu_MacAuth_Enable_Qiniu_Timestamp_Signature(); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); match_header_prefix(header, "Authorization: Qiniu ak:K8d62cW_hqjxQ3RElNz8g3BQHa8="); EXPECT_EQ(match_header_prefix(header, "X-Qiniu-Date:"), Qiniu_False); Qiniu_MacAuth_Disable_Qiniu_Timestamp_Signature(); err = auth.itbl->AuthV2(&mac, "GET", &header, "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df", body, strlen(body)); Qiniu_MacAuth_Enable_Qiniu_Timestamp_Signature(); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); match_header_prefix(header, "Authorization: Qiniu ak:CzOiB_NSxrvMLkhK8hhV_1vTqYk="); EXPECT_EQ(match_header_prefix(header, "X-Qiniu-Date:"), Qiniu_False); curl_slist_free_all(header); diff --git a/gtests/base_io_test.cc b/gtests/base_io_test.cc index f17b4e7b..903e3826 100644 --- a/gtests/base_io_test.cc +++ b/gtests/base_io_test.cc @@ -17,7 +17,7 @@ TEST(UnitTest, TestEqual) Qiniu_Int64 ncopy; Qiniu_Error err = Qiniu_Copy(w, r, NULL, 1024, &ncopy); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(ncopy, fsize); EXPECT_TRUE(Qiniu_Is(&eq)); } @@ -30,7 +30,7 @@ TEST(UnitTest, TestFileIO) Qiniu_File *fp; Qiniu_Error err = Qiniu_File_Open(&fp, __FILE__); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); buf[len] = '\0'; @@ -88,12 +88,12 @@ TEST(UnitTest, TestBaseIO) in = Qiniu_SectionReader(§, in3, 1, 13); crc32.val = 0; err = Qiniu_Copy(h, in, NULL, 4, NULL); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(crc32.val, 0x74e38c01); in = Qiniu_SectionReader(§, in3, 1, 13); crc32.val = 0; err = Qiniu_Copy(h, in, NULL, 16, NULL); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(crc32.val, 0x74e38c01); } diff --git a/gtests/equal.c b/gtests/equal.c index c72e027d..effec26a 100644 --- a/gtests/equal.c +++ b/gtests/equal.c @@ -104,7 +104,7 @@ int Qiniu_Is(Qiniu_Eq *self) { err = Qiniu_Copy(Qiniu_Discard, self->v, NULL, 1024, &ncopy); Qiniu_Log_Info("Qiniu_Is: copy.n = %D, copy.err = %E", ncopy, err); - return err.code == 200 && ncopy == 0; + return err.code == Qiniu_OK.code && ncopy == 0; } return 0; } diff --git a/gtests/fop_test.cc b/gtests/fop_test.cc index 2a372db8..18cc54d8 100644 --- a/gtests/fop_test.cc +++ b/gtests/fop_test.cc @@ -15,7 +15,7 @@ static void pfop(Qiniu_Client *client) const char *bucket = "csdk"; const char *key = "test_pfop.mp4"; err = Qiniu_FOP_Pfop(client, &ret, bucket, key, fop, fopCount, NULL, NULL, false); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_NE(ret.persistentId, nullptr); } diff --git a/gtests/io_put_test.cc b/gtests/io_put_test.cc index 5cc57f30..43fe5197 100644 --- a/gtests/io_put_test.cc +++ b/gtests/io_put_test.cc @@ -18,7 +18,7 @@ static void clientIoPutFile(const char *uptoken, const char *key) Qiniu_Client_EnableAutoQuery(&client, Qiniu_True); err = Qiniu_Io_PutFile(&client, &putRet, uptoken, key, __FILE__, NULL); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client.respHeader)); Qiniu_Log_Debug("hash: %s", putRet.hash ? putRet.hash : ""); @@ -43,7 +43,7 @@ static void clientIoPutBuffer(const char *uptoken, const char *key) Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client.respHeader)); Qiniu_Log_Debug("hash: %s", putRet.hash ? putRet.hash : ""); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(putRet.hash, "FpQ6cC0G80WZruH42o759ylgMdaZ"); Qiniu_Client_Cleanup(&client); @@ -75,7 +75,7 @@ static void clientIoGet(const char *url) EXPECT_EQ(code, 0); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); - EXPECT_EQ(httpCode, 200); + EXPECT_EQ(httpCode, Qiniu_OK.code); EXPECT_STREQ(Qiniu_Buffer_CStr(&resp), text); diff --git a/gtests/multipart_test.cc b/gtests/multipart_test.cc index 24334e29..1226316e 100644 --- a/gtests/multipart_test.cc +++ b/gtests/multipart_test.cc @@ -42,7 +42,7 @@ static const char *putMemoryData_multipart(const char *bucket, const char *key, Qiniu_ReaderAt reader = Qiniu_BufReaderAt(&rbuff, memData, (size_t)dataLen); err = Qiniu_Multipart_Put(&client, uptoken, key, reader, dataLen, &putExtra, &putRet); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client.respHeader)); Qiniu_Log_Debug("hash: %s , key:%s", putRet.hash, putRet.key); @@ -80,7 +80,7 @@ static const char *putFile_multipart(const char *bucket, const char *key, const #endif err = Qiniu_FileSystem_Recorder_New(tempDirPath, &recorder); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); putExtra.recorder = &recorder; putExtra.mimeType = mimeType; putExtra.enableContentMd5 = 1; @@ -94,7 +94,7 @@ static const char *putFile_multipart(const char *bucket, const char *key, const err = Qiniu_Multipart_PutFile(&client, uptoken, key, filePath, &putExtra, &putRet); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client.respHeader)); Qiniu_Log_Debug("hash: %s , key:%s", putRet.hash, putRet.key); @@ -138,12 +138,12 @@ TEST(IntegrationTest, TestMultipartUpload_smallfile) // step3: stat file Qiniu_RS_StatRet statResult; err = Qiniu_RS_Stat(&client, &statResult, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(statResult.mimeType, "txt"); // step4: delete file err = Qiniu_RS_Delete(&client, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); } Qiniu_Client_Cleanup(&client); @@ -179,13 +179,13 @@ TEST(IntegrationTest, TestMultipartUpload_largefile) // step3: stat file Qiniu_RS_StatRet statResult; err = Qiniu_RS_Stat(&client, &statResult, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(statResult.mimeType, "mp3"); EXPECT_EQ(statResult.fsize, 5097014); // step4: delete file err = Qiniu_RS_Delete(&client, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Client_Cleanup(&client); } @@ -218,13 +218,13 @@ TEST(IntegrationTest, TestMultipartUpload_emptyfile) // step3: stat file Qiniu_RS_StatRet statResult; err = Qiniu_RS_Stat(&client, &statResult, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(statResult.fsize, 0); EXPECT_STREQ(statResult.mimeType, "txt"); // step4: delete file err = Qiniu_RS_Delete(&client, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Client_Cleanup(&client); } @@ -254,12 +254,12 @@ TEST(IntegrationTest, TestMultipartUpload_inMemoryData) // step3: stat file Qiniu_RS_StatRet statResult; err = Qiniu_RS_Stat(&client, &statResult, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(statResult.fsize, sizeof(memData)); // step4: delete file err = Qiniu_RS_Delete(&client, Test_bucket, returnKey); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Client_Cleanup(&client); } diff --git a/gtests/recorder_test.cc b/gtests/recorder_test.cc index 3652ef24..432d51c1 100644 --- a/gtests/recorder_test.cc +++ b/gtests/recorder_test.cc @@ -19,50 +19,50 @@ TEST(UnitTest, TestFileSystemRecorder_readWriteEntries) Qiniu_FileSystem_Recorder_New(TEMP_DIR, &recorder); err = recorder.open(&recorder, KEY, "wb", &medium); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); const char *DATA_1 = "abcdefghijkl"; err = medium.writeEntry(&medium, DATA_1, &haveWritten); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(haveWritten, strlen(DATA_1)); const char *DATA_2 = "ABCDEFGHIJKL"; err = medium.writeEntry(&medium, DATA_2, &haveWritten); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(haveWritten, strlen(DATA_2)); err = medium.close(&medium); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); err = recorder.open(&recorder, "aaaaa", "rb", &medium); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); err = medium.hasNextEntry(&medium, &hasNext); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_TRUE(hasNext); memset(buf, 0, BUF_LEN); err = medium.readEntry(&medium, buf, BUF_LEN, &haveRead); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(haveRead, strlen(DATA_1)); EXPECT_EQ(strncmp(buf, DATA_1, BUF_LEN), 0); err = medium.hasNextEntry(&medium, &hasNext); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_TRUE(hasNext); memset(buf, 0, BUF_LEN); err = medium.readEntry(&medium, buf, BUF_LEN, &haveRead); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_EQ(haveRead, strlen(DATA_2)); EXPECT_EQ(strncmp(buf, DATA_2, BUF_LEN), 0); err = medium.hasNextEntry(&medium, &hasNext); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_FALSE(hasNext); err = recorder.remove(&recorder, KEY); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); recorder.free(&recorder); } diff --git a/gtests/region_test.cc b/gtests/region_test.cc index 599d9ac9..bb1c73ab 100644 --- a/gtests/region_test.cc +++ b/gtests/region_test.cc @@ -173,7 +173,7 @@ TEST(IntegrationTest, TestRegionQuery) Qiniu_Region *region; Qiniu_Error err = Qiniu_Region_Query(&client, ®ion, Test_bucket, Qiniu_True); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(Qiniu_Region_Get_Id(region), "z0"); @@ -245,23 +245,23 @@ TEST(IntegrationTest, TestRegionAutoQuery) Qiniu_Error err; err = _Qiniu_Region_Get_Up_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "https://upload.qiniup.com"); err = _Qiniu_Region_Get_Io_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "https://iovip.qbox.me"); err = _Qiniu_Region_Get_Rs_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "https://rs-z0.qbox.me"); err = _Qiniu_Region_Get_Rsf_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "https://rsf-z0.qbox.me"); err = _Qiniu_Region_Get_Api_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "https://api.qiniu.com"); } @@ -279,23 +279,23 @@ TEST(IntegrationTest, TestRegionSpecify) Qiniu_Error err; err = _Qiniu_Region_Get_Up_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "http://upload-z1.qiniup.com"); err = _Qiniu_Region_Get_Io_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "http://iovip-z1.qiniuio.com"); err = _Qiniu_Region_Get_Rs_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "http://rs-z1.qiniuapi.com"); err = _Qiniu_Region_Get_Rsf_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "http://rsf-z1.qiniuapi.com"); err = _Qiniu_Region_Get_Api_Host(&client, NULL, Test_bucket, &host); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(host, "http://api-z1.qiniuapi.com"); Qiniu_Region_Free(region); diff --git a/gtests/resumable_io_test.cc b/gtests/resumable_io_test.cc index 235df3cd..5dbd37e5 100644 --- a/gtests/resumable_io_test.cc +++ b/gtests/resumable_io_test.cc @@ -22,7 +22,7 @@ static void clientIoPutFile(const char *uptoken, const char *key) Qiniu_Zero(extra); err = Qiniu_Rio_PutFile(&client, &putRet, uptoken, key, __FILE__, &extra); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client.respHeader)); Qiniu_Log_Debug("hash: %s", putRet.hash); @@ -72,7 +72,7 @@ static void clientIoPutBuffer(const char *uptoken, const char *key) Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client.respHeader)); Qiniu_Log_Debug("hash: %s", putRet.hash); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); EXPECT_STREQ(putRet.hash, "FoErrxvY99fW7npWmVii0RncWKme"); Qiniu_Client_Cleanup(&client); @@ -106,7 +106,7 @@ static void clientIoGet(const char *url) EXPECT_EQ(code, 0); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); - EXPECT_EQ(httpCode, 200); + EXPECT_EQ(httpCode, Qiniu_OK.code); EXPECT_TRUE(Qiniu_Is(&eq)); diff --git a/gtests/rs_ops_test.cc b/gtests/rs_ops_test.cc index 240d8b05..d7ddf828 100644 --- a/gtests/rs_ops_test.cc +++ b/gtests/rs_ops_test.cc @@ -26,17 +26,17 @@ static void batchCopy(Qiniu_Client *client, { Qiniu_Log_Debug("code: %d", rets[curr].code); - if (rets[curr].code != 200) + if (rets[curr].code != Qiniu_OK.code) { Qiniu_Log_Error("error: %s", rets[curr].error); } Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client->respHeader)); - EXPECT_EQ(rets[curr].code, 200); + EXPECT_EQ(rets[curr].code, Qiniu_OK.code); curr++; } free(rets); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { debug(client, err); FAIL(); @@ -55,17 +55,17 @@ static void batchMove(Qiniu_Client *client, { Qiniu_Log_Debug("code: %d", rets[curr].code); - if (rets[curr].code != 200) + if (rets[curr].code != Qiniu_OK.code) { Qiniu_Log_Error("error: %s", rets[curr].error); } Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client->respHeader)); - EXPECT_EQ(rets[curr].code, 200); + EXPECT_EQ(rets[curr].code, Qiniu_OK.code); curr++; } free(rets); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { debug(client, err); FAIL(); @@ -84,7 +84,7 @@ static void batchStat(Qiniu_Client *client, { Qiniu_Log_Debug("code: %d", rets[curr].code); - if (rets[curr].code != 200) + if (rets[curr].code != Qiniu_OK.code) { Qiniu_Log_Error("error: %s", rets[curr].error); } @@ -96,13 +96,13 @@ static void batchStat(Qiniu_Client *client, Qiniu_Log_Debug("putTime: %D", rets[curr].data.putTime); } Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client->respHeader)); - EXPECT_EQ(rets[curr].code, 200); + EXPECT_EQ(rets[curr].code, Qiniu_OK.code); curr++; } free(rets); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { debug(client, err); FAIL(); @@ -121,17 +121,17 @@ static void batchChangeType(Qiniu_Client *client, { Qiniu_Log_Debug("code: %d", rets[curr].code); - if (rets[curr].code != 200) + if (rets[curr].code != Qiniu_OK.code) { Qiniu_Log_Error("error: %s", rets[curr].error); } Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client->respHeader)); - EXPECT_EQ(rets[curr].code, 200); + EXPECT_EQ(rets[curr].code, Qiniu_OK.code); curr++; } free(rets); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { debug(client, err); FAIL(); @@ -150,17 +150,17 @@ static void batchRestoreArchive(Qiniu_Client *client, { Qiniu_Log_Debug("code: %d", rets[curr].code); - if (rets[curr].code != 200) + if (rets[curr].code != Qiniu_OK.code) { Qiniu_Log_Error("error: %s", rets[curr].error); } Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client->respHeader)); - EXPECT_EQ(rets[curr].code, 200); + EXPECT_EQ(rets[curr].code, Qiniu_OK.code); curr++; } free(rets); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { debug(client, err); FAIL(); @@ -179,18 +179,18 @@ static void batchDelete(Qiniu_Client *client, { Qiniu_Log_Debug("code: %d", rets[curr].code); - if (rets[curr].code != 200) + if (rets[curr].code != Qiniu_OK.code) { Qiniu_Log_Error("error: %s", rets[curr].error); } Qiniu_Log_Debug("%s", Qiniu_Buffer_CStr(&client->respHeader)); - EXPECT_EQ(rets[curr].code, 200); + EXPECT_EQ(rets[curr].code, Qiniu_OK.code); curr++; } free(rets); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { debug(client, err); FAIL(); diff --git a/gtests/rsf_test.cc b/gtests/rsf_test.cc index 0f0bd287..92f5abdf 100644 --- a/gtests/rsf_test.cc +++ b/gtests/rsf_test.cc @@ -18,7 +18,7 @@ TEST(IntegrationTest, TestRsfListFiles) do { err = Qiniu_RSF_ListFiles(&client, &listRet, Test_bucket, "", "", listRet.marker, 1000); - EXPECT_EQ(err.code, 200); + EXPECT_EQ(err.code, Qiniu_OK.code); for (int i = 0; i < listRet.itemsCount; i++) { printf("%s\n", listRet.items[i].key); diff --git a/hashmap/hashmap.c b/hashmap/hashmap.c new file mode 100644 index 00000000..d70b1627 --- /dev/null +++ b/hashmap/hashmap.c @@ -0,0 +1,1154 @@ +// Copyright 2020 Joshua J Baker. All rights reserved. +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +#include +#include +#include +#include +#include +#include "hashmap.h" + +#define GROW_AT 0.60 /* 60% */ +#define SHRINK_AT 0.10 /* 10% */ + +#ifndef HASHMAP_LOAD_FACTOR +#define HASHMAP_LOAD_FACTOR GROW_AT +#endif + +static void *(*__malloc)(size_t) = NULL; +static void *(*__realloc)(void *, size_t) = NULL; +static void (*__free)(void *) = NULL; + +// hashmap_set_allocator allows for configuring a custom allocator for +// all hashmap library operations. This function, if needed, should be called +// only once at startup and a prior to calling hashmap_new(). +void hashmap_set_allocator(void *(*malloc)(size_t), void (*free)(void*)) { + __malloc = malloc; + __free = free; +} + +struct bucket { + uint64_t hash:48; + uint64_t dib:16; +}; + +// hashmap is an open addressed hash map using robinhood hashing. +struct hashmap { + void *(*malloc)(size_t); + void *(*realloc)(void *, size_t); + void (*free)(void *); + size_t elsize; + size_t cap; + uint64_t seed0; + uint64_t seed1; + uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1); + int (*compare)(const void *a, const void *b, void *udata); + void (*elfree)(void *item); + void *udata; + size_t bucketsz; + size_t nbuckets; + size_t count; + size_t mask; + size_t growat; + size_t shrinkat; + uint8_t loadfactor; + uint8_t growpower; + bool oom; + void *buckets; + void *spare; + void *edata; +}; + +void hashmap_set_grow_by_power(struct hashmap *map, size_t power) { + map->growpower = power < 1 ? 1 : power > 16 ? 16 : power; +} + +static double clamp_load_factor(double factor, double default_factor) { + // Check for NaN and clamp between 50% and 90% + return factor != factor ? default_factor : + factor < 0.50 ? 0.50 : + factor > 0.95 ? 0.95 : + factor; +} + +void hashmap_set_load_factor(struct hashmap *map, double factor) { + factor = clamp_load_factor(factor, map->loadfactor / 100.0); + map->loadfactor = factor * 100; + map->growat = map->nbuckets * (map->loadfactor / 100.0); +} + +static struct bucket *bucket_at0(void *buckets, size_t bucketsz, size_t i) { + return (struct bucket*)(((char*)buckets)+(bucketsz*i)); +} + +static struct bucket *bucket_at(struct hashmap *map, size_t index) { + return bucket_at0(map->buckets, map->bucketsz, index); +} + +static void *bucket_item(struct bucket *entry) { + return ((char*)entry)+sizeof(struct bucket); +} + +static uint64_t clip_hash(uint64_t hash) { + return hash & 0xFFFFFFFFFFFF; +} + +static uint64_t get_hash(struct hashmap *map, const void *key) { + return clip_hash(map->hash(key, map->seed0, map->seed1)); +} + + +// hashmap_new_with_allocator returns a new hash map using a custom allocator. +// See hashmap_new for more information information +struct hashmap *hashmap_new_with_allocator(void *(*_malloc)(size_t), + void *(*_realloc)(void*, size_t), void (*_free)(void*), + size_t elsize, size_t cap, uint64_t seed0, uint64_t seed1, + uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1), + int (*compare)(const void *a, const void *b, void *udata), + void (*elfree)(void *item), + void *udata) +{ + _malloc = _malloc ? _malloc : __malloc ? __malloc : malloc; + _realloc = _realloc ? _realloc : __realloc ? __realloc : realloc; + _free = _free ? _free : __free ? __free : free; + size_t ncap = 16; + if (cap < ncap) { + cap = ncap; + } else { + while (ncap < cap) { + ncap *= 2; + } + cap = ncap; + } + size_t bucketsz = sizeof(struct bucket) + elsize; + while (bucketsz & (sizeof(uintptr_t)-1)) { + bucketsz++; + } + // hashmap + spare + edata + size_t size = sizeof(struct hashmap)+bucketsz*2; + struct hashmap *map = _malloc(size); + if (!map) { + return NULL; + } + memset(map, 0, sizeof(struct hashmap)); + map->elsize = elsize; + map->bucketsz = bucketsz; + map->seed0 = seed0; + map->seed1 = seed1; + map->hash = hash; + map->compare = compare; + map->elfree = elfree; + map->udata = udata; + map->spare = ((char*)map)+sizeof(struct hashmap); + map->edata = (char*)map->spare+bucketsz; + map->cap = cap; + map->nbuckets = cap; + map->mask = map->nbuckets-1; + map->buckets = _malloc(map->bucketsz*map->nbuckets); + if (!map->buckets) { + _free(map); + return NULL; + } + memset(map->buckets, 0, map->bucketsz*map->nbuckets); + map->growpower = 1; + map->loadfactor = clamp_load_factor(HASHMAP_LOAD_FACTOR, GROW_AT) * 100; + map->growat = map->nbuckets * (map->loadfactor / 100.0); + map->shrinkat = map->nbuckets * SHRINK_AT; + map->malloc = _malloc; + map->realloc = _realloc; + map->free = _free; + return map; +} + +// hashmap_new returns a new hash map. +// Param `elsize` is the size of each element in the tree. Every element that +// is inserted, deleted, or retrieved will be this size. +// Param `cap` is the default lower capacity of the hashmap. Setting this to +// zero will default to 16. +// Params `seed0` and `seed1` are optional seed values that are passed to the +// following `hash` function. These can be any value you wish but it's often +// best to use randomly generated values. +// Param `hash` is a function that generates a hash value for an item. It's +// important that you provide a good hash function, otherwise it will perform +// poorly or be vulnerable to Denial-of-service attacks. This implementation +// comes with two helper functions `hashmap_sip()` and `hashmap_murmur()`. +// Param `compare` is a function that compares items in the tree. See the +// qsort stdlib function for an example of how this function works. +// The hashmap must be freed with hashmap_free(). +// Param `elfree` is a function that frees a specific item. This should be NULL +// unless you're storing some kind of reference data in the hash. +struct hashmap *hashmap_new(size_t elsize, size_t cap, uint64_t seed0, + uint64_t seed1, + uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1), + int (*compare)(const void *a, const void *b, void *udata), + void (*elfree)(void *item), + void *udata) +{ + return hashmap_new_with_allocator(NULL, NULL, NULL, elsize, cap, seed0, + seed1, hash, compare, elfree, udata); +} + +static void free_elements(struct hashmap *map) { + if (map->elfree) { + for (size_t i = 0; i < map->nbuckets; i++) { + struct bucket *bucket = bucket_at(map, i); + if (bucket->dib) map->elfree(bucket_item(bucket)); + } + } +} + +// hashmap_clear quickly clears the map. +// Every item is called with the element-freeing function given in hashmap_new, +// if present, to free any data referenced in the elements of the hashmap. +// When the update_cap is provided, the map's capacity will be updated to match +// the currently number of allocated buckets. This is an optimization to ensure +// that this operation does not perform any allocations. +void hashmap_clear(struct hashmap *map, bool update_cap) { + map->count = 0; + free_elements(map); + if (update_cap) { + map->cap = map->nbuckets; + } else if (map->nbuckets != map->cap) { + void *new_buckets = map->malloc(map->bucketsz*map->cap); + if (new_buckets) { + map->free(map->buckets); + map->buckets = new_buckets; + } + map->nbuckets = map->cap; + } + memset(map->buckets, 0, map->bucketsz*map->nbuckets); + map->mask = map->nbuckets-1; + map->growat = map->nbuckets * (map->loadfactor / 100.0) ; + map->shrinkat = map->nbuckets * SHRINK_AT; +} + +static bool resize0(struct hashmap *map, size_t new_cap) { + struct hashmap *map2 = hashmap_new_with_allocator(map->malloc, map->realloc, + map->free, map->elsize, new_cap, map->seed0, map->seed1, map->hash, + map->compare, map->elfree, map->udata); + if (!map2) return false; + for (size_t i = 0; i < map->nbuckets; i++) { + struct bucket *entry = bucket_at(map, i); + if (!entry->dib) { + continue; + } + entry->dib = 1; + size_t j = entry->hash & map2->mask; + while(1) { + struct bucket *bucket = bucket_at(map2, j); + if (bucket->dib == 0) { + memcpy(bucket, entry, map->bucketsz); + break; + } + if (bucket->dib < entry->dib) { + memcpy(map2->spare, bucket, map->bucketsz); + memcpy(bucket, entry, map->bucketsz); + memcpy(entry, map2->spare, map->bucketsz); + } + j = (j + 1) & map2->mask; + entry->dib += 1; + } + } + map->free(map->buckets); + map->buckets = map2->buckets; + map->nbuckets = map2->nbuckets; + map->mask = map2->mask; + map->growat = map2->growat; + map->shrinkat = map2->shrinkat; + map->free(map2); + return true; +} + +static bool resize(struct hashmap *map, size_t new_cap) { + return resize0(map, new_cap); +} + +// hashmap_set_with_hash works like hashmap_set but you provide your +// own hash. The 'hash' callback provided to the hashmap_new function +// will not be called +const void *hashmap_set_with_hash(struct hashmap *map, const void *item, + uint64_t hash) +{ + hash = clip_hash(hash); + map->oom = false; + if (map->count >= map->growat) { + if (!resize(map, map->nbuckets*(1<growpower))) { + map->oom = true; + return NULL; + } + } + + struct bucket *entry = map->edata; + entry->hash = hash; + entry->dib = 1; + void *eitem = bucket_item(entry); + memcpy(eitem, item, map->elsize); + + void *bitem; + size_t i = entry->hash & map->mask; + while(1) { + struct bucket *bucket = bucket_at(map, i); + if (bucket->dib == 0) { + memcpy(bucket, entry, map->bucketsz); + map->count++; + return NULL; + } + bitem = bucket_item(bucket); + if (entry->hash == bucket->hash && (!map->compare || + map->compare(eitem, bitem, map->udata) == 0)) + { + memcpy(map->spare, bitem, map->elsize); + memcpy(bitem, eitem, map->elsize); + return map->spare; + } + if (bucket->dib < entry->dib) { + memcpy(map->spare, bucket, map->bucketsz); + memcpy(bucket, entry, map->bucketsz); + memcpy(entry, map->spare, map->bucketsz); + eitem = bucket_item(entry); + } + i = (i + 1) & map->mask; + entry->dib += 1; + } +} + +// hashmap_set inserts or replaces an item in the hash map. If an item is +// replaced then it is returned otherwise NULL is returned. This operation +// may allocate memory. If the system is unable to allocate additional +// memory then NULL is returned and hashmap_oom() returns true. +const void *hashmap_set(struct hashmap *map, const void *item) { + return hashmap_set_with_hash(map, item, get_hash(map, item)); +} + +// hashmap_get_with_hash works like hashmap_get but you provide your +// own hash. The 'hash' callback provided to the hashmap_new function +// will not be called +const void *hashmap_get_with_hash(struct hashmap *map, const void *key, + uint64_t hash) +{ + hash = clip_hash(hash); + size_t i = hash & map->mask; + while(1) { + struct bucket *bucket = bucket_at(map, i); + if (!bucket->dib) return NULL; + if (bucket->hash == hash) { + void *bitem = bucket_item(bucket); + if (!map->compare || map->compare(key, bitem, map->udata) == 0) { + return bitem; + } + } + i = (i + 1) & map->mask; + } +} + +// hashmap_get returns the item based on the provided key. If the item is not +// found then NULL is returned. +const void *hashmap_get(struct hashmap *map, const void *key) { + return hashmap_get_with_hash(map, key, get_hash(map, key)); +} + +// hashmap_probe returns the item in the bucket at position or NULL if an item +// is not set for that bucket. The position is 'moduloed' by the number of +// buckets in the hashmap. +const void *hashmap_probe(struct hashmap *map, uint64_t position) { + size_t i = position & map->mask; + struct bucket *bucket = bucket_at(map, i); + if (!bucket->dib) { + return NULL; + } + return bucket_item(bucket); +} + +// hashmap_delete_with_hash works like hashmap_delete but you provide your +// own hash. The 'hash' callback provided to the hashmap_new function +// will not be called +const void *hashmap_delete_with_hash(struct hashmap *map, const void *key, + uint64_t hash) +{ + hash = clip_hash(hash); + map->oom = false; + size_t i = hash & map->mask; + while(1) { + struct bucket *bucket = bucket_at(map, i); + if (!bucket->dib) { + return NULL; + } + void *bitem = bucket_item(bucket); + if (bucket->hash == hash && (!map->compare || + map->compare(key, bitem, map->udata) == 0)) + { + memcpy(map->spare, bitem, map->elsize); + bucket->dib = 0; + while(1) { + struct bucket *prev = bucket; + i = (i + 1) & map->mask; + bucket = bucket_at(map, i); + if (bucket->dib <= 1) { + prev->dib = 0; + break; + } + memcpy(prev, bucket, map->bucketsz); + prev->dib--; + } + map->count--; + if (map->nbuckets > map->cap && map->count <= map->shrinkat) { + // Ignore the return value. It's ok for the resize operation to + // fail to allocate enough memory because a shrink operation + // does not change the integrity of the data. + resize(map, map->nbuckets/2); + } + return map->spare; + } + i = (i + 1) & map->mask; + } +} + +// hashmap_delete removes an item from the hash map and returns it. If the +// item is not found then NULL is returned. +const void *hashmap_delete(struct hashmap *map, const void *key) { + return hashmap_delete_with_hash(map, key, get_hash(map, key)); +} + +// hashmap_count returns the number of items in the hash map. +size_t hashmap_count(struct hashmap *map) { + return map->count; +} + +// hashmap_free frees the hash map +// Every item is called with the element-freeing function given in hashmap_new, +// if present, to free any data referenced in the elements of the hashmap. +void hashmap_free(struct hashmap *map) { + if (!map) return; + free_elements(map); + map->free(map->buckets); + map->free(map); +} + +// hashmap_oom returns true if the last hashmap_set() call failed due to the +// system being out of memory. +bool hashmap_oom(struct hashmap *map) { + return map->oom; +} + +// hashmap_scan iterates over all items in the hash map +// Param `iter` can return false to stop iteration early. +// Returns false if the iteration has been stopped early. +bool hashmap_scan(struct hashmap *map, + bool (*iter)(const void *item, void *udata), void *udata) +{ + for (size_t i = 0; i < map->nbuckets; i++) { + struct bucket *bucket = bucket_at(map, i); + if (bucket->dib && !iter(bucket_item(bucket), udata)) { + return false; + } + } + return true; +} + +// hashmap_iter iterates one key at a time yielding a reference to an +// entry at each iteration. Useful to write simple loops and avoid writing +// dedicated callbacks and udata structures, as in hashmap_scan. +// +// map is a hash map handle. i is a pointer to a size_t cursor that +// should be initialized to 0 at the beginning of the loop. item is a void +// pointer pointer that is populated with the retrieved item. Note that this +// is NOT a copy of the item stored in the hash map and can be directly +// modified. +// +// Note that if hashmap_delete() is called on the hashmap being iterated, +// the buckets are rearranged and the iterator must be reset to 0, otherwise +// unexpected results may be returned after deletion. +// +// This function has not been tested for thread safety. +// +// The function returns true if an item was retrieved; false if the end of the +// iteration has been reached. +bool hashmap_iter(struct hashmap *map, size_t *i, void **item) { + struct bucket *bucket; + do { + if (*i >= map->nbuckets) return false; + bucket = bucket_at(map, *i); + (*i)++; + } while (!bucket->dib); + *item = bucket_item(bucket); + return true; +} + + +//----------------------------------------------------------------------------- +// SipHash reference C implementation +// +// Copyright (c) 2012-2016 Jean-Philippe Aumasson +// +// Copyright (c) 2012-2014 Daniel J. Bernstein +// +// To the extent possible under law, the author(s) have dedicated all copyright +// and related and neighboring rights to this software to the public domain +// worldwide. This software is distributed without any warranty. +// +// You should have received a copy of the CC0 Public Domain Dedication along +// with this software. If not, see +// . +// +// default: SipHash-2-4 +//----------------------------------------------------------------------------- +static uint64_t SIP64(const uint8_t *in, const size_t inlen, uint64_t seed0, + uint64_t seed1) +{ +#define U8TO64_LE(p) \ + { (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ + ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ + ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ + ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) } +#define U64TO8_LE(p, v) \ + { U32TO8_LE((p), (uint32_t)((v))); \ + U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); } +#define U32TO8_LE(p, v) \ + { (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); } +#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) +#define SIPROUND \ + { v0 += v1; v1 = ROTL(v1, 13); \ + v1 ^= v0; v0 = ROTL(v0, 32); \ + v2 += v3; v3 = ROTL(v3, 16); \ + v3 ^= v2; \ + v0 += v3; v3 = ROTL(v3, 21); \ + v3 ^= v0; \ + v2 += v1; v1 = ROTL(v1, 17); \ + v1 ^= v2; v2 = ROTL(v2, 32); } + uint64_t k0 = U8TO64_LE((uint8_t*)&seed0); + uint64_t k1 = U8TO64_LE((uint8_t*)&seed1); + uint64_t v3 = UINT64_C(0x7465646279746573) ^ k1; + uint64_t v2 = UINT64_C(0x6c7967656e657261) ^ k0; + uint64_t v1 = UINT64_C(0x646f72616e646f6d) ^ k1; + uint64_t v0 = UINT64_C(0x736f6d6570736575) ^ k0; + const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t)); + for (; in != end; in += 8) { + uint64_t m = U8TO64_LE(in); + v3 ^= m; + SIPROUND; SIPROUND; + v0 ^= m; + } + const int left = inlen & 7; + uint64_t b = ((uint64_t)inlen) << 56; + switch (left) { + case 7: b |= ((uint64_t)in[6]) << 48; /* fall through */ + case 6: b |= ((uint64_t)in[5]) << 40; /* fall through */ + case 5: b |= ((uint64_t)in[4]) << 32; /* fall through */ + case 4: b |= ((uint64_t)in[3]) << 24; /* fall through */ + case 3: b |= ((uint64_t)in[2]) << 16; /* fall through */ + case 2: b |= ((uint64_t)in[1]) << 8; /* fall through */ + case 1: b |= ((uint64_t)in[0]); break; + case 0: break; + } + v3 ^= b; + SIPROUND; SIPROUND; + v0 ^= b; + v2 ^= 0xff; + SIPROUND; SIPROUND; SIPROUND; SIPROUND; + b = v0 ^ v1 ^ v2 ^ v3; + uint64_t out = 0; + U64TO8_LE((uint8_t*)&out, b); + return out; +} + +//----------------------------------------------------------------------------- +// MurmurHash3 was written by Austin Appleby, and is placed in the public +// domain. The author hereby disclaims copyright to this source code. +// +// Murmur3_86_128 +//----------------------------------------------------------------------------- +static uint64_t MM86128(const void *key, const int len, uint32_t seed) { +#define ROTL32(x, r) ((x << r) | (x >> (32 - r))) +#define FMIX32(h) h^=h>>16; h*=0x85ebca6b; h^=h>>13; h*=0xc2b2ae35; h^=h>>16; + const uint8_t * data = (const uint8_t*)key; + const int nblocks = len / 16; + uint32_t h1 = seed; + uint32_t h2 = seed; + uint32_t h3 = seed; + uint32_t h4 = seed; + uint32_t c1 = 0x239b961b; + uint32_t c2 = 0xab0e9789; + uint32_t c3 = 0x38b34ae5; + uint32_t c4 = 0xa1e38b93; + const uint32_t * blocks = (const uint32_t *)(data + nblocks*16); + for (int i = -nblocks; i; i++) { + uint32_t k1 = blocks[i*4+0]; + uint32_t k2 = blocks[i*4+1]; + uint32_t k3 = blocks[i*4+2]; + uint32_t k4 = blocks[i*4+3]; + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b; + k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; + h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747; + k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; + h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35; + k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; + h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17; + } + const uint8_t * tail = (const uint8_t*)(data + nblocks*16); + uint32_t k1 = 0; + uint32_t k2 = 0; + uint32_t k3 = 0; + uint32_t k4 = 0; + switch(len & 15) { + case 15: k4 ^= tail[14] << 16; /* fall through */ + case 14: k4 ^= tail[13] << 8; /* fall through */ + case 13: k4 ^= tail[12] << 0; + k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4; + /* fall through */ + case 12: k3 ^= tail[11] << 24; /* fall through */ + case 11: k3 ^= tail[10] << 16; /* fall through */ + case 10: k3 ^= tail[ 9] << 8; /* fall through */ + case 9: k3 ^= tail[ 8] << 0; + k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3; + /* fall through */ + case 8: k2 ^= tail[ 7] << 24; /* fall through */ + case 7: k2 ^= tail[ 6] << 16; /* fall through */ + case 6: k2 ^= tail[ 5] << 8; /* fall through */ + case 5: k2 ^= tail[ 4] << 0; + k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2; + /* fall through */ + case 4: k1 ^= tail[ 3] << 24; /* fall through */ + case 3: k1 ^= tail[ 2] << 16; /* fall through */ + case 2: k1 ^= tail[ 1] << 8; /* fall through */ + case 1: k1 ^= tail[ 0] << 0; + k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1; + /* fall through */ + }; + h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len; + h1 += h2; h1 += h3; h1 += h4; + h2 += h1; h3 += h1; h4 += h1; + FMIX32(h1); FMIX32(h2); FMIX32(h3); FMIX32(h4); + h1 += h2; h1 += h3; h1 += h4; + h2 += h1; h3 += h1; h4 += h1; + return (((uint64_t)h2)<<32)|h1; +} + +//----------------------------------------------------------------------------- +// xxHash Library +// Copyright (c) 2012-2021 Yann Collet +// All rights reserved. +// +// BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php) +// +// xxHash3 +//----------------------------------------------------------------------------- +#define XXH_PRIME_1 11400714785074694791ULL +#define XXH_PRIME_2 14029467366897019727ULL +#define XXH_PRIME_3 1609587929392839161ULL +#define XXH_PRIME_4 9650029242287828579ULL +#define XXH_PRIME_5 2870177450012600261ULL + +static uint64_t XXH_read64(const void* memptr) { + uint64_t val; + memcpy(&val, memptr, sizeof(val)); + return val; +} + +static uint32_t XXH_read32(const void* memptr) { + uint32_t val; + memcpy(&val, memptr, sizeof(val)); + return val; +} + +static uint64_t XXH_rotl64(uint64_t x, int r) { + return (x << r) | (x >> (64 - r)); +} + +static uint64_t xxh3(const void* data, size_t len, uint64_t seed) { + const uint8_t* p = (const uint8_t*)data; + const uint8_t* const end = p + len; + uint64_t h64; + + if (len >= 32) { + const uint8_t* const limit = end - 32; + uint64_t v1 = seed + XXH_PRIME_1 + XXH_PRIME_2; + uint64_t v2 = seed + XXH_PRIME_2; + uint64_t v3 = seed + 0; + uint64_t v4 = seed - XXH_PRIME_1; + + do { + v1 += XXH_read64(p) * XXH_PRIME_2; + v1 = XXH_rotl64(v1, 31); + v1 *= XXH_PRIME_1; + + v2 += XXH_read64(p + 8) * XXH_PRIME_2; + v2 = XXH_rotl64(v2, 31); + v2 *= XXH_PRIME_1; + + v3 += XXH_read64(p + 16) * XXH_PRIME_2; + v3 = XXH_rotl64(v3, 31); + v3 *= XXH_PRIME_1; + + v4 += XXH_read64(p + 24) * XXH_PRIME_2; + v4 = XXH_rotl64(v4, 31); + v4 *= XXH_PRIME_1; + + p += 32; + } while (p <= limit); + + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + + XXH_rotl64(v4, 18); + + v1 *= XXH_PRIME_2; + v1 = XXH_rotl64(v1, 31); + v1 *= XXH_PRIME_1; + h64 ^= v1; + h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4; + + v2 *= XXH_PRIME_2; + v2 = XXH_rotl64(v2, 31); + v2 *= XXH_PRIME_1; + h64 ^= v2; + h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4; + + v3 *= XXH_PRIME_2; + v3 = XXH_rotl64(v3, 31); + v3 *= XXH_PRIME_1; + h64 ^= v3; + h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4; + + v4 *= XXH_PRIME_2; + v4 = XXH_rotl64(v4, 31); + v4 *= XXH_PRIME_1; + h64 ^= v4; + h64 = h64 * XXH_PRIME_1 + XXH_PRIME_4; + } + else { + h64 = seed + XXH_PRIME_5; + } + + h64 += (uint64_t)len; + + while (p + 8 <= end) { + uint64_t k1 = XXH_read64(p); + k1 *= XXH_PRIME_2; + k1 = XXH_rotl64(k1, 31); + k1 *= XXH_PRIME_1; + h64 ^= k1; + h64 = XXH_rotl64(h64, 27) * XXH_PRIME_1 + XXH_PRIME_4; + p += 8; + } + + if (p + 4 <= end) { + h64 ^= (uint64_t)(XXH_read32(p)) * XXH_PRIME_1; + h64 = XXH_rotl64(h64, 23) * XXH_PRIME_2 + XXH_PRIME_3; + p += 4; + } + + while (p < end) { + h64 ^= (*p) * XXH_PRIME_5; + h64 = XXH_rotl64(h64, 11) * XXH_PRIME_1; + p++; + } + + h64 ^= h64 >> 33; + h64 *= XXH_PRIME_2; + h64 ^= h64 >> 29; + h64 *= XXH_PRIME_3; + h64 ^= h64 >> 32; + + return h64; +} + +// hashmap_sip returns a hash value for `data` using SipHash-2-4. +uint64_t hashmap_sip(const void *data, size_t len, uint64_t seed0, + uint64_t seed1) +{ + return SIP64((uint8_t*)data, len, seed0, seed1); +} + +// hashmap_murmur returns a hash value for `data` using Murmur3_86_128. +uint64_t hashmap_murmur(const void *data, size_t len, uint64_t seed0, + uint64_t seed1) +{ + (void)seed1; + return MM86128(data, len, seed0); +} + +uint64_t hashmap_xxhash3(const void *data, size_t len, uint64_t seed0, + uint64_t seed1) +{ + (void)seed1; + return xxh3(data, len ,seed0); +} + +//============================================================================== +// TESTS AND BENCHMARKS +// $ cc -DHASHMAP_TEST hashmap.c && ./a.out # run tests +// $ cc -DHASHMAP_TEST -O3 hashmap.c && BENCH=1 ./a.out # run benchmarks +//============================================================================== +#ifdef HASHMAP_TEST + +static size_t deepcount(struct hashmap *map) { + size_t count = 0; + for (size_t i = 0; i < map->nbuckets; i++) { + if (bucket_at(map, i)->dib) { + count++; + } + } + return count; +} + +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wpedantic" +#endif +#ifdef __clang__ +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wcompound-token-split-by-macro" +#pragma GCC diagnostic ignored "-Wgnu-statement-expression-from-macro-expansion" +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +#include +#include +#include +#include +#include +#include "hashmap.h" + +static bool rand_alloc_fail = false; +static int rand_alloc_fail_odds = 3; // 1 in 3 chance malloc will fail. +static uintptr_t total_allocs = 0; +static uintptr_t total_mem = 0; + +static void *xmalloc(size_t size) { + if (rand_alloc_fail && rand()%rand_alloc_fail_odds == 0) { + return NULL; + } + void *mem = malloc(sizeof(uintptr_t)+size); + assert(mem); + *(uintptr_t*)mem = size; + total_allocs++; + total_mem += size; + return (char*)mem+sizeof(uintptr_t); +} + +static void xfree(void *ptr) { + if (ptr) { + total_mem -= *(uintptr_t*)((char*)ptr-sizeof(uintptr_t)); + free((char*)ptr-sizeof(uintptr_t)); + total_allocs--; + } +} + +static void shuffle(void *array, size_t numels, size_t elsize) { + char tmp[elsize]; + char *arr = array; + for (size_t i = 0; i < numels - 1; i++) { + int j = i + rand() / (RAND_MAX / (numels - i) + 1); + memcpy(tmp, arr + j * elsize, elsize); + memcpy(arr + j * elsize, arr + i * elsize, elsize); + memcpy(arr + i * elsize, tmp, elsize); + } +} + +static bool iter_ints(const void *item, void *udata) { + int *vals = *(int**)udata; + vals[*(int*)item] = 1; + return true; +} + +static int compare_ints_udata(const void *a, const void *b, void *udata) { + return *(int*)a - *(int*)b; +} + +static int compare_strs(const void *a, const void *b, void *udata) { + return strcmp(*(char**)a, *(char**)b); +} + +static uint64_t hash_int(const void *item, uint64_t seed0, uint64_t seed1) { + return hashmap_xxhash3(item, sizeof(int), seed0, seed1); + // return hashmap_sip(item, sizeof(int), seed0, seed1); + // return hashmap_murmur(item, sizeof(int), seed0, seed1); +} + +static uint64_t hash_str(const void *item, uint64_t seed0, uint64_t seed1) { + return hashmap_xxhash3(*(char**)item, strlen(*(char**)item), seed0, seed1); + // return hashmap_sip(*(char**)item, strlen(*(char**)item), seed0, seed1); + // return hashmap_murmur(*(char**)item, strlen(*(char**)item), seed0, seed1); +} + +static void free_str(void *item) { + xfree(*(char**)item); +} + +static void all(void) { + int seed = getenv("SEED")?atoi(getenv("SEED")):time(NULL); + int N = getenv("N")?atoi(getenv("N")):2000; + printf("seed=%d, count=%d, item_size=%zu\n", seed, N, sizeof(int)); + srand(seed); + + rand_alloc_fail = true; + + // test sip and murmur hashes + assert(hashmap_sip("hello", 5, 1, 2) == 2957200328589801622); + assert(hashmap_murmur("hello", 5, 1, 2) == 1682575153221130884); + assert(hashmap_xxhash3("hello", 5, 1, 2) == 2584346877953614258); + + int *vals; + while (!(vals = xmalloc(N * sizeof(int)))) {} + for (int i = 0; i < N; i++) { + vals[i] = i; + } + + struct hashmap *map; + + while (!(map = hashmap_new(sizeof(int), 0, seed, seed, + hash_int, compare_ints_udata, NULL, NULL))) {} + shuffle(vals, N, sizeof(int)); + for (int i = 0; i < N; i++) { + // // printf("== %d ==\n", vals[i]); + assert(map->count == (size_t)i); + assert(map->count == hashmap_count(map)); + assert(map->count == deepcount(map)); + const int *v; + assert(!hashmap_get(map, &vals[i])); + assert(!hashmap_delete(map, &vals[i])); + while (true) { + assert(!hashmap_set(map, &vals[i])); + if (!hashmap_oom(map)) { + break; + } + } + + for (int j = 0; j < i; j++) { + v = hashmap_get(map, &vals[j]); + assert(v && *v == vals[j]); + } + while (true) { + v = hashmap_set(map, &vals[i]); + if (!v) { + assert(hashmap_oom(map)); + continue; + } else { + assert(!hashmap_oom(map)); + assert(v && *v == vals[i]); + break; + } + } + v = hashmap_get(map, &vals[i]); + assert(v && *v == vals[i]); + v = hashmap_delete(map, &vals[i]); + assert(v && *v == vals[i]); + assert(!hashmap_get(map, &vals[i])); + assert(!hashmap_delete(map, &vals[i])); + assert(!hashmap_set(map, &vals[i])); + assert(map->count == (size_t)(i+1)); + assert(map->count == hashmap_count(map)); + assert(map->count == deepcount(map)); + } + + int *vals2; + while (!(vals2 = xmalloc(N * sizeof(int)))) {} + memset(vals2, 0, N * sizeof(int)); + assert(hashmap_scan(map, iter_ints, &vals2)); + + // Test hashmap_iter. This does the same as hashmap_scan above. + size_t iter = 0; + void *iter_val; + while (hashmap_iter (map, &iter, &iter_val)) { + assert (iter_ints(iter_val, &vals2)); + } + for (int i = 0; i < N; i++) { + assert(vals2[i] == 1); + } + xfree(vals2); + + shuffle(vals, N, sizeof(int)); + for (int i = 0; i < N; i++) { + const int *v; + v = hashmap_delete(map, &vals[i]); + assert(v && *v == vals[i]); + assert(!hashmap_get(map, &vals[i])); + assert(map->count == (size_t)(N-i-1)); + assert(map->count == hashmap_count(map)); + assert(map->count == deepcount(map)); + for (int j = N-1; j > i; j--) { + v = hashmap_get(map, &vals[j]); + assert(v && *v == vals[j]); + } + } + + for (int i = 0; i < N; i++) { + while (true) { + assert(!hashmap_set(map, &vals[i])); + if (!hashmap_oom(map)) { + break; + } + } + } + + assert(map->count != 0); + size_t prev_cap = map->cap; + hashmap_clear(map, true); + assert(prev_cap < map->cap); + assert(map->count == 0); + + + for (int i = 0; i < N; i++) { + while (true) { + assert(!hashmap_set(map, &vals[i])); + if (!hashmap_oom(map)) { + break; + } + } + } + + prev_cap = map->cap; + hashmap_clear(map, false); + assert(prev_cap == map->cap); + + hashmap_free(map); + + xfree(vals); + + + while (!(map = hashmap_new(sizeof(char*), 0, seed, seed, + hash_str, compare_strs, free_str, NULL))); + + for (int i = 0; i < N; i++) { + char *str; + while (!(str = xmalloc(16))); + snprintf(str, 16, "s%i", i); + while(!hashmap_set(map, &str)); + } + + hashmap_clear(map, false); + assert(hashmap_count(map) == 0); + + for (int i = 0; i < N; i++) { + char *str; + while (!(str = xmalloc(16))); + snprintf(str, 16, "s%i", i); + while(!hashmap_set(map, &str)); + } + + hashmap_free(map); + + if (total_allocs != 0) { + fprintf(stderr, "total_allocs: expected 0, got %lu\n", total_allocs); + exit(1); + } +} + +#define bench(name, N, code) {{ \ + if (strlen(name) > 0) { \ + printf("%-14s ", name); \ + } \ + size_t tmem = total_mem; \ + size_t tallocs = total_allocs; \ + uint64_t bytes = 0; \ + clock_t begin = clock(); \ + for (int i = 0; i < N; i++) { \ + (code); \ + } \ + clock_t end = clock(); \ + double elapsed_secs = (double)(end - begin) / CLOCKS_PER_SEC; \ + double bytes_sec = (double)bytes/elapsed_secs; \ + printf("%d ops in %.3f secs, %.0f ns/op, %.0f op/sec", \ + N, elapsed_secs, \ + elapsed_secs/(double)N*1e9, \ + (double)N/elapsed_secs \ + ); \ + if (bytes > 0) { \ + printf(", %.1f GB/sec", bytes_sec/1024/1024/1024); \ + } \ + if (total_mem > tmem) { \ + size_t used_mem = total_mem-tmem; \ + printf(", %.2f bytes/op", (double)used_mem/N); \ + } \ + if (total_allocs > tallocs) { \ + size_t used_allocs = total_allocs-tallocs; \ + printf(", %.2f allocs/op", (double)used_allocs/N); \ + } \ + printf("\n"); \ +}} + +static void benchmarks(void) { + int seed = getenv("SEED")?atoi(getenv("SEED")):time(NULL); + int N = getenv("N")?atoi(getenv("N")):5000000; + printf("seed=%d, count=%d, item_size=%zu\n", seed, N, sizeof(int)); + srand(seed); + + + int *vals = xmalloc(N * sizeof(int)); + for (int i = 0; i < N; i++) { + vals[i] = i; + } + + shuffle(vals, N, sizeof(int)); + + struct hashmap *map; + shuffle(vals, N, sizeof(int)); + + map = hashmap_new(sizeof(int), 0, seed, seed, hash_int, compare_ints_udata, + NULL, NULL); + bench("set", N, { + const int *v = hashmap_set(map, &vals[i]); + assert(!v); + }) + shuffle(vals, N, sizeof(int)); + bench("get", N, { + const int *v = hashmap_get(map, &vals[i]); + assert(v && *v == vals[i]); + }) + shuffle(vals, N, sizeof(int)); + bench("delete", N, { + const int *v = hashmap_delete(map, &vals[i]); + assert(v && *v == vals[i]); + }) + hashmap_free(map); + + map = hashmap_new(sizeof(int), N, seed, seed, hash_int, compare_ints_udata, + NULL, NULL); + bench("set (cap)", N, { + const int *v = hashmap_set(map, &vals[i]); + assert(!v); + }) + shuffle(vals, N, sizeof(int)); + bench("get (cap)", N, { + const int *v = hashmap_get(map, &vals[i]); + assert(v && *v == vals[i]); + }) + shuffle(vals, N, sizeof(int)); + bench("delete (cap)" , N, { + const int *v = hashmap_delete(map, &vals[i]); + assert(v && *v == vals[i]); + }) + + hashmap_free(map); + + + xfree(vals); + + if (total_allocs != 0) { + fprintf(stderr, "total_allocs: expected 0, got %lu\n", total_allocs); + exit(1); + } +} + +int main(void) { + hashmap_set_allocator(xmalloc, xfree); + + if (getenv("BENCH")) { + printf("Running hashmap.c benchmarks...\n"); + benchmarks(); + } else { + printf("Running hashmap.c tests...\n"); + all(); + printf("PASSED\n"); + } +} + + +#endif + + + diff --git a/hashmap/hashmap.h b/hashmap/hashmap.h new file mode 100644 index 00000000..e22990e0 --- /dev/null +++ b/hashmap/hashmap.h @@ -0,0 +1,62 @@ +// Copyright 2020 Joshua J Baker. All rights reserved. +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +#ifndef HASHMAP_H +#define HASHMAP_H + +#include +#include +#include + +#if defined(__cplusplus) +extern "C" { +#endif // __cplusplus + +struct hashmap; + +struct hashmap *hashmap_new(size_t elsize, size_t cap, uint64_t seed0, + uint64_t seed1, + uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1), + int (*compare)(const void *a, const void *b, void *udata), + void (*elfree)(void *item), + void *udata); + +struct hashmap *hashmap_new_with_allocator(void *(*malloc)(size_t), + void *(*realloc)(void *, size_t), void (*free)(void*), size_t elsize, + size_t cap, uint64_t seed0, uint64_t seed1, + uint64_t (*hash)(const void *item, uint64_t seed0, uint64_t seed1), + int (*compare)(const void *a, const void *b, void *udata), + void (*elfree)(void *item), + void *udata); + +void hashmap_free(struct hashmap *map); +void hashmap_clear(struct hashmap *map, bool update_cap); +size_t hashmap_count(struct hashmap *map); +bool hashmap_oom(struct hashmap *map); +const void *hashmap_get(struct hashmap *map, const void *item); +const void *hashmap_set(struct hashmap *map, const void *item); +const void *hashmap_delete(struct hashmap *map, const void *item); +const void *hashmap_probe(struct hashmap *map, uint64_t position); +bool hashmap_scan(struct hashmap *map, bool (*iter)(const void *item, void *udata), void *udata); +bool hashmap_iter(struct hashmap *map, size_t *i, void **item); + +uint64_t hashmap_sip(const void *data, size_t len, uint64_t seed0, uint64_t seed1); +uint64_t hashmap_murmur(const void *data, size_t len, uint64_t seed0, uint64_t seed1); +uint64_t hashmap_xxhash3(const void *data, size_t len, uint64_t seed0, uint64_t seed1); + +const void *hashmap_get_with_hash(struct hashmap *map, const void *key, uint64_t hash); +const void *hashmap_delete_with_hash(struct hashmap *map, const void *key, uint64_t hash); +const void *hashmap_set_with_hash(struct hashmap *map, const void *item, uint64_t hash); +void hashmap_set_grow_by_power(struct hashmap *map, size_t power); +void hashmap_set_load_factor(struct hashmap *map, double load_factor); + + +// DEPRECATED: use `hashmap_new_with_allocator` +void hashmap_set_allocator(void *(*malloc)(size_t), void (*free)(void*)); + +#if defined(__cplusplus) +} +#endif // __cplusplus + +#endif // HASHMAP_H diff --git a/qiniu/auth_mac.c b/qiniu/auth_mac.c index 85301f81..bd33f254 100644 --- a/qiniu/auth_mac.c +++ b/qiniu/auth_mac.c @@ -188,7 +188,7 @@ static Qiniu_Error Qiniu_Mac_Auth( char const *path; err = Qiniu_Mac_Parse_Url(url, NULL, NULL, &path, NULL); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -222,7 +222,7 @@ static Qiniu_Error Qiniu_For_Each_Header(Qiniu_Header *header, Qiniu_Error (*for for (Qiniu_Header *cur_header = header; cur_header != NULL; cur_header = cur_header->next) { Qiniu_Error err = for_each(cur_header, data); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -419,7 +419,7 @@ Qiniu_Mac_AuthV2( size_t hostLen = 0; char const *path = NULL; err = Qiniu_Mac_Parse_Url(url, &host, &hostLen, &path, NULL); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } diff --git a/qiniu/base.c b/qiniu/base.c index 76b1f313..a0f36ac0 100644 --- a/qiniu/base.c +++ b/qiniu/base.c @@ -674,8 +674,7 @@ Qiniu_Writer Qiniu_FILE_Writer(FILE *fp) /*============================================================================*/ /* func Qiniu_Copy */ -Qiniu_Error Qiniu_OK = { - 200, "OK"}; +Qiniu_Error Qiniu_OK = {200, "OK"}; Qiniu_Error Qiniu_Copy(Qiniu_Writer w, Qiniu_Reader r, void *buf, size_t n, Qiniu_Int64 *ret) { diff --git a/qiniu/cdn.c b/qiniu/cdn.c index fa74dce9..101ac6e9 100644 --- a/qiniu/cdn.c +++ b/qiniu/cdn.c @@ -5,7 +5,8 @@ #include char *Qiniu_CDN_CreateTimestampAntiLeechURL(const char *host, const char *fileName, char *queryStr, - Qiniu_Uint64 deadline, const char *cryptKey) { + Qiniu_Uint64 deadline, const char *cryptKey) +{ char *finalURL = NULL; Qiniu_Bool fileNameEscapeOk = Qiniu_False; Qiniu_Bool queryStrEscapeOk = Qiniu_False; @@ -15,24 +16,30 @@ char *Qiniu_CDN_CreateTimestampAntiLeechURL(const char *host, const char *fileNa char expireHex[20]; sprintf(expireHex, "%0llx", deadline); - if (queryStr != NULL && strcmp("", queryStr) != 0) { + if (queryStr != NULL && strcmp("", queryStr) != 0) + { queryStrEscaped = Qiniu_PathEscape(queryStr, &queryStrEscapeOk); } char *path = Qiniu_String_Concat2("/", fileNameEscaped); char *signStr = Qiniu_String_Concat(cryptKey, path, expireHex, NULL); - char *sign = (char *) Qiniu_MD5_HexStr(signStr); + char *sign = (char *)Qiniu_MD5_HexStr(signStr); - if (queryStrEscaped != NULL) { + if (queryStrEscaped != NULL) + { finalURL = Qiniu_String_Concat(host, path, "?", queryStrEscaped, "&sign=", sign, "&t=", expireHex, NULL); - } else { + } + else + { finalURL = Qiniu_String_Concat(host, path, "?sign=", sign, "&t=", expireHex, NULL); } - if (fileNameEscapeOk == Qiniu_True) { + if (fileNameEscapeOk == Qiniu_True) + { Qiniu_Free(fileNameEscaped); } - if (queryStrEscapeOk == Qiniu_True) { + if (queryStrEscapeOk == Qiniu_True) + { Qiniu_Free(queryStrEscaped); } Qiniu_Free(path); @@ -42,7 +49,8 @@ char *Qiniu_CDN_CreateTimestampAntiLeechURL(const char *host, const char *fileNa } Qiniu_Error Qiniu_CDN_RefreshUrls(Qiniu_Client *self, Qiniu_CDN_RefreshRet *ret, const char *urls[], - const int urlsCount) { + const int urlsCount) +{ Qiniu_Error err; char *path = "/v2/tune/refresh"; @@ -55,9 +63,10 @@ Qiniu_Error Qiniu_CDN_RefreshUrls(Qiniu_Client *self, Qiniu_CDN_RefreshRet *ret, Qiniu_Json *jsonRet = NULL; err = Qiniu_Client_CallWithBuffer2( - self, &jsonRet, url, body, strlen(body), "application/json"); + self, &jsonRet, url, body, strlen(body), "application/json"); - if (err.code == 200) { + if (err.code == Qiniu_OK.code) + { err = Qiniu_Parse_CDNRefreshRet(jsonRet, ret); } @@ -69,7 +78,8 @@ Qiniu_Error Qiniu_CDN_RefreshUrls(Qiniu_Client *self, Qiniu_CDN_RefreshRet *ret, } Qiniu_Error Qiniu_CDN_RefreshDirs(Qiniu_Client *self, Qiniu_CDN_RefreshRet *ret, const char *dirs[], - const int dirsCount) { + const int dirsCount) +{ Qiniu_Error err; char *path = "/v2/tune/refresh"; @@ -83,9 +93,10 @@ Qiniu_Error Qiniu_CDN_RefreshDirs(Qiniu_Client *self, Qiniu_CDN_RefreshRet *ret, Qiniu_Json *jsonRet = NULL; err = Qiniu_Client_CallWithBuffer2( - self, &jsonRet, url, body, strlen(body), "application/json"); + self, &jsonRet, url, body, strlen(body), "application/json"); - if (err.code == 200) { + if (err.code == Qiniu_OK.code) + { err = Qiniu_Parse_CDNRefreshRet(jsonRet, ret); } @@ -97,7 +108,8 @@ Qiniu_Error Qiniu_CDN_RefreshDirs(Qiniu_Client *self, Qiniu_CDN_RefreshRet *ret, } Qiniu_Error Qiniu_CDN_PrefetchUrls(Qiniu_Client *self, Qiniu_CDN_PrefetchRet *ret, const char *urls[], - const int urlsCount) { + const int urlsCount) +{ Qiniu_Error err; char *path = "/v2/tune/prefetch"; @@ -111,10 +123,10 @@ Qiniu_Error Qiniu_CDN_PrefetchUrls(Qiniu_Client *self, Qiniu_CDN_PrefetchRet *re Qiniu_Json *jsonRet = NULL; err = Qiniu_Client_CallWithBuffer2( - self, &jsonRet, url, body, strlen(body), "application/json"); - + self, &jsonRet, url, body, strlen(body), "application/json"); - if (err.code == 200) { + if (err.code == Qiniu_OK.code) + { err = Qiniu_Parse_CDNPrefetchRet(jsonRet, ret); } @@ -126,13 +138,14 @@ Qiniu_Error Qiniu_CDN_PrefetchUrls(Qiniu_Client *self, Qiniu_CDN_PrefetchRet *re } extern Qiniu_Error Qiniu_CDN_GetFluxData( - Qiniu_Client *self, - Qiniu_CDN_FluxRet *ret, - const char *startDate, - const char *endDate, - const char *granularity, - char *domains[], - const int num) { + Qiniu_Client *self, + Qiniu_CDN_FluxRet *ret, + const char *startDate, + const char *endDate, + const char *granularity, + char *domains[], + const int num) +{ Qiniu_Error err; char *path = "/v2/tune/flux"; @@ -150,9 +163,10 @@ extern Qiniu_Error Qiniu_CDN_GetFluxData( Qiniu_Json *jsonRet = NULL; err = Qiniu_Client_CallWithBuffer2( - self, &jsonRet, url, body, strlen(body), "application/json"); + self, &jsonRet, url, body, strlen(body), "application/json"); - if (err.code == 200) { + if (err.code == Qiniu_OK.code) + { err = Qiniu_Parse_CDNFluxRet(jsonRet, ret, domains, num); } @@ -165,13 +179,14 @@ extern Qiniu_Error Qiniu_CDN_GetFluxData( } extern Qiniu_Error Qiniu_CDN_GetBandwidthData( - Qiniu_Client *self, - Qiniu_CDN_BandwidthRet *ret, - const char *startDate, - const char *endDate, - const char *granularity, - char *domains[], - const int num) { + Qiniu_Client *self, + Qiniu_CDN_BandwidthRet *ret, + const char *startDate, + const char *endDate, + const char *granularity, + char *domains[], + const int num) +{ Qiniu_Error err; char *path = "/v2/tune/bandwidth"; @@ -189,10 +204,10 @@ extern Qiniu_Error Qiniu_CDN_GetBandwidthData( Qiniu_Json *jsonRet = NULL; err = Qiniu_Client_CallWithBuffer2( - self, &jsonRet, url, body, strlen(body), "application/json"); - + self, &jsonRet, url, body, strlen(body), "application/json"); - if (err.code == 200) { + if (err.code == Qiniu_OK.code) + { err = Qiniu_Parse_CDNBandwidthRet(jsonRet, ret, domains, num); } @@ -205,7 +220,8 @@ extern Qiniu_Error Qiniu_CDN_GetBandwidthData( } extern Qiniu_Error Qiniu_CDN_GetLogList(Qiniu_Client *self, Qiniu_CDN_LogListRet *ret, - char *domains[], const int domainsCount, const char *day) { + char *domains[], const int domainsCount, const char *day) +{ Qiniu_Error err; char *path = "/v2/tune/log/list"; @@ -221,10 +237,10 @@ extern Qiniu_Error Qiniu_CDN_GetLogList(Qiniu_Client *self, Qiniu_CDN_LogListRet Qiniu_Json *jsonRet = NULL; err = Qiniu_Client_CallWithBuffer2( - self, &jsonRet, url, body, strlen(body), "application/json"); + self, &jsonRet, url, body, strlen(body), "application/json"); - - if (err.code == 200) { + if (err.code == Qiniu_OK.code) + { err = Qiniu_Parse_CDNLogListRet(jsonRet, ret, domains, domainsCount); } @@ -236,7 +252,8 @@ extern Qiniu_Error Qiniu_CDN_GetLogList(Qiniu_Client *self, Qiniu_CDN_LogListRet return err; } -Qiniu_Error Qiniu_Parse_CDNRefreshRet(Qiniu_Json *root, Qiniu_CDN_RefreshRet *ret) { +Qiniu_Error Qiniu_Parse_CDNRefreshRet(Qiniu_Json *root, Qiniu_CDN_RefreshRet *ret) +{ Qiniu_Error err; int i; @@ -246,31 +263,39 @@ Qiniu_Error Qiniu_Parse_CDNRefreshRet(Qiniu_Json *root, Qiniu_CDN_RefreshRet *re Qiniu_Json *invalidUrls = cJSON_GetObjectItem(root, "invalidUrls"); int invalidUrlsCount = cJSON_GetArraySize(invalidUrls); - if (invalidUrlsCount > 0) { + if (invalidUrlsCount > 0) + { ret->invalidUrlsCount = invalidUrlsCount; - ret->invalidUrls = (char **) malloc(sizeof(char **) * invalidUrlsCount); + ret->invalidUrls = (char **)malloc(sizeof(char **) * invalidUrlsCount); char **pUrls = ret->invalidUrls; - for (i = 0; i < invalidUrlsCount; i++) { + for (i = 0; i < invalidUrlsCount; i++) + { *pUrls = cJSON_GetArrayItem(invalidUrls, i)->valuestring; ++pUrls; } - } else { + } + else + { ret->invalidUrls = NULL; } Qiniu_Json *invalidDirs = cJSON_GetObjectItem(root, "invalidDirs"); int invalidDirsCount = cJSON_GetArraySize(invalidDirs); - if (invalidDirsCount > 0) { + if (invalidDirsCount > 0) + { ret->invalidDirsCount = invalidDirsCount; - ret->invalidDirs = (char **) malloc(sizeof(char **) * invalidDirsCount); + ret->invalidDirs = (char **)malloc(sizeof(char **) * invalidDirsCount); char **pDirs = ret->invalidDirs; - for (i = 0; i < invalidDirsCount; i++) { + for (i = 0; i < invalidDirsCount; i++) + { *pDirs = cJSON_GetArrayItem(invalidDirs, i)->valuestring; ++pDirs; } - } else { + } + else + { ret->invalidDirs = NULL; } @@ -279,13 +304,14 @@ Qiniu_Error Qiniu_Parse_CDNRefreshRet(Qiniu_Json *root, Qiniu_CDN_RefreshRet *re ret->dirQuotaDay = Qiniu_Json_GetInt(root, "dirQuotaDay", 0); ret->dirSurplusDay = Qiniu_Json_GetInt(root, "dirSurplusDay", 0); - err.code = 200; + err.code = Qiniu_OK.code; err.message = "OK"; return err; } -Qiniu_Error Qiniu_Parse_CDNPrefetchRet(Qiniu_Json *root, Qiniu_CDN_PrefetchRet *ret) { +Qiniu_Error Qiniu_Parse_CDNPrefetchRet(Qiniu_Json *root, Qiniu_CDN_PrefetchRet *ret) +{ Qiniu_Error err; int i; @@ -295,30 +321,35 @@ Qiniu_Error Qiniu_Parse_CDNPrefetchRet(Qiniu_Json *root, Qiniu_CDN_PrefetchRet * Qiniu_Json *invalidUrls = cJSON_GetObjectItem(root, "invalidUrls"); int invalidUrlsCount = cJSON_GetArraySize(invalidUrls); - if (invalidUrlsCount > 0) { + if (invalidUrlsCount > 0) + { ret->invalidUrlsCount = invalidUrlsCount; - ret->invalidUrls = (char **) malloc(sizeof(char **) * invalidUrlsCount); + ret->invalidUrls = (char **)malloc(sizeof(char **) * invalidUrlsCount); char **pUrls = ret->invalidUrls; - for (i = 0; i < invalidUrlsCount; i++) { + for (i = 0; i < invalidUrlsCount; i++) + { *pUrls = cJSON_GetArrayItem(invalidUrls, i)->valuestring; ++pUrls; } - } else { + } + else + { ret->invalidUrls = NULL; } ret->quotaDay = Qiniu_Json_GetInt(root, "quotaDay", 0); ret->surplusDay = Qiniu_Json_GetInt(root, "surplusDay", 0); - err.code = 200; + err.code = Qiniu_OK.code; err.message = "OK"; return err; } Qiniu_Error Qiniu_Parse_CDNFluxRet(Qiniu_Json *root, Qiniu_CDN_FluxRet *ret, char *domains[], - const int domainsCount) { + const int domainsCount) +{ Qiniu_Error err; int i, j; @@ -326,9 +357,10 @@ Qiniu_Error Qiniu_Parse_CDNFluxRet(Qiniu_Json *root, Qiniu_CDN_FluxRet *ret, cha ret->error = Qiniu_Json_GetString(root, "error", NULL); ret->domainsCount = domainsCount; - //parse time + // parse time int timeCount = Qiniu_Json_GetArraySize(root, "time", 0); - if (timeCount == 0) { + if (timeCount == 0) + { ret->time = NULL; ret->timeCount = 0; ret->data = NULL; @@ -340,77 +372,91 @@ Qiniu_Error Qiniu_Parse_CDNFluxRet(Qiniu_Json *root, Qiniu_CDN_FluxRet *ret, cha Qiniu_Json *time = Qiniu_Json_GetObjectItem(root, "time", NULL); ret->timeCount = timeCount; - ret->time = (char **) malloc(sizeof(char **) * timeCount); + ret->time = (char **)malloc(sizeof(char **) * timeCount); char **pTime = ret->time; - for (i = 0; i < timeCount; i++) { + for (i = 0; i < timeCount; i++) + { char *tVal = Qiniu_Json_GetArrayItem(time, i, NULL)->valuestring; *pTime = tVal; ++pTime; } - //parse data + // parse data Qiniu_Json *data = Qiniu_Json_GetObjectItem(root, "data", NULL); - if (data == NULL) { + if (data == NULL) + { ret->data = NULL; err.code = 9999; err.message = "No cdn flux data"; return err; } - ret->data = (Qiniu_CDN_FluxData *) malloc(sizeof(Qiniu_CDN_FluxData) * domainsCount); - for (i = 0; i < domainsCount; ++i) { + ret->data = (Qiniu_CDN_FluxData *)malloc(sizeof(Qiniu_CDN_FluxData) * domainsCount); + for (i = 0; i < domainsCount; ++i) + { char *domain = domains[i]; Qiniu_CDN_FluxData fluxData; fluxData.domain = domain; Qiniu_Json *domainFluxObj = Qiniu_Json_GetObjectItem(data, domain, NULL); - if (domainFluxObj == NULL) { + if (domainFluxObj == NULL) + { fluxData.chinaCount = 0; fluxData.china = NULL; fluxData.overseaCount = 0; fluxData.oversea = NULL; - } else { - //parse china + } + else + { + // parse china Qiniu_Json *china = Qiniu_Json_GetObjectItem(domainFluxObj, "china", NULL); - if (china == NULL) { + if (china == NULL) + { fluxData.chinaCount = 0; fluxData.china = NULL; - } else { + } + else + { int chinaCount = Qiniu_Json_GetArraySize(domainFluxObj, "china", 0); fluxData.chinaCount = chinaCount; - fluxData.china = (Qiniu_Uint64 *) malloc(sizeof(Qiniu_Uint64) * chinaCount); - for (j = 0; j < chinaCount; j++) { - fluxData.china[j] = (Qiniu_Uint64) Qiniu_Json_GetArrayItem(china, j, 0)->valuedouble; + fluxData.china = (Qiniu_Uint64 *)malloc(sizeof(Qiniu_Uint64) * chinaCount); + for (j = 0; j < chinaCount; j++) + { + fluxData.china[j] = (Qiniu_Uint64)Qiniu_Json_GetArrayItem(china, j, 0)->valuedouble; } } - //parse oversea + // parse oversea Qiniu_Json *oversea = Qiniu_Json_GetObjectItem(domainFluxObj, "oversea", NULL); - if (oversea == NULL) { + if (oversea == NULL) + { fluxData.overseaCount = 0; fluxData.oversea = NULL; - } else { + } + else + { int overseaCount = Qiniu_Json_GetArraySize(domainFluxObj, "oversea", 0); fluxData.overseaCount = overseaCount; - fluxData.oversea = (Qiniu_Uint64 *) malloc(sizeof(Qiniu_Uint64) * overseaCount); - for (j = 0; j < overseaCount; j++) { - fluxData.oversea[j] = (Qiniu_Uint64) Qiniu_Json_GetArrayItem(oversea, j, 0)->valuedouble; + fluxData.oversea = (Qiniu_Uint64 *)malloc(sizeof(Qiniu_Uint64) * overseaCount); + for (j = 0; j < overseaCount; j++) + { + fluxData.oversea[j] = (Qiniu_Uint64)Qiniu_Json_GetArrayItem(oversea, j, 0)->valuedouble; } } - } ret->data[i] = fluxData; } - err.code = 200; + err.code = Qiniu_OK.code; err.message = "OK"; return err; } Qiniu_Error Qiniu_Parse_CDNBandwidthRet(Qiniu_Json *root, Qiniu_CDN_BandwidthRet *ret, - char *domains[], const int domainsCount) { + char *domains[], const int domainsCount) +{ Qiniu_Error err; int i, j; @@ -418,9 +464,10 @@ Qiniu_Error Qiniu_Parse_CDNBandwidthRet(Qiniu_Json *root, Qiniu_CDN_BandwidthRet ret->error = Qiniu_Json_GetString(root, "error", NULL); ret->domainsCount = domainsCount; - //parse time + // parse time int timeCount = Qiniu_Json_GetArraySize(root, "time", 0); - if (timeCount == 0) { + if (timeCount == 0) + { ret->time = NULL; ret->timeCount = 0; ret->data = NULL; @@ -432,77 +479,91 @@ Qiniu_Error Qiniu_Parse_CDNBandwidthRet(Qiniu_Json *root, Qiniu_CDN_BandwidthRet Qiniu_Json *time = Qiniu_Json_GetObjectItem(root, "time", NULL); ret->timeCount = timeCount; - ret->time = (char **) malloc(sizeof(char **) * timeCount); + ret->time = (char **)malloc(sizeof(char **) * timeCount); char **pTime = ret->time; - for (i = 0; i < timeCount; i++) { + for (i = 0; i < timeCount; i++) + { char *tVal = Qiniu_Json_GetArrayItem(time, i, NULL)->valuestring; *pTime = tVal; ++pTime; } - //parse data + // parse data Qiniu_Json *data = Qiniu_Json_GetObjectItem(root, "data", NULL); - if (data == NULL) { + if (data == NULL) + { ret->data = NULL; err.code = 9999; err.message = "No cdn bandwidth data"; return err; } - ret->data = (Qiniu_CDN_BandwidthData *) malloc(sizeof(Qiniu_CDN_BandwidthData) * domainsCount); - for (i = 0; i < domainsCount; ++i) { + ret->data = (Qiniu_CDN_BandwidthData *)malloc(sizeof(Qiniu_CDN_BandwidthData) * domainsCount); + for (i = 0; i < domainsCount; ++i) + { char *domain = domains[i]; Qiniu_CDN_BandwidthData bandData; bandData.domain = domain; Qiniu_Json *domainBandObj = Qiniu_Json_GetObjectItem(data, domain, NULL); - if (domainBandObj == NULL) { + if (domainBandObj == NULL) + { bandData.chinaCount = 0; bandData.china = NULL; bandData.overseaCount = 0; bandData.oversea = NULL; - } else { - //parse china + } + else + { + // parse china Qiniu_Json *china = Qiniu_Json_GetObjectItem(domainBandObj, "china", NULL); - if (china == NULL) { + if (china == NULL) + { bandData.chinaCount = 0; bandData.china = NULL; - } else { + } + else + { int chinaCount = Qiniu_Json_GetArraySize(domainBandObj, "china", 0); bandData.chinaCount = chinaCount; - bandData.china = (Qiniu_Uint64 *) malloc(sizeof(Qiniu_Uint64) * chinaCount); - for (j = 0; j < chinaCount; j++) { - bandData.china[j] = (Qiniu_Uint64) Qiniu_Json_GetArrayItem(china, j, 0)->valuedouble; + bandData.china = (Qiniu_Uint64 *)malloc(sizeof(Qiniu_Uint64) * chinaCount); + for (j = 0; j < chinaCount; j++) + { + bandData.china[j] = (Qiniu_Uint64)Qiniu_Json_GetArrayItem(china, j, 0)->valuedouble; } } - //parse oversea + // parse oversea Qiniu_Json *oversea = Qiniu_Json_GetObjectItem(domainBandObj, "oversea", NULL); - if (oversea == NULL) { + if (oversea == NULL) + { bandData.overseaCount = 0; bandData.oversea = NULL; - } else { + } + else + { int overseaCount = Qiniu_Json_GetArraySize(domainBandObj, "oversea", 0); bandData.overseaCount = overseaCount; - bandData.oversea = (Qiniu_Uint64 *) malloc(sizeof(Qiniu_Uint64) * overseaCount); - for (j = 0; j < overseaCount; j++) { - bandData.oversea[j] = (Qiniu_Uint64) Qiniu_Json_GetArrayItem(oversea, j, 0)->valuedouble; + bandData.oversea = (Qiniu_Uint64 *)malloc(sizeof(Qiniu_Uint64) * overseaCount); + for (j = 0; j < overseaCount; j++) + { + bandData.oversea[j] = (Qiniu_Uint64)Qiniu_Json_GetArrayItem(oversea, j, 0)->valuedouble; } } - } ret->data[i] = bandData; } - err.code = 200; + err.code = Qiniu_OK.code; err.message = "OK"; return err; } Qiniu_Error Qiniu_Parse_CDNLogListRet(Qiniu_Json *root, Qiniu_CDN_LogListRet *ret, char *domains[], - const int domainsCount) { + const int domainsCount) +{ Qiniu_Error err; int i, j; @@ -511,30 +572,36 @@ Qiniu_Error Qiniu_Parse_CDNLogListRet(Qiniu_Json *root, Qiniu_CDN_LogListRet *re ret->domainsCount = domainsCount; Qiniu_Json *data = Qiniu_Json_GetObjectItem(root, "data", NULL); - if (data == NULL) { + if (data == NULL) + { ret->data = NULL; err.code = 9999; err.message = "No cdn log data"; return err; } - //parse log list - ret->data = (Qiniu_CDN_LogListData *) malloc(sizeof(Qiniu_CDN_LogListData) * domainsCount); - for (i = 0; i < domainsCount; i++) { + // parse log list + ret->data = (Qiniu_CDN_LogListData *)malloc(sizeof(Qiniu_CDN_LogListData) * domainsCount); + for (i = 0; i < domainsCount; i++) + { char *domain = domains[i]; Qiniu_CDN_LogListData logData; logData.domain = domain; int itemsCount = Qiniu_Json_GetArraySize(data, domain, 0); - if (itemsCount == 0) { + if (itemsCount == 0) + { logData.items = NULL; logData.itemsCount = 0; - } else { + } + else + { logData.itemsCount = itemsCount; Qiniu_Json *domainLogs = Qiniu_Json_GetObjectItem(data, domain, NULL); - logData.items = (Qiniu_CDN_LogListDataItem *) malloc(sizeof(Qiniu_CDN_LogListDataItem) * itemsCount); - for (j = 0; j < itemsCount; j++) { + logData.items = (Qiniu_CDN_LogListDataItem *)malloc(sizeof(Qiniu_CDN_LogListDataItem) * itemsCount); + for (j = 0; j < itemsCount; j++) + { Qiniu_Json *sub = Qiniu_Json_GetArrayItem(domainLogs, j, NULL); Qiniu_CDN_LogListDataItem logItem; logItem.name = Qiniu_Json_GetString(sub, "name", NULL); @@ -548,36 +615,46 @@ Qiniu_Error Qiniu_Parse_CDNLogListRet(Qiniu_Json *root, Qiniu_CDN_LogListRet *re ret->data[i] = logData; } - err.code = 200; + err.code = Qiniu_OK.code; err.message = "OK"; return err; } -void Qiniu_Free_CDNRefreshRet(Qiniu_CDN_RefreshRet *ret) { - if (ret->invalidUrls != NULL) { +void Qiniu_Free_CDNRefreshRet(Qiniu_CDN_RefreshRet *ret) +{ + if (ret->invalidUrls != NULL) + { Qiniu_Free(ret->invalidUrls); } - if (ret->invalidDirs != NULL) { + if (ret->invalidDirs != NULL) + { Qiniu_Free(ret->invalidDirs); } } -void Qiniu_Free_CDNPrefetchRet(Qiniu_CDN_PrefetchRet *ret) { - if (ret->invalidUrls != NULL) { +void Qiniu_Free_CDNPrefetchRet(Qiniu_CDN_PrefetchRet *ret) +{ + if (ret->invalidUrls != NULL) + { Qiniu_Free(ret->invalidUrls); } } -void Qiniu_Free_CDNFluxRet(Qiniu_CDN_FluxRet *ret) { +void Qiniu_Free_CDNFluxRet(Qiniu_CDN_FluxRet *ret) +{ int i; - if (ret->data != NULL) { - for (i = 0; i < ret->domainsCount; ++i) { - if (ret->data[i].china != NULL) { + if (ret->data != NULL) + { + for (i = 0; i < ret->domainsCount; ++i) + { + if (ret->data[i].china != NULL) + { Qiniu_Free(ret->data[i].china); } - if (ret->data[i].oversea != NULL) { + if (ret->data[i].oversea != NULL) + { Qiniu_Free(ret->data[i].oversea); } } @@ -585,15 +662,20 @@ void Qiniu_Free_CDNFluxRet(Qiniu_CDN_FluxRet *ret) { } } -void Qiniu_Free_CDNBandwidthRet(Qiniu_CDN_BandwidthRet *ret) { +void Qiniu_Free_CDNBandwidthRet(Qiniu_CDN_BandwidthRet *ret) +{ int i; - if (ret->data != NULL) { - for (i = 0; i < ret->domainsCount; ++i) { - if (ret->data[i].china != NULL) { + if (ret->data != NULL) + { + for (i = 0; i < ret->domainsCount; ++i) + { + if (ret->data[i].china != NULL) + { Qiniu_Free(ret->data[i].china); } - if (ret->data[i].oversea != NULL) { + if (ret->data[i].oversea != NULL) + { Qiniu_Free(ret->data[i].oversea); } } @@ -601,14 +683,18 @@ void Qiniu_Free_CDNBandwidthRet(Qiniu_CDN_BandwidthRet *ret) { } } -void Qiniu_Free_CDNLogListRet(Qiniu_CDN_LogListRet *ret) { +void Qiniu_Free_CDNLogListRet(Qiniu_CDN_LogListRet *ret) +{ int i; - if (ret->data != NULL) { - for (i = 0; i < ret->domainsCount; i++) { - if (ret->data[i].itemsCount > 0) { + if (ret->data != NULL) + { + for (i = 0; i < ret->domainsCount; i++) + { + if (ret->data[i].itemsCount > 0) + { Qiniu_Free(ret->data[i].items); } } Qiniu_Free(ret->data); } -} \ No newline at end of file +} diff --git a/qiniu/fop.c b/qiniu/fop.c index 3fdefe90..760c2f0f 100644 --- a/qiniu/fop.c +++ b/qiniu/fop.c @@ -94,7 +94,7 @@ Qiniu_Error Qiniu_FOP_Pfop(Qiniu_Client *self, Qiniu_FOP_PfopRet *ret, const cha const char *apiHost; err = _Qiniu_Region_Get_Api_Host(self, NULL, bucket, &apiHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { goto error; } @@ -107,7 +107,7 @@ Qiniu_Error Qiniu_FOP_Pfop(Qiniu_Client *self, Qiniu_FOP_PfopRet *ret, const cha body, strlen(body), "application/x-www-form-urlencoded"); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { ret->persistentId = Qiniu_Json_GetString(root, "persistentId", 0); } diff --git a/qiniu/http.c b/qiniu/http.c index 2068ace5..a43812fc 100644 --- a/qiniu/http.c +++ b/qiniu/http.c @@ -9,6 +9,7 @@ #include "http.h" #include "../cJSON/cJSON.h" +#include "../hashmap/hashmap.h" #include Qiniu_Error Qiniu_Client_config(Qiniu_Client *self); @@ -377,8 +378,11 @@ void Qiniu_Client_Cleanup(Qiniu_Client *self) Qiniu_Buffer_Cleanup(&self->b); Qiniu_Buffer_Cleanup(&self->respHeader); - Qiniu_FreeV2((void **)&self->cachedRegion); - Qiniu_FreeV2((void **)&self->cachedRegionBucketName); + if (self->cachedRegions != NULL) + { + hashmap_free(self->cachedRegions); + self->cachedRegions = NULL; + } } void Qiniu_Client_BindNic(Qiniu_Client *self, const char *nic) @@ -474,7 +478,7 @@ static Qiniu_Error Qiniu_Client_callWithBody( Qiniu_Header *headers = NULL; CURL *curl = (CURL *)self->curl; err = Qiniu_Client_config(self); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -509,7 +513,7 @@ static Qiniu_Error Qiniu_Client_callWithBody( bodyLen = 0; } err = Qiniu_Do_Auth(self, "POST", &headers, url, body, (size_t)bodyLen); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -587,7 +591,7 @@ Qiniu_Error Qiniu_Client_Call(Qiniu_Client *self, Qiniu_Json **ret, const char * Qiniu_Header *headers = NULL; CURL *curl = Qiniu_Client_initcall(self, url); err = Qiniu_Client_config(self); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -595,7 +599,7 @@ Qiniu_Error Qiniu_Client_Call(Qiniu_Client *self, Qiniu_Json **ret, const char * if (self->auth.itbl != NULL) { err = Qiniu_Do_Auth(self, "POST", &headers, url, NULL, 0); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -615,7 +619,7 @@ Qiniu_Error Qiniu_Client_CallNoRet(Qiniu_Client *self, const char *url) Qiniu_Header *headers = NULL; CURL *curl = Qiniu_Client_initcall(self, url); err = Qiniu_Client_config(self); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -623,7 +627,7 @@ Qiniu_Error Qiniu_Client_CallNoRet(Qiniu_Client *self, const char *url) if (self->auth.itbl != NULL) { err = Qiniu_Do_Auth(self, "POST", &headers, url, NULL, 0); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } diff --git a/qiniu/http.h b/qiniu/http.h index 240082b1..a06a5bc7 100644 --- a/qiniu/http.h +++ b/qiniu/http.h @@ -106,6 +106,8 @@ extern "C" typedef struct _Qiniu_Region Qiniu_Region; QINIU_DLLAPI extern void Qiniu_Region_Free(Qiniu_Region *region); + struct hashmap; + struct _Qiniu_Client { void *curl; @@ -116,8 +118,7 @@ extern "C" Qiniu_Bool autoQueryRegion; Qiniu_Bool autoQueryHttpsRegion; - const char *cachedRegionBucketName; - Qiniu_Region *cachedRegion; + struct hashmap *cachedRegions; Qiniu_Region *specifiedRegion; // Use the following field to specify which NIC to use for sending packets. diff --git a/qiniu/io.c b/qiniu/io.c index 31ea14e2..d030d057 100644 --- a/qiniu/io.c +++ b/qiniu/io.c @@ -72,7 +72,7 @@ static Qiniu_Error Get_Qiniu_UpHost(Qiniu_Client *client, const char *accessKey, else { Qiniu_Error err = _Qiniu_Region_Get_Up_Host(client, accessKey, bucketName, upHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -154,12 +154,12 @@ static Qiniu_Error Qiniu_Io_call( const char *upHost = NULL; err = Qiniu_Client_config(self); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } err = Get_Qiniu_UpHost(self, accessKey, bucketName, extra, &upHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -178,7 +178,7 @@ static Qiniu_Error Qiniu_Io_call( } // if err = Qiniu_callex(curl, &self->b, &self->root, Qiniu_False, &self->respHeader); - if (err.code == 200 && ret != NULL) + if (err.code == Qiniu_OK.code && ret != NULL) { if (extra->callbackRetParser != NULL) { @@ -233,14 +233,14 @@ Qiniu_Error Qiniu_Io_PutFile( rdr.abortUserData = extra->upAbortUserData; err = Qiniu_Rd_Reader_Open(&rdr, localFile); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { goto error; } // if Qiniu_Zero(fi); err = Qiniu_File_Stat(rdr.file, &fi); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { goto error; } // if @@ -332,12 +332,12 @@ static Qiniu_Error Qiniu_Io_call_with_callback( CURL *curl = Qiniu_Client_reset(self); err = Qiniu_Client_config(self); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } err = Get_Qiniu_UpHost(self, accessKey, bucketName, extra, &upHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -350,7 +350,7 @@ static Qiniu_Error Qiniu_Io_call_with_callback( curl_easy_setopt(curl, CURLOPT_READFUNCTION, rdr); err = Qiniu_callex(curl, &self->b, &self->root, Qiniu_False, &self->respHeader); - if (err.code == 200 && ret != NULL) + if (err.code == Qiniu_OK.code && ret != NULL) { if (extra->callbackRetParser != NULL) { @@ -421,7 +421,7 @@ Qiniu_Error Qiniu_UptokenAuth_ToHeader( *header = curl_slist_append(*header, self); - err.code = 200; + err.code = Qiniu_OK.code; err.message = "OK"; return err; } diff --git a/qiniu/multipart_upload.c b/qiniu/multipart_upload.c index cea290bd..e2d5cc8a 100644 --- a/qiniu/multipart_upload.c +++ b/qiniu/multipart_upload.c @@ -136,7 +136,7 @@ static Qiniu_Error init_upload(Qiniu_Client *client, const char *bucket, const c Qiniu_Log_Error("init_upload retry: %E", err); continue; } - else if (err.code != 200) + else if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("init_upload: %E", err); break; @@ -165,7 +165,7 @@ Qiniu_Error upload_one_part(Qiniu_Client *client, Qiniu_Multipart_PutExtra *extr Qiniu_Log_Error("upload_part retry: partNum:%d, %E", partNum, err); continue; } - else if (err.code != 200) + else if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("upload_part: partNum:%d, %E", partNum, err); break; @@ -180,11 +180,11 @@ Qiniu_Error upload_one_part(Qiniu_Client *client, Qiniu_Multipart_PutExtra *extr break; } // notify callback - if ((err.code == 200) && extraParam->notify) + if ((err.code == Qiniu_OK.code) && extraParam->notify) { extraParam->notify(ret); } - if ((err.code != 200) && extraParam->notifyErr) + if ((err.code != Qiniu_OK.code) && extraParam->notifyErr) { extraParam->notifyErr(partNum, err); } @@ -228,7 +228,7 @@ static Qiniu_Error upload_parts(Qiniu_Client *client, const char *bucket, const Qiniu_Multi_Free(2, (void *)reqUrl, (void *)md5str); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -236,7 +236,7 @@ static Qiniu_Error upload_parts(Qiniu_Client *client, const char *bucket, const if (recorder != NULL && recorder->recorderMedium != NULL) { err = writeMedium(recorder->recorderMedium, initParts, &uploadPartsRet->PartsRet[partNum]); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -298,7 +298,7 @@ static Qiniu_Error complete_upload( { extraParam->recorder->remove(extraParam->recorder, recorder->recorderKey); } - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("cupload: uploadId:%s, %E", initPartsRet->uploadId, err); break; @@ -330,7 +330,7 @@ static Qiniu_Error _Qiniu_Multipart_Put( } err = verifyParam(client, accessKey, bucket, extraParam); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Multi_Free(2, (void *)accessKey, (void *)bucket); Qiniu_Log_Error("invalid param %E", err); @@ -344,7 +344,7 @@ static Qiniu_Error _Qiniu_Multipart_Put( int totalPartNum; err = getTotalPartNum(fsize, extraParam->partSize, &totalPartNum); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -363,7 +363,7 @@ static Qiniu_Error _Qiniu_Multipart_Put( reinit: // step1: init part err = init_upload(client, bucket, encodedKey, extraParam, &initPartRet); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("initUpload %E ", err); Qiniu_Multi_Free(2, (void *)bucket, (void *)encodedKey); @@ -384,7 +384,7 @@ static Qiniu_Error _Qiniu_Multipart_Put( mustInitialize = Qiniu_True; goto reinit; } - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("upload_part %E", err); Qiniu_Multi_Free(2, (void *)bucket, (void *)encodedKey); @@ -404,7 +404,7 @@ static Qiniu_Error _Qiniu_Multipart_Put( mustInitialize = Qiniu_True; goto reinit; } - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("complete_upload %E", err); } @@ -434,13 +434,13 @@ Qiniu_Error Qiniu_Multipart_PutFile( Qiniu_Multipart_Recorder recorder = {NULL, NULL, Qiniu_False}, *pRecorder = NULL; Qiniu_Error err = openFileReader(fileName, &f); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("openFileReader failed %E", err); return err; } err = Qiniu_File_Stat(f, &fi); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("statFile failed %E", err); return err; @@ -448,7 +448,7 @@ Qiniu_Error Qiniu_Multipart_PutFile( Qiniu_ReaderAt reader = Qiniu_FileReaderAt(f); err = initializeRecorder(extraParam, uptoken, key, fileName, &fi, &medium, &recorder); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Log_Error("initializeRecorder failed %E", err); return err; @@ -478,7 +478,7 @@ Qiniu_Error openFileReader(const char *fileName, Qiniu_File **f) Qiniu_FileInfo fi; Qiniu_Error err = Qiniu_File_Open(f, fileName); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -563,7 +563,7 @@ Qiniu_Error readMedium(struct Qiniu_Record_Medium *medium, char **uploadId, Qini size_t haveRead; char buf[BUFFER_SIZE]; err = medium->readEntry(medium, buf, BUFFER_SIZE, &haveRead); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { medium->close(medium); return err; @@ -614,7 +614,7 @@ Qiniu_Error writeMedium(struct Qiniu_Record_Medium *medium, const Qiniu_InitPart } err = medium->writeEntry(medium, blockInfoJson, NULL); free(blockInfoJson); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -632,12 +632,12 @@ Qiniu_Error initializeRecorder(Qiniu_Multipart_PutExtra *param, if (param->recorder != NULL) { err = Qiniu_Utils_Generate_RecorderKey(uptoken, "v2", key, fileName, &recorder->recorderKey); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } err = Qiniu_Utils_Find_Medium(param->recorder, recorder->recorderKey, 1, medium, fi, &ok); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -649,7 +649,7 @@ Qiniu_Error initializeRecorder(Qiniu_Multipart_PutExtra *param, else { err = Qiniu_Utils_New_Medium(param->recorder, recorder->recorderKey, 1, medium, fi); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -665,7 +665,7 @@ Qiniu_Error reinitializeRecorder(Qiniu_Multipart_PutExtra *param, Qiniu_FileInfo Qiniu_Error err; recorder->recorderMedium->close(recorder->recorderMedium); err = Qiniu_Utils_New_Medium(param->recorder, recorder->recorderKey, 1, recorder->recorderMedium, fi); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -686,7 +686,7 @@ Qiniu_Error loadProgresses(Qiniu_Multipart_Recorder *recorder, Qiniu_InitPart_Re for (;;) { Qiniu_Error err = recorder->recorderMedium->hasNextEntry(recorder->recorderMedium, &hasNext); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -700,7 +700,7 @@ Qiniu_Error loadProgresses(Qiniu_Multipart_Recorder *recorder, Qiniu_InitPart_Re { err = readMedium(recorder->recorderMedium, &uploadId, &expireAt, &uploadPartRet); } - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } diff --git a/qiniu/qetag.c b/qiniu/qetag.c index 9e9bcaa5..7cfda6ff 100644 --- a/qiniu/qetag.c +++ b/qiniu/qetag.c @@ -15,384 +15,439 @@ extern "C" #define BLOCK_ELEMENT_MAX_COUNT 16 #define BLOCK_MAX_SIZE (1 << 22) -typedef struct _Qiniu_Qetag_Block { - struct { - unsigned int capacity:23; - unsigned int done:1; - unsigned int reserved:8; - }; + typedef struct _Qiniu_Qetag_Block + { + struct + { + unsigned int capacity : 23; + unsigned int done : 1; + unsigned int reserved : 8; + }; + + Qiniu_Digest *sha1Digest; + } Qiniu_Qetag_Block; + + typedef struct _Qiniu_Qetag_Context + { + unsigned short int blkElementCount; + unsigned short int blkUnused; + unsigned short int blkBegin; + unsigned short int blkEnd; + + unsigned int blkCount; + Qiniu_Qetag_Block *blk; + + Qiniu_Digest *sha1Digest; + Qiniu_Qetag_Block blkArray[1]; + } Qiniu_Qetag_Context; + + static Qiniu_Error Qiniu_Qetag_mergeBlocks(struct _Qiniu_Qetag_Context *ctx) + { + Qiniu_Error err; + unsigned char digest[SHA_DIGEST_LENGTH]; + struct _Qiniu_Qetag_Block *blk = NULL; + + while (ctx->blkUnused < ctx->blkElementCount && ctx->blkArray[ctx->blkBegin].done == YES) + { + blk = &ctx->blkArray[ctx->blkBegin]; + + if (Qiniu_Digest_Final(blk->sha1Digest, digest, NULL) != QINIU_CRYPTO_RESULT_OK) + { + err.code = 9999; + err.message = "openssl internal error"; + return err; + } - Qiniu_Digest *sha1Digest; -} Qiniu_Qetag_Block; + if (Qiniu_Digest_Update(ctx->sha1Digest, digest, sizeof(digest)) != QINIU_CRYPTO_RESULT_OK) + { + err.code = 9999; + err.message = "openssl internal error"; + return err; + } -typedef struct _Qiniu_Qetag_Context -{ - unsigned short int blkElementCount; - unsigned short int blkUnused; - unsigned short int blkBegin; - unsigned short int blkEnd; - - unsigned int blkCount; - Qiniu_Qetag_Block * blk; - - Qiniu_Digest *sha1Digest; - Qiniu_Qetag_Block blkArray[1]; -} Qiniu_Qetag_Context; - -static Qiniu_Error Qiniu_Qetag_mergeBlocks(struct _Qiniu_Qetag_Context * ctx) { - Qiniu_Error err; - unsigned char digest[SHA_DIGEST_LENGTH]; - struct _Qiniu_Qetag_Block * blk = NULL; - - while (ctx->blkUnused < ctx->blkElementCount && ctx->blkArray[ctx->blkBegin].done == YES) { - blk = &ctx->blkArray[ctx->blkBegin]; - - if (Qiniu_Digest_Final(blk->sha1Digest, digest, NULL) != QINIU_CRYPTO_RESULT_OK) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - - if (Qiniu_Digest_Update(ctx->sha1Digest, digest, sizeof(digest)) != QINIU_CRYPTO_RESULT_OK) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - - blk->done = NO; - - ctx->blkBegin += 1; - if (ctx->blkBegin >= ctx->blkElementCount) { - ctx->blkBegin = 0; - } - ctx->blkUnused += 1; - } // while + blk->done = NO; - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_mergeBlocks + ctx->blkBegin += 1; + if (ctx->blkBegin >= ctx->blkElementCount) + { + ctx->blkBegin = 0; + } + ctx->blkUnused += 1; + } // while -static Qiniu_Error Qiniu_Qetag_allocateBlock(struct _Qiniu_Qetag_Context * ctx, struct _Qiniu_Qetag_Block ** blk) -{ - Qiniu_Error err; - struct _Qiniu_Qetag_Block * newBlk = NULL; + err.code = Qiniu_OK.code; + err.message = "ok"; + return err; + } // Qiniu_Qetag_mergeBlocks + + static Qiniu_Error Qiniu_Qetag_allocateBlock(struct _Qiniu_Qetag_Context *ctx, struct _Qiniu_Qetag_Block **blk) + { + Qiniu_Error err; + struct _Qiniu_Qetag_Block *newBlk = NULL; + + if (ctx->blkUnused == 0) + { + err = Qiniu_Qetag_mergeBlocks(ctx); + if (err.code != Qiniu_OK.code) + { + return err; + } - if (ctx->blkUnused == 0) { - err = Qiniu_Qetag_mergeBlocks(ctx); - if (err.code != 200) { + if (ctx->blkUnused == 0) + { + *blk = NULL; + err.code = 9991; + err.message = "no enough blocks"; + return err; + } // if + } // if + + newBlk = &ctx->blkArray[ctx->blkEnd]; + newBlk->sha1Digest = Qiniu_Digest_New(QINIU_DIGEST_TYPE_SHA1); + if (newBlk->sha1Digest == NULL) + { + err.code = 9999; + err.message = "openssl internal error"; return err; } - if (ctx->blkUnused == 0) { - *blk = NULL; - err.code = 9991; - err.message = "no enough blocks"; - return err; - } // if - } // if - - newBlk = &ctx->blkArray[ctx->blkEnd]; - newBlk->sha1Digest = Qiniu_Digest_New(QINIU_DIGEST_TYPE_SHA1); - if (newBlk->sha1Digest == NULL) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - - newBlk->done = NO; - newBlk->capacity = BLOCK_MAX_SIZE; - - ctx->blkUnused -= 1; - ctx->blkEnd += 1; - if (ctx->blkEnd >= ctx->blkElementCount) { - ctx->blkEnd = 0; - } + newBlk->done = NO; + newBlk->capacity = BLOCK_MAX_SIZE; - *blk = newBlk; - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_allocateBlock + ctx->blkUnused -= 1; + ctx->blkEnd += 1; + if (ctx->blkEnd >= ctx->blkElementCount) + { + ctx->blkEnd = 0; + } -Qiniu_Error Qiniu_Qetag_New(struct _Qiniu_Qetag_Context ** ctx, unsigned int concurrency) -{ - Qiniu_Error err; - Qiniu_Qetag_Context * newCtx = NULL; - - // 分配主结构体 - if (concurrency < 1) { - concurrency = 1; - } else if (concurrency > BLOCK_ELEMENT_MAX_COUNT) { - concurrency = BLOCK_ELEMENT_MAX_COUNT; - } // if - - newCtx = calloc(1, sizeof(*newCtx) + sizeof(newCtx->blkArray[0]) * (concurrency - 1)); - if (newCtx == NULL) { - err.code = 9999; - err.message = "not enough memory"; + *blk = newBlk; + err.code = Qiniu_OK.code; + err.message = "ok"; return err; - } - newCtx->blkElementCount = concurrency; + } // Qiniu_Qetag_allocateBlock - err = Qiniu_Qetag_Reset(newCtx); - if (err.code != 200) { - Qiniu_Qetag_Destroy(newCtx); - return err; - } + Qiniu_Error Qiniu_Qetag_New(struct _Qiniu_Qetag_Context **ctx, unsigned int concurrency) + { + Qiniu_Error err; + Qiniu_Qetag_Context *newCtx = NULL; - *ctx = newCtx; - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_New + // 分配主结构体 + if (concurrency < 1) + { + concurrency = 1; + } + else if (concurrency > BLOCK_ELEMENT_MAX_COUNT) + { + concurrency = BLOCK_ELEMENT_MAX_COUNT; + } // if -Qiniu_Error Qiniu_Qetag_Reset(struct _Qiniu_Qetag_Context * ctx) -{ - Qiniu_Error err; - - ctx->sha1Digest = Qiniu_Digest_New(QINIU_DIGEST_TYPE_SHA1); - if (ctx->sha1Digest == NULL) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - - ctx->blkCount = 0; - ctx->blkUnused = ctx->blkElementCount; - ctx->blkBegin = 0; - ctx->blkEnd = 0; - ctx->blk = NULL; - - err = Qiniu_Qetag_allocateBlock(ctx, &ctx->blk); - if (err.code != 200) { + newCtx = calloc(1, sizeof(*newCtx) + sizeof(newCtx->blkArray[0]) * (concurrency - 1)); + if (newCtx == NULL) + { + err.code = 9999; + err.message = "not enough memory"; + return err; + } + newCtx->blkElementCount = concurrency; + + err = Qiniu_Qetag_Reset(newCtx); + if (err.code != Qiniu_OK.code) + { + Qiniu_Qetag_Destroy(newCtx); + return err; + } + + *ctx = newCtx; + err.code = Qiniu_OK.code; + err.message = "ok"; return err; - } + } // Qiniu_Qetag_New - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_Reset + Qiniu_Error Qiniu_Qetag_Reset(struct _Qiniu_Qetag_Context *ctx) + { + Qiniu_Error err; -void Qiniu_Qetag_Destroy(struct _Qiniu_Qetag_Context * ctx) -{ - if (ctx) { - free(ctx); - } // if -} // Qiniu_Qetag_Destroy + ctx->sha1Digest = Qiniu_Digest_New(QINIU_DIGEST_TYPE_SHA1); + if (ctx->sha1Digest == NULL) + { + err.code = 9999; + err.message = "openssl internal error"; + return err; + } -Qiniu_Error Qiniu_Qetag_Update(struct _Qiniu_Qetag_Context * ctx, const char * buf, size_t bufSize) -{ - Qiniu_Error err; - const char * pos = buf; - size_t size = bufSize; - size_t copySize = 0; - - while (size > 0) { - if (!ctx->blk) { - err = Qiniu_Qetag_allocateBlock(ctx, &ctx->blk); - if (err.code != 200) { - return err; - } - } // if + ctx->blkCount = 0; + ctx->blkUnused = ctx->blkElementCount; + ctx->blkBegin = 0; + ctx->blkEnd = 0; + ctx->blk = NULL; - copySize = (size >= ctx->blk->capacity) ? ctx->blk->capacity : size; - if (copySize) { - err = Qiniu_Qetag_UpdateBlock(ctx->blk, pos, copySize, NULL); - if (err.code != 200) { - return err; - } + err = Qiniu_Qetag_allocateBlock(ctx, &ctx->blk); + if (err.code != Qiniu_OK.code) + { + return err; + } - pos += copySize; - size -= copySize; + err.code = Qiniu_OK.code; + err.message = "ok"; + return err; + } // Qiniu_Qetag_Reset + + void Qiniu_Qetag_Destroy(struct _Qiniu_Qetag_Context *ctx) + { + if (ctx) + { + free(ctx); } // if + } // Qiniu_Qetag_Destroy + + Qiniu_Error Qiniu_Qetag_Update(struct _Qiniu_Qetag_Context *ctx, const char *buf, size_t bufSize) + { + Qiniu_Error err; + const char *pos = buf; + size_t size = bufSize; + size_t copySize = 0; + + while (size > 0) + { + if (!ctx->blk) + { + err = Qiniu_Qetag_allocateBlock(ctx, &ctx->blk); + if (err.code != Qiniu_OK.code) + { + return err; + } + } // if + + copySize = (size >= ctx->blk->capacity) ? ctx->blk->capacity : size; + if (copySize) + { + err = Qiniu_Qetag_UpdateBlock(ctx->blk, pos, copySize, NULL); + if (err.code != Qiniu_OK.code) + { + return err; + } + + pos += copySize; + size -= copySize; + } // if + + if (ctx->blk->capacity == 0) + { + Qiniu_Qetag_CommitBlock(ctx, ctx->blk); + ctx->blk = NULL; + } // if + } // while + + err.code = Qiniu_OK.code; + err.message = "ok"; + return err; + } // Qiniu_Qetag_Update - if (ctx->blk->capacity == 0) { + Qiniu_Error Qiniu_Qetag_Final(struct _Qiniu_Qetag_Context *ctx, char **digest) + { + Qiniu_Error err; + unsigned char digestSummary[4 + SHA_DIGEST_LENGTH]; + char *newDigest = NULL; + + if (ctx->blk) + { Qiniu_Qetag_CommitBlock(ctx, ctx->blk); ctx->blk = NULL; } // if - } // while - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_Update + if (ctx->blkCount <= 1) + { + digestSummary[0] = 0x16; + if (Qiniu_Digest_Final(ctx->sha1Digest, &digestSummary[1], NULL) != QINIU_CRYPTO_RESULT_OK) + { + err.code = 9999; + err.message = "openssl internal error"; + return err; + } + } + else + { + err = Qiniu_Qetag_mergeBlocks(ctx); + if (err.code != Qiniu_OK.code) + { + return err; + } -Qiniu_Error Qiniu_Qetag_Final(struct _Qiniu_Qetag_Context * ctx, char ** digest) -{ - Qiniu_Error err; - unsigned char digestSummary[4 + SHA_DIGEST_LENGTH]; - char * newDigest = NULL; + digestSummary[0] = 0x96; + if (Qiniu_Digest_Final(ctx->sha1Digest, &digestSummary[1], NULL) != QINIU_CRYPTO_RESULT_OK) + { + err.code = 9999; + err.message = "openssl internal error"; + return err; + } + } // if - if (ctx->blk) { - Qiniu_Qetag_CommitBlock(ctx, ctx->blk); - ctx->blk = NULL; - } // if - - if (ctx->blkCount <= 1) { - digestSummary[0] = 0x16; - if (Qiniu_Digest_Final(ctx->sha1Digest, &digestSummary[1], NULL) != QINIU_CRYPTO_RESULT_OK) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - } else { - err = Qiniu_Qetag_mergeBlocks(ctx); - if (err.code != 200) { + newDigest = Qiniu_Memory_Encode((const char *)digestSummary, 1 + SHA_DIGEST_LENGTH); + if (newDigest == NULL) + { + err.code = 9999; + err.message = "no enough memory"; return err; } - digestSummary[0] = 0x96; - if (Qiniu_Digest_Final(ctx->sha1Digest, &digestSummary[1], NULL) != QINIU_CRYPTO_RESULT_OK) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - } // if - - newDigest = Qiniu_Memory_Encode((const char *)digestSummary, 1 + SHA_DIGEST_LENGTH); - if (newDigest == NULL) { - err.code = 9999; - err.message = "no enough memory"; + *digest = newDigest; + err.code = Qiniu_OK.code; + err.message = "ok"; return err; - } + } // Qiniu_Qetag_Final - *digest = newDigest; - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_Final + Qiniu_Error Qiniu_Qetag_AllocateBlock(struct _Qiniu_Qetag_Context *ctx, struct _Qiniu_Qetag_Block **blk, size_t *remainderSize) + { + Qiniu_Error err; -Qiniu_Error Qiniu_Qetag_AllocateBlock(struct _Qiniu_Qetag_Context * ctx, struct _Qiniu_Qetag_Block ** blk, size_t * remainderSize) -{ - Qiniu_Error err; - - if (ctx->blk) { - *blk = ctx->blk; - ctx->blk = NULL; - } else { - err = Qiniu_Qetag_allocateBlock(ctx, blk); - if (err.code != 200) { - return err; + if (ctx->blk) + { + *blk = ctx->blk; + ctx->blk = NULL; } - } // if - - if (remainderSize) { - *remainderSize = (*blk)->capacity; - } - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_AllocateBlock + else + { + err = Qiniu_Qetag_allocateBlock(ctx, blk); + if (err.code != Qiniu_OK.code) + { + return err; + } + } // if -Qiniu_Error Qiniu_Qetag_UpdateBlock(struct _Qiniu_Qetag_Block * blk, const char * buf, size_t bufSize, size_t * remainderSize) -{ - Qiniu_Error err; - - if (bufSize > 0) { - if (Qiniu_Digest_Update(blk->sha1Digest, buf, (int) bufSize) != QINIU_CRYPTO_RESULT_OK) { - err.code = 9999; - err.message = "openssl internal error"; - return err; - } - blk->capacity -= bufSize; - } // if - - if (remainderSize) { - *remainderSize = blk->capacity; - } - err.code = 200; - err.message = "ok"; - return err; -} // Qiniu_Qetag_UpdateBlock - -void Qiniu_Qetag_CommitBlock(struct _Qiniu_Qetag_Context * ctx, struct _Qiniu_Qetag_Block * blk) -{ - if (blk->capacity < BLOCK_MAX_SIZE) { - blk->done = YES; - ctx->blkCount += 1; - } else { - blk->done = NO; - } -} // Qiniu_Qetag_CommitBlock - -Qiniu_Error Qiniu_Qetag_DigestFile(const char * localFile, char ** digest) -{ - Qiniu_Error err; - Qiniu_Off_T offset = 0; - size_t readingBytes = (BLOCK_MAX_SIZE >> 2); - ssize_t readBytes = 0; - Qiniu_File * f = NULL; - struct _Qiniu_Qetag_Context * ctx = NULL; - char * buf = NULL; - - // 1MB buffer - buf = malloc(readingBytes); - if (!buf) { - err.code = 9999; - err.message = "no enough memory"; + if (remainderSize) + { + *remainderSize = (*blk)->capacity; + } + err.code = Qiniu_OK.code; + err.message = "ok"; return err; - } - - err = Qiniu_Qetag_New(&ctx, 1); - if (err.code != 200) { - goto DIGESTFILE_NEWCTX_ERROR; - } - - err = Qiniu_File_Open(&f, localFile); - if (err.code != 200) { - goto DIGESTFILE_OPEN_ERROR; - } - - do { - readBytes = Qiniu_File_ReadAt(f, buf, readingBytes, offset); - if (readBytes < 0) { - err.code = 9990; - err.message = "failed in reading file"; - goto DIGESTFILE_UPDATE_ERROR; - } else if (readBytes > 0) { - err = Qiniu_Qetag_Update(ctx, buf, readBytes); - if (err.code != 200) { - goto DIGESTFILE_UPDATE_ERROR; + } // Qiniu_Qetag_AllocateBlock + + Qiniu_Error Qiniu_Qetag_UpdateBlock(struct _Qiniu_Qetag_Block *blk, const char *buf, size_t bufSize, size_t *remainderSize) + { + Qiniu_Error err; + + if (bufSize > 0) + { + if (Qiniu_Digest_Update(blk->sha1Digest, buf, (int)bufSize) != QINIU_CRYPTO_RESULT_OK) + { + err.code = 9999; + err.message = "openssl internal error"; + return err; } - offset += readBytes; + blk->capacity -= bufSize; } // if - } while(readBytes > 0); - - err = Qiniu_Qetag_Final(ctx, digest); -DIGESTFILE_UPDATE_ERROR: - Qiniu_File_Close(f); + if (remainderSize) + { + *remainderSize = blk->capacity; + } + err.code = Qiniu_OK.code; + err.message = "ok"; + return err; + } // Qiniu_Qetag_UpdateBlock + + void Qiniu_Qetag_CommitBlock(struct _Qiniu_Qetag_Context *ctx, struct _Qiniu_Qetag_Block *blk) + { + if (blk->capacity < BLOCK_MAX_SIZE) + { + blk->done = YES; + ctx->blkCount += 1; + } + else + { + blk->done = NO; + } + } // Qiniu_Qetag_CommitBlock + + Qiniu_Error Qiniu_Qetag_DigestFile(const char *localFile, char **digest) + { + Qiniu_Error err; + Qiniu_Off_T offset = 0; + size_t readingBytes = (BLOCK_MAX_SIZE >> 2); + ssize_t readBytes = 0; + Qiniu_File *f = NULL; + struct _Qiniu_Qetag_Context *ctx = NULL; + char *buf = NULL; + + // 1MB buffer + buf = malloc(readingBytes); + if (!buf) + { + err.code = 9999; + err.message = "no enough memory"; + return err; + } -DIGESTFILE_OPEN_ERROR: - Qiniu_Qetag_Destroy(ctx); + err = Qiniu_Qetag_New(&ctx, 1); + if (err.code != Qiniu_OK.code) + { + goto DIGESTFILE_NEWCTX_ERROR; + } -DIGESTFILE_NEWCTX_ERROR: - free(buf); + err = Qiniu_File_Open(&f, localFile); + if (err.code != Qiniu_OK.code) + { + goto DIGESTFILE_OPEN_ERROR; + } - return err; -} // Qiniu_Qetag_DigestFile + do + { + readBytes = Qiniu_File_ReadAt(f, buf, readingBytes, offset); + if (readBytes < 0) + { + err.code = 9990; + err.message = "failed in reading file"; + goto DIGESTFILE_UPDATE_ERROR; + } + else if (readBytes > 0) + { + err = Qiniu_Qetag_Update(ctx, buf, readBytes); + if (err.code != Qiniu_OK.code) + { + goto DIGESTFILE_UPDATE_ERROR; + } + offset += readBytes; + } // if + } while (readBytes > 0); + + err = Qiniu_Qetag_Final(ctx, digest); + + DIGESTFILE_UPDATE_ERROR: + Qiniu_File_Close(f); + + DIGESTFILE_OPEN_ERROR: + Qiniu_Qetag_Destroy(ctx); -Qiniu_Error Qiniu_Qetag_DigestBuffer(const char * buf, size_t bufSize, char ** digest) -{ - Qiniu_Error err; - struct _Qiniu_Qetag_Context * ctx = NULL; + DIGESTFILE_NEWCTX_ERROR: + free(buf); - err = Qiniu_Qetag_New(&ctx, bufSize); - if (err.code != 200) { return err; - } + } // Qiniu_Qetag_DigestFile - err = Qiniu_Qetag_Update(ctx, buf, bufSize); - if (err.code != 200) { + Qiniu_Error Qiniu_Qetag_DigestBuffer(const char *buf, size_t bufSize, char **digest) + { + Qiniu_Error err; + struct _Qiniu_Qetag_Context *ctx = NULL; + + err = Qiniu_Qetag_New(&ctx, bufSize); + if (err.code != Qiniu_OK.code) + { + return err; + } + + err = Qiniu_Qetag_Update(ctx, buf, bufSize); + if (err.code != Qiniu_OK.code) + { + Qiniu_Qetag_Destroy(ctx); + return err; + } + + err = Qiniu_Qetag_Final(ctx, digest); Qiniu_Qetag_Destroy(ctx); return err; - } - - err = Qiniu_Qetag_Final(ctx, digest); - Qiniu_Qetag_Destroy(ctx); - return err; -} // Qiniu_Qetag_DigestBuffer + } // Qiniu_Qetag_DigestBuffer #pragma pack() diff --git a/qiniu/reader.c b/qiniu/reader.c index bcaa8cd5..11af4b46 100644 --- a/qiniu/reader.c +++ b/qiniu/reader.c @@ -2,11 +2,12 @@ #include "reader.h" -QINIU_DLLAPI Qiniu_Error Qiniu_Rd_Reader_Open(Qiniu_Rd_Reader * rdr, const char * localFileName) +QINIU_DLLAPI Qiniu_Error Qiniu_Rd_Reader_Open(Qiniu_Rd_Reader *rdr, const char *localFileName) { Qiniu_Error err; err = Qiniu_File_Open(&rdr->file, localFileName); - if (err.code != 200) { + if (err.code != Qiniu_OK.code) + { return err; } // if @@ -16,23 +17,25 @@ QINIU_DLLAPI Qiniu_Error Qiniu_Rd_Reader_Open(Qiniu_Rd_Reader * rdr, const char return Qiniu_OK; } -QINIU_DLLAPI void Qiniu_Rd_Reader_Close(Qiniu_Rd_Reader * rdr) +QINIU_DLLAPI void Qiniu_Rd_Reader_Close(Qiniu_Rd_Reader *rdr) { Qiniu_File_Close(rdr->file); } -QINIU_DLLAPI size_t Qiniu_Rd_Reader_Callback(char * buffer, size_t size, size_t nitems, void * userData) +QINIU_DLLAPI size_t Qiniu_Rd_Reader_Callback(char *buffer, size_t size, size_t nitems, void *userData) { ssize_t ret; - Qiniu_Rd_Reader * rdr = (Qiniu_Rd_Reader *)userData; + Qiniu_Rd_Reader *rdr = (Qiniu_Rd_Reader *)userData; ret = Qiniu_File_ReadAt(rdr->file, buffer, size * nitems, rdr->offset); - if (ret < 0) { + if (ret < 0) + { rdr->status = QINIU_RD_ABORT_BY_READAT; return CURL_READFUNC_ABORT; } // if - if (rdr->abortCallback && rdr->abortCallback(rdr->abortUserData, buffer, ret)) { + if (rdr->abortCallback && rdr->abortCallback(rdr->abortUserData, buffer, ret)) + { rdr->status = QINIU_RD_ABORT_BY_CALLBACK; return CURL_READFUNC_ABORT; } // if diff --git a/qiniu/recorder.c b/qiniu/recorder.c index 3afc31dc..e7128d02 100644 --- a/qiniu/recorder.c +++ b/qiniu/recorder.c @@ -112,7 +112,7 @@ Qiniu_Error _Qiniu_FileSystem_RecordMedium_Read_Entry(const struct Qiniu_Record_ if (data->buf == NULL || data->bufOffset >= data->bufSize) { Qiniu_Error err = _Qiniu_FileSystem_RecordMedium_fulfill(medium); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -163,7 +163,7 @@ Qiniu_Error _Qiniu_FileSystem_RecordMedium_Has_Next_Entry(const struct Qiniu_Rec if (data->buf == NULL || data->bufOffset >= data->bufSize) { Qiniu_Error err = _Qiniu_FileSystem_RecordMedium_fulfill(medium); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } diff --git a/qiniu/recorder_utils.c b/qiniu/recorder_utils.c index 7443b462..f0e0475e 100644 --- a/qiniu/recorder_utils.c +++ b/qiniu/recorder_utils.c @@ -136,7 +136,7 @@ Qiniu_Error Qiniu_Utils_Find_Medium(Qiniu_Recorder *recorder, const char *record size_t haveRead; char buf[BUFFER_SIZE]; Qiniu_Error err = recorder->open(recorder, recorderKey, "rb+", medium); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { if (err.code == -ENOENT) { @@ -149,7 +149,7 @@ Qiniu_Error Qiniu_Utils_Find_Medium(Qiniu_Recorder *recorder, const char *record return err; } err = medium->readEntry(medium, buf, BUFFER_SIZE, &haveRead); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { medium->close(medium); return err; @@ -189,7 +189,7 @@ Qiniu_Error Qiniu_Utils_Find_Medium(Qiniu_Recorder *recorder, const char *record Qiniu_Error Qiniu_Utils_New_Medium(Qiniu_Recorder *recorder, const char *recorderKey, Qiniu_Int64 version, struct Qiniu_Record_Medium *medium, Qiniu_FileInfo *fileInfo) { Qiniu_Error err = recorder->open(recorder, recorderKey, "wb", medium); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -207,7 +207,7 @@ Qiniu_Error Qiniu_Utils_New_Medium(Qiniu_Recorder *recorder, const char *recorde } err = medium->writeEntry(medium, metadataJson, NULL); free(metadataJson); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } diff --git a/qiniu/region.c b/qiniu/region.c index 821a69b5..94bd93f9 100644 --- a/qiniu/region.c +++ b/qiniu/region.c @@ -11,6 +11,7 @@ #include "region.h" #include "tm.h" #include "../cJSON/cJSON.h" +#include "../hashmap/hashmap.h" /*============================================================================*/ @@ -880,7 +881,7 @@ static Qiniu_Error _Qiniu_Region_Query_call(Qiniu_Client *self, const char *acce url = Qiniu_String_Concat(hosts[i], "/v4/query?ak=", accessKey, "&bucket=", bucketName, NULL); err = Qiniu_Client_Call(self, ret, url); Qiniu_Free(url); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { cJSON *hostsJson = Qiniu_Json_GetObjectItem(*ret, "hosts", NULL); if (hostsJson == NULL) @@ -918,7 +919,7 @@ _Qiniu_Region_Query(Qiniu_Client *self, Qiniu_Region **pRegion, const char *acce accessKey = self->auth.itbl->GetAccessKey(self->auth.self); } err = _Qiniu_Region_Query_call(self, accessKey, bucketName, &root); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { cJSON *hostsJson = Qiniu_Json_GetObjectItem(root, "hosts", NULL); if (hostsJson == NULL) @@ -1204,47 +1205,192 @@ Qiniu_Error Qiniu_Region_Query(Qiniu_Client *self, Qiniu_Region **pRegion, const return _Qiniu_Region_Query(self, pRegion, NULL, bucketName, useHttps); } +struct Qiniu_Region_Cache +{ + Qiniu_Region *region; + const char *accessKey, *bucketName, *ucHost, *ucHostBackup, *apiHost; +}; + +static int _Qiniu_Compare_Str(const char *a, const char *b) +{ + if (a == NULL && b == NULL) + { + return 0; + } + else if (a == NULL) + { + return -1; + } + else if (b == NULL) + { + return 1; + } + else + { + return strcmp(a, b); + } +} + +static int +_Qiniu_Region_Cache_Compare(const void *a, const void *b, void *user_data) +{ + int result; + struct Qiniu_Region_Cache *cacheA = (struct Qiniu_Region_Cache *)a; + struct Qiniu_Region_Cache *cacheB = (struct Qiniu_Region_Cache *)b; + result = _Qiniu_Compare_Str(cacheA->accessKey, cacheB->accessKey); + if (!result) + { + return result; + } + result = _Qiniu_Compare_Str(cacheA->bucketName, cacheB->bucketName); + if (!result) + { + return result; + } + result = _Qiniu_Compare_Str(cacheA->ucHost, cacheB->ucHost); + if (!result) + { + return result; + } + result = _Qiniu_Compare_Str(cacheA->ucHostBackup, cacheB->ucHostBackup); + if (!result) + { + return result; + } + result = _Qiniu_Compare_Str(cacheA->apiHost, cacheB->apiHost); + if (!result) + { + return result; + } +} + +static uint64_t _Qiniu_Region_Cache_Hash(const void *r, uint64_t seed0, uint64_t seed1) +{ + struct Qiniu_Region_Cache *cache = (struct Qiniu_Region_Cache *)r; + uint64_t hash = 0; + if (cache->accessKey != NULL) + { + hash ^= hashmap_sip(cache->accessKey, strlen(cache->accessKey), seed0, seed1); + } + if (cache->bucketName != NULL) + { + hash ^= hashmap_sip(cache->bucketName, strlen(cache->bucketName), seed0, seed1); + } + if (cache->ucHost != NULL) + { + hash ^= hashmap_sip(cache->ucHost, strlen(cache->ucHost), seed0, seed1); + } + if (cache->ucHostBackup != NULL) + { + hash ^= hashmap_sip(cache->ucHostBackup, strlen(cache->ucHostBackup), seed0, seed1); + } + if (cache->apiHost != NULL) + { + hash ^= hashmap_sip(cache->apiHost, strlen(cache->apiHost), seed0, seed1); + } + return hash; +} + +static void _Qiniu_Region_Cache_Free(void *r) +{ + struct Qiniu_Region_Cache *cache = (struct Qiniu_Region_Cache *)r; + Qiniu_Free((void *)cache->accessKey); + Qiniu_Free((void *)cache->bucketName); + Qiniu_Free((void *)cache->ucHost); + Qiniu_Free((void *)cache->ucHostBackup); + Qiniu_Free((void *)cache->apiHost); + Qiniu_Region_Free(cache->region); + Qiniu_Zero_Ptr(cache); +} + +static Qiniu_Error _Qiniu_Region_Auto_Query_With_Cache(Qiniu_Client *self, const char *accessKey, const char *bucketName, Qiniu_Region **foundRegion) +{ + if (accessKey == NULL) + { + accessKey = QINIU_ACCESS_KEY; + } + const struct Qiniu_Region_Cache cacheKey = { + .accessKey = accessKey, + .bucketName = bucketName, + .ucHost = QINIU_UC_HOST, + .ucHostBackup = QINIU_UC_HOST_BACKUP, + .apiHost = QINIU_API_HOST, + }; + struct Qiniu_Region_Cache *cache = NULL; + if (self->cachedRegions != NULL) + { + cache = (struct Qiniu_Region_Cache *)hashmap_get(self->cachedRegions, &cacheKey); + if (cache != NULL && !Qiniu_Region_Is_Expired(cache->region)) + { + *foundRegion = cache->region; + return Qiniu_OK; + } + } + Qiniu_Error err = _Qiniu_Region_Query(self, foundRegion, accessKey, bucketName, self->autoQueryHttpsRegion); + if (err.code != Qiniu_OK.code) + { + if (cache != NULL) + { // 有已经过期的缓存区域可以使用 + *foundRegion = cache->region; + return Qiniu_OK; + } + return err; + } + if (self->cachedRegions == NULL) + { + self->cachedRegions = hashmap_new(sizeof(struct Qiniu_Region_Cache), 0, rand(), rand(), _Qiniu_Region_Cache_Hash, _Qiniu_Region_Cache_Compare, _Qiniu_Region_Cache_Free, NULL); + } + if (self->cachedRegions != NULL) + { + if (cache != NULL) + { // 复用前面已经过期的缓存的内存 + Qiniu_Region_Free(cache->region); + cache->region = *foundRegion; + } + else + { + struct Qiniu_Region_Cache *newCache = malloc(sizeof(struct Qiniu_Region_Cache)); + if (newCache != NULL) + { + *newCache = (struct Qiniu_Region_Cache){ + .region = *foundRegion, + .accessKey = Qiniu_String_Dup(accessKey), + .bucketName = Qiniu_String_Dup(bucketName), + .ucHost = Qiniu_String_Dup(QINIU_UC_HOST), + .ucHostBackup = Qiniu_String_Dup(QINIU_UC_HOST_BACKUP), + .apiHost = Qiniu_String_Dup(QINIU_API_HOST), + }; + hashmap_set(self->cachedRegions, newCache); + } + } + } + return Qiniu_OK; +} + Qiniu_Error _Qiniu_Region_Get_Up_Host(Qiniu_Client *self, const char *accessKey, const char *bucketName, const char **host) { const char *const *hosts; size_t count; Qiniu_Error err = Qiniu_OK; - Qiniu_Region **foundRegion = &self->cachedRegion; + Qiniu_Region *foundRegion = NULL; if (self->specifiedRegion) { - foundRegion = &self->specifiedRegion; + foundRegion = self->specifiedRegion; goto foundCache; } else if (self->autoQueryRegion) { - if (self->cachedRegion != NULL) + err = _Qiniu_Region_Auto_Query_With_Cache(self, accessKey, bucketName, &foundRegion); + if (err.code == Qiniu_OK.code) { - if (strcmp(self->cachedRegionBucketName, bucketName) == 0) - { - goto foundCache; - } - else - { - Qiniu_Region_Free(self->cachedRegion); - self->cachedRegion = NULL; - Qiniu_FreeV2((void **)&self->cachedRegionBucketName); - } + goto foundCache; } - err = _Qiniu_Region_Query(self, &self->cachedRegion, accessKey, bucketName, self->autoQueryHttpsRegion); - if (err.code != 200) - { - goto useDefault; - } - self->cachedRegionBucketName = Qiniu_String_Dup(bucketName); - goto foundCache; + *host = QINIU_UP_HOST; + return err; } -useDefault: - *host = QINIU_UP_HOST; - return err; - foundCache: - hosts = Qiniu_Region_Get_Up_Preferred_Hosts(*foundRegion, &count); + hosts = Qiniu_Region_Get_Up_Preferred_Hosts(foundRegion, &count); if (count == 0) { *host = QINIU_UP_HOST; @@ -1261,42 +1407,25 @@ Qiniu_Error _Qiniu_Region_Get_Io_Host(Qiniu_Client *self, const char *accessKey, const char *const *hosts; size_t count; Qiniu_Error err = Qiniu_OK; - Qiniu_Region **foundRegion = &self->cachedRegion; + Qiniu_Region *foundRegion = NULL; if (self->specifiedRegion) { - foundRegion = &self->specifiedRegion; + foundRegion = self->specifiedRegion; goto foundCache; } else if (self->autoQueryRegion) { - if (self->cachedRegion != NULL) - { - if (strcmp(self->cachedRegionBucketName, bucketName) == 0 && !Qiniu_Region_Is_Expired(self->cachedRegion)) - { - goto foundCache; - } - else - { - Qiniu_Region_Free(self->cachedRegion); - self->cachedRegion = NULL; - Qiniu_FreeV2((void **)&self->cachedRegionBucketName); - } - } - err = _Qiniu_Region_Query(self, &self->cachedRegion, accessKey, bucketName, self->autoQueryHttpsRegion); - if (err.code != 200) + err = _Qiniu_Region_Auto_Query_With_Cache(self, accessKey, bucketName, &foundRegion); + if (err.code == Qiniu_OK.code) { - goto useDefault; + goto foundCache; } - self->cachedRegionBucketName = Qiniu_String_Dup(bucketName); - goto foundCache; + *host = QINIU_UP_HOST; + return err; } -useDefault: - *host = QINIU_IOVIP_HOST; - return err; - foundCache: - hosts = Qiniu_Region_Get_Io_Preferred_Hosts(*foundRegion, &count); + hosts = Qiniu_Region_Get_Io_Preferred_Hosts(foundRegion, &count); if (count == 0) { *host = QINIU_IOVIP_HOST; @@ -1313,42 +1442,25 @@ Qiniu_Error _Qiniu_Region_Get_Rs_Host(Qiniu_Client *self, const char *accessKey, const char *const *hosts; size_t count; Qiniu_Error err = Qiniu_OK; - Qiniu_Region **foundRegion = &self->cachedRegion; + Qiniu_Region *foundRegion = NULL; if (self->specifiedRegion) { - foundRegion = &self->specifiedRegion; + foundRegion = self->specifiedRegion; goto foundCache; } else if (self->autoQueryRegion) { - if (self->cachedRegion != NULL) + err = _Qiniu_Region_Auto_Query_With_Cache(self, accessKey, bucketName, &foundRegion); + if (err.code == Qiniu_OK.code) { - if (strcmp(self->cachedRegionBucketName, bucketName) == 0) - { - goto foundCache; - } - else - { - Qiniu_Region_Free(self->cachedRegion); - self->cachedRegion = NULL; - Qiniu_FreeV2((void **)&self->cachedRegionBucketName); - } - } - err = _Qiniu_Region_Query(self, &self->cachedRegion, accessKey, bucketName, self->autoQueryHttpsRegion); - if (err.code != 200) - { - goto useDefault; + goto foundCache; } - self->cachedRegionBucketName = Qiniu_String_Dup(bucketName); - goto foundCache; + *host = QINIU_UP_HOST; + return err; } -useDefault: - *host = QINIU_RS_HOST; - return err; - foundCache: - hosts = Qiniu_Region_Get_Rs_Preferred_Hosts(*foundRegion, &count); + hosts = Qiniu_Region_Get_Rs_Preferred_Hosts(foundRegion, &count); if (count == 0) { *host = QINIU_RS_HOST; @@ -1365,42 +1477,25 @@ Qiniu_Error _Qiniu_Region_Get_Rsf_Host(Qiniu_Client *self, const char *accessKey const char *const *hosts; size_t count; Qiniu_Error err = Qiniu_OK; - Qiniu_Region **foundRegion = &self->cachedRegion; + Qiniu_Region *foundRegion = NULL; if (self->specifiedRegion) { - foundRegion = &self->specifiedRegion; + foundRegion = self->specifiedRegion; goto foundCache; } else if (self->autoQueryRegion) { - if (self->cachedRegion != NULL) - { - if (strcmp(self->cachedRegionBucketName, bucketName) == 0) - { - goto foundCache; - } - else - { - Qiniu_Region_Free(self->cachedRegion); - self->cachedRegion = NULL; - Qiniu_FreeV2((void **)&self->cachedRegionBucketName); - } - } - err = _Qiniu_Region_Query(self, &self->cachedRegion, accessKey, bucketName, self->autoQueryHttpsRegion); - if (err.code != 200) + err = _Qiniu_Region_Auto_Query_With_Cache(self, accessKey, bucketName, &foundRegion); + if (err.code == Qiniu_OK.code) { - goto useDefault; + goto foundCache; } - self->cachedRegionBucketName = Qiniu_String_Dup(bucketName); - goto foundCache; + *host = QINIU_UP_HOST; + return err; } -useDefault: - *host = QINIU_RSF_HOST; - return err; - foundCache: - hosts = Qiniu_Region_Get_Rsf_Preferred_Hosts(*foundRegion, &count); + hosts = Qiniu_Region_Get_Rsf_Preferred_Hosts(foundRegion, &count); if (count == 0) { *host = QINIU_RSF_HOST; @@ -1417,42 +1512,25 @@ Qiniu_Error _Qiniu_Region_Get_Api_Host(Qiniu_Client *self, const char *accessKey const char *const *hosts; size_t count; Qiniu_Error err = Qiniu_OK; - Qiniu_Region **foundRegion = &self->cachedRegion; + Qiniu_Region *foundRegion = NULL; if (self->specifiedRegion) { - foundRegion = &self->specifiedRegion; + foundRegion = self->specifiedRegion; goto foundCache; } else if (self->autoQueryRegion) { - if (self->cachedRegion != NULL) - { - if (strcmp(self->cachedRegionBucketName, bucketName) == 0) - { - goto foundCache; - } - else - { - Qiniu_Region_Free(self->cachedRegion); - self->cachedRegion = NULL; - Qiniu_FreeV2((void **)&self->cachedRegionBucketName); - } - } - err = _Qiniu_Region_Query(self, &self->cachedRegion, accessKey, bucketName, self->autoQueryHttpsRegion); - if (err.code != 200) + err = _Qiniu_Region_Auto_Query_With_Cache(self, accessKey, bucketName, &foundRegion); + if (err.code == Qiniu_OK.code) { - goto useDefault; + goto foundCache; } - self->cachedRegionBucketName = Qiniu_String_Dup(bucketName); - goto foundCache; + *host = QINIU_UP_HOST; + return err; } -useDefault: - *host = QINIU_API_HOST; - return err; - foundCache: - hosts = Qiniu_Region_Get_Api_Preferred_Hosts(*foundRegion, &count); + hosts = Qiniu_Region_Get_Api_Preferred_Hosts(foundRegion, &count); if (count == 0) { *host = QINIU_API_HOST; diff --git a/qiniu/resumable_io.c b/qiniu/resumable_io.c index e916c458..e988b1d5 100644 --- a/qiniu/resumable_io.c +++ b/qiniu/resumable_io.c @@ -362,7 +362,7 @@ static Qiniu_Error Qiniu_Rio_bput( Qiniu_Json *root; Qiniu_Error err = Qiniu_Client_CallWithBinary(self, &root, url, body, bodyLength, NULL); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { retFromResp.ctx = Qiniu_Json_GetString(root, "ctx", NULL); retFromResp.checksum = Qiniu_Json_GetString(root, "checksum", NULL); @@ -449,7 +449,7 @@ static Qiniu_Error Qiniu_Rio_ResumableBlockput( body = Qiniu_TeeReader(&tee, body1, h); err = Qiniu_Rio_Mkblock(c, ret, blkSize, body, bodyLength, extra); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -489,7 +489,7 @@ static Qiniu_Error Qiniu_Rio_ResumableBlockput( body = Qiniu_TeeReader(&tee, body1, h); err = Qiniu_Rio_Blockput(c, ret, body, bodyLength); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { httpCalled++; if (ret->crc32 == crc32.val) @@ -580,7 +580,7 @@ static Qiniu_Error Qiniu_Rio_Mkfile( Qiniu_Buffer_Cleanup(&url); Qiniu_Buffer_Cleanup(&body); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { ret->hash = Qiniu_Json_GetString(root, "hash", NULL); ret->key = Qiniu_Json_GetString(root, "key", NULL); @@ -605,7 +605,7 @@ static Qiniu_Error Qiniu_Rio_Recorder_Read_Medium(struct Qiniu_Record_Medium *me size_t haveRead; char buf[BUFFER_SIZE]; Qiniu_Error err = medium->readEntry(medium, buf, BUFFER_SIZE, &haveRead); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { medium->close(medium); return err; @@ -652,7 +652,7 @@ static Qiniu_Error Qiniu_Rio_Recorder_Write_Medium(struct Qiniu_Record_Medium *m } err = medium->writeEntry(medium, blockInfoJson, NULL); free(blockInfoJson); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -702,7 +702,7 @@ static void Qiniu_Rio_doTask(void *params) lzRetry: Qiniu_Rio_BlkputRet_Assign(&ret, &extra->progresses[blkIdx]); Qiniu_Error err = Qiniu_Rio_ResumableBlockput(c, &ret, task->f, blkIdx, task->blkSize1, extra, &chunksUploaded); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { if (err.code == Qiniu_Rio_PutInterrupted) { @@ -755,14 +755,14 @@ static Qiniu_Error Qiniu_Rio_loadProgresses(Qiniu_Rio_PutRet *ret, Qiniu_Rio_Put for (;;) { Qiniu_Error err = recorder->recorderMedium->hasNextEntry(recorder->recorderMedium, &hasNext); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } if (hasNext) { err = Qiniu_Rio_Recorder_Read_Medium(recorder->recorderMedium, &blkIdx, &blkPutRet); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -808,7 +808,7 @@ static Qiniu_Error _Qiniu_Rio_Put( int retCode; Qiniu_Count ninterrupts; Qiniu_Error err = Qiniu_Rio_PutExtra_Init(&extra, fsize, extra1); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -821,7 +821,7 @@ static Qiniu_Error _Qiniu_Rio_Put( return err; } err = _Qiniu_Region_Get_Up_Host(self, accessKey, bucketName, &extra.upHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { Qiniu_Free((void *)accessKey); Qiniu_Free((void *)bucketName); @@ -929,12 +929,12 @@ Qiniu_Error Qiniu_Rio_PutFile( Qiniu_Rio_Recorder recorder = {NULL, NULL, Qiniu_False}, *pRecorder = NULL; Qiniu_Record_Medium medium; Qiniu_Error err = Qiniu_File_Open(&f, localFile); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } err = Qiniu_File_Stat(f, &fi); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { fsize = Qiniu_FileInfo_Fsize(fi); if (fsize <= Qiniu_Rio_PutExtra_ChunkSize(extra)) @@ -947,7 +947,7 @@ Qiniu_Error Qiniu_Rio_PutFile( return Qiniu_Io_PutFile(self, ret, uptoken, key, localFile, &extra1); } err = initializeRecorder(uptoken, key, localFile, &fi, extra, &medium, &recorder); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -971,12 +971,12 @@ Qiniu_Error initializeRecorder(const char *uptoken, const char *key, const char if (extra->recorder != NULL) { err = Qiniu_Utils_Generate_RecorderKey(uptoken, "v1", key, localFile, &recorder->recorderKey); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } err = Qiniu_Utils_Find_Medium(extra->recorder, recorder->recorderKey, 1, medium, fi, &ok); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -988,7 +988,7 @@ Qiniu_Error initializeRecorder(const char *uptoken, const char *key, const char else { err = Qiniu_Utils_New_Medium(extra->recorder, recorder->recorderKey, 1, medium, fi); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -1004,7 +1004,7 @@ Qiniu_Error reinitializeRecorder(Qiniu_Rio_PutExtra *extra, Qiniu_FileInfo *fi, Qiniu_Error err; recorder->recorderMedium->close(recorder->recorderMedium); err = Qiniu_Utils_New_Medium(extra->recorder, recorder->recorderKey, 1, recorder->recorderMedium, fi); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } diff --git a/qiniu/rs.c b/qiniu/rs.c index a6da08c9..3be5e91b 100644 --- a/qiniu/rs.c +++ b/qiniu/rs.c @@ -197,7 +197,7 @@ Qiniu_Error Qiniu_RS_Stat( const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -212,7 +212,7 @@ Qiniu_Error Qiniu_RS_Stat( err = Qiniu_Client_Call(self, &root, url); Qiniu_Free(url); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { ret->hash = Qiniu_Json_GetString(root, "hash", 0); ret->mimeType = Qiniu_Json_GetString(root, "mimeType", 0); @@ -233,7 +233,7 @@ Qiniu_Error Qiniu_RS_Delete(Qiniu_Client *self, const char *bucket, const char * const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -261,7 +261,7 @@ Qiniu_Error Qiniu_RS_Copy(Qiniu_Client *self, const char *srcBucket, const char const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, srcBucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -304,7 +304,7 @@ Qiniu_Error Qiniu_RS_Move(Qiniu_Client *self, const char *srcBucket, const char const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, srcBucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -346,7 +346,7 @@ Qiniu_Error Qiniu_RS_ChangeMime(Qiniu_Client *self, const char *bucket, const ch const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -374,7 +374,7 @@ Qiniu_Error Qiniu_RS_ChangeType(Qiniu_Client *self, const char *bucket, const ch const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -424,7 +424,7 @@ Qiniu_Error Qiniu_RS_RestoreArchive(Qiniu_Client *self, const char *bucket, cons const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -454,7 +454,7 @@ Qiniu_Error Qiniu_RS_DeleteAfterDays(Qiniu_Client *self, const char *bucket, con const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -485,7 +485,7 @@ Qiniu_Error Qiniu_RS_Fetch(Qiniu_Client *self, Qiniu_RS_FetchRet *ret, const cha const char *ioHost; err = _Qiniu_Region_Get_Io_Host(self, NULL, bucket, &ioHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -512,7 +512,7 @@ Qiniu_Error Qiniu_RS_Fetch(Qiniu_Client *self, Qiniu_RS_FetchRet *ret, const cha err = Qiniu_Client_Call(self, &root, url); Qiniu_Free(url); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { ret->key = Qiniu_Json_GetString(root, "key", 0); ret->hash = Qiniu_Json_GetString(root, "hash", 0); @@ -531,7 +531,7 @@ Qiniu_Error Qiniu_RS_Prefetch(Qiniu_Client *self, const char *bucket, const char const char *ioHost; err = _Qiniu_Region_Get_Io_Host(self, NULL, bucket, &ioHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -567,7 +567,7 @@ Qiniu_Error Qiniu_RS_BatchStat( const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entry->bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -613,7 +613,7 @@ Qiniu_Error Qiniu_RS_BatchStat( rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -649,7 +649,7 @@ Qiniu_Error Qiniu_RS_BatchDelete( const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entry->bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -696,7 +696,7 @@ Qiniu_Error Qiniu_RS_BatchDelete( rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -725,7 +725,7 @@ Qiniu_Error Qiniu_RS_BatchMove( const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entryPair->src.bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -785,7 +785,7 @@ Qiniu_Error Qiniu_RS_BatchMove( rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -814,7 +814,7 @@ Qiniu_Error Qiniu_RS_BatchCopy( const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entryPair->src.bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -874,7 +874,7 @@ Qiniu_Error Qiniu_RS_BatchCopy( rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -899,7 +899,7 @@ Qiniu_Error Qiniu_RS_BatchChangeType(Qiniu_Client *self, Qiniu_RS_BatchItemRet * const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entry->bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -973,7 +973,7 @@ Qiniu_Error Qiniu_RS_BatchChangeType(Qiniu_Client *self, Qiniu_RS_BatchItemRet * rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -998,7 +998,7 @@ Qiniu_Error Qiniu_RS_BatchRestoreArchive(Qiniu_Client *self, Qiniu_RS_BatchItemR const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entry->bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -1052,7 +1052,7 @@ Qiniu_Error Qiniu_RS_BatchRestoreArchive(Qiniu_Client *self, Qiniu_RS_BatchItemR rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -1077,7 +1077,7 @@ Qiniu_Error Qiniu_RS_BatchChangeMime(Qiniu_Client *self, Qiniu_RS_BatchItemRet * const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entry->bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -1129,7 +1129,7 @@ Qiniu_Error Qiniu_RS_BatchChangeMime(Qiniu_Client *self, Qiniu_RS_BatchItemRet * rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } @@ -1154,7 +1154,7 @@ Qiniu_Error Qiniu_RS_BatchDeleteAfterDays(Qiniu_Client *self, Qiniu_RS_BatchItem const char *rsHost; err = _Qiniu_Region_Get_Rs_Host(self, NULL, entry->bucket, &rsHost); - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } @@ -1209,7 +1209,7 @@ Qiniu_Error Qiniu_RS_BatchDeleteAfterDays(Qiniu_Client *self, Qiniu_RS_BatchItem rets[curr].code = code; - if (code != 200) + if (code != Qiniu_OK.code) { rets[curr].error = Qiniu_Json_GetString(dataItem, "error", 0); } diff --git a/qiniu/rsf.c b/qiniu/rsf.c index 8d09c437..782b2cac 100644 --- a/qiniu/rsf.c +++ b/qiniu/rsf.c @@ -77,7 +77,7 @@ Qiniu_Error Qiniu_RSF_ListFiles(Qiniu_Client *self, Qiniu_RSF_ListRet *ret, cons err = _Qiniu_Region_Get_Rsf_Host(self, NULL, bucket, &rsfHost); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { url = Qiniu_String_Concat(rsfHost, "/list?", "bucket=", encodedBucket, "&prefix=", encodedPrefix, "&delimiter=", encodedDelimiter, "&marker=", encodedMarker, "&limit=", limitStr, NULL); @@ -105,14 +105,14 @@ Qiniu_Error Qiniu_RSF_ListFiles(Qiniu_Client *self, Qiniu_RSF_ListRet *ret, cons Qiniu_Free(encodedMarker); } - if (err.code != 200) + if (err.code != Qiniu_OK.code) { return err; } err = Qiniu_Client_Call(self, &root, url); Qiniu_Free(url); - if (err.code == 200) + if (err.code == Qiniu_OK.code) { ret->marker = Qiniu_Json_GetString(root, "marker", NULL); // check common prefixes