Ejemplo n.º 1
0
set_config_result_t set_mode_setting(const char *mode)
{
  return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
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);
}