3 This patch adds the --link-by-hash=DIR option, which hard links received files
4 in a link farm arranged by MD4 or MD5 file hash. The result is that the system
5 will only store one copy of the unique contents of each file, regardless of the
8 To use this patch, run these commands for a successful build:
10 patch -p1 <patches/link-by-hash.diff
15 based-on: 5b19cf787515fc2388dd070a85e86ced5d80510b
16 diff --git a/Makefile.in b/Makefile.in
19 @@ -45,7 +45,7 @@ OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
20 util.o util2.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/checksum.c b/checksum.c
31 @@ -36,6 +36,8 @@ extern int whole_file;
32 extern int checksum_seed;
33 extern int protocol_version;
34 extern int proper_seed_order;
35 +extern char *link_by_hash_dir;
36 +extern char link_by_hash_extra_sum[MAX_DIGEST_LEN];
37 extern const char *checksum_choice;
39 struct name_num_obj valid_checksums = {
40 @@ -385,7 +387,7 @@ static union {
47 static XXH64_state_t* xxh64_state;
49 @@ -409,6 +411,8 @@ void sum_init(int csum_type, int seed)
53 + if (link_by_hash_dir)
58 @@ -451,6 +455,8 @@ void sum_update(const char *p, int32 len)
61 MD5_Update(&ctx.m5, (uchar *)p, len);
62 + if (link_by_hash_dir)
63 + MD5_Update(&ctx2.m5, (uchar *)p, len);
67 @@ -505,6 +511,8 @@ int sum_end(char *sum)
70 MD5_Final((uchar *)sum, &ctx.m5);
71 + if (link_by_hash_dir)
72 + MD5_Final((uchar *)link_by_hash_extra_sum, &ctx2.m5);
76 diff --git a/clientserver.c b/clientserver.c
79 @@ -51,6 +51,7 @@ extern int logfile_format_has_i;
80 extern int logfile_format_has_o_or_i;
81 extern char *bind_address;
82 extern char *config_file;
83 +extern char *link_by_hash_dir;
84 extern char *logfile_format;
85 extern char *files_from;
87 @@ -545,6 +546,9 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
91 + if (*lp_link_by_hash_dir(i))
92 + link_by_hash_dir = lp_link_by_hash_dir(i);
95 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
97 diff --git a/hashlink.c b/hashlink.c
103 + Copyright (C) Cronosys, LLC 2004
105 + This program is free software; you can redistribute it and/or modify
106 + it under the terms of the GNU General Public License as published by
107 + the Free Software Foundation; either version 2 of the License, or
108 + (at your option) any later version.
110 + This program is distributed in the hope that it will be useful,
111 + but WITHOUT ANY WARRANTY; without even the implied warranty of
112 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
113 + GNU General Public License for more details.
115 + You should have received a copy of the GNU General Public License
116 + along with this program; if not, write to the Free Software
117 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
120 +/* This file contains code used by the --link-by-hash option. */
125 +extern int protocol_version;
126 +extern char *link_by_hash_dir;
127 +extern char sender_file_sum[MAX_DIGEST_LEN];
129 +char link_by_hash_extra_sum[MAX_DIGEST_LEN]; /* Only used when md4 sums are in the transfer */
133 +/* This function is always called after a file is received, so the
134 + * sender_file_sum buffer has whatever the last checksum was for the
135 + * transferred file. */
136 +void link_by_hash(const char *fname, const char *fnametmp, struct file_struct *file)
139 + char *hashname, *last_slash, *num_str;
143 + /* We don't bother to hard-link 0-length files. */
144 + if (F_LENGTH(file) == 0)
147 + hex = sum_as_hex(5, protocol_version >= 30 ? sender_file_sum : link_by_hash_extra_sum, 0);
148 + if (asprintf(&hashname, "%s/%.3s/%.3s/%.3s/%s.%s.000000",
149 + link_by_hash_dir, hex, hex+3, hex+6, hex+9, big_num(F_LENGTH(file))) < 0)
151 + out_of_memory("make_hash_name");
154 + last_slash = strrchr(hashname, '/');
155 + num_str = strrchr(last_slash, '.') + 1;
158 + if (num >= 999999) { /* Surely we'll never reach this... */
159 + if (DEBUG_GTE(HASHLINK, 1))
160 + rprintf(FINFO, "link-by-hash: giving up after \"%s\".\n", hashname);
163 + if (num > 0 && DEBUG_GTE(HASHLINK, 1))
164 + rprintf(FINFO, "link-by-hash: max link count exceeded, starting new file \"%s\".\n", hashname);
166 + snprintf(num_str, 7, "%d", num++);
167 + if (do_stat(hashname, &st) < 0)
170 + if (do_link(hashname, fnametmp) < 0) {
171 + if (errno == EMLINK)
173 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"", hashname, full_fname(fname));
175 + if (DEBUG_GTE(HASHLINK, 2))
176 + rprintf(FINFO, "link-by-hash (existing): \"%s\" -> %s\n", hashname, full_fname(fname));
177 + robust_rename(fnametmp, fname, NULL, 0644);
183 + if (DEBUG_GTE(HASHLINK, 2))
184 + rprintf(FINFO, "link-by-hash (new): %s -> \"%s\"\n", full_fname(fname), hashname);
186 + if (do_link(fname, hashname) < 0
187 + && (errno != ENOENT || make_path(hashname, MKP_DROP_NAME) < 0 || do_link(fname, hashname) < 0))
188 + rsyserr(FERROR, errno, "link \"%s\" -> \"%s\"", full_fname(fname), hashname);
194 diff --git a/loadparm.c b/loadparm.c
197 @@ -131,6 +131,7 @@ typedef struct {
200 char *incoming_chmod;
201 + char *link_by_hash_dir;
205 @@ -159,6 +160,7 @@ typedef struct {
207 BOOL include_from_EXP;
208 BOOL incoming_chmod_EXP;
209 + BOOL link_by_hash_dir_EXP;
213 @@ -245,6 +247,7 @@ static const all_vars Defaults = {
215 /* include_from; */ NULL,
216 /* incoming_chmod; */ NULL,
217 + /* link_by_hash_dir; */ NULL,
218 /* lock_file; */ DEFAULT_LOCK_FILE,
219 /* log_file; */ NULL,
220 /* log_format; */ "%o %h [%a] %m (%u) %f %l",
221 @@ -272,6 +275,7 @@ static const all_vars Defaults = {
222 /* include_EXP; */ False,
223 /* include_from_EXP; */ False,
224 /* incoming_chmod_EXP; */ False,
225 + /* link_by_hash_dir_EXP; */ False,
226 /* lock_file_EXP; */ False,
227 /* log_file_EXP; */ False,
228 /* log_format_EXP; */ False,
229 @@ -417,6 +421,7 @@ static struct parm_struct parm_table[] =
230 {"include from", P_STRING, P_LOCAL, &Vars.l.include_from, NULL,0},
231 {"include", P_STRING, P_LOCAL, &Vars.l.include, NULL,0},
232 {"incoming chmod", P_STRING, P_LOCAL, &Vars.l.incoming_chmod, NULL,0},
233 + {"link by hash dir", P_STRING, P_LOCAL, &Vars.l.link_by_hash_dir, NULL,0},
234 {"list", P_BOOL, P_LOCAL, &Vars.l.list, NULL,0},
235 {"lock file", P_STRING, P_LOCAL, &Vars.l.lock_file, NULL,0},
236 {"log file", P_STRING, P_LOCAL, &Vars.l.log_file, NULL,0},
237 @@ -552,6 +557,7 @@ FN_LOCAL_STRING(lp_hosts_deny, hosts_deny)
238 FN_LOCAL_STRING(lp_include, include)
239 FN_LOCAL_STRING(lp_include_from, include_from)
240 FN_LOCAL_STRING(lp_incoming_chmod, incoming_chmod)
241 +FN_LOCAL_STRING(lp_link_by_hash_dir, link_by_hash_dir)
242 FN_LOCAL_STRING(lp_lock_file, lock_file)
243 FN_LOCAL_STRING(lp_log_file, log_file)
244 FN_LOCAL_STRING(lp_log_format, log_format)
245 diff --git a/options.c b/options.c
248 @@ -164,6 +164,7 @@ char *backup_suffix = NULL;
250 char *partial_dir = NULL;
251 char *basis_dir[MAX_BASIS_DIRS+1];
252 +char *link_by_hash_dir = NULL;
253 char *config_file = NULL;
254 char *shell_cmd = NULL;
255 char *logfile_name = NULL;
256 @@ -215,7 +216,7 @@ static const char *debug_verbosity[] = {
257 /*2*/ "BIND,CMD,CONNECT,DEL,DELTASUM,DUP,FILTER,FLIST,ICONV",
258 /*3*/ "ACL,BACKUP,CONNECT2,DELTASUM2,DEL2,EXIT,FILTER2,FLIST2,FUZZY,GENR,OWN,RECV,SEND,TIME",
259 /*4*/ "CMD2,DELTASUM3,DEL3,EXIT2,FLIST3,ICONV2,OWN2,PROTO,TIME2",
260 - /*5*/ "CHDIR,DELTASUM4,FLIST4,FUZZY2,HASH,HLINK",
261 + /*5*/ "CHDIR,DELTASUM4,FLIST4,FUZZY2,HASH,HASHLINK,HLINK",
264 #define MAX_VERBOSITY ((int)(sizeof debug_verbosity / sizeof debug_verbosity[0]) - 1)
265 @@ -285,6 +286,7 @@ static struct output_struct debug_words[COUNT_DEBUG+1] = {
266 DEBUG_WORD(FUZZY, W_REC, "Debug fuzzy scoring (levels 1-2)"),
267 DEBUG_WORD(GENR, W_REC, "Debug generator functions"),
268 DEBUG_WORD(HASH, W_SND|W_REC, "Debug hashtable code"),
269 + DEBUG_WORD(HASHLINK, W_REC, "Debug hashlink code (levels 1-2)"),
270 DEBUG_WORD(HLINK, W_SND|W_REC, "Debug hard-link actions (levels 1-3)"),
271 DEBUG_WORD(ICONV, W_CLI|W_SRV, "Debug iconv character conversions (levels 1-2)"),
272 DEBUG_WORD(IO, W_CLI|W_SRV, "Debug I/O routines (levels 1-4)"),
273 @@ -829,6 +831,7 @@ void usage(enum logcode F)
274 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
275 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
276 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
277 + rprintf(F," --link-by-hash=DIR create hardlinks by hash into DIR\n");
278 rprintf(F," -z, --compress compress file data during the transfer\n");
279 rprintf(F," --compress-level=NUM explicitly set compression level\n");
280 rprintf(F," --skip-compress=LIST skip compressing files with a suffix in LIST\n");
281 @@ -886,7 +889,7 @@ enum {OPT_SERVER = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
282 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
283 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
284 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
285 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
286 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG, OPT_LINK_BY_HASH,
287 OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT,
288 OPT_OLD_COMPRESS, OPT_NEW_COMPRESS, OPT_NO_COMPRESS,
289 OPT_REFUSED_BASE = 9000};
290 @@ -1041,6 +1044,7 @@ static struct poptOption long_options[] = {
291 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
292 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
293 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
294 + {"link-by-hash", 0, POPT_ARG_STRING, 0, OPT_LINK_BY_HASH, 0, 0},
295 {"fuzzy", 'y', POPT_ARG_NONE, 0, 'y', 0, 0 },
296 {"no-fuzzy", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
297 {"no-y", 0, POPT_ARG_VAL, &fuzzy_basis, 0, 0, 0 },
298 @@ -1475,6 +1479,9 @@ int parse_arguments(int *argc_p, const char ***argv_p)
299 iconv_opt = strdup(arg);
302 + if (*lp_link_by_hash_dir(module_id))
303 + set_refuse_options("link-by-hash");
305 /* TODO: Call poptReadDefaultConfig; handle errors. */
307 /* The context leaks in case of an error, but if there's a
308 @@ -1971,6 +1978,21 @@ int parse_arguments(int *argc_p, const char ***argv_p)
312 + case OPT_LINK_BY_HASH:
314 + arg = poptGetOptArg(pc);
315 + if (sanitize_paths)
316 + arg = sanitize_path(NULL, arg, NULL, 0, SP_DEFAULT);
317 + link_by_hash_dir = (char *)arg;
320 + snprintf(err_buf, sizeof err_buf,
321 + "hard links are not supported on this %s\n",
322 + am_server ? "server" : "client");
323 + rprintf(FERROR, "ERROR: %s", err_buf);
328 /* A large opt value means that set_refuse_options()
329 * turned this option off. */
330 @@ -2281,6 +2303,8 @@ int parse_arguments(int *argc_p, const char ***argv_p)
331 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
333 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
334 + if (link_by_hash_dir)
335 + link_by_hash_dir = sanitize_path(NULL, link_by_hash_dir, NULL, 0, SP_DEFAULT);
337 if (daemon_filter_list.head && !am_sender) {
338 filter_rule_list *elp = &daemon_filter_list;
339 @@ -2956,6 +2980,12 @@ void server_options(char **args, int *argc_p)
341 args[ac++] = "--inplace";
343 + if (link_by_hash_dir && am_sender) {
344 + args[ac++] = "--link-by-hash";
345 + args[ac++] = link_by_hash_dir;
346 + link_by_hash_dir = NULL; /* optimize sending-side checksums */
349 if (files_from && (!am_sender || filesfrom_host)) {
350 if (filesfrom_host) {
351 args[ac++] = "--files-from";
352 diff --git a/rsync.1.md b/rsync.1.md
355 @@ -418,6 +418,7 @@ detailed description below for a complete description.
356 --compare-dest=DIR also compare received files relative to DIR
357 --copy-dest=DIR ... and include copies of unchanged files
358 --link-dest=DIR hardlink to files in DIR when unchanged
359 +--link-by-hash=DIR create hardlinks by hash into DIR
360 --compress, -z compress file data during the transfer
361 --compress-level=NUM explicitly set compression level
362 --skip-compress=LIST skip compressing files with suffix in LIST
363 @@ -2234,6 +2235,50 @@ your home directory (remove the '=' for that).
364 specified (or implied by `-a`). You can work-around this bug by avoiding
365 the `-o` option when sending to an old rsync.
367 +0. `--link-by-hash=DIR`
369 + This option hard links the destination files into `DIR`, a link farm
370 + arranged by MD5 file hash. The result is that the system will only store
371 + (usually) one copy of the unique contents of each file, regardless of the
372 + file's name (it will use extra files if the links overflow the available
375 + This patch does not take into account file permissions, extended
376 + attributes, or ACLs when linking things together, so you should only use
377 + this if you don't care about preserving those extra file attributes (or if
378 + they are always the same for identical files).
380 + The DIR is relative to the destination directory, so either specify a full
381 + path to the hash hierarchy, or specify a relative path that puts the links
382 + outside the destination (e.g. "../links").
384 + Keep in mind that the hierarchy is never pruned, so if you need to reclaim
385 + space, you should remove any files that have just one link (since they are
386 + not linked into any destination dirs anymore):
388 + > find $DIR -links 1 -delete
390 + The link farm's directory hierarchy is determined by the file's (32-char)
391 + MD5 hash and the file-length. The hash is split up into directory shards.
392 + For example, if a file is 54321 bytes long, it could be stored like this:
394 + > $DIR/123/456/789/01234567890123456789012.54321.0
396 + Note that the directory layout in this patch was modified for version
397 + 3.1.0, so anyone using an older version of this patch should move their
398 + existing link hierarchy out of the way and then use the newer rsync to copy
399 + the saved hierarchy into its new layout. Assuming that no files have
400 + overflowed their link limits, this would work:
403 + > rsync -aiv --link-by-hash=$DIR $DIR.old/ $DIR.tmp/
407 + If some of your files are at their link limit, you'd be better of using a
408 + script to calculate the md5 sum of each file in the hierarchy and move it
409 + to its new location.
411 0. `--compress`, `-z`
413 With this option, rsync compresses the file data as it is sent to the
414 diff --git a/rsync.c b/rsync.c
417 @@ -50,6 +50,7 @@ extern int flist_eof;
418 extern int file_old_total;
419 extern int keep_dirlinks;
420 extern int make_backups;
421 +extern char *link_by_hash_dir;
422 extern int sanitize_paths;
423 extern struct file_list *cur_flist, *first_flist, *dir_flist;
424 extern struct chmod_mode_struct *daemon_chmod_modes;
425 @@ -726,6 +727,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
428 /* The file was moved into place (not copied), so it's done. */
430 + if (link_by_hash_dir)
431 + link_by_hash(fname, fnametmp, file);
435 /* The file was copied, so tweak the perms of the copied file. If it
436 diff --git a/rsync.h b/rsync.h
439 @@ -1349,7 +1349,8 @@ extern short info_levels[], debug_levels[];
440 #define DEBUG_FUZZY (DEBUG_FLIST+1)
441 #define DEBUG_GENR (DEBUG_FUZZY+1)
442 #define DEBUG_HASH (DEBUG_GENR+1)
443 -#define DEBUG_HLINK (DEBUG_HASH+1)
444 +#define DEBUG_HASHLINK (DEBUG_HASH+1)
445 +#define DEBUG_HLINK (DEBUG_HASHLINK+1)
446 #define DEBUG_ICONV (DEBUG_HLINK+1)
447 #define DEBUG_IO (DEBUG_ICONV+1)
448 #define DEBUG_NSTR (DEBUG_IO+1)
449 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
450 --- a/rsyncd.conf.5.md
451 +++ b/rsyncd.conf.5.md
452 @@ -315,6 +315,23 @@ the values of parameters. See the GLOBAL PARAMETERS section for more details.
453 is 0, which means no limit. A negative value disables the module. See
454 also the "lock file" parameter.
456 +0. `link by hash dir`
458 + When the "link by hash dir" parameter is set to a non-empty string,
459 + received files will be hard linked into **DIR**, a link farm arranged by
460 + MD5 file hash. See the `--link-by-hash` option for a full explanation.
462 + The **DIR** must be accessible inside any chroot restrictions for the
463 + module, but can exist outside the transfer location if there is an
464 + inside-the-chroot path to the module (see "use chroot"). Note that a
465 + user-specified option does not allow this outside-the-transfer-area
468 + If this parameter is set, it will disable the `--link-by-hash` command-line
469 + option for copies into the module.
471 +The default is for this parameter to be unset.
475 When the "log file" parameter is set to a non-empty string, the rsync