bud_error_t bud_config_reload(bud_config_t* config) { bud_error_t err; bud_config_t* loaded; bud_config_t restore; loaded = bud_config_load(config->path, config->inlined, &err); if (!bud_is_ok(err)) return err; memset(&restore, 0, sizeof(restore)); bud_config_copy(&restore, config); bud_config_copy(config, loaded); /* Initialize config with new params */ err = bud_config_init(config); /* Restore everything on failure */ if (!bud_is_ok(err)) { bud_config_copy(config, &restore); bud_config_free(loaded); return err; } free(loaded); bud_config_destroy(&restore); return bud_ok(); }
bud_config_t* bud_config_cli_load(uv_loop_t* loop, int argc, char** argv, bud_error_t* err) { int c; int r; int index; int is_daemon; int is_worker; size_t path_len; bud_config_t* config; struct option long_options[] = { { "version", 0, NULL, 'v' }, { "config", 1, NULL, 'c' }, #ifndef _WIN32 { "daemonize", 0, NULL, 'd' }, #endif /* !_WIN32 */ { "worker", 0, NULL, 1000 }, { "default-config", 0, NULL, 1001 }, { NULL, 0, NULL, 0 } }; *err = bud_ok(); config = NULL; is_daemon = 0; is_worker = 0; do { index = 0; c = getopt_long(argc, argv, "vc:d", long_options, &index); switch (c) { case 'v': bud_print_version(); break; case 'c': config = bud_config_load(loop,optarg, err); if (config == NULL) { ASSERT(!bud_is_ok(*err), "Config load failed without error"); c = -1; break; } if (is_daemon) config->is_daemon = 1; if (is_worker) config->is_worker = 1; break; #ifndef _WIN32 case 'd': is_daemon = 1; if (config != NULL) config->is_daemon = 1; #endif /* !_WIN32 */ break; case 1000: is_worker = 1; if (config != NULL) config->is_worker = 1; break; case 1001: bud_config_print_default(); c = -1; break; default: if (config == NULL) bud_print_help(argc, argv); c = -1; break; } } while (c != -1); if (config != NULL) { /* CLI options */ config->argc = argc; config->argv = argv; /* Get executable path */ path_len = sizeof(config->exepath); r = uv_exepath(config->exepath, &path_len); ASSERT(path_len < sizeof(config->exepath), "Exepath OOB"); config->exepath[path_len] = 0; if (r != 0) { bud_config_free(config); config = NULL; *err = bud_error_num(kBudErrExePath, r); } /* Initialize config */ *err = bud_config_init(config); if (!bud_is_ok(*err)) { bud_config_free(config); return NULL; } } return config; }