1 --- hashlink.c 1969-12-31 19:00:00.000000000 -0500
2 +++ hashlink.c 2004-02-23 10:30:45.000000000 -0500
5 + Copyright (C) Cronosys, LLC 2004
7 + This program is free software; you can redistribute it and/or modify
8 + it under the terms of the GNU General Public License as published by
9 + the Free Software Foundation; either version 2 of the License, or
10 + (at your option) any later version.
12 + This program is distributed in the hope that it will be useful,
13 + but WITHOUT ANY WARRANTY; without even the implied warranty of
14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 + GNU General Public License for more details.
17 + You should have received a copy of the GNU General Public License
18 + along with this program; if not, write to the Free Software
19 + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 +/* This file contains code used by the --link-by-hash option. */
26 +extern char *link_by_hash_dir;
30 +char* make_hash_name(struct file_struct *file)
32 + char hash[33], *dst;
37 + src = (unsigned char*)file->u.sum;
38 + for (dst = hash, i = 0; i < 4; i++, src++) {
40 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
42 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
45 + for (i = 0; i < 12; i++, src++) {
47 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
49 + *(dst++) = (c >= 10) ? (c - 10 + 'a') : (c + '0');
53 + asprintf(&dst,"%s/%s",link_by_hash_dir,hash);
58 +void kill_hashfile(struct hashfile_struct *hashfile)
62 + free(hashfile->name);
63 + close(hashfile->fd);
68 +void kill_hashfiles(struct hashfile_struct *hashfiles)
70 + struct hashfile_struct *iter, *next;
71 + if ((iter = hashfiles) != NULL) {
74 + kill_hashfile(iter);
76 + } while (iter != hashfiles);
81 +struct hashfile_struct *find_hashfiles(char *hashname, int64 size, long *fnbr)
85 + struct hashfile_struct *hashfiles = NULL, *hashfile;
91 + /* Build a list of potential candidates and open
93 + if ((d = opendir(hashname)) == NULL) {
94 + rprintf(FERROR,"opendir \"%s\": %s\n",
95 + hashname, strerror(errno));
99 + while ((di = readdir(d)) != NULL) {
100 + if (!strcmp(di->d_name,".") || !strcmp(di->d_name,"..")) {
104 + /* We need to have the largest fnbr in case we need to store
106 + this_fnbr = atol(di->d_name);
107 + if (this_fnbr > *fnbr)
110 + hashfile = (struct hashfile_struct*)malloc(sizeof(struct hashfile_struct));
111 + asprintf(&hashfile->name,"%s/%s",hashname,
113 + if (do_stat(hashfile->name,&st) == -1) {
114 + rprintf(FERROR,"%s: %s", hashfile->name,
116 + kill_hashfile(hashfile);
119 + if (st.st_size != size) {
120 + kill_hashfile(hashfile);
123 + hashfile->nlink = st.st_nlink;
124 + hashfile->fd = open(hashfile->name,O_RDONLY|O_BINARY);
125 + if (hashfile->fd == -1) {
126 + rprintf(FERROR,"%s: %s\n", hashfile->name,
128 + kill_hashfile(hashfile);
131 + if (hashfiles == NULL)
132 + hashfiles = hashfile->next = hashfile->prev = hashfile;
134 + hashfile->next = hashfiles;
135 + hashfile->prev = hashfiles->prev;
136 + hashfile->next->prev = hashfile;
137 + hashfile->prev->next = hashfile;
146 +struct hashfile_struct *compare_hashfiles(int fd,struct hashfile_struct *files)
149 + char buffer[BUFSIZ], cmpbuffer[BUFSIZ];
150 + struct hashfile_struct *iter, *next, *best;
156 + iter = files; /* in case files are 0 bytes */
157 + while ((amt = read(fd, buffer, BUFSIZ)) > 0) {
160 + /* Icky bit to resync when we steal the first node. */
166 + hamt = read(iter->fd, cmpbuffer, BUFSIZ);
167 + if (amt != hamt || memcmp(buffer, cmpbuffer, amt)) {
168 + if (iter == files) {
169 + files = files->prev;
171 + if (iter->next == iter) {
172 + files = next = NULL;
175 + if (iter == files) {
176 + /* So we know to resync */
180 + iter->next->prev = iter->prev;
181 + iter->prev->next = iter->next;
182 + kill_hashfile(iter);
186 + } while (iter != files);
188 + if (iter == NULL && files == NULL) {
189 + /* There are no matches. */
196 + rprintf(FERROR,"%s",strerror(errno));
197 + kill_hashfiles(files);
201 + /* If we only have one file left, use it. */
202 + if (files == files->next) {
206 + /* All files which remain in the list are identical and should have
207 + * the same size. We pick the one with the lowest link count (we
208 + * may have rolled over because we hit the maximum link count for
209 + * the filesystem). */
210 + best = iter = files;
211 + nlink = iter->nlink;
213 + if (iter->nlink < nlink) {
214 + nlink = iter->nlink;
218 + } while (iter != files);
220 + best->next->prev = best->prev;
221 + best->prev->next = best->next;
223 + files = files->next;
224 + kill_hashfiles(files);
229 +int link_by_hash(char *fnametmp,char *fname,struct file_struct *file)
232 + char *hashname = make_hash_name(file);
237 + if (file->length == 0) {
238 + return robust_rename(fnametmp,fname,0644);
241 + if (do_stat(hashname, &st) == -1) {
244 + /* Directory does not exist. */
245 + dirname = strdup(hashname);
246 + *strrchr(dirname,'/') = 0;
247 + if (do_mkdir(dirname, 0755) == -1 && errno != EEXIST) {
248 + rprintf(FERROR, "mkdir %s: %s\n", dirname,
252 + return robust_rename(fnametmp,fname,0644);
256 + if (do_mkdir(hashname, 0755) == -1 && errno != EEXIST) {
257 + rprintf(FERROR, "mkdir %s: %s\n", hashname,
260 + return robust_rename(fnametmp,fname,0644);
264 + asprintf(&linkname,"%s/0",hashname);
265 + rprintf(FINFO, "(1) linkname = %s\n", linkname);
268 + struct hashfile_struct *hashfiles, *hashfile;
271 + if (do_stat(fnametmp,&st) == -1) {
272 + rprintf(FERROR,"%s: %s\n",fname,strerror(errno));
275 + hashfiles = find_hashfiles(hashname, st.st_size, &last_fnbr);
277 + if (hashfiles == NULL) {
279 + asprintf(&linkname,"%s/0",hashname);
280 + rprintf(FINFO, "(2) linkname = %s\n", linkname);
283 + /* Search for one identical to us. */
284 + if ((fd = open(fnametmp,O_RDONLY|O_BINARY)) == -1) {
285 + rprintf(FERROR,"%s: %s\n",fnametmp,
287 + kill_hashfiles(hashfiles);
290 + hashfile = compare_hashfiles(fd, hashfiles);
295 + linkname = strdup(hashfile->name);
296 + rprintf(FINFO, "(3) linkname = %s\n", linkname);
297 + kill_hashfile(hashfile);
300 + asprintf(&linkname, "%s/%ld", hashname,
302 + rprintf(FINFO, "(4) linkname = %s\n", linkname);
308 + rprintf(FINFO, "link-by-hash (existing): \"%s\" -> %s\n",
309 + linkname, full_fname(fname));
310 + rc = do_link(linkname, fname);
312 + if (errno == EMLINK) {
315 + asprintf(&linkname,"%s/%ld",hashname,
317 + rprintf(FINFO, "(5) linkname = %s\n", linkname);
318 + rprintf(FINFO,"link-by-hash: max link count exceeded, starting new file \"%s\".\n", linkname);
320 + rprintf(FERROR,"link \"%s\" -> %s: %s\n",
321 + linkname,full_fname(fname),
323 + robust_unlink(fname);
324 + rc = robust_rename(fnametmp,fname,0644);
327 + do_unlink(fnametmp);
332 + rprintf(FINFO, "link-by-hash (new): %s -> \"%s\"\n",
333 + full_fname(fname),linkname);
335 + rc = robust_rename(fnametmp,fname,0644);
337 + rprintf(FERROR,"rename \"%s\" -> \"%s\": %s\n",
338 + full_fname(fnametmp),full_fname(fname),
341 + rc = do_link(fname,linkname);
343 + rprintf(FERROR,"link \"%s\" -> \"%s\": %s\n",
344 + full_fname(fname),linkname,
355 --- Makefile.in 2004-02-23 10:22:51.000000000 -0500
356 +++ Makefile.in 2004-02-23 10:22:51.000000000 -0500
358 main.o checksum.o match.o syscall.o log.o backup.o
359 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
360 fileio.o batch.o clientname.o
361 -OBJS3=progress.o pipe.o
362 +OBJS3=progress.o pipe.o hashlink.o
363 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
364 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
365 popt/popthelp.o popt/poptparse.o
366 --- options.c 2004-02-23 10:22:51.000000000 -0500
367 +++ options.c 2004-02-23 10:29:14.000000000 -0500
369 char *password_file = NULL;
370 char *rsync_path = RSYNC_PATH;
371 char *backup_dir = NULL;
372 +char *link_by_hash_dir = NULL;
373 char backup_dir_buf[MAXPATHLEN];
374 int rsync_port = RSYNC_PORT;
377 rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n");
378 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
379 rprintf(F," --link-dest=DIR create hardlinks to DIR for unchanged files\n");
380 + rprintf(F," --link-by-hash=DIR create hardlinks by hash to DIR for regular files\n");
381 rprintf(F," -P equivalent to --partial --progress\n");
382 rprintf(F," -z, --compress compress file data\n");
383 rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n");
385 enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
386 OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
387 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
388 - OPT_READ_BATCH, OPT_WRITE_BATCH,
389 + OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_LINK_BY_HASH,
390 OPT_REFUSED_BASE = 9000};
392 static struct poptOption long_options[] = {
394 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
395 {"compare-dest", 0, POPT_ARG_STRING, &compare_dest, 0, 0, 0 },
396 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
397 + {"link-by-hash", 0, POPT_ARG_STRING, 0, OPT_LINK_BY_HASH, 0, 0},
398 /* TODO: Should this take an optional int giving the compression level? */
399 {"compress", 'z', POPT_ARG_NONE, &do_compression, 0, 0, 0 },
400 {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 },
405 + case OPT_LINK_BY_HASH:
407 + link_by_hash_dir = (char *)poptGetOptArg(pc);
408 + checksum_seed = FIXED_CHECKSUM_SEED;
411 + snprintf(err_buf, sizeof err_buf,
412 + "hard links are not supported on this %s\n",
413 + am_server ? "server" : "client");
414 + rprintf(FERROR, "ERROR: %s", err_buf);
419 /* A large opt value means that set_refuse_options()
420 * turned this option off (opt-BASE is its index). */
422 args[ac++] = compare_dest;
425 + if (link_by_hash_dir && am_sender) {
426 + args[ac++] = "--link-by-hash";
427 + args[ac++] = link_by_hash_dir;
430 if (files_from && (!am_sender || remote_filesfrom_file)) {
431 if (remote_filesfrom_file) {
432 args[ac++] = "--files-from";
433 --- proto.h 2004-02-23 10:22:51.000000000 -0500
434 +++ proto.h 2004-02-23 10:22:51.000000000 -0500
436 void write_sum_head(int f, struct sum_struct *sum);
437 void recv_generator(char *fname, struct file_struct *file, int i, int f_out);
438 void generate_files(int f, struct file_list *flist, char *local_name);
439 +char* make_hash_name(struct file_struct *file);
440 +void kill_hashfile(struct hashfile_struct *hashfile);
441 +void kill_hashfiles(struct hashfile_struct *hashfiles);
442 +struct hashfile_struct *find_hashfiles(char *hashname, int64 size, long *fnbr);
443 +struct hashfile_struct *compare_hashfiles(int fd,struct hashfile_struct *files);
444 +int link_by_hash(char *fnametmp,char *fname,struct file_struct *file);
445 void init_hard_links(struct file_list *flist);
446 int hard_link_check(struct file_struct *file, int skip);
447 void do_hard_links(void);
448 --- receiver.c 2004-02-23 10:22:51.000000000 -0500
449 +++ receiver.c 2004-02-23 10:22:51.000000000 -0500
450 @@ -186,10 +186,11 @@
453 static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
455 + OFF_T total_size,char *md4)
458 struct sum_struct sum;
459 + struct mdfour mdfour_data;
466 read_sum_head(f_in, &sum);
469 + mdfour_begin(&mdfour_data);
473 while ((i = recv_token(f_in, &data)) != 0) {
475 cleanup_got_literal = 1;
479 + mdfour_update(&mdfour_data,data,i);
481 if (fd != -1 && write_file(fd,data,i) != i) {
482 rprintf(FERROR, "write failed on %s: %s\n",
488 + mdfour_update(&mdfour_data,map,len);
491 if (fd != -1 && write_file(fd,map,len) != (int) len) {
497 + mdfour_result(&mdfour_data, (unsigned char*)md4);
499 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
502 extern int preserve_perms;
503 extern int delete_after;
504 extern int orig_umask;
505 + extern char *link_by_hash_dir;
506 struct stats initial_stats;
510 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
511 rprintf(FERROR, "fstat %s failed: %s\n",
512 full_fname(fnamecmp), strerror(errno));
513 - receive_data(f_in,NULL,-1,NULL,file->length);
514 + receive_data(f_in,NULL,-1,NULL,file->length,NULL);
520 rprintf(FERROR,"recv_files: %s is a directory\n",
521 full_fname(fnamecmp));
522 - receive_data(f_in, NULL, -1, NULL, file->length);
523 + receive_data(f_in,NULL,-1,NULL,file->length,NULL);
529 rprintf(FERROR, "mkstemp %s failed: %s\n",
530 full_fname(fnametmp), strerror(errno));
531 - receive_data(f_in,mapbuf,-1,NULL,file->length);
532 + receive_data(f_in,mapbuf,-1,NULL,file->length,NULL);
533 if (mapbuf) unmap_file(mapbuf);
534 if (fd1 != -1) close(fd1);
540 - recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
542 + if (link_by_hash_dir) {
543 + file->u.sum = (char*)malloc (MD4_SUM_LENGTH);
546 + recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length,file->u.sum);
548 log_recv(file, &initial_stats);
550 --- rsync.c 2004-02-23 10:22:51.000000000 -0500
551 +++ rsync.c 2004-02-23 10:27:15.000000000 -0500
553 extern int preserve_gid;
554 extern int preserve_perms;
555 extern int make_backups;
556 +extern char *link_by_hash_dir;
561 if (make_backups && !make_backup(fname))
565 + if (link_by_hash_dir) {
566 + if ((ret = link_by_hash(fnametmp,fname,file)) != 0)
570 /* move tmp file over real file */
571 ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS);
573 --- rsync.h 2004-02-23 10:22:51.000000000 -0500
574 +++ rsync.h 2004-02-23 10:22:51.000000000 -0500
576 int current_file_index;
579 +struct hashfile_struct {
580 + struct hashfile_struct *next;
581 + struct hashfile_struct *prev;
588 /* we need this function because of the silly way in which duplicate
589 entries are handled in the file lists - we can't change this