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