int set_network_setting(const char *config, const char *setting) { GKeyFile *settingsfile; gboolean test = FALSE; int ret = 0; gchar *keyfile; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!test) { log_debug("No conffile. Creating.\n"); create_conf_file(); } if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY)) { g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting); keyfile = g_key_file_to_data (settingsfile, NULL, NULL); /* free the settingsfile before writing things out to be sure the contents will be correctly written to file afterwards. Just a precaution. */ g_key_file_free(settingsfile); ret = g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL); free(keyfile); } else { g_key_file_free(settingsfile); return(1); } /* g_file_set_contents returns 1 on succes, since set_mode_settings returns 0 on succes we return the ! value */ return(!ret); }
set_config_result_t set_config_setting(const char *entry, const char *key, const char *value) { GKeyFile *settingsfile; gboolean test = FALSE; set_config_result_t ret = SET_CONFIG_ERROR; gchar *keyfile; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(test) { if(config_value_changed(settingsfile, entry, key, value)) { g_key_file_free(settingsfile); return SET_CONFIG_UNCHANGED; } } else { log_debug("No conffile. Creating.\n"); create_conf_file(); } g_key_file_set_string(settingsfile, entry, key, value); keyfile = g_key_file_to_data (settingsfile, NULL, NULL); /* free the settingsfile before writing things out to be sure the contents will be correctly written to file afterwards. Just a precaution. */ g_key_file_free(settingsfile); if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL)) ret = SET_CONFIG_UPDATED; g_free(keyfile); return (ret); }
static int get_conf_int(const gchar *entry, const gchar *key) { GKeyFile *settingsfile; gboolean test = FALSE; gchar **keys, **origkeys; int ret = 0; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!test) { log_debug("no conffile, Creating\n"); create_conf_file(); } keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL); if(keys == NULL) return ret; origkeys = keys; while (*keys != NULL) { if(!strcmp(*keys, key)) { ret = g_key_file_get_integer(settingsfile, entry, *keys, NULL); log_debug("%s key value = %d\n", key, ret); } keys++; } g_strfreev(origkeys); g_key_file_free(settingsfile); return(ret); }
int set_config_setting(const char *entry, const char *key, const char *value) { GKeyFile *settingsfile; gboolean test = FALSE; int ret = 0; gchar *keyfile; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!test) { log_debug("No conffile. Creating.\n"); create_conf_file(); } g_key_file_set_string(settingsfile, entry, key, value); keyfile = g_key_file_to_data (settingsfile, NULL, NULL); /* free the settingsfile before writing things out to be sure the contents will be correctly written to file afterwards. Just a precaution. */ g_key_file_free(settingsfile); ret = g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL); g_free(keyfile); /* g_file_set_contents returns 1 on succes, since set_mode_settings returns 0 on succes we return the ! value */ return(!ret); }
static const char * get_conf_string(const gchar *entry, const gchar *key) { GKeyFile *settingsfile; gboolean test = FALSE; gchar **keys, *tmp_char = NULL; const char *ret = NULL; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!test) { log_debug("No conffile. Creating\n"); create_conf_file(); } keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL); if(keys == NULL) return ret; while (*keys != NULL) { if(!strcmp(*keys, key)) { tmp_char = g_key_file_get_string(settingsfile, entry, *keys, NULL); if(tmp_char) { log_debug("key %s value = %s\n", key, tmp_char); } } keys++; } g_key_file_free(settingsfile); return(g_strdup(tmp_char)); }
/* * @param config : the key to be set * @param setting : The value to be set */ set_config_result_t set_network_setting(const char *config, const char *setting) { GKeyFile *settingsfile; gboolean test = FALSE; gchar *keyfile; if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY)) if(validate_ip(setting) != 0) return SET_CONFIG_ERROR; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY)) { set_config_result_t ret = SET_CONFIG_ERROR; if (test) { if(config_value_changed(settingsfile, NETWORK_ENTRY, config, setting)) { g_key_file_free(settingsfile); return SET_CONFIG_UNCHANGED; } } else { log_debug("No conffile. Creating.\n"); create_conf_file(); } g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting); keyfile = g_key_file_to_data (settingsfile, NULL, NULL); /* free the settingsfile before writing things out to be sure the contents will be correctly written to file afterwards. Just a precaution. */ g_key_file_free(settingsfile); if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL)) ret = SET_CONFIG_UPDATED; free(keyfile); return ret; } else { g_key_file_free(settingsfile); return SET_CONFIG_ERROR; } }
static bool torture_smbconf_txt(void) { sbcErr err; bool ret = true; const char *filename = "/tmp/smb.conf.smbconf_testsuite"; struct smbconf_ctx *conf_ctx = NULL; TALLOC_CTX *mem_ctx = talloc_stackframe(); printf("test: text backend\n"); if (!create_conf_file(filename)) { ret = false; goto done; } printf("TEST: init\n"); err = smbconf_init_txt(mem_ctx, &conf_ctx, filename); if (!SBC_ERROR_IS_OK(err)) { printf("FAIL: text backend failed: %s\n", sbcErrorString(err)); ret = false; goto done; } printf("OK: init\n"); ret &= test_get_includes(conf_ctx); smbconf_shutdown(conf_ctx); printf("TEST: unlink file\n"); if (unlink(filename) != 0) { printf("OK: unlink failed: %s\n", strerror(errno)); ret = false; goto done; } printf("OK: unlink file\n"); done: printf("%s: text backend\n", ret ? "success" : "failure"); talloc_free(mem_ctx); return ret; }
static char * get_conf_string(const gchar *entry, const gchar *key) { GKeyFile *settingsfile; gboolean test = FALSE; gchar **keys, **origkeys, *tmp_char = NULL; settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); if(!test) { log_debug("No conffile. Creating\n"); create_conf_file(); /* should succeed now */ g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL); } keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL); if(keys == NULL) goto end; origkeys = keys; while (*keys != NULL) { if(!strcmp(*keys, key)) { tmp_char = g_key_file_get_string(settingsfile, entry, *keys, NULL); if(tmp_char) { log_debug("key %s value = %s\n", key, tmp_char); } } keys++; } g_strfreev(origkeys); end: g_key_file_free(settingsfile); return(tmp_char); }
int conf_file_merge(void) { GDir *confdir; struct stat fileinfo, dir; char *mode = 0, *ip = 0, *gateway = 0, *udev = 0; gchar *filename_full; const gchar *filename; GString *keyfile_string = NULL; GKeyFile *settingsfile; int ret = 0, test = 0, conffile_created = 0; confdir = g_dir_open(CONFIG_FILE_DIR, 0, NULL); if(!confdir) { log_debug("No configuration. Creating defaults.\n"); create_conf_file(); /* since there was no configuration at all there is no info to be merged */ return (ret); } if (stat(FS_MOUNT_CONFIG_FILE, &fileinfo) == -1) { /* conf file not created yet, make default and merge all */ create_conf_file(); conffile_created = 1; } /* config file is created, so the dir must exists */ if(stat(CONFIG_FILE_DIR, &dir)) { log_warning("Directory still does not exists. FS might be ro/corrupted!\n"); ret = 1; goto end; } /* st_mtime is changed by file modifications, st_mtime of a directory is changed by the creation or deletion of files in that directory. So if the st_mtime of the config file is equal to the directory time we can be sure the config is untouched and we do not need to re-merge the config. */ if(fileinfo.st_mtime == dir.st_mtime) { /* if a conffile was created, the st_mtime would have been updated so this check will miss information that might be there already, like after a config file removal for example. So we run a merge anyway if we needed to create the conf file */ if(!conffile_created) goto end; } log_debug("Merging/creating configuration.\n"); keyfile_string = g_string_new(NULL); /* check each ini file and get contents */ while((filename = g_dir_read_name(confdir)) != NULL) { if(!strstr(filename, ".ini")) { /* skip this file as it might be a dir or not an ini file */ continue; } filename_full = g_strconcat(CONFIG_FILE_DIR, "/", filename, NULL); log_debug("filename = %s\n", filename_full); if(!strcmp(filename_full, FS_MOUNT_CONFIG_FILE)) { /* store mode info to add it later as we want to keep it */ mode = get_mode_setting(); /* store udev path (especially important for the upgrade path */ udev = find_udev_path(); ip = get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY); gateway = get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY); continue; } /* load contents of file, if it fails skip to next one */ settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, filename_full, G_KEY_FILE_KEEP_COMMENTS, NULL); if(!test) { log_debug("%d failed loading config file %s\n", test, filename_full); goto next; } log_debug("file data = %s\n", g_key_file_to_data(settingsfile, NULL, NULL)); keyfile_string = g_string_append(keyfile_string, g_key_file_to_data(settingsfile, NULL, NULL)); log_debug("keyfile_string = %s\n", keyfile_string->str); g_key_file_free(settingsfile); next: g_free(filename_full); } if(keyfile_string) { /* write out merged config file */ /* g_file_set_contents returns 1 on succes, this function returns 0 */ ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile_string->str, -1, NULL); g_string_free(keyfile_string, TRUE); if(mode) { set_mode_setting(mode); } if(udev) { set_config_setting(UDEV_PATH_ENTRY, UDEV_PATH_KEY, udev); } /* check if no network data came from an ini file */ if( get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY)) goto cleanup; if(ip) set_network_setting(NETWORK_IP_KEY, ip); if(gateway) set_network_setting(NETWORK_GATEWAY_KEY, gateway); } else ret = 1; cleanup: if(mode) free(mode); if(udev) free(udev); if(ip) free(ip); if(gateway) free(gateway); end: g_dir_close(confdir); return(ret); }
void read_cli_options(int argc, char **argv, struct kw_conf * (*kw)(const char *), FILE ** fin, FILE ** fout) { int i; if (argc == 1) return; // use stdin and stdout if (argc == 2 && strcmp(argv[1], "--create-config-file") == 0) { if (create_conf_file(FSQLF_CONFFILE_NAME) != 0) { exit(1); } else { fprintf(stderr, "File '%s' (re)created.\n", FSQLF_CONFFILE_NAME); exit(0); } } for (i = 1; i < argc; i++) { if (argv[i][0] != '-') { if ((*fin) == stdin) { //try to openinig INPUT file (*fin) = fopen(argv[1], "r"); if (!(*fin)) { FAIL_WITH_ERROR(1, "Error opening input file: %s", argv[i]); } } else if ((*fout) == stdout) { //try to openinig OUTPUT file (only if INPUT file is set) (*fout) = fopen(argv[2], "w+"); if (!(*fout)) FAIL_WITH_ERROR(1, "Error opening output file: %s", argv[i]); } } else if (ARGV_MATCH(i, "-i")) { if (++i >= argc) FAIL_WITH_ERROR(1, "Missing value for option : %s", argv[i-1]); (*fin) = fopen(argv[i], "r"); if (!(*fin)) FAIL_WITH_ERROR(1, "Error opening input file: %s", argv[i]); } else if (ARGV_MATCH(i, "-o")) { if (++i >= argc) FAIL_WITH_ERROR(1, "Missing value for option : %s", argv[i-1]); (*fout) = fopen(argv[i], "w+"); if (!(*fout)) FAIL_WITH_ERROR(1, "Error opening output file: %s", argv[i]); } else if (ARGV_MATCH(i, "--config-file")) { if (++i >= argc) FAIL_WITH_ERROR(1, "Missing value for option : %s", argv[i-1]); if (read_conf_file(argv[i], kw) == READ_FAILED) { FAIL_WITH_ERROR(1, "Error reading configuration file: %s", argv[i]); } } else if (ARGV_MATCH(i, "--select-comma-newline")) { if (++i >= argc) FAIL_WITH_ERROR(1, "Missing value for option : %s", argv[i-1]); if (strcmp(argv[i], "after") == 0) { kw("kw_comma")->before.new_line = 0; kw("kw_comma")->after.new_line = 1; } else if (strcmp(argv[i], "before") == 0) { kw("kw_comma")->before.new_line = 1; kw("kw_comma")->after.new_line = 0; } else if (strcmp(argv[i], "none") == 0) { kw("kw_comma")->before.new_line = 0; kw("kw_comma")->after.new_line = 0; } } else if (ARGV_MATCH(i, "--keyword-case")) { if (++i >= argc) FAIL_WITH_ERROR(1, "Missing value for option : %s", argv[i-1]); if (strcmp(argv[i], "none") == 0) { set_case(CASE_none); } else if (strcmp(argv[i], "upper") == 0) { set_case(CASE_UPPER); } else if (strcmp(argv[i], "lower") == 0) { set_case(CASE_lower); } else if (strcmp(argv[i], "initcap") == 0) { set_case(CASE_Initcap); } } else if (ARGV_MATCH(i, "--keyword-text")) { if (++i >= argc) FAIL_WITH_ERROR(1, "Missing value for option : %s", argv[i-1]); if (strcmp(argv[i], "original") == 0) { set_text_original(1); } else if (strcmp(argv[i], "default") == 0) { set_text_original(0); } } else if (ARGV_MATCH(i, "--select-newline-after")) { kw("kw_select")->after.new_line = get_int_arg(++i, argc, argv); } else if (ARGV_MATCH(i, "--newline-or-before")) { kw("kw_or")->before.new_line = get_int_arg(++i, argc, argv); } else if (ARGV_MATCH(i, "--newline-or-after")) { kw("kw_or")->after.new_line = get_int_arg(++i, argc, argv); } else if (ARGV_MATCH(i, "--newline-and-before")) { kw("kw_and")->before.new_line = get_int_arg(++i, argc, argv); } else if (ARGV_MATCH(i, "--newline-and-after")) { kw("kw_and")->after.new_line = get_int_arg(++i, argc, argv); } else if (ARGV_MATCH(i, "--newline-major-sections")) { int new_line_count = get_int_arg(++i, argc, argv); kw("kw_from")->before.new_line = new_line_count; kw("kw_where")->before.new_line = new_line_count; kw("kw_inner_join")->before.new_line = new_line_count; kw("kw_left_join")->before.new_line = new_line_count; kw("kw_right_join")->before.new_line = new_line_count; kw("kw_full_join")->before.new_line = new_line_count; kw("kw_cross_join")->before.new_line = new_line_count; } else if (ARGV_MATCH(i, "--debug")) { if (++i >= argc ) FAIL_WITH_ERROR(1, "Missing or invalid value for option : %s", argv[i-1]); if (ARGV_MATCH(i, "none")) debug_level |= DEBUGNONE; else if (ARGV_MATCH(i, "state")) debug_level |= DEBUGSTATES; else if (ARGV_MATCH(i, "match")) debug_level |= DEBUGMATCHES; else if (ARGV_MATCH(i, "parenthesis")) debug_level |= DEBUGPARCOUNTS; else FAIL_WITH_ERROR(1, "Missing or invalid value for option : %s", argv[i-1]); } else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { usage_info(argc, argv); exit(0); } else FAIL_WITH_ERROR(1, "Option `%s' is not recognised or used incorrectly.\nTry `%s --help' for more information\n", argv[i], argv[0]); } }
int conf_file_merge(void) { GDir *confdir; struct stat fileinfo, dir; const gchar *filename, *mode = 0; gchar *filename_full; GString *keyfile_string = NULL; GKeyFile *settingsfile; int ret = 0, test = 0; #ifdef UDEV const gchar *udev = 0; #endif /* UDEV */ confdir = g_dir_open(CONFIG_FILE_DIR, 0, NULL); if(!confdir) { log_debug("No configuration. Creating defaults.\n"); create_conf_file(); return (ret); } if (stat(FS_MOUNT_CONFIG_FILE, &fileinfo) == -1) { /* conf file not created yet, make default and merge all */ create_conf_file(); } /* config file is created, so the dir must exists */ stat(CONFIG_FILE_DIR, &dir); /* st_mtime is changed by file modifications, st_mtime of a directory is changed by the creation or deletion of files in that directory. So if the st_mtime of the config file is equal to the directory time we can be sure the config is untouched and we do not need to re-merge the config. */ if(fileinfo.st_mtime == dir.st_mtime) return 0; log_debug("Merging/creating configuration.\n"); keyfile_string = g_string_new(NULL); /* check each ini file and get contents */ while((filename = g_dir_read_name(confdir)) != NULL) { log_debug("filename = %s\n", filename); filename_full = g_strconcat(CONFIG_FILE_DIR, "/", filename, NULL); if(!strcmp(filename_full, FS_MOUNT_CONFIG_FILE)) { /* store mode info to add it later as we want to keep it */ mode = get_mode_setting(); #ifdef UDEV /* store udev path (especially important for the upgrade path */ udev = find_udev_path(); #endif /* UDEV */ break; } /* load contents of file, if it fails skip to next one */ settingsfile = g_key_file_new(); test = g_key_file_load_from_file(settingsfile, filename_full, G_KEY_FILE_KEEP_COMMENTS, NULL); if(!test) { log_debug("%d failed loading config file %s\n", test, filename_full); g_free(filename_full); break; } g_free(filename_full); log_debug("file data = %s\n", g_key_file_to_data(settingsfile, NULL, NULL)); keyfile_string = g_string_append(keyfile_string, g_key_file_to_data(settingsfile, NULL, NULL)); log_debug("keyfile_string = %s\n", keyfile_string->str); g_key_file_free(settingsfile); } if(keyfile_string) { /* write out merged config file */ /* g_file_set_contents returns 1 on succes, this function returns 0 */ ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile_string->str, -1, NULL); g_string_free(keyfile_string, TRUE); if(mode) { set_mode_setting(mode); } #ifdef UDEV if(udev) { set_config_setting(UDEV_PATH_ENTRY, UDEV_PATH_KEY, udev); } #endif /* UDEV */ } else ret = 1; if(mode) free((void *)mode); #ifdef UDEV if(udev) free((void *)udev); #endif /* UDEV */ g_dir_close(confdir); return(ret); }