From 4c7093f68e8c576e1e10dbb31597c810691fd6e2 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Tue, 11 Jun 2024 14:11:06 +0530 Subject: src/jobs/job_read: refactor --- src/jobs.c | 210 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 128 insertions(+), 82 deletions(-) diff --git a/src/jobs.c b/src/jobs.c index 1a386ff..a862c8a 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -8,69 +8,64 @@ #include "jobs.h" #include "util.h" -static int job_output_insert(struct job *j, char *name, char *store_path); static void output_free(struct output *output); +static int job_output_insert(struct job *j, char *name, char *store_path); +static int job_read_inputdrvs(struct job *job, cJSON *input_drvs); +static int job_read_outputs(struct job *job, cJSON *outputs); -int job_read(FILE *stream, struct job **job) +static void output_free(struct output *output) { - int ret; - struct job *dep_job; - cJSON *root, *temp, *input_drvs, *array, *outputs; - char *name = NULL; - char *drv_path = NULL; - char *out_name = NULL; - char *out_path = NULL; + if (output == NULL) + return; - ret = json_streaming_read(stream, &root); - if (ret < 0) - return ret; + free(output->name); + free(output->store_path); - temp = cJSON_GetObjectItemCaseSensitive(root, "name"); - if (!cJSON_IsString(temp)) { - ret = -EPERM; - goto out_free; - } - name = strdup(temp->valuestring); - if (name == NULL) { - ret = -EPERM; - goto out_free; - } + free(output); +} - temp = cJSON_GetObjectItemCaseSensitive(root, "drvPath"); - if (!cJSON_IsString(temp)) { - ret = -EPERM; - goto out_free; - } - drv_path = strdup(temp->valuestring); - if (drv_path == NULL) { - ret = -EPERM; - goto out_free; +static int job_output_insert(struct job *j, char *name, char *store_path) +{ + struct output *o; + + o = malloc(sizeof(*o)); + if (o == NULL) { + print_err("%s", strerror(errno)); + return -errno; } - ret = job_new(job, name, drv_path); - if (ret < 0) - goto out_free; + o->name = name; + o->store_path = store_path; + LIST_INSERT_HEAD(&j->outputs, o, dlist); - input_drvs = cJSON_GetObjectItemCaseSensitive(root, "inputDrvs"); - for (temp = input_drvs; temp != NULL; temp = temp->next) { - array = cJSON_GetObjectItemCaseSensitive(temp, temp->string); + return 0; +} + +static int job_read_inputdrvs(struct job *job, cJSON *input_drvs) +{ + cJSON *array; + + int ret = 0; + struct job *dep_job = NULL; + char *drv_path = NULL; + char *out_name = NULL; + + for (cJSON *i = input_drvs; i != NULL; i = i->next) { + array = cJSON_GetObjectItemCaseSensitive(i, i->string); if (!cJSON_IsArray(array)) { ret = -EPERM; - job_free(*job); goto out_free; } - drv_path = strdup(temp->string); + drv_path = strdup(i->string); if (drv_path == NULL) { ret = -EPERM; - job_free(*job); goto out_free; } ret = job_new(&dep_job, NULL, drv_path); if (ret < 0) { ret = -EPERM; - job_free(*job); goto out_free; } @@ -78,13 +73,11 @@ int job_read(FILE *stream, struct job **job) out_name = strdup(array->string); if (out_name == NULL) { ret = -EPERM; - job_free(*job); goto out_free; } ret = job_output_insert(dep_job, out_name, NULL); if (ret < 0) { - job_free(*job); job_free(dep_job); goto out_free; } @@ -92,56 +85,126 @@ int job_read(FILE *stream, struct job **job) drv_path = NULL; out_name = NULL; - LIST_INSERT_HEAD(&(*job)->deps, dep_job, dlist); + dep_job = NULL; + + LIST_INSERT_HEAD(&job->deps, dep_job, dlist); + } + +out_free: + if (ret < 0) { + job_free(dep_job); + free(drv_path); + free(out_name); } - outputs = cJSON_GetObjectItemCaseSensitive(root, "outputs"); - for (temp = outputs; temp != NULL; temp = temp->next) { - out_name = strdup(temp->string); + return ret; +} + +static int job_read_outputs(struct job *job, cJSON *outputs) +{ + int ret = 0; + char *out_name = NULL; + char *out_path = NULL; + + for (cJSON *i = outputs; i != NULL; i = i->next) { + out_name = strdup(i->string); if (out_name == NULL) { ret = -EPERM; - job_free(*job); goto out_free; } - out_path = strdup(temp->valuestring); + out_path = strdup(i->valuestring); if (out_path == NULL) { ret = -EPERM; - job_free(*job); goto out_free; } - ret = job_output_insert(*job, out_name, out_path); - free(out_path); + ret = job_output_insert(job, out_name, out_path); if (ret < 0) { ret = -EPERM; - job_free(*job); goto out_free; } + out_path = NULL; + out_name = NULL; } out_free: - cJSON_Delete(root); - if (ret < 0) { - print_err("%s", "Invalid JSON"); - free(name); - free(drv_path); free(out_name); + free(out_path); } - return ret; + + return 0; } -static void output_free(struct output *output) +int job_read(FILE *stream, struct job **job) { - if (output == NULL) - return; + cJSON *temp; - free(output->name); - free(output->store_path); + int ret = 0; + cJSON *root = NULL; + char *name = NULL; + char *drv_path = NULL; - free(output); + ret = json_streaming_read(stream, &root); + if (ret < 0) + return ret; + + temp = cJSON_GetObjectItemCaseSensitive(root, "name"); + if (!cJSON_IsString(temp)) { + ret = -EPERM; + goto out_free; + } + name = strdup(temp->valuestring); + if (name == NULL) { + ret = -EPERM; + goto out_free; + } + + temp = cJSON_GetObjectItemCaseSensitive(root, "drvPath"); + if (!cJSON_IsString(temp)) { + ret = -EPERM; + goto out_free; + } + drv_path = strdup(temp->valuestring); + if (drv_path == NULL) { + ret = -EPERM; + goto out_free; + } + + ret = job_new(job, name, drv_path); + if (ret < 0) + goto out_free; + + temp = cJSON_GetObjectItemCaseSensitive(root, "inputDrvs"); + if (!cJSON_IsObject(temp)) { + ret = -EPERM; + goto out_free; + } + ret = job_read_inputdrvs(*job, temp); + if (ret < 0) + goto out_free; + + temp = cJSON_GetObjectItemCaseSensitive(root, "outputs"); + if (!cJSON_IsObject(temp)) { + ret = -EPERM; + goto out_free; + } + ret = job_read_outputs(*job, temp); + if (ret < 0) + goto out_free; + +out_free: + cJSON_Delete(root); + if (ret < 0) { + print_err("%s", "Invalid JSON"); + + free(name); + free(drv_path); + } + + return ret; } void job_free(struct job *job) @@ -164,23 +227,6 @@ void job_free(struct job *job) free(job); } -static int job_output_insert(struct job *j, char *name, char *store_path) -{ - struct output *o; - - o = malloc(sizeof(*o)); - if (o == NULL) { - print_err("%s", strerror(errno)); - return -errno; - } - - o->name = name; - o->store_path = store_path; - LIST_INSERT_HEAD(&j->outputs, o, dlist); - - return 0; -} - int job_new(struct job **j, char *name, char *drv_path) { struct job *job; -- cgit v1.2.3