]>
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" |
85611cd9 | 44 | |
388cf5a4 | 45 | static gboolean check_file_first_line(FILE* f, gchar *patterns[]) |
6e78a423 | 46 | { |
533bbf34 MA |
47 | gchar **s; |
48 | gchar *bp; | |
6e78a423 | 49 | fpos_t pos; |
533bbf34 | 50 | gchar buf[33]; |
6e78a423 | 51 | size_t nr; |
6e78a423 | 52 | |
c44594ee | 53 | memset(buf, 0, sizeof(buf)); |
6e78a423 QT |
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)) | |
1ac37c09 | 63 | return FALSE; |
388cf5a4 | 64 | for (s = patterns; *s; s++) { |
1918a993 | 65 | if (strncasecmp(*s, bp, strlen(*s)) == 0) |
1ac37c09 | 66 | return TRUE; |
6e78a423 | 67 | } |
1ac37c09 GB |
68 | return FALSE; |
69 | } | |
70 | ||
388cf5a4 GB |
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 | ||
1ac37c09 GB |
84 | gboolean a_check_map_file(FILE* f) |
85 | { | |
388cf5a4 | 86 | /* FIXME no more true since a_check_kml_file */ |
1ac37c09 | 87 | return !a_check_html_file(f); |
6e78a423 QT |
88 | } |
89 | ||
ae941b4c HMJ |
90 | gboolean a_check_kml_file(FILE* f) |
91 | { | |
388cf5a4 | 92 | gchar * kml_str[] = { |
ae941b4c HMJ |
93 | "<?xml", |
94 | NULL | |
95 | }; | |
96 | ||
388cf5a4 | 97 | return check_file_first_line(f, kml_str); |
ae941b4c HMJ |
98 | } |
99 | ||
4b992365 GB |
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 | ||
825413ba | 129 | static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp, void *handle) |
932f8f22 GB |
130 | { |
131 | FILE *f; | |
132 | int ret; | |
533bbf34 | 133 | gchar *tmpfilename; |
1ac37c09 | 134 | gboolean failure = FALSE; |
6a4a29aa | 135 | time_t time_condition = 0; |
932f8f22 GB |
136 | |
137 | /* Check file */ | |
45acf79e | 138 | if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE ) |
932f8f22 | 139 | { |
6a4a29aa JJ |
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; | |
ae40c2dc GB |
145 | if ( (time(NULL) - time_condition) < options->check_file_server_time ) |
146 | /* File cache is too recent, so return */ | |
147 | return -3; | |
6a4a29aa JJ |
148 | } else { |
149 | /* Nothing to do as file already exists, so return */ | |
150 | return -3; | |
151 | } | |
932f8f22 | 152 | } else { |
a1618d62 MA |
153 | gchar *dir = g_path_get_dirname ( fn ); |
154 | g_mkdir_with_parents ( dir , 0777 ); | |
155 | g_free ( dir ); | |
3335ae4e EB |
156 | } |
157 | ||
158 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
4b992365 GB |
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 */ | |
3335ae4e | 166 | if ( ! f ) { |
4b992365 | 167 | g_warning("Couldn't open temporary file \"%s\": %s", tmpfilename, g_strerror(errno)); |
3335ae4e | 168 | g_free ( tmpfilename ); |
3335ae4e | 169 | return -4; |
932f8f22 GB |
170 | } |
171 | ||
172 | /* Call the backend function */ | |
825413ba | 173 | ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition, handle ); |
6a4a29aa JJ |
174 | |
175 | if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) { | |
1ac37c09 GB |
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 | } | |
932f8f22 | 184 | |
1ac37c09 | 185 | if (failure) |
932f8f22 | 186 | { |
4258f4e2 | 187 | g_warning(_("Download error: %s"), fn); |
8c060406 | 188 | g_remove ( tmpfilename ); |
4b992365 | 189 | unlock_file ( tmpfilename ); |
3335ae4e | 190 | g_free ( tmpfilename ); |
6a4a29aa JJ |
191 | fclose ( f ); |
192 | f = NULL; | |
8c060406 | 193 | g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ |
932f8f22 GB |
194 | return -1; |
195 | } | |
196 | ||
d4939f80 | 197 | if (ret == DOWNLOAD_NO_NEWER_FILE) { |
6a4a29aa | 198 | g_remove ( tmpfilename ); |
7de36baf GB |
199 | #if GLIB_CHECK_VERSION(2,18,0) |
200 | g_utime ( fn, NULL ); /* update mtime of local copy */ | |
201 | #else | |
d4939f80 | 202 | utimes ( fn, NULL ); /* update mtime of local copy */ |
7de36baf | 203 | #endif |
d4939f80 | 204 | } else |
6a4a29aa | 205 | g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */ |
4b992365 | 206 | unlock_file ( tmpfilename ); |
6a4a29aa | 207 | g_free ( tmpfilename ); |
932f8f22 | 208 | fclose ( f ); |
8c060406 | 209 | f = NULL; |
6a4a29aa | 210 | return 0; |
932f8f22 GB |
211 | } |
212 | ||
85611cd9 GB |
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. */ | |
825413ba | 216 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle ) |
85611cd9 | 217 | { |
825413ba | 218 | return download ( hostname, uri, fn, opt, FALSE, handle ); |
0c1044e9 EB |
219 | } |
220 | ||
825413ba | 221 | int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt, void *handle ) |
0c1044e9 | 222 | { |
825413ba SW |
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 ); | |
85611cd9 | 234 | } |