summaryrefslogtreecommitdiff
path: root/src/download.c
blob: a6dcc493252f113dbfc16273d456cab086293734 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright 2006,2008 Nick White
 *
 * This file is part of GetHT
 *
 * GetHT 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 3 of the License, or
 * (at your option) any later version.
 *
 * GetHT 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GetHT.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#include "getht.h"
#include "issue.h"

int read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
	{ return fread(ptr, size, nmemb, stream); }
int write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
	{ return fwrite(ptr, size, nmemb, stream); }
int update_progress(void *data, double dltotal, double dlnow,
					double ultotal, double ulnow);

int save_file(char *uri, char *filepath, char *filetitle, long resume_offset, struct config * options)
/*	Save the file *uri to *filepath */
{
	if(!options->quiet)
	{
		printf("Downloading %s       ",filetitle);
		fflush(stdout);
	}

	if(options->curl_handle) {
		FILE *file;
		file = fopen(filepath, resume_offset?"a":"w");
		if(!file)
		{
			fprintf(stderr,"Error: cannot open file %s for writing.\n",filepath);
			return 1;
		}

		curl_easy_setopt(options->curl_handle, CURLOPT_URL, uri);
		curl_easy_setopt(options->curl_handle, CURLOPT_READFUNCTION, read_func);
		curl_easy_setopt(options->curl_handle, CURLOPT_WRITEFUNCTION, write_func);
		curl_easy_setopt(options->curl_handle, CURLOPT_WRITEDATA, file);

		if(options->proxy.type)
		{
			curl_easy_setopt(options->curl_handle, CURLOPT_PROXYTYPE, options->proxy.type);
			curl_easy_setopt(options->curl_handle, CURLOPT_PROXY, options->proxy.address);
			if(options->proxy.port)
				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYPORT, options->proxy.port);
			if(options->proxy.auth)
				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYAUTH, options->proxy.auth);
			if(options->proxy.user[0] && options->proxy.pass[0])
			{
				char userpass[STR_MAX];
				snprintf(userpass, STR_MAX, "%s:%s", options->proxy.user, options->proxy.pass);
				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYUSERPWD, userpass);
			}
		}

		if(!options->quiet)
		{
			curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 0);
			curl_easy_setopt(options->curl_handle, CURLOPT_PROGRESSFUNCTION, update_progress);
			curl_easy_setopt(options->curl_handle, CURLOPT_PROGRESSDATA, &resume_offset);
		}
		else
			curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 1);

		curl_easy_setopt(options->curl_handle, CURLOPT_RESUME_FROM, resume_offset);

		/* create a buffer to hold any curl errors */
		char errorinfo[CURL_ERROR_SIZE];
		curl_easy_setopt(options->curl_handle, CURLOPT_ERRORBUFFER, errorinfo);

		if(curl_easy_perform(options->curl_handle))
		{
			remove(filepath);
			fprintf(stderr,"\nError, could not download %s: %s\n",uri, errorinfo);
			return 1;
		}

		fclose(file);

		if(!options->quiet)
			printf("\rDownloaded %s       \n",filetitle);
	}
	else {
		fprintf(stderr,"Error: curl failed to initialise.\n");
		printf("Could not download %s\n",uri);
		return 1;
	}
	return 0;
}

int update_progress(void *data, double dltotal, double dlnow,
				double ultotal, double ulnow)
/*	Print status information */
{
	double frac;
	long *startsize = NULL;

	startsize = (long *)data;
	long cur = (long) dlnow + *startsize;
	long total = (long) dltotal + *startsize;

	if(cur > 0)
		frac = 100 * (double) cur / (double) total;
	else
		frac = 0;

	printf("\b\b\b\b\b\b\b: %3.0lf%% ", frac);
	fflush(stdout);

	return 0;
}

