File: downloadcache.c

package info (click to toggle)
reprepro 5.3.0-1.2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,960 kB
  • sloc: ansic: 52,311; sh: 1,875; python: 1,625; makefile: 167
file content (315 lines) | stat: -rw-r--r-- 8,415 bytes parent folder | download | duplicates (11)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*  This file is part of "reprepro"
 *  Copyright (C) 2004,2005,2007,2009 Bernhard R. Link
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include <config.h>

#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "error.h"
#include "strlist.h"
#include "names.h"
#include "dirs.h"
#include "files.h"
#include "freespace.h"
#include "downloadcache.h"


struct downloaditem {
	/*@dependent@*//*@null@*/struct downloaditem *parent;
	/*@null@*/struct downloaditem *left, *right;
	char *filekey;
	struct checksums *checksums;
	bool done;
};

/* Initialize a new download session */
retvalue downloadcache_initialize(enum spacecheckmode mode, off_t reserveddb, off_t reservedother, struct downloadcache **download) {
	struct downloadcache *cache;
	retvalue r;

	cache = zNEW(struct downloadcache);
	if (FAILEDTOALLOC(cache))
		return RET_ERROR_OOM;
	r = space_prepare(&cache->devices, mode, reserveddb, reservedother);
	if (RET_WAS_ERROR(r)) {
		free(cache);
		return r;
	}
	*download = cache;
	return RET_OK;
}

/* free all memory */
static void freeitem(/*@null@*//*@only@*/struct downloaditem *item) {
	if (item == NULL)
		return;
	freeitem(item->left);
	freeitem(item->right);
	free(item->filekey);
	checksums_free(item->checksums);
	free(item);
}

retvalue downloadcache_free(struct downloadcache *download) {
	if (download == NULL)
		return RET_NOTHING;

	freeitem(download->items);
	space_free(download->devices);
	free(download);
	return RET_OK;
}

static retvalue downloaditem_callback(enum queue_action action, void *privdata, void *privdata2, const char *uri, const char *gotfilename, const char *wantedfilename, /*@null@*/const struct checksums *checksums, const char *method) {
	struct downloaditem *d = privdata;
	struct downloadcache *cache = privdata2;
	struct checksums *read_checksums = NULL;
	retvalue r;
	bool improves;

	if (action != qa_got)
		// TODO: instead store in downloaditem?
		return RET_ERROR;

	/* if the file is somewhere else, copy it: */
	if (strcmp(gotfilename, wantedfilename) != 0) {
		if (verbose > 1)
			fprintf(stderr,
"Linking file '%s' to '%s'...\n", gotfilename, wantedfilename);
		r = checksums_linkorcopyfile(wantedfilename, gotfilename,
				&read_checksums);
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Cannot open '%s', obtained from '%s' method.\n",
					gotfilename, method);
			r = RET_ERROR_MISSING;
		}
		if (RET_WAS_ERROR(r)) {
			// TODO: instead store in downloaditem?
			return r;
		}
		if (read_checksums != NULL)
			checksums = read_checksums;
	}

	if (checksums == NULL || !checksums_iscomplete(checksums)) {
		assert(read_checksums == NULL);
		r = checksums_read(wantedfilename, &read_checksums);
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Cannot open '%s', though '%s' method claims to have put it there!\n",
					wantedfilename, method);
			r = RET_ERROR_MISSING;
		}
		if (RET_WAS_ERROR(r)) {
			// TODO: instead store in downloaditem?
			return r;
		}
		checksums = read_checksums;
	}
	assert (checksums != NULL);

	if (!checksums_check(d->checksums, checksums, &improves)) {
		fprintf(stderr, "Wrong checksum during receive of '%s':\n",
				uri);
		checksums_printdifferences(stderr, d->checksums, checksums);
		checksums_free(read_checksums);
		(void)unlink(wantedfilename);
		// TODO: instead store in downloaditem?
		return RET_ERROR_WRONG_MD5;
	}
	if (improves) {
		r = checksums_combine(&d->checksums, checksums, NULL);
		checksums_free(read_checksums);
		if (RET_WAS_ERROR(r))
			return r;
	} else
		checksums_free(read_checksums);

	if (global.showdownloadpercent > 0) {
		unsigned int percent;

		cache->size_done += checksums_getfilesize(d->checksums);

		percent = (100 * cache->size_done) / cache->size_todo;
		if (global.showdownloadpercent > 1
				|| percent > cache->last_percent) {
			unsigned long long all = cache->size_done;
			int kb, mb, gb, tb, b, groups = 0;

			cache->last_percent = percent;

			printf("Got %u%%: ", percent);
			b = all & 1023;
			all = all >> 10;
			kb = all & 1023;
			all = all >> 10;
			mb = all & 1023;
			all = all >> 10;
			gb = all & 1023;
			all = all >> 10;
			tb = all;
			if (tb != 0) {
				printf("%dT ", tb);
				groups++;
			}
			if (groups < 2 && (groups > 0 || gb != 0)) {
				printf("%dG ", gb);
				groups++;
			}
			if (groups < 2 && (groups > 0 || mb != 0)) {
				printf("%dM ", mb);
				groups++;
			}
			if (groups < 2 && (groups > 0 || kb != 0)) {
				printf("%dK ", kb);
				groups++;
			}
			if (groups < 2 && (groups > 0 || b != 0))
				printf("%d ", b);
			puts("bytes");
		}
	}
	r = files_add_checksums(d->filekey, d->checksums);
	if (RET_WAS_ERROR(r))
		return r;
	d->done = true;
	return RET_OK;
}

