File: needbuild.c

package info (click to toggle)
reprepro 4.2.0-2squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,016 kB
  • ctags: 3,674
  • sloc: ansic: 46,905; sh: 13,899; pascal: 160; makefile: 159; python: 138
file content (269 lines) | stat: -rw-r--r-- 7,496 bytes parent folder | download
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
/*  This file is part of "reprepro"
 *  Copyright (C) 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 <assert.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "error.h"
#include "atoms.h"
#include "strlist.h"
#include "chunks.h"
#include "trackingt.h"
#include "tracking.h"
#include "globmatch.h"
#include "needbuild.h"

/* list all source packages in a distribution that needs buildd action

   For each source package check:
	- if tracking is enabled and there is a .log or .changes file
	  for the given arch -> SKIP
   	- if there is a binary package for the given architecture -> SKIP
	- if the package's Architecture field excludes this arch -> SKIP
	- if the package's Binary field only lists existing ones
          (i.e. architecture all) -> SKIP
*/

static retvalue tracked_source_needs_build(architecture_t architecture, const char *sourcename, const char *sourceversion, const char *dscfilename, const struct strlist *binary, const struct trackedpackage *tp) {
	bool found_binary[binary->count];
	const char *archstring = atoms_architectures[architecture];
	size_t archstringlen= strlen(archstring);
	int i;

	memset(found_binary, 0, sizeof(bool)*binary->count);
	for( i = 0 ; i < tp->filekeys.count ; i++ ) {
		enum filetype ft = tp->filetypes[i];
		const char *fk = tp->filekeys.values[i];

		if( ft == ft_XTRA_DATA )
			continue;
		if( ft == ft_ALL_BINARY ) {
			int j;

			/* determine which binary files are arch all
			   packages: */
			for( j = 0 ; j < binary->count ; j++ ) {
				const char *b = binary->values[j];
				size_t l = strlen(b);

				if( strncmp(fk, b, l) == 0 &&
						fk[l] == '_' )
					found_binary[j] = true;
			}
			continue;
		}
		if( ft == ft_ARCH_BINARY ) {
			const char *a = strrchr(fk, '_');

			if( a == NULL )
				continue;
			a++;
			if( strncmp(a, archstring, archstringlen) != 0 ||
					a[archstringlen] != '.' )
				continue;
			/* found an .deb with this architecture,
			   so nothing is to be done */
			return RET_NOTHING;
		}
		if( ft == ft_LOG || ft == ft_CHANGES ) {
			const char *a = strrchr(fk, '_');
			const char *e;

			if( a == NULL )
				continue;
			a++;
			while( (e = strchr(a, '+')) != NULL ) {
				if( (size_t)(e-a) != archstringlen ) {
					a = e+1;
					continue;
				}
				if( memcmp(a, archstring, archstringlen) != 0 ){
					a = e+1;
					continue;
				}
				/* found something for this architecture */
					return RET_NOTHING;
			}
			e = strchr(a, '.');
			if( e == NULL )
				continue;
			if( (size_t)(e-a) != archstringlen ) {
				a = e+1;
				continue;
			}
			if( memcmp(a, archstring, archstringlen) != 0 ){
				a = e+1;
				continue;
			}
			/* found something for this architecture */
			return RET_NOTHING;
		}
	}
	/* nothing for this architecture was found, check if is has any binary
	   packages that are lacking: */
	for( i = 0 ; i < binary->count ; i++ ) {
		if( !found_binary[i] ) {
			printf("%s %s %s\n",
					sourcename, sourceversion,
					dscfilename);
			return RET_OK;
		}
	}
	/* all things listed in Binary already exists, nothing to do: */
	return RET_NOTHING;
}

struct needbuild_data { architecture_t architecture;
	trackingdb tracks;
	/*@null@*/ const char *glob;
};

