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
343
344
|
/* Copyright (C) 2001 Red Hat, Inc.
Written by Jakub Jelinek <jakub@redhat.com>.
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; see the file COPYING. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* Changes by Rémy Card to use constants and add option -n. */
#define _GNU_SOURCE
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#define NHASH 131072 /* Must be a power of 2! */
#define NAMELEN 4096
#define NBUF 64
struct _f;
typedef struct _h {
struct _h *next;
struct _f *chain;
off_t size;
time_t mtime;
} h;
typedef struct _d {
struct _d *next;
char name[0];
} d;
d *dirs;
h *hps[NHASH];
int no_link = 0;
int verbose = 0;
int content_only = 0;
typedef struct _f {
struct _f *next;
ino_t ino;
dev_t dev;
unsigned int cksum;
char name[0];
} f;
inline unsigned int hash(off_t size, time_t mtime)
{
return (size ^ mtime) & (NHASH - 1);
}
inline int stcmp(struct stat *st1, struct stat *st2, int content_only)
{
if (content_only)
return st1->st_size != st2->st_size;
return st1->st_mode != st2->st_mode || st1->st_uid != st2->st_uid ||
st1->st_gid != st2->st_gid || st1->st_size != st2->st_size ||
st1->st_mtime != st2->st_mtime;
}
long long ndirs, nobjects, nregfiles, nmmap, ncomp, nlinks, nsaved;
void doexit(int i)
{
if (verbose) {
fprintf(stderr, "\n\n");
fprintf(stderr, "Directories %lld\n", ndirs);
fprintf(stderr, "Objects %lld\n", nobjects);
fprintf(stderr, "IFREG %lld\n", nregfiles);
fprintf(stderr, "Mmaps %lld\n", nmmap);
fprintf(stderr, "Comparisons %lld\n", ncomp);
fprintf(stderr, "%s %lld\n", (no_link ? "Would link" : "Linked"), nlinks);
fprintf(stderr, "%s %lld\n", (no_link ? "Would save" : "saved"), nsaved);
}
exit(i);
}
void usage(prog)
{
fprintf (stderr, "Usage: %s [-cnv] directories...\n", prog);
exit(255);
}
unsigned int buf[NBUF];
char nambuf1[NAMELEN], nambuf2[NAMELEN];
void rf (char *name)
{
struct stat st, st2, st3;
nobjects++;
if (lstat (name, &st))
return;
if (S_ISDIR (st.st_mode)) {
d * dp = malloc(sizeof(d) + 1 + strlen (name));
if (!dp) {
fprintf(stderr, "\nOut of memory 3\n");
doexit(3);
}
strcpy (dp->name, name);
dp->next = dirs;
dirs = dp;
} else if (S_ISREG (st.st_mode)) {
int fd, i;
f * fp, * fp2;
h * hp;
char *p, *q;
char *n1, *n2;
int cksumsize = sizeof(buf);
unsigned int cksum;
time_t mtime = content_only ? 0 : st.st_mtime;
unsigned int hsh = hash (st.st_size, mtime);
nregfiles++;
if (verbose > 1)
fprintf(stderr, " %s", name);
fd = open (name, O_RDONLY);
if (fd < 0) return;
if (st.st_size < sizeof(buf)) {
cksumsize = st.st_size;
memset (((char *)buf) + cksumsize, 0, (sizeof(buf) - cksumsize) % sizeof(buf[0]));
}
if (read (fd, buf, cksumsize) != cksumsize) {
close(fd);
if (verbose > 1)
fprintf(stderr, "\r%*s\r", (int)strlen(name)+2, "");
return;
}
cksumsize = (cksumsize + sizeof(buf[0]) - 1) / sizeof(buf[0]);
for (i = 0, cksum = 0; i < cksumsize; i++) {
if (cksum + buf[i] < cksum)
cksum += buf[i] + 1;
else
cksum += buf[i];
}
for (hp = hps[hsh]; hp; hp = hp->next)
if (hp->size == st.st_size && hp->mtime == mtime)
break;
if (!hp) {
hp = malloc(sizeof(h));
if (!hp) {
fprintf(stderr, "\nOut of memory 1\n");
doexit(1);
}
hp->size = st.st_size;
hp->mtime = mtime;
hp->chain = NULL;
hp->next = hps[hsh];
hps[hsh] = hp;
}
for (fp = hp->chain; fp; fp = fp->next)
if (fp->cksum == cksum)
break;
for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
if (fp2->ino == st.st_ino && fp2->dev == st.st_dev) {
close(fd);
if (verbose > 1)
fprintf(stderr, "\r%*s\r", (int)strlen(name)+2, "");
return;
}
if (fp) {
p = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
nmmap++;
if (p == (void *)-1) {
close(fd);
fprintf(stderr, "\nFailed to mmap %s\n", name);
return;
}
}
for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
if (!lstat (fp2->name, &st2) && S_ISREG (st2.st_mode) &&
!stcmp (&st, &st2, content_only) &&
st2.st_ino != st.st_ino &&
st2.st_dev == st.st_dev) {
int fd2 = open (fp2->name, O_RDONLY);
if (fd2 < 0) continue;
if (fstat (fd2, &st2) || !S_ISREG (st2.st_mode)) {
close (fd2);
continue;
}
ncomp++;
q = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd2, 0);
if (q == (void *)-1) {
close(fd2);
fprintf(stderr, "\nFailed to mmap %s\n", fp2->name);
continue;
}
if (memcmp (p, q, st.st_size)) {
munmap (q, st.st_size);
close(fd2);
continue;
}
munmap (q, st.st_size);
close(fd2);
if (lstat (name, &st3)) {
fprintf(stderr, "\nCould not stat %s again\n", name);
munmap (p, st.st_size);
close(fd);
return;
}
st3.st_atime = st.st_atime;
if (stcmp (&st, &st3, 0)) {
fprintf(stderr, "\nFile %s changed underneath us\n", name);
munmap (p, st.st_size);
close(fd);
return;
}
n1 = fp2->name;
n2 = name;
if (!no_link) {
strcpy (stpcpy (nambuf2, n2), ".$$$___cleanit___$$$");
if (rename (n2, nambuf2)) {
fprintf(stderr, "\nFailed to rename %s to %s\n", n2, nambuf2);
continue;
}
if (link (n1, n2)) {
fprintf(stderr, "\nFailed to hardlink %s to %s\n", n1, n2);
if (rename (nambuf2, n2)) {
fprintf(stderr, "\nBad bad - failed to rename back %s to %s\n", nambuf2, n2);
}
munmap (p, st.st_size);
close(fd);
return;
}
unlink (nambuf2);
}
nlinks++;
if (st3.st_nlink > 1) {
/* We actually did not save anything this time, since the link second argument
had some other links as well. */
if (verbose > 1)
fprintf(stderr, "\r%*s\r%s %s to %s\n", (int)strlen(name)+2, "", (no_link ? "Would link" : "Linked"), n1, n2);
} else {
nsaved+=st.st_size;
if (verbose > 1)
fprintf(stderr, "\r%*s\r%s %s to %s, %s %ld\n", (int)strlen(name)+2, "", (no_link ? "Would link" : "Linked"), n1, n2, (no_link ? "would save" : "saved"), st.st_size);
}
munmap (p, st.st_size);
close(fd);
return;
}
if (fp)
munmap (p, st.st_size);
fp2 = malloc(sizeof(f) + 1 + strlen (name));
if (!fp2) {
fprintf(stderr, "\nOut of memory 2\n");
doexit(2);
}
close(fd);
fp2->ino = st.st_ino;
fp2->dev = st.st_dev;
fp2->cksum = cksum;
strcpy(fp2->name, name);
if (fp) {
fp2->next = fp->next;
fp->next = fp2;
} else {
fp2->next = hp->chain;
hp->chain = fp2;
}
if (verbose > 1)
fprintf(stderr, "\r%*s\r", (int)strlen(name)+2, "");
return;
}
}
int main(int argc, char **argv)
{
int ch;
int i;
char *p;
d * dp;
DIR *dh;
struct dirent *di;
while ((ch = getopt (argc, argv, "cnv")) != -1) {
switch (ch) {
case 'n':
no_link++;
break;
case 'v':
verbose++;
break;
case 'c':
content_only++;
break;
default:
usage(argv[0]);
}
}
if (optind >= argc)
usage(argv[0]);
for (i = optind; i < argc; i++)
rf(argv[i]);
while (dirs) {
dp = dirs;
dirs = dp->next;
strcpy (nambuf1, dp->name);
free (dp);
strcat (nambuf1, "/");
p = strchr (nambuf1, 0);
dh = opendir (nambuf1);
if (dh == NULL)
continue;
ndirs++;
while ((di = readdir (dh)) != NULL) {
if (!di->d_name[0])
continue;
if (di->d_name[0] == '.') {
char *q;
if (!di->d_name[1] || !strcmp (di->d_name, "..") || !strncmp (di->d_name, ".in.", 4))
continue;
q = strrchr (di->d_name, '.');
if (q && strlen (q) == 7 && q != di->d_name) {
*p = 0;
if (verbose)
fprintf(stderr, "Skipping %s%s\n", nambuf1, di->d_name);
continue;
}
}
strcpy (p, di->d_name);
rf(nambuf1);
}
closedir(dh);
}
doexit(0);
return 0;
}
|