1 This patch is the start of storing/using checksum information from
2 extended attribute values. The rsync code only reads the values
3 at the moment. There is also a perl script that can create them.
5 To use this patch, run these commands for a successful build:
7 patch -p1 <patches/checksum-xattr.diff
8 ./configure (optional if already run)
13 @@ -1183,7 +1183,8 @@ struct file_struct *make_file(const char
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);
22 F_PATHNAME(file) = pathname;
25 @@ -626,7 +626,8 @@ int unchanged_file(char *fn, struct file
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;
42 +use Cwd qw(abs_path cwd);
45 +use File::ExtAttr ':all';
47 +our($recurse_opt, $help_opt);
50 +&Getopt::Long::Configure('bundling');
51 +&usage if !&GetOptions(
52 + 'recurse|r' => \$recurse_opt,
53 + 'verbose|v+' => \$verbosity,
54 + 'help|h' => \$help_opt,
57 +my $start_dir = cwd();
60 +@dirs = '.' unless @dirs;
67 +my $md4 = Digest::MD4->new;
68 +my $md5 = Digest::MD5->new;
71 + my $dir = shift @dirs;
74 + warn "Unable to chdir to $dir: $!\n";
77 + if (!opendir(DP, '.')) {
78 + warn "Unable to opendir $dir: $!\n";
84 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
85 + print "scanning $reldir\n";
89 + while (defined(my $fn = readdir(DP))) {
90 + next if $fn =~ /^\.\.?$/ || -l $fn;
92 + push(@subdirs, "$dir/$fn");
97 + my($size,$mtime) = (stat(_))[7,9];
99 + my $sum4 = getfattr($fn, 'rsync.%md4');
100 + my $sum5 = getfattr($fn, 'rsync.%md5');
102 + foreach ($sum4, $sum5) {
104 + if (length($_) == 24) {
105 + my($sz,$mt,$sum) = unpack('V2a16', $_);
106 + if ($sz != ($size & 0xFFFFFFFF)
107 + || $mt != ($mtime & 0xFFFFFFFF)) {
117 + if (!defined($sum4) || !defined($sum5)) {
118 + if (!open(IN, $fn)) {
119 + print STDERR "Unable to read $fn: $!\n";
123 + while (sysread(IN, $_, 64*1024)) {
129 + $sum4 = $md4->digest;
130 + $sum5 = $md5->digest;
131 + print " $fn\n" if $verbosity > 1;
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);
136 + #utime $mtime, $mtime, $fn; # Set mtime if it changes.
142 + unshift(@dirs, sort @subdirs) if $recurse_opt;
148 +Usage: rsyncsums [OPTIONS] [DIRS]
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.
158 @@ -33,6 +33,8 @@ extern int read_only;
159 extern int list_only;
160 extern int preserve_xattrs;
161 extern int checksum_seed;
162 +extern int checksum_len;
163 +extern int protocol_version;
165 #define RSYNC_XAL_INITIAL 5
166 #define RSYNC_XAL_LIST_INITIAL 100
167 @@ -66,6 +68,8 @@ extern int checksum_seed;
168 #define XSTAT_ATTR RSYNC_PREFIX "%stat"
169 #define XACC_ACL_ATTR RSYNC_PREFIX "%aacl"
170 #define XDEF_ACL_ATTR RSYNC_PREFIX "%dacl"
171 +#define MD4_ATTR RSYNC_PREFIX "%md4"
172 +#define MD5_ATTR RSYNC_PREFIX "%md5"
176 @@ -825,6 +829,39 @@ int del_def_xattr_acl(const char *fname)
180 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
182 + const char *mdattr = protocol_version >= 30
183 + ? MD5_ATTR : MD4_ATTR;
185 + uint32 file_length, mtime;
188 + len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
190 + if (errno == ENOTSUP || errno == ENOATTR)
192 + rsyserr(FERROR, errno, "failed to read xattr %s for %s",
193 + mdattr, full_fname(fname));
196 + if (len != 4 + 4 + checksum_len) {
197 + rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
198 + mdattr, full_fname(fname));
202 + file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
203 + mtime = IVAL(buf, 4);
205 + if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
208 + memcpy(sum, buf + 8, checksum_len);
213 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
215 int mode, rdev_major, rdev_minor, uid, gid, len;