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