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 based-on: 25082d1ef6712a15c52a1dacb36b7f0642c23ac8
16 diff --git a/Makefile.in b/Makefile.in
19 @@ -37,7 +37,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
20 util.o main.o checksum.o match.o syscall.o log.o backup.o delete.o
21 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
22 fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
23 -OBJS3=progress.o pipe.o
24 +OBJS3=progress.o pipe.o hashlink.o
25 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
26 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
27 popt/popthelp.o popt/poptparse.o
28 diff --git a/flist.c b/flist.c
31 @@ -74,6 +74,7 @@ extern int sender_keeps_checksum;
32 extern int unsort_ndx;
33 extern struct stats stats;
34 extern char *filesfrom_host;
35 +extern char *link_by_hash_dir;
36 extern char *usermap, *groupmap;
38 extern char curr_dir[MAXPATHLEN];
39 @@ -910,7 +911,7 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
40 extra_len += EXTRA_LEN;
43 - if (always_checksum && S_ISREG(mode))
44 + if ((always_checksum || link_by_hash_dir) && S_ISREG(mode))
45 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
48 diff --git a/hashlink.c b/hashlink.c
54 + Copyright (C) Cronosys, LLC 2004
56 + This program is free software; you can redistribute it and/or modify
57 + it under the terms of the GNU General Public License as published by
58 + the Free Software Foundation; either version 2 of the License, or
59 + (at your option) any later version.
61 + This program is distributed in the hope that it will be useful,
62 + but WITHOUT ANY WARRANTY; without even the implied warranty of
63 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 + GNU General Public License for more details.
66 + You should have received a copy of the GNU General Public License
67 + along with this program; if not, write to the Free Software
68 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
71 +/* This file contains code used by the --link-by-hash option. */
75 +extern char *link_by_hash_dir;
79 +char *make_hash_name(struct file_struct *file)
81 + char hash[33], *dst;
82 + uchar c, *src = (uchar*)F_SUM(file);
85 + for (dst = hash, i = 0; i < 4; i++, src++) {
87 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
89 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
92 + for (i = 0; i < 12; i++, src++) {
94 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
96 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
100 + if (asprintf(&dst,"%s/%s",link_by_hash_dir,hash) < 0)
101 + out_of_memory("make_hash_name");
106 +void kill_hashfile(struct hashfile_struct *hashfile)
110 + free(hashfile->name);
111 + close(hashfile->fd);
116 +void kill_hashfiles(struct hashfile_struct *hashfiles)
118 + struct hashfile_struct *iter, *next;
119 + if ((iter = hashfiles) != NULL) {
122 + kill_hashfile(iter);
124 + } while (iter != hashfiles);
129 +struct hashfile_struct *find_hashfiles(char *hashname, int64 size, long *fnbr)
133 + struct hashfile_struct *hashfiles = NULL, *hashfile;
139 + /* Build a list of potential candidates and open
141 + if ((d = opendir(hashname)) == NULL) {
142 + rsyserr(FERROR, errno, "opendir failed: \"%s\"", hashname);
146 + while ((di = readdir(d)) != NULL) {
147 + if (!strcmp(di->d_name,".") || !strcmp(di->d_name,"..")) {
151 + /* We need to have the largest fnbr in case we need to store
153 + this_fnbr = atol(di->d_name);
154 + if (this_fnbr > *fnbr)
157 + hashfile = new_array(struct hashfile_struct, 1);
158 + if (asprintf(&hashfile->name,"%s/%s",hashname, di->d_name) < 0)
159 + out_of_memory("find_hashfiles");
160 + if (do_stat(hashfile->name,&st) == -1) {
161 + rsyserr(FERROR, errno, "stat failed: %s", hashfile->name);
162 + kill_hashfile(hashfile);
165 + if (st.st_size != size) {
166 + kill_hashfile(hashfile);
169 + hashfile->nlink = st.st_nlink;
170 + hashfile->fd = open(hashfile->name,O_RDONLY|O_BINARY);
171 + if (hashfile->fd == -1) {
172 + rsyserr(FERROR, errno, "open failed: %s", hashfile->name);
173 + kill_hashfile(hashfile);
176 + if (hashfiles == NULL)
177 + hashfiles = hashfile->next = hashfile->prev = hashfile;
179 + hashfile->next = hashfiles;
180 + hashfile->prev = hashfiles->prev;
181 + hashfile->next->prev = hashfile;
182 + hashfile->prev->next = hashfile;
191 +struct hashfile_struct *compare_hashfiles(int fd,struct hashfile_struct *files)
194 + char buffer[BUFSIZ], cmpbuffer[BUFSIZ];
195 + struct hashfile_struct *iter, *next, *best;
201 + iter = files; /* in case files are 0 bytes */
202 + while ((amt = read(fd, buffer, BUFSIZ)) > 0) {
205 + /* Icky bit to resync when we steal the first node. */
211 + hamt = read(iter->fd, cmpbuffer, BUFSIZ);
212 + if (amt != hamt || memcmp(buffer, cmpbuffer, amt)) {
213 + if (iter == files) {
214 + files = files->prev;
216 + if (iter->next == iter) {
217 + files = next = NULL;
220 + if (iter == files) {
221 + /* So we know to resync */
225 + iter->next->prev = iter->prev;
226 + iter->prev->next = iter->next;
227 + kill_hashfile(iter);
231 + } while (iter != files);
233 + if (iter == NULL && files == NULL) {
234 + /* There are no matches. */
240 + rsyserr(FERROR, errno, "read failed in compare_hashfiles()");
241 + kill_hashfiles(files);
245 + /* If we only have one file left, use it. */
246 + if (files == files->next) {
250 + /* All files which remain in the list are identical and should have
251 + * the same size. We pick the one with the lowest link count (we
252 + * may have rolled over because we hit the maximum link count for
253 + * the filesystem). */
254 + best = iter = files;
255 + nlink = iter->nlink;
257 + if (iter->nlink < nlink) {
258 + nlink = iter->nlink;
262 + } while (iter != files);
264 + best->next->prev = best->prev;
265 + best->prev->next = best->next;
267 + files = files->next;
268 + kill_hashfiles(files);
273 +int link_by_hash(const char *fnametmp, const char *fname, struct file_struct *file)
276 + char *hashname = make_hash_name(file);
281 + if (F_LENGTH(file) == 0)
282 + return robust_rename(fnametmp, fname, NULL, 0644);
284 + if (do_stat(hashname, &st) == -1) {
287 + /* Directory does not exist. */
288 + dirname = strdup(hashname);
289 + *strrchr(dirname,'/') = 0;
290 + if (do_mkdir(dirname, 0755) == -1 && errno != EEXIST) {
291 + rsyserr(FERROR, errno, "mkdir failed: %s", dirname);
294 + return robust_rename(fnametmp, fname, NULL, 0644);
298 + if (do_mkdir(hashname, 0755) == -1 && errno != EEXIST) {
299 + rsyserr(FERROR, errno, "mkdir failed: %s", hashname);
301 + return robust_rename(fnametmp, fname, NULL, 0644);
305 + if (asprintf(&linkname,"%s/0",hashname) < 0)
306 + out_of_memory("link_by_hash");
307 + rprintf(FINFO, "(1) linkname = %s\n", linkname);
309 + struct hashfile_struct *hashfiles, *hashfile;
311 + if (do_stat(fnametmp,&st) == -1) {
312 + rsyserr(FERROR, errno, "stat failed: %s", fname);
315 + hashfiles = find_hashfiles(hashname, st.st_size, &last_fnbr);
317 + if (hashfiles == NULL) {
319 + if (asprintf(&linkname,"%s/0",hashname) < 0)
320 + out_of_memory("link_by_hash");
321 + rprintf(FINFO, "(2) linkname = %s\n", linkname);
324 + /* Search for one identical to us. */
325 + if ((fd = open(fnametmp,O_RDONLY|O_BINARY)) == -1) {
326 + rsyserr(FERROR, errno, "open failed: %s", fnametmp);
327 + kill_hashfiles(hashfiles);
330 + hashfile = compare_hashfiles(fd, hashfiles);
336 + linkname = strdup(hashfile->name);
337 + rprintf(FINFO, "(3) linkname = %s\n", linkname);
338 + kill_hashfile(hashfile);
341 + if (asprintf(&linkname, "%s/%ld", hashname, last_fnbr + 1) < 0)
342 + out_of_memory("link_by_hash");
343 + rprintf(FINFO, "(4) linkname = %s\n", linkname);
349 + rprintf(FINFO, "link-by-hash (existing): \"%s\" -> %s\n",
350 + linkname, full_fname(fname));
351 + robust_unlink(fname);
352 + rc = do_link(linkname, fname);
354 + if (errno == EMLINK) {
357 + if (asprintf(&linkname,"%s/%ld",hashname, last_fnbr + 1) < 0)
358 + out_of_memory("link_by_hash");
359 + rprintf(FINFO, "(5) linkname = %s\n", linkname);
360 + rprintf(FINFO,"link-by-hash: max link count exceeded, starting new file \"%s\".\n", linkname);
362 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
363 + linkname, full_fname(fname));
364 + rc = robust_rename(fnametmp, fname, NULL, 0644);
367 + do_unlink(fnametmp);
372 + rprintf(FINFO, "link-by-hash (new): %s -> \"%s\"\n",
373 + full_fname(fname),linkname);
375 + rc = robust_rename(fnametmp, fname, NULL, 0644);
377 + rsyserr(FERROR, errno, "rename \"%s\" -> \"%s\"",
378 + full_fname(fnametmp), full_fname(fname));
380 + rc = do_link(fname,linkname);
382 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
383 + full_fname(fname), linkname);
392 diff --git a/options.c b/options.c
395 @@ -158,6 +158,7 @@ char *backup_suffix = NULL;
397 char *partial_dir = NULL;
398 char *basis_dir[MAX_BASIS_DIRS+1];
399 +char *link_by_hash_dir = NULL;
400 char *config_file = NULL;
401 char *shell_cmd = NULL;
402 char *logfile_name = NULL;
403 @@ -746,6 +747,7 @@ void usage(enum logcode F)
404 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
405 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
406 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
407 + rprintf(F," --link-by-hash=DIR create hardlinks by hash into DIR\n");
408 rprintf(F," -z, --compress compress file data during the transfer\n");
409 rprintf(F," --compress-level=NUM explicitly set compression level\n");
410 rprintf(F," --skip-compress=LIST skip compressing files with a suffix in LIST\n");
411 @@ -798,7 +800,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
412 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
413 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
414 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
415 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
416 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG, OPT_LINK_BY_HASH,
417 OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT,
418 OPT_SERVER, OPT_REFUSED_BASE = 9000};
420 @@ -938,6 +940,7 @@ static struct poptOption long_options[] = {
421 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
422 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
423 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
424 + {"link-by-hash", 0, POPT_ARG_STRING, 0, OPT_LINK_BY_HASH, 0, 0},
425 {"fuzzy", 'y', POPT_ARG_VAL, &fuzzy_basis, 1, 0, 0 },
426 {"no-fuzzy", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
427 {"no-y", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
428 @@ -1764,6 +1767,21 @@ int parse_arguments(int *argc_p, const char ***argv_p)
432 + case OPT_LINK_BY_HASH:
434 + arg = poptGetOptArg(pc);
435 + if (sanitize_paths)
436 + arg = sanitize_path(NULL, arg, NULL, 0, SP_DEFAULT);
437 + link_by_hash_dir = (char *)arg;
440 + snprintf(err_buf, sizeof err_buf,
441 + "hard links are not supported on this %s\n",
442 + am_server ? "server" : "client");
443 + rprintf(FERROR, "ERROR: %s", err_buf);
448 /* A large opt value means that set_refuse_options()
449 * turned this option off. */
450 @@ -2636,6 +2654,11 @@ void server_options(char **args, int *argc_p)
452 args[ac++] = "--inplace";
454 + if (link_by_hash_dir && am_sender) {
455 + args[ac++] = "--link-by-hash";
456 + args[ac++] = link_by_hash_dir;
459 if (files_from && (!am_sender || filesfrom_host)) {
460 if (filesfrom_host) {
461 args[ac++] = "--files-from";
462 diff --git a/receiver.c b/receiver.c
465 @@ -197,11 +197,13 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
468 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
469 - const char *fname, int fd, OFF_T total_size)
470 + const char *fname, int fd, OFF_T total_size,
473 static char file_sum1[MAX_DIGEST_LEN];
474 struct map_struct *mapbuf;
475 struct sum_struct sum;
476 + md_context mdfour_data;
480 @@ -221,6 +223,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
485 + mdfour_begin(&mdfour_data);
487 sum_init(checksum_seed);
489 if (append_mode > 0) {
490 @@ -268,6 +273,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
491 cleanup_got_literal = 1;
495 + mdfour_update(&mdfour_data, (uchar*)data, i);
497 if (fd != -1 && write_file(fd,data,i) != i)
498 goto report_write_error;
499 @@ -294,6 +301,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
502 sum_update(map, len);
504 + mdfour_update(&mdfour_data, (uchar*)map, len);
507 if (updating_basis_or_equiv) {
508 @@ -341,6 +350,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
509 if (sum_end(file_sum1) != checksum_len)
510 overflow_exit("checksum_len"); /* Impossible... */
513 + mdfour_result(&mdfour_data, (uchar*)md4);
518 @@ -355,7 +367,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
520 static void discard_receive_data(int f_in, OFF_T length)
522 - receive_data(f_in, NULL, -1, 0, NULL, -1, length);
523 + receive_data(f_in, NULL, -1, 0, NULL, -1, length, NULL);
526 static void handle_delayed_updates(char *local_name)
527 @@ -783,7 +795,7 @@ int recv_files(int f_in, int f_out, char *local_name)
530 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
531 - fname, fd2, F_LENGTH(file));
532 + fname, fd2, F_LENGTH(file), F_SUM(file));
534 log_item(log_code, file, iflags, NULL);
536 diff --git a/rsync.c b/rsync.c
539 @@ -49,6 +49,7 @@ extern int flist_eof;
540 extern int file_old_total;
541 extern int keep_dirlinks;
542 extern int make_backups;
543 +extern char *link_by_hash_dir;
544 extern struct file_list *cur_flist, *first_flist, *dir_flist;
545 extern struct chmod_mode_struct *daemon_chmod_modes;
547 @@ -644,8 +645,15 @@ int finish_transfer(const char *fname, const char *fnametmp,
548 /* move tmp file over real file */
549 if (DEBUG_GTE(RECV, 1))
550 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
551 - ret = robust_rename(fnametmp, fname, temp_copy_name,
552 - file->mode & INITACCESSPERMS);
554 + if (link_by_hash_dir)
555 + ret = link_by_hash(fnametmp, fname, file);
559 + ret = robust_rename(fnametmp, fname, temp_copy_name,
560 + file->mode & INITACCESSPERMS);
563 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
564 ret == -2 ? "copy" : "rename",
565 diff --git a/rsync.h b/rsync.h
568 @@ -869,6 +869,14 @@ struct stats {
572 +struct hashfile_struct {
573 + struct hashfile_struct *next;
574 + struct hashfile_struct *prev;
580 struct chmod_mode_struct;
582 struct flist_ndx_item {
583 diff --git a/rsync.yo b/rsync.yo
586 @@ -400,6 +400,7 @@ to the detailed description below for a complete description. verb(
587 --compare-dest=DIR also compare received files relative to DIR
588 --copy-dest=DIR ... and include copies of unchanged files
589 --link-dest=DIR hardlink to files in DIR when unchanged
590 + --link-by-hash=DIR create hardlinks by hash into DIR
591 -z, --compress compress file data during the transfer
592 --compress-level=NUM explicitly set compression level
593 --skip-compress=LIST skip compressing files with suffix in LIST