]> git.street.me.uk Git - andy/viking.git/blame - src/download.c
Windows port: typo in util.c
[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 26#include <stdio.h>
6e78a423
QT
27#include <ctype.h>
28#include <string.h>
f83131b9
MA
29#include <glib.h>
30#include <glib/gstdio.h>
4c77d5e0 31#include <glib/gi18n.h>
932f8f22 32
85611cd9 33#include "download.h"
3292ba8b 34
3292ba8b 35#include "curl_download.h"
85611cd9 36
1ac37c09 37gboolean a_check_html_file(FILE* f)
6e78a423 38{
533bbf34
MA
39 gchar **s;
40 gchar *bp;
6e78a423 41 fpos_t pos;
533bbf34 42 gchar buf[33];
6e78a423 43 size_t nr;
533bbf34 44 gchar * html_str[] = {
6e78a423
QT
45 "<html",
46 "<!DOCTYPE html",
47 "<head",
48 "<title",
49 NULL
50 };
51
c44594ee 52 memset(buf, 0, sizeof(buf));
6e78a423
QT
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))
1ac37c09 62 return FALSE;
6e78a423 63 for (s = html_str; *s; s++) {
1918a993 64 if (strncasecmp(*s, bp, strlen(*s)) == 0)
1ac37c09 65 return TRUE;
6e78a423 66 }
1ac37c09
GB
67 return FALSE;
68}
69
70gboolean a_check_map_file(FILE* f)
71{
72 return !a_check_html_file(f);
6e78a423
QT
73}
74
0c1044e9 75static int download( const char *hostname, const char *uri, const char *fn, DownloadOptions *options, gboolean ftp)
932f8f22
GB
76{
77 FILE *f;
78 int ret;
533bbf34 79 gchar *tmpfilename;
1ac37c09 80 gboolean failure = FALSE;
932f8f22
GB
81
82 /* Check file */
45acf79e 83 if ( g_file_test ( fn, G_FILE_TEST_EXISTS ) == TRUE )
932f8f22
GB
84 {
85 /* File exists: return */
86 return -3;
87 } else {
a1618d62
MA
88 gchar *dir = g_path_get_dirname ( fn );
89 g_mkdir_with_parents ( dir , 0777 );
90 g_free ( dir );
91
3335ae4e 92 /* create placeholder file */
8c060406 93 if ( ! (f = g_fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
932f8f22 94 return -4;
3335ae4e 95 fclose ( f );
8c060406 96 f = NULL;
3335ae4e
EB
97 }
98
99 tmpfilename = g_strdup_printf("%s.tmp", fn);
8c060406 100 f = g_fopen ( tmpfilename, "w+b" );
3335ae4e
EB
101 if ( ! f ) {
102 g_free ( tmpfilename );
8c060406 103 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
3335ae4e 104 return -4;
932f8f22
GB
105 }
106
107 /* Call the backend function */
0c1044e9 108 ret = curl_download_get_url ( hostname, uri, f, options, ftp );
1ac37c09
GB
109 if (ret == -1 || ret == 1 || ret == -2) {
110 g_debug("%s: download failed: curl_download_get_url=%d", __FUNCTION__, ret);
111 failure = TRUE;
112 }
113
114 if (!failure && options != NULL && options->check_file != NULL && ! options->check_file(f)) {
115 g_debug("%s: file content checking failed", __FUNCTION__);
116 failure = TRUE;
117 }
932f8f22 118
1ac37c09 119 if (failure)
932f8f22 120 {
4258f4e2 121 g_warning(_("Download error: %s"), fn);
932f8f22 122 fclose ( f );
8c060406
MA
123 f = NULL;
124 g_remove ( tmpfilename );
3335ae4e 125 g_free ( tmpfilename );
8c060406 126 g_remove ( fn ); /* couldn't create temporary. delete 0-byte file. */
932f8f22
GB
127 return -1;
128 }
129
130 fclose ( f );
8c060406 131 f = NULL;
533bbf34 132 g_rename ( tmpfilename, fn ); /* move completely-downloaded file to permanent location */
3335ae4e 133 g_free ( tmpfilename );
932f8f22
GB
134 return ret;
135}
136
85611cd9
GB
137/* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
138/* uri: like "/uri.html?whatever" */
139/* only reason for the "wrapper" is so we can do redirects. */
80214df2 140int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
85611cd9 141{
0c1044e9
EB
142 return download ( hostname, uri, fn, opt, FALSE );
143}
144
145int a_ftp_download_get_url ( const char *hostname, const char *uri, const char *fn, DownloadOptions *opt )
146{
147 return download ( hostname, uri, fn, opt, TRUE );
85611cd9 148}