~ubuntu-branches/ubuntu/trusty/rsync/trusty

« back to all changes in this revision

Viewing changes to rsync.c

  • Committer: Package Import Robot
  • Author(s): Paul Slootman
  • Date: 2013-10-27 12:01:10 UTC
  • mfrom: (1.1.13) (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20131027120110-mpr03n5scqmf40mi
Tags: 3.1.0-2
fix build failure if zlib1g-dev package is not installed;
solved by building without the included zlib source and adding a
build-depends on zlib1g-dev >= 1:1.2.8
closes:32379

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Copyright (C) 1996 Andrew Tridgell
5
5
 * Copyright (C) 1996 Paul Mackerras
6
 
 * Copyright (C) 2003-2009 Wayne Davison
 
6
 * Copyright (C) 2003-2013 Wayne Davison
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
9
9
 * it under the terms of the GNU General Public License as published by
27
27
#include <langinfo.h>
28
28
#endif
29
29
 
30
 
extern int verbose;
31
30
extern int dry_run;
32
31
extern int preserve_acls;
33
32
extern int preserve_xattrs;
36
35
extern int preserve_times;
37
36
extern int am_root;
38
37
extern int am_server;
 
38
extern int am_daemon;
39
39
extern int am_sender;
40
40
extern int am_receiver;
41
41
extern int am_generator;
42
42
extern int am_starting_up;
43
43
extern int allow_8bit_chars;
44
44
extern int protocol_version;
 
45
extern int got_kill_signal;
45
46
extern int inc_recurse;
46
47
extern int inplace;
47
48
extern int flist_eof;
 
49
extern int file_old_total;
48
50
extern int keep_dirlinks;
49
51
extern int make_backups;
50
52
extern struct file_list *cur_flist, *first_flist, *dir_flist;
82
84
                /* It's OK if this fails... */
83
85
                ic_chck = iconv_open(defset, defset);
84
86
 
85
 
                if (verbose > 3) {
 
87
                if (DEBUG_GTE(ICONV, 2)) {
86
88
                        if (ic_chck == (iconv_t)-1) {
87
89
                                rprintf(FINFO,
88
 
                                        "note: iconv_open(\"%s\", \"%s\") failed (%d)"
89
 
                                        " -- using isprint() instead of iconv().\n",
 
90
                                        "msg checking via isprint()"
 
91
                                        " (iconv_open(\"%s\", \"%s\") errno: %d)\n",
90
92
                                        defset, defset, errno);
91
93
                        } else {
92
94
                                rprintf(FINFO,
93
 
                                        "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
94
 
                                        defset, defset);
 
95
                                        "msg checking charset: %s\n",
 
96
                                        defset);
95
97
                        }
96
98
                }
97
 
        }
 
99
        } else
 
100
                ic_chck = (iconv_t)-1;
98
101
 
99
102
# ifdef ICONV_OPTION
100
103
        if (!iconv_opt)
124
127
                exit_cleanup(RERR_UNSUPPORTED);
125
128
        }
126
129
 
127
 
        if (verbose > 1) {
128
 
                rprintf(FINFO, "%s charset: %s\n",
129
 
                        am_server ? "server" : "client",
130
 
                        *charset ? charset : "[LOCALE]");
 
130
        if (DEBUG_GTE(ICONV, 1)) {
 
131
                rprintf(FINFO, "[%s] charset: %s\n",
 
132
                        who_am_i(), *charset ? charset : "[LOCALE]");
131
133
        }
132
134
# endif
133
135
}
134
136
 
135
 
