]>
Commit | Line | Data |
---|---|---|
85611cd9 GB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program 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 | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
3292ba8b GB |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
932f8f22 | 26 | #include <stdio.h> |
6e78a423 | 27 | #include <ctype.h> |
6a4a29aa | 28 | #include <errno.h> |
6e78a423 | 29 | #include <string.h> |
7de36baf | 30 | #ifdef HAVE_SYS_TYPES_H |
6a4a29aa | 31 | #include <sys/types.h> |
7de36baf GB |
32 | #endif |
33 | #ifdef HAVE_UTIME_H | |
6a4a29aa | 34 | #include <utime.h> |
7de36baf | 35 | #endif |
f83131b9 MA |
36 | #include <glib.h> |
37 | #include <glib/gstdio.h> | |
4c77d5e0 | 38 | #include <glib/gi18n.h> |
932f8f22 | 39 | |
6a4a29aa | 40 | |
85611cd9 | 41 | #include "download.h" |
3292ba8b | 42 | |
3292ba8b | 43 | #include "curl_download.h" |
6693f5f9 GB |
44 | #include "preferences.h" |
45 | #include "globals.h" | |
85611cd9 | 46 | |
388cf5a4 | 47 | static gboolean check_file_first_line(FILE* f, gchar *patterns[]) |
6e78a423 | 48 | { |
533bbf34 MA |
49 | gchar **s; |
50 | gchar *bp; | |
6e78a423 | 51 | fpos_t pos; |
533bbf34 | 52 | gchar buf[33]; |
6e78a423 | 53 | size_t nr; |
6e78a423 | 54 | |
c44594ee | 55 | memset(buf, 0, sizeof(buf)); |
6e78a423 QT |
56 | fgetpos(f, &pos); |
57 | rewind(f); | |
58 | nr = fread(buf, 1, sizeof(buf) - 1, f); | |
59 | fsetpos(f, &pos); | |
60 | for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) { | |
61 | if (!(isspace(*bp))) | |
62 | break; | |
63 | } | |
64 | if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr)) | |
1ac37c09 | 65 | return FALSE; |
388cf5a4 | 66 | for (s = patterns; *s; s++) { |
1918a993 | 67 | if (strncasecmp(*s, bp, strlen(*s)) == 0) |
1ac37c09 | 68 | return TRUE; |
6e78a423 | 69 | } |
1ac37c09 GB |
70 | return FALSE; |
71 | } | |
72 | ||
388cf5a4 GB |
73 | gboolean a_check_html_file(FILE* f) |
74 | { | |
75 | gchar * html_str[] = { | |
76 | "<html", | |
77 | "<!DOCTYPE html", | |
78 | "<head", | |
79 | "<title", | |
80 | NULL | |
81 | }; | |
82 | ||
83 | return check_file_first_line(f, html_str); | |
84 | } | |
85 | ||
1ac37c09 GB |
86 | gboolean a_check_map_file(FILE* f) |
87 | { | |
388cf5a4 | 88 | /* FIXME no more true since a_check_kml_file */ |
1ac37c09 | 89 | return !a_check_html_file(f); |
6e78a423 QT |
90 | } |
91 | ||
ae941b4c HMJ |
92 | gboolean a_check_kml_file(FILE* f) |
93 | { | |
388cf5a4 | 94 | gchar * kml_str[] = { |
ae941b4c HMJ |
95 | "<?xml", |
96 | NULL | |
97 | }; | |
98 | ||
388cf5a4 | 99 | return check_file_first_line(f, kml_str); |
ae941b4c HMJ |
100 | } |
101 | ||
4b992365 GB |
102 | static GList *file_list = NULL; |
103 | static GMutex *file_list_mutex = NULL; | |
104 | ||
6693f5f9 GB |
105 | /* spin button scales */ |
106 | VikLayerParamScale params_scales[] = { | |
107 | {1, 10000, 10, 0}, | |
108 | }; | |
109 | ||
110 | static VikLayerParam prefs[] = { | |
111 | { 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 }, | |
112 | }; | |
113 | ||
4b992365 GB |
114 | void a_download_init (void) |
115 | { | |
6693f5f9 GB |
116 | VikLayerParamData tmp; |
117 | tmp.u = VIK_CONFIG_DEFAULT_TILE_AGE; | |
118 | a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY); | |
119 | ||
4b992365 GB |
120 | file_list_mutex = g_mutex_new(); |
121 | } | |
122 | ||
123 | static gboolean lock_file(const char *fn) | |
124 | { | |
125 | gboolean locked = FALSE; | |
126 | g_mutex_lock(file_list_mutex); | |
127 | if (g_list_find(file_list, fn) == NULL) | |
128 | { | |
129 | // The filename is not yet locked | |
130 | file_list = g_list_append(file_list, (gpointer)fn), | |
131 | locked = TRUE; | |
132 | } | |
133 | g_mutex_unlock(file_list_mutex); | |
134 | return locked; | |
135 | } | |
136 | ||
137 | static void unlock_file(const char *fn) | |
138 | { | |
139 | g_mutex_lock(file_list_mutex); | |
140 | file_list = g_list_remove(file_list, (gconstpointer)fn); | |
141 | g_mutex_unlock(file_list_mutex); | |
142 | } | |
143 | ||
825413ba | 144 | static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp, void *handle) |
932f8f22 GB |
145 | { |
146 | FILE *f; | |
147 | int ret; | |
533bbf34 | 148 | gchar *tmpfilename; |
1ac37c09 | 149 | gboolean failure = FALSE; |
6a4a29aa | 150 | time_t time_condition = 0; |
932f8f22 GB |
151 | |
152 | /* Check file */ | |
45acf79e | 153 | if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE ) |
932f8f22 | 154 | { |
6a4a29aa | 155 | if (options != NULL && options->check_file_server_time) { |
6693f5f9 | 156 | time_t tile_age = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "download_tile_age")->u; |
6a4a29aa JJ |
157 | /* Get the modified time of this file */ |
158 | struct stat buf; | |
159 | g_stat ( fn, &buf ); | |
160 | time_condition = buf.st_mtime; | |
6693f5f9 | 161 | if ( (time(NULL) - time_condition) < tile_age ) |
ae40c2dc GB |
162 | /* File cache is too recent, so return */ |
163 | return -3; | |
6a4a29aa JJ |
164 | } else { |
165 | /* Nothing to do as file already exists, so return */ | |
166 | return -3; | |
167 | } | |
932f8f22 | 168 | } else { |
a1618d62 MA |
169 | gchar *dir = g_path_get_dirname ( fn ); |
170 | g_mkdir_with_parents ( dir , 0777 ); | |
171 | g_free ( dir ); | |
3335ae4e EB |
172 | } |
173 | ||
174 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
4b992365 GB |
175 | if (!lock_file ( tmpfilename ) ) |
176 | { | |
177 | g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename); | |
178 | g_free ( tmpfilename ); | |
179 | return -4; | |
180 | } | |
181 | f = g_fopen ( tmpfilename, "w+b" ); /* truncate file and open it */ | |
3335ae4e | 182 | if ( ! f ) { |
4b992365 | 183 | g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno)); |
3335ae4e | 184 | g_free ( tmpfilename ); |
3335ae4e | 185 | return -4; |
932f8f22 GB |
186 | } |
187 | ||
188 | /* Call the backend function */ | |
825413ba | 189 | ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition, handle ); |
6a4a29aa JJ |
190 | |
191 | if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) { | |
1ac37c09 GB |
192 | g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret); |
193 | failure = TRUE; | |
194 | } | |
195 | ||
196 | if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) { | |
197 | g_debug("%s: file content checking failed", __FUNCTION__); | |
198 | failure = TRUE; | |
199 | } | |
932f8f22 | 200 | |
1ec35593 MA |
201 | fclose ( f ); |
202 | f = NULL; | |
203 | ||
1ac37c09 | 204 | if (failure) |
932f8f22 | 205 | { |
4258f4e2 | 206 | g_warning(_("Download error: %s"), fn); |
8c060406 | 207 | g_remove ( tmpfilename ); |
4b992365 | 208 | unlock_file ( tmpfilename ); |
3335ae4e | 209 | g_free ( tmpfilename ); |
8c060406 | 210 | g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ |
932f8f22 GB |
211 | return -1; |
212 | } | |
213 | ||
d4939f80 | 214 | if (ret == DOWNLOAD_NO_NEWER_FILE) { |
6a4a29aa | 215 | g_remove ( tmpfilename ); |
7de36baf GB |
216 | #if GLIB_CHECK_VERSION(2,18,0) |
217 | g_utime ( fn, NULL ); /* update mtime of local copy */ | |
218 | #else | |
d4939f80 | 219 | utimes ( fn, NULL ); /* update mtime of local copy */ |
7de36baf | 220 | #endif |
d4939f80 | 221 | } else |
6a4a29aa | 222 | g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */ |
4b992365 | 223 | unlock_file ( tmpfilename ); |
6a4a29aa | 224 | g_free ( tmpfilename ); |
1ec35593 | 225 | |
6a4a29aa | 226 | return 0; |
932f8f22 GB |
227 | } |
228 | ||
85611cd9 GB |
229 | /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */ |
230 | /* uri: like "/uri.html?whatever" */ | |
231 | /* only reason for the "wrapper" is so we can do redirects. */ | |
825413ba | 232 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle ) |
85611cd9 | 233 | { |
825413ba | 234 | return download ( hostname, uri, fn, opt, FALSE, handle ); |
0c1044e9 EB |
235 | } |
236 | ||
825413ba | 237 | int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle ) |
0c1044e9 | 238 | { |
825413ba SW |
239 | return download ( hostname, uri, fn, opt, TRUE, handle ); |
240 | } | |
241 | ||
242 | void * a_download_handle_init () | |
243 | { | |
244 | return curl_download_handle_init (); | |
245 | } | |
246 | ||
247 | void a_download_handle_cleanup ( void *handle ) | |
248 | { | |
249 | curl_download_handle_cleanup ( handle ); | |
85611cd9 | 250 | } |