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