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