]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/download.c
Cosmetic change
[andy/viking.git] / src / download.c
... / ...
CommitLineData
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
37static int check_map_file(FILE* f)
38{
39 char **s;
40 char *bp;
41 int res = 0; /* good */
42 fpos_t pos;
43 char buf[33];
44 size_t nr;
45 char * html_str[] = {
46 "<html",
47 "<!DOCTYPE html",
48 "<head",
49 "<title",
50 NULL
51 };
52
53
54 memset(buf, 0, sizeof(buf));
55 fgetpos(f, &pos);
56 rewind(f);
57 nr = fread(buf, 1, sizeof(buf) - 1, f);
58 fsetpos(f, &pos);
59 for (bp = buf; (bp < (buf + sizeof(buf) - 1)) && (nr > (bp - buf)); bp++) {
60 if (!(isspace(*bp)))
61 break;
62 }
63 if ((bp >= (buf + sizeof(buf) -1)) || ((bp - buf) >= nr))
64 return(res);
65 for (s = html_str; *s; s++) {
66 if (strncasecmp(*s, bp, strlen(*s)) == 0)
67 return(-1);
68 }
69 return(res);
70}
71
72static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
73{
74 FILE *f;
75 int ret;
76 char *tmpfilename;
77
78 /* Check file */
79 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
80 {
81 /* File exists: return */
82 return -3;
83 } else {
84 gchar *dir = g_path_get_dirname ( fn );
85 g_mkdir_with_parents ( dir , 0777 );
86 g_free ( dir );
87
88 /* create placeholder file */
89 if ( ! (f = g_fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
90 return -4;
91 fclose ( f );
92 f = NULL;
93 }
94
95 tmpfilename = g_strdup_printf("%s.tmp", fn);
96 f = g_fopen ( tmpfilename, "w+b" );
97 if ( ! f ) {
98 g_free ( tmpfilename );
99 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
100 return -4;
101 }
102
103 /* Call the backend function */
104 ret = curl_download_get_url ( hostname, uri, f, options, ftp );
105
106 if (ret == -1 || ret == 1 || ret == -2 || check_map_file(f))
107 {
108 g_warning(_("Download error: %s"), fn);
109 fclose ( f );
110 f = NULL;
111 g_remove ( tmpfilename );
112 g_free ( tmpfilename );
113 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
114 return -1;
115 }
116
117 fclose ( f );
118 f = NULL;
119 rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
120 g_free ( tmpfilename );
121 return ret;
122}
123
124/* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
125/* uri: like "/uri.html?whatever" */
126/* only reason for the "wrapper" is so we can do redirects. */
127int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
128{
129 return download ( hostname, uri, fn, opt, FALSE );
130}
131
132int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
133{
134 return download ( hostname, uri, fn, opt, TRUE );
135}