Updated patches to work with the current trunk.
[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
cc3e685d 11diff --git a/flist.c b/flist.c
fc557362 12index 09b4fc5..3295724 100644
cc3e685d
WD
13--- a/flist.c
14+++ b/flist.c
fc557362 15@@ -1268,7 +1268,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
502d2817
WD
16 #endif
17
fc557362
WD
18 if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
19- file_checksum(thisname, tmp_sum, st.st_size);
20+ if (!get_sum_xattr(thisname, &st, tmp_sum))
21+ file_checksum(thisname, tmp_sum, st.st_size);
22 if (sender_keeps_checksum)
23 extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
24 }
cc3e685d 25diff --git a/generator.c b/generator.c
fc557362 26index 12007a1..2587bc9 100644
cc3e685d
WD
27--- a/generator.c
28+++ b/generator.c
fc557362 29@@ -531,7 +531,8 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
502d2817
WD
30 of the file time to determine whether to sync */
31 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
32 char sum[MAX_DIGEST_LEN];
33- file_checksum(fn, sum, st->st_size);
34+ if (!get_sum_xattr(fn, st, sum))
35+ file_checksum(fn, sum, st->st_size);
36 return memcmp(sum, F_SUM(file), checksum_len) == 0;
37 }
38
cc3e685d
WD
39diff --git a/support/xsums b/support/xsums
40new file mode 100644
fc557362 41index 0000000..31d2537
cc3e685d
WD
42--- /dev/null
43+++ b/support/xsums
fc557362
WD
44@@ -0,0 +1,118 @@
45+#!/usr/bin/perl -w
502d2817
WD
46+use strict;
47+
48+use Getopt::Long;
49+use Cwd qw(abs_path cwd);
50+use Digest::MD4;
51+use Digest::MD5;
52+use File::ExtAttr ':all';
53+
54+our($recurse_opt, $help_opt);
55+our $verbosity = 0;
56+
57+&Getopt::Long::Configure('bundling');
58+&usage if !&GetOptions(
59+ 'recurse|r' => \$recurse_opt,
60+ 'verbose|v+' => \$verbosity,
61+ 'help|h' => \$help_opt,
62+) || $help_opt;
63+
64+my $start_dir = cwd();
65+
66+my @dirs = @ARGV;
67+@dirs = '.' unless @dirs;
68+foreach (@dirs) {
69+ $_ = abs_path($_);
70+}
71+
72+$| = 1;
73+
74+my $md4 = Digest::MD4->new;
75+my $md5 = Digest::MD5->new;
76+
77+while (@dirs) {
78+ my $dir = shift @dirs;
79+
80+ if (!chdir($dir)) {
81+ warn "Unable to chdir to $dir: $!\n";
82+ next;
83+ }
84+ if (!opendir(DP, '.')) {
85+ warn "Unable to opendir $dir: $!\n";
86+ next;
87+ }
88+
89+ if ($verbosity) {
90+ my $reldir = $dir;
91+ $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
92+ print "scanning $reldir\n";
93+ }
94+
95+ my @subdirs;
96+ while (defined(my $fn = readdir(DP))) {
97+ next if $fn =~ /^\.\.?$/ || -l $fn;
98+ if (-d _) {
99+ push(@subdirs, "$dir/$fn");
100+ next;
101+ }
102+ next unless -f _;
103+
104+ my($size,$mtime) = (stat(_))[7,9];
105+
106+ my $sum4 = getfattr($fn, 'rsync.%md4');
107+ my $sum5 = getfattr($fn, 'rsync.%md5');
108+
109+ foreach ($sum4, $sum5) {
110+ if (defined $_) {
adc8e875
WD
111+ if (length($_) == 24) {
112+ my($sz,$mt,$sum) = unpack('V2a16', $_);
113+ if ($sz != ($size & 0xFFFFFFFF)
114+ || $mt != ($mtime & 0xFFFFFFFF)) {
115+ $_ = undef;
116+ } else {
117+ $_ = $sum;
118+ }
502d2817 119+ } else {
adc8e875 120+ $_ = undef;
502d2817
WD
121+ }
122+ }
123+ }
124+ if (!defined($sum4) || !defined($sum5)) {
125+ if (!open(IN, $fn)) {
126+ print STDERR "Unable to read $fn: $!\n";
127+ next;
128+ }
129+
130+ while (sysread(IN, $_, 64*1024)) {
131+ $md4->add($_);
132+ $md5->add($_);
133+ }
134+ close IN;
135+
adc8e875
WD
136+ $sum4 = $md4->digest;
137+ $sum5 = $md5->digest;
502d2817
WD
138+ print " $fn\n" if $verbosity > 1;
139+
adc8e875
WD
140+ my $szmt = pack('V2', $size, $mtime); # 32-bits, may truncate
141+ setfattr($fn, 'rsync.%md4', $szmt.$sum4);
142+ setfattr($fn, 'rsync.%md5', $szmt.$sum5);
502d2817
WD
143+ #utime $mtime, $mtime, $fn; # Set mtime if it changes.
144+ }
145+ }
146+
147+ closedir DP;
148+
149+ unshift(@dirs, sort @subdirs) if $recurse_opt;
150+}
151+
152+sub usage
153+{
154+ die <<EOT;
155+Usage: rsyncsums [OPTIONS] [DIRS]
156+
157+Options:
158+ -r, --recurse Update checksums in subdirectories too.
159+ -v, --verbose Mention what we're doing. Repeat for more info.
160+ -h, --help Display this help message.
161+EOT
162+}
cc3e685d 163diff --git a/xattrs.c b/xattrs.c
fc557362 164index 2d0e050..f364a2a 100644
cc3e685d
WD
165--- a/xattrs.c
166+++ b/xattrs.c
fc557362 167@@ -34,6 +34,8 @@ extern int read_only;
502d2817 168 extern int list_only;
898a2112 169 extern int preserve_xattrs;
502d2817
WD
170 extern int checksum_seed;
171+extern int checksum_len;
172+extern int protocol_version;
173
174 #define RSYNC_XAL_INITIAL 5
175 #define RSYNC_XAL_LIST_INITIAL 100
fc557362 176@@ -69,6 +71,10 @@ extern int checksum_seed;
c0c7984e
WD
177 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
178 #define XDEF_ACL_SUFFIX "dacl"
179 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
180+#define MD4_SUFFIX "md4"
181+#define MD4_ATTR RSYNC_PREFIX "%" MD4_SUFFIX
182+#define MD5_SUFFIX "md5"
183+#define MD5_ATTR RSYNC_PREFIX "%" MD5_SUFFIX
adc8e875 184
502d2817
WD
185 typedef struct {
186 char *datum, *name;
fc557362 187@@ -239,7 +245,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
c0c7984e
WD
188 || (am_root < 0
189 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
190 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
191- || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
192+ || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0
193+ || strcmp(name+RPRE_LEN+1, MD4_SUFFIX) == 0
194+ || strcmp(name+RPRE_LEN+1, MD5_SUFFIX) == 0)))
195 continue;
196 }
197
fc557362 198@@ -895,6 +903,39 @@ int del_def_xattr_acl(const char *fname)
502d2817 199 }
9f085fa3 200 #endif
502d2817
WD
201
202+int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
203+{
204+ const char *mdattr = protocol_version >= 30
205+ ? MD5_ATTR : MD4_ATTR;
adc8e875
WD
206+ char buf[256];
207+ uint32 file_length, mtime;
208+ int len;
e2e42a01 209+
adc8e875 210+ len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
502d2817
WD
211+ if (len < 0) {
212+ if (errno == ENOTSUP || errno == ENOATTR)
213+ return 0;
cc3e685d 214+ rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
502d2817
WD
215+ mdattr, full_fname(fname));
216+ return 0;
217+ }
adc8e875
WD
218+ if (len != 4 + 4 + checksum_len) {
219+ rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
220+ mdattr, full_fname(fname));
221+ return 0;
502d2817 222+ }
502d2817 223+
adc8e875
WD
224+ file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
225+ mtime = IVAL(buf, 4);
502d2817 226+
adc8e875 227+ if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
502d2817
WD
228+ return 0;
229+
adc8e875 230+ memcpy(sum, buf + 8, checksum_len);
502d2817 231+
adc8e875 232+ return 1;
502d2817
WD
233+}
234+
235 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
236 {
237 int mode, rdev_major, rdev_minor, uid, gid, len;