Create a default config file if it doesn't exist.

This commit is contained in:
Themaister 2013-10-01 11:57:07 +02:00
parent 0c5a47bc63
commit 57cbbde420
4 changed files with 122 additions and 4 deletions

2
file.h
View File

@ -74,6 +74,8 @@ bool path_is_directory(const char *path);
bool path_file_exists(const char *path);
const char *path_get_extension(const char *path);
bool path_mkdir(const char *dir);
// Removes all text after and including the last '.'
char *path_remove_extension(char *path);

View File

@ -19,6 +19,7 @@
#include "boolean.h"
#include <string.h>
#include <time.h>
#include <errno.h>
#include "compat/strl.h"
#include "compat/posix_string.h"
@ -44,6 +45,7 @@
#else
#include <io.h>
#include <fcntl.h>
#include <direct.h>
#include <windows.h>
#endif
#else
@ -564,6 +566,64 @@ void path_resolve_realpath(char *buf, size_t size)
#endif
}
static bool path_mkdir_norecurse(const char *dir)
{
#if (defined(_WIN32) && !defined(_XBOX)) || !defined(RARCH_CONSOLE)
#ifdef _WIN32
int ret = _mkdir(dir);
#else
int ret = mkdir(dir, 0750);
#endif
if (ret < 0 && errno == EEXIST && path_is_directory(dir)) // Don't treat this as an error.
ret = 0;
if (ret < 0)
RARCH_ERR("mkdir(%s) error: %s.\n", dir, strerror(errno));
return ret == 0;
#else
(void)dir;
return false;
#endif
}
bool path_mkdir(const char *dir)
{
const char *target = NULL;
char *basedir = strdup(dir); // Use heap. Real chance of stack overflow if we recurse too hard.
bool ret = true;
if (!basedir)
return false;
path_parent_dir(basedir);
if (!*basedir || !strcmp(basedir, dir))
{
ret = false;
goto end;
}
if (path_is_directory(basedir))
{
target = dir;
ret = path_mkdir_norecurse(dir);
}
else
{
target = basedir;
ret = path_mkdir(basedir);
if (ret)
{
target = dir;
ret = path_mkdir_norecurse(dir);
}
}
end:
if (target && !ret)
RARCH_ERR("Failed to create directory: \"%s\".\n", target);
free(basedir);
return ret;
}
void fill_pathname_resolve_relative(char *out_path, const char *in_refpath, const char *in_path, size_t size)
{
if (path_is_absolute(in_path))

View File

@ -67,6 +67,7 @@ static bool libretro_install_core(const char *path_prefix,
void rarch_make_dir(const char *x, const char *name)
{
// FIXME: This should use path_mkdir() in file_path.c.
RARCH_LOG("Checking directory name %s [%s]\n", name, x);
if (strlen(x) > 0)
{

View File

@ -392,6 +392,28 @@ static config_file_t *open_default_config_file(void)
}
}
// Try to create a new config file.
if (!conf)
{
conf = config_file_new(NULL);
bool saved = false;
if (conf) // Since this is a clean config file, we can safely use config_save_on_exit.
{
fill_pathname_resolve_relative(conf_path, app_path, "retroarch.cfg", sizeof(conf_path));
config_set_bool(conf, "config_save_on_exit", true);
saved = config_file_write(conf, conf_path);
}
if (saved)
RARCH_WARN("Created new config file in: \"%s\".\n", conf_path); // WARN here to make sure user has a good chance of seeing it.
else
{
RARCH_ERR("Failed to create new config file in: \"%s\".\n", conf_path);
config_file_free(conf);
conf = NULL;
}
}
if (conf)
strlcpy(g_extern.config_path, conf_path, sizeof(g_extern.config_path));
#elif !defined(__CELLOS_LV2__) && !defined(_XBOX)
@ -401,9 +423,9 @@ static config_file_t *open_default_config_file(void)
// XDG_CONFIG_HOME falls back to $HOME/.config.
if (xdg)
snprintf(conf_path, sizeof(conf_path), "%s/retroarch/retroarch.cfg", xdg);
fill_pathname_join(conf_path, xdg, "retroarch/retroarch.cfg", sizeof(conf_path));
else if (home)
snprintf(conf_path, sizeof(conf_path), "%s/.config/retroarch/retroarch.cfg", home);
fill_pathname_join(conf_path, home, ".config/retroarch/retroarch.cfg", sizeof(conf_path));
if (xdg || home)
{
@ -414,12 +436,45 @@ static config_file_t *open_default_config_file(void)
// Fallback to $HOME/.retroarch.cfg.
if (!conf && home)
{
snprintf(conf_path, sizeof(conf_path), "%s/.retroarch.cfg", home);
fill_pathname_join(conf_path, home, ".retroarch.cfg", sizeof(conf_path));
RARCH_LOG("Looking for config in: \"%s\".\n", conf_path);
conf = config_file_new(conf_path);
}
// Try this as a last chance ...
// Try to create a new config file.
if (!conf && (home || xdg))
{
// XDG_CONFIG_HOME falls back to $HOME/.config.
if (xdg)
fill_pathname_join(conf_path, xdg, "retroarch/retroarch.cfg", sizeof(conf_path));
else if (home)
fill_pathname_join(conf_path, home, ".config/retroarch/retroarch.cfg", sizeof(conf_path));
char basedir[PATH_MAX];
fill_pathname_basedir(basedir, conf_path, sizeof(basedir));
if (path_mkdir(basedir))
{
conf = config_file_new(NULL);
bool saved = false;
if (conf)
{
config_set_bool(conf, "config_save_on_exit", true); // Since this is a clean config file, we can safely use config_save_on_exit.
saved = config_file_write(conf, conf_path);
}
if (saved)
RARCH_WARN("Created new config file in: \"%s\".\n", conf_path); // WARN here to make sure user has a good chance of seeing it.
else
{
RARCH_ERR("Failed to create new config file in: \"%s\".\n", conf_path);
config_file_free(conf);
conf = NULL;
}
}
}
// Try this as a very last chance ...
if (!conf)
{
#ifndef GLOBAL_CONFIG_DIR