Document the "copy-some-dirlinks" trick in the man page.
[rsync/rsync.git] / token.c
CommitLineData
d67c8bdf 1/*
0f78b815
WD
2 * Routines used by the file-transfer code.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
b3bf9b9d 6 * Copyright (C) 2003-2009 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
8e41b68e
WD
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
0f78b815
WD
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
70d794dc
AT
21
22#include "rsync.h"
5dd14f0c 23#include "itypes.h"
5914bf15 24#include "zlib/zlib.h"
70d794dc
AT
25
26extern int do_compression;
d67c8bdf 27extern int module_id;
e8a8167a 28extern int def_compress_level;
6e058b4b 29extern char *skip_compress;
d67c8bdf 30
ec497df1 31static int compression_level, per_file_default_level;
70d794dc 32
6e058b4b
WD
33struct suffix_tree {
34 struct suffix_tree *sibling;
35 struct suffix_tree *child;
36 char letter, word_end;
37};
38
39static char *match_list;
40static struct suffix_tree *suftree;
41
42static void add_suffix(struct suffix_tree **prior, char ltr, const char *str)
83fff1aa 43{
6e058b4b 44 struct suffix_tree *node, *newnode;
83fff1aa 45
6e058b4b
WD
46 if (ltr == '[') {
47 const char *after = strchr(str, ']');
2f1fb732
WD
48 /* Treat "[foo" and "[]" as having a literal '['. */
49 if (after && after++ != str+1) {
50 while ((ltr = *str++) != ']')
51 add_suffix(prior, ltr, after);
6e058b4b 52 return;
2f1fb732 53 }
6e058b4b 54 }
83fff1aa 55
6e058b4b
WD
56 for (node = *prior; node; prior = &node->sibling, node = node->sibling) {
57 if (node->letter == ltr) {
58 if (*str)
59 add_suffix(&node->child, *str, str+1);
60 else
61 node->word_end = 1;
62 return;
ec497df1 63 }
6e058b4b
WD
64 if (node->letter > ltr)
65 break;
66 }
67 if (!(newnode = new(struct suffix_tree)))
68 out_of_memory("add_suffix");
69 newnode->sibling = node;
70 newnode->child = NULL;
71 newnode->letter = ltr;
72 *prior = newnode;
73 if (*str) {
74 add_suffix(&newnode->child, *str, str+1);
75 newnode->word_end = 0;
76 } else
77 newnode->word_end = 1;
78}
79
80static void add_nocompress_suffixes(const char *str)
81{
82 char *buf, *t;
83 const char *f = str;
84
85 if (!(buf = new_array(char, strlen(f) + 1)))
86 out_of_memory("add_nocompress_suffixes");
87
88 while (*f) {
89 if (*f == '/') {
90 f++;
91 continue;
92 }
93
94 t = buf;
95 do {
96 if (isUpper(f))
97 *t++ = toLower(f);
98 else
99 *t++ = *f;
100 } while (*++f != '/' && *f);
101 *t++ = '\0';
102
6e058b4b
WD
103 add_suffix(&suftree, *buf, buf+1);
104 }
105
106 free(buf);
107}
108
109static void init_set_compression(void)
110{
111 const char *f;
112 char *t, *start;
113
114 if (skip_compress)
115 add_nocompress_suffixes(skip_compress);
116
117 /* A non-daemon transfer skips the default suffix list if the
118 * user specified --skip-compress. */
119 if (skip_compress && module_id < 0)
120 f = "";
121 else
122 f = lp_dont_compress(module_id);
123
124 if (!(match_list = t = new_array(char, strlen(f) + 2)))
125 out_of_memory("set_compression");
126
127 per_file_default_level = def_compress_level;
128
129 while (*f) {
130 if (*f == ' ') {
131 f++;
132 continue;
133 }
134
135 start = t;
136 do {
137 if (isUpper(f))
138 *t++ = toLower(f);
139 else
140 *t++ = *f;
141 } while (*++f != ' ' && *f);
ec497df1 142 *t++ = '\0';
6e058b4b
WD
143
144 if (t - start == 1+1 && *start == '*') {
145 /* Optimize a match-string of "*". */
146 *match_list = '\0';
147 suftree = NULL;
148 per_file_default_level = 0;
149 break;
150 }
151
152 /* Move *.foo items into the stuffix tree. */
153 if (*start == '*' && start[1] == '.' && start[2]
0d585188 154 && !strpbrk(start+2, ".?*")) {
6e058b4b
WD
155 add_suffix(&suftree, start[2], start+3);
156 t = start;
157 }
63f0774f 158 }
6e058b4b
WD
159 *t++ = '\0';
160}
161
162/* determine the compression level based on a wildcard filename list */
163void set_compression(const char *fname)
164{
165 const struct suffix_tree *node;
166 const char *s;
167 char ltr;
168
169 if (!do_compression)
170 return;
171
172 if (!match_list)
173 init_set_compression();
63f0774f 174
ec497df1 175 compression_level = per_file_default_level;
0fe987e2 176
6e058b4b 177 if (!*match_list && !suftree)
d67c8bdf 178 return;
83fff1aa 179
ec497df1
WD
180 if ((s = strrchr(fname, '/')) != NULL)
181 fname = s + 1;
83fff1aa 182
ec497df1
WD
183 for (s = match_list; *s; s += strlen(s) + 1) {
184 if (iwildmatch(s, fname)) {
83fff1aa 185 compression_level = 0;
6e058b4b
WD
186 return;
187 }
188 }
189
190 if (!(node = suftree) || !(s = strrchr(fname, '.'))
191 || s == fname || !(ltr = *++s))
192 return;
193
194 while (1) {
195 while (node->letter != ltr) {
196 if (node->letter > ltr)
197 return;
198 if (!(node = node->sibling))
199 return;
200 }
201 if ((ltr = *++s) == '\0') {
202 if (node->word_end)
203 compression_level = 0;
204 return;
83fff1aa 205 }
6e058b4b
WD
206 if (!(node = node->child))
207 return;
83fff1aa 208 }
83fff1aa 209}
70d794dc
AT
210
211/* non-compressing recv token */
7fcbf9e4 212static int32 simple_recv_token(int f, char **data)
70d794dc 213{
acc461c7 214 static int32 residue;
c5eb3650 215 static char *buf;
7fcbf9e4 216 int32 n;
70d794dc 217
c5eb3650 218 if (!buf) {
58cadc86 219 buf = new_array(char, CHUNK_SIZE);
d67c8bdf
WD
220 if (!buf)
221 out_of_memory("simple_recv_token");
c5eb3650 222 }
70d794dc 223
c5eb3650 224 if (residue == 0) {
7fcbf9e4 225 int32 i = read_int(f);
d67c8bdf
WD
226 if (i <= 0)
227 return i;
c5eb3650
AT
228 residue = i;
229 }
70d794dc 230
c5eb3650
AT
231 *data = buf;
232 n = MIN(CHUNK_SIZE,residue);
233 residue -= n;
234 read_buf(f,buf,n);
235 return n;
70d794dc
AT
236}
237
70d794dc 238/* non-compressing send token */
acc461c7 239static void simple_send_token(int f, int32 token, struct map_struct *buf,
7fcbf9e4 240 OFF_T offset, int32 n)
70d794dc 241{
c5eb3650 242 if (n > 0) {
7fcbf9e4
WD
243 int32 len = 0;
244 while (len < n) {
245 int32 n1 = MIN(CHUNK_SIZE, n-len);
246 write_int(f, n1);
247 write_buf(f, map_ptr(buf, offset+len, n1), n1);
248 len += n1;
c5eb3650
AT
249 }
250 }
45f133b9 251 /* a -2 token means to send data only and no token */
7fcbf9e4
WD
252 if (token != -2)
253 write_int(f, -(token+1));
70d794dc
AT
254}
255
861c20b4
PM
256/* Flag bytes in compressed stream are encoded as follows: */
257#define END_FLAG 0 /* that's all folks */
258#define TOKEN_LONG 0x20 /* followed by 32-bit token number */
259#define TOKENRUN_LONG 0x21 /* ditto with 16-bit run count */
260#define DEFLATED_DATA 0x40 /* + 6-bit high len, then low len byte */
261#define TOKEN_REL 0x80 /* + 6-bit relative token number */
262#define TOKENRUN_REL 0xc0 /* ditto with 16-bit run count */
263
264#define MAX_DATA_COUNT 16383 /* fit 14 bit count into 2 bytes with flags */
265
24733919
WD
266/* zlib.h says that if we want to be able to compress something in a single
267 * call, avail_out must be at least 0.1% larger than avail_in plus 12 bytes.
268 * We'll add in 0.1%+16, just to be safe (and we'll avoid floating point,
269 * to ensure that this is a compile-time value). */
270#define AVAIL_OUT_SIZE(avail_in_size) ((avail_in_size)*1001/1000+16)
271
861c20b4 272/* For coding runs of tokens */
acc461c7
WD
273static int32 last_token = -1;
274static int32 run_start;
275static int32 last_run_end;
861c20b4
PM
276
277/* Deflation state */
278static z_stream tx_strm;
279
280/* Output buffer */
3a6a366f 281static char *obuf;
24733919
WD
282
283/* We want obuf to be able to hold both MAX_DATA_COUNT+2 bytes as well as
284 * AVAIL_OUT_SIZE(CHUNK_SIZE) bytes, so make sure that it's large enough. */
285#if MAX_DATA_COUNT+2 > AVAIL_OUT_SIZE(CHUNK_SIZE)
286#define OBUF_SIZE (MAX_DATA_COUNT+2)
287#else
288#define OBUF_SIZE AVAIL_OUT_SIZE(CHUNK_SIZE)
289#endif
861c20b4
PM
290
291/* Send a deflated token */
292static void
acc461c7 293send_deflated_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
7fcbf9e4 294 int32 nb, int32 toklen)
861c20b4 295{
7fcbf9e4 296 int32 n, r;
5914bf15
PM
297 static int init_done, flush_pending;
298
299 if (last_token == -1) {
300 /* initialization */
301 if (!init_done) {
302 tx_strm.next_in = NULL;
303 tx_strm.zalloc = NULL;
304 tx_strm.zfree = NULL;
83fff1aa 305 if (deflateInit2(&tx_strm, compression_level,
5914bf15
PM
306 Z_DEFLATED, -15, 8,
307 Z_DEFAULT_STRATEGY) != Z_OK) {
308 rprintf(FERROR, "compression init failed\n");
e4c598c8 309 exit_cleanup(RERR_PROTOCOL);
5914bf15 310 }
58cadc86 311 if ((obuf = new_array(char, OBUF_SIZE)) == NULL)
5914bf15
PM
312 out_of_memory("send_deflated_token");
313 init_done = 1;
314 } else
315 deflateReset(&tx_strm);
316 last_run_end = 0;
317 run_start = token;
318 flush_pending = 0;
5914bf15
PM
319 } else if (last_token == -2) {
320 run_start = token;
5914bf15
PM
321 } else if (nb != 0 || token != last_token + 1
322 || token >= run_start + 65536) {
323 /* output previous run */
324 r = run_start - last_run_end;
325 n = last_token - run_start;
326 if (r >= 0 && r <= 63) {
327 write_byte(f, (n==0? TOKEN_REL: TOKENRUN_REL) + r);
328 } else {
329 write_byte(f, (n==0? TOKEN_LONG: TOKENRUN_LONG));
330 write_int(f, run_start);
331 }
332 if (n != 0) {
333 write_byte(f, n);
334 write_byte(f, n >> 8);
335 }
336 last_run_end = last_token;
337 run_start = token;
861c20b4 338 }
5914bf15
PM
339
340 last_token = token;
341
342 if (nb != 0 || flush_pending) {
343 /* deflate the data starting at offset */
344 int flush = Z_NO_FLUSH;
345 tx_strm.avail_in = 0;
346 tx_strm.avail_out = 0;
347 do {
348 if (tx_strm.avail_in == 0 && nb != 0) {
349 /* give it some more input */
350 n = MIN(nb, CHUNK_SIZE);
351 tx_strm.next_in = (Bytef *)
352 map_ptr(buf, offset, n);
353 tx_strm.avail_in = n;
354 nb -= n;
355 offset += n;
356 }
357 if (tx_strm.avail_out == 0) {
358 tx_strm.next_out = (Bytef *)(obuf + 2);
359 tx_strm.avail_out = MAX_DATA_COUNT;
360 if (flush != Z_NO_FLUSH) {
361 /*
362 * We left the last 4 bytes in the
363 * buffer, in case they are the
364 * last 4. Move them to the front.
365 */
366 memcpy(tx_strm.next_out,
367 obuf+MAX_DATA_COUNT-2, 4);
368 tx_strm.next_out += 4;
369 tx_strm.avail_out -= 4;
370 }
371 }
372 if (nb == 0 && token != -2)
373 flush = Z_SYNC_FLUSH;
374 r = deflate(&tx_strm, flush);
375 if (r != Z_OK) {
376 rprintf(FERROR, "deflate returned %d\n", r);
65417579 377 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
378 }
379 if (nb == 0 || tx_strm.avail_out == 0) {
380 n = MAX_DATA_COUNT - tx_strm.avail_out;
381 if (flush != Z_NO_FLUSH) {
382 /*
383 * We have to trim off the last 4
384 * bytes of output when flushing
385 * (they are just 0, 0, ff, ff).
386 */
387 n -= 4;
388 }
389 if (n > 0) {
390 obuf[0] = DEFLATED_DATA + (n >> 8);
391 obuf[1] = n;
392 write_buf(f, obuf, n+2);
393 }
394 }
395 } while (nb != 0 || tx_strm.avail_out == 0);
396 flush_pending = token == -2;
861c20b4 397 }
861c20b4 398
5914bf15
PM
399 if (token == -1) {
400 /* end of file - clean up */
401 write_byte(f, END_FLAG);
5914bf15 402 } else if (token != -2) {
acc461c7
WD
403 /* Add the data in the current block to the compressor's
404 * history and hash table. */
01f439ec
WD
405 do {
406 /* Break up long sections in the same way that
407 * see_deflate_token() does. */
408 int32 n1 = toklen > 0xffff ? 0xffff : toklen;
409 toklen -= n1;
410 tx_strm.next_in = (Bytef *)map_ptr(buf, offset, n1);
411 tx_strm.avail_in = n1;
412 tx_strm.next_out = (Bytef *) obuf;
413 tx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
414 r = deflate(&tx_strm, Z_INSERT_ONLY);
415 if (r != Z_OK || tx_strm.avail_in != 0) {
416 rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n",
417 r, tx_strm.avail_in);
418 exit_cleanup(RERR_STREAMIO);
419 }
420 } while (toklen > 0);
861c20b4 421 }
861c20b4
PM
422}
423
861c20b4
PM
424/* tells us what the receiver is in the middle of doing */
425static enum { r_init, r_idle, r_running, r_inflating, r_inflated } recv_state;
70d794dc 426
861c20b4
PM
427/* for inflating stuff */
428static z_stream rx_strm;
429static char *cbuf;
430static char *dbuf;
431
432/* for decoding runs of tokens */
7fcbf9e4
WD
433static int32 rx_token;
434static int32 rx_run;
861c20b4
PM
435
436/* Receive a deflated token and inflate it */
7fcbf9e4 437static int32 recv_deflated_token(int f, char **data)
861c20b4 438{
5914bf15 439 static int init_done;
acc461c7
WD
440 static int32 saved_flag;
441 int32 n, flag;
442 int r;
5914bf15
PM
443
444 for (;;) {
445 switch (recv_state) {
446 case r_init:
447 if (!init_done) {
448 rx_strm.next_out = NULL;
449 rx_strm.zalloc = NULL;
450 rx_strm.zfree = NULL;
451 if (inflateInit2(&rx_strm, -15) != Z_OK) {
452 rprintf(FERROR, "inflate init failed\n");
e4c598c8 453 exit_cleanup(RERR_PROTOCOL);
5914bf15 454 }
58cadc86
WD
455 if (!(cbuf = new_array(char, MAX_DATA_COUNT))
456 || !(dbuf = new_array(char, AVAIL_OUT_SIZE(CHUNK_SIZE))))
5914bf15
PM
457 out_of_memory("recv_deflated_token");
458 init_done = 1;
459 } else {
460 inflateReset(&rx_strm);
461 }
b8d4524b 462 recv_state = r_idle;
5914bf15
PM
463 rx_token = 0;
464 break;
465
466 case r_idle:
467 case r_inflated:
468 if (saved_flag) {
469 flag = saved_flag & 0xff;
470 saved_flag = 0;
471 } else
472 flag = read_byte(f);
473 if ((flag & 0xC0) == DEFLATED_DATA) {
474 n = ((flag & 0x3f) << 8) + read_byte(f);
475 read_buf(f, cbuf, n);
476 rx_strm.next_in = (Bytef *)cbuf;
477 rx_strm.avail_in = n;
478 recv_state = r_inflating;
479 break;
480 }
481 if (recv_state == r_inflated) {
482 /* check previous inflated stuff ended correctly */
483 rx_strm.avail_in = 0;
484 rx_strm.next_out = (Bytef *)dbuf;
1dbb94ca 485 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
5914bf15 486 r = inflate(&rx_strm, Z_SYNC_FLUSH);
1dbb94ca 487 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
5914bf15
PM
488 /*
489 * Z_BUF_ERROR just means no progress was
490 * made, i.e. the decompressor didn't have
491 * any pending output for us.
492 */
493 if (r != Z_OK && r != Z_BUF_ERROR) {
494 rprintf(FERROR, "inflate flush returned %d (%d bytes)\n",
495 r, n);
65417579 496 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
497 }
498 if (n != 0 && r != Z_BUF_ERROR) {
499 /* have to return some more data and
500 save the flag for later. */
501 saved_flag = flag + 0x10000;
502 *data = dbuf;
503 return n;
504 }
505 /*
506 * At this point the decompressor should
507 * be expecting to see the 0, 0, ff, ff bytes.
508 */
509 if (!inflateSyncPoint(&rx_strm)) {
510 rprintf(FERROR, "decompressor lost sync!\n");
65417579 511 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
512 }
513 rx_strm.avail_in = 4;
514 rx_strm.next_in = (Bytef *)cbuf;
515 cbuf[0] = cbuf[1] = 0;
516 cbuf[2] = cbuf[3] = 0xff;
517 inflate(&rx_strm, Z_SYNC_FLUSH);
518 recv_state = r_idle;
519 }
520 if (flag == END_FLAG) {
521 /* that's all folks */
522 recv_state = r_init;
523 return 0;
524 }
525
526 /* here we have a token of some kind */
527 if (flag & TOKEN_REL) {
528 rx_token += flag & 0x3f;
529 flag >>= 6;
530 } else
531 rx_token = read_int(f);
532 if (flag & 1) {
533 rx_run = read_byte(f);
534 rx_run += read_byte(f) << 8;
535 recv_state = r_running;
536 }
537 return -1 - rx_token;
538
539 case r_inflating:
540 rx_strm.next_out = (Bytef *)dbuf;
1dbb94ca 541 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
5914bf15 542 r = inflate(&rx_strm, Z_NO_FLUSH);
1dbb94ca 543 n = AVAIL_OUT_SIZE(CHUNK_SIZE) - rx_strm.avail_out;
5914bf15
PM
544 if (r != Z_OK) {
545 rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n);
65417579 546 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
547 }
548 if (rx_strm.avail_in == 0)
549 recv_state = r_inflated;
550 if (n != 0) {
551 *data = dbuf;
552 return n;
553 }
554 break;
555
556 case r_running:
557 ++rx_token;
558 if (--rx_run == 0)
559 recv_state = r_idle;
560 return -1 - rx_token;
b8d4524b 561 }
861c20b4 562 }
861c20b4
PM
563}
564
565/*
566 * put the data corresponding to a token that we've just returned
567 * from recv_deflated_token into the decompressor's history buffer.
568 */
7fcbf9e4 569static void see_deflate_token(char *buf, int32 len)
861c20b4 570{
acc461c7
WD
571 int r;
572 int32 blklen;
5914bf15
PM
573 unsigned char hdr[5];
574
575 rx_strm.avail_in = 0;
576 blklen = 0;
577 hdr[0] = 0;
578 do {
579 if (rx_strm.avail_in == 0 && len != 0) {
580 if (blklen == 0) {
581 /* Give it a fake stored-block header. */
582 rx_strm.next_in = (Bytef *)hdr;
583 rx_strm.avail_in = 5;
584 blklen = len;
585 if (blklen > 0xffff)
586 blklen = 0xffff;
587 hdr[1] = blklen;
588 hdr[2] = blklen >> 8;
589 hdr[3] = ~hdr[1];
590 hdr[4] = ~hdr[2];
591 } else {
592 rx_strm.next_in = (Bytef *)buf;
593 rx_strm.avail_in = blklen;
594 len -= blklen;
595 blklen = 0;
596 }
597 }
598 rx_strm.next_out = (Bytef *)dbuf;
1dbb94ca 599 rx_strm.avail_out = AVAIL_OUT_SIZE(CHUNK_SIZE);
5914bf15 600 r = inflate(&rx_strm, Z_SYNC_FLUSH);
4286ea60 601 if (r != Z_OK && r != Z_BUF_ERROR) {
5914bf15 602 rprintf(FERROR, "inflate (token) returned %d\n", r);
65417579 603 exit_cleanup(RERR_STREAMIO);
5914bf15
PM
604 }
605 } while (len || rx_strm.avail_out == 0);
861c20b4 606}
70d794dc 607
79f671cc
MP
608/**
609 * Transmit a verbatim buffer of length @p n followed by a token.
d67c8bdf 610 * If token == -1 then we have reached EOF
70d794dc 611 * If n == 0 then don't send a buffer
70d794dc 612 */
acc461c7 613void send_token(int f, int32 token, struct map_struct *buf, OFF_T offset,
7fcbf9e4 614 int32 n, int32 toklen)
70d794dc 615{
7fcbf9e4
WD
616 if (!do_compression)
617 simple_send_token(f, token, buf, offset, n);
618 else
5914bf15 619 send_deflated_token(f, token, buf, offset, n, toklen);
70d794dc
AT
620}
621
70d794dc
AT
622/*
623 * receive a token or buffer from the other end. If the reurn value is >0 then
624 * it is a data buffer of that length, and *data will point at the data.
625 * if the return value is -i then it represents token i-1
626 * if the return value is 0 then the end has been reached
627 */
7fcbf9e4 628int32 recv_token(int f, char **data)
70d794dc 629{
5914bf15
PM
630 int tok;
631
632 if (!do_compression) {
633 tok = simple_recv_token(f,data);
634 } else {
635 tok = recv_deflated_token(f, data);
636 }
637 return tok;
861c20b4
PM
638}
639
640/*
641 * look at the data corresponding to a token, if necessary
642 */
7fcbf9e4 643void see_token(char *data, int32 toklen)
861c20b4 644{
5914bf15
PM
645 if (do_compression)
646 see_deflate_token(data, toklen);
70d794dc 647}