fixed mem leak
[ppastats.git] / src / lp_ws.c
index 8e66167..3905a28 100644 (file)
 #include "fcache.h"
 #include "http.h"
 #include "list.h"
-#include "log.h"
 #include "lp_ws.h"
 #include "lp_json.h"
+#include <plog.h>
 #include "ppastats.h"
+#include <ptime.h>
 
 /** Default ws.size value for the getPublishedBinaries request. */
 static const int DEFAULT_WS_SIZE = 150;
@@ -119,7 +120,7 @@ static char *get_last_creation_date(struct bpph **list)
                }
 
        if (last)
-               return time_to_str(last);
+               return time_to_ISO8601_time(&last);
        else
                return NULL;
 }
@@ -308,17 +309,6 @@ const struct distro_series *get_distro_series(const char *url)
        return distro;
 }
 
-char *date_to_str(struct tm tm)
-{
-       char *str;
-
-       str = malloc(4 + 1 + 2 + 1 + 2 + 1);
-
-       strftime(str, 11, "%Y-%m-%d", &tm);
-
-       return str;
-}
-
 /*
   Convert ddts older than 4 weeks to the same JSON representation than
   the LP one.  Newer ddts are not stored in the cache because the data
@@ -347,7 +337,7 @@ static json_object *ddts_to_json_for_cache(struct daily_download_total **ddts)
                d = difftime(tv->tv_sec, t);
 
                if (d > 4 * 7 * 24 * 60 * 60) { /* older than 4 weeks */
-                       date = date_to_str(ddt->date);
+                       date = tm_to_ISO8601_date(&ddt->date);
                        json_object_object_add(j_ddts,
                                               date,
                                               json_object_new_int(ddt->count));
@@ -362,25 +352,13 @@ static json_object *ddts_to_json_for_cache(struct daily_download_total **ddts)
        return j_ddts;
 }
 
-static char *time_t_to_str(time_t t)
-{
-       struct tm *tm;
-       char *str;
-
-       tm = gmtime(&t);
-
-       str = date_to_str(*tm);
-
-       return str;
-}
-
 char *create_ddts_query(const char *binary_url, time_t st)
 {
        char *q;
        char *sdate;
 
        if (st) {
-               sdate = time_t_to_str(st);
+               sdate = time_to_ISO8601_date(&st);
 
                q = malloc(strlen(binary_url)
                           + strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)
@@ -415,10 +393,12 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url)
        key = get_ddts_list_cache_key(binary_url);
 
        content = fcache_get(key);
-       if (content)
+       if (content) {
                json = json_tokener_parse(content);
-       else
+               free(content);
+       } else {
                json = NULL;
+       }
 
        if (json) {
                cached_ddts = json_object_to_daily_download_totals(json);
@@ -448,7 +428,8 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url)
        }
 
        free(key);
-       free(cached_ddts);
+       if (ddts != cached_ddts)
+               free(cached_ddts);
        free(retrieved_ddts);
 
        return ddts;