/* This function converts the characters in the "in" xbuf into characters
136
 
 * in the "out" xbuf.  The "len" of the "in" xbuf is used starting from its
137
 
 * "pos".  The "size" of the "out" xbuf restricts how many characters can be
138
 
 * stored, starting at its "pos+len" position.  Note that the last byte of
139
 
 * the buffer is never used, which reserves space for a terminating '\0'.
 
137
/* This function converts the chars in the "in" xbuf into characters in the
 
138
 * "out" xbuf.  The ".len" chars of the "in" xbuf is used starting from its
 
139
 * ".pos".  The ".size" of the "out" xbuf restricts how many characters can
 
140
 * be stored, starting at its ".pos+.len" position.  Note that the last byte
 
141
 * of the "out" xbuf is not used, which reserves space for a trailing '\0'
 
142
 * (though it is up to the caller to store a trailing '\0', as needed).
 
143
 *
140
144
 * We return a 0 on success or a -1 on error.  An error also sets errno to
141
145
 * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
142
 
 * The "in" xbuf is altered to update "pos" and "len".  The "out" xbuf has
143
 
 * data appended, and its "len" incremented.   If ICB_EXPAND_OUT is set in
144
 
 * "flags", the "out" xbuf will also be allocated if empty, and expanded if
145
 
 * too small (so E2BIG will not be returned).  If ICB_INCLUDE_BAD is set in
146
 
 * "flags", any badly-encoded chars are included verbatim in the "out" xbuf,
147
 
 * so EILSEQ will not be returned.  Likewise for ICB_INCLUDE_INCOMPLETE with
148
 
 * respect to an incomplete multi-byte char at the end, which ensures that
149
 
 * EINVAL is not returned.  Anytime "in.pos" is 0 we will reset the iconv()
150
 
 * state prior to processing the characters. */
 
146
 * The "in" xbuf is altered to update ".pos" and ".len".  The "out" xbuf has
 
147
 * data appended, and its ".len" incremented (see below for a ".size" note).
 
148
 *
 
149
 * If ICB_CIRCULAR_OUT is set in "flags", the chars going into the "out" xbuf
 
150
 * can wrap around to the start, and the xbuf may have its ".size" reduced
 
151
 * (presumably by 1 byte) if the iconv code doesn't have space to store a
 
152
 * multi-byte character at the physical end of the ".buf" (though no reducing
 
153
 * happens if ".pos" is <= 1, since there is no room to wrap around).
 
154
 *
 
155
 * If ICB_EXPAND_OUT is set in "flags", the "out" xbuf will be allocated if
 
156
 * empty, and (as long as ICB_CIRCULAR_OUT is not set) expanded if too small.
 
157
 * This prevents the return of E2BIG (except for a circular xbuf).
 
158
 *
 
159
 * If ICB_INCLUDE_BAD is set in "flags", any badly-encoded chars are included
 
160
 * verbatim in the "out" xbuf, so EILSEQ will not be returned.
 
161
 *
 
162
 * If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte
 
163
 * chars are included, which ensures that EINVAL is not returned.
 
164
 *
 
165
 * If ICB_INIT is set, the iconv() conversion state is initialized prior to
 
166
 * processing the characters. */
151
167
int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
152
168
{
153
169
        ICONV_CONST char *ibuf;
154
 
        size_t icnt, ocnt;
 
170
        size_t icnt, ocnt, opos;
155
171
        char *obuf;
156
172
 
157
 
        if (!out->size && flags & ICB_EXPAND_OUT)
158
 
                alloc_xbuf(out, 1024);
 
173
        if (!out->size && flags & ICB_EXPAND_OUT) {
 
174
                size_t siz = ROUND_UP_1024(in->len * 2);
 
175
                alloc_xbuf(out, siz);
 
176
        } else if (out->len+1 >= out->size) {
 
177
                /* There is no room to even start storing data. */
 
178
                if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
 
179
                        errno = E2BIG;
 
180
                        return -1;
 
181
                }
 
182
                realloc_xbuf(out, out->size + ROUND_UP_1024(in->len * 2));
 
183
        }
159
184
 
160
 
        if (!in->pos)
 
185
        if (flags & ICB_INIT)
161
186
                iconv(ic, NULL, 0, NULL, 0);
162
187
 
163
188
        ibuf = in->buf + in->pos;
164
189
        icnt = in->len;
165
190
 
166
 
        obuf = out->buf + (out->pos + out->len);
167
 
        ocnt = out->size - (out->pos + out->len) - 1;
 
191
        opos = out->pos + out->len;
 
192
        if (flags & ICB_CIRCULAR_OUT) {
 
193
                if (opos >= out->size) {
 
194
                        opos -= out->size;
 
195
                        /* We know that out->pos is not 0 due to the "no room" check
 
196
                         * above, so this can't go "negative". */
 
197
                        ocnt = out->pos - opos - 1;
 
198
                } else {
 
199
                        /* Allow the use of all bytes to the physical end of the buffer
 
200
                         * unless pos is 0, in which case we reserve our trailing '\0'. */
 
201
                        ocnt = out->size - opos - (out->pos ? 0 : 1);
 
202
                }
 
203
        } else
 