/*@null@*//*@dependent@*/ static struct downloaditem *searchforitem(struct downloadcache *list,
					const char *filekey,
					/*@out@*/struct downloaditem **p,
					/*@out@*/struct downloaditem ***h) {
	struct downloaditem *item;
	int c;

	*h = &list->items;
	*p = NULL;
	item = list->items;
	while (item != NULL) {
		*p = item;
		c = strcmp(filekey, item->filekey);
		if (c == 0)
			return item;
		else if (c < 0) {
			*h = &item->left;
			item = item->left;
		} else {
			*h = &item->right;
			item = item->right;
		}
	}
	return NULL;
}

/* queue a new file to be downloaded:
 * results in RET_ERROR_WRONG_MD5, if someone else already asked
 * for the same destination with other md5sum created. */
retvalue downloadcache_add(struct downloadcache *cache, struct aptmethod *method, const char *orig, const char *filekey, const struct checksums *checksums) {

	struct downloaditem *i;
	struct downloaditem *item, **h, *parent;
	char *fullfilename;
	retvalue r;

	assert (cache != NULL && method != NULL);
	r = files_expect(filekey, checksums, false);
	if (r != RET_NOTHING)
		return r;

	i = searchforitem(cache, filekey, &parent, &h);
	if (i != NULL) {
		bool improves;

		assert (i->filekey != NULL);
		if (!checksums_check(i->checksums, checksums, &improves)) {
			fprintf(stderr,
"ERROR: Same file is requested with conflicting checksums:\n");
			checksums_printdifferences(stderr,
					i->checksums, checksums);
			return RET_ERROR_WRONG_MD5;
		}
		if (improves) {
			r = checksums_combine(&i->checksums,
					checksums, NULL);
			if (RET_WAS_ERROR(r))
				return r;
		}
		return RET_NOTHING;
	}
	item = zNEW(struct downloaditem);
	if (FAILEDTOALLOC(item))
		return RET_ERROR_OOM;

	item->done = false;
	item->filekey = strdup(filekey);
	item->checksums = checksums_dup(checksums);
	if (FAILEDTOALLOC(item->filekey) || FAILEDTOALLOC(item->checksums)) {
		freeitem(item);
		return RET_ERROR_OOM;
	}

	fullfilename = files_calcfullfilename(filekey);
	if (FAILEDTOALLOC(fullfilename)) {
		freeitem(item);
		return RET_ERROR_OOM;
	}
	(void)dirs_make_parent(fullfilename);
	r = space_needed(cache->devices, fullfilename, checksums);
	if (RET_WAS_ERROR(r)) {
		free(fullfilename);
		freeitem(item);
		return r;
	}
	r = aptmethod_enqueue(method, orig, fullfilename,
			downloaditem_callback, item, cache);
	if (RET_WAS_ERROR(r)) {
		freeitem(item);
		return r;
	}
	item->left = item->right = NULL;

	item->parent = parent;
	*h = item;

	cache->size_todo += checksums_getfilesize(item->checksums);

	return RET_OK;
}

/* some as above, only for more files... */
retvalue downloadcache_addfiles(struct downloadcache *cache, struct aptmethod *method, const struct checksumsarray *origfiles, const struct strlist *filekeys) {
	retvalue result, r;
	int i;

	assert (origfiles != NULL && filekeys != NULL
		&& origfiles->names.count == filekeys->count);

	result = RET_NOTHING;

	for (i = 0 ; i < filekeys->count ; i++) {
		r = downloadcache_add(cache, method,
			origfiles->names.values[i],
			filekeys->values[i],
			origfiles->checksums[i]);
		RET_UPDATE(result, r);
	}
	return result;
}