Fixed misplaced hunk.
[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 @@ -32,6 +32,8 @@ extern int read_only;
159  extern int list_only;
160  extern int preserve_xattrs;
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 @@ -63,6 +65,8 @@ extern int checksum_seed;
168  #define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
169  
170  #define XSTAT_ATTR RSYNC_PREFIX "%stat"
171 +#define MD4_ATTR RSYNC_PREFIX "%md4"
172 +#define MD5_ATTR RSYNC_PREFIX "%md5"
173  
174  typedef struct {
175         char *datum, *name;
176 @@ -797,6 +801,39 @@ int set_xattr(const char *fname, const s
177         return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
178  }
179  
180 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
181 +{
182 +       const char *mdattr = protocol_version >= 30
183 +                          ? MD5_ATTR : MD4_ATTR;
184 +       char buf[256];
185 +       uint32 file_length, mtime;
186 +       int len;
187 +       
188 +       len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
189 +       if (len < 0) {
190 +               if (errno == ENOTSUP || errno == ENOATTR)
191 +                       return 0;
192 +               rsyserr(FERROR, errno, "failed to read xattr %s for %s",
193 +                       mdattr, full_fname(fname));
194 +               return 0;
195 +       }
196 +       if (len != 4 + 4 + checksum_len) {
197 +               rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
198 +                       mdattr, full_fname(fname));
199 +               return 0;
200 +       }
201 +
202 +       file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
203 +       mtime = IVAL(buf, 4);
204 +
205 +       if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
206 +               return 0;
207 +
208 +       memcpy(sum, buf + 8, checksum_len);
209 +
210 +       return 1;
211 +}
212 +
213  int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
214  {
215         int mode, rdev_major, rdev_minor, uid, gid, len;