2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
33 #include <sys/types.h>
34 #include <glib/gi18n.h>
38 #include "curl_download.h"
43 #define access(a,b) _access(a,b)
44 #define close(a) closesocket(a)
46 char *dirname ( char * dir )
48 char *tmp = dir + strlen(dir) - 1;
49 while ( tmp != dir && *tmp != '\\' )
58 #include <sys/types.h>
65 static int check_map_file(FILE* f)
69 int res = 0; /* good */
82 bzero(buf, sizeof(buf));
85 nr = fread(buf, 1, sizeof(buf) - 1, f);
87 for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
91 if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
93 for (s = html_str; *s; s++) {
94 if (strncasecmp(*s, bp, strlen(*s)) == 0)
100 static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
107 if ( access ( fn, F_OK ) == 0 )
109 /* File exists: return */
112 if ( errno == ENOENT)
114 char *tmp = g_strdup ( fn );
116 mkdir( dirname ( dirname ( tmp ) ) );
117 g_free ( tmp ); tmp = g_strdup ( fn );
118 mkdir( dirname ( tmp ) );
120 mkdir( dirname ( dirname ( tmp ) ), 0777 );
121 g_free ( tmp ); tmp = g_strdup ( fn );
122 mkdir( dirname ( tmp ), 0777 );
126 /* create placeholder file */
127 if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
132 tmpfilename = g_strdup_printf("%s.tmp", fn);
133 f = fopen ( tmpfilename, "w+b" );
135 g_free ( tmpfilename );
136 remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
140 /* Call the backend function */
141 ret = curl_download_get_url ( hostname, uri, f, options, ftp );
143 if (ret == -1 || ret == 1 || ret == -2 || check_map_file(f))
145 g_warning(_("Download error: %s"), fn);
147 remove ( tmpfilename );
148 g_free ( tmpfilename );
149 remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
154 rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
155 g_free ( tmpfilename );
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. */
162 int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
164 return download ( hostname, uri, fn, opt, FALSE );
167 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
169 return download ( hostname, uri, fn, opt, TRUE );