1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * Copyright (C) 2012-2014 Red Hat, Inc.
4  * Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
5  *
6  * Licensed under the GNU Lesser General Public License Version 2.1
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or(at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22 
23 /**
24  * SECTION:dnf-packagedelta
25  * @short_description: Package delta
26  * @include: libdnf.h
27  * @stability: Unstable
28  *
29  * An object representing a package delta.
30  *
31  * See also: #DnfContext
32  */
33 
34 
35 #include <solv/repodata.h>
36 #include <solv/util.h>
37 
38 #include "dnf-packagedelta-private.hpp"
39 #include "hy-iutil-private.hpp"
40 
41 typedef struct
42 {
43     char            *location;
44     char            *baseurl;
45     guint64          downloadsize;
46     int              checksum_type;
47     unsigned char   *checksum;
48 } DnfPackageDeltaPrivate;
49 
G_DEFINE_TYPE_WITH_PRIVATE(DnfPackageDelta,dnf_packagedelta,G_TYPE_OBJECT)50 G_DEFINE_TYPE_WITH_PRIVATE(DnfPackageDelta, dnf_packagedelta, G_TYPE_OBJECT)
51 #define GET_PRIVATE(o) (static_cast<DnfPackageDeltaPrivate *>(dnf_packagedelta_get_instance_private (o)))
52 
53 /**
54  * dnf_packagedelta_finalize:
55  **/
56 static void
57 dnf_packagedelta_finalize(GObject *object)
58 {
59     DnfPackageDelta *packagedelta = DNF_PACKAGEDELTA(object);
60     DnfPackageDeltaPrivate *priv = GET_PRIVATE(packagedelta);
61 
62     g_free(priv->location);
63     g_free(priv->baseurl);
64     g_free(priv->checksum);
65 
66     G_OBJECT_CLASS(dnf_packagedelta_parent_class)->finalize(object);
67 }
68 
69 /**
70  * dnf_packagedelta_init:
71  **/
72 static void
dnf_packagedelta_init(DnfPackageDelta * packagedelta)73 dnf_packagedelta_init(DnfPackageDelta *packagedelta)
74 {
75 }
76 
77 /**
78  * dnf_packagedelta_class_init:
79  **/
80 static void
dnf_packagedelta_class_init(DnfPackageDeltaClass * klass)81 dnf_packagedelta_class_init(DnfPackageDeltaClass *klass)
82 {
83     GObjectClass *object_class = G_OBJECT_CLASS(klass);
84     object_class->finalize = dnf_packagedelta_finalize;
85 }
86 
87 /**
88  * dnf_packagedelta_new:
89  *
90  * Creates a new #DnfPackageDelta.
91  *
92  * Returns:(transfer full): a #DnfPackageDelta
93  *
94  * Since: 0.7.0
95  **/
96 DnfPackageDelta *
dnf_packagedelta_new(Pool * pool)97 dnf_packagedelta_new(Pool *pool)
98 {
99     Id checksum_type;
100 
101     auto delta = DNF_PACKAGEDELTA(g_object_new(DNF_TYPE_PACKAGEDELTA, NULL));
102     auto priv = GET_PRIVATE(delta);
103 
104     /* obtain info */
105     priv->location = g_strdup(pool_lookup_deltalocation(pool, SOLVID_POS, 0));
106     priv->baseurl = g_strdup(pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_BASE));
107     priv->downloadsize = pool_lookup_num(pool, SOLVID_POS, DELTA_DOWNLOADSIZE, 0);
108     auto checksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &checksum_type);
109     if (checksum) {
110         priv->checksum_type = checksumt_l2h(checksum_type);
111         priv->checksum = static_cast<unsigned char *>(
112             solv_memdup((void*)checksum, checksum_type2length(priv->checksum_type)));
113     }
114 
115     return delta;
116 }
117 
118 /**
119  * dnf_packagedelta_get_location:
120  * @delta: a #DnfPackageDelta instance.
121  *
122  * Gets the delta location.
123  *
124  * Returns: location URL
125  *
126  * Since: 0.7.0
127  */
128 const char *
dnf_packagedelta_get_location(DnfPackageDelta * delta)129 dnf_packagedelta_get_location(DnfPackageDelta *delta)
130 {
131     DnfPackageDeltaPrivate *priv = GET_PRIVATE(delta);
132     return priv->location;
133 }
134 
135 /**
136  * dnf_packagedelta_get_baseurl:
137  * @delta: a #DnfPackageDelta instance.
138  *
139  * Gets the delta baseurl.
140  *
141  * Returns: string URL
142  *
143  * Since: 0.7.0
144  */
145 const char *
dnf_packagedelta_get_baseurl(DnfPackageDelta * delta)146 dnf_packagedelta_get_baseurl(DnfPackageDelta *delta)
147 {
148     DnfPackageDeltaPrivate *priv = GET_PRIVATE(delta);
149     return priv->baseurl;
150 }
151 
152 /**
153  * dnf_packagedelta_get_downloadsize:
154  * @delta: a #DnfPackageDelta instance.
155  *
156  * Gets the delta download size.
157  *
158  * Returns: size in bytes
159  *
160  * Since: 0.7.0
161  */
162 guint64
dnf_packagedelta_get_downloadsize(DnfPackageDelta * delta)163 dnf_packagedelta_get_downloadsize(DnfPackageDelta *delta)
164 {
165     DnfPackageDeltaPrivate *priv = GET_PRIVATE(delta);
166     return priv->downloadsize;
167 }
168 
169 /**
170  * dnf_packagedelta_get_chksum:
171  * @delta: a #DnfPackageDelta instance.
172  * @type: the checksum type
173  *
174  * Returns the checksum of the delta.
175  *
176  * Returns: checksum data
177  *
178  * Since: 0.7.0
179  */
180 const unsigned char *
dnf_packagedelta_get_chksum(DnfPackageDelta * delta,int * type)181 dnf_packagedelta_get_chksum(DnfPackageDelta *delta, int *type)
182 {
183     DnfPackageDeltaPrivate *priv = GET_PRIVATE(delta);
184     if (type)
185         *type = priv->checksum_type;
186     return priv->checksum;
187 }
188