]>
Commit | Line | Data |
---|---|---|
a3697549 | 1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
85611cd9 GB |
2 | /* |
3 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
4 | * | |
5 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
a482007a | 6 | * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
a3697549 | 7 | * Copyright (C) 2013, Rob Norris <rw_norris@hotmail.com> |
85611cd9 GB |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | * | |
23 | */ | |
24 | ||
3292ba8b GB |
25 | #ifdef HAVE_CONFIG_H |
26 | #include "config.h" | |
27 | #endif | |
28 | ||
932f8f22 | 29 | #include <stdio.h> |
6e78a423 | 30 | #include <ctype.h> |
6a4a29aa | 31 | #include <errno.h> |
6e78a423 | 32 | #include <string.h> |
7de36baf | 33 | #ifdef HAVE_SYS_TYPES_H |
6a4a29aa | 34 | #include <sys/types.h> |
7de36baf GB |
35 | #endif |
36 | #ifdef HAVE_UTIME_H | |
6a4a29aa | 37 | #include <utime.h> |
7de36baf | 38 | #endif |
f83131b9 MA |
39 | #include <glib.h> |
40 | #include <glib/gstdio.h> | |
4c77d5e0 | 41 | #include <glib/gi18n.h> |
932f8f22 | 42 | |
a3697549 RN |
43 | #ifdef HAVE_MAGIC_H |
44 | #include <magic.h> | |
45 | #endif | |
46 | #include "compression.h" | |
6a4a29aa | 47 | |
85611cd9 | 48 | #include "download.h" |
3292ba8b | 49 | |
3292ba8b | 50 | #include "curl_download.h" |
6693f5f9 GB |
51 | #include "preferences.h" |
52 | #include "globals.h" | |
85611cd9 | 53 | |
388cf5a4 | 54 | static gboolean check_file_first_line(FILE* f, gchar *patterns[]) |
6e78a423 | 55 | { |
533bbf34 MA |
56 | gchar **s; |
57 | gchar *bp; | |
6e78a423 | 58 | fpos_t pos; |
533bbf34 | 59 | gchar buf[33]; |
6e78a423 | 60 | size_t nr; |
6e78a423 | 61 | |
c44594ee | 62 | memset(buf, 0, sizeof(buf)); |
6e78a423 QT |
63 | fgetpos(f, &pos); |
64 | rewind(f); | |
65 | nr = fread(buf, 1, sizeof(buf) - 1, f); | |
66 | fsetpos(f, &pos); | |
67 | for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) { | |
68 | if (!(isspace(*bp))) | |
69 | break; | |
70 | } | |
71 | if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr)) | |
1ac37c09 | 72 | return FALSE; |
388cf5a4 | 73 | for (s = patterns; *s; s++) { |
1918a993 | 74 | if (strncasecmp(*s, bp, strlen(*s)) == 0) |
1ac37c09 | 75 | return TRUE; |
6e78a423 | 76 | } |
1ac37c09 GB |
77 | return FALSE; |
78 | } | |
79 | ||
388cf5a4 GB |
80 | gboolean a_check_html_file(FILE* f) |
81 | { | |
82 | gchar * html_str[] = { | |
83 | "<html", | |
84 | "<!DOCTYPE html", | |
85 | "<head", | |
86 | "<title", | |
87 | NULL | |
88 | }; | |
89 | ||
90 | return check_file_first_line(f, html_str); | |
91 | } | |
92 | ||
1ac37c09 GB |
93 | gboolean a_check_map_file(FILE* f) |
94 | { | |
388cf5a4 | 95 | /* FIXME no more true since a_check_kml_file */ |
1ac37c09 | 96 | return !a_check_html_file(f); |
6e78a423 QT |
97 | } |
98 | ||
ae941b4c HMJ |
99 | gboolean a_check_kml_file(FILE* f) |
100 | { | |
388cf5a4 | 101 | gchar * kml_str[] = { |
ae941b4c HMJ |
102 | "<?xml", |
103 | NULL | |
104 | }; | |
105 | ||
388cf5a4 | 106 | return check_file_first_line(f, kml_str); |
ae941b4c HMJ |
107 | } |
108 | ||
4b992365 GB |
109 | static GList *file_list = NULL; |
110 | static GMutex *file_list_mutex = NULL; | |
111 | ||
6693f5f9 | 112 | /* spin button scales */ |
32dfea86 RN |
113 | static VikLayerParamScale params_scales[] = { |
114 | {1, 365, 1, 0}, /* download_tile_age */ | |
6693f5f9 GB |
115 | }; |
116 | ||
32dfea86 RN |
117 | static VikLayerParamData convert_to_display ( VikLayerParamData value ) |
118 | { | |
119 | // From seconds into days | |
120 | return VIK_LPD_UINT ( value.u / 86400 ); | |
121 | } | |
122 | ||
123 | static VikLayerParamData convert_to_internal ( VikLayerParamData value ) | |
124 | { | |
125 | // From days into seconds | |
126 | return VIK_LPD_UINT ( 86400 * value.u ); | |
127 | } | |
128 | ||
6693f5f9 | 129 | static VikLayerParam prefs[] = { |
32dfea86 | 130 | { VIK_LAYER_NUM_TYPES, VIKING_PREFERENCES_NAMESPACE "download_tile_age", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Tile age (days):"), VIK_LAYER_WIDGET_SPINBUTTON, ¶ms_scales[0], NULL, NULL, NULL, convert_to_display, convert_to_internal }, |
6693f5f9 GB |
131 | }; |
132 | ||
4b992365 GB |
133 | void a_download_init (void) |
134 | { | |
6693f5f9 | 135 | VikLayerParamData tmp; |
32dfea86 | 136 | tmp.u = VIK_CONFIG_DEFAULT_TILE_AGE / 86400; // Now in days |
6693f5f9 GB |
137 | a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); |
138 | ||
4b992365 GB |
139 | file_list_mutex = g_mutex_new(); |
140 | } | |
141 | ||
142 | static gboolean lock_file(const char *fn) | |
143 | { | |
144 | gboolean locked = FALSE; | |
145 | g_mutex_lock(file_list_mutex); | |
2894744e | 146 | if (g_list_find_custom(file_list, fn, (GCompareFunc)g_strcmp0) == NULL) |
4b992365 GB |
147 | { |
148 | // The filename is not yet locked | |
149 | file_list = g_list_append(file_list, (gpointer)fn), | |
150 | locked = TRUE; | |
151 | } | |
152 | g_mutex_unlock(file_list_mutex); | |
153 | return locked; | |
154 | } | |
155 | ||
156 | static void unlock_file(const char *fn) | |
157 | { | |
158 | g_mutex_lock(file_list_mutex); | |
159 | file_list = g_list_remove(file_list, (gconstpointer)fn); | |
160 | g_mutex_unlock(file_list_mutex); | |
161 | } | |
162 | ||
a3697549 RN |
163 | |
164 | static void uncompress_zip ( gchar *name ) | |
165 | { | |
166 | GError *error = NULL; | |
167 | GMappedFile *mf; | |
168 | ||
169 | if ((mf = g_mapped_file_new ( name, FALSE, &error )) == NULL) { | |
170 | g_critical(_("Couldn't map file %s: %s"), name, error->message); | |
171 | g_error_free(error); | |
172 | return; | |
173 | } | |
174 | gchar *file_contents = g_mapped_file_get_contents ( mf ); | |
175 | ||
176 | void *unzip_mem = NULL; | |
177 | gulong ucsize; | |
178 | ||
179 | if ((unzip_mem = unzip_file (file_contents, &ucsize)) == NULL) { | |
180 | g_mapped_file_unref ( mf ); | |
181 | return; | |
182 | } | |
183 | ||
184 | // This overwrires any previous file contents | |
185 | if ( ! g_file_set_contents ( name, unzip_mem, ucsize, &error ) ) { | |
186 | g_critical ( "Couldn't write file '%s', because of %s", name, error->message ); | |
187 | g_error_free ( error ); | |
188 | } | |
189 | } | |
190 | ||
191 | /** | |
192 | * a_try_decompress_file: | |
193 | * @name: The potentially compressed filename | |
194 | * | |
195 | * Perform magic to decide how which type of decompression to attempt | |
196 | */ | |
197 | void a_try_decompress_file (gchar *name) | |
198 | { | |
199 | #ifdef HAVE_MAGIC_H | |
200 | magic_t myt = magic_open ( MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_MIME ); | |
201 | gboolean zip = FALSE; | |
202 | gboolean bzip2 = FALSE; | |
203 | if ( myt ) { | |
9702385a RN |
204 | #ifdef WINDOWS |
205 | // We have to 'package' the magic database ourselves :( | |
206 | // --> %PROGRAM FILES%\Viking\magic.mgc | |
207 | magic_load ( myt, "magic.mgc" ); | |
208 | #else | |
209 | // Use system default | |
a3697549 | 210 | magic_load ( myt, NULL ); |
9702385a | 211 | #endif |
a3697549 RN |
212 | const char* magic = magic_file (myt, name); |
213 | g_debug ("%s: magic output: %s", __FUNCTION__, magic ); | |
214 | ||
215 | if ( g_strcmp0 (magic, "application/zip; charset=binary") == 0 ) | |
216 | zip = TRUE; | |
217 | ||
218 | if ( g_strcmp0 (magic, "application/x-bzip2; charset=binary") == 0 ) | |
219 | bzip2 = TRUE; | |
220 | ||
221 | magic_close ( myt ); | |
222 | } | |
223 | ||
224 | if ( !(zip || bzip2) ) | |
225 | return; | |
226 | ||
227 | if ( zip ) { | |
228 | uncompress_zip ( name ); | |
229 | } | |
230 | else if ( bzip2 ) { | |
231 | gchar* bz2_name = uncompress_bzip2 ( name ); | |
232 | g_remove ( name ); | |
233 | g_rename ( bz2_name, name ); | |
234 | } | |
235 | ||
236 | return; | |
237 | #endif | |
238 | } | |
239 | ||
4e815e90 | 240 | static DownloadResult_t download( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *options, gboolean ftp, void *handle) |
932f8f22 GB |
241 | { |
242 | FILE *f; | |
243 | int ret; | |
533bbf34 | 244 | gchar *tmpfilename; |
1ac37c09 | 245 | gboolean failure = FALSE; |
ab716260 | 246 | DownloadFileOptions file_options = {0, NULL, NULL}; |
932f8f22 GB |
247 | |
248 | /* Check file */ | |
45acf79e | 249 | if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE ) |
932f8f22 | 250 | { |
8853eed9 JJ |
251 | if (options == NULL || (!options->check_file_server_time && |
252 | !options->use_etag)) { | |
253 | /* Nothing to do as file already exists and we don't want to check server */ | |
4e815e90 | 254 | return DOWNLOAD_NOT_REQUIRED; |
6a4a29aa | 255 | } |
8853eed9 | 256 | |
57ecf9cb JJ |
257 | time_t tile_age = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "download_tile_age")->u; |
258 | /* Get the modified time of this file */ | |
259 | struct stat buf; | |
260 | g_stat ( fn, &buf ); | |
261 | time_t file_time = buf.st_mtime; | |
262 | if ( (time(NULL) - file_time) < tile_age ) { | |
263 | /* File cache is too recent, so return */ | |
4e815e90 | 264 | return DOWNLOAD_NOT_REQUIRED; |
57ecf9cb JJ |
265 | } |
266 | ||
8853eed9 | 267 | if (options->check_file_server_time) { |
57ecf9cb | 268 | file_options.time_condition = file_time; |
6a4a29aa | 269 | } |
55246377 | 270 | if (options->use_etag) { |
f91cc826 | 271 | gchar *etag_filename = g_strdup_printf("%s.etag", fn); |
9137c580 | 272 | gsize etag_length = 0; |
f91cc826 | 273 | g_file_get_contents (etag_filename, &(file_options.etag), &etag_length, NULL); |
9137c580 GB |
274 | g_free (etag_filename); |
275 | etag_filename = NULL; | |
f91cc826 JJ |
276 | |
277 | /* check if etag is short enough */ | |
9137c580 | 278 | if (etag_length > 100) { |
f91cc826 | 279 | g_free(file_options.etag); |
9137c580 GB |
280 | file_options.etag = NULL; |
281 | } | |
f91cc826 JJ |
282 | |
283 | /* TODO: should check that etag is a valid string */ | |
55246377 | 284 | } |
f91cc826 | 285 | |
932f8f22 | 286 | } else { |
a1618d62 MA |
287 | gchar *dir = g_path_get_dirname ( fn ); |
288 | g_mkdir_with_parents ( dir , 0777 ); | |
289 | g_free ( dir ); | |
3335ae4e EB |
290 | } |
291 | ||
292 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
4b992365 GB |
293 | if (!lock_file ( tmpfilename ) ) |
294 | { | |
295 | g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename); | |
296 | g_free ( tmpfilename ); | |
9137c580 GB |
297 | if (options->use_etag) |
298 | g_free ( file_options.etag ); | |
4e815e90 | 299 | return DOWNLOAD_FILE_WRITE_ERROR; |
4b992365 GB |
300 | } |
301 | f = g_fopen ( tmpfilename, "w+b" ); /* truncate file and open it */ | |
3335ae4e | 302 | if ( ! f ) { |
4b992365 | 303 | g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno)); |
3335ae4e | 304 | g_free ( tmpfilename ); |
9137c580 GB |
305 | if (options->use_etag) |
306 | g_free ( file_options.etag ); | |
4e815e90 | 307 | return DOWNLOAD_FILE_WRITE_ERROR; |
932f8f22 GB |
308 | } |
309 | ||
310 | /* Call the backend function */ | |
9a021e4f | 311 | ret = curl_download_get_url ( hostname, uri, f, options, ftp, &file_options, handle ); |
6a4a29aa | 312 | |
4e815e90 RN |
313 | DownloadResult_t result = DOWNLOAD_SUCCESS; |
314 | ||
315 | if (ret != CURL_DOWNLOAD_NO_ERROR && ret != CURL_DOWNLOAD_NO_NEWER_FILE) { | |
1ac37c09 GB |
316 | g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret); |
317 | failure = TRUE; | |
4e815e90 | 318 | result = DOWNLOAD_HTTP_ERROR; |
1ac37c09 GB |
319 | } |
320 | ||
321 | if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) { | |
322 | g_debug("%s: file content checking failed", __FUNCTION__); | |
323 | failure = TRUE; | |
4e815e90 | 324 | result = DOWNLOAD_CONTENT_ERROR; |
1ac37c09 | 325 | } |
932f8f22 | 326 | |
1ec35593 MA |
327 | fclose ( f ); |
328 | f = NULL; | |
329 | ||
1ac37c09 | 330 | if (failure) |
932f8f22 | 331 | { |
4258f4e2 | 332 | g_warning(_("Download error: %s"), fn); |
8c060406 | 333 | g_remove ( tmpfilename ); |
4b992365 | 334 | unlock_file ( tmpfilename ); |
3335ae4e | 335 | g_free ( tmpfilename ); |
55246377 JJ |
336 | if (options->use_etag) { |
337 | g_free ( file_options.etag ); | |
338 | g_free ( file_options.new_etag ); | |
339 | } | |
4e815e90 | 340 | return result; |
932f8f22 GB |
341 | } |
342 | ||
a3697549 RN |
343 | if ( options->convert_file ) |
344 | options->convert_file ( tmpfilename ); | |
345 | ||
55246377 JJ |
346 | if (options->use_etag) { |
347 | if (file_options.new_etag) { | |
348 | /* server returned an etag value */ | |
f91cc826 JJ |
349 | gchar *etag_filename = g_strdup_printf("%s.etag", fn); |
350 | g_file_set_contents (etag_filename, file_options.new_etag, -1, NULL); | |
9137c580 GB |
351 | g_free (etag_filename); |
352 | etag_filename = NULL; | |
55246377 JJ |
353 | } |
354 | } | |
355 | ||
4e815e90 | 356 | if (ret == CURL_DOWNLOAD_NO_NEWER_FILE) { |
6a4a29aa | 357 | g_remove ( tmpfilename ); |
7de36baf GB |
358 | #if GLIB_CHECK_VERSION(2,18,0) |
359 | g_utime ( fn, NULL ); /* update mtime of local copy */ | |
360 | #else | |
d4939f80 | 361 | utimes ( fn, NULL ); /* update mtime of local copy */ |
7de36baf | 362 | #endif |
4945e425 | 363 | } else { |
4bdc96fc RN |
364 | /* move completely-downloaded file to permanent location */ |
365 | if ( g_rename ( tmpfilename, fn ) ) | |
366 | g_warning ("%s: file rename failed [%s] to [%s]", __FUNCTION__, tmpfilename, fn ); | |
4945e425 | 367 | } |
4b992365 | 368 | unlock_file ( tmpfilename ); |
6a4a29aa | 369 | g_free ( tmpfilename ); |
1ec35593 | 370 | |
55246377 JJ |
371 | if (options->use_etag) { |
372 | g_free ( file_options.etag ); | |
373 | g_free ( file_options.new_etag ); | |
374 | } | |
4e815e90 | 375 | return DOWNLOAD_SUCCESS; |
932f8f22 GB |
376 | } |
377 | ||
4e815e90 RN |
378 | /** |
379 | * uri: like "/uri.html?whatever" | |
380 | * only reason for the "wrapper" is so we can do redirects. | |
381 | */ | |
382 | DownloadResult_t a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle ) | |
85611cd9 | 383 | { |
825413ba | 384 | return download ( hostname, uri, fn, opt, FALSE, handle ); |
0c1044e9 EB |
385 | } |
386 | ||
4e815e90 | 387 | DownloadResult_t a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle ) |
0c1044e9 | 388 | { |
825413ba SW |
389 | return download ( hostname, uri, fn, opt, TRUE, handle ); |
390 | } | |
391 | ||
392 | void * a_download_handle_init () | |
393 | { | |
394 | return curl_download_handle_init (); | |
395 | } | |
396 | ||
397 | void a_download_handle_cleanup ( void *handle ) | |
398 | { | |
399 | curl_download_handle_cleanup ( handle ); | |
85611cd9 | 400 | } |
e09b94fe RN |
401 | |
402 | /** | |
403 | * a_download_url_to_tmp_file: | |
404 | * @uri: The URI (Uniform Resource Identifier) | |
405 | * @options: Download options (maybe NULL) | |
406 | * | |
407 | * returns name of the temporary file created - NULL if unsuccessful | |
408 | * this string needs to be freed once used | |
409 | * the file needs to be removed once used | |
410 | */ | |
411 | gchar *a_download_uri_to_tmp_file ( const gchar *uri, DownloadMapOptions *options ) | |
412 | { | |
413 | FILE *tmp_file; | |
414 | int tmp_fd; | |
415 | gchar *tmpname; | |
416 | ||
417 | if ( (tmp_fd = g_file_open_tmp ("viking-download.XXXXXX", &tmpname, NULL)) == -1 ) { | |
418 | g_critical (_("couldn't open temp file")); | |
419 | return NULL; | |
420 | } | |
421 | ||
422 | tmp_file = fdopen(tmp_fd, "r+"); | |
423 | ||
424 | if ( curl_download_uri ( uri, tmp_file, options, NULL, NULL ) ) { | |
425 | // error | |
426 | fclose ( tmp_file ); | |
427 | g_remove ( tmpname ); | |
428 | g_free ( tmpname ); | |
429 | return NULL; | |
430 | } | |
431 | fclose ( tmp_file ); | |
432 | ||
433 | return tmpname; | |
434 | } |