]>
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 QT |
27 | #include <ctype.h> |
28 | #include <string.h> | |
f83131b9 MA |
29 | #include <glib.h> |
30 | #include <glib/gstdio.h> | |
4c77d5e0 | 31 | #include <glib/gi18n.h> |
932f8f22 | 32 | |
85611cd9 | 33 | #include "download.h" |
3292ba8b | 34 | |
3292ba8b | 35 | #include "curl_download.h" |
85611cd9 | 36 | |
6e78a423 QT |
37 | static int check_map_file(FILE* f) |
38 | { | |
39 | char **s; | |
40 | char *bp; | |
41 | int res = 0; /* good */ | |
42 | fpos_t pos; | |
43 | char buf[33]; | |
44 | size_t nr; | |
45 | char * html_str[] = { | |
46 | "<html", | |
47 | "<!DOCTYPE html", | |
48 | "<head", | |
49 | "<title", | |
50 | NULL | |
51 | }; | |
52 | ||
53 | ||
c44594ee | 54 | memset(buf, 0, sizeof(buf)); |
6e78a423 QT |
55 | fgetpos(f, &pos); |
56 | rewind(f); | |
57 | nr = fread(buf, 1, sizeof(buf) - 1, f); | |
58 | fsetpos(f, &pos); | |
59 | for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) { | |
60 | if (!(isspace(*bp))) | |
61 | break; | |
62 | } | |
63 | if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr)) | |
64 | return(res); | |
65 | for (s = html_str; *s; s++) { | |
1918a993 | 66 | if (strncasecmp(*s, bp, strlen(*s)) == 0) |
6e78a423 QT |
67 | return(-1); |
68 | } | |
69 | return(res); | |
70 | } | |
71 | ||
0c1044e9 | 72 | static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp) |
932f8f22 GB |
73 | { |
74 | FILE *f; | |
75 | int ret; | |
3335ae4e | 76 | char *tmpfilename; |
932f8f22 GB |
77 | |
78 | /* Check file */ | |
45acf79e | 79 | if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE ) |
932f8f22 GB |
80 | { |
81 | /* File exists: return */ | |
82 | return -3; | |
83 | } else { | |
a1618d62 MA |
84 | gchar *dir = g_path_get_dirname ( fn ); |
85 | g_mkdir_with_parents ( dir , 0777 ); | |
86 | g_free ( dir ); | |
87 | ||
3335ae4e | 88 | /* create placeholder file */ |
8c060406 | 89 | if ( ! (f = g_fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */ |
932f8f22 | 90 | return -4; |
3335ae4e | 91 | fclose ( f ); |
8c060406 | 92 | f = NULL; |
3335ae4e EB |
93 | } |
94 | ||
95 | tmpfilename = g_strdup_printf("%s.tmp", fn); | |
8c060406 | 96 | f = g_fopen ( tmpfilename, "w+b" ); |
3335ae4e EB |
97 | if ( ! f ) { |
98 | g_free ( tmpfilename ); | |
8c060406 | 99 | g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ |
3335ae4e | 100 | return -4; |
932f8f22 GB |
101 | } |
102 | ||
103 | /* Call the backend function */ | |
0c1044e9 | 104 | ret = curl_download_get_url ( hostname, uri, f, options, ftp ); |
932f8f22 | 105 | |
6e78a423 | 106 | if (ret == -1 || ret == 1 || ret == -2 || check_map_file(f)) |
932f8f22 | 107 | { |
4258f4e2 | 108 | g_warning(_("Download error: %s"), fn); |
932f8f22 | 109 | fclose ( f ); |
8c060406 MA |
110 | f = NULL; |
111 | g_remove ( tmpfilename ); | |
3335ae4e | 112 | g_free ( tmpfilename ); |
8c060406 | 113 | g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */ |
932f8f22 GB |
114 | return -1; |
115 | } | |
116 | ||
117 | fclose ( f ); | |
8c060406 | 118 | f = NULL; |
3335ae4e EB |
119 | rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */ |
120 | g_free ( tmpfilename ); | |
932f8f22 GB |
121 | return ret; |
122 | } | |
123 | ||
85611cd9 GB |
124 | /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */ |
125 | /* uri: like "/uri.html?whatever" */ | |
126 | /* only reason for the "wrapper" is so we can do redirects. */ | |
80214df2 | 127 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt ) |
85611cd9 | 128 | { |
0c1044e9 EB |
129 | return download ( hostname, uri, fn, opt, FALSE ); |
130 | } | |
131 | ||
132 | int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt ) | |
133 | { | |
134 | return download ( hostname, uri, fn, opt, TRUE ); | |
85611cd9 | 135 | } |