2.5-18.1
[glibc.git] / fedora / hardlink.c
blob62e633786d3eb8245d0aa3b5fd4d9c2772884591
1 /* Copyright (C) 2001 Red Hat, Inc.
3 Written by Jakub Jelinek <jakub@redhat.com>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public
16 License along with this program; see the file COPYING. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* Changes by Rémy Card to use constants and add option -n. */
22 #define _GNU_SOURCE
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <fcntl.h>
33 #define NHASH 131072 /* Must be a power of 2! */
34 #define NAMELEN 4096
35 #define NBUF 64
37 struct _f;
38 typedef struct _h {
39 struct _h *next;
40 struct _f *chain;
41 off_t size;
42 time_t mtime;
43 } h;
45 typedef struct _d {
46 struct _d *next;
47 char name[0];
48 } d;
50 d *dirs;
52 h *hps[NHASH];
54 int no_link = 0;
55 int verbose = 0;
56 int content_only = 0;
58 typedef struct _f {
59 struct _f *next;
60 ino_t ino;
61 dev_t dev;
62 unsigned int cksum;
63 char name[0];
64 } f;
66 inline unsigned int hash(off_t size, time_t mtime)
68 return (size ^ mtime) & (NHASH - 1);
71 inline int stcmp(struct stat *st1, struct stat *st2, int content_only)
73 if (content_only)
74 return st1->st_size != st2->st_size;
75 return st1->st_mode != st2->st_mode || st1->st_uid != st2->st_uid ||
76 st1->st_gid != st2->st_gid || st1->st_size != st2->st_size ||
77 st1->st_mtime != st2->st_mtime;
80 long long ndirs, nobjects, nregfiles, nmmap, ncomp, nlinks, nsaved;
82 void doexit(int i)
84 if (verbose) {
85 fprintf(stderr, "\n\n");
86 fprintf(stderr, "Directories %lld\n", ndirs);
87 fprintf(stderr, "Objects %lld\n", nobjects);
88 fprintf(stderr, "IFREG %lld\n", nregfiles);
89 fprintf(stderr, "Mmaps %lld\n", nmmap);
90 fprintf(stderr, "Comparisons %lld\n", ncomp);
91 fprintf(stderr, "%s %lld\n", (no_link ? "Would link" : "Linked"), nlinks);
92 fprintf(stderr, "%s %lld\n", (no_link ? "Would save" : "saved"), nsaved);
94 exit(i);
97 void usage(prog)
99 fprintf (stderr, "Usage: %s [-cnv] directories...\n", prog);
100 exit(255);
103 unsigned int buf[NBUF];
104 char nambuf1[NAMELEN], nambuf2[NAMELEN];
106 void rf (char *name)
108 struct stat st, st2, st3;
109 nobjects++;
110 if (lstat (name, &st))
111 return;
112 if (S_ISDIR (st.st_mode)) {
113 d * dp = malloc(sizeof(d) + 1 + strlen (name));
114 if (!dp) {
115 fprintf(stderr, "\nOut of memory 3\n");
116 doexit(3);
118 strcpy (dp->name, name);
119 dp->next = dirs;
120 dirs = dp;
121 } else if (S_ISREG (st.st_mode)) {
122 int fd, i;
123 f * fp, * fp2;
124 h * hp;
125 char *p, *q;
126 char *n1, *n2;
127 int cksumsize = sizeof(buf);
128 unsigned int cksum;
129 time_t mtime = content_only ? 0 : st.st_mtime;
130 unsigned int hsh = hash (st.st_size, mtime);
131 nregfiles++;
132 if (verbose > 1)
133 fprintf(stderr, " %s", name);
134 fd = open (name, O_RDONLY);
135 if (fd < 0) return;
136 if (st.st_size < sizeof(buf)) {
137 cksumsize = st.st_size;
138 memset (((char *)buf) + cksumsize, 0, (sizeof(buf) - cksumsize) % sizeof(buf[0]));
140 if (read (fd, buf, cksumsize) != cksumsize) {
141 close(fd);
142 if (verbose > 1)
143 fprintf(stderr, "\r%*s\r", (int)strlen(name)+2, "");
144 return;
146 cksumsize = (cksumsize + sizeof(buf[0]) - 1) / sizeof(buf[0]);
147 for (i = 0, cksum = 0; i < cksumsize; i++) {
148 if (cksum + buf[i] < cksum)
149 cksum += buf[i] + 1;
150 else
151 cksum += buf[i];
153 for (hp = hps[hsh]; hp; hp = hp->next)
154 if (hp->size == st.st_size && hp->mtime == mtime)
155 break;
156 if (!hp) {
157 hp = malloc(sizeof(h));
158 if (!hp) {
159 fprintf(stderr, "\nOut of memory 1\n");
160 doexit(1);
162 hp->size = st.st_size;
163 hp->mtime = mtime;
164 hp->chain = NULL;
165 hp->next = hps[hsh];
166 hps[hsh] = hp;
168 for (fp = hp->chain; fp; fp = fp->next)
169 if (fp->cksum == cksum)
170 break;
171 for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
172 if (fp2->ino == st.st_ino && fp2->dev == st.st_dev) {
173 close(fd);
174 if (verbose > 1)
175 fprintf(stderr, "\r%*s\r", (int)strlen(name)+2, "");
176 return;
178 if (fp) {
179 p = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
180 nmmap++;
181 if (p == (void *)-1) {
182 close(fd);
183 fprintf(stderr, "\nFailed to mmap %s\n", name);
184 return;
187 for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next)
188 if (!lstat (fp2->name, &st2) && S_ISREG (st2.st_mode) &&
189 !stcmp (&st, &st2, content_only) &&
190 st2.st_ino != st.st_ino &&
191 st2.st_dev == st.st_dev) {
192 int fd2 = open (fp2->name, O_RDONLY);
193 if (fd2 < 0) continue;
194 if (fstat (fd2, &st2) || !S_ISREG (st2.st_mode)) {
195 close (fd2);
196 continue;
198 ncomp++;
199 q = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd2, 0);
200 if (q == (void *)-1) {
201 close(fd2);
202 fprintf(stderr, "\nFailed to mmap %s\n", fp2->name);
203 continue;
205 if (memcmp (p, q, st.st_size)) {
206 munmap (q, st.st_size);
207 close(fd2);
208 continue;
210 munmap (q, st.st_size);
211 close(fd2);
212 if (lstat (name, &st3)) {
213 fprintf(stderr, "\nCould not stat %s again\n", name);
214 munmap (p, st.st_size);
215 close(fd);
216 return;
218 st3.st_atime = st.st_atime;
219 if (stcmp (&st, &st3, 0)) {
220 fprintf(stderr, "\nFile %s changed underneath us\n", name);
221 munmap (p, st.st_size);
222 close(fd);
223 return;
225 n1 = fp2->name;
226 n2 = name;
227 if (!no_link) {
228 strcpy (stpcpy (nambuf2, n2), ".$$$___cleanit___$$$");
229 if (rename (n2, nambuf2)) {
230 fprintf(stderr, "\nFailed to rename %s to %s\n", n2, nambuf2);
231 continue;
233 if (link (n1, n2)) {
234 fprintf(stderr, "\nFailed to hardlink %s to %s\n", n1, n2);
235 if (rename (nambuf2, n2)) {
236 fprintf(stderr, "\nBad bad - failed to rename back %s to %s\n", nambuf2, n2);
238 munmap (p, st.st_size);
239 close(fd);
240 return;
242 unlink (nambuf2);
244 nlinks++;
245 if (st3.st_nlink > 1) {
246 /* We actually did not save anything this time, since the link second argument
247 had some other links as well. */
248 if (verbose > 1)
249 fprintf(stderr, "\r%*s\r%s %s to %s\n", (int)strlen(name)+2, "", (no_link ? "Would link" : "Linked"), n1, n2);
250 } else {
251 nsaved+=st.st_size;
252 if (verbose > 1)
253 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);
255 munmap (p, st.st_size);
256 close(fd);
257 return;
259 if (fp)
260 munmap (p, st.st_size);
261 fp2 = malloc(sizeof(f) + 1 + strlen (name));
262 if (!fp2) {
263 fprintf(stderr, "\nOut of memory 2\n");
264 doexit(2);
266 close(fd);
267 fp2->ino = st.st_ino;
268 fp2->dev = st.st_dev;
269 fp2->cksum = cksum;
270 strcpy(fp2->name, name);
271 if (fp) {
272 fp2->next = fp->next;
273 fp->next = fp2;
274 } else {
275 fp2->next = hp->chain;
276 hp->chain = fp2;
278 if (verbose > 1)
279 fprintf(stderr, "\r%*s\r", (int)strlen(name)+2, "");
280 return;
284 int main(int argc, char **argv)
286 int ch;
287 int i;
288 char *p;
289 d * dp;
290 DIR *dh;
291 struct dirent *di;
292 while ((ch = getopt (argc, argv, "cnv")) != -1) {
293 switch (ch) {
294 case 'n':
295 no_link++;
296 break;
297 case 'v':
298 verbose++;
299 break;
300 case 'c':
301 content_only++;
302 break;
303 default:
304 usage(argv[0]);
307 if (optind >= argc)
308 usage(argv[0]);
309 for (i = optind; i < argc; i++)
310 rf(argv[i]);
311 while (dirs) {
312 dp = dirs;
313 dirs = dp->next;
314 strcpy (nambuf1, dp->name);
315 free (dp);
316 strcat (nambuf1, "/");
317 p = strchr (nambuf1, 0);
318 dh = opendir (nambuf1);
319 if (dh == NULL)
320 continue;
321 ndirs++;
322 while ((di = readdir (dh)) != NULL) {
323 if (!di->d_name[0])
324 continue;
325 if (di->d_name[0] == '.') {
326 char *q;
327 if (!di->d_name[1] || !strcmp (di->d_name, "..") || !strncmp (di->d_name, ".in.", 4))
328 continue;
329 q = strrchr (di->d_name, '.');
330 if (q && strlen (q) == 7 && q != di->d_name) {
331 *p = 0;
332 if (verbose)
333 fprintf(stderr, "Skipping %s%s\n", nambuf1, di->d_name);
334 continue;
337 strcpy (p, di->d_name);
338 rf(nambuf1);
340 closedir(dh);
342 doexit(0);
343 return 0;