1 /* librepo - A library providing (libcURL like) API to downloading repository
2  * Copyright (C) 2012  Tomas Mlcoch
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <assert.h>
25 
26 #include "librepo.h"
27 #include "util.h"
28 #include "result.h"
29 #include "handle_internal.h"
30 #include "result_internal.h"
31 #include "yum.h"
32 #include "repomd.h"
33 
34 LrResult *
lr_result_init(void)35 lr_result_init(void)
36 {
37     return lr_malloc0(sizeof(struct _LrResult));
38 }
39 
40 void
lr_result_clear(LrResult * result)41 lr_result_clear(LrResult *result)
42 {
43     if (!result)
44         return;
45     lr_free(result->destdir);
46     lr_yum_repomd_free(result->yum_repomd);
47     lr_yum_repo_free(result->yum_repo);
48     memset(result, 0, sizeof(struct _LrResult));
49 }
50 
51 void
lr_result_free(LrResult * result)52 lr_result_free(LrResult *result)
53 {
54     if (!result)
55         return;
56     lr_result_clear(result);
57     lr_free(result);
58 }
59 
60 gboolean
lr_result_getinfo(LrResult * result,GError ** err,LrResultInfoOption option,...)61 lr_result_getinfo(LrResult *result,
62                   GError **err,
63                   LrResultInfoOption option,
64                   ...)
65 {
66     gboolean rc = TRUE;
67     va_list arg;
68     GError *tmp_err = NULL;
69 
70     assert(!err || *err == NULL);
71 
72     if (!result) {
73         g_set_error(err, LR_RESULT_ERROR, LRE_BADFUNCARG,
74                     "No result specified");
75         return FALSE;
76     }
77 
78     va_start(arg, option);
79 
80     switch (option) {
81     case LRR_RPMMD_REPO:
82     case LRR_YUM_REPO: {
83         LrYumRepo **repo;
84         repo = va_arg(arg, LrYumRepo **);
85         *repo = result->yum_repo;
86         break;
87     }
88 
89     case LRR_RPMMD_REPOMD:
90     case LRR_YUM_REPOMD: {
91         LrYumRepoMd **repomd = va_arg(arg, LrYumRepoMd **);
92         *repomd = result->yum_repomd;
93         break;
94     }
95 
96     case LRR_RPMMD_TIMESTAMP:
97     case LRR_YUM_TIMESTAMP: {
98         gint64 *ts = va_arg(arg, gint64 *);
99         if (result->yum_repomd) {
100             *ts = lr_yum_repomd_get_highest_timestamp(result->yum_repomd, &tmp_err);
101             if (tmp_err) {
102                 rc = FALSE;
103                 g_propagate_error(err, tmp_err);
104             }
105         } else {
106             *ts = 0;
107             rc = FALSE;
108             g_set_error(err, LR_RESULT_ERROR, LRE_REPOMD,
109                         "No repomd data available - cannot get a timestamp");
110         }
111         break;
112     }
113 
114     default:
115         rc = FALSE;
116         g_set_error(err, LR_RESULT_ERROR, LRE_UNKNOWNOPT,
117                     "Unknown option");
118         break;
119     }
120 
121     va_end(arg);
122     return rc;
123 }
124