204
                ocnt = out->size - opos - 1;
 
205
        obuf = out->buf + opos;
168
206
 
169
207
        while (icnt) {
170
208
                while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
173
211
                        if (errno == EINVAL) {
174
212
                                if (!(flags & ICB_INCLUDE_INCOMPLETE))
175
213
                                        goto finish;
 
214
                                if (!ocnt)
 
215
                                        goto e2big;
176
216
                        } else if (errno == EILSEQ) {
177
217
                                if (!(flags & ICB_INCLUDE_BAD))
178
218
                                        goto finish;
179
 
                        } else {
180
 
                                size_t opos = obuf - out->buf;
181
 
                                if (!(flags & ICB_EXPAND_OUT)) {
 
219
                                if (!ocnt)
 
220
                                        goto e2big;
 
221
                        } else if (errno == E2BIG) {
 
222
                                size_t siz;
 
223
                          e2big:
 
224
                                opos = obuf - out->buf;
 
225
                                if (flags & ICB_CIRCULAR_OUT && out->pos > 1 && opos > out->pos) {
 
226
                                        /* We are in a divided circular buffer at the physical
 
227
                                         * end with room to wrap to the start.  If iconv() refused
 
228
                                         * to use one or more trailing bytes in the buffer, we
 
229
                                         * set the size to ignore the unused bytes. */
 
230
                                        if (opos < out->size)
 
231
                                                reduce_iobuf_size(out, opos);
 
232
                                        obuf = out->buf;
 
233
                                        ocnt = out->pos - 1;
 
234
                                        continue;
 
235
                                }
 
236
                                if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
182
237
                                        errno = E2BIG;
183
238
                                        goto finish;
184
239
                                }
185
 
                                realloc_xbuf(out, out->size + 1024);
 
240
                                siz = ROUND_UP_1024(in->len * 2);
 
241
                                realloc_xbuf(out, out->size + siz);
186
242
                                obuf = out->buf + opos;
187
 
                                ocnt += 1024;
 
243
                                ocnt += siz;
188
244
                                continue;
 
245
                        } else {
 
246
                                rsyserr(FERROR, errno, "unexpected error from iconv()");
 
247
                                exit_cleanup(RERR_UNSUPPORTED);
189
248
                        }
190
249
                        *obuf++ = *ibuf++;
191
250
                        ocnt--, icnt--;
 
251
                        if (!icnt)
 
252
                                break;
192
253
                }
193
254
        }
194
255
 
195
256
        errno = 0;
196
257
 
197
258
  finish:
 
259
        opos = obuf - out->buf;
 
260
        if (flags & ICB_CIRCULAR_OUT && opos < out->pos)
 
261
                opos += out->size;
 
262
        out->len = opos - out->pos;
 
263
 
198
264
        in->len = icnt;
199
265
        in->pos = ibuf - in->buf;
200
 
        out->len = obuf - out->buf - out->pos;
201
266
 
202
267
        return errno ? -1 : 0;
203
268
}
216
281
 
217
282
        for (i = 0; args[i]; i++) {} /* find first NULL */
218
283
        args[i] = "rsync"; /* set a new arg0 */
219
 
        if (verbose > 1)
 
284
        if (DEBUG_GTE(CMD, 1))
220
285
                print_child_argv("protected args:", args + i + 1);
221
286
        do {
222
287
                if (!args[i][0])
225
290
                else if (convert) {
226
291
                        INIT_XBUF_STRLEN(inbuf, args[i]);
227
292
                        iconvbufs(ic_send, &inbuf, &outbuf,
228
 
                                  ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE);
 
293
                                  ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT);
229
294
                        outbuf.buf[outbuf.len] = '\0';
230
295
                        write_buf(fd, outbuf.buf, outbuf.len + 1);
231
296
                        outbuf.len = 0;
242
307
#endif
243
308
}
244
309
 
245
 
int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
 
310
int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr, uchar *type_ptr,
246
311
                       char *buf, int *len_ptr)
