]>
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> | |
28 | #include <gtk/gtk.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/types.h> | |
31 | ||
85611cd9 | 32 | #include "download.h" |
3292ba8b GB |
33 | |
34 | #ifdef HAVE_LIBCURL | |
35 | #include "curl_download.h" | |
36 | #else | |
85611cd9 | 37 | #include "http.h" |
3292ba8b | 38 | #endif |
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 | ||
65 | static int download( const char *hostname, const char *uri, const char *fn, int sendhostname) | |
66 | { | |
67 | FILE *f; | |
68 | int ret; | |
69 | ||
70 | /* Check file */ | |
71 | if ( access ( fn, F_OK ) == 0 ) | |
72 | { | |
73 | /* File exists: return */ | |
74 | return -3; | |
75 | } else { | |
76 | if ( errno == ENOENT) | |
77 | { | |
78 | char *tmp = g_strdup ( fn ); | |
79 | #ifdef WINDOWS | |
80 | mkdir( dirname ( dirname ( tmp ) ) ); | |
81 | g_free ( tmp ); tmp = g_strdup ( fn ); | |
82 | mkdir( dirname ( tmp ) ); | |
83 | #else | |
84 | mkdir( dirname ( dirname ( tmp ) ), 0777 ); | |
85 | g_free ( tmp ); tmp = g_strdup ( fn ); | |
86 | mkdir( dirname ( tmp ), 0777 ); | |
87 | #endif | |
88 | g_free ( tmp ); | |
89 | } | |
90 | if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */ | |
91 | return -4; | |
92 | } | |
93 | ||
94 | /* Call the backend function */ | |
3292ba8b GB |
95 | #ifdef HAVE_LIBCURL |
96 | ret = curl_download_get_url ( hostname, uri, f ); | |
97 | #else | |
932f8f22 | 98 | ret = http_download_get_url ( hostname, uri, f, 0, sendhostname ); |
3292ba8b | 99 | #endif |
932f8f22 GB |
100 | |
101 | if (ret == -1 || ret == 1 || ret == -2) | |
102 | { | |
103 | fclose ( f ); | |
104 | remove ( fn ); | |
105 | return -1; | |
106 | } | |
107 | ||
108 | fclose ( f ); | |
109 | return ret; | |
110 | } | |
111 | ||
85611cd9 GB |
112 | /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */ |
113 | /* uri: like "/uri.html?whatever" */ | |
114 | /* only reason for the "wrapper" is so we can do redirects. */ | |
115 | int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn ) | |
116 | { | |
932f8f22 | 117 | return download ( hostname, uri, fn, 1 ); |
85611cd9 GB |
118 | } |
119 | ||
120 | int a_http_download_get_url_nohostname ( const char *hostname, const char *uri, const char *fn ) | |
121 | { | |
932f8f22 | 122 | return download ( hostname, uri, fn, 0 ); |
85611cd9 | 123 | } |