fixed memleak
[ppastats.git] / src / ppastats.c
1 /*
2  * Copyright (C) 2011-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "list.h"
28 #include "lp_ws.h"
29 #include <plog.h>
30 #include "ppastats.h"
31
32 static struct package_stats *get_package_stats(struct ppa_stats *stats,
33                                                const char *name)
34
35 {
36         struct package_stats *p, **p_cur, **tmp;
37
38         p_cur = stats->packages;
39         while (p_cur && *p_cur) {
40                 struct package_stats *p = *p_cur;
41
42                 if (!strcmp(p->name, name))
43                         return p;
44
45                 p_cur++;
46         }
47
48         p = malloc(sizeof(struct package_stats));
49         p->name = strdup(name);
50         p->versions = NULL;
51         p->download_count = 0;
52         p->daily_download_totals = NULL;
53         p->distros = NULL;
54
55         tmp = (struct package_stats **)list_add
56                 ((void **)stats->packages, p);
57         free(stats->packages);
58         stats->packages = tmp;
59
60         return p;
61 }
62
63 static struct version_stats *get_version_stats(struct package_stats *package,
64                                                const char *version)
65 {
66         struct version_stats *v, **cur, **tmp;
67
68         cur = package->versions;
69         while (cur && *cur) {
70                 struct version_stats *v = *cur;
71
72                 if (!strcmp(v->version, version))
73                         return v;
74
75                 cur++;
76         }
77
78         v = malloc(sizeof(struct version_stats));
79         v->version = strdup(version);
80         v->distros = NULL;
81         v->download_count = 0;
82         v->daily_download_totals = NULL;
83
84         tmp = (struct version_stats **)list_add((void **)package->versions,
85                                                 v);
86         free((void **)package->versions);
87         package->versions = tmp;
88
89         return v;
90 }
91
92 static struct distro_stats *distro_stats_new(const char *name)
93 {
94         struct distro_stats *d;
95
96         d = malloc(sizeof(struct distro_stats));
97         d->name = strdup(name);
98         d->archs = NULL;
99         d->download_count = 0;
100         d->ddts = NULL;
101
102         return d;
103 }
104
105 static struct distro_stats *get_distro_stats(struct version_stats *version,
106                                              const char *name)
107 {
108         struct distro_stats **cur = version->distros;
109         struct distro_stats *d;
110         struct distro_stats **tmp;
111
112         while (cur && *cur) {
113                 d = *cur;
114
115                 if (!strcmp(d->name, name))
116                         return d;
117
118                 cur++;
119         }
120
121         d = distro_stats_new(name);
122
123
124         tmp = (struct distro_stats **)list_add((void **)version->distros,
125                                                    d);
126         free(version->distros);
127         version->distros = tmp;
128
129         return d;
130 }
131
132 static struct arch_stats *get_arch_stats(struct distro_stats *distro,
133                                          const char *name)
134 {
135         struct arch_stats **cur = distro->archs;
136         struct arch_stats *a;
137         struct arch_stats **tmp;
138
139         while (cur && *cur) {
140                 a = *cur;
141
142                 if (!strcmp(a->name, name))
143                         return a;
144
145                 cur++;
146         }
147
148         a = malloc(sizeof(struct arch_stats));
149         a->name = strdup(name);
150         a->download_count = 0;
151
152         tmp = (struct arch_stats **)list_add((void **)distro->archs,
153                                              a);
154         free((void **)distro->archs);
155         distro->archs = tmp;
156
157         return a;
158 }
159
160
161 static struct daily_download_total **add_total
162 (struct daily_download_total **totals, struct daily_download_total *total)
163 {
164         struct daily_download_total **cur, **result, *item;
165
166         if (totals) {
167                 cur = totals;
168                 while (*cur) {
169                         item = *cur;
170
171                         if (item->date.tm_year == total->date.tm_year &&
172                             item->date.tm_mon == total->date.tm_mon &&
173                             item->date.tm_mday == total->date.tm_mday) {
174                                 item->count += total->count;
175                                 return totals;
176                         }
177
178                         cur++;
179                 }
180         }
181
182         result = (struct daily_download_total **)list_add((void **)totals,
183                                                           ddt_clone(total));
184         return result;
185 }
186
187 static struct daily_download_total **add_totals
188 (struct daily_download_total **total1, struct daily_download_total **total2)
189 {
190         struct daily_download_total **cur, **result, **tmp;
191
192         result = total1;
193         cur = total2;
194         while (*cur) {
195                 tmp = add_total(result, *cur);
196                 if (result != total1 && result != tmp)
197                         free(result);
198                 result = tmp;
199                 cur++;
200         }
201
202         return result;
203 }
204
205 static void
206 pkg_add_distro(struct package_stats *pkg,
207                const char *distro_name,
208                int distro_count,
209                struct daily_download_total **ddts)
210 {
211         struct distro_stats **pkg_distros, *pkg_distro, **tmp;
212         struct daily_download_total **tmp_ddts;
213
214         pkg_distros = pkg->distros;
215         pkg_distro = NULL;
216
217         if (pkg_distros)
218                 while (*pkg_distros)  {
219                         if (!strcmp((*pkg_distros)->name, distro_name)) {
220                                 pkg_distro = *pkg_distros;
221                                 break;
222                         }
223
224                         pkg_distros++;
225                 }
226
227         if (!pkg_distro) {
228                 pkg_distro = distro_stats_new(distro_name);
229                 tmp = (struct distro_stats **)list_add((void **)pkg->distros,
230                                                        (void *)pkg_distro);
231                 free(pkg->distros);
232                 pkg->distros = tmp;
233         }
234
235         pkg_distro->download_count += distro_count;
236
237         tmp_ddts = add_totals(pkg_distro->ddts, ddts);
238         if (pkg_distro->ddts && pkg_distro->ddts != tmp_ddts)
239                 free(pkg_distro->ddts);
240         pkg_distro->ddts = tmp_ddts;
241 }
242
243 static struct ppa_stats *ppa_stats_new(const char *owner, const char *ppa_name)
244 {
245         struct ppa_stats *ppa;
246
247         ppa = malloc(sizeof(struct ppa_stats));
248         ppa->name = strdup(ppa_name);
249         ppa->owner = strdup(owner);
250         ppa->packages = NULL;
251         ppa->daily_download_totals = NULL;
252         ppa->download_count = 0;
253
254         return ppa;
255 }
256
257 struct ppa_stats *
258 create_ppa_stats(const char *owner,
259                  const char *ppa_name,
260                  const char *package_status,
261                  int ws_size)
262 {
263         struct ppa_stats *ppa;
264         struct bpph **history, **h_cur, *h;
265         char *ppa_url, *pkg_name, *pkg_version;
266         struct package_stats *pkg;
267         struct version_stats *version;
268         const struct distro_series *distro_series;
269         const struct distro_arch_series *arch_series;
270         struct distro_stats *distro;
271         struct arch_stats *arch;
272         int count;
273         struct daily_download_total **totals, **tmp;
274
275         ppa_url = get_archive_url(owner, ppa_name);
276         history = get_bpph_list(ppa_url, package_status, ws_size);
277         free(ppa_url);
278
279         if (!history) {
280                 log_err(_("Failed to retrieve PPA information"));
281                 exit(EXIT_FAILURE);
282         }
283
284         ppa = ppa_stats_new(owner, ppa_name);
285
286         for (h_cur = history; *h_cur; ++h_cur) {
287                 h = *h_cur;
288                 totals = get_daily_download_totals(h->self_link);
289                 if (!totals) {
290                         log_err(_("Failed to retrieve download totals for %s"),
291                                 h->self_link);
292                         continue;
293                 }
294                 count = ddts_get_count(totals);
295                 pkg_name = h->binary_package_name;
296                 pkg_version = h->binary_package_version;
297                 arch_series
298                         = get_distro_arch_series(h->distro_arch_series_link);
299                 distro_series
300                         = get_distro_series(arch_series->distroseries_link);
301
302                 ppa->download_count += count;
303                 tmp = add_totals(ppa->daily_download_totals, totals);
304                 if (ppa->daily_download_totals != tmp)
305                         free(ppa->daily_download_totals);
306                 ppa->daily_download_totals = tmp;
307
308                 pkg = get_package_stats(ppa, pkg_name);
309                 pkg->download_count += count;
310                 tmp = add_totals(pkg->daily_download_totals, totals);
311                 if (pkg->daily_download_totals != tmp)
312                         free(pkg->daily_download_totals);
313                 pkg->daily_download_totals = tmp;
314
315                 version = get_version_stats(pkg, pkg_version);
316                 version->download_count += count;
317                 tmp = add_totals(version->daily_download_totals, totals);
318                 if (version->daily_download_totals != tmp)
319                         free(version->daily_download_totals);
320                 version->daily_download_totals = tmp;
321
322                 distro = get_distro_stats(version, distro_series->name);
323                 distro->download_count += count;
324
325                 arch = get_arch_stats(distro, arch_series->architecture_tag);
326                 arch->download_count += count;
327
328                 pkg_add_distro(pkg, distro_series->name, count, totals);
329
330                 daily_download_total_list_free(totals);
331         }
332
333         bpph_list_free(history);
334
335         return ppa;
336 }
337
338 static void arch_stats_free(struct arch_stats *arch)
339 {
340         free(arch->name);
341         free(arch);
342 }
343
344 static void distro_stats_free(struct distro_stats *distro)
345 {
346         struct arch_stats **archs;
347
348         archs = distro->archs;
349         if (archs) {
350                 while (*archs) {
351                         arch_stats_free(*archs);
352                         archs++;
353                 }
354                 free(distro->archs);
355         }
356
357         daily_download_total_list_free(distro->ddts);
358
359         free(distro->name);
360         free(distro);
361 }
362
363 static void distro_stats_list_free(struct distro_stats **distros)
364 {
365         if (distros) {
366                 while (*distros) {
367                         distro_stats_free(*distros);
368                         distros++;
369                 }
370         }
371 }
372
373 static void version_stats_free(struct version_stats *version)
374 {
375         struct distro_stats **distros;
376
377         distros = version->distros;
378         if (distros) {
379                 while (*distros) {
380                         distro_stats_free(*distros);
381                         distros++;
382                 }
383                 free(version->distros);
384         }
385
386         daily_download_total_list_free(version->daily_download_totals);
387
388         free(version->version);
389         free(version);
390 }
391
392 static void package_stats_free(struct package_stats *package)
393 {
394         struct version_stats **versions;
395
396         versions = package->versions;
397         if (versions) {
398                 while (*versions) {
399                         version_stats_free(*versions);
400                         versions++;
401                 }
402                 free(package->versions);
403         }
404         distro_stats_list_free(package->distros);
405         daily_download_total_list_free(package->daily_download_totals);
406         free(package->name);
407         free(package);
408 }
409
410 void ppa_stats_free(struct ppa_stats *ppastats)
411 {
412         struct package_stats **packages;
413
414         packages = ppastats->packages;
415         if (packages) {
416                 while (*packages) {
417                         package_stats_free(*packages);
418                         packages++;
419                 }
420                 free(ppastats->packages);
421         }
422
423         free(ppastats->owner);
424         free(ppastats->name);
425
426         daily_download_total_list_free(ppastats->daily_download_totals);
427
428         free(ppastats);
429 }