]> git.street.me.uk Git - andy/viking.git/blob - src/download.c
Remove trailing '\n' in g_message calls
[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 #include <glib/gi18n.h>
35
36 #include "download.h"
37
38 #include "curl_download.h"
39
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 check_map_file(FILE* f)
66 {
67   char **s;
68   char *bp;
69   int res = 0;  /* good */
70   fpos_t pos;
71   char buf[33];
72   size_t nr;
73   char * html_str[] = {
74     "<html",
75     "<!DOCTYPE html",
76     "<head",
77     "<title",
78     NULL
79   };
80
81
82   bzero(buf, sizeof(buf));
83   fgetpos(f, &pos);
84   rewind(f);
85   nr = fread(buf, 1, sizeof(buf) - 1, f);
86   fsetpos(f, &pos);
87   for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
88     if (!(isspace(*bp)))
89       break;
90   }
91   if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
92     return(res);
93   for (s = html_str; *s; s++) {
94     if (strncasecmp(*s, bp, strlen(*s)) == 0)
95       return(-1);
96   }
97   return(res);
98 }
99
100 static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
101 {
102   FILE *f;
103   int ret;
104   char *tmpfilename;
105
106   /* Check file */
107   if ( access ( fn, F_OK ) == 0 )
108   {
109     /* File exists: return */
110     return -3;
111   } else {
112     if ( errno == ENOENT)
113     {
114       char *tmp = g_strdup ( fn );
115 #ifdef WINDOWS
116       mkdir( dirname ( dirname ( tmp ) ) );
117       g_free ( tmp ); tmp = g_strdup ( fn );
118       mkdir( dirname ( tmp ) );
119 #else
120       mkdir( dirname ( dirname ( tmp ) ), 0777 );
121       g_free ( tmp ); tmp = g_strdup ( fn );
122       mkdir( dirname ( tmp ), 0777 );
123 #endif
124       g_free ( tmp );
125     }
126     /* create placeholder file */
127     if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
128       return -4;
129     fclose ( f );
130   }
131
132   tmpfilename = g_strdup_printf("%s.tmp", fn);
133   f = fopen ( tmpfilename, "w+b" );
134   if ( ! f ) {
135     g_free ( tmpfilename );
136     remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
137     return -4;
138   }
139
140   /* Call the backend function */
141   ret = curl_download_get_url ( hostname, uri, f, options, ftp );
142
143   if (ret == -1 || ret == 1 || ret == -2 || check_map_file(f))
144   {
145     g_warning(_("Download error: %s"), fn);
146     fclose ( f );
147     remove ( tmpfilename );
148     g_free ( tmpfilename );
149     remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
150     return -1;
151   }
152
153   fclose ( f );
154   rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
155   g_free ( tmpfilename );
156   return ret;
157 }
158
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 )
163 {
164   return download ( hostname, uri, fn, opt, FALSE );
165 }
166
167 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
168 {
169   return download ( hostname, uri, fn, opt, TRUE );
170 }