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 diff --git a/flist.c b/flist.c
14 @@ -1286,7 +1286,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
15 memcpy(bp + basename_len, linkname, linkname_len);
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);
24 diff --git a/generator.c b/generator.c
27 @@ -721,7 +721,8 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
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;
37 diff --git a/support/xsums b/support/xsums
46 +use Cwd qw(abs_path cwd);
49 +use File::ExtAttr ':all';
51 +our($recurse_opt, $help_opt);
54 +&Getopt::Long::Configure('bundling');
55 +&usage if !&GetOptions(
56 + 'recurse|r' => \$recurse_opt,
57 + 'verbose|v+' => \$verbosity,
58 + 'help|h' => \$help_opt,
61 +my $start_dir = cwd();
64 +@dirs = '.' unless @dirs;
71 +my $md4 = Digest::MD4->new;
72 +my $md5 = Digest::MD5->new;
75 + my $dir = shift @dirs;
78 + warn "Unable to chdir to $dir: $!\n";
81 + if (!opendir(DP, '.')) {
82 + warn "Unable to opendir $dir: $!\n";
88 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
89 + print "scanning $reldir\n";
93 + while (defined(my $fn = readdir(DP))) {
94 + next if $fn =~ /^\.\.?$/ || -l $fn;
96 + push(@subdirs, "$dir/$fn");
101 + my($size,$mtime) = (stat(_))[7,9];
103 + my $sum4 = getfattr($fn, 'rsync.%md4');
104 + my $sum5 = getfattr($fn, 'rsync.%md5');
106 + foreach ($sum4, $sum5) {
108 + if (length($_) == 24) {
109 + my($sz,$mt,$sum) = unpack('V2a16', $_);
110 + if ($sz != ($size & 0xFFFFFFFF)
111 + || $mt != ($mtime & 0xFFFFFFFF)) {
121 + if (!defined($sum4) || !defined($sum5)) {
122 + if (!open(IN, $fn)) {
123 + print STDERR "Unable to read $fn: $!\n";
127 + while (sysread(IN, $_, 64*1024)) {
133 + $sum4 = $md4->digest;
134 + $sum5 = $md5->digest;
135 + print " $fn\n" if $verbosity > 1;
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);
140 + #utime $mtime, $mtime, $fn; # Set mtime if it changes.
146 + unshift(@dirs, sort @subdirs) if $recurse_opt;
152 +Usage: rsyncsums [OPTIONS] [DIRS]
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.
160 diff --git a/xattrs.c b/xattrs.c
163 @@ -33,6 +33,8 @@ extern int read_only;
164 extern int list_only;
165 extern int preserve_xattrs;
166 extern int checksum_seed;
167 +extern int checksum_len;
168 +extern int protocol_version;
170 #define RSYNC_XAL_INITIAL 5
171 #define RSYNC_XAL_LIST_INITIAL 100
172 @@ -68,6 +70,10 @@ extern int checksum_seed;
173 #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
174 #define XDEF_ACL_SUFFIX "dacl"
175 #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
176 +#define MD4_SUFFIX "md4"
177 +#define MD4_ATTR RSYNC_PREFIX "%" MD4_SUFFIX
178 +#define MD5_SUFFIX "md5"
179 +#define MD5_ATTR RSYNC_PREFIX "%" MD5_SUFFIX
183 @@ -238,7 +244,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
185 && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
186 || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
187 - || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
188 + || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0
189 + || strcmp(name+RPRE_LEN+1, MD4_SUFFIX) == 0
190 + || strcmp(name+RPRE_LEN+1, MD5_SUFFIX) == 0)))
194 @@ -894,6 +902,39 @@ int del_def_xattr_acl(const char *fname)
198 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
200 + const char *mdattr = protocol_version >= 30
201 + ? MD5_ATTR : MD4_ATTR;
203 + uint32 file_length, mtime;
206 + len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
208 + if (errno == ENOTSUP || errno == ENOATTR)
210 + rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
211 + mdattr, full_fname(fname));
214 + if (len != 4 + 4 + checksum_len) {
215 + rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
216 + mdattr, full_fname(fname));
220 + file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
221 + mtime = IVAL(buf, 4);
223 + if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
226 + memcpy(sum, buf + 8, checksum_len);
231 int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
233 int mode, rdev_major, rdev_minor, uid, gid, len;