summaryrefslogtreecommitdiff
path: root/lib/libalpm/filelist.c
blob: 233cfda4344076cd475a9a4e5db8668e8c99a75f (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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 *  filelist.c
 *
 *  Copyright (c) 2012 Pacman Development Team <pacman-dev@archlinux.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
 *  (at your option) 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <limits.h>
#include <string.h>
#include <sys/stat.h>

/* libalpm */
#include "filelist.h"
#include "util.h"

/** Helper function for comparing strings when sorting */
static int _alpm_filelist_strcmp(const void *s1, const void *s2)
{
	return strcmp(*(char **)s1, *(char **)s2);
}

/**
 * @brief Resolves a symlink and its children.
 *
 * @attention Pre-condition: files must be sorted!
 *
 * @param files filelist to resolve
 * @param i index in files to start processing
 * @param path absolute path for the symlink being resolved
 * @param root_len length of the root portion of path
 * @param resolving is file \i in \files a symlink that needs to be resolved
 *
 * @return the index of the last file resolved
 */
size_t _alpm_filelist_resolve_link(
		alpm_filelist_t *files, size_t i, char *path, size_t root_len, int resolving)
{
	char *causal_dir = NULL; /* symlink being resolved */
	char *filename_r = NULL; /* resolved filename */
	size_t causal_dir_len = 0, causal_dir_r_len = 0;

	if(resolving) {
		/* deal with the symlink being resolved */
		MALLOC(filename_r, PATH_MAX, goto error);
		causal_dir = files->files[i].name;
		causal_dir_len = strlen(causal_dir);
		if(realpath(path, filename_r) == NULL) {
			files->resolved_path[i] = causal_dir;
			FREE(filename_r);
			return i;
		}
		causal_dir_r_len = strlen(filename_r + root_len) + 1;
		if(causal_dir_r_len >= PATH_MAX) {
			files->resolved_path[i] = causal_dir;
			FREE(filename_r);
			return i;
		}
		/* remove root_r from filename_r */
		memmove(filename_r, filename_r + root_len, causal_dir_r_len);
		filename_r[causal_dir_r_len - 1] = '/';
		filename_r[causal_dir_r_len] = '\0';
		STRDUP(files->resolved_path[i], filename_r, goto error);
		i++;
	}

	for(; i < files->count; i++) {
		char *filename = files->files[i].name;
		size_t filename_len = strlen(filename);
		size_t filename_r_len = filename_len;
		struct stat sbuf;
		int exists;

		if(resolving) {
			if(filename_len < causal_dir_len || strncmp(filename, causal_dir, causal_dir_len) != 0) {
				/* not inside causal_dir anymore */
				break;
			}

			filename_r_len = filename_len + causal_dir_r_len - causal_dir_len;
			if(filename_r_len >= PATH_MAX) {
				/* resolved path is too long */
				files->resolved_path[i] = filename;
				continue;
			}

			strcpy(filename_r + causal_dir_r_len, filename + causal_dir_len);
		}

		/* deal with files and paths too long to resolve*/
		if(filename[filename_len - 1] != '/' || root_len + filename_r_len >= PATH_MAX) {
			if(resolving) {
				STRDUP(files->resolved_path[i], filename_r, goto error);
			} else {
				files->resolved_path[i] = filename;
			}
			continue;
		}

		/* construct absolute path and stat() */
		strcpy(path + root_len, resolving ? filename_r : filename);
		exists = !_alpm_lstat(path, &sbuf);

		/* deal with symlinks */
		if(exists && S_ISLNK(sbuf.st_mode)) {
			i = _alpm_filelist_resolve_link(files, i, path, root_len, 1);
			continue;
		}

		/* deal with normal directories */
		if(resolving) {
			STRDUP(files->resolved_path[i], filename_r, goto error);
		} else {
			files->resolved_path[i] = filename;
		}

		/* deal with children of non-existent directories to reduce lstat() calls */
		if (!exists) {
			for(i++; i < files->count; i++) {
				char *f = files->files[i].name;;
				size_t f_len = strlen(f);
				size_t f_r_len;

				if(f_len < filename_len || strncmp(f, filename, filename_len) != 0) {
					/* not inside the non-existent dir anymore */
					break;
				}

				f_r_len = f_len + causal_dir_r_len - causal_dir_len;
				if(resolving && f_r_len <= PATH_MAX) {
					strcpy(filename_r + causal_dir_r_len, f + causal_dir_len);
					STRDUP(files->resolved_path[i], filename_r, goto error);
				} else {
					files->resolved_path[i] = f;
				}
			}
			i--;
		}
	}

	FREE(filename_r);

	return i-1;

error:
	FREE(filename_r);
	/* out of memory, set remaining files to their original names */
	for(; i < files->count; (i)++) {
		files->resolved_path[i] = files->files[i].name;
	}
	return i-1;
}

/**
 * @brief Takes a file list and resolves all directory paths according to the
 * filesystem
 *
 * @attention Pre-condition: files must be sorted!
 *
 * @note A symlink and directory at the same path in two difference packages
 * causes a conflict so the filepath can not change as packages get installed
 *
 * @param handle the context handle
 * @param files list of files to resolve
 */
void _alpm_filelist_resolve(alpm_handle_t *handle, alpm_filelist_t *files)
{
	char path[PATH_MAX];
	size_t root_len;

	if(!files || files->resolved_path) {
		return;
	}

	CALLOC(files->resolved_path, files->count, sizeof(char *), return);

	/* not much point in going on if we can't even resolve root */
	if(realpath(handle->root, path) == NULL){
		return;
	}
	root_len = strlen(path) + 1;
	if(root_len >= PATH_MAX) {
		return;
	}
	path[root_len - 1] = '/';
	path[root_len] = '\0';

	_alpm_filelist_resolve_link(files, 0, path, root_len, 0);

	qsort(files->resolved_path, files->count, sizeof(char *),
			_alpm_filelist_strcmp);

	return;
}


/* Returns the difference of the provided two lists of files.
 * Pre-condition: both lists are sorted!
 * When done, free the list but NOT the contained data.
 */
alpm_list_t *_alpm_filelist_difference(alpm_filelist_t *filesA,
		alpm_filelist_t *filesB)
{
	alpm_list_t *ret = NULL;
	size_t ctrA = 0, ctrB = 0;

	while(ctrA < filesA->count && ctrB < filesB->count) {
		alpm_file_t *fileA = filesA->files + ctrA;
		alpm_file_t *fileB = filesB->files + ctrB;
		const char *strA = fileA->name;
		const char *strB = fileB->name;
		/* skip directories, we don't care about them */
		if(strA[strlen(strA)-1] == '/') {
			ctrA++;
		} else if(strB[strlen(strB)-1] == '/') {
			ctrB++;
		} else {
			int cmp = strcmp(strA, strB);
			if(cmp < 0) {
				/* item only in filesA, qualifies as a difference */
				ret = alpm_list_add(ret, fileA);
				ctrA++;
			} else if(cmp > 0) {
				ctrB++;
			} else {
				ctrA++;
				ctrB++;
			}
		}
	}

	/* ensure we have completely emptied pA */
	while(ctrA < filesA->count) {
		alpm_file_t *fileA = filesA->files + ctrA;
		const char *strA = fileA->name;
		/* skip directories */
		if(strA[strlen(strA)-1] != '/') {
			ret = alpm_list_add(ret, fileA);
		}
		ctrA++;
	}

	return ret;
}

/* Returns the intersection of the provided two lists of files.
 * Pre-condition: both lists are sorted!
 * When done, free the list but NOT the contained data.
 */
alpm_list_t *_alpm_filelist_intersection(alpm_filelist_t *filesA,
		alpm_filelist_t *filesB)
{
	alpm_list_t *ret = NULL;
	size_t ctrA = 0, ctrB = 0;

	while(ctrA < filesA->count && ctrB < filesB->count) {
		int cmp, isdirA, isdirB;
		char *strA, *strB;

		isdirA = 0;
		strA = filesA->resolved_path[ctrA];
		if(strA[strlen(strA)-1] == '/') {
			isdirA = 1;
			strA = strndup(filesA->resolved_path[ctrA], strlen(strA)-1);
		}

		isdirB = 0;
		strB = filesB->resolved_path[ctrB];
		if(strB[strlen(strB)-1] == '/') {
			isdirB = 1;
			strB = strndup(filesB->resolved_path[ctrB], strlen(strB)-1);
		}

		cmp = strcmp(strA, strB);
		if(cmp < 0) {
			ctrA++;
		} else if(cmp > 0) {
			ctrB++;
		} else {
			/* TODO: this creates conflicts between a symlink to a directory in
			 * one package and a real directory in the other. For example,
			 * lib -> /usr/lib in pkg1 and /lib in pkg2.  This would be allowed
			 * when installing one package at a time _provided_ pkg1 is installed
			 * first. This will need adjusted if the order of package install can
			 * be guaranteed to install the symlink first */

			/* when not directories, item in both qualifies as an intersect */
			if(! (isdirA && isdirB)) {
				ret = alpm_list_add(ret, filesA->files + ctrA);
			}
			ctrA++;
			ctrB++;
		}

		if(isdirA) {
			free(strA);
		}
		if(isdirB) {
			free(strB);
		}
	}

	return ret;
}

/* Helper function for comparing files list entries
 */
int _alpm_files_cmp(const void *f1, const void *f2)
{
	const alpm_file_t *file1 = f1;
	const alpm_file_t *file2 = f2;
	return strcmp(file1->name, file2->name);
}


alpm_file_t *alpm_filelist_contains(alpm_filelist_t *filelist,
		const char *path)
{
	alpm_file_t key;

	if(!filelist) {
		return NULL;
	}

	key.name = (char *)path;

	return bsearch(&key, filelist->files, filelist->count,
			sizeof(alpm_file_t), _alpm_files_cmp);
}

/* vim: set ts=2 sw=2 noet: */