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)
11 based-on: 181c9faf928faad08ef095f4667afe460ec3bef6
12 diff --git a/flist.c b/flist.c
15 @@ -1277,7 +1277,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
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;
25 diff --git a/generator.c b/generator.c
28 @@ -531,7 +531,8 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
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;
38 diff --git a/support/xsums b/support/xsums
47 +use Cwd qw(abs_path cwd);
50 +use File::ExtAttr ':all';
52 +our($recurse_opt, $help_opt);
55 +&Getopt::Long::Configure('bundling');
56 +&usage if !&GetOptions(
57 + 'recurse|r' => \$recurse_opt,
58 + 'verbose|v+' => \$verbosity,
59 + 'help|h' => \$help_opt,
62 +my $start_dir = cwd();
65 +@dirs = '.' unless @dirs;
72 +my $md4 = Digest::MD4->new;
73 +my $md5 = Digest::MD5->new;
76 + my $dir = shift @dirs;
79 + warn "Unable to chdir to $dir: $!\n";
82 + if (!opendir(DP, '.')) {
83 + warn "Unable to opendir $dir: $!\n";
89 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
90 + print "scanning $reldir\n";
94 + while (defined(my $fn = readdir(DP))) {
95 + next if $fn =~ /^\.\.?$/ || -l $fn;
97 + push(@subdirs, "$dir/$fn");
102 + my($size,$mtime) = (stat(_))[7,9];
104 + my $sum4 = getfattr($fn, 'rsync.%md4');
105 + my $sum5 = getfattr($fn, 'rsync.%md5');
107 + foreach ($sum4, $sum5) {
109 + if (length($_) == 24) {
110 + my($sz,$mt,$sum) = unpack('V2a16', $_);
111 + if ($sz != ($size & 0xFFFFFFFF)
112 + || $mt != ($mtime & 0xFFFFFFFF)) {
122 + if (!defined($sum4) || !defined($sum5)) {
123 + if (!open(IN, $fn)) {
124 + print STDERR "Unable to read $fn: $!\n";
128 + while (sysread(IN, $_, 64*1024)) {
134 + $sum4 = $md4->digest;
135 + $sum5 = $md5->digest;
136 + print " $fn\n" if $verbosity > 1;
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);
141 + #utime $mtime, $mtime, $fn; # Set mtime if it changes.
147 + unshift(@dirs, sort @subdirs) if $recurse_opt;
153 +Usage: rsyncsums [OPTIONS] [DIRS]
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.
161 diff --git a/xattrs.c b/xattrs.c
164 @@ -34,6 +34,8 @@ extern int read_only;
165 extern int list_only;
166 extern int preserve_xattrs;
167 extern int checksum_seed;
168 +extern int checksum_len;
169 +extern int protocol_version;
171 #define RSYNC_XAL_INITIAL 5
172 #define RSYNC_XAL_LIST_INITIAL 100
173 @@ -69,6 +71,10 @@ extern int checksum_seed;
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
184 @@ -241,7 +247,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
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)))
195 @@ -914,6 +922,39 @@ int del_def_xattr_acl(const char *fname)
199 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
201 + const char *mdattr = protocol_version >= 30
202 + ? MD5_ATTR : MD4_ATTR;
204 + uint32 file_length, mtime;
207 + len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
209 + if (errno == ENOTSUP || errno == ENOATTR)
211 + rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
212 + mdattr, full_fname(fname));
215 + if (len != 4 + 4 + checksum_len) {
216 + rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
217 + mdattr, full_fname(fname));
221 + file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
222 + mtime = IVAL(buf, 4);
224 + if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
227 + memcpy(sum, buf + 8, checksum_len);
232 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
234 int mode, rdev_major, rdev_minor, uid, gid, len;