diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-09-03 18:58:49 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-09-03 18:58:49 +0530 |
commit | 887b31ee12704e8c9da3bf4d26801d9236146192 (patch) | |
tree | 210d036978bad153f5e8ee46c14c4fc902ce7d37 /src/evanix.c | |
parent | a1eb8e06c42bf15e916a83180fc0d62c5a86fd37 (diff) |
nix: init
Diffstat (limited to 'src/evanix.c')
-rw-r--r-- | src/evanix.c | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/src/evanix.c b/src/evanix.c index 92f9596..25bbd8e 100644 --- a/src/evanix.c +++ b/src/evanix.c @@ -1,10 +1,12 @@ #include <errno.h> #include <getopt.h> +#include <nix/nix_api_value.h> #include <stdlib.h> #include <string.h> #include "build.h" #include "evanix.h" +#include "nix.h" #include "queue.h" #include "solver_conformity.h" #include "solver_highs.h" @@ -52,6 +54,8 @@ static int evanix(char *expr); static int evanix_free(struct evanix_opts_t *opts); static int opts_read(struct evanix_opts_t *opts, char **expr, int argc, char *argv[]); +static int evanix_opts_system_set(struct evanix_opts_t *opts, + nix_c_context *nix_ctx); /* This function returns errno on failure, consistent with the POSIX threads * functions, rather than returning -errno. */ @@ -71,13 +75,42 @@ static int evanix_build_thread_create(struct build_thread *build_thread) return 0; } +static int evanix_opts_system_set(struct evanix_opts_t *opts, + nix_c_context *nix_ctx) +{ + nix_err nix_ret; + + if (opts->system) + return 0; + + nix_ret = nix_setting_get(nix_ctx, "system", _nix_get_string_strdup, + &opts->system); + if (nix_ret != NIX_OK) { + print_err("%s", nix_err_msg(NULL, nix_ctx, NULL)); + return -EPERM; + } else if (opts->system == NULL) { + return -EPERM; + } + + return 0; +} + static int evanix(char *expr) { + nix_c_context *nix_ctx = NULL; struct queue_thread *queue_thread = NULL; struct build_thread *build_thread = NULL; FILE *jobs_stream = NULL; /* nix-eval-jobs stdout */ int ret = 0; + ret = _nix_init(&nix_ctx); + if (ret < 0) + goto out_free; + + ret = evanix_opts_system_set(&evanix_opts, nix_ctx); + if (ret < 0) + goto out_free; + ret = jobs_init(&jobs_stream, expr); if (ret < 0) goto out_free; @@ -127,6 +160,7 @@ static int evanix(char *expr) } out_free: + nix_c_context_free(nix_ctx); fclose(jobs_stream); queue_thread_free(queue_thread); free(build_thread); @@ -182,7 +216,13 @@ static int opts_read(struct evanix_opts_t *opts, char **expr, int argc, opts->isdryrun = true; break; case 's': - opts->system = optarg; + opts->system = strdup(optarg); + if (opts->system == NULL) { + print_err("%s", strerror(errno)); + ret = -errno; + goto out_free_evanix; + } + break; case 'r': opts->solver_report = true; @@ -194,7 +234,8 @@ static int opts_read(struct evanix_opts_t *opts, char **expr, int argc, "Try 'evanix --help' for more " "information.\n", c); - return -EINVAL; + ret = -EINVAL; + goto out_free_evanix; } ret = sqlite3_open_v2(optarg, &opts->statistics.db, @@ -212,7 +253,8 @@ static int opts_read(struct evanix_opts_t *opts, char **expr, int argc, NULL); if (ret != SQLITE_OK) { print_err("%s", "Failed to prepare sql"); - return -EPERM; + ret = -EPERM; + goto out_free_evanix; } break; @@ -351,11 +393,12 @@ static int evanix_free(struct evanix_opts_t *opts) { int ret; + free(opts->system); + if (opts->statistics.statement) { sqlite3_finalize(opts->statistics.statement); opts->statistics.statement = NULL; } - if (opts->statistics.db) { ret = sqlite3_close(opts->statistics.db); if (ret != SQLITE_OK) { |