Annotation of src/usr.bin/rsync/uploader.c, Revision 1.7
1.7 ! deraadt 1: /* $Id: uploader.c,v 1.6 2019/02/14 18:31:01 florian 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) {
245: tv[0].tv_sec = time(NULL);
246: tv[0].tv_nsec = 0;
247: tv[1].tv_sec = f->st.mtime;
248: tv[1].tv_nsec = 0;
1.7 ! deraadt 249: rc = utimensat(p->rootfd, f->path, tv, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 250: if (rc == -1) {
1.1 benno 251: ERR(sess, "%s: utimensat", f->path);
252: return -1;
253: }
254: LOG4(sess, "%s: updated symlink date", f->path);
255: }
1.2 benno 256:
257: /*
1.1 benno 258: * FIXME: if newlink is set because we updated the symlink, we
259: * want to carry over the permissions from the last.
260: */
261:
262: if (newlink || sess->opts->preserve_perms) {
1.7 ! deraadt 263: rc = fchmodat(p->rootfd, f->path, f->st.mode, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 264: if (rc == -1) {
1.1 benno 265: ERR(sess, "%s: fchmodat", f->path);
266: return -1;
267: }
268: LOG4(sess, "%s: updated symlink mode", f->path);
269: }
270:
271: log_link(sess, f);
272: return 0;
273: }
274:
275: /*
276: * If not found, create the destination directory in prefix order.
277: * Create directories using the existing umask.
278: * Return <0 on failure 0 on success.
279: */
280: static int
281: pre_dir(const struct upload *p, struct sess *sess)
282: {
283: struct stat st;
1.2 benno 284: int rc;
1.1 benno 285: const struct flist *f;
286:
287: f = &p->fl[p->idx];
288: assert(S_ISDIR(f->st.mode));
289:
1.3 deraadt 290: if (!sess->opts->recursive) {
1.1 benno 291: WARNX(sess, "%s: ignoring directory", f->path);
292: return 0;
293: } else if (sess->opts->dry_run) {
294: log_dir(sess, f);
295: return 0;
296: }
297:
1.4 deraadt 298: assert(p->rootfd != -1);
1.1 benno 299: rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
1.4 deraadt 300: if (rc == -1 && errno != ENOENT) {
1.1 benno 301: WARN(sess, "%s: fstatat", f->path);
302: return -1;
1.4 deraadt 303: } else if (rc != -1 && !S_ISDIR(st.st_mode)) {
1.1 benno 304: WARNX(sess, "%s: not a directory", f->path);
305: return -1;
1.4 deraadt 306: } else if (rc != -1) {
1.2 benno 307: /*
1.1 benno 308: * FIXME: we should fchmod the permissions here as well,
309: * as we may locally have shut down writing into the
310: * directory and that doesn't work.
311: */
312: LOG3(sess, "%s: updating directory", f->path);
313: return 0;
314: }
315:
316: /*
317: * We want to make the directory with default permissions (using
318: * our old umask, which we've since unset), then adjust
319: * permissions (assuming preserve_perms or new) afterward in
320: * case it's u-w or something.
321: */
322:
323: LOG3(sess, "%s: creating directory", f->path);
1.4 deraadt 324: if (mkdirat(p->rootfd, f->path, 0777 & ~p->oumask) == -1) {
1.1 benno 325: WARN(sess, "%s: mkdirat", f->path);
326: return -1;
327: }
328:
329: p->newdir[p->idx] = 1;
330: log_dir(sess, f);
331: return 0;
332: }
333:
334: /*
335: * Process the directory time and mode for "idx" in the file list.
336: * Returns zero on failure, non-zero on success.
337: */
338: static int
339: post_dir(struct sess *sess, const struct upload *u, size_t idx)
340: {
341: struct timespec tv[2];
342: int rc;
343: struct stat st;
344: const struct flist *f;
345:
346: f = &u->fl[idx];
347: assert(S_ISDIR(f->st.mode));
348:
349: /* We already warned about the directory in pre_process_dir(). */
350:
1.3 deraadt 351: if (!sess->opts->recursive)
1.1 benno 352: return 1;
353: else if (sess->opts->dry_run)
354: return 1;
355:
1.4 deraadt 356: if (fstatat(u->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW) == -1) {
1.1 benno 357: ERR(sess, "%s: fstatat", f->path);
358: return 0;
1.3 deraadt 359: } else if (!S_ISDIR(st.st_mode)) {
1.1 benno 360: WARNX(sess, "%s: not a directory", f->path);
361: return 0;
362: }
363:
1.2 benno 364: /*
1.1 benno 365: * Update the modification time if we're a new directory *or* if
366: * we're preserving times and the time has changed.
1.6 florian 367: * FIXME: run rsync_set_metadata()?
1.1 benno 368: */
369:
1.2 benno 370: if (u->newdir[idx] ||
371: (sess->opts->preserve_times &&
1.1 benno 372: st.st_mtime != f->st.mtime)) {
373: tv[0].tv_sec = time(NULL);
374: tv[0].tv_nsec = 0;
375: tv[1].tv_sec = f->st.mtime;
376: tv[1].tv_nsec = 0;
377: rc = utimensat(u->rootfd, f->path, tv, 0);
1.4 deraadt 378: if (rc == -1) {
1.1 benno 379: ERR(sess, "%s: utimensat", f->path);
380: return 0;
381: }
382: LOG4(sess, "%s: updated date", f->path);
383: }
384:
385: /*
386: * Update the mode if we're a new directory *or* if we're
387: * preserving modes and it has changed.
388: */
389:
1.2 benno 390: if (u->newdir[idx] ||
1.1 benno 391: (sess->opts->preserve_perms &&
392: st.st_mode != f->st.mode)) {
393: rc = fchmodat(u->rootfd, f->path, f->st.mode, 0);
1.4 deraadt 394: if (rc == -1) {
1.1 benno 395: ERR(sess, "%s: fchmodat", f->path);
396: return 0;
397: }
398: LOG4(sess, "%s: updated mode", f->path);
399: }
400:
401: return 1;
402: }
403:
404: /*
405: * Try to open the file at the current index.
406: * If the file does not exist, returns with success.
407: * Return <0 on failure, 0 on success w/nothing to be done, >0 on
408: * success and the file needs attention.
409: */
410: static int
411: pre_file(const struct upload *p, int *filefd, struct sess *sess)
412: {
413: const struct flist *f;
414:
415: f = &p->fl[p->idx];
416: assert(S_ISREG(f->st.mode));
417:
418: if (sess->opts->dry_run) {
419: log_file(sess, f);
1.3 deraadt 420: if (!io_write_int(sess, p->fdout, p->idx)) {
1.1 benno 421: ERRX1(sess, "io_write_int");
422: return -1;
423: }
424: return 0;
425: }
426:
427: /*
428: * For non dry-run cases, we'll write the acknowledgement later
429: * in the rsync_uploader() function because we need to wait for
430: * the open() call to complete.
431: * If the call to openat() fails with ENOENT, there's a
432: * fast-path between here and the write function, so we won't do
433: * any blocking between now and then.
434: */
435:
436: *filefd = openat(p->rootfd, f->path,
437: O_RDONLY | O_NOFOLLOW | O_NONBLOCK, 0);
1.4 deraadt 438: if (*filefd != -1 || errno == ENOENT)
1.1 benno 439: return 1;
440: ERR(sess, "%s: openat", f->path);
441: return -1;
442: }
443:
444: /*
445: * Allocate an uploader object in the correct state to start.
446: * Returns NULL on failure or the pointer otherwise.
447: * On success, upload_free() must be called with the allocated pointer.
448: */
449: struct upload *
1.2 benno 450: upload_alloc(struct sess *sess, int rootfd, int fdout,
1.1 benno 451: size_t clen, const struct flist *fl, size_t flsz, mode_t msk)
452: {
453: struct upload *p;
454:
1.4 deraadt 455: if ((p = calloc(1, sizeof(struct upload))) == NULL) {
1.1 benno 456: ERR(sess, "calloc");
457: return NULL;
458: }
459:
460: p->state = UPLOAD_FIND_NEXT;
461: p->oumask = msk;
462: p->rootfd = rootfd;
463: p->csumlen = clen;
464: p->fdout = fdout;
465: p->fl = fl;
466: p->flsz = flsz;
467: p->newdir = calloc(flsz, sizeof(int));
1.4 deraadt 468: if (p->newdir == NULL) {
1.1 benno 469: ERR(sess, "calloc");
470: free(p);
471: return NULL;
472: }
473: return p;
474: }
475:
476: /*
477: * Perform all cleanups and free.
478: * Passing a NULL to this function is ok.
479: */
480: void
481: upload_free(struct upload *p)
482: {
483:
1.4 deraadt 484: if (p == NULL)
1.1 benno 485: return;
486: free(p->newdir);
487: free(p->buf);
488: free(p);
489: }
490:
491: /*
492: * Iterates through all available files and conditionally gets the file
493: * ready for processing to check whether it's up to date.
494: * If not up to date or empty, sends file information to the sender.
495: * If returns 0, we've processed all files there are to process.
496: * If returns >0, we're waiting for POLLIN or POLLOUT data.
497: * Otherwise returns <0, which is an error.
498: */
499: int
1.2 benno 500: rsync_uploader(struct upload *u, int *fileinfd,
1.1 benno 501: struct sess *sess, int *fileoutfd)
502: {
1.5 florian 503: struct blkset blk;
504: struct stat st;
505: void *map, *bufp;
506: size_t i, mapsz, pos, sz;
507: off_t offs;
508: int c;
509: const struct flist *f;
1.1 benno 510:
511: /* This should never get called. */
512:
1.4 deraadt 513: assert(u->state != UPLOAD_FINISHED);
1.1 benno 514:
515: /*
516: * If we have an upload in progress, then keep writing until the
517: * buffer has been fully written.
518: * We must only have the output file descriptor working and also
519: * have a valid buffer to write.
520: */
521:
1.4 deraadt 522: if (u->state == UPLOAD_WRITE_LOCAL) {
1.1 benno 523: assert(NULL != u->buf);
1.4 deraadt 524: assert(*fileoutfd != -1);
525: assert(*fileinfd == -1);
1.1 benno 526:
527: /*
528: * Unfortunately, we need to chunk these: if we're
529: * the server side of things, then we're multiplexing
530: * output and need to wrap this in chunks.
531: * This is a major deficiency of rsync.
532: * FIXME: add a "fast-path" mode that simply dumps out
533: * the buffer non-blocking if we're not mplexing.
534: */
535:
536: if (u->bufpos < u->bufsz) {
537: sz = MAX_CHUNK < (u->bufsz - u->bufpos) ?
538: MAX_CHUNK : (u->bufsz - u->bufpos);
1.2 benno 539: c = io_write_buf(sess, u->fdout,
1.1 benno 540: u->buf + u->bufpos, sz);
1.4 deraadt 541: if (c == 0) {
1.1 benno 542: ERRX1(sess, "io_write_nonblocking");
543: return -1;
544: }
545: u->bufpos += sz;
546: if (u->bufpos < u->bufsz)
547: return 1;
548: }
549:
1.2 benno 550: /*
1.1 benno 551: * Let the UPLOAD_FIND_NEXT state handle things if we
552: * finish, as we'll need to write a POLLOUT message and
553: * not have a writable descriptor yet.
554: */
555:
556: u->state = UPLOAD_FIND_NEXT;
557: u->idx++;
558: return 1;
559: }
560:
561: /*
562: * If we invoke the uploader without a file currently open, then
563: * we iterate through til the next available regular file and
564: * start the opening process.
565: * This means we must have the output file descriptor working.
566: */
567:
1.4 deraadt 568: if (u->state == UPLOAD_FIND_NEXT) {
569: assert(*fileinfd == -1);
570: assert(*fileoutfd != -1);
1.1 benno 571:
572: for ( ; u->idx < u->flsz; u->idx++) {
573: if (S_ISDIR(u->fl[u->idx].st.mode))
574: c = pre_dir(u, sess);
575: else if (S_ISLNK(u->fl[u->idx].st.mode))
576: c = pre_link(u, sess);
577: else if (S_ISREG(u->fl[u->idx].st.mode))
578: c = pre_file(u, fileinfd, sess);
579: else
580: c = 0;
581:
582: if (c < 0)
583: return -1;
584: else if (c > 0)
585: break;
586: }
587:
1.2 benno 588: /*
1.1 benno 589: * Whether we've finished writing files or not, we
590: * disable polling on the output channel.
591: */
592:
593: *fileoutfd = -1;
594: if (u->idx == u->flsz) {
1.4 deraadt 595: assert(*fileinfd == -1);
1.3 deraadt 596: if (!io_write_int(sess, u->fdout, -1)) {
1.1 benno 597: ERRX1(sess, "io_write_int");
598: return -1;
599: }
600: u->state = UPLOAD_FINISHED;
601: LOG4(sess, "uploader: finished");
602: return 0;
603: }
604:
605: /* Go back to the event loop, if necessary. */
606:
607: u->state = -1 == *fileinfd ?
608: UPLOAD_WRITE_LOCAL : UPLOAD_READ_LOCAL;
1.4 deraadt 609: if (u->state == UPLOAD_READ_LOCAL)
1.1 benno 610: return 1;
611: }
612:
1.2 benno 613: /*
1.1 benno 614: * If an input file is open, stat it and see if it's already up
615: * to date, in which case close it and go to the next one.
616: * Either way, we don't have a write channel open.
617: */
618:
1.4 deraadt 619: if (u->state == UPLOAD_READ_LOCAL) {
620: assert(*fileinfd != -1);
621: assert(*fileoutfd == -1);
1.5 florian 622: f = &u->fl[u->idx];
1.1 benno 623:
1.4 deraadt 624: if (fstat(*fileinfd, &st) == -1) {
1.5 florian 625: ERR(sess, "%s: fstat", f->path);
1.1 benno 626: close(*fileinfd);
627: *fileinfd = -1;
628: return -1;
1.3 deraadt 629: } else if (!S_ISREG(st.st_mode)) {
1.5 florian 630: ERRX(sess, "%s: not regular", f->path);
1.1 benno 631: close(*fileinfd);
632: *fileinfd = -1;
633: return -1;
634: }
635:
1.5 florian 636: if (st.st_size == f->st.size &&
637: st.st_mtime == f->st.mtime) {
638: LOG3(sess, "%s: skipping: up to date", f->path);
639: #if 0
640: /* Not yet: investigate behaviour. */
641: if (!rsync_set_metadata
642: (sess, 0, *fileinfd, f, f->path)) {
643: ERRX1(sess, "rsync_set_metadata");
644: close(*fileinfd);
645: *fileinfd = -1;
646: return -1;
647: }
648: #endif
1.1 benno 649: close(*fileinfd);
650: *fileinfd = -1;
651: *fileoutfd = u->fdout;
652: u->state = UPLOAD_FIND_NEXT;
653: u->idx++;
654: return 1;
655: }
656:
657: /* Fallthrough... */
658:
659: u->state = UPLOAD_WRITE_LOCAL;
660: }
661:
662: /* Initialies our blocks. */
663:
1.4 deraadt 664: assert(u->state == UPLOAD_WRITE_LOCAL);
1.1 benno 665: memset(&blk, 0, sizeof(struct blkset));
666: blk.csum = u->csumlen;
667:
1.4 deraadt 668: if (*fileinfd != -1 && st.st_size > 0) {
1.1 benno 669: mapsz = st.st_size;
1.7 ! deraadt 670: map = mmap(NULL, mapsz, PROT_READ, MAP_SHARED, *fileinfd, 0);
1.4 deraadt 671: if (map == MAP_FAILED) {
1.1 benno 672: WARN(sess, "%s: mmap", u->fl[u->idx].path);
673: close(*fileinfd);
674: *fileinfd = -1;
675: return -1;
676: }
677:
678: init_blkset(&blk, st.st_size);
679: assert(blk.blksz);
680:
681: blk.blks = calloc(blk.blksz, sizeof(struct blk));
1.4 deraadt 682: if (blk.blks == NULL) {
1.1 benno 683: ERR(sess, "calloc");
684: munmap(map, mapsz);
685: close(*fileinfd);
686: *fileinfd = -1;
687: return -1;
688: }
689:
690: offs = 0;
691: for (i = 0; i < blk.blksz; i++) {
1.2 benno 692: init_blk(&blk.blks[i],
1.1 benno 693: &blk, offs, i, map, sess);
694: offs += blk.len;
695: }
696:
697: munmap(map, mapsz);
698: close(*fileinfd);
699: *fileinfd = -1;
700: LOG3(sess, "%s: mapped %jd B with %zu blocks",
1.2 benno 701: u->fl[u->idx].path, (intmax_t)blk.size,
1.1 benno 702: blk.blksz);
703: } else {
1.4 deraadt 704: if (*fileinfd != -1) {
1.1 benno 705: close(*fileinfd);
706: *fileinfd = -1;
707: }
708: blk.len = MAX_CHUNK; /* Doesn't matter. */
709: LOG3(sess, "%s: not mapped", u->fl[u->idx].path);
710: }
711:
1.4 deraadt 712: assert(*fileinfd == -1);
1.1 benno 713:
714: /* Make sure the block metadata buffer is big enough. */
715:
1.2 benno 716: u->bufsz =
1.1 benno 717: sizeof(int32_t) + /* identifier */
718: sizeof(int32_t) + /* block count */
719: sizeof(int32_t) + /* block length */
720: sizeof(int32_t) + /* checksum length */
721: sizeof(int32_t) + /* block remainder */
1.2 benno 722: blk.blksz *
1.1 benno 723: (sizeof(int32_t) + /* short checksum */
724: blk.csum); /* long checksum */
725:
726: if (u->bufsz > u->bufmax) {
1.4 deraadt 727: if ((bufp = realloc(u->buf, u->bufsz)) == NULL) {
1.1 benno 728: ERR(sess, "realloc");
729: return -1;
730: }
731: u->buf = bufp;
732: u->bufmax = u->bufsz;
733: }
734:
735: u->bufpos = pos = 0;
736: io_buffer_int(sess, u->buf, &pos, u->bufsz, u->idx);
737: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.blksz);
738: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.len);
739: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.csum);
740: io_buffer_int(sess, u->buf, &pos, u->bufsz, blk.rem);
741: for (i = 0; i < blk.blksz; i++) {
1.2 benno 742: io_buffer_int(sess, u->buf, &pos, u->bufsz,
1.1 benno 743: blk.blks[i].chksum_short);
1.2 benno 744: io_buffer_buf(sess, u->buf, &pos, u->bufsz,
1.1 benno 745: blk.blks[i].chksum_long, blk.csum);
746: }
747: assert(pos == u->bufsz);
748:
749: /* Reenable the output poller and clean up. */
750:
751: *fileoutfd = u->fdout;
752: free(blk.blks);
753: return 1;
754: }
755:
756: /*
757: * Fix up the directory permissions and times post-order.
758: * We can't fix up directory permissions in place because the server may
759: * want us to have overly-tight permissions---say, those that don't
760: * allow writing into the directory.
761: * We also need to do our directory times post-order because making
762: * files within the directory will change modification times.
763: * Returns zero on failure, non-zero on success.
764: */
765: int
766: rsync_uploader_tail(struct upload *u, struct sess *sess)
767: {
768: size_t i;
769:
770:
1.3 deraadt 771: if (!sess->opts->preserve_times &&
772: !sess->opts->preserve_perms)
1.1 benno 773: return 1;
774:
775: LOG2(sess, "fixing up directory times and permissions");
776:
777: for (i = 0; i < u->flsz; i++)
778: if (S_ISDIR(u->fl[i].st.mode))
1.3 deraadt 779: if (!post_dir(sess, u, i))
1.1 benno 780: return 0;
781:
782: return 1;
783: }