]> git.street.me.uk Git - andy/viking.git/blob - src/download.c
Fix magic scissors
[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 <ctype.h>
28 #include <string.h>
29 #include <glib.h>
30 #include <glib/gstdio.h>
31 #include <glib/gi18n.h>
32
33 #include "download.h"
34
35 #include "curl_download.h"
36
37 gboolean a_check_html_file(FILE* f)
38 {
39   gchar **s;
40   gchar *bp;
41   fpos_t pos;
42   gchar buf[33];
43   size_t nr;
44   gchar * html_str[] = {
45     "<html",
46     "<!DOCTYPE html",
47     "<head",
48     "<title",
49     NULL
50   };
51
52   memset(buf, 0, sizeof(buf));
53   fgetpos(f, &pos);
54   rewind(f);
55   nr = fread(buf, 1, sizeof(buf) - 1, f);
56   fsetpos(f, &pos);
57   for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
58     if (!(isspace(*bp)))
59       break;
60   }
61   if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
62     return FALSE;
63   for (s = html_str; *s; s++) {
64     if (strncasecmp(*s, bp, strlen(*s)) == 0)
65       return TRUE;
66   }
67   return FALSE;
68 }
69
70 gboolean a_check_map_file(FILE* f)
71 {
72   return !a_check_html_file(f);
73 }
74
75 gboolean a_check_kml_file(FILE* f)
76 {
77   gchar **s;
78   gchar *bp;
79   fpos_t pos;
80   gchar buf[33];
81   size_t nr;
82   gchar * html_str[] = {
83     "<?xml",
84     NULL
85   };
86
87   memset(buf, 0, sizeof(buf));
88   fgetpos(f, &pos);
89   rewind(f);
90   nr = fread(buf, 1, sizeof(buf) - 1, f);
91   fsetpos(f, &pos);
92   for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
93     if (!(isspace(*bp)))
94       break;
95   }
96   if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
97     return FALSE;
98   for (s = html_str; *s; s++) {
99     if (strncasecmp(*s, bp, strlen(*s)) == 0)
100       return TRUE;
101   }
102   return FALSE;
103 }
104
105 static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
106 {
107   FILE *f;
108   int ret;
109   gchar *tmpfilename;
110   gboolean failure = FALSE;
111
112   /* Check file */
113   if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
114   {
115     /* File exists: return */
116     return -3;
117   } else {
118     gchar *dir = g_path_get_dirname ( fn );
119     g_mkdir_with_parents ( dir , 0777 );
120     g_free ( dir );
121
122     /* create placeholder file */
123     if ( ! (f = g_fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
124       return -4;
125     fclose ( f );
126     f = NULL;
127   }
128
129   tmpfilename = g_strdup_printf("%s.tmp", fn);
130   f = g_fopen ( tmpfilename, "w+b" );
131   if ( ! f ) {
132     g_free ( tmpfilename );
133     g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
134     return -4;
135   }
136
137   /* Call the backend function */
138   ret = curl_download_get_url ( hostname, uri, f, options, ftp );
139   if (ret == -1 || ret == 1 || ret == -2) {
140     g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
141     failure = TRUE;
142   }
143
144   if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
145     g_debug("%s: file content checking failed", __FUNCTION__);
146     failure = TRUE;
147   }
148
149   if (failure)
150   {
151     g_warning(_("Download error: %s"), fn);
152     fclose ( f );
153     f = NULL;
154     g_remove ( tmpfilename );
155     g_free ( tmpfilename );
156     g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
157     return -1;
158   }
159
160   fclose ( f );
161   f = NULL;
162   g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
163   g_free ( tmpfilename );
164   return ret;
165 }
166
167 /* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
168 /* uri: like "/uri.html?whatever" */
169 /* only reason for the "wrapper" is so we can do redirects. */
170 int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
171 {
172   return download ( hostname, uri, fn, opt, FALSE );
173 }
174
175 int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
176 {
177   return download ( hostname, uri, fn, opt, TRUE );
178 }