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

MODULE: Check magic number for already downloaded karas and fix dl test

parent c91ae025
Branches
Étiquettes
1 requête de fusion!133MODULE: Check magic number for already downloaded karas and fix dl test
...@@ -63,4 +63,8 @@ int metadata_set_file(char *karapath, const char *mkvpropedit); ...@@ -63,4 +63,8 @@ int metadata_set_file(char *karapath, const char *mkvpropedit);
/* Set metadata for files under a given directory by their path. */ /* Set metadata for files under a given directory by their path. */
int metadata_set_directory(const char *kara_dir, const char *mkvpropedit); int metadata_set_directory(const char *kara_dir, const char *mkvpropedit);
/* Returns 0 if the correct magic number is found in the file, anything
* otherwise. The correct magic number is `1A 45 DF A3`. */
int mkv_check_magic(const char *file);
#endif /* __LKT_MKV_H__ */ #endif /* __LKT_MKV_H__ */
...@@ -2,9 +2,13 @@ ...@@ -2,9 +2,13 @@
#include <lektor/common.h> #include <lektor/common.h>
#include <lektor/mkv.h> #include <lektor/mkv.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void void
mdtcat(struct kara_metadata *mdt, char **ret) mdtcat(struct kara_metadata *mdt, char **ret)
...@@ -24,3 +28,24 @@ mdtcat(struct kara_metadata *mdt, char **ret) ...@@ -24,3 +28,24 @@ mdtcat(struct kara_metadata *mdt, char **ret)
mdt->language, mdt->source_name, mdt->song_type, mdt->language, mdt->source_name, mdt->song_type,
mdt->song_number, mdt->song_name, mdt->author_name); mdt->song_number, mdt->song_name, mdt->author_name);
} }
int
mkv_check_magic(const char *file)
{
uint8_t magic[] = { 0x1A, 0x45, 0xDF, 0xA3 };
int fd = open(file, O_RDONLY);
uint8_t buff;
if (fd < 0)
return 1;
int ret = 0;
for (int i = 0; i < 4; ++i) {
ret += (1 != read(fd, &buff, 1));
ret += (buff != magic[i]);
}
close(fd);
return ret;
}
...@@ -245,6 +245,7 @@ __download_kara(const char *url, const char *path, int override) ...@@ -245,6 +245,7 @@ __download_kara(const char *url, const char *path, int override)
char ret = 1; char ret = 1;
errno = 0; errno = 0;
int fd = open(path, O_WRONLY | O_APPEND | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR); int fd = open(path, O_WRONLY | O_APPEND | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
struct curl_slist *headers = NULL;
retest: retest:
if (fd < 0) { if (fd < 0) {
...@@ -276,7 +277,12 @@ retest: ...@@ -276,7 +277,12 @@ retest:
.fd = fd, .fd = fd,
}; };
headers = curl_slist_append(headers, "Accept: video/x-matroska");
headers = curl_slist_append(headers, "Content-Type: video/x-matroska");
/* The file must begin by `1A 45 DF A3` */
curl_handle = curl_easy_init(); curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl_handle, CURLOPT_URL, url); curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, __write_disk); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, __write_disk);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &file); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &file);
...@@ -291,6 +297,7 @@ retest: ...@@ -291,6 +297,7 @@ retest:
err: err:
__clean_file(&file); __clean_file(&file);
curl_easy_cleanup(curl_handle); curl_easy_cleanup(curl_handle);
curl_slist_free_all(headers);
return ret; return ret;
} }
...@@ -381,6 +388,7 @@ __handle_got_json_internal_callback(const char *key, const char *val, int comp, ...@@ -381,6 +388,7 @@ __handle_got_json_internal_callback(const char *key, const char *val, int comp,
long filestamp = 0; long filestamp = 0;
int current_id = 0; int current_id = 0;
int magic_error = 0;
/* Timestamp and presence verification */ /* Timestamp and presence verification */
kara->repo->craft_filename(kara->filename, kara); kara->repo->craft_filename(kara->filename, kara);
...@@ -389,15 +397,15 @@ __handle_got_json_internal_callback(const char *key, const char *val, int comp, ...@@ -389,15 +397,15 @@ __handle_got_json_internal_callback(const char *key, const char *val, int comp,
goto do_it; goto do_it;
} }
/* Override calculated filename if it exists */
size_t db_fp_size = (strlen(kara->database_filepath) + 1) * sizeof(char);
memcpy(kara->filename, kara->database_filepath, db_fp_size);
/* Set filepath for kara */
filestamp = get_mtime(kara->filename); filestamp = get_mtime(kara->filename);
if (!(filestamp > kara->unix_timestamp)) magic_error = mkv_check_magic(kara->filename);
if (filestamp == 0 ||
filestamp <= kara->unix_timestamp ||
magic_error) {
goto do_it; goto do_it;
}
/* Ignore kara */
else { else {
++(kara->ignored_count); ++(kara->ignored_count);
database_update_touch(kara->db, kara->id); database_update_touch(kara->db, kara->id);
...@@ -475,8 +483,7 @@ __worker_update(void *__repo) ...@@ -475,8 +483,7 @@ __worker_update(void *__repo)
GOTO_IF(pthread_mutex_unlock(&(repo->mtx)), "Failed to unlock", end_no_lock); GOTO_IF(pthread_mutex_unlock(&(repo->mtx)), "Failed to unlock", end_no_lock);
char *json; char *json;
LOG_INFO("REPO", "Download kara list from %s (%s), directory is %s", LOG_INFO("REPO", "Download kara list from %s (%s)", repo->name, repo->get_all_json);
repo->name, repo->get_all_json, repo->kara_dir);
if (__json_dl(repo->get_all_json, &json)) { if (__json_dl(repo->get_all_json, &json)) {
LOG_ERROR("REPO", "Failed to get json, possibly no internet connexion or repo is down"); LOG_ERROR("REPO", "Failed to get json, possibly no internet connexion or repo is down");
pthread_exit(NULL); pthread_exit(NULL);
...@@ -606,7 +613,7 @@ __worker_import_favorites(void *__repo) ...@@ -606,7 +613,7 @@ __worker_import_favorites(void *__repo)
GOTO_IF(pthread_mutex_unlock(&(repo->mtx)), "Failed to unlock", end_no_lock); GOTO_IF(pthread_mutex_unlock(&(repo->mtx)), "Failed to unlock", end_no_lock);
char *json; char *json;
LOG_INFO("REPO", "Download favorite lists from %s (%s)", repo->name, repo->get_fav_json); LOG_INFO("REPO", "Download fav lists: %s", repo->get_fav_json);
if (__json_dl(repo->get_fav_json, &json)) { if (__json_dl(repo->get_fav_json, &json)) {
LOG_ERROR("REPO", "Failed to get json, possibly no internet connexion or repo is down"); LOG_ERROR("REPO", "Failed to get json, possibly no internet connexion or repo is down");
pthread_exit(NULL); pthread_exit(NULL);
......
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