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