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
|
#include "system.h"
#include <rpmlib.h>
#include "fprint.h"
struct lookupCache {
char * match;
int pathsStripped;
int matchLength;
int stripLength;
dev_t dev;
ino_t ino;
};
static int strCompare(const void * a, const void * b)
{
const char * const * one = a;
const char * const * two = a;
return strcmp(*one, *two);
}
static fingerPrint doLookup(const char * fullName, int scareMemory,
struct lookupCache * cache)
{
char dir[PATH_MAX];
const char * chptr1;
char * end, * bn;
fingerPrint fp;
struct stat sb;
char * buf;
int stripCount;
/* assert(*fullName == '/' || !scareMemory); */
/* FIXME: a better cache might speed things up? */
if (cache && cache->pathsStripped &&
!strncmp(fullName, cache->match, cache->matchLength)) {
chptr1 = fullName + cache->matchLength;
if (*chptr1 == '/') {
chptr1++;
stripCount = cache->pathsStripped - 1;
while (*chptr1) {
if (*chptr1 == '/') stripCount--;
chptr1++;
}
chptr1 = fullName + cache->matchLength + 1;
if (stripCount == 0 && !(cache->stripLength > 0 &&
strncmp(cache->match+cache->matchLength+1, chptr1, cache->stripLength))) {
if (scareMemory)
fp.basename = chptr1;
else
fp.basename = strdup(chptr1);
fp.ino = cache->ino;
fp.dev = cache->dev;
return fp;
}
}
}
if (*fullName != '/') {
scareMemory = 0;
/* Using realpath on the arg isn't correct if the arg is a symlink,
* especially if the symlink is a dangling link. What we should
* instead do is use realpath() on `.' and then append arg to
* it.
*/
/* if the current directory doesn't exist, we might fail.
oh well. likewise if it's too long. */
if (realpath(".", dir) != NULL) {
char *s = alloca(strlen(dir) + strlen(fullName) + 2);
sprintf(s, "%s/%s", dir, fullName);
fullName = chptr1 = s;
}
}
/* FIXME: perhaps we should collapse //, /./, and /../ stuff if
!scareMemory?? */
buf = alloca(strlen(fullName) + 1);
strcpy(buf, fullName);
end = bn = strrchr(buf, '/');
stripCount = 0;
while (*buf) {
*end = '\0';
stripCount++;
/* as we're stating paths here, we want to follow symlinks */
if (!stat(*buf ? buf : "/", &sb)) {
chptr1 = fullName + (end - buf) + 1;
if (scareMemory)
fp.basename = chptr1;
else
fp.basename = strdup(chptr1);
fp.ino = sb.st_ino;
fp.dev = sb.st_dev;
if (cache) {
strcpy(cache->match, fullName);
cache->match[strlen(fullName)+1] = '\0';
cache->matchLength = (end-buf);
cache->match[cache->matchLength] = '\0';
cache->stripLength = (bn - (end+1));
cache->match[bn-buf] = '\0';
cache->pathsStripped = stripCount;
cache->dev = sb.st_dev;
cache->ino = sb.st_ino;
}
return fp;
}
end--;
while ((end > buf) && *end != '/') end--;
}
/* this can't happen, or stat('/') just failed! */
fp.basename = 0;
fp.ino = fp.dev = 0;
return fp;
}
unsigned int fpHashFunction(const void * key)
{
const fingerPrint * fp = key;
unsigned int hash = 0;
char ch;
const char * chptr;
ch = 0;
chptr = fp->basename;
while (*chptr) ch ^= *chptr++;
hash |= ch << 24;
hash |= (((fp->dev >> 8) ^ fp->dev) & 0xFF) << 16;
hash |= fp->ino & 0xFFFF;
return hash;
}
int fpEqual(const void * key1, const void * key2)
{
return FP_EQUAL(*((const fingerPrint *) key1), *((fingerPrint *) key2));
}
fingerPrint fpLookup(const char * fullName, int scareMemory)
{
return doLookup(fullName, scareMemory, NULL);
}
void fpLookupList(const char ** fullNames, fingerPrint * fpList, int numItems,
int alreadySorted)
{
int i, j;
struct lookupCache cache;
int maxLen = 0;
if (!alreadySorted) {
qsort(fullNames, numItems, sizeof(char *), strCompare);
}
for (i = 0; i < numItems; i++) {
j = strlen(fullNames[i]);
if (j > maxLen) maxLen = j;
}
cache.match = alloca(maxLen + 2);
*cache.match = '\0';
cache.matchLength = 0;
cache.pathsStripped = 0;
cache.stripLength = 0;
for (i = 0; i < numItems; i++) {
fpList[i] = doLookup(fullNames[i], 1, &cache);
}
}
|