Annotation of src/usr.bin/rsync/uploader.c, Revision 1.8
1.8 ! deraadt 1: /* $Id: uploader.c,v 1.7 2019/02/16 05:06:30 deraadt Exp $ */
1.1 benno 2: /*
3: * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/mman.h>
18: #include <sys/stat.h>
19:
20: #include <assert.h>
21: #include <errno.h>
22: #include <fcntl.h>
23: #include <inttypes.h>
24: #include <math.h>
25: #include <poll.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <time.h>
30: #include <unistd.h>
31:
32: #include "extern.h"
33:
34: enum uploadst {
35: UPLOAD_FIND_NEXT = 0, /* find next to upload to sender */
36: UPLOAD_WRITE_LOCAL, /* wait to write to sender */
37: UPLOAD_READ_LOCAL, /* wait to read from local file */
38: UPLOAD_FINISHED /* nothing more to do in phase */
39: };
40:
41: /*
42: * Used to keep track of data flowing from the receiver to the sender.
43: * This is managed by the receiver process.
44: */
45: struct upload {
46: enum uploadst state;
47: char *buf; /* if not NULL, pending upload */
48: size_t bufsz; /* size of buf */
49: size_t bufmax; /* maximum size of buf */
50: size_t bufpos; /* position in buf */
51: size_t idx; /* current transfer index */
52: mode_t oumask; /* umask for creating files */
53: int rootfd; /* destination directory */
54: size_t csumlen; /* checksum length */
55: int fdout; /* write descriptor to sender */
56: const struct flist *fl; /* file list */
57: size_t flsz; /* size of file list */
58: int *newdir; /* non-zero if mkdir'd */
59: };
60:
61: /*
62: * Log a directory by emitting the file and a trailing slash, just to
63: * show the operator that we're a directory.
64: */
65: static void
66: log_dir(struct sess *sess, const struct flist *f)
67: {
68: size_t sz;
69:
70: if (sess->opts->server)
71: return;
72: sz = strlen(f->path);
73: assert(sz > 0);
1.7 deraadt 74: LOG1(sess, "%s%s", f->path, ('/' == f->path[sz - 1]) ? "" : "/");
1.1 benno 75: }
76:
77: /*
78: * Log a link by emitting the file and the target, just to show the
79: * operator that we're a link.
80: */
81: static void
82: log_link(struct sess *sess, const struct flist *f)
83: {
84:
1.3 deraadt 85: if (!sess->opts->server)
1.1 benno 86: LOG1(sess, "%s -> %s", f->path, f->link);
87: }
88:
89: /*
90: * Simply log the filename.
91: */
92: static void
93: log_file(struct sess *sess, const struct flist *f)
94: {
95:
1.3 deraadt 96: if (!sess->opts->server)
1.1 benno 97: LOG1(sess, "%s", f->path);
98: }
99:
100: /*
101: * Prepare the overall block set's metadata.
102: * We always have at least one block.
103: * The block size is an important part of the algorithm.
104: * I use the same heuristic as the reference rsync, but implemented in a
105: * bit more of a straightforward way.
106: * In general, the individual block length is the rounded square root of
107: * the total file size.
108: * The minimum block length is 700.
109: */
110: static void
111: init_blkset(struct blkset *p, off_t sz)
112: {
113: double v;
114:
115: if (sz >= (BLOCK_SIZE_MIN * BLOCK_SIZE_MIN)) {
116: /* Simple rounded-up integer square root. */
117:
118: v = sqrt(sz);
119: p->len = ceil(v);
120:
1.2 benno 121: /*
1.1 benno 122: * Always be a multiple of eight.
123: * There's no reason to do this, but rsync does.
124: */
125:
126: if ((p->len % 8) > 0)
127: p->len += 8 - (p->len % 8);
128: } else
129: p->len = BLOCK_SIZE_MIN;
130:
131: p->size = sz;
1.4 deraadt 132: if ((p->blksz = sz / p->len) == 0)
1.1 benno 133: p->rem = sz;
134: else
135: p->rem = sz % p->len;
136:
137: /* If we have a remainder, then we need an extra block. */
138:
139: if (p->rem)
140: p->blksz++;
141: }
142:
143: /*
144: * For each block, prepare the block's metadata.
145: * We use the mapped "map" file to set our checksums.
146: */
147: static void
148: init_blk(struct blk *p, const struct blkset *set, off_t offs,
149: size_t idx, const void *map, const struct sess *sess)
150: {
151:
1.4 deraadt 152: assert(map != MAP_FAILED);
1.1 benno 153:
154: /* Block length inherits for all but the last. */
155:
156: p->idx = idx;
157: p->len = idx < set->blksz - 1 ? set->len : set->rem;
158: p->offs = offs;
159:
160: p->chksum_short = hash_fast(map + offs, p->len);
161: hash_slow(map + offs, p->len, p->chksum_long, sess);
162: }
163:
164: /*
165: * Return <0 on failure 0 on success.
166: */
167: static int
168: pre_link(struct upload *p, struct sess *sess)
169: {
170: int rc, newlink = 0;
171: char *b;
172: struct stat st;
173: struct timespec tv[2];
174: const struct flist *f;
175:
176: f = &p->fl[p->idx];
177: assert(S_ISLNK(f->st.mode));
178:
1.3 deraadt 179: if (!sess->opts->preserve_links) {
1.1 benno 180: WARNX(sess, "%s: ignoring symlink", f->path);
181: return 0;
182: } else if (sess->opts->dry_run) {
183: log_link(sess, f);
184: return 0;
185: }
186:
187: /* See if the symlink already exists. */
188:
1.4 deraadt 189: assert(p->rootfd != -1);
1.1 benno 190: rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 191: if (rc != -1 && !S_ISLNK(st.st_mode)) {
1.1 benno 192: WARNX(sess, "%s: not a symlink", f->path);
193: return -1;
1.4 deraadt 194: } else if (rc == -1 && errno != ENOENT) {
1.1 benno 195: WARN(sess, "%s: fstatat", f->path);
196: return -1;
197: }
198:
199: /*
200: * If the symbolic link already exists, then make sure that it
201: * points to the correct place.
202: * FIXME: does symlinkat() set permissions on the link using the
203: * destination file or the default umask?
204: * Do we need a fchmod in here as well?
205: */
206:
1.4 deraadt 207: if (rc == -1) {
1.1 benno 208: LOG3(sess, "%s: creating "
209: "symlink: %s", f->path, f->link);
1.4 deraadt 210: if (symlinkat(f->link, p->rootfd, f->path) == -1) {
1.1 benno 211: WARN(sess, "%s: symlinkat", f->path);
212: return -1;
213: }
214: newlink = 1;
215: } else {
216: b = symlinkat_read(sess, p->rootfd, f->path);
1.4 deraadt 217: if (b == NULL) {
1.1 benno 218: ERRX1(sess, "%s: symlinkat_read", f->path);
219: return -1;
220: }
221: if (strcmp(f->link, b)) {
222: free(b);
223: b = NULL;
224: LOG3(sess, "%s: updating "
225: "symlink: %s", f->path, f->link);
1.4 deraadt 226: if (unlinkat(p->rootfd, f->path, 0) == -1) {
1.1 benno 227: WARN(sess, "%s: unlinkat", f->path);
228: return -1;
229: }
1.4 deraadt 230: if (symlinkat(f->link, p->rootfd, f->path) == -1) {
1.1 benno 231: WARN(sess, "%s: symlinkat", f->path);
232: return -1;
233: }
234: newlink = 1;
1.2 benno 235: }
1.1 benno 236: free(b);
237: }
238:
1.6 florian 239: /*
240: * Optionally preserve times/perms on the symlink.
241: * FIXME: run rsync_set_metadata()?
242: */
1.1 benno 243:
244: if (sess->opts->preserve_times) {
1.8 ! deraadt 245: struct timeval now;
! 246:
! 247: gettimeofday(&now, NULL);
! 248: TIMEVAL_TO_TIMESPEC(&now, &tv[0]);
1.1 benno 249: tv[1].tv_sec = f->st.mtime;
250: tv[1].tv_nsec = 0;
1.7 deraadt 251: rc = utimensat(p->rootfd, f->path, tv, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 252: if (rc == -1) {
1.1 benno 253: ERR(sess, "%s: utimensat", f->path);
254: return -1;
255: }
256: LOG4(sess, "%s: updated symlink date", f->path);
257: }
1.2 benno 258:
259: /*
1.1 benno 260: * FIXME: if newlink is set because we updated the symlink, we
261: * want to carry over the permissions from the last.
262: */
263:
264: if (newlink || sess->opts->preserve_perms) {
1.7 deraadt 265: rc = fchmodat(p->rootfd, f->path, f->st.mode, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 266: if (rc == -1) {
1.1 benno 267: ERR(sess, "%s: fchmodat", f->path);
268: return -1;
269: }
270: LOG4(sess, "%s: updated symlink mode", f->path);
271: }
272:
273: log_link(sess, f);
274: return 0;
275: }
276:
277: /*
278: * If not found, create the destination directory in prefix order.
279: * Create directories using the existing umask.
280: * Return <0 on failure 0 on success.
281: */
282: static int
283: pre_dir(const struct upload *p, struct sess *sess)
284: {
285: struct stat st;
1.2 benno 286: int rc;
1.1 benno 287: const struct flist *f;
288:
289: f = &p->fl[p->idx];
290: assert(S_ISDIR(f->st.mode));
291:
1.3 deraadt 292: if (!sess->opts->recursive) {
1.1 benno 293: WARNX(sess, "%s: ignoring directory", f->path);
294: return 0;
295: } else if (sess->opts->dry_run) {
296: log_dir(sess, f);
297: return 0;
298: }
299:
1.4 deraadt 300: assert(p->rootfd != -1);
1.1 benno 301: rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 302: if (rc == -1 && errno != ENOENT) {
1.1 benno 303: WARN(sess, "%s: fstatat", f->path);
304: return -1;
1.4 deraadt 305: } else if (rc != -1 && !S_ISDIR(st.st_mode)) {
1.1 benno 306: WARNX(sess, "%s: not a directory", f->path);
307: return -1;
1.4 deraadt 308: } else if (rc != -1) {
1.2 benno 309: /*
1.1 benno 310: * FIXME: we should fchmod the permissions here as well,
311: * as we may locally have shut down writing into the
312: * directory and that doesn't work.
313: */
314: LOG3(sess, "%s: updating directory", f->path);
315: return 0;
316: }
317:
318: /*
319: * We want to make the directory with default permissions (using
320: * our old umask, which we've since unset), then adjust
321: * permissions (assuming preserve_perms or new) afterward in
322: * case it's u-w or something.
323: */
324:
325: LOG3(sess, "%s: creating directory", f->path);
1.4 deraadt 326: if (mkdirat(p->rootfd, f->path, 0777 & ~p->oumask) == -1) {
1.1 benno 327: WARN(sess, "%s: mkdirat", f->path);
328: return -1;
329: }
330:
331: p->newdir[p->idx] = 1;
332: log_dir(sess, f);
333: return 0;
334: }
335:
336: /*
337: * Process the directory time and mode for "idx" in the file list.
338: * Returns zero on failure, non-zero on success.
339: */
340: static int
341: post_dir(struct sess *sess, const struct upload *u, size_t idx)
342: {
343: struct timespec tv[2];
344: int rc;
345: struct stat st;
346: const struct flist *f;
347:
348: f = &u->fl[idx];
349: assert(S_ISDIR(f->st.mode));
350:
351: /* We already warned about the directory in pre_process_dir(). */
352:
1.3 deraadt 353: if (!sess->opts->recursive)
1.1 benno 354: return 1;
355: else if (sess->opts->dry_run)
356: return 1;
357:
1.4 deraadt 358: if (fstatat(u->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW) == -1) {
1.1 benno 359: ERR(sess, "%s: fstatat", f->path);
360: return 0;
1.3 deraadt 361: } else if (!S_ISDIR(st.st_mode)) {
1.1 benno 362: WARNX(sess, "%s: not a directory", f->path);
363: return 0;
364: }
365:
1.2 benno 366: /*
1.1 benno 367: * Update the modification time if we're a new directory *or* if
368: * we're preserving times and the time has changed.
1.6 florian 369: * FIXME: run rsync_set_metadata()?
1.1 benno 370: */
371:
1.2 benno 372: if (u->newdir[idx] ||
373: (sess->opts->preserve_times &&
1.1 benno 374: st.st_mtime != f->st.mtime)) {
375: tv[0].tv_sec = time(NULL);
376: tv[0].tv_nsec = 0;
377: tv[1].tv_sec = f->st.mtime;
378: tv[1].tv_nsec = 0;
379: rc = utimensat(u->rootfd, f->path, tv, 0);
1.4 deraadt 380: if (rc == -1) {
1.1 benno 381: ERR(sess, "%s: utimensat", f->path);
382: return 0;
383: }
384: LOG4(sess, "%s: updated date", f->path);
385: }
386:
387: /*
388: * Update the mode if we're a new directory *or* if we're
389: * preserving modes and it has changed.
390: */
391:
1.2 benno 392: if (u->newdir[idx] ||
1.1 benno 393: (sess->opts->preserve_perms &&
394: st.st_mode != f->st.mode)) {
395: rc = fchmodat(u->rootfd, f->path, f->st.mode, 0);
1.4 deraadt 396: if (rc == -1) {
1.1 benno 397: ERR(sess, "%s: fchmodat", f->path);
398: return 0;
399: }
400: LOG4(sess, "%s: updated mode", f->path);
401: }
402:
403: return 1;
404: }
405:
406: /*
407: * Try to open the file at the current index.
408: * If the file does not exist, returns with success.
409: * Return <0 on failure, 0 on success w/nothing to be done, >0 on
410: * success and the file needs attention.
411: */
412: static int
413: pre_file(const struct upload *p, int *filefd, struct sess *sess)
414: {
415: const struct flist *f;
416:
417: f = &p->fl[p->idx];
418: assert(S_ISREG(f->st.mode));
419:
420: if (sess->opts->dry_run) {
421: log_file(sess, f);
1.3 deraadt 422: if (!io_write_int(sess, p->fdout, p->idx)) {
1.1 benno 423: ERRX1(sess, "io_write_int");
424: return -1;
425: }
426: return 0;
427: }
428:
429: /*
430: * For non dry-run cases, we'll write the acknowledgement later
431: * in the rsync_uploader() function because we need to wait for
432: * the open() call to complete.
433: * If the call to openat() fails with ENOENT, there's a
434: * fast-path between here and the write function, so we won't do
435: * any blocking between now and then.
436: */
437:
438: *filefd = openat(p->rootfd, f->path,
439: O_RDONLY | O_NOFOLLOW | O_NONBLOCK, 0);
1.4 deraadt 440: if (*filefd != -1 || errno == ENOENT)
1.1 benno 441: return 1;
442: ERR(sess, "%s: openat", f->path);
443: return -1;
444: }
445:
446: /*
447: * Allocate an uploader object in the correct state to start.
448: * Returns NULL on failure or the pointer otherwise.
449: * On success, upload_free() must be called with the allocated pointer.
450: */
451: struct upload *
1.2 benno 452: upload_alloc(struct sess *sess, int rootfd, int fdout,
1.1 benno 453: size_t clen, const struct flist *fl, size_t flsz, mode_t msk)
454: {
455: struct upload *p;
456:
1.4 deraadt 457: if ((p = calloc(1, sizeof(struct upload))) == NULL) {
1.1 benno 458: ERR(sess, "calloc");
459: return NULL;
460: }
461:
462: p->state = UPLOAD_FIND_NEXT;
463: p->oumask = msk;
464: p->rootfd = rootfd;
465: p->csumlen = clen;
466: p->fdout = fdout;
467: p->fl = fl;
468: p->flsz = flsz;
469: p->newdir = calloc(flsz, sizeof(int));
1.4 deraadt 470: if (p->newdir == NULL) {
1.1 benno 471: ERR(sess, "calloc");
472: free(p);
473: return NULL;
474: }
475: return p;
476: }
477:
478: /*
479: * Perform all cleanups and free.
480: * Passing a NULL to this function is ok.
481: */
482: void
483: upload_free(struct upload *p)
484: {
485:
1.4 deraadt 486: if (p == NULL)
1.1 benno 487: return;
488: free(p->newdir);
489: free(p->buf);
490: free(p);
491: }
492:
493: /*
494: * Iterates through all available files and conditionally gets the file
495: * ready for processing to check whether it's up to date.
496: * If not up to date or empty, sends file information to the sender.
497: * If returns 0, we've processed all files there are to process.
498: * If returns >0, we're waiting for POLLIN or POLLOUT data.
499: * Otherwise returns <0, which is an error.
500: */
501: int
1.2 benno 502: rsync_uploader(struct upload *u, int *fileinfd,
1.1 benno 503: struct sess *sess, int *fileoutfd)
504: {
1.5 florian 505: struct blkset blk;
506: struct stat st;
507: void *map, *bufp;
508: size_t i, mapsz, pos, sz;
509: off_t offs;
510: int c;
511: const struct flist *f;
1.1 benno 512:
513: /* This should never get called. */
514:
1.4 deraadt 515: assert(u->state != UPLOAD_FINISHED);
1.1 benno 516:
517: /*
518: * If we have an upload in progress, then keep writing until the
519: * buffer has been fully written.
520: * We must only have the output file descriptor working and also
521: * have a valid buffer to write.
522: */
523:
1.4 deraadt 524: if (u->state == UPLOAD_WRITE_LOCAL) {
1.1 benno 525: assert(NULL != u->buf);
1.4 deraadt 526: assert(*fileoutfd != -1);
527: assert(*fileinfd == -1);
1.1 benno 528:
529: /*
530: * Unfortunately, we need to chunk these: if we're
531: * the server side of things, then we're multiplexing
532: * output and need to wrap this in chunks.
533: * This is a major deficiency of rsync.
534: * FIXME: add a "fast-path" mode that simply dumps out
535: * the buffer non-blocking if we're not mplexing.
536: */
537:
538: if (u->bufpos < u->bufsz) {
539: sz = MAX_CHUNK < (u->bufsz - u->bufpos) ?
540: MAX_CHUNK : (u->bufsz - u->bufpos);
1.2 benno 541: c = io_write_buf(sess, u->fdout,
1.1 benno 542: u->buf + u->bufpos, sz);
1.4 deraadt 543: if (c == 0) {
1.1 benno 544: ERRX1(sess, "io_write_nonblocking");
545: return -1;
546: }
547: u->bufpos += sz;
548: if (u->bufpos < u->bufsz)
549: return 1;
550: }
551:
1.2 benno 552: /*
1.1 benno 553: * Let the UPLOAD_FIND_NEXT state handle things if we
554: * finish, as we'll need to write a POLLOUT message and
555: * not have a writable descriptor yet.
556: */
557:
558: u->state = UPLOAD_FIND_NEXT;
559: u->idx++;
560: return 1;
561: }
562:
563: /*
564: * If we invoke the uploader without a file currently open, then
565: * we iterate through til the next available regular file and
566: * start the opening process.
567: * This means we must have the output file descriptor working.
568: */
569:
1.4 deraadt 570: if (u->state == UPLOAD_FIND_NEXT) {
571: assert(*fileinfd == -1);
572: assert(*fileoutfd != -1);
1.1 benno 573:
574: for ( ; u->idx < u->flsz; u->idx++) {
575: if (S_ISDIR(u->fl[u->idx].st.mode))
576: c = pre_dir(u, sess);
577: else if (S_ISLNK(u->fl[u->idx].st.mode))
578: c = pre_link(u, sess);
579: else if (S_ISREG(u->fl[u->idx].st.mode))
580: c = pre_file(u, fileinfd, sess);
581: else
582: c = 0;
583:
584: if (c < 0)
585: return -1;
586: else if (c > 0)
587: break;
588: }
589:
1.2 benno 590: /*
1.1 benno 591: * Whether we've finished writing files or not, we
592: * disable polling on the output channel.
593: */
594:
595: *fileoutfd = -1;
596: if (u->idx == u->flsz) {
1.4 deraadt 597: assert(*fileinfd == -1);
1.3 deraadt 598: if (!io_write_int(sess, u->fdout, -1)) {
1.1 benno 599: ERRX1(sess, "io_write_int");
600: return -1;
601: }
602: u->state = UPLOAD_FINISHED;
603: LOG4(sess, "uploader: finished");
604: return 0;
605: }
606:
607: /* Go back to the event loop, if necessary. */
608:
609: u->state = -1 == *fileinfd ?
610: UPLOAD_WRITE_LOCAL : UPLOAD_READ_LOCAL;
1.4 deraadt 611: if (u->state == UPLOAD_READ_LOCAL)
1.1 benno 612: return 1;
613: }
614:
1.2 benno 615: /*
1.1 benno 616: * If an input file is open, stat it and see if it's already up
617: * to date, in which case close it and go to the next one.
618: * Either way, we don't have a write channel open.
619: */
620:
1.4 deraadt 621: if (u->state == UPLOAD_READ_LOCAL) {
622: assert(*fileinfd != -1);
623: assert(*fileoutfd == -1);
1.5 florian 624: f = &u->fl[u->idx];
1.1 benno 625:
1.4 deraadt 626: if (fstat(*fileinfd, &st) == -1) {
1.5 florian 627: ERR(sess, "%s: fstat", f->path);
1.1 benno 628: close(*fileinfd);
629: *fileinfd = -1;
630: return -1;
1.3 deraadt 631: } else if (!S_ISREG(st.st_mode)) {
1.5 florian 632: ERRX(sess, "%s: not regular", f->path);
1.1 benno 633: close(*fileinfd);
634: *fileinfd = -1;
635: return -1;
636: }
637:
1.5 florian 638: if (st.st_size == f->st.size &&
639: st.st_mtime == f->st.mtime) {
640: LOG3(sess, "%s: skipping: up to date", f->path);
641: #if 0
642: /* Not yet: investigate behaviour. */
643: if (!rsync_set_metadata
644: (sess, 0, *fileinfd, f, f->path)) {
645: ERRX1(sess, "rsync_set_metadata");
646: close(*fileinfd);
647: *fileinfd = -1;
648: return -1;
649: }
650: #endif
1.1 benno 651: close(*fileinfd);
652: *fileinfd = -1;
653: *fileoutfd = u->fdout;
654: u->state = UPLOAD_FIND_NEXT;
655: u->idx++;
656: return 1;
657: }
658:
659: /* Fallthrough... */
660:
661: u->state = UPLOAD_WRITE_LOCAL;
662: }
663:
664: /* Initialies our blocks. */
665:
1.4 deraadt 666: assert(u->state == UPLOAD_WRITE_LOCAL);
1.1 benno 667: memset(&blk, 0, sizeof(struct blkset));
668: blk.csum = u->csumlen;
669:
1.4 deraadt 670: if (*fileinfd != -1 && st.st_size > 0) {
1.1 benno 671: mapsz = st.st_size;
1.7 deraadt 672: map = mmap(NULL, mapsz, PROT_READ, MAP_SHARED, *fileinfd, 0);
1.4 deraadt 673: if (map == MAP_FAILED) {
1.1 benno 674: WARN(sess, "%s: mmap", u->fl[u->idx].path);
675: close(*fileinfd);
676: *fileinfd = -1;
677: return -1;
678: }
679:
680: init_blkset(&blk, st.st_size);
681: assert(blk.blksz);
682:
683: blk.blks = calloc(blk.blksz, sizeof(struct blk));
1.4 deraadt 684: if (blk.blks == NULL) {
1.1 benno 685: ERR(sess, "calloc");
686: munmap(map, mapsz);
687: close(*fileinfd);
688: *fileinfd = -1;
689: return -1;
690: }
691:
692: offs = 0;
693: for (i = 0; i < blk.blksz; i++) {
1.2 benno 694: init_blk(&blk.blks[i],
1.1 benno 695: &blk, offs, i, map, sess);
696: offs += blk.len;
697: }
698:
699: munmap(map, mapsz);
700: close(*fileinfd);
701: *fileinfd = -1;
702: LOG3(sess, "%s: mapped %jd B with %zu blocks",
1.2 benno 703: u->fl[u->idx].path, (intmax_t)blk.size,
1.1 benno 704: blk.blksz);
705: } else {
1.4 deraadt 706: if (*fileinfd != -1) {
1.1 benno 707: close(*fileinfd);
708: *fileinfd = -1;
709: }
710: blk.len = MAX_CHUNK; /* Doesn't matter. */
711: LOG3(sess, "%s: not mapped", u->fl[u->idx].path);
712: }
713:
1.4 deraadt 714: assert(*fileinfd == -1);
1.1 benno 715:
716: /* Make sure the block metadata buffer is big enough. */
717:
1.2 benno 718: u->bufsz =
1.1 benno 719: sizeof(int32_t) + /* identifier */
720: sizeof(int32_t) + /* block count */
721: sizeof(int32_t) + /* block length */
722: sizeof(int32_t) + /* checksum length */
723: sizeof(int32_t) + /* block remainder */
1.2 benno 724: blk.blksz *
1.1 benno 725: (sizeof(int32_t) + /* short checksum */
726: blk.csum); /* long checksum */
727:
728: if (u->bufsz > u->bufmax) {
1.4 deraadt 729: if ((bufp = realloc(u->buf, u->bufsz)) == NULL) {
1.1 benno 730: ERR(sess, "realloc");
731: return -1;
732: }
733: u->buf = bufp;
734: u->bufmax = u->bufsz;
735: }
736:
737: u->bufpos = pos = 0;
738: io_buffer_int(sess, u->buf, &pos, u->bufsz, u->idx);
739: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.blksz);
740: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.len);
741: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.csum);
742: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.rem);
743: for (i = 0; i < blk.blksz; i++) {
1.2 benno 744: io_buffer_int(sess, u->buf, &pos, u->bufsz,
1.1 benno 745: blk.blks[i].chksum_short);
1.2 benno 746: io_buffer_buf(sess, u->buf, &pos, u->bufsz,
1.1 benno 747: blk.blks[i].chksum_long, blk.csum);
748: }
749: assert(pos == u->bufsz);
750:
751: /* Reenable the output poller and clean up. */
752:
753: *fileoutfd = u->fdout;
754: free(blk.blks);
755: return 1;
756: }
757:
758: /*
759: * Fix up the directory permissions and times post-order.
760: * We can't fix up directory permissions in place because the server may
761: * want us to have overly-tight permissions---say, those that don't
762: * allow writing into the directory.
763: * We also need to do our directory times post-order because making
764: * files within the directory will change modification times.
765: * Returns zero on failure, non-zero on success.
766: */
767: int
768: rsync_uploader_tail(struct upload *u, struct sess *sess)
769: {
770: size_t i;
771:
772:
1.3 deraadt 773: if (!sess->opts->preserve_times &&
774: !sess->opts->preserve_perms)
1.1 benno 775: return 1;
776:
777: LOG2(sess, "fixing up directory times and permissions");
778:
779: for (i = 0; i < u->flsz; i++)
780: if (S_ISDIR(u->fl[i].st.mode))
1.3 deraadt 781: if (!post_dir(sess, u, i))
1.1 benno 782: return 0;
783:
784: return 1;
785: }