Skip to content
Extraits de code Groupes Projets

Add the query to the `update` command to filter karas to update

Fusionnées Kubat a demandé de fusionner dev-kubat vers master
1 file
+ 90
84
Comparer les modifications
  • Côte à côte
  • En ligne
+ 90
84
@@ -3,6 +3,7 @@
@@ -3,6 +3,7 @@
#include <lektor/database.h>
#include <lektor/database.h>
#include <lektor/net.h>
#include <lektor/net.h>
#include <lektor/reg.h>
#include <lektor/reg.h>
 
#include <lektor/lib/strv.h>
#include <dlfcn.h>
#include <dlfcn.h>
#include <pwd.h>
#include <pwd.h>
@@ -20,62 +21,57 @@ PRIVATE_FUNCTION int config_new(lkt_db *db, const char *conf);
@@ -20,62 +21,57 @@ PRIVATE_FUNCTION int config_new(lkt_db *db, const char *conf);
/* Get the default user config file, the ~/.config/lektor/lektor.ini file. */
/* Get the default user config file, the ~/.config/lektor/lektor.ini file. */
PRIVATE_FUNCTION void config_default_file(char *dest, size_t len);
PRIVATE_FUNCTION void config_default_file(char *dest, size_t len);
static inline char *
strip(char *s)
{
char *p = s + strlen(s);
while (p > s && isspace(*(--p)))
*p = 0;
return s;
}
static inline char *
skip(char *s)
{
while (*s && isspace(*s))
s++;
return s;
}
PRIVATE_FUNCTION int
PRIVATE_FUNCTION int
handler(lkt_db *user, const char *section, const char *name, const char *value)
handler(lkt_db *user, const char *section, const struct strv name, const struct strv value)
{
{
RETURN_UNLESS(section && name && value, "Skip incomplete line", 1);
RETURN_UNLESS(section && strv_len(name) && strv_len(value), "Skip incomplete line", 1);
RETURN_UNLESS(database_config_set(user, section, name, value), "Failed to update config", 1);
RETURN_UNLESS(database_config_set_strv_1_2(user, section, name, value),
 
"Failed to update config", 1);
return 0;
return 0;
}
}
PRIVATE_FUNCTION void
PRIVATE_FUNCTION void
___set_log_level_internal(const char *level)
___set_log_level_internal(const struct strv level)
{
{
if (!level[0]) {
static UNUSED struct strv error_strv = STRV_STATIC("error");
 
static UNUSED struct strv warning_strv = STRV_STATIC("warning");
 
static UNUSED struct strv warn_strv = STRV_STATIC("warn");
 
static UNUSED struct strv debug_strv = STRV_STATIC("debug");
 
static UNUSED struct strv info_strv = STRV_STATIC("info");
 
 
if (strv_len(level) == 0) {
LOG_WARN("CONFIG", "Invalid empty 'log' option");
LOG_WARN("CONFIG", "Invalid empty 'log' option");
return;
return;
}
}
if (STR_MATCH(level, "error"))
if (strv_equal_nocase(level, error_strv))
lkt_set_log_level(LOG_LEVEL_ERROR);
lkt_set_log_level(LOG_LEVEL_ERROR);
else if (STR_MATCH(level, "warn") || STR_MATCH(level, "warning"))
else if (strv_equal_nocase(level, warn_strv))
lkt_set_log_level(LOG_LEVEL_WARN);
lkt_set_log_level(LOG_LEVEL_WARN);
else if (STR_MATCH(level, "info"))
else if (strv_equal_nocase(level, warning_strv))
 
lkt_set_log_level(LOG_LEVEL_WARN);
 
else if (strv_equal_nocase(level, info_strv))
lkt_set_log_level(LOG_LEVEL_INFO);
lkt_set_log_level(LOG_LEVEL_INFO);
else if (STR_MATCH(level, "debug"))
else if (strv_equal_nocase(level, debug_strv))
lkt_set_log_level(LOG_LEVEL_DEBUG);
lkt_set_log_level(LOG_LEVEL_DEBUG);
else
else
lkt_set_log_level((LOG_LEVEL)(int)strtol(level, NULL, 0));
lkt_set_log_level((LOG_LEVEL)(int)strv_to_int(level));
LOG_INFO("CONFIG", "Log level set to %d", lkt_get_log_level());
LOG_INFO("CONFIG", "Log level set to %d", lkt_get_log_level());
}
}
PRIVATE_FUNCTION void
PRIVATE_FUNCTION void
___set_log_level(const char *name, const char *level)
___set_log_level(const struct strv name, const struct strv level)
{
{
 
static const struct strv log_strv = STRV_STATIC("log");
 
/* Just check the key name here */
/* Just check the key name here */
if (!STR_MATCH(name, "log")) {
if (strv_equal_nocase(name, log_strv)) {
LOG_WARN("CONFIG", "Invalid option '%s[:=]%s' with no section", name, level);
___set_log_level_internal(level);
return;
} else {
 
LOG_WARN("CONFIG", "Invalid option '" STRV_FMT "=" STRV_FMT "' with no section",
 
STRV_ARG(name), STRV_ARG(level));
}
}
___set_log_level_internal(level);
}
}
PRIVATE_FUNCTION void
PRIVATE_FUNCTION void
@@ -89,16 +85,21 @@ ___apply_log_level(lkt_db *db)
@@ -89,16 +85,21 @@ ___apply_log_level(lkt_db *db)
database_config_set(db, "log", "level", loglevel);
database_config_set(db, "log", "level", loglevel);
} else {
} else {
/* 'log/level' is present, use that one */
/* 'log/level' is present, use that one */
___set_log_level_internal(loglevel);
___set_log_level_internal(strv_from_str(loglevel));
}
}
}
}
 
