]>
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 GB |
26 | #include <stdio.h> |
27 | #include <errno.h> | |
6e78a423 QT |
28 | #include <ctype.h> |
29 | #include <string.h> | |
30 | #include <strings.h> | |
932f8f22 GB |
31 | #include <gtk/gtk.h> |
32 | #include <sys/stat.h> | |
33 | #include <sys/types.h> | |
4c77d5e0 | 34 | #include <glib/gi18n.h> |
932f8f22 | 35 | |
85611cd9 | 36 | #include "download.h" |
3292ba8b | 37 | |
3292ba8b | 38 | #include "curl_download.h" |
85611cd9 | 39 | |
932f8f22 GB |
40 | #ifdef WINDOWS |
41 | ||
42 | #include <io.h> | |
43 | #define access(a,b) _access(a,b) | |
44 | #define close(a) closesocket(a) | |
45 | ||
46 | char *dirname ( char * dir ) | |
47 | { | |
48 | char *tmp = dir + strlen(dir) - 1; | |
49 | while ( tmp != dir && *tmp != '\\' ) | |
50 | tmp--; | |
51 | *tmp = '\0'; | |
52 | return dir; | |
53 | } | |
54 | ||
55 | #else | |
56 | ||
57 | #include <unistd.h> | |
58 | #include <sys/types.h> | |
59 | ||
60 | /* dirname */ | |
61 | #include <libgen.h> | |
62 | ||
63 | #endif | |
64 | ||
6e78a423 QT |
65 | static int check_map_file(FILE* f) |
66 | { | |
67 | char **s; | |
68 | char *bp; | |
69 | int res = 0; /* good */ | |
70 | fpos_t pos; | |
71 | char buf[33]; | |
72 | size_t nr; | |
73 | char * html_str[] = { | |
74 | "<html", | |
75 | "<!DOCTYPE html", | |
76 | "<head", | |
77 | "<title", | |
78 | NULL | |
79 | }; | |
80 | ||
81 | ||
82 | bzero(buf, sizeof(buf)); | |
83 | fgetpos(f, &pos); | |
84 | rewind(f); | |
85 | nr = fread(buf, 1, sizeof(buf) - 1, f); | |
86 | fsetpos(f, &pos); | |
87 | for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) { | |
88 | if (!(isspace(*bp))) | |
89 | break; | |
90 | } | |
91 | if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr)) | |
92 | return(res); | |
93 | for (s = html_str; *s; s++) { | |
1918a993 | 94 | if (strncasecmp(*s, bp, strlen(*s)) == 0) |
6e78a423 QT |
95 | return(-1); |
96 | } | |
97 | return(res); | |
98 | } | |
99 | ||
0c1044e9 | 100 | static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp) |
932f8f22 GB |
101 | { |
102 | FILE *f; | |
103 | int ret; | |
3335ae4e | 104 | char *tmpfilename; |
932f8f22 GB |
105 | |
106 | /* Check file */ | |
107 | if ( access ( fn, F_OK ) == 0 ) | |
108 | { | |
109 | /* File exists: return */ | |
110 | return -3; | |
111 | } else { | |
112 | if ( errno == ENOENT) | |
113 | { | |
114 | char *tmp = g_strdup ( fn ); | |
115 | #ifdef WINDOWS | |
116 | mkdir( dirname ( dirname ( tmp ) ) ); | |
117 | g_free ( tmp ); tmp = g_strdup ( fn ); | |
118 | mkdir( dirname ( tmp ) ); | |
119 | #else | |
120 | mkdir( dirname ( dirname ( tmp ) ), 0777 ); | |
121 | g_free ( tmp ); tmp = g_strdup ( fn ); | |
122 | mkdir( dirname ( tmp ), 0777 ); | |
123 | #endif | |
124 | g_free ( tmp ); | |
125 | } | |
3335ae4e | 126 | /* create placeholder file */ |
932f8f22 GB |
127 | if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */ |
128 | return -4; | |
3335ae4e EB |
129 | fclose ( f ); |
130 | } | |
131 | ||
132 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
133 | f = fopen ( tmpfilename, "w+b" ); | |
134 | if ( ! f ) { | |
135 | g_free ( tmpfilename ); | |
136 | remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ | |
137 | return -4; | |
932f8f22 GB |
138 | } |
139 | ||
140 | /* Call the backend function */ | |
0c1044e9 | 141 | ret = curl_download_get_url ( hostname, uri, f, options, ftp ); |
932f8f22 | 142 | |
6e78a423 | 143 | if (ret == -1 || ret == 1 || ret == -2 || check_map_file(f)) |
932f8f22 | 144 | { |
4c77d5e0 | 145 | g_warning(_("Download error: %s\n"), fn); |
932f8f22 | 146 | fclose ( f ); |
3335ae4e EB |
147 | remove ( tmpfilename ); |
148 | g_free ( tmpfilename ); | |
6e78a423 | 149 | remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ |
932f8f22 GB |
150 | return -1; |
151 | } | |
152 | ||
153 | fclose ( f ); | |
3335ae4e EB |
154 | rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */ |
155 | g_free ( tmpfilename ); | |
932f8f22 GB |
156 | return ret; |
157 | } | |
158 | ||
85611cd9 GB |
159 | /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */ |
160 | /* uri: like "/uri.html?whatever" */ | |
161 | /* only reason for the "wrapper" is so we can do redirects. */ | |
80214df2 | 162 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt ) |
85611cd9 | 163 | { |
0c1044e9 EB |
164 | return download ( hostname, uri, fn, opt, FALSE ); |
165 | } | |
166 | ||
167 | int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt ) | |
168 | { | |
169 | return download ( hostname, uri, fn, opt, TRUE ); | |
85611cd9 | 170 | } |