]>
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> |
6a4a29aa JJ |
30 | #include <sys/types.h> |
31 | #include <utime.h> | |
f83131b9 MA |
32 | #include <glib.h> |
33 | #include <glib/gstdio.h> | |
4c77d5e0 | 34 | #include <glib/gi18n.h> |
932f8f22 | 35 | |
6a4a29aa | 36 | |
85611cd9 | 37 | #include "download.h" |
3292ba8b | 38 | |
3292ba8b | 39 | #include "curl_download.h" |
85611cd9 | 40 | |
388cf5a4 | 41 | static gboolean check_file_first_line(FILE* f, gchar *patterns[]) |
6e78a423 | 42 | { |
533bbf34 MA |
43 | gchar **s; |
44 | gchar *bp; | |
6e78a423 | 45 | fpos_t pos; |
533bbf34 | 46 | gchar buf[33]; |
6e78a423 | 47 | size_t nr; |
6e78a423 | 48 | |
c44594ee | 49 | memset(buf, 0, sizeof(buf)); |
6e78a423 QT |
50 | fgetpos(f, &pos); |
51 | rewind(f); | |
52 | nr = fread(buf, 1, sizeof(buf) - 1, f); | |
53 | fsetpos(f, &pos); | |
54 | for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) { | |
55 | if (!(isspace(*bp))) | |
56 | break; | |
57 | } | |
58 | if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr)) | |
1ac37c09 | 59 | return FALSE; |
388cf5a4 | 60 | for (s = patterns; *s; s++) { |
1918a993 | 61 | if (strncasecmp(*s, bp, strlen(*s)) == 0) |
1ac37c09 | 62 | return TRUE; |
6e78a423 | 63 | } |
1ac37c09 GB |
64 | return FALSE; |
65 | } | |
66 | ||
388cf5a4 GB |
67 | gboolean a_check_html_file(FILE* f) |
68 | { | |
69 | gchar * html_str[] = { | |
70 | "<html", | |
71 | "<!DOCTYPE html", | |
72 | "<head", | |
73 | "<title", | |
74 | NULL | |
75 | }; | |
76 | ||
77 | return check_file_first_line(f, html_str); | |
78 | } | |
79 | ||
1ac37c09 GB |
80 | gboolean a_check_map_file(FILE* f) |
81 | { | |
388cf5a4 | 82 | /* FIXME no more true since a_check_kml_file */ |
1ac37c09 | 83 | return !a_check_html_file(f); |
6e78a423 QT |
84 | } |
85 | ||
ae941b4c HMJ |
86 | gboolean a_check_kml_file(FILE* f) |
87 | { | |
388cf5a4 | 88 | gchar * kml_str[] = { |
ae941b4c HMJ |
89 | "<?xml", |
90 | NULL | |
91 | }; | |
92 | ||
388cf5a4 | 93 | return check_file_first_line(f, kml_str); |
ae941b4c HMJ |
94 | } |
95 | ||
0c1044e9 | 96 | static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp) |
932f8f22 GB |
97 | { |
98 | FILE *f; | |
99 | int ret; | |
533bbf34 | 100 | gchar *tmpfilename; |
1ac37c09 | 101 | gboolean failure = FALSE; |
6a4a29aa | 102 | time_t time_condition = 0; |
932f8f22 GB |
103 | |
104 | /* Check file */ | |
45acf79e | 105 | if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE ) |
932f8f22 | 106 | { |
6a4a29aa JJ |
107 | if (options != NULL && options->check_file_server_time) { |
108 | /* Get the modified time of this file */ | |
109 | struct stat buf; | |
110 | g_stat ( fn, &buf ); | |
111 | time_condition = buf.st_mtime; | |
112 | } else { | |
113 | /* Nothing to do as file already exists, so return */ | |
114 | return -3; | |
115 | } | |
932f8f22 | 116 | } else { |
a1618d62 MA |
117 | gchar *dir = g_path_get_dirname ( fn ); |
118 | g_mkdir_with_parents ( dir , 0777 ); | |
119 | g_free ( dir ); | |
3335ae4e EB |
120 | } |
121 | ||
122 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
6a4a29aa | 123 | f = g_fopen ( tmpfilename, "w+bx" ); /* truncate file and open it in exclusive mode */ |
3335ae4e | 124 | if ( ! f ) { |
6a4a29aa JJ |
125 | if (errno == EEXIST) |
126 | g_debug("%s: Couldn't take lock on temporary file \"%s\"\n", __FUNCTION__, tmpfilename); | |
3335ae4e | 127 | g_free ( tmpfilename ); |
3335ae4e | 128 | return -4; |
932f8f22 GB |
129 | } |
130 | ||
131 | /* Call the backend function */ | |
6a4a29aa JJ |
132 | ret = curl_download_get_url ( hostname, uri, f, options, ftp, time_condition ); |
133 | ||
134 | if (ret != DOWNLOAD_NO_ERROR && ret != DOWNLOAD_NO_NEWER_FILE) { | |
1ac37c09 GB |
135 | g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret); |
136 | failure = TRUE; | |
137 | } | |
138 | ||
139 | if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) { | |
140 | g_debug("%s: file content checking failed", __FUNCTION__); | |
141 | failure = TRUE; | |
142 | } | |
932f8f22 | 143 | |
1ac37c09 | 144 | if (failure) |
932f8f22 | 145 | { |
4258f4e2 | 146 | g_warning(_("Download error: %s"), fn); |
8c060406 | 147 | g_remove ( tmpfilename ); |
3335ae4e | 148 | g_free ( tmpfilename ); |
6a4a29aa JJ |
149 | fclose ( f ); |
150 | f = NULL; | |
8c060406 | 151 | g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ |
932f8f22 GB |
152 | return -1; |
153 | } | |
154 | ||
6a4a29aa JJ |
155 | if (ret == DOWNLOAD_NO_NEWER_FILE) |
156 | g_remove ( tmpfilename ); | |
157 | else | |
158 | g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */ | |
159 | g_free ( tmpfilename ); | |
932f8f22 | 160 | fclose ( f ); |
8c060406 | 161 | f = NULL; |
6a4a29aa | 162 | return 0; |
932f8f22 GB |
163 | } |
164 | ||
85611cd9 GB |
165 | /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */ |
166 | /* uri: like "/uri.html?whatever" */ | |
167 | /* only reason for the "wrapper" is so we can do redirects. */ | |
80214df2 | 168 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt ) |
85611cd9 | 169 | { |
0c1044e9 EB |
170 | return download ( hostname, uri, fn, opt, FALSE ); |
171 | } | |
172 | ||
173 | int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt ) | |
174 | { | |
175 | return download ( hostname, uri, fn, opt, TRUE ); | |
85611cd9 | 176 | } |