double getremotefilesize(char *uri, struct config * options)
{
	double filesize;

	if(options->curl_handle) {

		curl_easy_setopt(options->curl_handle, CURLOPT_URL, uri);
		curl_easy_setopt(options->curl_handle, CURLOPT_READFUNCTION, read_func);

		/* don't download or return either body or header */
		curl_easy_setopt(options->curl_handle, CURLOPT_NOBODY, 1);
		curl_easy_setopt(options->curl_handle, CURLOPT_HEADER, 0);

		if(options->proxy.type)
		{
			curl_easy_setopt(options->curl_handle, CURLOPT_PROXYTYPE, options->proxy.type);
			curl_easy_setopt(options->curl_handle, CURLOPT_PROXY, options->proxy.address);
			if(options->proxy.port)
				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYPORT, options->proxy.port);
			if(options->proxy.auth)
				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYAUTH, options->proxy.auth);
			if(options->proxy.user[0] && options->proxy.pass[0])
			{
				char userpass[STR_MAX];
				snprintf(userpass, STR_MAX, "%s:%s", options->proxy.user, options->proxy.pass);
				curl_easy_setopt(options->curl_handle, CURLOPT_PROXYUSERPWD, userpass);
			}
		}

		curl_easy_setopt(options->curl_handle, CURLOPT_NOPROGRESS, 1);

		if(curl_easy_perform(options->curl_handle))
			filesize = -1;

		curl_easy_getinfo(options->curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &filesize);

	}
	else
		filesize = -1;

	curl_easy_reset(options->curl_handle);
	return filesize;
}

char * checkdir(char * dir)
/* Checks that dir is writable. If necessary prompt
 * the user for a new directory and return it. */
{
	int dirchanged = 0;

	if(!opendir(dir))
		while(mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
		{
			fprintf(stderr,"Cannot create directory %s\n", dir);
			printf("Please enter the path of a save directory: ");
			scanf("%s", dir);
			dirchanged = 1;
		}

	if(dirchanged)
		return dir;
	else
		return 0;
}

char * getissuedir(struct config * options, iss * issue)
/*	Returns and prepares download directory */
{
	char * newsavedir;
	char * newdir;

	newsavedir = malloc(STR_MAX);
	newdir = malloc(STR_MAX);

	/* Check that main save path is ok */
	newsavedir = checkdir(options->save_path);
	/* Save the new save path if it changed */
	if(newsavedir)
	{
		char getht_path[STR_MAX];
		snprintf(getht_path,STR_MAX,"%s/.getht",getenv("HOME"));
		updateconfig(getht_path, &options);
	}

	snprintf(newdir,STR_MAX,"%s/%i_%i-%i",options->save_path,
		issue->date.year,issue->date.firstmonth,issue->date.lastmonth);

	/* Check that specific issue path is ok */
	checkdir(newdir);

	return newdir;
}

void downloadsection(struct config * options, sec * section, char * downdir, int force)
/*	Download section pointed to */
{
	char filename[STR_MAX];
	char filepath[STR_MAX];

	snprintf(filename,STR_MAX,"section_%i.pdf", section->number);
	snprintf(filepath,STR_MAX,"%s/%s", downdir, filename);

	if(!force){
		struct stat fileinfo;
		/* see if local file exists */
		if(stat(filepath, &fileinfo))
			save_file(section->uri, filepath, filename, 0, options);
		else
		{
			/* get size of local file */
			long localsize = 0;
			localsize = (long) fileinfo.st_size;

			/* get size of remote file */
			long remotesize = 0;
			remotesize = (long) getremotefilesize(section->uri, options);

			/* if size of local file != size of remote file, resume */
			if(remotesize > 0 && localsize < remotesize)
				save_file(section->uri, filepath, filename, localsize, options);
			else
			{
				if(!options->quiet)
					printf("Skipping download of completed section %i\n", section->number);
			}
		}
	}
	else
		save_file(section->uri, filepath, filename, 0, options);
}

void downloadissue(struct config * options, iss * issue, int force)
/*	Download issue pointed to */
{
	char downdir[STR_MAX];
	strncpy(downdir, getissuedir(options, issue), STR_MAX);

	printf("Downloading %s to %s\n",issue->title, downdir);

	int count;
	for(count = 0; count <= issue->no_of_sections; count++)
		downloadsection(options, issue->section[count], downdir, force);
}