Switched over to using binary data in the xattr values.
[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 --- old/flist.c
12 +++ new/flist.c
13 @@ -1193,7 +1193,8 @@ struct file_struct *make_file(const char
14         }
15  #endif
16  
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);
21  
22         F_PATHNAME(file) = pathname;
23 --- old/generator.c
24 +++ new/generator.c
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;
33         }
34  
35 --- old/support/xsums
36 +++ new/support/xsums
37 @@ -0,0 +1,118 @@
38 +#!/usr/bin/perl -w
39 +use strict;
40 +
41 +use Getopt::Long;
42 +use Cwd qw(abs_path cwd);
43 +use Digest::MD4;
44 +use Digest::MD5;
45 +use File::ExtAttr ':all';
46 +
47 +our($recurse_opt, $help_opt);
48 +our $verbosity = 0;
49 +
50 +&Getopt::Long::Configure('bundling');
51 +&usage if !&GetOptions(
52 +    'recurse|r' => \$recurse_opt,
53 +    'verbose|v+' => \$verbosity,
54 +    'help|h' => \$help_opt,
55 +) || $help_opt;
56 +
57 +my $start_dir = cwd();
58 +
59 +my @dirs = @ARGV;
60 +@dirs = '.' unless @dirs;
61 +foreach (@dirs) {
62 +    $_ = abs_path($_);
63 +}
64 +
65 +$| = 1;
66 +
67 +my $md4 = Digest::MD4->new;
68 +my $md5 = Digest::MD5->new;
69 +
70 +while (@dirs) {
71 +    my $dir = shift @dirs;
72 +
73 +    if (!chdir($dir)) {
74 +       warn "Unable to chdir to $dir: $!\n";
75 +       next;
76 +    }
77 +    if (!opendir(DP, '.')) {
78 +       warn "Unable to opendir $dir: $!\n";
79 +       next;
80 +    }
81 +
82 +    if ($verbosity) {
83 +       my $reldir = $dir;
84 +       $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
85 +       print "scanning $reldir\n";
86 +    }
87 +
88 +    my @subdirs;
89 +    while (defined(my $fn = readdir(DP))) {
90 +       next if $fn =~ /^\.\.?$/ || -l $fn;
91 +       if (-d _) {
92 +           push(@subdirs, "$dir/$fn");
93 +           next;
94 +       }
95 +       next unless -f _;
96 +
97 +       my($size,$mtime) = (stat(_))[7,9];
98 +
99 +       my $sum4 = getfattr($fn, 'rsync.%md4');
100 +       my $sum5 = getfattr($fn, 'rsync.%md5');
101 +
102 +       foreach ($sum4, $sum5) {
103 +           if (defined $_) {
104 +               if (length($_) == 24) {
105 +                   my($sz,$mt,$sum) = unpack('V2a16', $_);
106 +                   if ($sz != ($size & 0xFFFFFFFF)
107 +                    || $mt != ($mtime & 0xFFFFFFFF)) {
108 +                       $_ = undef;
109 +                   } else {
110 +                       $_ = $sum;
111 +                   }
112 +               } else {
113 +                   $_ = undef;
114 +               }
115 +           }
116 +       }
117 +       if (!defined($sum4) || !defined($sum5)) {
118 +           if (!open(IN, $fn)) {
119 +               print STDERR "Unable to read $fn: $!\n";
120 +               next;
121 +           }
122 +
123 +           while (sysread(IN, $_, 64*1024)) {
124 +               $md4->add($_);
125 +               $md5->add($_);
126 +           }
127 +           close IN;
128 +
129 +           $sum4 = $md4->digest;
130 +           $sum5 = $md5->digest;
131 +           print " $fn\n" if $verbosity > 1;
132 +
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.
137 +       }
138 +    }
139 +
140 +    closedir DP;
141 +
142 +    unshift(@dirs, sort @subdirs) if $recurse_opt;
143 +}
144 +
145 +sub usage
146 +{
147 +    die <<EOT;
148 +Usage: rsyncsums [OPTIONS] [DIRS]
149 +
150 +Options:
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.
154 +EOT
155 +}
156 --- old/xattrs.c
157 +++ new/xattrs.c
158 @@ -31,6 +31,8 @@ extern int am_generator;
159  extern int read_only;
160  extern int list_only;
161  extern int checksum_seed;
162 +extern int checksum_len;
163 +extern int protocol_version;
164  
165  #define RSYNC_XAL_INITIAL 5
166  #define RSYNC_XAL_LIST_INITIAL 100
167 @@ -62,7 +64,8 @@ extern int checksum_seed;
168  #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
169  
170  #define XSTAT_ATTR RSYNC_PREFIX "%stat"
171 -#define XSTAT_LEN ((int)sizeof XSTAT_ATTR - 1)
172 +#define MD4_ATTR RSYNC_PREFIX "%md4"
173 +#define MD5_ATTR RSYNC_PREFIX "%md5"
174  
175  typedef struct {
176         char *datum, *name;
177 @@ -223,8 +226,8 @@ static int rsync_xal_get(const char *fna
178                         continue;
179  #endif
180  
181 -               if (am_root < 0 && name_len == XSTAT_LEN + 1
182 -                && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0)
183 +               if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
184 +                && HAS_PREFIX(name, RSYNC_PREFIX))
185                         continue;
186  
187                 datum_len = name_len; /* Pass extra size to get_xattr_data() */
188 @@ -636,8 +639,8 @@ void receive_xattr(struct file_struct *f
189                         continue;
190                 }
191  #endif
192 -               if (am_root < 0 && name_len == XSTAT_LEN + 1
193 -                && name[RPRE_LEN] == '%' && strcmp(name, XSTAT_ATTR) == 0) {
194 +               if (name_len > RPRE_LEN && name[RPRE_LEN] == '%'
195 +                && HAS_PREFIX(name, RSYNC_PREFIX)) {
196                         free(ptr);
197                         continue;
198                 }
199 @@ -795,6 +798,39 @@ int set_xattr(const char *fname, const s
200         return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
201  }
202  
203 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
204 +{
205 +       const char *mdattr = protocol_version >= 30
206 +                          ? MD5_ATTR : MD4_ATTR;
207 +       char buf[256];
208 +       uint32 file_length, mtime;
209 +       int len;
210 +       
211 +       len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
212 +       if (len < 0) {
213 +               if (errno == ENOTSUP || errno == ENOATTR)
214 +                       return 0;
215 +               rsyserr(FERROR, errno, "failed to read xattr %s for %s",
216 +                       mdattr, full_fname(fname));
217 +               return 0;
218 +       }
219 +       if (len != 4 + 4 + checksum_len) {
220 +               rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
221 +                       mdattr, full_fname(fname));
222 +               return 0;
223 +       }
224 +
225 +       file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
226 +       mtime = IVAL(buf, 4);
227 +
228 +       if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
229 +               return 0;
230 +
231 +       memcpy(sum, buf + 8, checksum_len);
232 +
233 +       return 1;
234 +}
235 +
236  int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
237  {
238         int mode, rdev_major, rdev_minor, uid, gid, len;