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