247
312
{
248
313
        int len, iflags = 0;
249
314
        struct file_list *flist;
250
315
        uchar fnamecmp_type = FNAMECMP_FNAME;
251
 
        int ndx, save_verbose = verbose;
 
316
        int ndx;
252
317
 
253
318
  read_loop:
254
319
        while (1) {
258
323
                        break;
259
324
                if (ndx == NDX_DONE)
260
325
                        return ndx;
 
326
                if (ndx == NDX_DEL_STATS) {
 
327
                        read_del_stats(f_in);
 
328
                        if (am_sender && am_server)
 
329
                                write_del_stats(f_out);
 
330
                        continue;
 
331
                }
261
332
                if (!inc_recurse || am_sender) {
262
333
                        int last;
263
334
                        if (first_flist)
271
342
                }
272
343
                if (ndx == NDX_FLIST_EOF) {
273
344
                        flist_eof = 1;
274
 
                        send_msg(MSG_FLIST_EOF, "", 0, 0);
 
345
                        if (DEBUG_GTE(FLIST, 3))
 
346
                                rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
 
347
                        write_int(f_out, NDX_FLIST_EOF);
275
348
                        continue;
276
349
                }
277
350
                ndx = NDX_FLIST_OFFSET - ndx;
285
358
                        exit_cleanup(RERR_PROTOCOL);
286
359
                }
287
360
 
288
 
                /* Send everything read from f_in to msg_fd_out. */
289
 
                if (verbose > 3) {
 
361
                if (DEBUG_GTE(FLIST, 2)) {
290
362
                        rprintf(FINFO, "[%s] receiving flist for dir %d\n",
291
363
                                who_am_i(), ndx);
292
364
                }
293
 
                verbose = 0;
294
 
                send_msg_int(MSG_FLIST, ndx);
295
 
                start_flist_forward(f_in);
 
365
                /* Send all the data we read for this flist to the generator. */
 
366
                start_flist_forward(ndx);
296
367
                flist = recv_file_list(f_in);
297
368
                flist->parent_ndx = ndx;
298
369
                stop_flist_forward();
299
 
                verbose = save_verbose;
300
370
        }
301
371
 
302
372
        iflags = protocol_version >= 29 ? read_shortint(f_in)
305
375
        /* Support the protocol-29 keep-alive style. */
306
376
        if (protocol_version < 30 && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
307
377
                if (am_sender)
308
 
                        maybe_send_keepalive();
 
378
                        maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
309
379
                goto read_loop;
310
380
        }
311
381
 
312
 
        cur_flist = flist_for_ndx(ndx, "read_ndx_and_attrs");
 
382
        flist = flist_for_ndx(ndx, "read_ndx_and_attrs");
 
383
        if (flist != cur_flist) {
 
384
                cur_flist = flist;
 
385
                if (am_sender) {
 
386
                        file_old_total = cur_flist->used;
 
387
                        for (flist = first_flist; flist != cur_flist; flist = flist->next)
 
388
                                file_old_total += flist->used;
 
389
                }
 
390
        }
313
391
 
314
392
        if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
315
393
                fnamecmp_type = read_byte(f_in);
391
469
                                full_fname(fname));
392
470
                        return 0;
393
471
                }
394
 
#ifdef SUPPORT_ACLS
395
 
                sx2.acc_acl = sx2.def_acl = NULL;
396
 
#endif
397
 
#ifdef SUPPORT_XATTRS
398
 
                sx2.xattr = NULL;
399
 
#endif
 
472
                init_stat_x(&sx2);
400
473
                sxp = &sx2;
401
474
                inherit = !preserve_perms;
402
475
        } else
429
502
                flags |= ATTRS_SKIP_MTIME;
430
503
        if (!(flags & ATTRS_SKIP_MTIME)
431
504
            && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
432
 
                int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
 
505
                int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode);
433
506
                if (ret < 0) {
434
507
                        rsyserr(FERROR_XFER, errno, "failed to set times on %s",
435
508
                                full_fname(fname));
450
523
        } else
