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