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