1 /* librepo - A library providing (libcURL like) API to downloading repository
2  * Copyright (C) 2013  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_DOWNLOADTARGET_H__
22 #define __LR_DOWNLOADTARGET_H__
23 
24 #include <glib.h>
25 
26 #ifdef WITH_ZCHUNK
27 #include <zck.h>
28 #endif /* WITH_ZCHUNK */
29 
30 #include "handle.h"
31 #include "rcodes.h"
32 #include "checksum.h"
33 #include "types.h"
34 #include "yum.h"
35 
36 G_BEGIN_DECLS
37 
38 typedef struct {
39     LrChecksumType type;
40     gchar *value;
41 } LrDownloadTargetChecksum;
42 
43 /** Create new LrDownloadTargetChecksum object.
44  * @param type      Checksum type
45  * @param value     Checksum value. This value will be stduped.
46  */
47 LrDownloadTargetChecksum *
48 lr_downloadtargetchecksum_new(LrChecksumType type, const gchar *value);
49 
50 /** Free LrDownloadTargetChecksum object.
51  * @param dtch      LrDownloadTargetChecksum object
52  */
53 void
54 lr_downloadtargetchecksum_free(LrDownloadTargetChecksum *dtch);
55 
56 /** Single download target
57  */
58 typedef struct {
59 
60     LrHandle *handle; /*!<
61         Handle */
62 
63     char *path; /*!<
64         Relative path for URL (URL: "http://foo.bar/stuff",
65         path: "somestuff.xml") */
66 
67     char *baseurl; /*!<
68         Base URL for this target. If used, then mirrorlist will be ignored. */
69 
70     int fd; /*!<
71         Opened file descriptor where data will be written or -1.
72         Note: Only one, fd or fn, is set simultaneously. */
73 
74     char *fn; /*!<
75         Filename where data will be written or NULL.
76         Note: Only one, fd or fn, is set simultaneously. */
77 
78     GSList *checksums; /*!<
79         NULL or GSList with pointers to LrDownloadTargetChecksum
80         structures. With possible checksums of the file.
81         Checksum check is stopped right after first match.
82         Useful in situation when the file could has one from set
83         of available checksums.
84         E.g. <mm0:alternates> element in metalink could contain alternate
85         checksums for the repomd.xml, because metalink and mirrors
86         could be out of sync for a while. */
87 
88     gint64 expectedsize; /*!<
89         Expected size of the target */
90 
91     gint64 origsize; /*!<
92         Original expected size of the target.  Sometimes expectedsize will
93         change, especially if zchunk is in use, but this will never change */
94 
95     gboolean resume; /*!<
96         Resume:
97          0  - no resume, download whole file,
98          otherwise - autodetect offset for resume */
99 
100     LrProgressCb progresscb; /*!<
101         Progression callback for the target */
102 
103     void *cbdata; /*!<
104         Callback user data */
105 
106     LrEndCb endcb; /*!<
107         Callback called when target transfer is done.
108         (Use status to check if successfully or unsuccessfully) */
109 
110     LrMirrorFailureCb mirrorfailurecb; /*!<
111         Called when download from a mirror failed. */
112 
113     GStringChunk *chunk; /*!<
114         Chunk for strings used in this structure. */
115 
116     gint64 byterangestart; /*!<
117         Download only specified range of bytes. */
118 
119     gint64 byterangeend; /*!<
120         Download only specified range of bytes. */
121 
122     gboolean no_cache; /*!<
123         Add headers that tell proxy server to provide a fresh data
124         instead of cached one. */
125 
126     // Items filled by downloader
127 
128     char *usedmirror; /*!<
129         Used mirror. Filled only if transfer was successful. */
130 
131     char *effectiveurl; /*!<
132         Effective url. Filled only if transfer was successful. */
133 
134     LrRc rcode; /*!<
135         Return code */
136 
137     char *err; /*!<
138         NULL or error message */
139 
140     // Other items
141 
142     void *userdata; /*!<
143         User data - This data are not used by lr_downloader or touched
144         by lr_downloadtarget_free. */
145 
146     // New options for zchunk - put at end to maintain API stability
147     gboolean is_zchunk; /*!<
148         Whether record is a zchunk file or not */
149 
150     char *range; /*!<
151         Range string to download, overrides byterangestart and end */
152 
153     #ifdef WITH_ZCHUNK
154     zckDL *zck_dl; /*!<
155         Zchunk download context */
156 
157     gint64 zck_header_size; /*!<
158         Zchunk header size */
159 
160     double total_to_download; /*!<
161         Total to download in zchunk file */
162 
163     double downloaded; /*!<
164         Amount already downloaded in zchunk file */
165     #endif /* WITH_ZCHUNK */
166 
167 } LrDownloadTarget;
168 
169 /** Create new empty ::LrDownloadTarget.
170  * @param handle            Handle or NULL
171  * @param path              Absolute or relative URL path
172  * @param baseurl           Base URL for relative path specified in path param
173  * @param fd                Opened file descriptor where data will be written
174  *                          or -1.
175  *                          Note: Set this or fn, no both!
176  * @param fn                Filename where data will be written or NULL.
177  *                          Note: Set this or fd, no both!
178  * @param possiblechecksums NULL or GSList with pointers to
179  *                          LrDownloadTargetChecksum structures. With possible
180  *                          checksums of the file. Checksum check is stopped
181  *                          right after first match. Useful in situation when
182  *                          the file could has one from set of available
183  *                          checksums.
184  *                          E.g. <mm0:alternates> element in metalink could
185  *                          contain alternate checksums for the repomd.xml,
186  *                          because metalink and mirrors could be out of
187  *                          sync for a while.
188  *                          Note: LrDownloadTarget takes responsibility
189  *                          for destroying this list! So the list must not
190  *                          be destroyed by the user, since it is passed
191  *                          to this function.
192  * @param expectedsize      Expected size of the target. If mirror reports
193  *                          different size, then no download is performed.
194  *                          If 0 then size of downloaded target is not checked.
195  * @param resume            If FALSE, then no resume is done and whole file is
196  *                          downloaded again. Otherwise offset will be
197  *                          automatically detected from opened file descriptor
198  *                          by seek to the end.
199  * @param progresscb        Progression callback or NULL
200  * @param cbdata            Callback data or NULL
201  * @param endcb             Callback called when target transfer is done.
202  *                          (Use status to check if successfully
203  *                          or unsuccessfully)
204  * @param mirrorfailurecb   Called when download from a mirror failed.
205  * @param userdata          This variable could be used to store some user
206  *                          data. This data are not used/touched by
207  *                          lr_download* functions or by
208  *                          lr_downloadtarget_free().
209  * @param byterangestart    Download only specified byte range. 0 is default.
210  * @param byterangeend      Download only specified byte range.
211  *                          If this value is less or equal byterangestart, then
212  *                          it is ignored. 0 is default.
213  * @param no_cache          Tell proxy server that we don't want to use cache
214  *                          for this request and we want fresh data.
215  * @param is_zchunk         This target is a zchunk file, so be ready to use
216  *                          different download code paths
217  * @return                  New allocated target
218  */
219 LrDownloadTarget *
220 lr_downloadtarget_new(LrHandle *handle,
221                       const char *path,
222                       const char *baseurl,
223                       int fd,
224                       const char *fn,
225                       GSList *possiblechecksums,
226                       gint64 expectedsize,
227                       gboolean resume,
228                       LrProgressCb progresscb,
229                       void *cbdata,
230                       LrEndCb endcb,
231                       LrMirrorFailureCb mirrorfailurecb,
232                       void *userdata,
233                       gint64 byterangestart,
234                       gint64 byterangeend,
235                       char *range,
236                       gboolean no_cache,
237                       gboolean is_zchunk);
238 
239 /** Reset download data filled during downloading. E.g. Error messages,
240  * effective URL, used mirror etc.
241  * @param target        Target
242  */
243 void
244 lr_downloadtarget_reset(LrDownloadTarget *target);
245 
246 /** Free a ::LrDownloadTarget element and its content.
247  * @param target        Target to free.
248  */
249 void
250 lr_downloadtarget_free(LrDownloadTarget *target);
251 
252 G_END_DECLS
253 
254 #endif
255