PRIVATE_FUNCTION bool
 
___char_is_not_comment_delim(char x)
 
{
 
return !(x == ';' || x == '#');
 
}
 
PRIVATE_FUNCTION int
PRIVATE_FUNCTION int
ini_parse(const char *path, lkt_db *db)
ini_parse(const char *path, lkt_db *db)
{
{
char *start, *end, *name, *value;
char section[LKT_LINE_MAX], file_line[LKT_LINE_MAX];
char section[LKT_LINE_MAX], line[LKT_LINE_MAX];
int error = 0, linenum = 0;
int error = 0, linenum = 0, len;
FILE *file = fopen(path, "r");
FILE *file = fopen(path, "r");
if (!file) {
if (!file) {
LOG_ERROR("PARSER", "Failed to open config file '%s'", path);
LOG_ERROR("PARSER", "Failed to open config file '%s'", path);
@@ -106,71 +107,76 @@ ini_parse(const char *path, lkt_db *db)
@@ -106,71 +107,76 @@ ini_parse(const char *path, lkt_db *db)
}
}
memset(section, 0, LKT_LINE_MAX);
memset(section, 0, LKT_LINE_MAX);
memset(line, 0, LKT_LINE_MAX);
memset(file_line, 0, LKT_LINE_MAX);
 
 
static struct strv comment_prefix_1 = STRV_STATIC(";");
 
static struct strv comment_prefix_2 = STRV_STATIC("#");
 
static struct strv bracket_left_strv = STRV_STATIC("[");
 
static struct strv bracket_right_strv = STRV_STATIC("]");
LOG_INFO("CONFIG", "Save config to database from file");
LOG_INFO("CONFIG", "Save config to database from file");
/* Parse the file */
/* Parse the file */
while (NULL != fgets(line, LKT_LINE_MAX, file)) {
while (NULL != fgets(file_line, LKT_LINE_MAX, file)) {
++linenum;
++linenum;
start = skip(strip(line));
struct strv line = strv_trim(strv_from_str(file_line));
/* Skip comments */
/* Skip comments */
if (strspn(start, ";#"))
if (strv_starts_with(line, comment_prefix_1) || strv_starts_with(line, comment_prefix_2))
continue;
continue;
/* Handle sections */
/* Handle sections */
else if (start[0] == '[') {
else if (strv_starts_with(line, bracket_left_strv)) {
end = &start[1 + strcspn(start + 1, "]")];
if (strv_ends_with(line, bracket_right_strv)) {
if (end[0] == ']') {
strv_chop_left(&line, 1);
end[0] = '\0';
strv_chop_right(&line, 1);
len = (int)strlen(&start[1]);
if (!strv_as_str(line, section, LKT_LINE_MAX)) {
len = MIN(len + 1, LKT_LINE_MAX);
error = 1;
memcpy(section, &start[1], (size_t)len);
LOG_ERROR("PARSER", "Failed to copy the section name at line '%d'", linenum);
section[LKT_LINE_MAX - 1] = '\0';
}
}
} else {
else {
error = 1;
error = 1;
LOG_ERROR("PARSER", "Invalid section name at line '%d'", linenum);
LOG_ERROR("PARSER", "Invalid section name at line '%d'", linenum);
}
}
}
}
/* Handle name[:=]name pair */
/* Handle empty lines */
else if (start[0]) {
else if (strv_len(line) == 0) {
end = &start[1 + strcspn(start + 1, ":=")];
continue;
if (end[0] == '=' || end[0] == ':') {
}
end[0] = '\0';
name = strip(start);
/* Handle name=name pair */
value = &end[1];
else {
int separator_index = -1;
/* Find a comment */
if (!strv_index_of(line, '=', &separator_index)) {
end = &value[strcspn(value, ";#")];
LOG_ERROR("PARSER", "Invalid line '%d', no (key, value) pair found", linenum);
if (end[0])
continue;
end[0] = '\0';
}
/* Skip all spaces */
const struct strv key = strv_trim(strv_chop_left(&line, separator_index));
value = skip(value);
strv_chop_left(&line, 1); /* Skip the "=" character */
strip(value);
const struct strv value = strv_trim(strv_take_left_while(line, ___char_is_not_comment_delim));
/* Handle the SECTION, NAME[:=]VALUE
/* Handle the (key, value) pair */
The only option that has no SECTION is the log level:
if (strv_len(value) == 0 || strv_len(key) == 0) {
log[:=]ERROR|WARN|INFO|DEBUG|\d+ */
LOG_ERROR("PARSER", "Invalid key pair at line '%d'", linenum);
if (section[0]) {
LOG_ERROR_IF(strv_len(key), "PARSER", "The key was: '"STRV_FMT"'", STRV_ARG(key));
if (handler(db, section, name, value)) {
LOG_ERROR_IF(strv_len(value), "PARSER", "The value was: '"STRV_FMT"'", STRV_ARG(value));
error = 1;
}
LOG_ERROR("PARSER", "Failed to '[handle] %s, %s{:,=}%s' at line '%d'",
section, name, value, linenum);
else if (section[0]) {
}
LOG_DEBUG("PARSER", "Found line: [%s] '"STRV_FMT"' = '"STRV_FMT"'", section, STRV_ARG(key), STRV_ARG(value));
} else {
if (handler(db, section, key, value)) {
/* Keep for legacy reasons */
error = 1;
___set_log_level(name, value);
LOG_ERROR("PARSER",
 
"Failed to '[handle] %s, " STRV_FMT "=" STRV_FMT "' at line '%d'",
 
section, STRV_ARG(key), STRV_ARG(value), linenum);
}
}
}
}
else {
else {
error = 1;
/* Keep for legacy reasons */
LOG_ERROR("PARSER", "Invalid name[:=]value pair at line '%d'", linenum);
___set_log_level(key, value);
}
}
}
}
}
}
@@ -305,7 +311,7 @@ config_default(FILE *output)
@@ -305,7 +311,7 @@ config_default(FILE *output)
/* Recursive mkdir, where the last word of the string is a file, not a folder. */
/* Recursive mkdir, where the last word of the string is a file, not a folder. */
PRIVATE_FUNCTION void
PRIVATE_FUNCTION void
__mkdir(const char *dir)
___mkdir(const char *dir)
{
{
char tmp[PATH_MAX];
char tmp[PATH_MAX];
char *p = NULL;
char *p = NULL;
@@ -336,7 +342,7 @@ retry_config:
@@ -336,7 +342,7 @@ retry_config:
LOG_INFO("INIT", "Creating default config file");
LOG_INFO("INIT", "Creating default config file");
config_default_file(conf_file, conf_len);
config_default_file(conf_file, conf_len);
__mkdir(conf_file); /* Create the folder for the file. */
___mkdir(conf_file); /* Create the folder for the file. */
FILE *file_desc = fopen(conf_file, "w+");
FILE *file_desc = fopen(conf_file, "w+");
RETURN_UNLESS(file_desc, "Failed to open default config file to initialize it", 1);
RETURN_UNLESS(file_desc, "Failed to open default config file to initialize it", 1);
Chargement en cours