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 @@ -1193,7 +1193,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 @@ -627,7 +627,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;
37 @@ -1070,6 +1070,12 @@ isDigit(const char *ptr)
41 +isXDigit(const char *ptr)
43 + return isxdigit(*(unsigned char *)ptr);
47 isPrint(const char *ptr)
49 return isprint(*(unsigned char *)ptr);
57 +use Cwd qw(abs_path cwd);
60 +use File::ExtAttr ':all';
62 +our($recurse_opt, $help_opt);
65 +&Getopt::Long::Configure('bundling');
66 +&usage if !&GetOptions(
67 + 'recurse|r' => \$recurse_opt,
68 + 'verbose|v+' => \$verbosity,
69 + 'help|h' => \$help_opt,
72 +my $start_dir = cwd();
75 +@dirs = '.' unless @dirs;
82 +my $md4 = Digest::MD4->new;
83 +my $md5 = Digest::MD5->new;
86 + my $dir = shift @dirs;
89 + warn "Unable to chdir to $dir: $!\n";
92 + if (!opendir(DP, '.')) {
93 + warn "Unable to opendir $dir: $!\n";
99 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
100 + print "scanning $reldir\n";
104 + while (defined(my $fn = readdir(DP))) {
105 + next if $fn =~ /^\.\.?$/ || -l $fn;
107 + push(@subdirs, "$dir/$fn");
112 + my($size,$mtime) = (stat(_))[7,9];
114 + my $sum4 = getfattr($fn, 'rsync.%md4');
115 + my $sum5 = getfattr($fn, 'rsync.%md5');
117 + foreach ($sum4, $sum5) {
119 + my($sum, $sz, $mt) = /^([0-9a-f]{32}) (\d+) (\d+)$/;
120 + if (!defined($mt) || $sz != $size || $mt != $mtime) {
127 + if (!defined($sum4) || !defined($sum5)) {
128 + if (!open(IN, $fn)) {
129 + print STDERR "Unable to read $fn: $!\n";
133 + while (sysread(IN, $_, 64*1024)) {
139 + $sum4 = $md4->hexdigest;
140 + $sum5 = $md5->hexdigest;
141 + print " $sum4 $sum5" if $verbosity > 2;
142 + print " $fn\n" if $verbosity > 1;
144 + setfattr($fn, 'rsync.%md4', "$sum4 $size $mtime");
145 + setfattr($fn, 'rsync.%md5', "$sum5 $size $mtime");
146 + #utime $mtime, $mtime, $fn; # Set mtime if it changes.
152 + unshift(@dirs, sort @subdirs) if $recurse_opt;
158 +Usage: rsyncsums [OPTIONS] [DIRS]
161 + -r, --recurse Update checksums in subdirectories too.
162 + -v, --verbose Mention what we're doing. Repeat for more info.
163 + -h, --help Display this help message.
168 @@ -31,6 +31,8 @@ extern int am_generator;
169 extern int read_only;
170 extern int list_only;
171 extern int checksum_seed;
172 +extern int checksum_len;
173 +extern int protocol_version;
175 #define RSYNC_XAL_INITIAL 5
176 #define RSYNC_XAL_LIST_INITIAL 100
177 @@ -64,6 +66,9 @@ extern int checksum_seed;
178 #define XSTAT_ATTR RSYNC_PREFIX "%stat"
179 #define XSTAT_LEN ((int)sizeof XSTAT_ATTR - 1)
181 +#define MD4_ATTR RSYNC_PREFIX "%md4"
182 +#define MD5_ATTR RSYNC_PREFIX "%md5"
186 size_t datum_len, name_len;
187 @@ -795,6 +800,71 @@ int set_xattr(const char *fname, const s
188 return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
191 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
193 + const char *mdattr = protocol_version >= 30
194 + ? MD5_ATTR : MD4_ATTR;
195 + char *cp, buf[256];
200 + len = sys_lgetxattr(fname, mdattr, buf, sizeof buf - 1);
201 + if (len >= (int)sizeof buf) {
206 + if (errno == ENOTSUP || errno == ENOATTR)
208 + rsyserr(FERROR, errno, "failed to read xattr %s for %s",
209 + mdattr, full_fname(fname));
214 + for (i = 0, cp = buf; i < checksum_len*2; i++, cp++) {
216 + if (isXDigit(cp)) {
220 + x = (*cp & 0xF) + 9;
234 + while (isDigit(cp))
235 + file_length = file_length * 10 + *cp++ - '0';
240 + while (isDigit(cp))
241 + mtime = mtime * 10 + *cp++ - '0';
245 + if (stp->st_size != file_length || stp->st_mtime != mtime)
251 + rprintf(FERROR, "Corrupt %s xattr attached to %s: \"%s\"\n",
252 + mdattr, full_fname(fname), buf);
253 + exit_cleanup(RERR_FILEIO);
256 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
258 int mode, rdev_major, rdev_minor, uid, gid, len;