-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchecksum-xattr.diff
320 lines (311 loc) · 8.01 KB
/
checksum-xattr.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
This patch is the start of storing/using checksum information from
extended attribute values. The rsync code only reads the values
at the moment. There is also a perl script that can create them.
To use this patch, run these commands for a successful build:
patch -p1 <patches/checksum-xattr.diff
./configure (optional if already run)
make
based-on: ed4b3448be243b1bdb30a5da811f1e217f5a0601
diff --git a/flist.c b/flist.c
--- a/flist.c
+++ b/flist.c
@@ -1410,7 +1410,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
#endif
if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
- file_checksum(thisname, &st, tmp_sum);
+ if (!get_sum_xattr(thisname, &st, tmp_sum))
+ file_checksum(thisname, &st, tmp_sum);
if (sender_keeps_checksum)
extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
}
diff --git a/generator.c b/generator.c
--- a/generator.c
+++ b/generator.c
@@ -625,7 +625,8 @@ int quick_check_ok(enum filetype ftype, const char *fn, struct file_struct *file
* of the file mtime to determine whether to sync. */
if (always_checksum > 0) {
char sum[MAX_DIGEST_LEN];
- file_checksum(fn, st, sum);
+ if (!get_sum_xattr(fn, st, sum))
+ file_checksum(fn, st, sum);
return memcmp(sum, F_SUM(file), flist_csum_len) == 0;
}
diff --git a/support/xsums b/support/xsums
new file mode 100755
--- /dev/null
+++ b/support/xsums
@@ -0,0 +1,204 @@
+#!/usr/bin/perl -w
+use strict;
+
+use Getopt::Long;
+use Cwd qw(abs_path cwd);
+use Digest::MD4;
+use Digest::MD5;
+use File::ExtAttr ':all';
+
+&Getopt::Long::Configure('bundling');
+&usage if !&GetOptions(
+ 'recurse|r' => \( my $recurse_opt ),
+ 'list|l' => \( my $list_opt ),
+ 'check|c' => \( my $check_opt ),
+ 'verbose|v+' => \( my $verbosity = 0 ),
+ 'help|h' => \( my $help_opt ),
+);
+&usage if $help_opt;
+
+my $start_dir = cwd();
+
+my @dirs = @ARGV;
+@dirs = '.' unless @dirs;
+foreach (@dirs) {
+ $_ = abs_path($_);
+}
+
+$| = 1;
+
+my $exit_code = 0;
+
+my $md4 = Digest::MD4->new;
+my $md5 = Digest::MD5->new;
+
+while (@dirs) {
+ my $dir = shift @dirs;
+
+ if (!chdir($dir)) {
+ warn "Unable to chdir to $dir: $!\n";
+ next;
+ }
+ if (!opendir(DP, '.')) {
+ warn "Unable to opendir $dir: $!\n";
+ next;
+ }
+
+ my $reldir = $dir;
+ $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
+ print "$reldir ... " if $verbosity;
+
+ my @subdirs;
+ my $d_cnt = 0;
+ my $need_newline = $verbosity;
+ while (defined(my $fn = readdir(DP))) {
+ next if $fn =~ /^\.\.?$/ || -l $fn;
+ if (-d _) {
+ push(@subdirs, "$dir/$fn");
+ next;
+ }
+ next unless -f _;
+ $d_cnt++;
+
+ my($size,$mtime,$ctime) = (stat(_))[7,9,10];
+
+ my $xsum4 = getfattr($fn, 'rsync.%md4');
+ my $xsum5 = getfattr($fn, 'rsync.%md5');
+
+ my $sum_count = 0;
+ foreach ($xsum4, $xsum5) {
+ if (defined $_) {
+ if (length($_) == 24) {
+ my($sz,$mt,$sum) = unpack('V2a16', $_);
+ if ($sz != ($size & 0xFFFFFFFF)
+ || $mt != ($mtime & 0xFFFFFFFF)) {
+ $_ = undef;
+ } else {
+ $_ = $sum;
+ $sum_count++;
+ }
+ } else {
+ $_ = undef;
+ }
+ }
+ }
+
+ if ($list_opt) {
+ if ($need_newline) {
+ print "\n";
+ $need_newline = 0;
+ }
+ if (defined $xsum4) {
+ print ' ', unpack('H32', $xsum4);
+ } else {
+ print ' ' x (1 + 32);
+ }
+ if (defined $xsum5) {
+ print ' ', unpack('H32', $xsum5);
+ } else {
+ print ' ' x (1 + 32);
+ }
+ print $verbosity ? ' ' : " $reldir/";
+ print $fn, "\n";
+ next;
+ }
+
+ if ($check_opt) {
+ if (!$sum_count) {
+ if ($need_newline) {
+ print "\n";
+ $need_newline = 0;
+ }
+ print ' ' x (1 + 32 + 1 + 32) if $verbosity > 2;
+ print $verbosity ? ' ' : "$reldir/";
+ print $fn, " MISSING\n";
+ next;
+ }
+ } else {
+ next if $sum_count == 2;
+ print 'UPDATING' if $need_newline && $verbosity == 1;
+ }
+
+ if ($need_newline && (!$check_opt || $verbosity > 1)) {
+ print "\n";
+ $need_newline = 0;
+ }
+
+ if (!open(IN, $fn)) {
+ print STDERR "Unable to read $fn: $!\n";
+ next;
+ }
+
+ my($sum4, $sum5);
+ while (1) {
+ while (sysread(IN, $_, 64*1024)) {
+ $md4->add($_);
+ $md5->add($_);
+ }
+ $sum4 = $md4->digest;
+ $sum5 = $md5->digest;
+ print ' ', unpack('H32', $sum4), ' ', unpack('H32', $sum5) if $verbosity > 2;
+ print " $fn" if $verbosity > 1;
+ my($size2,$mtime2,$ctime2) = (stat(IN))[7,9,10];
+ last if $size == $size2 && $mtime == $mtime2 && $ctime == $ctime2;
+ $size = $size2;
+ $mtime = $mtime2;
+ $ctime = $ctime2;
+ sysseek(IN, 0, 0);
+ print " REREADING\n" if $verbosity > 1;
+ }
+
+ close IN;
+
+ if ($check_opt) {
+ if ((!defined $xsum4 || $xsum4 eq $sum4) && (!defined $xsum5 || $xsum5 eq $sum5)) {
+ print " OK\n" if $verbosity > 1;
+ next;
+ }
+ if ($need_newline) {
+ print "\n";
+ $need_newline = 0;
+ }
+ if ($verbosity < 2) {
+ print $verbosity ? ' ' : "$reldir/";
+ print $fn;
+ }
+ print " FAILED\n";
+ $exit_code = 1;
+ } else {
+ print "\n" if $verbosity > 1;
+ my $szmt = pack('V2', $size, $mtime); # 32-bits, may truncate
+ setfattr($fn, 'rsync.%md4', $szmt.$sum4);
+ setfattr($fn, 'rsync.%md5', $szmt.$sum5);
+ #utime $mtime, $mtime, $fn; # Set mtime if it changes.
+ }
+ }
+
+ if ($need_newline) {
+ if ($d_cnt) {
+ print "ok\n";
+ } else {
+ print "empty\n";
+ }
+ }
+
+ closedir DP;
+
+ unshift(@dirs, sort @subdirs) if $recurse_opt;
+}
+
+exit $exit_code;
+
+sub usage
+{
+ die <<EOT;
+Usage: rsyncsums [OPTIONS] [DIRS]
+
+Options:
+ -r, --recurse Update checksums in subdirectories too.
+ -l, --list List the checksums for each file (doesn't update).
+ -c, --check Check if the checksums are right (doesn't update).
+ -v, --verbose Mention what we're doing. Repeat for more info.
+ -h, --help Display this help message.
+EOT
+}
diff --git a/xattrs.c b/xattrs.c
--- a/xattrs.c
+++ b/xattrs.c
@@ -36,7 +36,9 @@ extern int preserve_xattrs;
extern int preserve_links;
extern int preserve_devices;
extern int preserve_specials;
+extern int checksum_type;
extern int checksum_seed;
+extern int flist_csum_len;
extern int saw_xattr_filter;
extern struct name_num_item *xattr_sum_nni;
@@ -76,6 +78,10 @@ extern int xattr_sum_len;
#define XACC_ACL_ATTR RSYNC_PREFIX "%" XACC_ACL_SUFFIX
#define XDEF_ACL_SUFFIX "dacl"
#define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
+#define MD4_SUFFIX "md4"
+#define MD4_ATTR RSYNC_PREFIX "%" MD4_SUFFIX
+#define MD5_SUFFIX "md5"
+#define MD5_ATTR RSYNC_PREFIX "%" MD5_SUFFIX
typedef struct {
char *datum, *name;
@@ -263,7 +269,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
|| (am_root < 0
&& (strcmp(name+RPRE_LEN+1, XSTAT_SUFFIX) == 0
|| strcmp(name+RPRE_LEN+1, XACC_ACL_SUFFIX) == 0
- || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0)))
+ || strcmp(name+RPRE_LEN+1, XDEF_ACL_SUFFIX) == 0
+ || strcmp(name+RPRE_LEN+1, MD4_SUFFIX) == 0
+ || strcmp(name+RPRE_LEN+1, MD5_SUFFIX) == 0)))
continue;
}
@@ -1124,6 +1132,38 @@ int del_def_xattr_acl(const char *fname)
}
#endif
+int get_sum_xattr(const char *fname, STRUCT_STAT *stp, char *sum)
+{
+ const char *mdattr = checksum_type == 5 ? MD5_ATTR : MD4_ATTR;
+ char buf[256];
+ uint32 file_length, mtime;
+ int len;
+
+ len = sys_lgetxattr(fname, mdattr, buf, sizeof buf);
+ if (len < 0) {
+ if (errno == ENOTSUP || errno == ENOATTR)
+ return 0;
+ rsyserr(FERROR_XFER, errno, "failed to read xattr %s for %s",
+ mdattr, full_fname(fname));
+ return 0;
+ }
+ if (len != 4 + 4 + flist_csum_len) {
+ rprintf(FERROR, "Corrupt %s xattr attached to %s -- skipping\n",
+ mdattr, full_fname(fname));
+ return 0;
+ }
+
+ file_length = IVAL(buf, 0); /* 32-bit values -- trunctions are OK */
+ mtime = IVAL(buf, 4);
+
+ if ((uint32)stp->st_size != file_length || (uint32)stp->st_mtime != mtime)
+ return 0;
+
+ memcpy(sum, buf + 8, flist_csum_len);
+
+ return 1;
+}
+
int get_stat_xattr(const char *fname, int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
{
unsigned int mode;