Added the munge-links.diff patch.
[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 @@ -1250,7 +1250,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
15                 memcpy(bp + basename_len, linkname, linkname_len);
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         if (am_sender)
24 diff --git a/generator.c b/generator.c
25 --- a/generator.c
26 +++ b/generator.c
27 @@ -721,7 +721,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 @@ -68,6 +70,10 @@ extern int checksum_seed;
173  #define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
174  #define XDEF_ACL_SUFFIX "dacl"
175  #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
176 +#define MD4_SUFFIX "md4"
177 +#define MD4_ATTR RSYNC_PREFIX "%" MD4_SUFFIX
178 +#define MD5_SUFFIX "md5"
179 +#define MD5_ATTR RSYNC_PREFIX "%" MD5_SUFFIX
180  
181  typedef struct {
182         char *datum, *name;
183 @@ -238,7 +244,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
184                          || (am_root < 0
185                           && (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
186                            || strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
187 -                          || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
188 +                          || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0
189 +                          || strcmp(name+RPRE_LEN+1, MD4_SUFFIX) == 0
190 +                          || strcmp(name+RPRE_LEN+1, MD5_SUFFIX) == 0)))
191                                 continue;
192                 }
193  
194 @@ -894,6 +902,39 @@ int del_def_xattr_acl(const char *fname)
195  }
196  #endif
197  
198 +int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
199 +{
200 +       const char *mdattr = protocol_version >= 30
201 +                          ? MD5_ATTR : MD4_ATTR;
202 +       char buf[256];
203 +       uint32 file_length, mtime;
204 +       int len;
205 +
206 +       len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
207 +       if (len < 0) {
208 +               if (errno == ENOTSUP || errno == ENOATTR)
209 +                       return 0;
210 +               rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
211 +                       mdattr, full_fname(fname));
212 +               return 0;
213 +       }
214 +       if (len != 4 + 4 + checksum_len) {
215 +               rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
216 +                       mdattr, full_fname(fname));
217 +               return 0;
218 +       }
219 +
220 +       file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
221 +       mtime = IVAL(buf, 4);
222 +
223 +       if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
224 +               return 0;
225 +
226 +       memcpy(sum, buf + 8, checksum_len);
227 +
228 +       return 1;
229 +}
230 +
231  int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
232  {
233         int mode, rdev_major, rdev_minor, uid, gid, len;