2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
37 #include <glib/gstdio.h>
38 #include <glib/gi18n.h>
43 #include "curl_download.h"
44 #include "preferences.h"
47 static gboolean check_file_first_line(FILE* f, gchar *patterns[])
55 memset(buf, 0, sizeof(buf));
58 nr = fread(buf, 1, sizeof(buf) - 1, f);
60 for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
64 if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
66 for (s = patterns; *s; s++) {
67 if (strncasecmp(*s, bp, strlen(*s)) == 0)
73 gboolean a_check_html_file(FILE* f)
75 gchar * html_str[] = {
83 return check_file_first_line(f, html_str);
86 gboolean a_check_map_file(FILE* f)
88 /* FIXME no more true since a_check_kml_file */
89 return !a_check_html_file(f);
92 gboolean a_check_kml_file(FILE* f)
99 return check_file_first_line(f, kml_str);
102 static GList *file_list = NULL;
103 static GMutex *file_list_mutex = NULL;
105 /* spin button scales */
106 VikLayerParamScale params_scales[] = {
107 {1, 86400*7, 10, 0}, /* download_tile_age */
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 },
114 void a_download_init (void)
116 VikLayerParamData tmp;
117 tmp.u = VIK_CONFIG_DEFAULT_TILE_AGE;
118 a_preferences_register(prefs, tmp, VIKING_PREFERENCES_GROUP_KEY);
120 file_list_mutex = g_mutex_new();
123 static gboolean lock_file(const char *fn)
125 gboolean locked = FALSE;
126 g_mutex_lock(file_list_mutex);
127 if (g_list_find(file_list, fn) == NULL)
129 // The filename is not yet locked
130 file_list = g_list_append(file_list, (gpointer)fn),
133 g_mutex_unlock(file_list_mutex);
137 static void unlock_file(const char *fn)
139 g_mutex_lock(file_list_mutex);
140 file_list = g_list_remove(file_list, (gconstpointer)fn);
141 g_mutex_unlock(file_list_mutex);
144 static int download( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *options, gboolean ftp, void *handle)
149 gboolean failure = FALSE;
150 DownloadFileOptions file_options = {0, NULL, NULL};
153 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
155 if (options == NULL || (!options->check_file_server_time &&
156 !options->use_etag)) {
157 /* Nothing to do as file already exists and we don't want to check server */
161 time_t tile_age = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "download_tile_age")->u;
162 /* Get the modified time of this file */
165 time_t file_time = buf.st_mtime;
166 if ( (time(NULL) - file_time) < tile_age ) {
167 /* File cache is too recent, so return */
171 if (options->check_file_server_time) {
172 file_options.time_condition = file_time;
174 if (options->use_etag) {
175 gchar *etag_filename = g_strdup_printf("%s.etag", fn);
177 g_file_get_contents (etag_filename, &(file_options.etag), &etag_length, NULL);
179 /* check if etag is short enough */
180 if (etag_length > 100)
181 g_free(file_options.etag);
183 /* TODO: should check that etag is a valid string */
187 gchar *dir = g_path_get_dirname ( fn );
188 g_mkdir_with_parents ( dir , 0777 );
192 tmpfilename = g_strdup_printf("%s.tmp", fn);
193 if (!lock_file ( tmpfilename ) )
195 g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename);
196 g_free ( tmpfilename );
199 f = g_fopen ( tmpfilename, "w+b" ); /* truncate file and open it */
201 g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno));
202 g_free ( tmpfilename );
206 /* Call the backend function */
207 ret = curl_download_get_url ( hostname, uri, f, options, ftp, &file_options, handle );
209 if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) {
210 g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
214 if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
215 g_debug("%s: file content checking failed", __FUNCTION__);
224 g_warning(_("Download error: %s"), fn);
225 g_remove ( tmpfilename );
226 unlock_file ( tmpfilename );
227 g_free ( tmpfilename );
228 if (options->use_etag) {
229 g_free ( file_options.etag );
230 g_free ( file_options.new_etag );
232 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
236 if (options->use_etag) {
237 if (file_options.new_etag) {
238 /* server returned an etag value */
239 gchar *etag_filename = g_strdup_printf("%s.etag", fn);
240 g_file_set_contents (etag_filename, file_options.new_etag, -1, NULL);
244 if (ret == DOWNLOAD_NO_NEWER_FILE) {
245 g_remove ( tmpfilename );
246 #if GLIB_CHECK_VERSION(2,18,0)
247 g_utime ( fn, NULL ); /* update mtime of local copy */
249 utimes ( fn, NULL ); /* update mtime of local copy */
252 g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
253 unlock_file ( tmpfilename );
254 g_free ( tmpfilename );
256 if (options->use_etag) {
257 g_free ( file_options.etag );
258 g_free ( file_options.new_etag );
263 /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
264 /* uri: like "/uri.html?whatever" */
265 /* only reason for the "wrapper" is so we can do redirects. */
266 int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
268 return download ( hostname, uri, fn, opt, FALSE, handle );
271 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadMapOptions *opt, void *handle )
273 return download ( hostname, uri, fn, opt, TRUE, handle );
276 void * a_download_handle_init ()
278 return curl_download_handle_init ();
281 void a_download_handle_cleanup ( void *handle )
283 curl_download_handle_cleanup ( handle );