diff --git a/inc/lektor/macro.h b/inc/lektor/macro.h index 4b93b9e4b726b41adb61babe3ec9d45f7c817085..712ae9f9a6799646dd2bace4b3d459746631f940 100644 --- a/inc/lektor/macro.h +++ b/inc/lektor/macro.h @@ -49,3 +49,12 @@ #ifndef MIN #define MIN(a, b) ((a) > (b) ? (b) : (a)) #endif /* MIN */ + +#define RETURN_IF(cond, msg, ret) \ + if (cond) { \ + fprintf(stderr, " ! %s: %s\n", __func__, msg); \ + return ret; \ + } + +#define RETURN_UNLESS(cond, msg, ret) RETURN_IF(!(cond), msg, ret) +#define NOTHING /* Usefull to return nothing. */ diff --git a/src/repo/async.c b/src/repo/async.c index 8fdb2ce7756dffcb677eb6ccc215f48f40060d81..8bd48e0fe58a79b7033ecb35b42dba2c685cbae9 100644 --- a/src/repo/async.c +++ b/src/repo/async.c @@ -5,6 +5,7 @@ #include <unistd.h> #include <string.h> +#include <lektor/macro.h> #include <lektor/repo.h> #include <lektor/thread.h> #include <limits.h> @@ -22,11 +23,7 @@ int repo_join_thread(void) { int ret = 1; - - if (pthread_mutex_lock(&mtx)) { - fprintf(stderr, " ! repo_join_thread: Failed to lock mutex\n"); - return 3; - } + RETURN_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", 3); if (!init) { fprintf(stderr, " ! repo_join_thread: repo thread is not launched, can't join\n"); @@ -44,10 +41,7 @@ repo_join_thread(void) ret = 0; error: - if (pthread_mutex_unlock(&mtx)) { - fprintf(stderr, " ! repo_join_thread: Failed to unlock mutex\n"); - ret = 3; - } + RETURN_IF(pthread_mutex_unlock(&mtx), "Failed to unlock mutex", 3); return ret; } @@ -66,10 +60,7 @@ __handle_got_json(struct lkt_thread *self, struct lkt_repo *repo, struct json_ob struct kara *kara; int err; - if (len <= 0 || NULL == json_object_get_array(json)) { - fprintf(stderr, "__handle_got_json: Got json is invalid or the array is empty\n"); - return; - } + RETURN_UNLESS(len > 0 && json_object_get_array(json), "Json invalid or array empty", NOTHING); for (i = 0; i < len; ++i) { kara_json = json_object_array_get_idx(json, i); @@ -91,10 +82,7 @@ __handle_got_json(struct lkt_thread *self, struct lkt_repo *repo, struct json_ob kara->filename[PATH_MAX - 1] = 0; fprintf(stderr, " . __handle_got_json: Crafted filename is '%s'\n", kara->filename); - if (!kara) { - fprintf(stderr, " ! __handle_got_json: Out of memory\n"); - return; - } + RETURN_UNLESS(kara, "Out of memory", NOTHING); /* Get the fields from the json. */ err |= safe_json_get_string(kara_json, "song_name", kara->mdt.song_name, LEKTOR_TAG_MAX); @@ -216,25 +204,11 @@ end_loop: int repo_new_thread(struct lkt_repo *const repo) { - if (init) { - fprintf(stderr, " * repo_new_thread: Already running\n"); - return 1; - } - + RETURN_IF(init, "Already running", 1); struct lkt_thread_arg *arg = calloc(1, sizeof(struct lkt_thread_arg)); - - if (!arg) { - fprintf(stderr, " ! repo_new_thread: Out of memory\n"); - return errno = ENOMEM; - } - + RETURN_UNLESS(arg, "Out of memory", errno = ENOMEM); arg->args = repo; - - if (lkt_th_new(&repo_thread, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg)) { - fprintf(stderr, " ! repo_new_thread: Failed to create the repo thread\n"); - return 1; - } - + RETURN_IF(lkt_th_new(&repo_thread, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg), "Thread error", 1); init = 1; return 0; } @@ -242,11 +216,8 @@ repo_new_thread(struct lkt_repo *const repo) int repo_download_id_async(const size_t id) { - if (lkt_th_append_input(&repo_thread, (void *) id)) { - fprintf(stderr, " ! repo_download_id_async: Failed to push kara to download, id was %lu\n", id); - return 1; - } - + RETURN_IF(id == 0, "Invalid argument", 1); + RETURN_IF(lkt_th_append_input(&repo_thread, (void *) id), "Failed to push downloaded id", id); fprintf(stderr, " * Asked to download kara with id %lu\n", id); return 0; } @@ -270,17 +241,8 @@ err: inline int repo_get_allid_async(void) { - if (pthread_mutex_lock(&mtx)) { - fprintf(stderr, " ! repo_get_allid_async: Failed to lock mutex\n"); - return 3; - } - + RETURN_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", 3); all_json = 1; - - if (pthread_mutex_unlock(&mtx)) { - fprintf(stderr, " ! repo_get_allid_async: Failed to unlock mutex\n"); - return 3; - } - + RETURN_IF(pthread_mutex_unlock(&mtx), "Failed to lock mutex", 3); return 0; } diff --git a/src/repo/curl.c b/src/repo/curl.c index 0f4b8191e615353459a2d77669bf9dace72aa147..d613a7238fcc3368f01ffb85f2e43bdcf928a0a8 100644 --- a/src/repo/curl.c +++ b/src/repo/curl.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/repo.h> +#include <lektor/macro.h> #include <lektor/database.h> #include <errno.h> #include <stdlib.h> @@ -33,15 +34,11 @@ write_mem__(char *data, size_t size, size_t nmem, void *user) struct memory *mem = (struct memory *) user; void *ptr = realloc(mem->mem, mem->size + realsize); - if (ptr == NULL) { - fprintf(stderr, " ! write_mem__: not enough memory (realloc returned NULL)\n"); - return 0; - } + RETURN_UNLESS(ptr, "Out of memory", 0); mem->mem = ptr; memcpy(((uint8_t *) mem->mem) + mem->size, data, realsize); mem->size += realsize; - return realsize; } @@ -50,12 +47,7 @@ write_disk__(char *data, size_t size, size_t nmem, void *user) { ssize_t realsize = size * nmem; struct file *file = (struct file *) user; - - if (write(file->fd, data, realsize) != realsize) { - fprintf(stderr, " ! write_disk__: failed to write to '%s'\n", file->path); - return 0; - } - + RETURN_IF(write(file->fd, data, realsize) != realsize, "Failed to write", 0); return realsize; } @@ -70,14 +62,8 @@ repo_new(struct lkt_repo *const repo_, const char *name_, const char *url_) const size_t init_size = 30; uint64_t *calloc1 = calloc(init_size, sizeof(uint64_t)); - - if (!calloc1) { - fprintf(stderr, " ! repo_new: Out of memory\n"); - return ENOMEM; - } - + RETURN_UNLESS(calloc1, "Out of memory", errno = ENOMEM); uint64_t *calloc2 = calloc(init_size, sizeof(uint64_t)); - if (!calloc2) { free(calloc1); fprintf(stderr, " ! repo_new: Out of memory\n"); @@ -113,65 +99,32 @@ repo_free(struct lkt_repo *const repo) int safe_json_get_string(struct json_object *jobj, const char *key, char *content, const size_t len) { - int ret = 1; const char *got; struct json_object *field; - - if (!json_object_object_get_ex(jobj, key, &field)) { - fprintf(stderr, " ! safe_json_get_string: No object with key %s in json\n", key); - goto err; - } - + RETURN_UNLESS(json_object_object_get_ex(jobj, key, &field), "Key not found in json", 1); got = json_object_get_string(field); - - if (!got) { - fprintf(stderr, " ! safe_json_get_string: Got a NULL for key %s, may be an error\n", key); - goto err; - } - + RETURN_UNLESS(got, "Got a NULL for the key, may be an error", 1); strncpy(content, got, len - 1); content[len - 1] = 0; - - ret = 0; -err: - return ret; + return 0; } int safe_json_get_int32(struct json_object *json, const char *key, int32_t *ret) { struct json_object *field; - - if (!json_object_object_get_ex(json, key, &field)) { - fprintf(stderr, " . safe_json_get_int32: No object with key '%s' in json, error\n", key); - goto err; - } - + RETURN_UNLESS(json_object_object_get_ex(json, key, &field), "Key not found", 1); errno = 0; *ret = json_object_get_int(field); - - if (errno == EINVAL) { - fprintf(stderr, " . __handle_got_json: Invalid integer for field with key 'song_number'\n"); - goto err; - } - - if (*ret == INT32_MAX || *ret == INT32_MIN) { - fprintf(stderr, " . __handle_got_json: Out of bound integer for field with key 'song_number'\n"); - goto err; - } - + RETURN_IF(errno = EINVAL, "Invalid 32bit integer", 1); + RETURN_IF(*ret == INT32_MAX || *ret == INT32_MIN, "Out of bound 32bit integer", 1); return 0; -err: - return 1; } int repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json) { - if (!json) { - fprintf(stderr, " ! repo_get_alljson_sync: Invalid argument\n"); - return 1; - } + RETURN_UNLESS(json, "Invalid argument", 1); CURL *curl_handle; CURLcode res; @@ -197,7 +150,6 @@ repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json) } *json = json_tokener_parse(file.mem); - ret = 0; err: curl_easy_cleanup(curl_handle); @@ -212,15 +164,9 @@ repo_get_id(struct lkt_repo *const repo, const uint64_t id, struct kara_metadata struct json_object *jobj, *field; int ret = 1, err = 0; - if (!mdt) - fprintf(stderr, " ! repo_get_id: invalid argument\n"); - + RETURN_UNLESS(mdt, "Invalid argument", 1); char *url = calloc(URL_MAX_LEN, sizeof(char)); - - if (!url) { - fprintf(stderr, " ! repo_get_id: out of memory\n"); - return ENOMEM; - } + RETURN_UNLESS(url, "Out of memory", errno = ENOMEM); struct memory file = { .mem = NULL, @@ -297,22 +243,13 @@ int repo_download_id_sync(struct lkt_repo *const repo, sqlite3 *db, const uint64_t id, const char *kara_path, struct kara_metadata *mdt_ret) { - if (!kara_path) { - fprintf(stderr, " ! repo_download_id_sync: Can't download kara to a NULL path\n"); - return 1; - } - + RETURN_UNLESS(kara_path, "Invalid argument", 1); struct kara_metadata mdt; CURL *curl_handle; int ret = 1, fd; char *url = calloc(URL_MAX_LEN, sizeof(char)); char *ct; - - if (!url) { - fprintf(stderr, " ! repo_download_id_sync: Out of memory\n"); - errno = ENOMEM; - return ENOMEM; - } + RETURN_UNLESS(url, "Out of memory", errno = ENOMEM); if (repo_get_id(repo, id, mdt_ret ? mdt_ret : &mdt)) { fprintf(stderr, " ! repo_download_id_sync: Failed to get kara metadata from kurisu\n");