]> git.street.me.uk Git - andy/viking.git/blame - src/download.c
Fix translatable string with variable argument
[andy/viking.git] / src / download.c
CommitLineData
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 54static 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
80gboolean 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
93gboolean 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
99gboolean 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
109static GList *file_list = NULL;
110static GMutex *file_list_mutex = NULL;
111
6693f5f9 112/* spin button scales */
32dfea86
RN
113static VikLayerParamScale params_scales[] = {
114 {1, 365, 1, 0}, /* download_tile_age */
6693f5f9
GB
115};
116
32dfea86
RN
117static VikLayerParamData convert_to_display ( VikLayerParamData value )
118{
119 // From seconds into days
120 return VIK_LPD_UINT ( value.u / 86400 );
121}
122
123static VikLayerParamData convert_to_internal ( VikLayerParamData value )
124{
125 // From days into seconds
126 return VIK_LPD_UINT ( 86400 * value.u );
127}
128
6693f5f9 129static 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, &params_scales[0], NULL, NULL, NULL, convert_to_display, convert_to_internal },
6693f5f9
GB
131};
132
4b992365
GB
133void 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
142static 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
156static 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
164static 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 */
197void 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
b529dc9c 240static int 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 */
6a4a29aa
JJ
254 return -3;
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 */
264 return -3;
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 );
4b992365
GB
299 return -4;
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 );
3335ae4e 307 return -4;
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
JJ
312
313 if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) {
1ac37c09
GB
314 g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
315 failure = TRUE;
316 }
317
318 if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
319 g_debug("%s: file content checking failed", __FUNCTION__);
320 failure = TRUE;
321 }
932f8f22 322
1ec35593
MA
323 fclose ( f );
324 f = NULL;
325
1ac37c09 326 if (failure)
932f8f22 327 {
4258f4e2 328 g_warning(_("Download error: %s"), fn);
8c060406 329 g_remove ( tmpfilename );
4b992365 330 unlock_file ( tmpfilename );
3335ae4e 331 g_free ( tmpfilename );
55246377
JJ
332 if (options->use_etag) {
333 g_free ( file_options.etag );
334 g_free ( file_options.new_etag );
335 }
932f8f22
GB
336 return -1;
337 }
338
a3697549
RN
339 if ( options->convert_file )
340 options->convert_file ( tmpfilename );
341
55246377
JJ
342 if (options->use_etag) {
343 if (file_options.new_etag) {
344 /* server returned an etag value */
f91cc826
JJ
345 gchar *etag_filename = g_strdup_printf("%s.etag", fn);
346 g_file_set_contents (etag_filename, file_options.new_etag, -1, NULL);
9137c580
GB
347 g_free (etag_filename);
348 etag_filename = NULL;
55246377
JJ
349 }
350 }
351
d4939f80 352 if (ret == DOWNLOAD_NO_NEWER_FILE) {
6a4a29aa 353 g_remove ( tmpfilename );
7de36baf
GB
354#if GLIB_CHECK_VERSION(2,18,0)
355 g_utime ( fn, NULL ); /* update mtime of local copy */
356#else
d4939f80 357 utimes ( fn, NULL ); /* update mtime of local copy */
7de36baf 358#endif
4945e425 359 } else {
6a4a29aa 360 g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
4945e425 361 }
4b992365 362 unlock_file ( tmpfilename );
6a4a29aa 363 g_free ( tmpfilename );
1ec35593 364
55246377
JJ
365 if (options->use_etag) {
366 g_free ( file_options.etag );
367 g_free ( file_options.new_etag );
368 }
6a4a29aa 369 return 0;
932f8f22
GB
370}
371
85611cd9
GB
372/* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
373/* uri: like "/uri.html?whatever" */
374/* only reason for the "wrapper" is so we can do redirects. */
b529dc9c 375int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
85611cd9 376{
825413ba 377 return download ( hostname, uri, fn, opt, FALSE, handle );
0c1044e9
EB
378}
379
b529dc9c 380int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
0c1044e9 381{
825413ba
SW
382 return download ( hostname, uri, fn, opt, TRUE, handle );
383}
384
385void * a_download_handle_init ()
386{
387 return curl_download_handle_init ();
388}
389
390void a_download_handle_cleanup ( void *handle )
391{
392 curl_download_handle_cleanup ( handle );
85611cd9 393}
e09b94fe
RN
394
395/**
396 * a_download_url_to_tmp_file:
397 * @uri: The URI (Uniform Resource Identifier)
398 * @options: Download options (maybe NULL)
399 *
400 * returns name of the temporary file created - NULL if unsuccessful
401 * this string needs to be freed once used
402 * the file needs to be removed once used
403 */
404gchar *a_download_uri_to_tmp_file ( const gchar *uri, DownloadMapOptions *options )
405{
406 FILE *tmp_file;
407 int tmp_fd;
408 gchar *tmpname;
409
410 if ( (tmp_fd = g_file_open_tmp ("viking-download.XXXXXX", &tmpname, NULL)) == -1 ) {
411 g_critical (_("couldn't open temp file"));
412 return NULL;
413 }
414
415 tmp_file = fdopen(tmp_fd, "r+");
416
417 if ( curl_download_uri ( uri, tmp_file, options, NULL, NULL ) ) {
418 // error
419 fclose ( tmp_file );
420 g_remove ( tmpname );
421 g_free ( tmpname );
422 return NULL;
423 }
424 fclose ( tmp_file );
425
426 return tmpname;
427}