The patches for 3.0.0pre6.
[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 --- a/flist.c
13 +++ b/flist.c
14 @@ -1211,7 +1211,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
15         }
16  #endif
17  
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);
22  
23         F_PATHNAME(file) = pathname;
24 diff --git a/generator.c b/generator.c
25 --- a/generator.c
26 +++ b/generator.c
27 @@ -637,7 +637,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;
35         }
36  
37 diff --git a/support/xsums b/support/xsums
38 new file mode 100644
39 --- /dev/null
40 +++ b/support/xsums
41 @@ -0,0 +1,118 @@
42 +#!/usr/bin/perl -w
43 +use strict;
44 +
45 +use Getopt::Long;
46 +use Cwd qw(abs_path cwd);
47 +use Digest::MD4;
48 +use Digest::MD5;
49 +use File::ExtAttr ':all';
50 +
51 +our($recurse_opt, $help_opt);
52 +our $verbosity = 0;
53 +
54 +&Getopt::Long::Configure('bundling');
55 +&usage if !&GetOptions(
56 +    'recurse|r' => \$recurse_opt,
57 +    'verbose|v+' => \$verbosity,
58 +    'help|h' => \$help_opt,
59 +) || $help_opt;
60 +
61 +my $start_dir = cwd();
62 +
63 +my @dirs = @ARGV;
64 +@dirs = '.' unless @dirs;
65 +foreach (@dirs) {
66 +    $_ = abs_path($_);
67 +}
68 +
69 +$| = 1;
70 +
71 +my $md4 = Digest::MD4->new;
72 +my $md5 = Digest::MD5->new;
73 +
74 +while (@dirs) {
75 +    my $dir = shift @dirs;
76 +
77 +    if (!chdir($dir)) {
78 +       warn "Unable to chdir to $dir: $!\n";
79 +       next;
80 +    }
81 +    if (!opendir(DP, '.')) {
82 +       warn "Unable to opendir $dir: $!\n";
83 +       next;
84 +    }
85 +
86 +    if ($verbosity) {
87 +       my $reldir = $dir;
88 +       $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
89 +       print "scanning $reldir\n";
90 +    }
91 +
92 +    my @subdirs;
93 +    while (defined(my $fn = readdir(DP))) {
94 +       next if $fn =~ /^\.\.?$/ || -l $fn;
95 +       if (-d _) {
96 +           push(@subdirs, "$dir/$fn");
97 +           next;
98 +       }
99 +       next unless -f _;
100 +
101 +       my($size,$mtime) = (stat(_))[7,9];
102 +
103 +       my $sum4 = getfattr($fn, 'rsync.%md4');
104 +       my $sum5 = getfattr($fn, 'rsync.%md5');
105 +
106 +       foreach ($sum4, $sum5) {
107 +           if (defined $_) {
108 +               if (length($_) == 24) {
109 +                   my($sz,$mt,$sum) = unpack('V2a16', $_);
110 +                   if ($sz != ($size & 0xFFFFFFFF)
111 +                    || $mt != ($mtime & 0xFFFFFFFF)) {
112 +                       $_ = undef;
113 +                   } else {
114 +                       $_ = $sum;
115 +                   }
116 +               } else {
117 +                   $_ = undef;
118 +               }
119 +           }
120 +       }
121 +       if (!defined($sum4) || !defined($sum5)) {
122 +           if (!open(IN, $fn)) {
123 +               print STDERR "Unable to read $fn: $!\n";
124 +               next;
125 +           }
126 +
127 +           while (sysread(IN, $_, 64*1024)) {
128 +               $md4->add($_);
129 +               $md5->add($_);
130 +           }
131 +           close IN;
132 +
133 +           $sum4 = $md4->digest;
134 +           $sum5 = $md5->digest;
135 +           print " $fn\n" if $verbosity > 1;
136 +
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.
141 +       }
142 +    }
143 +
144 +    closedir DP;
145 +
146 +    unshift(@dirs, sort @subdirs) if $recurse_opt;
147 +}
148 +
149 +sub usage
150 +{
151 +    die <<EOT;
152 +Usage: rsyncsums [OPTIONS] [DIRS]
153 +
154 +Options:
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.
158 +EOT
159 +}
160 diff --git a/xattrs.c b/xattrs.c
161 --- a/xattrs.c
162 +++ 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;
169  
170  #define RSYNC_XAL_INITIAL 5
171  #define RSYNC_XAL_LIST_INITIAL 100
172 @@ -66,6 +68,8 @@ extern int checksum_seed;
173  #define XSTAT_ATTR RSYNC_PREFIX "%stat"
174  #define XACC_ACL_ATTR RSYNC_PREFIX "%aacl"
175  #define XDEF_ACL_ATTR RSYNC_PREFIX "%dacl"
176 +#define MD4_ATTR RSYNC_PREFIX "%md4"
177 +#define MD5_ATTR RSYNC_PREFIX "%md5"
178  
179  typedef struct {
180         char *datum, *name;
181 @@ -841,6 +845,39 @@ int del_def_xattr_acl(const char *fname)
182  }
183  #endif
184  
185 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
186 +{
187 +       const char *mdattr = protocol_version >= 30
188 +                          ? MD5_ATTR : MD4_ATTR;
189 +       char buf[256];
190 +       uint32 file_length, mtime;
191 +       int len;
192 +       
193 +       len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
194 +       if (len < 0) {
195 +               if (errno == ENOTSUP || errno == ENOATTR)
196 +                       return 0;
197 +               rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
198 +                       mdattr, full_fname(fname));
199 +               return 0;
200 +       }
201 +       if (len != 4 + 4 + checksum_len) {
202 +               rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
203 +                       mdattr, full_fname(fname));
204 +               return 0;
205 +       }
206 +
207 +       file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
208 +       mtime = IVAL(buf, 4);
209 +
210 +       if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
211 +               return 0;
212 +
213 +       memcpy(sum, buf + 8, checksum_len);
214 +
215 +       return 1;
216 +}
217 +
218  int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
219  {
220         int mode, rdev_major, rdev_minor, uid, gid, len;