]> git.street.me.uk Git - andy/viking.git/blame - src/download.c
Moving up cache management
[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
932f8f22
GB
22#include <stdio.h>
23#include <errno.h>
24#include <gtk/gtk.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
85611cd9
GB
28#include "download.h"
29#include "http.h"
30
932f8f22
GB
31#ifdef WINDOWS
32
33#include <io.h>
34#define access(a,b) _access(a,b)
35#define close(a) closesocket(a)
36
37char *dirname ( char * dir )
38{
39 char *tmp = dir + strlen(dir) - 1;
40 while ( tmp != dir && *tmp != '\\' )
41 tmp--;
42 *tmp = '\0';
43 return dir;
44}
45
46#else
47
48#include <unistd.h>
49#include <sys/types.h>
50
51/* dirname */
52#include <libgen.h>
53
54#endif
55
56static int download( const char *hostname, const char *uri, const char *fn, int sendhostname)
57{
58 FILE *f;
59 int ret;
60
61 /* Check file */
62 if ( access ( fn, F_OK ) == 0 )
63 {
64 /* File exists: return */
65 return -3;
66 } else {
67 if ( errno == ENOENT)
68 {
69 char *tmp = g_strdup ( fn );
70#ifdef WINDOWS
71 mkdir( dirname ( dirname ( tmp ) ) );
72 g_free ( tmp ); tmp = g_strdup ( fn );
73 mkdir( dirname ( tmp ) );
74#else
75 mkdir( dirname ( dirname ( tmp ) ), 0777 );
76 g_free ( tmp ); tmp = g_strdup ( fn );
77 mkdir( dirname ( tmp ), 0777 );
78#endif
79 g_free ( tmp );
80 }
81 if ( ! (f = fopen ( fn, "w+b" )) ) /* immediately open file so other threads won't -- prevents race condition */
82 return -4;
83 }
84
85 /* Call the backend function */
86 ret = http_download_get_url ( hostname, uri, f, 0, sendhostname );
87
88 if (ret == -1 || ret == 1 || ret == -2)
89 {
90 fclose ( f );
91 remove ( fn );
92 return -1;
93 }
94
95 fclose ( f );
96 return ret;
97}
98
85611cd9
GB
99/* success = 0, -1 = couldn't connect, -2 HTTP error, -3 file exists, -4 couldn't write to file... */
100/* uri: like "/uri.html?whatever" */
101/* only reason for the "wrapper" is so we can do redirects. */
102int a_http_download_get_url ( const char *hostname, const char *uri, const char *fn )
103{
932f8f22 104 return download ( hostname, uri, fn, 1 );
85611cd9
GB
105}
106
107int a_http_download_get_url_nohostname ( const char *hostname, const char *uri, const char *fn )
108{
932f8f22 109 return download ( hostname, uri, fn, 0 );
85611cd9 110}