124
127
exit_cleanup(RERR_UNSUPPORTED);
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]");
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).
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).
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).
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).
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.
162
* If ICB_INCLUDE_INCOMPLETE is set in "flags", any incomplete multi-byte
163
* chars are included, which ensures that EINVAL is not returned.
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)
153
169
ICONV_CONST char *ibuf;
170
size_t icnt, ocnt, opos;
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) {
182
realloc_xbuf(out, out->size + ROUND_UP_1024(in->len * 2));
185
if (flags & ICB_INIT)
161
186
iconv(ic, NULL, 0, NULL, 0);
163
188
ibuf = in->buf + in->pos;
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) {
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;
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);
204
ocnt = out->size - opos - 1;
205
obuf = out->buf + opos;
170
208
while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
173
211
if (errno == EINVAL) {
174
212
if (!(flags & ICB_INCLUDE_INCOMPLETE))
176
216
} else if (errno == EILSEQ) {
177
217
if (!(flags & ICB_INCLUDE_BAD))
180
size_t opos = obuf - out->buf;
181
if (!(flags & ICB_EXPAND_OUT)) {
221
} else if (errno == 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);
236
if (!(flags & ICB_EXPAND_OUT) || flags & ICB_CIRCULAR_OUT) {
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;
246
rsyserr(FERROR, errno, "unexpected error from iconv()");
247
exit_cleanup(RERR_UNSUPPORTED);
190
249
*obuf++ = *ibuf++;
259
opos = obuf - out->buf;
260
if (flags & ICB_CIRCULAR_OUT && opos < out->pos)
262
out->len = opos - out->pos;
199
265
in->pos = ibuf - in->buf;
200
out->len = obuf - out->buf - out->pos;
202
267
return errno ? -1 : 0;
546
612
* not ssh waiting for a password, then this tiny delay
547
613
* shouldn't hurt anything. */
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)
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;
549
632
exit_cleanup(RERR_SIGNAL);
581
665
ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
583
667
/* move tmp file over real file */
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);
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);