3 This patch adds the --link-by-hash=DIR option, which hard links received
4 files in a link farm arranged by MD4 file hash. The result is that the system
5 will only store one copy of the unique contents of each file, regardless of
8 To use this patch, run these commands for a successful build:
10 patch -p1 <patches/link-by-hash.diff
15 diff --git a/Makefile.in b/Makefile.in
18 @@ -36,7 +36,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
19 util.o main.o checksum.o match.o syscall.o log.o backup.o
20 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
21 fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
22 -OBJS3=progress.o pipe.o
23 +OBJS3=progress.o pipe.o hashlink.o
24 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
25 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
26 popt/popthelp.o popt/poptparse.o
27 diff --git a/flist.c b/flist.c
30 @@ -70,6 +70,7 @@ extern int need_unsorted_flist;
31 extern int unsort_ndx;
32 extern struct stats stats;
33 extern char *filesfrom_host;
34 +extern char *link_by_hash_dir;
36 extern char curr_dir[MAXPATHLEN];
38 @@ -830,7 +831,7 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
39 extra_len += EXTRA_LEN;
42 - if (always_checksum && S_ISREG(mode))
43 + if ((always_checksum || link_by_hash_dir) && S_ISREG(mode))
44 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
46 if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
47 diff --git a/hashlink.c b/hashlink.c
53 + Copyright (C) Cronosys, LLC 2004
55 + This program is free software; you can redistribute it and/or modify
56 + it under the terms of the GNU General Public License as published by
57 + the Free Software Foundation; either version 2 of the License, or
58 + (at your option) any later version.
60 + This program is distributed in the hope that it will be useful,
61 + but WITHOUT ANY WARRANTY; without even the implied warranty of
62 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 + GNU General Public License for more details.
65 + You should have received a copy of the GNU General Public License
66 + along with this program; if not, write to the Free Software
67 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
70 +/* This file contains code used by the --link-by-hash option. */
74 +extern char *link_by_hash_dir;
78 +char *make_hash_name(struct file_struct *file)
80 + char hash[33], *dst;
81 + uchar c, *src = (uchar*)F_SUM(file);
84 + for (dst = hash, i = 0; i < 4; i++, src++) {
86 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
88 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
91 + for (i = 0; i < 12; i++, src++) {
93 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
95 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
99 + asprintf(&dst,"%s/%s",link_by_hash_dir,hash);
104 +void kill_hashfile(struct hashfile_struct *hashfile)
108 + free(hashfile->name);
109 + close(hashfile->fd);
114 +void kill_hashfiles(struct hashfile_struct *hashfiles)
116 + struct hashfile_struct *iter, *next;
117 + if ((iter = hashfiles) != NULL) {
120 + kill_hashfile(iter);
122 + } while (iter != hashfiles);
127 +struct hashfile_struct *find_hashfiles(char *hashname, int64 size, long *fnbr)
131 + struct hashfile_struct *hashfiles = NULL, *hashfile;
137 + /* Build a list of potential candidates and open
139 + if ((d = opendir(hashname)) == NULL) {
140 + rsyserr(FERROR, errno, "opendir failed: \"%s\"", hashname);
144 + while ((di = readdir(d)) != NULL) {
145 + if (!strcmp(di->d_name,".") || !strcmp(di->d_name,"..")) {
149 + /* We need to have the largest fnbr in case we need to store
151 + this_fnbr = atol(di->d_name);
152 + if (this_fnbr > *fnbr)
155 + hashfile = new_array(struct hashfile_struct, 1);
156 + asprintf(&hashfile->name,"%s/%s",hashname,
158 + if (do_stat(hashfile->name,&st) == -1) {
159 + rsyserr(FERROR, errno, "stat failed: %s", hashfile->name);
160 + kill_hashfile(hashfile);
163 + if (st.st_size != size) {
164 + kill_hashfile(hashfile);
167 + hashfile->nlink = st.st_nlink;
168 + hashfile->fd = open(hashfile->name,O_RDONLY|O_BINARY);
169 + if (hashfile->fd == -1) {
170 + rsyserr(FERROR, errno, "open failed: %s", hashfile->name);
171 + kill_hashfile(hashfile);
174 + if (hashfiles == NULL)
175 + hashfiles = hashfile->next = hashfile->prev = hashfile;
177 + hashfile->next = hashfiles;
178 + hashfile->prev = hashfiles->prev;
179 + hashfile->next->prev = hashfile;
180 + hashfile->prev->next = hashfile;
189 +struct hashfile_struct *compare_hashfiles(int fd,struct hashfile_struct *files)
192 + char buffer[BUFSIZ], cmpbuffer[BUFSIZ];
193 + struct hashfile_struct *iter, *next, *best;
199 + iter = files; /* in case files are 0 bytes */
200 + while ((amt = read(fd, buffer, BUFSIZ)) > 0) {
203 + /* Icky bit to resync when we steal the first node. */
209 + hamt = read(iter->fd, cmpbuffer, BUFSIZ);
210 + if (amt != hamt || memcmp(buffer, cmpbuffer, amt)) {
211 + if (iter == files) {
212 + files = files->prev;
214 + if (iter->next == iter) {
215 + files = next = NULL;
218 + if (iter == files) {
219 + /* So we know to resync */
223 + iter->next->prev = iter->prev;
224 + iter->prev->next = iter->next;
225 + kill_hashfile(iter);
229 + } while (iter != files);
231 + if (iter == NULL && files == NULL) {
232 + /* There are no matches. */
238 + rsyserr(FERROR, errno, "read failed in compare_hashfiles()");
239 + kill_hashfiles(files);
243 + /* If we only have one file left, use it. */
244 + if (files == files->next) {
248 + /* All files which remain in the list are identical and should have
249 + * the same size. We pick the one with the lowest link count (we
250 + * may have rolled over because we hit the maximum link count for
251 + * the filesystem). */
252 + best = iter = files;
253 + nlink = iter->nlink;
255 + if (iter->nlink < nlink) {
256 + nlink = iter->nlink;
260 + } while (iter != files);
262 + best->next->prev = best->prev;
263 + best->prev->next = best->next;
265 + files = files->next;
266 + kill_hashfiles(files);
271 +int link_by_hash(const char *fnametmp, const char *fname, struct file_struct *file)
274 + char *hashname = make_hash_name(file);
279 + if (F_LENGTH(file) == 0)
280 + return robust_rename(fnametmp, fname, NULL, 0644);
282 + if (do_stat(hashname, &st) == -1) {
285 + /* Directory does not exist. */
286 + dirname = strdup(hashname);
287 + *strrchr(dirname,'/') = 0;
288 + if (do_mkdir(dirname, 0755) == -1 && errno != EEXIST) {
289 + rsyserr(FERROR, errno, "mkdir failed: %s", dirname);
292 + return robust_rename(fnametmp, fname, NULL, 0644);
296 + if (do_mkdir(hashname, 0755) == -1 && errno != EEXIST) {
297 + rsyserr(FERROR, errno, "mkdir failed: %s", hashname);
299 + return robust_rename(fnametmp, fname, NULL, 0644);
303 + asprintf(&linkname,"%s/0",hashname);
304 + rprintf(FINFO, "(1) linkname = %s\n", linkname);
306 + struct hashfile_struct *hashfiles, *hashfile;
308 + if (do_stat(fnametmp,&st) == -1) {
309 + rsyserr(FERROR, errno, "stat failed: %s", fname);
312 + hashfiles = find_hashfiles(hashname, st.st_size, &last_fnbr);
314 + if (hashfiles == NULL) {
316 + asprintf(&linkname,"%s/0",hashname);
317 + rprintf(FINFO, "(2) linkname = %s\n", linkname);
320 + /* Search for one identical to us. */
321 + if ((fd = open(fnametmp,O_RDONLY|O_BINARY)) == -1) {
322 + rsyserr(FERROR, errno, "open failed: %s", fnametmp);
323 + kill_hashfiles(hashfiles);
326 + hashfile = compare_hashfiles(fd, hashfiles);
332 + linkname = strdup(hashfile->name);
333 + rprintf(FINFO, "(3) linkname = %s\n", linkname);
334 + kill_hashfile(hashfile);
337 + asprintf(&linkname, "%s/%ld", hashname,
339 + rprintf(FINFO, "(4) linkname = %s\n", linkname);
345 + rprintf(FINFO, "link-by-hash (existing): \"%s\" -> %s\n",
346 + linkname, full_fname(fname));
347 + robust_unlink(fname);
348 + rc = do_link(linkname, fname);
350 + if (errno == EMLINK) {
353 + asprintf(&linkname,"%s/%ld",hashname,
355 + rprintf(FINFO, "(5) linkname = %s\n", linkname);
356 + rprintf(FINFO,"link-by-hash: max link count exceeded, starting new file \"%s\".\n", linkname);
358 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
359 + linkname, full_fname(fname));
360 + rc = robust_rename(fnametmp, fname, NULL, 0644);
363 + do_unlink(fnametmp);
368 + rprintf(FINFO, "link-by-hash (new): %s -> \"%s\"\n",
369 + full_fname(fname),linkname);
371 + rc = robust_rename(fnametmp, fname, NULL, 0644);
373 + rsyserr(FERROR, errno, "rename \"%s\" -> \"%s\"",
374 + full_fname(fnametmp), full_fname(fname));
376 + rc = do_link(fname,linkname);
378 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
379 + full_fname(fname), linkname);
388 diff --git a/options.c b/options.c
391 @@ -157,6 +157,7 @@ char *backup_suffix = NULL;
393 char *partial_dir = NULL;
394 char *basis_dir[MAX_BASIS_DIRS+1];
395 +char *link_by_hash_dir = NULL;
396 char *config_file = NULL;
397 char *shell_cmd = NULL;
398 char *logfile_name = NULL;
399 @@ -394,6 +395,7 @@ void usage(enum logcode F)
400 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
401 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
402 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
403 + rprintf(F," --link-by-hash=DIR create hardlinks by hash into DIR\n");
404 rprintf(F," -z, --compress compress file data during the transfer\n");
405 rprintf(F," --compress-level=NUM explicitly set compression level\n");
406 rprintf(F," --skip-compress=LIST skip compressing files with a suffix in LIST\n");
407 @@ -446,7 +448,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
408 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
409 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
410 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
411 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
412 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_LINK_BY_HASH,
413 OPT_SERVER, OPT_REFUSED_BASE = 9000};
415 static struct poptOption long_options[] = {
416 @@ -578,6 +580,7 @@ static struct poptOption long_options[] = {
417 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
418 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
419 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
420 + {"link-by-hash", 0, POPT_ARG_STRING, 0, OPT_LINK_BY_HASH, 0, 0},
421 {"fuzzy", 'y', POPT_ARG_VAL, &fuzzy_basis, 1, 0, 0 },
422 {"no-fuzzy", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
423 {"no-y", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
424 @@ -1260,6 +1263,21 @@ int parse_arguments(int *argc_p, const char ***argv_p)
428 + case OPT_LINK_BY_HASH:
430 + arg = poptGetOptArg(pc);
431 + if (sanitize_paths)
432 + arg = sanitize_path(NULL, arg, NULL, 0, SP_DEFAULT);
433 + link_by_hash_dir = (char *)arg;
436 + snprintf(err_buf, sizeof err_buf,
437 + "hard links are not supported on this %s\n",
438 + am_server ? "server" : "client");
439 + rprintf(FERROR, "ERROR: %s", err_buf);
444 /* A large opt value means that set_refuse_options()
445 * turned this option off. */
446 @@ -2023,6 +2041,11 @@ void server_options(char **args, int *argc_p)
448 args[ac++] = "--inplace";
450 + if (link_by_hash_dir && am_sender) {
451 + args[ac++] = "--link-by-hash";
452 + args[ac++] = link_by_hash_dir;
455 if (files_from && (!am_sender || filesfrom_host)) {
456 if (filesfrom_host) {
457 args[ac++] = "--files-from";
458 diff --git a/receiver.c b/receiver.c
461 @@ -162,12 +162,14 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
464 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
465 - const char *fname, int fd, OFF_T total_size)
466 + const char *fname, int fd, OFF_T total_size,
469 static char file_sum1[MAX_DIGEST_LEN];
470 static char file_sum2[MAX_DIGEST_LEN];
471 struct map_struct *mapbuf;
472 struct sum_struct sum;
473 + md_context mdfour_data;
477 @@ -187,6 +189,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
482 + mdfour_begin(&mdfour_data);
484 sum_init(checksum_seed);
486 if (append_mode > 0) {
487 @@ -231,6 +236,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
488 cleanup_got_literal = 1;
492 + mdfour_update(&mdfour_data, (uchar*)data, i);
494 if (fd != -1 && write_file(fd,data,i) != i)
495 goto report_write_error;
496 @@ -257,6 +264,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
499 sum_update(map, len);
501 + mdfour_update(&mdfour_data, (uchar*)map, len);
504 if (updating_basis_or_equiv) {
505 @@ -299,6 +308,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
508 sum_len = sum_end(file_sum1);
510 + mdfour_result(&mdfour_data, (uchar*)md4);
514 @@ -314,7 +325,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
516 static void discard_receive_data(int f_in, OFF_T length)
518 - receive_data(f_in, NULL, -1, 0, NULL, -1, length);
519 + receive_data(f_in, NULL, -1, 0, NULL, -1, length, NULL);
522 static void handle_delayed_updates(char *local_name)
523 @@ -676,7 +687,7 @@ int recv_files(int f_in, char *local_name)
526 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
527 - fname, fd2, F_LENGTH(file));
528 + fname, fd2, F_LENGTH(file), F_SUM(file));
530 log_item(log_code, file, &initial_stats, iflags, NULL);
532 diff --git a/rsync.c b/rsync.c
535 @@ -48,6 +48,7 @@ extern int inplace;
536 extern int flist_eof;
537 extern int keep_dirlinks;
538 extern int make_backups;
539 +extern char *link_by_hash_dir;
540 extern struct file_list *cur_flist, *first_flist, *dir_flist;
541 extern struct chmod_mode_struct *daemon_chmod_modes;
543 @@ -571,8 +572,15 @@ int finish_transfer(const char *fname, const char *fnametmp,
544 /* move tmp file over real file */
546 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
547 - ret = robust_rename(fnametmp, fname, temp_copy_name,
548 - file->mode & INITACCESSPERMS);
550 + if (link_by_hash_dir)
551 + ret = link_by_hash(fnametmp, fname, file);
555 + ret = robust_rename(fnametmp, fname, temp_copy_name,
556 + file->mode & INITACCESSPERMS);
559 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
560 ret == -2 ? "copy" : "rename",
561 diff --git a/rsync.h b/rsync.h
564 @@ -820,6 +820,14 @@ struct stats {
565 int num_transferred_files;
568 +struct hashfile_struct {
569 + struct hashfile_struct *next;
570 + struct hashfile_struct *prev;
576 struct chmod_mode_struct;
578 #define EMPTY_ITEM_LIST {NULL, 0, 0}
579 diff --git a/rsync.yo b/rsync.yo
582 @@ -388,6 +388,7 @@ to the detailed description below for a complete description. verb(
583 --compare-dest=DIR also compare received files relative to DIR
584 --copy-dest=DIR ... and include copies of unchanged files
585 --link-dest=DIR hardlink to files in DIR when unchanged
586 + --link-by-hash=DIR create hardlinks by hash into DIR
587 -z, --compress compress file data during the transfer
588 --compress-level=NUM explicitly set compression level
589 --skip-compress=LIST skip compressing files with suffix in LIST