1 After applying this patch and running configure, you MUST run this
6 Jason M. Felice writes:
8 This patch adds the --link-by-hash=DIR option, which hard links received
9 files in a link farm arranged by MD4 file hash. The result is that the system
10 will only store one copy of the unique contents of each file, regardless of
14 --- orig/Makefile.in 2004-07-04 08:59:17
15 +++ Makefile.in 2004-07-03 20:20:15
16 @@ -35,7 +35,7 @@ OBJS1=rsync.o generator.o receiver.o cle
17 main.o checksum.o match.o syscall.o log.o backup.o
18 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
19 fileio.o batch.o clientname.o
20 -OBJS3=progress.o pipe.o
21 +OBJS3=progress.o pipe.o hashlink.o
22 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
23 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
24 popt/popthelp.o popt/poptparse.o
25 --- orig/hashlink.c 2004-07-02 21:41:54
26 +++ hashlink.c 2004-07-02 21:41:54
29 + Copyright (C) Cronosys, LLC 2004
31 + This program is free software; you can redistribute it and/or modify
32 + it under the terms of the GNU General Public License as published by
33 + the Free Software Foundation; either version 2 of the License, or
34 + (at your option) any later version.
36 + This program is distributed in the hope that it will be useful,
37 + but WITHOUT ANY WARRANTY; without even the implied warranty of
38 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 + GNU General Public License for more details.
41 + You should have received a copy of the GNU General Public License
42 + along with this program; if not, write to the Free Software
43 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
46 +/* This file contains code used by the --link-by-hash option. */
50 +extern char *link_by_hash_dir;
54 +char* make_hash_name(struct file_struct *file)
56 + char hash[33], *dst;
61 + src = (unsigned char*)file->u.sum;
62 + for (dst = hash, i = 0; i < 4; i++, src++) {
64 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
66 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
69 + for (i = 0; i < 12; i++, src++) {
71 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
73 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
77 + asprintf(&dst,"%s/%s",link_by_hash_dir,hash);
82 +void kill_hashfile(struct hashfile_struct *hashfile)
86 + free(hashfile->name);
87 + close(hashfile->fd);
92 +void kill_hashfiles(struct hashfile_struct *hashfiles)
94 + struct hashfile_struct *iter, *next;
95 + if ((iter = hashfiles) != NULL) {
98 + kill_hashfile(iter);
100 + } while (iter != hashfiles);
105 +struct hashfile_struct *find_hashfiles(char *hashname, int64 size, long *fnbr)
109 + struct hashfile_struct *hashfiles = NULL, *hashfile;
115 + /* Build a list of potential candidates and open
117 + if ((d = opendir(hashname)) == NULL) {
118 + rsyserr(FERROR, errno, "opendir \"%s\"", hashname);
122 + while ((di = readdir(d)) != NULL) {
123 + if (!strcmp(di->d_name,".") || !strcmp(di->d_name,"..")) {
127 + /* We need to have the largest fnbr in case we need to store
129 + this_fnbr = atol(di->d_name);
130 + if (this_fnbr > *fnbr)
133 + hashfile = (struct hashfile_struct*)malloc(sizeof(struct hashfile_struct));
134 + asprintf(&hashfile->name,"%s/%s",hashname,
136 + if (do_stat(hashfile->name,&st) == -1) {
137 + rsyserr(FERROR, errno, "%s: %s", hashfile->name);
138 + kill_hashfile(hashfile);
141 + if (st.st_size != size) {
142 + kill_hashfile(hashfile);
145 + hashfile->nlink = st.st_nlink;
146 + hashfile->fd = open(hashfile->name,O_RDONLY|O_BINARY);
147 + if (hashfile->fd == -1) {
148 + rsyserr(FERROR, errno, "%s", hashfile->name);
149 + kill_hashfile(hashfile);
152 + if (hashfiles == NULL)
153 + hashfiles = hashfile->next = hashfile->prev = hashfile;
155 + hashfile->next = hashfiles;
156 + hashfile->prev = hashfiles->prev;
157 + hashfile->next->prev = hashfile;
158 + hashfile->prev->next = hashfile;
167 +struct hashfile_struct *compare_hashfiles(int fd,struct hashfile_struct *files)
170 + char buffer[BUFSIZ], cmpbuffer[BUFSIZ];
171 + struct hashfile_struct *iter, *next, *best;
177 + iter = files; /* in case files are 0 bytes */
178 + while ((amt = read(fd, buffer, BUFSIZ)) > 0) {
181 + /* Icky bit to resync when we steal the first node. */
187 + hamt = read(iter->fd, cmpbuffer, BUFSIZ);
188 + if (amt != hamt || memcmp(buffer, cmpbuffer, amt)) {
189 + if (iter == files) {
190 + files = files->prev;
192 + if (iter->next == iter) {
193 + files = next = NULL;
196 + if (iter == files) {
197 + /* So we know to resync */
201 + iter->next->prev = iter->prev;
202 + iter->prev->next = iter->next;
203 + kill_hashfile(iter);
207 + } while (iter != files);
209 + if (iter == NULL && files == NULL) {
210 + /* There are no matches. */
217 + rsyserr(FERROR, errno, "%s");
218 + kill_hashfiles(files);
222 + /* If we only have one file left, use it. */
223 + if (files == files->next) {
227 + /* All files which remain in the list are identical and should have
228 + * the same size. We pick the one with the lowest link count (we
229 + * may have rolled over because we hit the maximum link count for
230 + * the filesystem). */
231 + best = iter = files;
232 + nlink = iter->nlink;
234 + if (iter->nlink < nlink) {
235 + nlink = iter->nlink;
239 + } while (iter != files);
241 + best->next->prev = best->prev;
242 + best->prev->next = best->next;
244 + files = files->next;
245 + kill_hashfiles(files);
250 +int link_by_hash(char *fnametmp,char *fname,struct file_struct *file)
253 + char *hashname = make_hash_name(file);
258 + if (file->length == 0) {
259 + return robust_rename(fnametmp,fname,0644);
262 + if (do_stat(hashname, &st) == -1) {
265 + /* Directory does not exist. */
266 + dirname = strdup(hashname);
267 + *strrchr(dirname,'/') = 0;
268 + if (do_mkdir(dirname, 0755) == -1 && errno != EEXIST) {
269 + rsyserr(FERROR, errno, "mkdir %s", dirname);
272 + return robust_rename(fnametmp,fname,0644);
276 + if (do_mkdir(hashname, 0755) == -1 && errno != EEXIST) {
277 + rsyserr(FERROR, errno, "mkdir %s", hashname);
279 + return robust_rename(fnametmp,fname,0644);
283 + asprintf(&linkname,"%s/0",hashname);
284 + rprintf(FINFO, "(1) linkname = %s\n", linkname);
287 + struct hashfile_struct *hashfiles, *hashfile;
290 + if (do_stat(fnametmp,&st) == -1) {
291 + rsyserr(FERROR, errno, "%s", fname);
294 + hashfiles = find_hashfiles(hashname, st.st_size, &last_fnbr);
296 + if (hashfiles == NULL) {
298 + asprintf(&linkname,"%s/0",hashname);
299 + rprintf(FINFO, "(2) linkname = %s\n", linkname);
302 + /* Search for one identical to us. */
303 + if ((fd = open(fnametmp,O_RDONLY|O_BINARY)) == -1) {
304 + rsyserr(FERROR, errno, "%s", fnametmp);
305 + kill_hashfiles(hashfiles);
308 + hashfile = compare_hashfiles(fd, hashfiles);
313 + linkname = strdup(hashfile->name);
314 + rprintf(FINFO, "(3) linkname = %s\n", linkname);
315 + kill_hashfile(hashfile);
318 + asprintf(&linkname, "%s/%ld", hashname,
320 + rprintf(FINFO, "(4) linkname = %s\n", linkname);
326 + rprintf(FINFO, "link-by-hash (existing): \"%s\" -> %s\n",
327 + linkname, full_fname(fname));
328 + rc = do_link(linkname, fname);
330 + if (errno == EMLINK) {
333 + asprintf(&linkname,"%s/%ld",hashname,
335 + rprintf(FINFO, "(5) linkname = %s\n", linkname);
336 + rprintf(FINFO,"link-by-hash: max link count exceeded, starting new file \"%s\".\n", linkname);
338 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
339 + linkname, full_fname(fname));
340 + robust_unlink(fname);
341 + rc = robust_rename(fnametmp,fname,0644);
344 + do_unlink(fnametmp);
349 + rprintf(FINFO, "link-by-hash (new): %s -> \"%s\"\n",
350 + full_fname(fname),linkname);
352 + rc = robust_rename(fnametmp,fname,0644);
354 + rsyserr(FERROR, errno, "rename \"%s\" -> \"%s\"",
355 + full_fname(fnametmp), full_fname(fname));
357 + rc = do_link(fname,linkname);
359 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
360 + full_fname(fname), linkname);
370 --- orig/options.c 2004-07-16 20:07:22
371 +++ options.c 2004-07-03 20:20:15
372 @@ -125,6 +125,7 @@ char *log_format = NULL;
373 char *password_file = NULL;
374 char *rsync_path = RSYNC_PATH;
375 char *backup_dir = NULL;
376 +char *link_by_hash_dir = NULL;
377 char backup_dir_buf[MAXPATHLEN];
378 int rsync_port = RSYNC_PORT;
380 @@ -276,6 +277,7 @@ void usage(enum logcode F)
381 rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n");
382 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
383 rprintf(F," --link-dest=DIR create hardlinks to DIR for unchanged files\n");
384 + rprintf(F," --link-by-hash=DIR create hardlinks by hash to DIR for regular files\n");
385 rprintf(F," -P equivalent to --partial --progress\n");
386 rprintf(F," -z, --compress compress file data\n");
387 rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n");
388 @@ -316,7 +318,7 @@ void usage(enum logcode F)
389 enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
390 OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
391 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
392 - OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT,
393 + OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_LINK_BY_HASH,
394 OPT_REFUSED_BASE = 9000};
396 static struct poptOption long_options[] = {
397 @@ -375,6 +377,7 @@ static struct poptOption long_options[]
398 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
399 {"compare-dest", 0, POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
400 {"link-dest", 0, POPT_ARG_STRING, &compare_dest, OPT_LINK_DEST, 0, 0 },
401 + {"link-by-hash", 0, POPT_ARG_STRING, 0, OPT_LINK_BY_HASH, 0, 0},
402 /* TODO: Should this take an optional int giving the compression level? */
403 {"compress", 'z', POPT_ARG_NONE, &do_compression, 0, 0, 0 },
404 {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 },
405 @@ -601,6 +604,19 @@ int parse_arguments(int *argc, const cha
409 + case OPT_LINK_BY_HASH:
411 + link_by_hash_dir = (char *)poptGetOptArg(pc);
412 + checksum_seed = FIXED_CHECKSUM_SEED;
415 + snprintf(err_buf, sizeof err_buf,
416 + "hard links are not supported on this %s\n",
417 + am_server ? "server" : "client");
418 + rprintf(FERROR, "ERROR: %s", err_buf);
423 /* A large opt value means that set_refuse_options()
424 * turned this option off (opt-BASE is its index). */
425 @@ -1007,6 +1023,11 @@ void server_options(char **args,int *arg
426 args[ac++] = compare_dest;
429 + if (link_by_hash_dir && am_sender) {
430 + args[ac++] = "--link-by-hash";
431 + args[ac++] = link_by_hash_dir;
434 if (files_from && (!am_sender || remote_filesfrom_file)) {
435 if (remote_filesfrom_file) {
436 args[ac++] = "--files-from";
437 --- orig/receiver.c 2004-07-19 17:06:10
438 +++ receiver.c 2004-07-19 17:07:31
439 @@ -37,6 +37,7 @@ extern int cvs_exclude;
442 extern char *compare_dest;
443 +extern char *link_by_hash_dir;
444 extern int make_backups;
445 extern int do_progress;
446 extern char *backup_dir;
447 @@ -200,10 +201,11 @@ static int get_tmpname(char *fnametmp, c
450 static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
452 + OFF_T total_size,char *md4)
455 struct sum_struct sum;
456 + struct mdfour mdfour_data;
460 @@ -214,6 +216,9 @@ static int receive_data(int f_in,struct
462 read_sum_head(f_in, &sum);
465 + mdfour_begin(&mdfour_data);
467 sum_init(checksum_seed);
469 while ((i = recv_token(f_in, &data)) != 0) {
470 @@ -230,6 +235,8 @@ static int receive_data(int f_in,struct
471 cleanup_got_literal = 1;
475 + mdfour_update(&mdfour_data,data,i);
477 if (fd != -1 && write_file(fd,data,i) != i) {
478 rsyserr(FERROR, errno, "write failed on %s",
479 @@ -257,6 +264,8 @@ static int receive_data(int f_in,struct
484 + mdfour_update(&mdfour_data,map,len);
487 if (!inplace || offset != offset2) {
488 @@ -293,6 +302,8 @@ static int receive_data(int f_in,struct
493 + mdfour_result(&mdfour_data, (unsigned char*)md4);
495 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
497 @@ -305,7 +316,7 @@ static int receive_data(int f_in,struct
499 static void discard_receive_data(int f_in, OFF_T length)
501 - receive_data(f_in, NULL, -1, NULL, length);
502 + receive_data(f_in, NULL, -1, NULL, length, NULL);
506 @@ -510,7 +521,11 @@ int recv_files(int f_in, struct file_lis
507 rprintf(FINFO, "%s\n", fname);
510 - recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
512 + if (link_by_hash_dir)
513 + file->u.sum = (char*)malloc(MD4_SUM_LENGTH);
515 + recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length,file->u.sum);
517 log_recv(file, &initial_stats);
519 --- orig/rsync.c 2004-07-16 20:07:23
520 +++ rsync.c 2004-07-16 20:16:53
521 @@ -33,6 +33,7 @@ extern int preserve_gid;
522 extern int force_delete;
524 extern int make_backups;
525 +extern char *link_by_hash_dir;
526 extern char *backup_dir;
529 @@ -250,6 +251,12 @@ void finish_transfer(char *fname, char *
530 /* move tmp file over real file */
532 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
534 + if (link_by_hash_dir)
535 + ret = link_by_hash(fnametmp, fname, file);
538 + ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS);
539 ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS);
541 rsyserr(FERROR, errno, "%s %s -> \"%s\"",
542 --- orig/rsync.h 2004-07-16 20:07:23
543 +++ rsync.h 2004-07-03 20:20:15
544 @@ -524,6 +524,14 @@ struct stats {
545 int current_file_index;
548 +struct hashfile_struct {
549 + struct hashfile_struct *next;
550 + struct hashfile_struct *prev;
557 /* we need this function because of the silly way in which duplicate
558 entries are handled in the file lists - we can't change this