static retvalue check_source_needs_build(UNUSED(struct database *database), struct distribution *distribution, struct target *target, const char *sourcename, const char *control, void *data) {
	struct needbuild_data *d = data;
	char *sourceversion;
	struct strlist binary, architectures, filekeys;
	const char *dscfilename = NULL;
	int i;
	retvalue r;

	if( d->glob != NULL && !globmatch(sourcename, d->glob) )
		return RET_NOTHING;

	r = target->getversion(control, &sourceversion);
	if( !RET_IS_OK(r) )
		return r;
	r = chunk_getwordlist(control, "Architecture", &architectures);
	if( RET_IS_OK(r) ) {
		bool skip = true;

		for( i = 0 ; i < architectures.count ; i++ ) {
			const char *a = architectures.values[i];
			if( strcmp(a, "any") == 0 ) {
				skip = false;
				break;
			}
			if( strcmp(a, atoms_architectures[
						d->architecture]) == 0 ) {
				skip = false;
				break;
			}
		}
		strlist_done(&architectures);
		if( skip ) {
			free(sourceversion);
			return RET_NOTHING;
		}
	}
	r = chunk_getwordlist(control, "Binary", &binary);
	if( !RET_IS_OK(r) ) {
		free(sourceversion);
		return r;
	}
	r = target->getfilekeys(control, &filekeys);
	if( !RET_IS_OK(r) ) {
		strlist_done(&binary);
		free(sourceversion);
		return r;
	}
	for( i = 0 ; i < filekeys.count ; i++ ) {
		if( endswith(filekeys.values[i], ".dsc") ) {
			dscfilename = filekeys.values[i];
			break;
		}
	}
	if( dscfilename == NULL ) {
		fprintf(stderr,
"Warning: source package '%s' in '%s' without dsc file!\n",
				sourcename, target->identifier);
		free(sourceversion);
		strlist_done(&binary);
		strlist_done(&filekeys);
		return RET_NOTHING;
	}

	if( d->tracks != NULL ) {
		struct trackedpackage *tp;

		r = tracking_get(d->tracks, sourcename, sourceversion, &tp);
		if( RET_WAS_ERROR(r) ) {
			free(sourceversion);
			strlist_done(&binary);
			strlist_done(&filekeys);
			return r;
		}
		if( RET_IS_OK(r) ) {
			r = tracked_source_needs_build(
					d->architecture, sourcename,
					sourceversion, dscfilename,
					&binary, tp);
			trackedpackage_free(tp);
			free(sourceversion);
			strlist_done(&binary);
			strlist_done(&filekeys);
			return r;
		}
		fprintf(stderr, "Warning: %s's tracking data of %s (%s) is out of date. Run retrack to repair!\n", distribution->codename, sourcename, sourceversion);
	}
	// TODO: implement without tracking
	free(sourceversion);
	strlist_done(&binary);
	strlist_done(&filekeys);
	return RET_NOTHING;
}


retvalue find_needs_build(struct database *database, struct distribution *distribution, architecture_t architecture, const struct atomlist *onlycomponents, const char *glob) {
	retvalue result, r;
	struct needbuild_data d;

	d.architecture = architecture;
	d.glob = glob;

	if( distribution->tracking == dt_NONE ) {
		fprintf(stderr,
"ERROR: '%s' has no source package Tracking enabled and\n"
"build-needing is currently only implemented for distributions where\n"
"this is enabled.\n"
"(i.e. you need to add e.g. Tracking: minimal in conf/distribution\n"
"and run retrack (and repeat running it after every update and pull.)\n",
			distribution->codename);
		return RET_ERROR;
	}

	if( distribution->tracking != dt_NONE ) {
		r = tracking_initialize(&d.tracks, database, distribution, true);
		if( RET_WAS_ERROR(r) )
			return r;
		if( r == RET_NOTHING )
			d.tracks = NULL;
	} else
			d.tracks = NULL;

	result = distribution_foreach_package_c(distribution, database,
			onlycomponents, architecture_source, pt_dsc,
			check_source_needs_build, &d);

	r = tracking_done(d.tracks);
	RET_ENDUPDATE(result, r);
	return result;
}