diff --git a/src/net/downloader.c b/src/net/downloader.c index 1bbe9a2ed07f265581a6e166013abe79b428efbd..aae9cb97e1ac34825e4ef5dccb5616e09c84fe55 100644 --- a/src/net/downloader.c +++ b/src/net/downloader.c @@ -87,8 +87,7 @@ repo_free(struct lkt_repo *const repo) if (!curl_init) curl_global_cleanup(); } - -int +static inline int safe_json_get_string(struct json_object *jobj, const char *key, char *content, const size_t len) { const char *got; @@ -101,7 +100,7 @@ safe_json_get_string(struct json_object *jobj, const char *key, char *content, c return 0; } -int +static inline int safe_json_get_int32(struct json_object *json, const char *key, int32_t *ret) { struct json_object *field; @@ -112,6 +111,28 @@ safe_json_get_int32(struct json_object *json, const char *key, int32_t *ret) return 0; } +static inline long +get_digit_number(long i) +{ + int count = 0; + while (i) { + i /= 10; + ++count; + } + return count; +} + +int +safe_json_get_long(struct json_object *json, const char *key, long *ret) +{ + const int len = get_digit_number(LONG_MAX); + char content[len], *endptr, err; + if (safe_json_get_string(json, key, content, len)) + return 1; + STRTOL(*ret, content, endptr, err); + return err; +} + int repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json) { @@ -347,6 +368,13 @@ repo_download_id_async(struct lkt_repo *const repo, const size_t id) /* Get all the kara, make them unavailable */ +static inline time_t +get_mtime(const char *path) +{ + struct stat statbuf; + return (stat(path, &statbuf) == -1) ? 0 : statbuf.st_mtime; +} + static inline void __handle_got_json(volatile sqlite3 *db, struct lkt_repo *repo, struct json_object *json) { @@ -355,6 +383,7 @@ __handle_got_json(volatile sqlite3 *db, struct lkt_repo *repo, struct json_objec int32_t integer, err; struct kara *kara; char url[URL_MAX_LEN]; + long stamp, timestamp; RETURN_UNLESS(len > 0 && json_object_get_array(json), "Json invalid or array empty", NOTHING); LOG_INFO_SCT("REPO", "Starting to process json for repo %s", repo->name); @@ -380,6 +409,14 @@ __handle_got_json(volatile sqlite3 *db, struct lkt_repo *repo, struct json_objec kara->filename[PATH_MAX - 1] = 0; LOG_INFO_SCT("REPO", "Crafted filename is '%s'", kara->filename); + if (safe_json_get_long(kara_json, "unix_timestamp", ×tamp)) + continue; + stamp = get_mtime(kara->filename); + if (stamp > timestamp) { + LOG_INFO_SCT("REPO", "Ignore kara '%ld', last timestamp was %ld, new is %ld", kara->id, stamp, timestamp); + continue; + } + err |= safe_json_get_string(kara_json, "song_name", kara->mdt.song_name, LEKTOR_TAG_MAX); err |= safe_json_get_string(kara_json, "source_name", kara->mdt.source_name, LEKTOR_TAG_MAX); err |= safe_json_get_string(kara_json, "category", kara->mdt.category, LEKTOR_TAG_MAX);