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

MODULE: First try of obfuscate / non obfuscate download

parent 621c9cba
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!119Download obfuscation
......@@ -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 {} \;
......@@ -181,10 +181,9 @@ 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
config_detect_file(char *conf, size_t conf_len)
......
......@@ -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:
......
......@@ -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, &current_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;
}
......
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