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