diff --git a/inc/lektor/lktmodule.h b/inc/lektor/lktmodule.h index 3ec608e87f7e17b35db82317a8e5fd7bcc0f4ef5..b7db5dc10c3a934301cd69ffd5fb06fafe84dac3 100644 --- a/inc/lektor/lktmodule.h +++ b/inc/lektor/lktmodule.h @@ -15,10 +15,10 @@ /* Include source files, only do it once per module ! */ #if defined(__LKT_MODULE_MAIN_SOURCE__) && ! defined(LKT_STATIC_MODULE) -#include "../../src/base/common.c" -#include "../../src/base/stack.c" -#include "../../src/database/queue.c" -#include "../../src/module/thread.c" + #include "../../src/base/common.c" + #include "../../src/base/stack.c" + #include "../../src/database/queue.c" + #include "../../src/module/thread.c" #endif /* __LKT_MODULE_MAIN_SOURCE__ && ! LKT_STATIC_MODULE*/ #endif /* __LKT_LKTMODULE_H__ */ diff --git a/scripts/astyle.bash b/scripts/astyle.bash index 22a8bd734db3dee27b6eb93def3b6b51b3e10375..ac3e70e98bcf0b6a77df13e7cb8d502238881c90 100755 --- a/scripts/astyle.bash +++ b/scripts/astyle.bash @@ -22,8 +22,8 @@ if [ $# -gt 0 ] && [ "$1" = "--check" ] ; then [ $? -eq 1 ] && exit 0 || exit 1 fi -find . -name '*.c' -exec astyle $OPTIONS --quiet {} \; -find . -name '*.h' -exec astyle $OPTIONS --quiet {} \; -find . -name '*.hpp' -exec astyle $OPTIONS --quiet {} \; -find . -name '*.cpp' -exec astyle $OPTIONS --quiet {} \; -find . -name '*.orig' -exec rm {} \; +find ./src ./inc -name '*.c' -exec astyle $OPTIONS --quiet {} \; +find ./src ./inc -name '*.h' -exec astyle $OPTIONS --quiet {} \; +find ./src ./inc -name '*.hpp' -exec astyle $OPTIONS --quiet {} \; +find ./src ./inc -name '*.cpp' -exec astyle $OPTIONS --quiet {} \; +find ./src ./inc -name '*.orig' -exec rm {} \; diff --git a/src/base/config.c b/src/base/config.c index c417c7d0ca8d0f9e7d3a0dabd616dfc9d09175ad..90e73d665ed5ba2bf3e8ace1008b80acd726f84a 100644 --- a/src/base/config.c +++ b/src/base/config.c @@ -181,9 +181,8 @@ config_default_file(char *dest, size_t len) safe_strncpy(dest, home, len); strncat(dest, "/.config/lektor/lektor.ini", len - 1); LOG_DEBUG("CONFIG", "Use '%s' insted of XDG_CONFIG_HOME", home); - } else { + } else strncat(dest, "/lektor/lektor.ini", len - 1); - } } int diff --git a/src/database/config.c b/src/database/config.c index 86fd2d605dc58eaec6adfd56db04057c101b9c0c..cbd7dbc2b7ce195af472e9fa95a0395dabada73d 100644 --- a/src/database/config.c +++ b/src/database/config.c @@ -59,7 +59,7 @@ database_config_get_text(volatile sqlite3 *db, const char *section, SQLITE_STEP_ROW(db, stmt, error); row = (char *) sqlite3_column_text(stmt, 0); - strncpy(value, row, len); + safe_strncpy(value, row, len); value[len - 1] = 0; ret = true; error: diff --git a/src/main/server.c b/src/main/server.c index c2c6a6040ae75d649a35f465eaf6a69c5b70a420..9bf84a16e0d2ed2ccfe6a8a3993dd2d0f3aaf516 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -19,8 +19,8 @@ #include <sys/stat.h> #if defined (LKT_STATIC_MODULE) -REG_DECLARE(sdl2_reg) -REG_DECLARE(repo_reg) + REG_DECLARE(sdl2_reg) + REG_DECLARE(repo_reg) #endif /* Recursive mkdir, where the last word of the string is a file, not a folder. */ @@ -35,7 +35,7 @@ __mkdir(const char *dir) if (tmp[len - 1] == '/') tmp[len - 1] = 0; for (p = tmp + 1; *p; p++) { - if(*p == '/') { + if (*p == '/') { *p = 0; mkdir(tmp, 00700); *p = '/'; @@ -136,7 +136,7 @@ retry_config: database_config_get_text(srv.db, "server", "port", srv.port, 5); /* Quick check with an explicit error message, to remide the users to create the kara folder */ - if (access(kara_dir, R_OK|W_OK)) { + if (access(kara_dir, R_OK | W_OK)) { LOG_ERROR("INIT", "No access in read / write for folder %s", kara_dir); exit(EXIT_FAILURE); } diff --git a/src/module/module_repo.c b/src/module/module_repo.c index fffc8ff6ba8f76607876c20354b76f23beb7021c..2813d723139c7bee46ae5b492220f739394aa82c 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -231,27 +231,88 @@ err: return ret; } +/* Recursive mkdir, where the last word of the string is a file, not a folder. */ +static inline void +__mkdir(const char *dir) +{ + /* TODO pour le Kubat du futur: include le umask dans la conf. */ + + char tmp[PATH_MAX]; + char *p = NULL; + safe_snprintf(tmp, sizeof(tmp) / sizeof(char), "%s", dir); + size_t len = strlen(tmp); + if (tmp[len - 1] == '/') + tmp[len - 1] = 0; + for (p = tmp + 1; *p; p++) { + if (*p == '/') { + *p = 0; + mkdir(tmp, 00700); + *p = '/'; + } + } + /* Do the final mkdir, because it's a folder. */ + mkdir(tmp, 00700); +} + +/* Obfuscate filename */ +static inline void +__craft_filename_obfuscate(char str[PATH_MAX], size_t len, struct kara *kara) +{ + safe_snprintf(str + len, PATH_MAX - len, "%ld.mkv", kara->id); +} + +/* Not obfuscate filename, need to create directories, won't fail if not possible. + * The program will fail later, when write will be attempted. */ +static inline void +__craft_filename_non_obfuscate(char str[PATH_MAX], size_t len, struct kara *kara) +{ + len += safe_snprintf(str + len, PATH_MAX - len, "%s/%s/%s/", kara->mdt.category, + kara->mdt.language, kara->mdt.author_name); + __mkdir(str); + if (access(str, R_OK | W_OK)) + LOG_ERROR("REPO", "No access in read / write for folder %s", str); + safe_snprintf(str + len, PATH_MAX - len, "%s - %s%d - %s.mkv", kara->mdt.source_name, + kara->mdt.song_type, kara->mdt.song_number, kara->mdt.song_name); +} + +/* Craft a filename: + * - 0 -> without obfuscation + * - 1 -> with obfuscation + * Arguments: + * - str: the destination string, the prefix, i.e. the kara_dir, is already here + * - len: the length of the string, not its size (which is PATH_MAX) + * - kara: the kara structure, which contains all the data to create the filename + * TODO pour le kubat du futur: stocker ça dans un pointeur de fonction dans la + * structure du module repo. */ +static void (*__craft_filename[2])(char str[PATH_MAX], size_t, struct kara *) = { + __craft_filename_non_obfuscate, + __craft_filename_obfuscate, +}; + static inline void __handle_got_json(volatile sqlite3 *db, struct module_repo_internal *repo, struct json_object *json) { - size_t i, ignored_count = 0, update_count = 0, - len = json_object_array_length(json); - struct json_object *kara_json; + size_t ignored_count = 0, update_count = 0, + len = json_object_array_length(json), i; + struct json_object *kara_json = NULL; struct kara kara; - long filestamp = 0, timestamp = 0, download_id; + long filestamp = 0, timestamp = 0, long_integer; char *mkvpropedit = safe_zero_malloc(sizeof(char) * PATH_MAX); char *url = safe_zero_malloc(sizeof(char) * LKT_LINE_MAX); - int current_id, err; + int current_id, err, obfuscation; struct timespec time_sleep = { .tv_sec = 0, .tv_nsec = 100000000L, }; /* Sleep for 0.1s */ + RETURN_UNLESS(database_config_get_int(db, "repo", "obfuscate", &obfuscation), + "Failed to get obfuscation property from config, it should be OK at this point", + NOTHING); + obfuscation = obfuscation ? 1 : 0; /* Safe values */ RETURN_UNLESS(len > 0 && json_object_get_array(json), "Json invalid or array empty", NOTHING); - RETURN_UNLESS(database_config_get_text(db, "externals", "mkvpropedit", - mkvpropedit, PATH_MAX - 1), + RETURN_UNLESS(database_config_get_text(db, "externals", "mkvpropedit", mkvpropedit, PATH_MAX), "Can't get the mkvpropedit executable path", NOTHING); LOG_INFO("REPO", "Starting to process json for repo %s", repo->name); @@ -260,10 +321,9 @@ __handle_got_json(volatile sqlite3 *db, struct module_repo_internal *repo, kara_json = json_object_array_get_idx(json, i); err = 0; - /* Get the id of the kara. */ - if (__safe_json_get_long(kara_json, "id", &download_id)) + if (__safe_json_get_long(kara_json, "id", &long_integer)) continue; - kara.id = download_id; + kara.id = long_integer; /* Craft a fake filepath here, it will be used later. */ size_t kara_dir_len = strlen(repo->kara_dir); @@ -272,7 +332,23 @@ __handle_got_json(volatile sqlite3 *db, struct module_repo_internal *repo, strncat(kara.filename, "/", PATH_MAX - 1); kara.filename[++kara_dir_len] = 0; } - safe_snprintf(kara.filename + kara_dir_len, PATH_MAX - kara_dir_len, "%ld.mkv", download_id); + + /* Reads the json, needed in case of download without obfuscation */ +#define __get_string(field, json_field) \ + err |= __safe_json_get_string(kara_json, #field, kara.mdt.json_field, LEKTOR_TAG_MAX) + __get_string(song_name, song_name); + __get_string(source_name, source_name); + __get_string(category, category); + __get_string(language, language); + __get_string(author_name, author_name); + __get_string(song_type, song_type); +#undef __get_string + if (err || __safe_json_get_long(kara_json, "song_number", &long_integer)) { + LOG_WARN("REPO", "Json is invalid for kara '%ld', skip it", kara.id); + continue; + } + kara.mdt.song_number = long_integer; + __craft_filename[obfuscation](kara.filename, kara_dir_len, &kara); /* Timestamp and presence verification */ if (!database_get_kara_path(db, kara.id, NULL)) @@ -286,39 +362,20 @@ __handle_got_json(volatile sqlite3 *db, struct module_repo_internal *repo, ++ignored_count; database_update_touch(db, kara.id); database_update_set_available(db, kara.id); - LOG_DEBUG("REPO", "Ignore kara '%ld' with path '%s'", - kara.id, kara.filename); + LOG_DEBUG("REPO", "Ignore kara '%ld' with path '%s'", kara.id, kara.filename); continue; } do_it: - /* Reads the json */ -#define __get_string(field, json_field) \ - err |= __safe_json_get_string(kara_json, #field, kara.mdt.json_field, LEKTOR_TAG_MAX) - __get_string(song_name, song_name); - __get_string(source_name, source_name); - __get_string(category, category); - __get_string(language, language); - __get_string(author_name, author_name); - __get_string(song_type, song_type); -#undef __get_string - if (err || __safe_json_get_long(kara_json, "song_number", &download_id)) { - LOG_WARN("REPO", "Json is invalid for kara '%ld', skip it", kara.id); - continue; - } - kara.mdt.song_number = download_id; - current_id = 0; database_queue_current_kara(db, NULL, ¤t_id); if (current_id == (int) kara.id) { - LOG_WARN("REPO", "Update currently playing kara %d, skip it", - current_id); + LOG_WARN("REPO", "Update currently playing kara %d, skip it", current_id); lkt_queue_send(repo->queue, lkt_event_skip_current, NULL); } if (!database_update_add(db, kara.filename, &kara.mdt, kara.id, false)) { - LOG_ERROR("REPO", "Could not add unavailable kara %ld to db", - kara.id); + LOG_ERROR("REPO", "Could not add unavailable kara %ld to db", kara.id); continue; } @@ -331,8 +388,8 @@ __handle_got_json(volatile sqlite3 *db, struct module_repo_internal *repo, } if (kara_metadata_write(&kara.mdt, kara.filename, mkvpropedit)) { - LOG_WARN("REPO", "Could not write metadata to kara '%ld' with " - "path '%s'", kara.id, kara.filename); + LOG_WARN("REPO", "Could not write metadata to kara '%ld' with path '%s'", + kara.id, kara.filename); continue; }