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 #ifndef __LR_PACKAGE_DOWNLOADER_H__
22 #define __LR_PACKAGE_DOWNLOADER_H__
23 
24 #include <glib.h>
25 
26 #include "rcodes.h"
27 #include "handle.h"
28 #include "checksum.h"
29 
30 G_BEGIN_DECLS
31 
32 /** \defgroup   package_downloader    Package downloading
33  *  \addtogroup package_downloader
34  *  @{
35  */
36 
37 /** Download package from repository.
38  * @param handle            Librepo handle.
39  * @param relative_url      Relative part of url.
40  * @param err               GError **
41  * @return                  See ::lr_download_package
42  */
43 #define lr_download_simple(handle, relative_url, err) \
44                     lr_download_package((handle), (relative_url), NULL, 0, \
45                                         NULL, 0, NULL, 0, (err))
46 
47 /** Download package from repository or base_url.
48  * Note: If resume, checksum and checksum_type are specified and
49  * the downloaded package already exists and checksum matches, then
50  * no downloading is done and LRE_ALREADYDOWNLOADED return code is returned.
51  * @param handle            Librepo handle.
52  * @param relative_url      Relative part of url.
53  * @param dest              Destination file, directory
54  *                          or NULL (current working dir is used).
55  * @param checksum_type     Type of checksum.
56  * @param checksum          Checksum value or NULL.
57  * @param expectedsize      Expected size of target. If >0 and server reports
58  *                          different size, then no download is performed.
59  * @param base_url          If specified, mirrors from handle are ignored
60  *                          and this base_url is used for downloading.
61  * @param resume            If TRUE try to resume downloading if dest file
62  *                          already exists.
63  * @param err               GError **
64  * @return                  TRUE if everything is ok, FALSE if err is set.
65  */
66 gboolean
67 lr_download_package(LrHandle *handle,
68                     const char *relative_url,
69                     const char *dest,
70                     LrChecksumType checksum_type,
71                     const char *checksum,
72                     gint64 expectedsize,
73                     const char *base_url,
74                     gboolean resume,
75                     GError **err);
76 
77 /** LrPackageTarget structure */
78 typedef struct {
79 
80     LrHandle *handle; /*!<
81         Related handle */
82 
83     char *relative_url; /*!<
84         Relative part of URL */
85 
86     char *dest; /*!<
87         Destination: filename, dirname or NULL */
88 
89     char *base_url; /*!<
90         Base URL for this target */
91 
92     LrChecksumType checksum_type; /*!<
93         Checksum type */
94 
95     char *checksum; /*!<
96         Expected checksum value */
97 
98     gint64 expectedsize; /*!<
99         Expected size of the target */
100 
101     gboolean resume; /*!<
102         Indicate if resume is enabled */
103 
104     LrProgressCb progresscb; /*!<
105         Progress callback */
106 
107     void *cbdata; /*!<
108         Callback data */
109 
110     LrEndCb endcb; /*!<
111         Callback called when target transfer is done.
112         (Use status to check if successfully or unsuccessfully) */
113 
114     LrMirrorFailureCb mirrorfailurecb; /*!<
115         Called when download from a mirror failed. */
116 
117     gint64 byterangestart; /*!<
118         Download only specified range of bytes. */
119 
120     gint64 byterangeend; /*!<
121         Download only specified range of bytes. */
122 
123     // Will be filled by ::lr_download_packages()
124 
125     char *local_path; /*!<
126         Local path */
127 
128     char *err; /*!<
129         Error message or NULL. NULL means no error. */
130 
131     GStringChunk *chunk; /*!<
132         String chunk */
133 
134 } LrPackageTarget;
135 
136 /** Create new LrPackageTarget object.
137  * @param handle            Handle related to this download or NULL.
138  * @param relative_url      Relative part of URL to download.
139  *                          First part of URL will be picked from the LrHandle
140  *                          (LRO_URL or mirror) during download process or
141  *                          base_url will be used if it is specified. It is
142  *                          expected to already come URL-encoded.
143  * @param dest              Destination filename or just directory (filename
144  *                          itself will be derived from the relative_url) or
145  *                          NULL (current working directory + filename derived
146  *                          from relative_url will be used).
147  * @param checksum_type     Type of checksum or LR_CHECKSUM_UNKNOWN.
148  * @param checksum          Expected checksum value or NULL.
149  * @param base_url          Base URL or NULL
150  * @param expectedsize      Expected size of the target. If server reports
151  *                          different size, then download won't be performed.
152  * @param resume            If TRUE, then downloader will try to resume download
153  *                          if the destination file exists. If the file doesn't
154  *                          exist it will be downloaded.
155  * @param progresscb        Progress callback for this transfer.
156  * @param cbdata            User data for the callback
157  * @param err               GError **
158  * @return                  Newly allocated LrPackageTarget or NULL on error
159  */
160 LrPackageTarget *
161 lr_packagetarget_new(LrHandle *handle,
162                      const char *relative_url,
163                      const char *dest,
164                      LrChecksumType checksum_type,
165                      const char *checksum,
166                      gint64 expectedsize,
167                      const char *base_url,
168                      gboolean resume,
169                      LrProgressCb progresscb,
170                      void *cbdata,
171                      GError **err);
172 
173 /** Create new LrPackageTarget object.
174  * Almost same as lr_packagetarget_new() except this function
175  * could set more callbacks.
176  * @param handle            Handle related to this download or NULL.
177  * @param relative_url      Relative part of URL to download.
178  *                          First part of URL will be picked from the LrHandle
179  *                          (LRO_URL or mirror) during download process or
180  *                          base_url will be used if it is specified.
181  * @param dest              Destination filename or just directory (filename
182  *                          itself will be derived from the relative_url) or
183  *                          NULL (current working directory + filename derived
184  *                          from relative_url will be used).
185  * @param checksum_type     Type of checksum or LR_CHECKSUM_UNKNOWN.
186  * @param checksum          Expected checksum value or NULL.
187  * @param base_url          Base URL or NULL
188  * @param expectedsize      Expected size of the target. If server reports
189  *                          different size, then no download is performed.
190  *                          If 0 then size check is ignored.
191  * @param resume            If TRUE, then downloader try to resume download
192  *                          if destination file exists. If the file doesn't
193  *                          exists, it will be normally downloaded again.
194  * @param progresscb        Progress callback for this transfer.
195  * @param cbdata            User data for the callbacks
196  * @param endcb             Callback called when target transfer is done.
197  *                          (Use status to check if successfully
198  * @param mirrorfailurecb   Called when download from a mirror failed.
199  * @param err               GError **
200  * @return                  Newly allocated LrPackageTarget or NULL on error
201  */
202 LrPackageTarget *
203 lr_packagetarget_new_v2(LrHandle *handle,
204                         const char *relative_url,
205                         const char *dest,
206                         LrChecksumType checksum_type,
207                         const char *checksum,
208                         gint64 expectedsize,
209                         const char *base_url,
210                         gboolean resume,
211                         LrProgressCb progresscb,
212                         void *cbdata,
213                         LrEndCb endcb,
214                         LrMirrorFailureCb mirrorfailurecb,
215                         GError **err);
216 
217 /** Create new LrPackageTarget object.
218  * Almost same as lr_packagetarget_new_v2() except this function
219  * could set a required byte range of the package.
220  * For params see lr_packagetarget_new_v2().
221  * @param byterangestart    Download only specified range of bytes. This param
222  *                          specifies the begin. 0 is default.
223  *                          Note: When this options is != 0 then resume must
224  *                          be disabled - resume param must be FALSE.
225  * @param byterangeend      Download only specified range of bytes. This param
226  *                          specifies the end. 0 is default.
227  *                          If this value is less or equal to byterangestart,
228  *                          then it is ignored.
229  * @return                  Newly allocated LrPackageTarget or NULL on error
230  */
231 LrPackageTarget *
232 lr_packagetarget_new_v3(LrHandle *handle,
233                         const char *relative_url,
234                         const char *dest,
235                         LrChecksumType checksum_type,
236                         const char *checksum,
237                         gint64 expectedsize,
238                         const char *base_url,
239                         gboolean resume,
240                         LrProgressCb progresscb,
241                         void *cbdata,
242                         LrEndCb endcb,
243                         LrMirrorFailureCb mirrorfailurecb,
244                         gint64 byterangestart,
245                         gint64 byterangeend,
246                         GError **err);
247 
248 /** Free ::LrPackageTarget object.
249  * @param target        LrPackageTarget object
250  */
251 void
252 lr_packagetarget_free(LrPackageTarget *target);
253 
254 /** Available flags for package downloader */
255 typedef enum {
256     LR_PACKAGEDOWNLOAD_FAILFAST    = 1 << 0, /*!<
257         If TRUE, then whole downloading is stopped immediately when any
258         of download fails (FALSE is returned and err is set). If the failfast
259         is FALSE, then this function returns after all downloads finish
260         (no matter if successfully or unsuccessfully) and FALSE is returned
261         only if a nonrecoverable error related to the function itself is meet
262         (Errors related to individual downloads are reported via corresponding
263         PackageTarget objects). */
264 } LrPackageDownloadFlag;
265 
266 /** Download all LrPackageTargets at the targets GSList.
267  * @param targets           GSList where each element is a ::LrPackageTarget
268  *                          object
269  * @param flags             Bitfield with flags to download
270  * @param err               GError **
271  * @return                  If FALSE then err is set.
272  */
273 gboolean
274 lr_download_packages(GSList *targets,
275                      LrPackageDownloadFlag flags,
276                      GError **err);
277 
278 typedef enum {
279     LR_PACKAGECHECK_FAILFAST    = 1 << 0, /*!<
280         If TRUE, then whole check is stoped immediately when any
281         of target doesn't exist locally or its checksum doesn't match.
282         (FALSE is returned and err is set). If the failfast
283         is FALSE, then this function returns after check of all targets
284         is finished (no matter if successfully or unsuccessfully) and
285         FALSE is returned only if a nonrecoverable error related to the
286         function itself is meet (Errors related to individual targets
287         are reported via corresponding PackageTarget objects). */
288 } LrPackageCheckFlag;
289 
290 /** Check if targets locally exist and checksums match.
291  * If target locally exists, then its err is NULL,
292  * if it doesn't exists, or checksum is differ. Then target->err is
293  * an error message.
294  */
295 gboolean
296 lr_check_packages(GSList *targets,
297                   LrPackageCheckFlag flags,
298                   GError **err);
299 
300 /** @} */
301 
302 G_END_DECLS
303 
304 #endif
305