Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 6d5b096d rédigé par Kubat's avatar Kubat
Parcourir les fichiers

CONF: Write default config file if it doesn't exists

parent f82b5c40
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!117Resolve "Config file created by lektord if needed"
Pipeline #1897 réussi
...@@ -37,6 +37,9 @@ int config_new(volatile sqlite3 *db, const char *conf); ...@@ -37,6 +37,9 @@ int config_new(volatile sqlite3 *db, const char *conf);
/* Prints the default config file. */ /* Prints the default config file. */
void config_default(FILE *output); void config_default(FILE *output);
/* Get the default user config file, the ~/.config/lektor/lektor.ini file. */
void config_default_file(char *dest, size_t len);
/* Manipulate Environment Variables */ /* Manipulate Environment Variables */
#define env_set(var, val) setenv(var, val, 1) #define env_set(var, val) setenv(var, val, 1)
#define env_get getenv #define env_get getenv
......
...@@ -164,6 +164,25 @@ ini_parse(const char *path, volatile sqlite3 *db) ...@@ -164,6 +164,25 @@ ini_parse(const char *path, volatile sqlite3 *db)
return error; return error;
} }
void
config_default_file(char *dest, size_t len)
{
/* TODO: Use ~/.config/baka and not ~/.config/lektor. */
/* First try the XDG_CONFIG_HOME variable, else the default location HOME/.config. */
memset(dest, 0, len * sizeof(char));
char *home = getenv("XDG_CONFIG_HOME");
if (NULL == home || strlen(home) >= len) {
home = getenv("HOME");
if (NULL == home) {
LOG_ERROR("FATAL", "Failed to get home folder for user, will now exit");
exit(EXIT_FAILURE);
}
strncat(dest, "/.config/lektor/lektor.ini", len - 1);
} else {
strncat(dest, "/lektor/lektor.ini", len - 1);
}
}
int int
config_detect_file(char *conf, size_t conf_len) config_detect_file(char *conf, size_t conf_len)
{ {
......
...@@ -16,12 +16,36 @@ ...@@ -16,12 +16,36 @@
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <pthread.h> #include <pthread.h>
#include <sys/stat.h>
#if defined (LKT_STATIC_MODULE) #if defined (LKT_STATIC_MODULE)
REG_DECLARE(sdl2_reg) REG_DECLARE(sdl2_reg)
REG_DECLARE(repo_reg) REG_DECLARE(repo_reg)
#endif #endif
/* Recursive mkdir, where the last word of the string is a file, not a folder. */
static inline void
__mkdir(const char *dir)
{
char tmp[PATH_MAX];
char *p = NULL;
safe_snprintf(tmp, sizeof(tmp), "%s", dir);
size_t len = strlen(tmp);
/* In our case, the final word is always a file, not a folder. */
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++) {
if(*p == '/') {
*p = 0;
mkdir(tmp, 00700);
*p = '/';
}
}
/* Don't do final mkdir here, because in our case the final word in the string
* is a file, not a folder.
* mkdir(tmp, S_IRWXU); */
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
...@@ -62,9 +86,7 @@ main(int argc, char *argv[]) ...@@ -62,9 +86,7 @@ main(int argc, char *argv[])
/* Init the server */ /* Init the server */
char *db_path = safe_malloc(PATH_MAX * sizeof(char)); char *db_path = safe_malloc(PATH_MAX * sizeof(char));
char *kara_dir = safe_malloc(PATH_MAX * sizeof(char)); char *kara_dir = safe_malloc(PATH_MAX * sizeof(char));
struct lkt_state srv = { struct lkt_state srv = { .kara_prefix = kara_dir };
.kara_prefix = kara_dir,
};
if (lkt_queue_new(&srv.queue)) { if (lkt_queue_new(&srv.queue)) {
LOG_ERROR("INIT", "Faield to create server queue"); LOG_ERROR("INIT", "Faield to create server queue");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -76,9 +98,28 @@ main(int argc, char *argv[]) ...@@ -76,9 +98,28 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Read or create default config file. */
int retry_config_once = 0;
retry_config:
if (conf_file[0] == '\0' && config_detect_file(conf_file, PATH_MAX)) { if (conf_file[0] == '\0' && config_detect_file(conf_file, PATH_MAX)) {
if (retry_config_once) {
LOG_ERROR("INIT", "Failed to find a config file"); LOG_ERROR("INIT", "Failed to find a config file");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else {
LOG_INFO("INIT", "Creating default config file");
config_default_file(conf_file, PATH_MAX);
__mkdir(conf_file); /* Create the folder for the file. */
FILE *file_desc = fopen(conf_file, "w+");
if (NULL == file_desc) {
LOG_ERROR("FATAL", "Failed to open default config fiel for initializing it");
exit(EXIT_FAILURE);
}
config_default(file_desc);
fclose(file_desc);
LOG_INFO("INIT", "Default configuration file has been writen to %s", conf_file);
retry_config_once = 1;
goto retry_config;
}
} }
if (config_new(srv.db, conf_file)) { if (config_new(srv.db, conf_file)) {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter