Use "use warnings" rather than -w on the #! line.
[rsync/rsync-patches.git] / link-by-hash.diff
CommitLineData
03019e41 1Jason M. Felice wrote:
2eb075b2
WD
2
3This patch adds the --link-by-hash=DIR option, which hard links received
4files in a link farm arranged by MD4 file hash. The result is that the system
5will only store one copy of the unique contents of each file, regardless of
6the file's name.
7
03019e41
WD
8To use this patch, run these commands for a successful build:
9
10 patch -p1 <patches/link-by-hash.diff
11 ./prepare-source
12 ./configure
13 make
2eb075b2 14
cc3e685d
WD
15diff --git a/Makefile.in b/Makefile.in
16--- a/Makefile.in
17+++ b/Makefile.in
18@@ -36,7 +36,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
fc068916 19 util.o main.o checksum.o match.o syscall.o log.o backup.o
790ba11a 20 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
5795bf59 21 fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
8a529471
WD
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
cc3e685d
WD
27diff --git a/flist.c b/flist.c
28--- a/flist.c
29+++ b/flist.c
ae306a29 30@@ -71,6 +71,7 @@ extern int sender_symlink_iconv;
d4dd2dd5 31 extern int unsort_ndx;
96148342 32 extern struct stats stats;
790ba11a 33 extern char *filesfrom_host;
96148342 34+extern char *link_by_hash_dir;
d4dd2dd5
WD
35
36 extern char curr_dir[MAXPATHLEN];
37
ae306a29 38@@ -844,7 +845,7 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
c0c7984e 39 extra_len += EXTRA_LEN;
96148342
WD
40 #endif
41
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;
45
7f0bf1cb 46 #if SIZEOF_INT64 >= 8
cc3e685d
WD
47diff --git a/hashlink.c b/hashlink.c
48new file mode 100644
49--- /dev/null
50+++ b/hashlink.c
96148342 51@@ -0,0 +1,336 @@
c57f4101
WD
52+/*
53+ Copyright (C) Cronosys, LLC 2004
54+
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.
59+
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.
64+
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.
68+*/
69+
70+/* This file contains code used by the --link-by-hash option. */
71+
72+#include "rsync.h"
73+
74+extern char *link_by_hash_dir;
75+
96148342 76+#ifdef HAVE_LINK
c57f4101 77+
96148342 78+char *make_hash_name(struct file_struct *file)
c57f4101
WD
79+{
80+ char hash[33], *dst;
96148342 81+ uchar c, *src = (uchar*)F_SUM(file);
c57f4101
WD
82+ int i;
83+
c57f4101
WD
84+ for (dst = hash, i = 0; i < 4; i++, src++) {
85+ c = *src >> 4;
86+ *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
87+ c = *src & 0x0f;
88+ *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
89+ }
90+ *dst++ = '/';
91+ for (i = 0; i < 12; i++, src++) {
92+ c = *src >> 4;
93+ *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
94+ c = *src & 0x0f;
95+ *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
96+ }
97+ *dst = 0;
98+
99+ asprintf(&dst,"%s/%s",link_by_hash_dir,hash);
100+ return dst;
101+}
102+
103+
104+void kill_hashfile(struct hashfile_struct *hashfile)
105+{
106+ if (!hashfile)
107+ return;
108+ free(hashfile->name);
109+ close(hashfile->fd);
110+ free(hashfile);
111+}
112+
113+
114+void kill_hashfiles(struct hashfile_struct *hashfiles)
115+{
116+ struct hashfile_struct *iter, *next;
117+ if ((iter = hashfiles) != NULL) {
118+ do {
119+ next = iter->next;
120+ kill_hashfile(iter);
121+ iter = next;
122+ } while (iter != hashfiles);
123+ }
124+}
125+
126+
127+struct hashfile_struct *find_hashfiles(char *hashname, int64 size, long *fnbr)
128+{
129+ DIR *d;
130+ struct dirent *di;
131+ struct hashfile_struct *hashfiles = NULL, *hashfile;
132+ STRUCT_STAT st;
133+ long this_fnbr;
134+
135+ *fnbr = 0;
47841496 136+
c57f4101
WD
137+ /* Build a list of potential candidates and open
138+ * them. */
139+ if ((d = opendir(hashname)) == NULL) {
d0320a46 140+ rsyserr(FERROR, errno, "opendir failed: \"%s\"", hashname);
c57f4101
WD
141+ free(hashname);
142+ return NULL;
143+ }
144+ while ((di = readdir(d)) != NULL) {
145+ if (!strcmp(di->d_name,".") || !strcmp(di->d_name,"..")) {
146+ continue;
147+ }
148+
149+ /* We need to have the largest fnbr in case we need to store
150+ * a new file. */
151+ this_fnbr = atol(di->d_name);
152+ if (this_fnbr > *fnbr)
153+ *fnbr = this_fnbr;
154+
39cc637d 155+ hashfile = new_array(struct hashfile_struct, 1);
c57f4101
WD
156+ asprintf(&hashfile->name,"%s/%s",hashname,
157+ di->d_name);
158+ if (do_stat(hashfile->name,&st) == -1) {
d0320a46 159+ rsyserr(FERROR, errno, "stat failed: %s", hashfile->name);
c57f4101
WD
160+ kill_hashfile(hashfile);
161+ continue;
162+ }
163+ if (st.st_size != size) {
164+ kill_hashfile(hashfile);
165+ continue;
166+ }
167+ hashfile->nlink = st.st_nlink;
168+ hashfile->fd = open(hashfile->name,O_RDONLY|O_BINARY);
169+ if (hashfile->fd == -1) {
d0320a46 170+ rsyserr(FERROR, errno, "open failed: %s", hashfile->name);
c57f4101
WD
171+ kill_hashfile(hashfile);
172+ continue;
173+ }
174+ if (hashfiles == NULL)
175+ hashfiles = hashfile->next = hashfile->prev = hashfile;
176+ else {
177+ hashfile->next = hashfiles;
178+ hashfile->prev = hashfiles->prev;
179+ hashfile->next->prev = hashfile;
180+ hashfile->prev->next = hashfile;
181+ }
182+ }
183+ closedir(d);
184+
185+ return hashfiles;
186+}
187+
188+
189+struct hashfile_struct *compare_hashfiles(int fd,struct hashfile_struct *files)
190+{
191+ int amt, hamt;
192+ char buffer[BUFSIZ], cmpbuffer[BUFSIZ];
193+ struct hashfile_struct *iter, *next, *best;
194+ uint32 nlink;
195+
196+ if (!files)
197+ return NULL;
198+
199+ iter = files; /* in case files are 0 bytes */
200+ while ((amt = read(fd, buffer, BUFSIZ)) > 0) {
201+ iter = files;
202+ do {
203+ /* Icky bit to resync when we steal the first node. */
204+ if (!files)
205+ files = iter;
206+
207+ next = iter->next;
208+
209+ hamt = read(iter->fd, cmpbuffer, BUFSIZ);
210+ if (amt != hamt || memcmp(buffer, cmpbuffer, amt)) {
211+ if (iter == files) {
212+ files = files->prev;
213+ }
214+ if (iter->next == iter) {
215+ files = next = NULL;
216+ } else {
217+ next = iter->next;
218+ if (iter == files) {
219+ /* So we know to resync */
220+ files = NULL;
221+ }
222+ }
223+ iter->next->prev = iter->prev;
224+ iter->prev->next = iter->next;
225+ kill_hashfile(iter);
226+ }
227+
228+ iter = next;
229+ } while (iter != files);
230+
231+ if (iter == NULL && files == NULL) {
232+ /* There are no matches. */
233+ return NULL;
234+ }
c57f4101
WD
235+ }
236+
237+ if (amt == -1) {
d0320a46 238+ rsyserr(FERROR, errno, "read failed in compare_hashfiles()");
c57f4101
WD
239+ kill_hashfiles(files);
240+ return NULL;
241+ }
242+
243+ /* If we only have one file left, use it. */
244+ if (files == files->next) {
245+ return files;
246+ }
247+
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;
254+ do {
255+ if (iter->nlink < nlink) {
256+ nlink = iter->nlink;
257+ best = iter;
258+ }
259+ iter = iter->next;
260+ } while (iter != files);
261+
262+ best->next->prev = best->prev;
263+ best->prev->next = best->next;
264+ if (files == best)
265+ files = files->next;
266+ kill_hashfiles(files);
267+ return best;
268+}
269+
270+
7bfcb297 271+int link_by_hash(const char *fnametmp, const char *fname, struct file_struct *file)
c57f4101
WD
272+{
273+ STRUCT_STAT st;
47841496 274+ char *hashname = make_hash_name(file);
c57f4101
WD
275+ int first = 0, rc;
276+ char *linkname;
277+ long last_fnbr;
278+
a3044834 279+ if (F_LENGTH(file) == 0)
4c38ad2a 280+ return robust_rename(fnametmp, fname, NULL, 0644);
c57f4101
WD
281+
282+ if (do_stat(hashname, &st) == -1) {
283+ char *dirname;
284+
285+ /* Directory does not exist. */
286+ dirname = strdup(hashname);
287+ *strrchr(dirname,'/') = 0;
288+ if (do_mkdir(dirname, 0755) == -1 && errno != EEXIST) {
d0320a46 289+ rsyserr(FERROR, errno, "mkdir failed: %s", dirname);
c57f4101
WD
290+ free(hashname);
291+ free(dirname);
4c38ad2a 292+ return robust_rename(fnametmp, fname, NULL, 0644);
c57f4101
WD
293+ }
294+ free(dirname);
295+
296+ if (do_mkdir(hashname, 0755) == -1 && errno != EEXIST) {
d0320a46 297+ rsyserr(FERROR, errno, "mkdir failed: %s", hashname);
c57f4101 298+ free(hashname);
4c38ad2a 299+ return robust_rename(fnametmp, fname, NULL, 0644);
c57f4101
WD
300+ }
301+
302+ first = 1;
303+ asprintf(&linkname,"%s/0",hashname);
304+ rprintf(FINFO, "(1) linkname = %s\n", linkname);
c57f4101
WD
305+ } else {
306+ struct hashfile_struct *hashfiles, *hashfile;
c57f4101
WD
307+
308+ if (do_stat(fnametmp,&st) == -1) {
d0320a46 309+ rsyserr(FERROR, errno, "stat failed: %s", fname);
c57f4101
WD
310+ return -1;
311+ }
312+ hashfiles = find_hashfiles(hashname, st.st_size, &last_fnbr);
313+
314+ if (hashfiles == NULL) {
315+ first = 1;
316+ asprintf(&linkname,"%s/0",hashname);
317+ rprintf(FINFO, "(2) linkname = %s\n", linkname);
318+ } else {
47841496 319+ int fd;
c57f4101
WD
320+ /* Search for one identical to us. */
321+ if ((fd = open(fnametmp,O_RDONLY|O_BINARY)) == -1) {
d0320a46 322+ rsyserr(FERROR, errno, "open failed: %s", fnametmp);
c57f4101
WD
323+ kill_hashfiles(hashfiles);
324+ return -1;
325+ }
326+ hashfile = compare_hashfiles(fd, hashfiles);
327+ hashfiles = NULL;
47841496 328+ close(fd);
c57f4101
WD
329+
330+ if (hashfile) {
331+ first = 0;
332+ linkname = strdup(hashfile->name);
333+ rprintf(FINFO, "(3) linkname = %s\n", linkname);
334+ kill_hashfile(hashfile);
335+ } else {
336+ first = 1;
337+ asprintf(&linkname, "%s/%ld", hashname,
338+ last_fnbr + 1);
339+ rprintf(FINFO, "(4) linkname = %s\n", linkname);
340+ }
341+ }
342+ }
343+
344+ if (!first) {
345+ rprintf(FINFO, "link-by-hash (existing): \"%s\" -> %s\n",
346+ linkname, full_fname(fname));
cad12f62 347+ robust_unlink(fname);
c57f4101
WD
348+ rc = do_link(linkname, fname);
349+ if (rc == -1) {
350+ if (errno == EMLINK) {
351+ first = 1;
352+ free(linkname);
353+ asprintf(&linkname,"%s/%ld",hashname,
354+ last_fnbr + 1);
355+ rprintf(FINFO, "(5) linkname = %s\n", linkname);
356+ rprintf(FINFO,"link-by-hash: max link count exceeded, starting new file \"%s\".\n", linkname);
357+ } else {
fe6407b5
WD
358+ rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
359+ linkname, full_fname(fname));
4c38ad2a 360+ rc = robust_rename(fnametmp, fname, NULL, 0644);
c57f4101
WD
361+ }
362+ } else {
363+ do_unlink(fnametmp);
364+ }
365+ }
366+
367+ if (first) {
368+ rprintf(FINFO, "link-by-hash (new): %s -> \"%s\"\n",
369+ full_fname(fname),linkname);
370+
4c38ad2a 371+ rc = robust_rename(fnametmp, fname, NULL, 0644);
c57f4101 372+ if (rc != 0) {
fe6407b5
WD
373+ rsyserr(FERROR, errno, "rename \"%s\" -> \"%s\"",
374+ full_fname(fnametmp), full_fname(fname));
c57f4101
WD
375+ }
376+ rc = do_link(fname,linkname);
377+ if (rc != 0) {
fe6407b5
WD
378+ rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"",
379+ full_fname(fname), linkname);
c57f4101
WD
380+ }
381+ }
382+
383+ free(linkname);
384+ free(hashname);
385+ return rc;
386+}
c57f4101 387+#endif
cc3e685d
WD
388diff --git a/options.c b/options.c
389--- a/options.c
390+++ b/options.c
c0c7984e 391@@ -157,6 +157,7 @@ char *backup_suffix = NULL;
e0e47893
WD
392 char *tmpdir = NULL;
393 char *partial_dir = NULL;
394 char *basis_dir[MAX_BASIS_DIRS+1];
c57f4101 395+char *link_by_hash_dir = NULL;
e0e47893
WD
396 char *config_file = NULL;
397 char *shell_cmd = NULL;
ff318e90 398 char *logfile_name = NULL;
abd3adb8 399@@ -395,6 +396,7 @@ void usage(enum logcode F)
c57f4101 400 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
6ba1be7d 401 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
0808daa5 402 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
b78a6aba
WD
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");
610969d1 405 rprintf(F," --compress-level=NUM explicitly set compression level\n");
5ba66156 406 rprintf(F," --skip-compress=LIST skip compressing files with a suffix in LIST\n");
abd3adb8 407@@ -447,7 +449,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
93ca4d27 408 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
0ca6aebe 409 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
5398d042 410 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
85096e5e
WD
411- OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
412+ OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_LINK_BY_HASH,
0ca6aebe 413 OPT_SERVER, OPT_REFUSED_BASE = 9000};
c57f4101 414
70c5d149 415 static struct poptOption long_options[] = {
abd3adb8 416@@ -579,6 +581,7 @@ static struct poptOption long_options[] = {
0808daa5 417 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
6ba1be7d 418 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
0808daa5 419 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
5388f859 420+ {"link-by-hash", 0, POPT_ARG_STRING, 0, OPT_LINK_BY_HASH, 0, 0},
c0c7984e
WD
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 },
abd3adb8 424@@ -1261,6 +1264,21 @@ int parse_arguments(int *argc_p, const char ***argv_p)
5795bf59 425 return 0;
ffc18846
WD
426 #endif
427
c57f4101 428+ case OPT_LINK_BY_HASH:
96148342 429+#ifdef HAVE_LINK
d0320a46
WD
430+ arg = poptGetOptArg(pc);
431+ if (sanitize_paths)
c0c7984e 432+ arg = sanitize_path(NULL, arg, NULL, 0, SP_DEFAULT);
d0320a46 433+ link_by_hash_dir = (char *)arg;
c57f4101
WD
434+ break;
435+#else
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);
440+ return 0;
441+#endif
442+
443 default:
444 /* A large opt value means that set_refuse_options()
27a7053c 445 * turned this option off. */
abd3adb8 446@@ -2038,6 +2056,11 @@ void server_options(char **args, int *argc_p)
5ba66156
WD
447 } else if (inplace)
448 args[ac++] = "--inplace";
7b675ff5 449
c57f4101
WD
450+ if (link_by_hash_dir && am_sender) {
451+ args[ac++] = "--link-by-hash";
452+ args[ac++] = link_by_hash_dir;
7b675ff5
WD
453+ }
454+
def2ace9
WD
455 if (files_from && (!am_sender || filesfrom_host)) {
456 if (filesfrom_host) {
7b675ff5 457 args[ac++] = "--files-from";
cc3e685d
WD
458diff --git a/receiver.c b/receiver.c
459--- a/receiver.c
460+++ b/receiver.c
963ca808 461@@ -163,12 +163,14 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
99397a25 462 }
c57f4101 463
dc3ae351 464 static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
87a38eea 465- const char *fname, int fd, OFF_T total_size)
a3044834
WD
466+ const char *fname, int fd, OFF_T total_size,
467+ const char *md4)
c57f4101 468 {
87a38eea
WD
469 static char file_sum1[MAX_DIGEST_LEN];
470 static char file_sum2[MAX_DIGEST_LEN];
dc3ae351 471 struct map_struct *mapbuf;
c57f4101 472 struct sum_struct sum;
87a38eea
WD
473+ md_context mdfour_data;
474 int32 len, sum_len;
c57f4101
WD
475 OFF_T offset = 0;
476 OFF_T offset2;
963ca808 477@@ -188,6 +190,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
dc3ae351
WD
478 } else
479 mapbuf = NULL;
7b675ff5 480
c57f4101
WD
481+ if (md4)
482+ mdfour_begin(&mdfour_data);
7b675ff5
WD
483+
484 sum_init(checksum_seed);
c57f4101 485
fc068916 486 if (append_mode > 0) {
963ca808 487@@ -232,6 +237,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
c57f4101
WD
488 cleanup_got_literal = 1;
489
2c2d83dc 490 sum_update(data, i);
c57f4101 491+ if (md4)
4c38ad2a 492+ mdfour_update(&mdfour_data, (uchar*)data, i);
c57f4101 493
afbebe13
WD
494 if (fd != -1 && write_file(fd,data,i) != i)
495 goto report_write_error;
963ca808 496@@ -258,6 +265,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
c57f4101
WD
497
498 see_token(map, len);
2c2d83dc 499 sum_update(map, len);
c57f4101 500+ if (md4)
4c38ad2a 501+ mdfour_update(&mdfour_data, (uchar*)map, len);
c57f4101
WD
502 }
503
d4dd2dd5 504 if (updating_basis_or_equiv) {
abd3adb8 505@@ -303,6 +312,8 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
c57f4101
WD
506 }
507
87a38eea 508 sum_len = sum_end(file_sum1);
c57f4101 509+ if (md4)
87a38eea 510+ mdfour_result(&mdfour_data, (uchar*)md4);
c57f4101 511
dc3ae351
WD
512 if (mapbuf)
513 unmap_file(mapbuf);
abd3adb8 514@@ -318,7 +329,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
5823d322
WD
515
516 static void discard_receive_data(int f_in, OFF_T length)
517 {
dc3ae351
WD
518- receive_data(f_in, NULL, -1, 0, NULL, -1, length);
519+ receive_data(f_in, NULL, -1, 0, NULL, -1, length, NULL);
5823d322
WD
520 }
521
fc068916 522 static void handle_delayed_updates(char *local_name)
abd3adb8 523@@ -708,7 +719,7 @@ int recv_files(int f_in, char *local_name)
a3044834
WD
524
525 /* recv file data */
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));
529
530 log_item(log_code, file, &initial_stats, iflags, NULL);
531
cc3e685d
WD
532diff --git a/rsync.c b/rsync.c
533--- a/rsync.c
534+++ b/rsync.c
4c107044
WD
535@@ -48,6 +48,7 @@ extern int inplace;
536 extern int flist_eof;
537 extern int keep_dirlinks;
c57f4101 538 extern int make_backups;
c968d24c 539+extern char *link_by_hash_dir;
fc068916 540 extern struct file_list *cur_flist, *first_flist, *dir_flist;
c769ea2c 541 extern struct chmod_mode_struct *daemon_chmod_modes;
5ba66156 542 #ifdef ICONV_OPTION
963ca808 543@@ -578,8 +579,15 @@ int finish_transfer(const char *fname, const char *fnametmp,
93ca4d27
WD
544 /* move tmp file over real file */
545 if (verbose > 2)
546 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
4c15e800 547- ret = robust_rename(fnametmp, fname, temp_copy_name,
4a65fe72 548- file->mode & INITACCESSPERMS);
96148342 549+#ifdef HAVE_LINK
2eb075b2 550+ if (link_by_hash_dir)
7b675ff5 551+ ret = link_by_hash(fnametmp, fname, file);
2eb075b2 552+ else
c57f4101 553+#endif
4a65fe72 554+ {
4c15e800 555+ ret = robust_rename(fnametmp, fname, temp_copy_name,
4a65fe72
WD
556+ file->mode & INITACCESSPERMS);
557+ }
54691942 558 if (ret < 0) {
cc3e685d 559 rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
93ca4d27 560 ret == -2 ? "copy" : "rename",
cc3e685d
WD
561diff --git a/rsync.h b/rsync.h
562--- a/rsync.h
563+++ b/rsync.h
ae306a29 564@@ -830,6 +830,14 @@ struct stats {
f2863bc0 565 int num_transferred_files;
c57f4101
WD
566 };
567
568+struct hashfile_struct {
569+ struct hashfile_struct *next;
570+ struct hashfile_struct *prev;
571+ char *name;
572+ int fd;
573+ uint32 nlink;
574+};
575+
4a65fe72 576 struct chmod_mode_struct;
c57f4101 577
963ca808 578 struct flist_ndx_item {
cc3e685d
WD
579diff --git a/rsync.yo b/rsync.yo
580--- a/rsync.yo
581+++ b/rsync.yo
abd3adb8 582@@ -392,6 +392,7 @@ to the detailed description below for a complete description. verb(
79f132a1 583 --compare-dest=DIR also compare received files relative to DIR
6ba1be7d 584 --copy-dest=DIR ... and include copies of unchanged files
79f132a1 585 --link-dest=DIR hardlink to files in DIR when unchanged
b78a6aba
WD
586+ --link-by-hash=DIR create hardlinks by hash into DIR
587 -z, --compress compress file data during the transfer
610969d1 588 --compress-level=NUM explicitly set compression level
5ba66156 589 --skip-compress=LIST skip compressing files with suffix in LIST