]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | ||
23 | #ifdef HAVE_CONFIG_H | |
24 | #include "config.h" | |
25 | #endif | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <ctype.h> | |
29 | #include <errno.h> | |
30 | #include <string.h> | |
31 | #ifdef HAVE_SYS_TYPES_H | |
32 | #include <sys/types.h> | |
33 | #endif | |
34 | #ifdef HAVE_UTIME_H | |
35 | #include <utime.h> | |
36 | #endif | |
37 | #include <glib.h> | |
38 | #include <glib/gstdio.h> | |
39 | #include <glib/gi18n.h> | |
40 | ||
41 | ||
42 | #include "download.h" | |
43 | ||
44 | #include "curl_download.h" | |
45 | #include "preferences.h" | |
46 | #include "globals.h" | |
47 | ||
48 | static gboolean check_file_first_line(FILE* f, gchar *patterns[]) | |
49 | { | |
50 | gchar **s; | |
51 | gchar *bp; | |
52 | fpos_t pos; | |
53 | gchar buf[33]; | |
54 | size_t nr; | |
55 | ||
56 | memset(buf, 0, sizeof(buf)); | |
57 | fgetpos(f, &pos); | |
58 | rewind(f); | |
59 | nr = fread(buf, 1, sizeof(buf) - 1, f); | |
60 | fsetpos(f, &pos); | |
61 | for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) { | |
62 | if (!(isspace(*bp))) | |
63 | break; | |
64 | } | |
65 | if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr)) | |
66 | return FALSE; | |
67 | for (s = patterns; *s; s++) { | |
68 | if (strncasecmp(*s, bp, strlen(*s)) == 0) | |
69 | return TRUE; | |
70 | } | |
71 | return FALSE; | |
72 | } | |
73 | ||
74 | gboolean a_check_html_file(FILE* f) | |
75 | { | |
76 | gchar * html_str[] = { | |
77 | "<html", | |
78 | "<!DOCTYPE html", | |
79 | "<head", | |
80 | "<title", | |
81 | NULL | |
82 | }; | |
83 | ||
84 | return check_file_first_line(f, html_str); | |
85 | } | |
86 | ||
87 | gboolean a_check_map_file(FILE* f) | |
88 | { | |
89 | /* FIXME no more true since a_check_kml_file */ | |
90 | return !a_check_html_file(f); | |
91 | } | |
92 | ||
93 | gboolean a_check_kml_file(FILE* f) | |
94 | { | |
95 | gchar * kml_str[] = { | |
96 | "<?xml", | |
97 | NULL | |
98 | }; | |
99 | ||
100 | return check_file_first_line(f, kml_str); | |
101 | } | |
102 | ||
103 | static GList *file_list = NULL; | |
104 | static GMutex *file_list_mutex = NULL; | |
105 | ||
106 | /* spin button scales */ | |
107 | VikLayerParamScale params_scales[] = { | |
108 | {1, 86400*7, 10, 0}, /* download_tile_age */ | |
109 | }; | |
110 | ||
111 | static VikLayerParam prefs[] = { | |
112 | { VIKING_PREFERENCES_NAMESPACE "download_tile_age", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Tile age (s):"), VIK_LAYER_WIDGET_SPINBUTTON, params_scales + 0, NULL }, | |
113 | }; | |
114 | ||
115 | void a_download_init (void) | |
116 | { | |
117 | VikLayerParamData tmp; | |
118 | tmp.u = VIK_CONFIG_DEFAULT_TILE_AGE; | |
119 | a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); | |
120 | ||
121 | file_list_mutex = g_mutex_new(); | |
122 | } | |
123 | ||
124 | static gboolean lock_file(const char *fn) | |
125 | { | |
126 | gboolean locked = FALSE; | |
127 | g_mutex_lock(file_list_mutex); | |
128 | if (g_list_find_custom(file_list, fn, (GCompareFunc)g_strcmp0) == NULL) | |
129 | { | |
130 | // The filename is not yet locked | |
131 | file_list = g_list_append(file_list, (gpointer)fn), | |
132 | locked = TRUE; | |
133 | } | |
134 | g_mutex_unlock(file_list_mutex); | |
135 | return locked; | |
136 | } | |
137 | ||
138 | static void unlock_file(const char *fn) | |
139 | { | |
140 | g_mutex_lock(file_list_mutex); | |
141 | file_list = g_list_remove(file_list, (gconstpointer)fn); | |
142 | g_mutex_unlock(file_list_mutex); | |
143 | } | |
144 | ||
145 | static int download( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *options, gboolean ftp, void *handle) | |
146 | { | |
147 | FILE *f; | |
148 | int ret; | |
149 | gchar *tmpfilename; | |
150 | gboolean failure = FALSE; | |
151 | DownloadFileOptions file_options = {0, NULL, NULL}; | |
152 | ||
153 | /* Check file */ | |
154 | if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE ) | |
155 | { | |
156 | if (options == NULL || (!options->check_file_server_time && | |
157 | !options->use_etag)) { | |
158 | /* Nothing to do as file already exists and we don't want to check server */ | |
159 | return -3; | |
160 | } | |
161 | ||
162 | time_t tile_age = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "download_tile_age")->u; | |
163 | /* Get the modified time of this file */ | |
164 | struct stat buf; | |
165 | g_stat ( fn, &buf ); | |
166 | time_t file_time = buf.st_mtime; | |
167 | if ( (time(NULL) - file_time) < tile_age ) { | |
168 | /* File cache is too recent, so return */ | |
169 | return -3; | |
170 | } | |
171 | ||
172 | if (options->check_file_server_time) { | |
173 | file_options.time_condition = file_time; | |
174 | } | |
175 | if (options->use_etag) { | |
176 | gchar *etag_filename = g_strdup_printf("%s.etag", fn); | |
177 | gsize etag_length = 0; | |
178 | g_file_get_contents (etag_filename, &(file_options.etag), &etag_length, NULL); | |
179 | g_free (etag_filename); | |
180 | etag_filename = NULL; | |
181 | ||
182 | /* check if etag is short enough */ | |
183 | if (etag_length > 100) { | |
184 | g_free(file_options.etag); | |
185 | file_options.etag = NULL; | |
186 | } | |
187 | ||
188 | /* TODO: should check that etag is a valid string */ | |
189 | } | |
190 | ||
191 | } else { | |
192 | gchar *dir = g_path_get_dirname ( fn ); | |
193 | g_mkdir_with_parents ( dir , 0777 ); | |
194 | g_free ( dir ); | |
195 | } | |
196 | ||
197 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
198 | if (!lock_file ( tmpfilename ) ) | |
199 | { | |
200 | g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename); | |
201 | g_free ( tmpfilename ); | |
202 | if (options->use_etag) | |
203 | g_free ( file_options.etag ); | |
204 | return -4; | |
205 | } | |
206 | f = g_fopen ( tmpfilename, "w+b" ); /* truncate file and open it */ | |
207 | if ( ! f ) { | |
208 | g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno)); | |
209 | g_free ( tmpfilename ); | |
210 | if (options->use_etag) | |
211 | g_free ( file_options.etag ); | |
212 | return -4; | |
213 | } | |
214 | ||
215 | /* Call the backend function */ | |
216 | ret = curl_download_get_url ( hostname, uri, f, options, ftp, &file_options, handle ); | |
217 | ||
218 | if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) { | |
219 | g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret); | |
220 | failure = TRUE; | |
221 | } | |
222 | ||
223 | if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) { | |
224 | g_debug("%s: file content checking failed", __FUNCTION__); | |
225 | failure = TRUE; | |
226 | } | |
227 | ||
228 | fclose ( f ); | |
229 | f = NULL; | |
230 | ||
231 | if (failure) | |
232 | { | |
233 | g_warning(_("Download error: %s"), fn); | |
234 | g_remove ( tmpfilename ); | |
235 | unlock_file ( tmpfilename ); | |
236 | g_free ( tmpfilename ); | |
237 | if (options->use_etag) { | |
238 | g_free ( file_options.etag ); | |
239 | g_free ( file_options.new_etag ); | |
240 | } | |
241 | g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ | |
242 | return -1; | |
243 | } | |
244 | ||
245 | if (options->use_etag) { | |
246 | if (file_options.new_etag) { | |
247 | /* server returned an etag value */ | |
248 | gchar *etag_filename = g_strdup_printf("%s.etag", fn); | |
249 | g_file_set_contents (etag_filename, file_options.new_etag, -1, NULL); | |
250 | g_free (etag_filename); | |
251 | etag_filename = NULL; | |
252 | } | |
253 | } | |
254 | ||
255 | if (ret == DOWNLOAD_NO_NEWER_FILE) { | |
256 | g_remove ( tmpfilename ); | |
257 | #if GLIB_CHECK_VERSION(2,18,0) | |
258 | g_utime ( fn, NULL ); /* update mtime of local copy */ | |
259 | #else | |
260 | utimes ( fn, NULL ); /* update mtime of local copy */ | |
261 | #endif | |
262 | } else { | |
263 | g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */ | |
264 | } | |
265 | unlock_file ( tmpfilename ); | |
266 | g_free ( tmpfilename ); | |
267 | ||
268 | if (options->use_etag) { | |
269 | g_free ( file_options.etag ); | |
270 | g_free ( file_options.new_etag ); | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
275 | /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */ | |
276 | /* uri: like "/uri.html?whatever" */ | |
277 | /* only reason for the "wrapper" is so we can do redirects. */ | |
278 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle ) | |
279 | { | |
280 | return download ( hostname, uri, fn, opt, FALSE, handle ); | |
281 | } | |
282 | ||
283 | int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle ) | |
284 | { | |
285 | return download ( hostname, uri, fn, opt, TRUE, handle ); | |
286 | } | |
287 | ||
288 | void * a_download_handle_init () | |
289 | { | |
290 | return curl_download_handle_init (); | |
291 | } | |
292 | ||
293 | void a_download_handle_cleanup ( void *handle ) | |
294 | { | |
295 | curl_download_handle_cleanup ( handle ); | |
296 | } |