aboutsummaryrefslogtreecommitdiffstats
path: root/src/curl.c
blob: 627f29a868a1ae740f031b33ea6c38f930342f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2003 Jason Woodward <woodwardj at jaos dot org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <main.h>

void download_data(FILE *fh,const char *url){
	CURL *ch = NULL;
	CURLcode response;
	char curl_err_buff[1024];

#if DEBUG == 1
	printf("Fetching url:[%s]\n",url);
#endif
	ch = curl_easy_init();
	curl_easy_setopt(ch, CURLOPT_URL, url);
	curl_easy_setopt(ch, CURLOPT_WRITEDATA, fh);
	curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 0);
#if USE_CURL_PROGRESS == 0
	curl_easy_setopt(ch, CURLOPT_PROGRESSFUNCTION, progress_callback);
#endif
	curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, curl_err_buff );

	if( (response = curl_easy_perform(ch)) != 0 ){
		fprintf(stderr,"failed to download: %s\n",curl_err_buff);
		exit(1);
	}
	/* curl_easy_cleanup(ch); */
	curl_free(ch);
}

FILE *download_pkg_list(const rc_config *global_config){
	FILE *fh = NULL;
	char *url = NULL;

	fh = open_file(PKG_LIST_L,"w+");
	if( fh == NULL ){
		fprintf(stderr,"Failed to open package list target\n");
		exit(1);
	}

#if USE_CURL_PROGRESS == 0
	printf("Retrieving package data...");
#else
	printf("Retrieving package data...\n");
#endif

	url = calloc(
		strlen(global_config->mirror_url) + strlen(PKG_LIST) + 1, sizeof(char)
	);
	if( url == NULL ){
		fprintf(stderr,"Failed to calloc url\n");
		exit(1);
	}

	memcpy(url,global_config->mirror_url,strlen(global_config->mirror_url) );
	strncat(url,PKG_LIST,strlen(PKG_LIST) );
	download_data(fh,url);
#if USE_CURL_PROGRESS == 0
	printf("Done\n");
#endif

	free(url);
	rewind(fh); /* make sure we are back at the front of the file */
	return fh;
}

FILE *download_patches_list(const rc_config *global_config){
	FILE *fh = NULL;
	char *url = NULL;

	fh = open_file(PATCHES_LIST_L,"w+");
	if( fh == NULL ){
		fprintf(stderr,"Failed to open patch list target\n");
		exit(1);
	}

#if USE_CURL_PROGRESS == 0
	printf("Retrieving patch list...");
#else
	printf("Retrieving patch list...\n");
#endif

	url = calloc(
		strlen(global_config->mirror_url) + strlen(PATCHDIR) + strlen(PATCHES_LIST) + 1 , sizeof(char)
	);
	if( url == NULL ){
		fprintf(stderr,"Failed to calloc url\n");
		exit(1);
	}

	memcpy(url,global_config->mirror_url,strlen(global_config->mirror_url) );
	strncat(url,PATCHDIR,strlen(PATCHDIR) );
	strncat(url,PATCHES_LIST,strlen(PATCHES_LIST) );
	download_data(fh,url);
#if USE_CURL_PROGRESS == 0
	printf("Done\n");
#endif

	free(url);
	rewind(fh); /* make sure we are back at the front of the file */
	return fh;
}

char *download_pkg(const rc_config *global_config,pkg_info *pkg){
	FILE *fh = NULL;
	char *file_name = NULL;
	char *url = NULL;

	/* build the file name */
	file_name = calloc(
		strlen(pkg->name)+strlen("-")+strlen(pkg->version)+strlen(".tgz") + 1 , sizeof(char)
	);
	if( file_name == NULL ){
		fprintf(stderr,"Failed to calloc file_name\n");
		exit(1);
	}
	file_name = memcpy(file_name,pkg->name,strlen(pkg->name));
	file_name = strncat(file_name,"-",strlen("-"));
	file_name = strncat(file_name,pkg->version,strlen(pkg->version));
	file_name = strncat(file_name,".tgz",strlen(".tgz"));
	/* file_name[ strlen(file_name) ] = '\0'; */

	fh = open_file(file_name,"wb");

	/*
	 * TODO: here we will use the md5sum (or some other means)
	 *	to see if the file is already present and valid
	 */


	/* build the url */
	url = calloc(
		strlen(global_config->mirror_url) + strlen(pkg->location) + strlen(file_name) + strlen("/") + 1 , sizeof(char)
	);
	if( url == NULL ){
		fprintf(stderr,"Failed to calloc url\n");
		exit(1);
	}
	url = memcpy(url,global_config->mirror_url,strlen(global_config->mirror_url));
	url = strncat(url,pkg->location,strlen(pkg->location));
	url = strncat(url,"/",strlen("/"));
	url = strncat(url,file_name,strlen(file_name));
	url[ strlen(url) ] = '\0';

#if USE_CURL_PROGRESS == 0
	printf("Downloading %s...",pkg->name);
#else
	printf("Downloading %s...\n",pkg->name);
#endif
	download_data(fh,url);
#if USE_CURL_PROGRESS == 0
	printf("Done\n");
#endif

	if( url != NULL ){
		free(url);
	}
	fclose(fh);
	return file_name;
}

int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow){
	/* supress unused parameter warning */
	(void) clientp;
	(void) dltotal;
	(void) dlnow;
	(void) ultotal;
	(void) ulnow;
	/* */
	printf("%c\b",spinner());
	return 0;
}