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"
45 static gboolean check_file_first_line(FILE* f, gchar *patterns[])
53 memset(buf, 0, sizeof(buf));
56 nr = fread(buf, 1, sizeof(buf) - 1, f);
58 for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
62 if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
64 for (s = patterns; *s; s++) {
65 if (strncasecmp(*s, bp, strlen(*s)) == 0)
71 gboolean a_check_html_file(FILE* f)
73 gchar * html_str[] = {
81 return check_file_first_line(f, html_str);
84 gboolean a_check_map_file(FILE* f)
86 /* FIXME no more true since a_check_kml_file */
87 return !a_check_html_file(f);
90 gboolean a_check_kml_file(FILE* f)
97 return check_file_first_line(f, kml_str);
100 static GList *file_list = NULL;
101 static GMutex *file_list_mutex = NULL;
103 void a_download_init (void)
105 file_list_mutex = g_mutex_new();
108 static gboolean lock_file(const char *fn)
110 gboolean locked = FALSE;
111 g_mutex_lock(file_list_mutex);
112 if (g_list_find(file_list, fn) == NULL)
114 // The filename is not yet locked
115 file_list = g_list_append(file_list, (gpointer)fn),
118 g_mutex_unlock(file_list_mutex);
122 static void unlock_file(const char *fn)
124 g_mutex_lock(file_list_mutex);
125 file_list = g_list_remove(file_list, (gconstpointer)fn);
126 g_mutex_unlock(file_list_mutex);
129 static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp, void *handle)
134 gboolean failure = FALSE;
135 time_t time_condition = 0;
138 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
140 if (options != NULL && options->check_file_server_time) {
141 /* Get the modified time of this file */
144 time_condition = buf.st_mtime;
145 if ( (time(NULL) - time_condition) < options->check_file_server_time )
146 /* File cache is too recent, so return */
149 /* Nothing to do as file already exists, so return */
153 gchar *dir = g_path_get_dirname ( fn );
154 g_mkdir_with_parents ( dir , 0777 );
158 tmpfilename = g_strdup_printf("%s.tmp", fn);
159 if (!lock_file ( tmpfilename ) )
161 g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename);
162 g_free ( tmpfilename );
165 f = g_fopen ( tmpfilename, "w+b" ); /* truncate file and open it */
167 g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno));
168 g_free ( tmpfilename );
172 /* Call the backend function */
173 ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition, handle );
175 if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) {
176 g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
180 if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
181 g_debug("%s: file content checking failed", __FUNCTION__);
187 g_warning(_("Download error: %s"), fn);
188 g_remove ( tmpfilename );
189 unlock_file ( tmpfilename );
190 g_free ( tmpfilename );
193 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
197 if (ret == DOWNLOAD_NO_NEWER_FILE) {
198 g_remove ( tmpfilename );
199 #if GLIB_CHECK_VERSION(2,18,0)
200 g_utime ( fn, NULL ); /* update mtime of local copy */
202 utimes ( fn, NULL ); /* update mtime of local copy */
205 g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
206 unlock_file ( tmpfilename );
207 g_free ( tmpfilename );
213 /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
214 /* uri: like "/uri.html?whatever" */
215 /* only reason for the "wrapper" is so we can do redirects. */
216 int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle )
218 return download ( hostname, uri, fn, opt, FALSE, handle );
221 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle )
223 return download ( hostname, uri, fn, opt, TRUE, handle );
226 void * a_download_handle_init ()
228 return curl_download_handle_init ();
231 void a_download_handle_cleanup ( void *handle )
233 curl_download_handle_cleanup ( handle );