451
524
#endif
452
525
        if (change_uid || change_gid) {
453
 
                if (verbose > 2) {
 
526
                if (DEBUG_GTE(OWN, 1)) {
454
527
                        if (change_uid) {
455
528
                                rprintf(FINFO,
456
529
                                        "set uid of %s from %u to %u\n",
515
588
        }
516
589
#endif
517
590
 
518
 
        if (verbose > 1 && flags & ATTRS_REPORT) {
 
591
        if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
519
592
                if (updated)
520
593
                        rprintf(FCLIENT, "%s\n", fname);
521
594
                else
522
595
                        rprintf(FCLIENT, "%s is uptodate\n", fname);
523
596
        }
524
597
  cleanup:
525
 
        if (sxp == &sx2) {
526
 
#ifdef SUPPORT_ACLS
527
 
                if (preserve_acls)
528
 
                        free_acl(&sx2);
529
 
#endif
530
 
#ifdef SUPPORT_XATTRS
531
 
                if (preserve_xattrs)
532
 
                        free_xattr(&sx2);
533
 
#endif
534
 
        }
 
598
        if (sxp == &sx2)
 
599
                free_stat_x(&sx2);
535
600
        return updated;
536
601
}
537
602
 
538
 
RETSIGTYPE sig_int(UNUSED(int val))
 
603
/* This is only called for SIGINT, SIGHUP, and SIGTERM. */
 
604
RETSIGTYPE sig_int(int sig_num)
539
605
{
540
606
        /* KLUGE: if the user hits Ctrl-C while ssh is prompting
541
607
         * for a password, then our cleanup's sending of a SIGUSR1
546
612
         * not ssh waiting for a password, then this tiny delay
547
613
         * shouldn't hurt anything. */
548
614
        msleep(400);
 
615
 
 
616
        /* If we're an rsync daemon listener (not a daemon server),
 
617
         * we'll exit with status 0 if we received SIGTERM. */
 
618
        if (am_daemon && !am_server && sig_num == SIGTERM)
 
619
                exit_cleanup(0);
 
620
 
 
621
        /* If the signal arrived on the server side (or for the receiver
 
622
         * process on the client), we want to try to do a controlled shutdown
 
623
         * that lets the client side (generator process) know what happened.
 
624
         * To do this, we set a flag and let the normal process handle the
 
625
         * shutdown.  We only attempt this if multiplexed IO is in effect and
 
626
         * we didn't already set the flag. */
 
627
        if (!got_kill_signal && (am_server || am_receiver)) {
 
628
                got_kill_signal = sig_num;
 
629
                return;
 
630
        }
 
631
 
549
632
        exit_cleanup(RERR_SIGNAL);
550
633
}
551
634
 
563
646
        const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
564
647
 
565
648
        if (inplace) {
566
 
                if (verbose > 2)
 
649
                if (DEBUG_GTE(RECV, 1))
567
650
                        rprintf(FINFO, "finishing %s\n", fname);
568
651
                fnametmp = fname;
569
652
                goto do_set_file_attrs;
570
653
        }
571
654
 
572
655
        if (make_backups > 0 && overwriting_basis) {
573
 
                if (!make_backup(fname))
 
656
                int ok = make_backup(fname, False);
 
657
                if (!ok)
574
658
                        return 1;
575
 
                if (fnamecmp == fname)
 
659
                if (ok == 1 && fnamecmp == fname)
576
660
                        fnamecmp = get_backup_name(fname);
577
661
        }
578
662
 
581
665
                       ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
582
666
 
583
667
        /* move tmp file over real file */
584
 
        if (verbose > 2)
 
668
        if (DEBUG_GTE(RECV, 1))
585
669
                rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
586
 
        ret = robust_rename(fnametmp, fname, temp_copy_name,
587
 
                            file->mode & INITACCESSPERMS);
 
670
        ret = robust_rename(fnametmp, fname, temp_copy_name, file->mode);
588
671
        if (ret < 0) {
589
672
                rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
590
673
                        ret == -2 ? "copy" : "rename",
591
674
                        full_fname(fnametmp), fname);
592
675
                if (!partialptr || (ret == -2 && temp_copy_name)
593
 
                 || robust_rename(fnametmp, partialptr, NULL,
594
 
                                  file->mode & INITACCESSPERMS) < 0)
 
676
                 || robust_rename(fnametmp, partialptr, NULL, file->mode) < 0)
595
677
                        do_unlink(fnametmp);
596
678
                return 0;
597
679
        }