]> git.street.me.uk Git - andy/viking.git/blob - src/download.c
Portability: use GLib function g_utime if possible
[andy/viking.git] / src / download.c
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
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <string.h>
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33 #ifdef HAVE_UTIME_H
34 #include <utime.h>
35 #endif
36 #include <glib.h>
37 #include <glib/gstdio.h>
38 #include <glib/gi18n.h>
39
40
41 #include "download.h"
42
43 #include "curl_download.h"
44
45 static gboolean check_file_first_line(FILE* f, gchar *patterns[])
46 {
47   gchar **s;
48   gchar *bp;
49   fpos_t pos;
50   gchar buf[33];
51   size_t nr;
52
53   memset(buf, 0, sizeof(buf));
54   fgetpos(f, &pos);
55   rewind(f);
56   nr = fread(buf, 1, sizeof(buf) - 1, f);
57   fsetpos(f, &pos);
58   for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
59     if (!(isspace(*bp)))
60       break;
61   }
62   if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
63     return FALSE;
64   for (s = patterns; *s; s++) {
65     if (strncasecmp(*s, bp, strlen(*s)) == 0)
66       return TRUE;
67   }
68   return FALSE;
69 }
70
71 gboolean a_check_html_file(FILE* f)
72 {
73   gchar * html_str[] = {
74     "<html",
75     "<!DOCTYPE html",
76     "<head",
77     "<title",
78     NULL
79   };
80
81   return check_file_first_line(f, html_str);
82 }
83
84 gboolean a_check_map_file(FILE* f)
85 {
86   /* FIXME no more true since a_check_kml_file */
87   return !a_check_html_file(f);
88 }
89
90 gboolean a_check_kml_file(FILE* f)
91 {
92   gchar * kml_str[] = {
93     "<?xml",
94     NULL
95   };
96
97   return check_file_first_line(f, kml_str);
98 }
99
100 static GList *file_list = NULL;
101 static GMutex *file_list_mutex = NULL;
102
103 void a_download_init (void)
104 {
105         file_list_mutex = g_mutex_new();
106 }
107
108 static gboolean lock_file(const char *fn)
109 {
110         gboolean locked = FALSE;
111         g_mutex_lock(file_list_mutex);
112         if (g_list_find(file_list, fn) == NULL)
113         {
114                 // The filename is not yet locked
115                 file_list = g_list_append(file_list, (gpointer)fn),
116                 locked = TRUE;
117         }
118         g_mutex_unlock(file_list_mutex);
119         return locked;
120 }
121
122 static void unlock_file(const char *fn)
123 {
124         g_mutex_lock(file_list_mutex);
125         file_list = g_list_remove(file_list, (gconstpointer)fn);
126         g_mutex_unlock(file_list_mutex);
127 }
128
129 static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp, void *handle)
130 {
131   FILE *f;
132   int ret;
133   gchar *tmpfilename;
134   gboolean failure = FALSE;
135   time_t time_condition = 0;
136
137   /* Check file */
138   if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
139   {
140     if (options != NULL && options->check_file_server_time) {
141       /* Get the modified time of this file */
142       struct stat buf;
143       g_stat ( fn, &buf );
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 */
147                                 return -3;
148     } else {
149       /* Nothing to do as file already exists, so return */
150       return -3;
151     }
152   } else {
153     gchar *dir = g_path_get_dirname ( fn );
154     g_mkdir_with_parents ( dir , 0777 );
155     g_free ( dir );
156   }
157
158   tmpfilename = g_strdup_printf("%s.tmp", fn);
159   if (!lock_file ( tmpfilename ) )
160   {
161     g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename);
162     g_free ( tmpfilename );
163     return -4;
164   }
165   f = g_fopen ( tmpfilename, "w+b" );  /* truncate file and open it */
166   if ( ! f ) {
167     g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno));
168     g_free ( tmpfilename );
169     return -4;
170   }
171
172   /* Call the backend function */
173   ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition, handle );
174
175   if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) {
176     g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
177     failure = TRUE;
178   }
179
180   if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
181     g_debug("%s: file content checking failed", __FUNCTION__);
182     failure = TRUE;
183   }
184
185   if (failure)
186   {
187     g_warning(_("Download error: %s"), fn);
188     g_remove ( tmpfilename );
189     unlock_file ( tmpfilename );
190     g_free ( tmpfilename );
191     fclose ( f );
192     f = NULL;
193     g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
194     return -1;
195   }
196
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 */
201 #else
202     utimes ( fn, NULL ); /* update mtime of local copy */
203 #endif
204   } else
205     g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
206   unlock_file ( tmpfilename );
207   g_free ( tmpfilename );
208   fclose ( f );
209   f = NULL;
210   return 0;
211 }
212
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 )
217 {
218   return download ( hostname, uri, fn, opt, FALSE, handle );
219 }
220
221 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle )
222 {
223   return download ( hostname, uri, fn, opt, TRUE, handle );
224 }
225
226 void * a_download_handle_init ()
227 {
228   return curl_download_handle_init ();
229 }
230
231 void a_download_handle_cleanup ( void *handle )
232 {
233   curl_download_handle_cleanup ( handle );
234 }