Mention option in the man-page.
[rsync/rsync-patches.git] / checksum-xattr.diff
CommitLineData
502d2817
WD
1This patch is the start of storing/using checksum information from
2extended attribute values. The rsync code only reads the values
3at the moment. There is also a perl script that can create them.
4
5To use this patch, run these commands for a successful build:
6
7 patch -p1 <patches/checksum-xattr.diff
8 ./configure (optional if already run)
9 make
10
11--- old/flist.c
12+++ new/flist.c
9f085fa3 13@@ -1183,7 +1183,8 @@ struct file_struct *make_file(const char
502d2817
WD
14 }
15 #endif
16
17- if (always_checksum && am_sender && S_ISREG(st.st_mode))
18+ if (always_checksum && am_sender && S_ISREG(st.st_mode)
19+ && !get_sum_xattr(thisname, &st, tmp_sum))
20 file_checksum(thisname, tmp_sum, st.st_size);
21
22 F_PATHNAME(file) = pathname;
23--- old/generator.c
24+++ new/generator.c
9f085fa3 25@@ -626,7 +626,8 @@ int unchanged_file(char *fn, struct file
502d2817
WD
26 of the file time to determine whether to sync */
27 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
28 char sum[MAX_DIGEST_LEN];
29- file_checksum(fn, sum, st->st_size);
30+ if (!get_sum_xattr(fn, st, sum))
31+ file_checksum(fn, sum, st->st_size);
32 return memcmp(sum, F_SUM(file), checksum_len) == 0;
33 }
34
502d2817
WD
35--- old/support/xsums
36+++ new/support/xsums
adc8e875 37@@ -0,0 +1,118 @@
502d2817
WD
38+#!/usr/bin/perl -w
39+use strict;
40+
41+use Getopt::Long;
42+use Cwd qw(abs_path cwd);
43+use Digest::MD4;
44+use Digest::MD5;
45+use File::ExtAttr ':all';
46+
47+our($recurse_opt, $help_opt);
48+our $verbosity = 0;
49+
50+&Getopt::Long::Configure('bundling');
51+&usage if !&GetOptions(
52+ 'recurse|r' => \$recurse_opt,
53+ 'verbose|v+' => \$verbosity,
54+ 'help|h' => \$help_opt,
55+) || $help_opt;
56+
57+my $start_dir = cwd();
58+
59+my @dirs = @ARGV;
60+@dirs = '.' unless @dirs;
61+foreach (@dirs) {
62+ $_ = abs_path($_);
63+}
64+
65+$| = 1;
66+
67+my $md4 = Digest::MD4->new;
68+my $md5 = Digest::MD5->new;
69+
70+while (@dirs) {
71+ my $dir = shift @dirs;
72+
73+ if (!chdir($dir)) {
74+ warn "Unable to chdir to $dir: $!\n";
75+ next;
76+ }
77+ if (!opendir(DP, '.')) {
78+ warn "Unable to opendir $dir: $!\n";
79+ next;
80+ }
81+
82+ if ($verbosity) {
83+ my $reldir = $dir;
84+ $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
85+ print "scanning $reldir\n";
86+ }
87+
88+ my @subdirs;
89+ while (defined(my $fn = readdir(DP))) {
90+ next if $fn =~ /^\.\.?$/ || -l $fn;
91+ if (-d _) {
92+ push(@subdirs, "$dir/$fn");
93+ next;
94+ }
95+ next unless -f _;
96+
97+ my($size,$mtime) = (stat(_))[7,9];
98+
99+ my $sum4 = getfattr($fn, 'rsync.%md4');
100+ my $sum5 = getfattr($fn, 'rsync.%md5');
101+
102+ foreach ($sum4, $sum5) {
103+ if (defined $_) {
adc8e875
WD
104+ if (length($_) == 24) {
105+ my($sz,$mt,$sum) = unpack('V2a16', $_);
106+ if ($sz != ($size & 0xFFFFFFFF)
107+ || $mt != ($mtime & 0xFFFFFFFF)) {
108+ $_ = undef;
109+ } else {
110+ $_ = $sum;
111+ }
502d2817 112+ } else {
adc8e875 113+ $_ = undef;
502d2817
WD
114+ }
115+ }
116+ }
117+ if (!defined($sum4) || !defined($sum5)) {
118+ if (!open(IN, $fn)) {
119+ print STDERR "Unable to read $fn: $!\n";
120+ next;
121+ }
122+
123+ while (sysread(IN, $_, 64*1024)) {
124+ $md4->add($_);
125+ $md5->add($_);
126+ }
127+ close IN;
128+
adc8e875
WD
129+ $sum4 = $md4->digest;
130+ $sum5 = $md5->digest;
502d2817
WD
131+ print " $fn\n" if $verbosity > 1;
132+
adc8e875
WD
133+ my $szmt = pack('V2', $size, $mtime); # 32-bits, may truncate
134+ setfattr($fn, 'rsync.%md4', $szmt.$sum4);
135+ setfattr($fn, 'rsync.%md5', $szmt.$sum5);
502d2817
WD
136+ #utime $mtime, $mtime, $fn; # Set mtime if it changes.
137+ }
138+ }
139+
140+ closedir DP;
141+
142+ unshift(@dirs, sort @subdirs) if $recurse_opt;
143+}
144+
145+sub usage
146+{
147+ die <<EOT;
148+Usage: rsyncsums [OPTIONS] [DIRS]
149+
150+Options:
151+ -r, --recurse Update checksums in subdirectories too.
152+ -v, --verbose Mention what we're doing. Repeat for more info.
153+ -h, --help Display this help message.
154+EOT
155+}
156--- old/xattrs.c
157+++ new/xattrs.c
9f085fa3 158@@ -33,6 +33,8 @@ extern int read_only;
502d2817 159 extern int list_only;
898a2112 160 extern int preserve_xattrs;
502d2817
WD
161 extern int checksum_seed;
162+extern int checksum_len;
163+extern int protocol_version;
164
165 #define RSYNC_XAL_INITIAL 5
166 #define RSYNC_XAL_LIST_INITIAL 100
9f085fa3 167@@ -66,6 +68,8 @@ extern int checksum_seed;
adc8e875 168 #define XSTAT_ATTR RSYNC_PREFIX "%stat"
9f085fa3
WD
169 #define XACC_ACL_ATTR RSYNC_PREFIX "%aacl"
170 #define XDEF_ACL_ATTR RSYNC_PREFIX "%dacl"
502d2817
WD
171+#define MD4_ATTR RSYNC_PREFIX "%md4"
172+#define MD5_ATTR RSYNC_PREFIX "%md5"
adc8e875 173
502d2817
WD
174 typedef struct {
175 char *datum, *name;
9f085fa3 176@@ -825,6 +829,39 @@ int del_def_xattr_acl(const char *fname)
502d2817 177 }
9f085fa3 178 #endif
502d2817
WD
179
180+int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
181+{
182+ const char *mdattr = protocol_version >= 30
183+ ? MD5_ATTR : MD4_ATTR;
adc8e875
WD
184+ char buf[256];
185+ uint32 file_length, mtime;
186+ int len;
502d2817 187+
adc8e875 188+ len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
502d2817
WD
189+ if (len < 0) {
190+ if (errno == ENOTSUP || errno == ENOATTR)
191+ return 0;
192+ rsyserr(FERROR, errno, "failed to read xattr %s for %s",
193+ mdattr, full_fname(fname));
194+ return 0;
195+ }
adc8e875
WD
196+ if (len != 4 + 4 + checksum_len) {
197+ rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
198+ mdattr, full_fname(fname));
199+ return 0;
502d2817 200+ }
502d2817 201+
adc8e875
WD
202+ file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
203+ mtime = IVAL(buf, 4);
502d2817 204+
adc8e875 205+ if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
502d2817
WD
206+ return 0;
207+
adc8e875 208+ memcpy(sum, buf + 8, checksum_len);
502d2817 209+
adc8e875 210+ return 1;
502d2817
WD
211+}
212+
213 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
214 {
215 int mode, rdev_major, rdev_minor, uid, gid, len;