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
|
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include "filelist.h"
#include "util.h"
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) {
char *strA = filesA->files[ctrA].name;
char *strB = filesB->files[ctrB].name;
int cmp = strcmp(strA, strB);
if(cmp < 0) {
ret = alpm_list_add(ret, strA);
ctrA++;
} else if(cmp > 0) {
ctrB++;
} else {
ctrA++;
ctrB++;
}
}
while(ctrA < filesA->count) {
ret = alpm_list_add(ret, filesA->files[ctrA].name);
ctrA++;
}
return ret;
}
static int _alpm_filelist_pathcmp(const char *p1, const char *p2)
{
while(*p1 && *p1 == *p2) {
p1++;
p2++;
}
if(*p1 == '\0' && *p2 == '/') {
p2++;
} else if(*p2 == '\0' && *p1 == '/') {
p1++;
}
return *p1 - *p2;
}
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;
alpm_file_t *arrA = filesA->files, *arrB = filesB->files;
while(ctrA < filesA->count && ctrB < filesB->count) {
const char *strA = arrA[ctrA].name, *strB = arrB[ctrB].name;
int cmp = _alpm_filelist_pathcmp(strA, strB);
if(cmp < 0) {
ctrA++;
} else if(cmp > 0) {
ctrB++;
} else {
if(strA[strlen(strA) - 1] != '/' || strB[strlen(strB) - 1] != '/') {
ret = alpm_list_add(ret, arrA[ctrA].name);
}
ctrA++;
ctrB++;
}
}
return ret;
}
static 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 SYMEXPORT *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);
}
void _alpm_filelist_sort(alpm_filelist_t *filelist)
{
size_t i;
for(i = 1; i < filelist->count; i++) {
if(strcmp(filelist->files[i - 1].name, filelist->files[i].name) > 0) {
qsort(filelist->files, filelist->count,
sizeof(alpm_file_t), _alpm_files_cmp);
return;
}
}
}
|