[BACK]Return to uploader.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rsync

Annotation of src/usr.bin/rsync/uploader.c, Revision 1.25

1.25    ! claudio     1: /*     $Id: uploader.c,v 1.24 2021/03/22 11:20:04 claudio Exp $ */
1.1       benno       2: /*
                      3:  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
1.12      florian     4:  * Copyright (c) 2019 Florian Obser <florian@openbsd.org>
1.1       benno       5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18: #include <sys/mman.h>
                     19: #include <sys/stat.h>
                     20:
                     21: #include <assert.h>
                     22: #include <errno.h>
                     23: #include <fcntl.h>
                     24: #include <inttypes.h>
                     25: #include <math.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 */
1.25    ! claudio    36:        UPLOAD_WRITE, /* wait to write to sender */
1.1       benno      37:        UPLOAD_FINISHED /* nothing more to do in phase */
                     38: };
                     39:
                     40: /*
                     41:  * Used to keep track of data flowing from the receiver to the sender.
                     42:  * This is managed by the receiver process.
                     43:  */
                     44: struct upload {
                     45:        enum uploadst       state;
                     46:        char               *buf; /* if not NULL, pending upload */
                     47:        size_t              bufsz; /* size of buf */
                     48:        size_t              bufmax; /* maximum size of buf */
                     49:        size_t              bufpos; /* position in buf */
                     50:        size_t              idx; /* current transfer index */
                     51:        mode_t              oumask; /* umask for creating files */
1.11      florian    52:        char               *root; /* destination directory path */
1.1       benno      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.21      benno      74:        LOG1("%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.21      benno      86:                LOG1("%s -> %s", f->path, f->link);
1.1       benno      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.21      benno      97:                LOG1("%s", f->path);
1.1       benno      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.24      claudio   152:        p->idx = idx;
1.1       benno     153:        /* Block length inherits for all but the last. */
                    154:        p->len = idx < set->blksz - 1 ? set->len : set->rem;
                    155:        p->offs = offs;
                    156:
1.23      benno     157:        p->chksum_short = hash_fast(map, p->len);
                    158:        hash_slow(map, p->len, p->chksum_long, sess);
1.1       benno     159: }
                    160:
                    161: /*
1.17      benno     162:  * Handle a symbolic link.
                    163:  * If we encounter directories existing in the symbolic link's place,
                    164:  * then try to unlink the directory.
                    165:  * Otherwise, simply overwrite with the symbolic link by renaming.
1.1       benno     166:  * Return <0 on failure 0 on success.
                    167:  */
                    168: static int
                    169: pre_link(struct upload *p, struct sess *sess)
                    170: {
1.9       florian   171:        struct stat              st;
                    172:        const struct flist      *f;
                    173:        int                      rc, newlink = 0, updatelink = 0;
1.11      florian   174:        char                    *b, *temp = NULL;
1.1       benno     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.21      benno     180:                WARNX("%s: ignoring symlink", f->path);
1.1       benno     181:                return 0;
1.25    ! claudio   182:        }
        !           183:        if (sess->opts->dry_run) {
1.1       benno     184:                log_link(sess, f);
                    185:                return 0;
                    186:        }
                    187:
1.17      benno     188:        /*
                    189:         * See if the symlink already exists.
                    190:         * If it's a directory, then try to unlink the directory prior
                    191:         * to overwriting with a symbolic link.
                    192:         * If it's a non-directory, we just overwrite it.
                    193:         */
1.1       benno     194:
1.4       deraadt   195:        assert(p->rootfd != -1);
1.1       benno     196:        rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
1.4       deraadt   197:        if (rc != -1 && !S_ISLNK(st.st_mode)) {
1.17      benno     198:                if (S_ISDIR(st.st_mode) &&
                    199:                    unlinkat(p->rootfd, f->path, AT_REMOVEDIR) == -1) {
1.21      benno     200:                        ERR("%s: unlinkat", f->path);
1.17      benno     201:                        return -1;
1.9       florian   202:                }
1.17      benno     203:                rc = -1;
1.4       deraadt   204:        } else if (rc == -1 && errno != ENOENT) {
1.21      benno     205:                ERR("%s: fstatat", f->path);
1.1       benno     206:                return -1;
                    207:        }
                    208:
                    209:        /*
                    210:         * If the symbolic link already exists, then make sure that it
                    211:         * points to the correct place.
                    212:         */
                    213:
1.9       florian   214:        if (rc != -1) {
1.22      benno     215:                b = symlinkat_read(p->rootfd, f->path);
1.4       deraadt   216:                if (b == NULL) {
1.21      benno     217:                        ERRX1("symlinkat_read");
1.1       benno     218:                        return -1;
                    219:                }
                    220:                if (strcmp(f->link, b)) {
                    221:                        free(b);
                    222:                        b = NULL;
1.21      benno     223:                        LOG3("%s: updating symlink: %s", f->path, f->link);
1.9       florian   224:                        updatelink = 1;
1.2       benno     225:                }
1.1       benno     226:                free(b);
1.9       florian   227:                b = NULL;
                    228:        }
                    229:
1.17      benno     230:        /*
                    231:         * Create the temporary file as a symbolic link, then rename the
                    232:         * temporary file as the real one, overwriting anything there.
                    233:         */
                    234:
1.9       florian   235:        if (rc == -1 || updatelink) {
1.21      benno     236:                LOG3("%s: creating symlink: %s", f->path, f->link);
1.22      benno     237:                if (mktemplate(&temp, f->path, sess->opts->recursive) == -1) {
1.21      benno     238:                        ERRX1("mktemplate");
1.9       florian   239:                        return -1;
                    240:                }
1.11      florian   241:                if (mkstemplinkat(f->link, p->rootfd, temp) == NULL) {
1.21      benno     242:                        ERR("mkstemplinkat");
1.11      florian   243:                        free(temp);
1.9       florian   244:                        return -1;
                    245:                }
                    246:                newlink = 1;
1.1       benno     247:        }
                    248:
1.17      benno     249:        rsync_set_metadata_at(sess, newlink,
                    250:                p->rootfd, f, newlink ? temp : f->path);
1.11      florian   251:
                    252:        if (newlink) {
                    253:                if (renameat(p->rootfd, temp, p->rootfd, f->path) == -1) {
1.21      benno     254:                        ERR("%s: renameat %s", temp, f->path);
1.11      florian   255:                        (void)unlinkat(p->rootfd, temp, 0);
                    256:                        free(temp);
                    257:                        return -1;
                    258:                }
                    259:                free(temp);
                    260:        }
                    261:
                    262:        log_link(sess, f);
                    263:        return 0;
                    264: }
                    265:
                    266: /*
1.17      benno     267:  * See pre_link(), but for devices.
                    268:  * FIXME: this is very similar to the other pre_xxx() functions.
1.11      florian   269:  * Return <0 on failure 0 on success.
                    270:  */
                    271: static int
                    272: pre_dev(struct upload *p, struct sess *sess)
                    273: {
                    274:        struct stat              st;
                    275:        const struct flist      *f;
                    276:        int                      rc, newdev = 0, updatedev = 0;
                    277:        char                    *temp = NULL;
                    278:
                    279:        f = &p->fl[p->idx];
                    280:        assert(S_ISBLK(f->st.mode) || S_ISCHR(f->st.mode));
                    281:
                    282:        if (!sess->opts->devices || getuid() != 0) {
1.21      benno     283:                WARNX("skipping non-regular file %s", f->path);
1.11      florian   284:                return 0;
1.25    ! claudio   285:        }
        !           286:        if (sess->opts->dry_run) {
1.11      florian   287:                log_file(sess, f);
                    288:                return 0;
                    289:        }
                    290:
1.17      benno     291:        /*
                    292:         * See if the dev already exists.
                    293:         * If a non-device exists in its place, we'll replace that.
                    294:         * If it replaces a directory, remove the directory first.
                    295:         */
                    296:
1.16      benno     297:        assert(p->rootfd != -1);
1.11      florian   298:        rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
                    299:
                    300:        if (rc != -1 && !(S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))) {
1.17      benno     301:                if (S_ISDIR(st.st_mode) &&
                    302:                    unlinkat(p->rootfd, f->path, AT_REMOVEDIR) == -1) {
1.21      benno     303:                        ERR("%s: unlinkat", f->path);
1.17      benno     304:                        return -1;
1.11      florian   305:                }
1.17      benno     306:                rc = -1;
1.11      florian   307:        } else if (rc == -1 && errno != ENOENT) {
1.21      benno     308:                ERR("%s: fstatat", f->path);
1.11      florian   309:                return -1;
                    310:        }
                    311:
1.17      benno     312:        /* Make sure existing device is of the correct type. */
1.1       benno     313:
1.11      florian   314:        if (rc != -1) {
                    315:                if ((f->st.mode & (S_IFCHR|S_IFBLK)) !=
1.17      benno     316:                    (st.st_mode & (S_IFCHR|S_IFBLK)) ||
                    317:                    f->st.rdev != st.st_rdev) {
1.21      benno     318:                        LOG3("%s: updating device", f->path);
1.11      florian   319:                        updatedev = 1;
                    320:                }
                    321:        }
                    322:
                    323:        if (rc == -1 || updatedev) {
                    324:                newdev = 1;
1.22      benno     325:                if (mktemplate(&temp, f->path, sess->opts->recursive) == -1) {
1.21      benno     326:                        ERRX1("mktemplate");
1.11      florian   327:                        return -1;
                    328:                }
1.17      benno     329:                if (mkstempnodat(p->rootfd, temp,
                    330:                    f->st.mode & (S_IFCHR|S_IFBLK), f->st.rdev) == NULL) {
1.21      benno     331:                        ERR("mkstempnodat");
1.11      florian   332:                        free(temp);
                    333:                        return -1;
                    334:                }
                    335:        }
                    336:
1.17      benno     337:        rsync_set_metadata_at(sess, newdev,
1.22      benno     338:            p->rootfd, f, newdev ? temp : f->path);
1.11      florian   339:
                    340:        if (newdev) {
                    341:                if (renameat(p->rootfd, temp, p->rootfd, f->path) == -1) {
1.21      benno     342:                        ERR("%s: renameat %s", temp, f->path);
1.11      florian   343:                        (void)unlinkat(p->rootfd, temp, 0);
                    344:                        free(temp);
                    345:                        return -1;
                    346:                }
                    347:                free(temp);
                    348:        }
1.17      benno     349:
1.11      florian   350:        log_file(sess, f);
                    351:        return 0;
                    352: }
                    353:
                    354: /*
1.17      benno     355:  * See pre_link(), but for FIFOs.
                    356:  * FIXME: this is very similar to the other pre_xxx() functions.
1.11      florian   357:  * Return <0 on failure 0 on success.
                    358:  */
                    359: static int
                    360: pre_fifo(struct upload *p, struct sess *sess)
                    361: {
                    362:        struct stat              st;
                    363:        const struct flist      *f;
                    364:        int                      rc, newfifo = 0;
                    365:        char                    *temp = NULL;
                    366:
                    367:        f = &p->fl[p->idx];
                    368:        assert(S_ISFIFO(f->st.mode));
                    369:
                    370:        if (!sess->opts->specials) {
1.21      benno     371:                WARNX("skipping non-regular file %s", f->path);
1.11      florian   372:                return 0;
1.25    ! claudio   373:        }
        !           374:        if (sess->opts->dry_run) {
1.11      florian   375:                log_file(sess, f);
                    376:                return 0;
                    377:        }
                    378:
1.17      benno     379:        /*
                    380:         * See if the fifo already exists.
                    381:         * If it exists as a non-FIFO, unlink it (if a directory) then
                    382:         * mark it from replacement.
                    383:         */
                    384:
1.16      benno     385:        assert(p->rootfd != -1);
1.11      florian   386:        rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
1.8       deraadt   387:
1.11      florian   388:        if (rc != -1 && !S_ISFIFO(st.st_mode)) {
1.17      benno     389:                if (S_ISDIR(st.st_mode) &&
                    390:                    unlinkat(p->rootfd, f->path, AT_REMOVEDIR) == -1) {
1.21      benno     391:                        ERR("%s: unlinkat", f->path);
1.17      benno     392:                        return -1;
1.11      florian   393:                }
1.17      benno     394:                rc = -1;
1.11      florian   395:        } else if (rc == -1 && errno != ENOENT) {
1.21      benno     396:                ERR("%s: fstatat", f->path);
1.11      florian   397:                return -1;
                    398:        }
                    399:
                    400:        if (rc == -1) {
                    401:                newfifo = 1;
1.22      benno     402:                if (mktemplate(&temp, f->path, sess->opts->recursive) == -1) {
1.21      benno     403:                        ERRX1("mktemplate");
1.11      florian   404:                        return -1;
                    405:                }
                    406:                if (mkstempfifoat(p->rootfd, temp) == NULL) {
1.21      benno     407:                        ERR("mkstempfifoat");
1.11      florian   408:                        free(temp);
1.1       benno     409:                        return -1;
                    410:                }
                    411:        }
1.2       benno     412:
1.17      benno     413:        rsync_set_metadata_at(sess, newfifo,
                    414:                p->rootfd, f, newfifo ? temp : f->path);
1.11      florian   415:
                    416:        if (newfifo) {
                    417:                if (renameat(p->rootfd, temp, p->rootfd, f->path) == -1) {
1.21      benno     418:                        ERR("%s: renameat %s", temp, f->path);
1.11      florian   419:                        (void)unlinkat(p->rootfd, temp, 0);
                    420:                        free(temp);
                    421:                        return -1;
                    422:                }
                    423:                free(temp);
                    424:        }
1.17      benno     425:
1.11      florian   426:        log_file(sess, f);
                    427:        return 0;
                    428: }
1.1       benno     429:
1.11      florian   430: /*
1.17      benno     431:  * See pre_link(), but for socket files.
                    432:  * FIXME: this is very similar to the other pre_xxx() functions.
1.11      florian   433:  * Return <0 on failure 0 on success.
                    434:  */
                    435: static int
                    436: pre_sock(struct upload *p, struct sess *sess)
                    437: {
                    438:        struct stat              st;
                    439:        const struct flist      *f;
                    440:        int                      rc, newsock = 0;
                    441:        char                    *temp = NULL;
                    442:
                    443:        f = &p->fl[p->idx];
                    444:        assert(S_ISSOCK(f->st.mode));
                    445:
                    446:        if (!sess->opts->specials) {
1.21      benno     447:                WARNX("skipping non-regular file %s", f->path);
1.11      florian   448:                return 0;
1.25    ! claudio   449:        }
        !           450:        if (sess->opts->dry_run) {
1.11      florian   451:                log_file(sess, f);
                    452:                return 0;
                    453:        }
                    454:
1.17      benno     455:        /*
                    456:         * See if the fifo already exists.
                    457:         * If it exists as a non-FIFO, unlink it (if a directory) then
                    458:         * mark it from replacement.
                    459:         */
                    460:
1.18      deraadt   461:        assert(p->rootfd != -1);
1.11      florian   462:        rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
                    463:
                    464:        if (rc != -1 && !S_ISSOCK(st.st_mode)) {
1.17      benno     465:                if (S_ISDIR(st.st_mode) &&
                    466:                    unlinkat(p->rootfd, f->path, AT_REMOVEDIR) == -1) {
1.21      benno     467:                        ERR("%s: unlinkat", f->path);
1.17      benno     468:                        return -1;
1.11      florian   469:                }
1.17      benno     470:                rc = -1;
1.11      florian   471:        } else if (rc == -1 && errno != ENOENT) {
1.21      benno     472:                ERR("%s: fstatat", f->path);
1.11      florian   473:                return -1;
                    474:        }
                    475:
                    476:        if (rc == -1) {
                    477:                newsock = 1;
1.22      benno     478:                if (mktemplate(&temp, f->path, sess->opts->recursive) == -1) {
1.21      benno     479:                        ERRX1("mktemplate");
1.11      florian   480:                        return -1;
                    481:                }
                    482:                if (mkstempsock(p->root, temp) == NULL) {
1.21      benno     483:                        ERR("mkstempsock");
1.11      florian   484:                        free(temp);
1.1       benno     485:                        return -1;
                    486:                }
1.9       florian   487:        }
                    488:
1.17      benno     489:        rsync_set_metadata_at(sess, newsock,
                    490:                p->rootfd, f, newsock ? temp : f->path);
1.11      florian   491:
                    492:        if (newsock) {
                    493:                if (renameat(p->rootfd, temp, p->rootfd, f->path) == -1) {
1.21      benno     494:                        ERR("%s: renameat %s", temp, f->path);
1.11      florian   495:                        (void)unlinkat(p->rootfd, temp, 0);
                    496:                        free(temp);
1.9       florian   497:                        return -1;
                    498:                }
1.11      florian   499:                free(temp);
1.1       benno     500:        }
1.17      benno     501:
1.11      florian   502:        log_file(sess, f);
1.1       benno     503:        return 0;
                    504: }
                    505:
                    506: /*
                    507:  * If not found, create the destination directory in prefix order.
                    508:  * Create directories using the existing umask.
                    509:  * Return <0 on failure 0 on success.
                    510:  */
                    511: static int
                    512: pre_dir(const struct upload *p, struct sess *sess)
                    513: {
                    514:        struct stat      st;
1.2       benno     515:        int              rc;
1.1       benno     516:        const struct flist *f;
                    517:
                    518:        f = &p->fl[p->idx];
                    519:        assert(S_ISDIR(f->st.mode));
                    520:
1.3       deraadt   521:        if (!sess->opts->recursive) {
1.21      benno     522:                WARNX("%s: ignoring directory", f->path);
1.1       benno     523:                return 0;
1.25    ! claudio   524:        }
        !           525:        if (sess->opts->dry_run) {
1.1       benno     526:                log_dir(sess, f);
                    527:                return 0;
                    528:        }
                    529:
1.4       deraadt   530:        assert(p->rootfd != -1);
1.1       benno     531:        rc = fstatat(p->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW);
1.17      benno     532:
1.4       deraadt   533:        if (rc == -1 && errno != ENOENT) {
1.21      benno     534:                ERR("%s: fstatat", f->path);
1.1       benno     535:                return -1;
1.4       deraadt   536:        } else if (rc != -1 && !S_ISDIR(st.st_mode)) {
1.21      benno     537:                ERRX("%s: not a directory", f->path);
1.1       benno     538:                return -1;
1.4       deraadt   539:        } else if (rc != -1) {
1.2       benno     540:                /*
1.1       benno     541:                 * FIXME: we should fchmod the permissions here as well,
                    542:                 * as we may locally have shut down writing into the
                    543:                 * directory and that doesn't work.
                    544:                 */
1.21      benno     545:                LOG3("%s: updating directory", f->path);
1.1       benno     546:                return 0;
                    547:        }
                    548:
                    549:        /*
                    550:         * We want to make the directory with default permissions (using
                    551:         * our old umask, which we've since unset), then adjust
                    552:         * permissions (assuming preserve_perms or new) afterward in
                    553:         * case it's u-w or something.
                    554:         */
                    555:
1.21      benno     556:        LOG3("%s: creating directory", f->path);
1.4       deraadt   557:        if (mkdirat(p->rootfd, f->path, 0777 & ~p->oumask) == -1) {
1.21      benno     558:                ERR("%s: mkdirat", f->path);
1.1       benno     559:                return -1;
                    560:        }
                    561:
                    562:        p->newdir[p->idx] = 1;
                    563:        log_dir(sess, f);
                    564:        return 0;
                    565: }
                    566:
                    567: /*
                    568:  * Process the directory time and mode for "idx" in the file list.
                    569:  * Returns zero on failure, non-zero on success.
                    570:  */
                    571: static int
                    572: post_dir(struct sess *sess, const struct upload *u, size_t idx)
                    573: {
                    574:        struct timespec  tv[2];
                    575:        int              rc;
                    576:        struct stat      st;
                    577:        const struct flist *f;
                    578:
                    579:        f = &u->fl[idx];
                    580:        assert(S_ISDIR(f->st.mode));
                    581:
                    582:        /* We already warned about the directory in pre_process_dir(). */
                    583:
1.3       deraadt   584:        if (!sess->opts->recursive)
1.1       benno     585:                return 1;
1.25    ! claudio   586:        if (sess->opts->dry_run)
1.1       benno     587:                return 1;
                    588:
1.4       deraadt   589:        if (fstatat(u->rootfd, f->path, &st, AT_SYMLINK_NOFOLLOW) == -1) {
1.21      benno     590:                ERR("%s: fstatat", f->path);
1.1       benno     591:                return 0;
1.25    ! claudio   592:        }
        !           593:        if (!S_ISDIR(st.st_mode)) {
1.21      benno     594:                WARNX("%s: not a directory", f->path);
1.1       benno     595:                return 0;
                    596:        }
                    597:
1.2       benno     598:        /*
1.1       benno     599:         * Update the modification time if we're a new directory *or* if
                    600:         * we're preserving times and the time has changed.
1.6       florian   601:         * FIXME: run rsync_set_metadata()?
1.1       benno     602:         */
                    603:
1.2       benno     604:        if (u->newdir[idx] ||
                    605:            (sess->opts->preserve_times &&
1.1       benno     606:             st.st_mtime != f->st.mtime)) {
                    607:                tv[0].tv_sec = time(NULL);
                    608:                tv[0].tv_nsec = 0;
                    609:                tv[1].tv_sec = f->st.mtime;
                    610:                tv[1].tv_nsec = 0;
                    611:                rc = utimensat(u->rootfd, f->path, tv, 0);
1.4       deraadt   612:                if (rc == -1) {
1.21      benno     613:                        ERR("%s: utimensat", f->path);
1.1       benno     614:                        return 0;
                    615:                }
1.21      benno     616:                LOG4("%s: updated date", f->path);
1.1       benno     617:        }
                    618:
                    619:        /*
                    620:         * Update the mode if we're a new directory *or* if we're
                    621:         * preserving modes and it has changed.
                    622:         */
                    623:
1.2       benno     624:        if (u->newdir[idx] ||
1.20      deraadt   625:            (sess->opts->preserve_perms && st.st_mode != f->st.mode)) {
1.1       benno     626:                rc = fchmodat(u->rootfd, f->path, f->st.mode, 0);
1.4       deraadt   627:                if (rc == -1) {
1.21      benno     628:                        ERR("%s: fchmodat", f->path);
1.1       benno     629:                        return 0;
                    630:                }
1.21      benno     631:                LOG4("%s: updated mode", f->path);
1.1       benno     632:        }
                    633:
                    634:        return 1;
                    635: }
                    636:
                    637: /*
                    638:  * Try to open the file at the current index.
1.25    ! claudio   639:  * If the file does not exist, returns with >0.
1.1       benno     640:  * Return <0 on failure, 0 on success w/nothing to be done, >0 on
                    641:  * success and the file needs attention.
                    642:  */
                    643: static int
1.25    ! claudio   644: pre_file(const struct upload *p, int *filefd, struct stat *st,
        !           645:     struct sess *sess)
1.1       benno     646: {
                    647:        const struct flist *f;
                    648:
                    649:        f = &p->fl[p->idx];
                    650:        assert(S_ISREG(f->st.mode));
                    651:
                    652:        if (sess->opts->dry_run) {
                    653:                log_file(sess, f);
1.3       deraadt   654:                if (!io_write_int(sess, p->fdout, p->idx)) {
1.21      benno     655:                        ERRX1("io_write_int");
1.1       benno     656:                        return -1;
                    657:                }
                    658:                return 0;
                    659:        }
                    660:
                    661:        /*
                    662:         * For non dry-run cases, we'll write the acknowledgement later
1.25    ! claudio   663:         * in the rsync_uploader() function.
1.1       benno     664:         * If the call to openat() fails with ENOENT, there's a
1.25    ! claudio   665:         * fast-path between here and the write function.
1.1       benno     666:         */
                    667:
                    668:        *filefd = openat(p->rootfd, f->path,
1.25    ! claudio   669:                O_RDONLY | O_NOFOLLOW, 0);
        !           670:
        !           671:        if (*filefd == -1 && errno != ENOENT) {
        !           672:                ERR("%s: openat", f->path);
        !           673:                return -1;
        !           674:        }
        !           675:        if (*filefd == -1)
1.1       benno     676:                return 1;
1.25    ! claudio   677:
        !           678:        /*
        !           679:         * Check if the file is already up to date, in which case close it
        !           680:         * and go to the next one.
        !           681:         */
        !           682:
        !           683:        if (fstat(*filefd, st) == -1) {
        !           684:                ERR("%s: fstat", f->path);
        !           685:                close(*filefd);
        !           686:                *filefd = -1;
        !           687:                return -1;
        !           688:        } else if (!S_ISREG(st->st_mode)) {
        !           689:                ERRX("%s: not regular", f->path);
        !           690:                close(*filefd);
        !           691:                *filefd = -1;
        !           692:                return -1;
        !           693:        }
        !           694:
        !           695:        if (st->st_size == f->st.size &&
        !           696:            st->st_mtime == f->st.mtime) {
        !           697:                LOG3("%s: skipping: up to date", f->path);
        !           698:                if (!rsync_set_metadata(sess, 0, *filefd, f, f->path)) {
        !           699:                        ERRX1("rsync_set_metadata");
        !           700:                        close(*filefd);
        !           701:                        *filefd = -1;
        !           702:                        return -1;
        !           703:                }
        !           704:                close(*filefd);
        !           705:                *filefd = -1;
        !           706:                return 0;
        !           707:        }
        !           708:
        !           709:        /* file needs attention */
        !           710:        return 1;
1.1       benno     711: }
                    712:
                    713: /*
                    714:  * Allocate an uploader object in the correct state to start.
                    715:  * Returns NULL on failure or the pointer otherwise.
                    716:  * On success, upload_free() must be called with the allocated pointer.
                    717:  */
                    718: struct upload *
1.22      benno     719: upload_alloc(const char *root, int rootfd, int fdout,
1.1       benno     720:        size_t clen, const struct flist *fl, size_t flsz, mode_t msk)
                    721: {
                    722:        struct upload   *p;
                    723:
1.4       deraadt   724:        if ((p = calloc(1, sizeof(struct upload))) == NULL) {
1.21      benno     725:                ERR("calloc");
1.1       benno     726:                return NULL;
                    727:        }
                    728:
                    729:        p->state = UPLOAD_FIND_NEXT;
                    730:        p->oumask = msk;
1.11      florian   731:        p->root = strdup(root);
                    732:        if (p->root == NULL) {
1.21      benno     733:                ERR("strdup");
1.11      florian   734:                free(p);
                    735:                return NULL;
                    736:        }
1.1       benno     737:        p->rootfd = rootfd;
                    738:        p->csumlen = clen;
                    739:        p->fdout = fdout;
                    740:        p->fl = fl;
                    741:        p->flsz = flsz;
                    742:        p->newdir = calloc(flsz, sizeof(int));
1.4       deraadt   743:        if (p->newdir == NULL) {
1.21      benno     744:                ERR("calloc");
1.11      florian   745:                free(p->root);
1.1       benno     746:                free(p);
                    747:                return NULL;
                    748:        }
                    749:        return p;
                    750: }
                    751:
                    752: /*
                    753:  * Perform all cleanups and free.
                    754:  * Passing a NULL to this function is ok.
                    755:  */
                    756: void
                    757: upload_free(struct upload *p)
                    758: {
                    759:
1.4       deraadt   760:        if (p == NULL)
1.1       benno     761:                return;
1.11      florian   762:        free(p->root);
1.1       benno     763:        free(p->newdir);
                    764:        free(p->buf);
                    765:        free(p);
                    766: }
                    767:
                    768: /*
                    769:  * Iterates through all available files and conditionally gets the file
                    770:  * ready for processing to check whether it's up to date.
                    771:  * If not up to date or empty, sends file information to the sender.
                    772:  * If returns 0, we've processed all files there are to process.
                    773:  * If returns >0, we're waiting for POLLIN or POLLOUT data.
                    774:  * Otherwise returns <0, which is an error.
                    775:  */
                    776: int
1.2       benno     777: rsync_uploader(struct upload *u, int *fileinfd,
1.1       benno     778:        struct sess *sess, int *fileoutfd)
                    779: {
1.5       florian   780:        struct blkset       blk;
                    781:        struct stat         st;
1.23      benno     782:        void               *mbuf, *bufp;
                    783:        ssize_t             msz;
                    784:        size_t              i, pos, sz;
1.5       florian   785:        off_t               offs;
                    786:        int                 c;
1.1       benno     787:
                    788:        /* This should never get called. */
                    789:
1.4       deraadt   790:        assert(u->state != UPLOAD_FINISHED);
1.1       benno     791:
                    792:        /*
                    793:         * If we have an upload in progress, then keep writing until the
                    794:         * buffer has been fully written.
                    795:         * We must only have the output file descriptor working and also
                    796:         * have a valid buffer to write.
                    797:         */
                    798:
1.25    ! claudio   799:        if (u->state == UPLOAD_WRITE) {
1.14      deraadt   800:                assert(u->buf != NULL);
1.4       deraadt   801:                assert(*fileoutfd != -1);
                    802:                assert(*fileinfd == -1);
1.1       benno     803:
                    804:                /*
                    805:                 * Unfortunately, we need to chunk these: if we're
                    806:                 * the server side of things, then we're multiplexing
                    807:                 * output and need to wrap this in chunks.
                    808:                 * This is a major deficiency of rsync.
                    809:                 * FIXME: add a "fast-path" mode that simply dumps out
                    810:                 * the buffer non-blocking if we're not mplexing.
                    811:                 */
                    812:
                    813:                if (u->bufpos < u->bufsz) {
                    814:                        sz = MAX_CHUNK < (u->bufsz - u->bufpos) ?
                    815:                                MAX_CHUNK : (u->bufsz - u->bufpos);
1.2       benno     816:                        c = io_write_buf(sess, u->fdout,
1.1       benno     817:                                u->buf + u->bufpos, sz);
1.4       deraadt   818:                        if (c == 0) {
1.21      benno     819:                                ERRX1("io_write_nonblocking");
1.1       benno     820:                                return -1;
                    821:                        }
                    822:                        u->bufpos += sz;
                    823:                        if (u->bufpos < u->bufsz)
                    824:                                return 1;
                    825:                }
                    826:
1.2       benno     827:                /*
1.1       benno     828:                 * Let the UPLOAD_FIND_NEXT state handle things if we
                    829:                 * finish, as we'll need to write a POLLOUT message and
                    830:                 * not have a writable descriptor yet.
                    831:                 */
                    832:
                    833:                u->state = UPLOAD_FIND_NEXT;
                    834:                u->idx++;
                    835:                return 1;
                    836:        }
                    837:
                    838:        /*
                    839:         * If we invoke the uploader without a file currently open, then
                    840:         * we iterate through til the next available regular file and
                    841:         * start the opening process.
                    842:         * This means we must have the output file descriptor working.
                    843:         */
                    844:
1.4       deraadt   845:        if (u->state == UPLOAD_FIND_NEXT) {
                    846:                assert(*fileinfd == -1);
                    847:                assert(*fileoutfd != -1);
1.1       benno     848:
                    849:                for ( ; u->idx < u->flsz; u->idx++) {
                    850:                        if (S_ISDIR(u->fl[u->idx].st.mode))
                    851:                                c = pre_dir(u, sess);
                    852:                        else if (S_ISLNK(u->fl[u->idx].st.mode))
                    853:                                c = pre_link(u, sess);
                    854:                        else if (S_ISREG(u->fl[u->idx].st.mode))
1.25    ! claudio   855:                                c = pre_file(u, fileinfd, &st, sess);
1.11      florian   856:                        else if (S_ISBLK(u->fl[u->idx].st.mode) ||
                    857:                            S_ISCHR(u->fl[u->idx].st.mode))
                    858:                                c = pre_dev(u, sess);
                    859:                        else if (S_ISFIFO(u->fl[u->idx].st.mode))
                    860:                                c = pre_fifo(u, sess);
                    861:                        else if (S_ISSOCK(u->fl[u->idx].st.mode))
                    862:                                c = pre_sock(u, sess);
1.1       benno     863:                        else
                    864:                                c = 0;
                    865:
                    866:                        if (c < 0)
                    867:                                return -1;
                    868:                        else if (c > 0)
                    869:                                break;
                    870:                }
                    871:
1.2       benno     872:                /*
1.1       benno     873:                 * Whether we've finished writing files or not, we
                    874:                 * disable polling on the output channel.
                    875:                 */
                    876:
                    877:                *fileoutfd = -1;
                    878:                if (u->idx == u->flsz) {
1.4       deraadt   879:                        assert(*fileinfd == -1);
1.3       deraadt   880:                        if (!io_write_int(sess, u->fdout, -1)) {
1.21      benno     881:                                ERRX1("io_write_int");
1.1       benno     882:                                return -1;
                    883:                        }
                    884:                        u->state = UPLOAD_FINISHED;
1.21      benno     885:                        LOG4("uploader: finished");
1.1       benno     886:                        return 0;
                    887:                }
                    888:
                    889:                /* Go back to the event loop, if necessary. */
                    890:
1.25    ! claudio   891:                u->state = UPLOAD_WRITE;
1.1       benno     892:        }
                    893:
                    894:        /* Initialies our blocks. */
                    895:
1.25    ! claudio   896:        assert(u->state == UPLOAD_WRITE);
1.1       benno     897:        memset(&blk, 0, sizeof(struct blkset));
                    898:        blk.csum = u->csumlen;
                    899:
1.4       deraadt   900:        if (*fileinfd != -1 && st.st_size > 0) {
1.23      benno     901:                init_blkset(&blk, st.st_size);
                    902:                assert(blk.blksz);
                    903:
                    904:                blk.blks = calloc(blk.blksz, sizeof(struct blk));
                    905:                if (blk.blks == NULL) {
                    906:                        ERR("calloc");
1.1       benno     907:                        close(*fileinfd);
                    908:                        *fileinfd = -1;
                    909:                        return -1;
                    910:                }
                    911:
1.25    ! claudio   912:                if ((mbuf = malloc(blk.len)) == NULL) {
        !           913:                        ERR("malloc");
1.1       benno     914:                        close(*fileinfd);
                    915:                        *fileinfd = -1;
                    916:                        return -1;
                    917:                }
                    918:
                    919:                offs = 0;
1.23      benno     920:                i = 0;
                    921:                do {
                    922:                        msz = pread(*fileinfd, mbuf, blk.len, offs);
1.25    ! claudio   923:                        if ((size_t)msz != blk.len && (size_t)msz != blk.rem) {
1.23      benno     924:                                ERR("pread");
                    925:                                close(*fileinfd);
                    926:                                *fileinfd = -1;
1.25    ! claudio   927:                                free(mbuf);
1.23      benno     928:                                return -1;
                    929:                        }
                    930:                        init_blk(&blk.blks[i], &blk, offs, i, mbuf, sess);
1.1       benno     931:                        offs += blk.len;
1.23      benno     932:                        LOG3(
                    933:                            "i=%ld, offs=%lld, msz=%ld, blk.len=%lu, blk.rem=%lu",
                    934:                            i, offs, msz, blk.len, blk.rem);
                    935:                        i++;
                    936:                } while (i < blk.blksz);
1.1       benno     937:
1.25    ! claudio   938:                free(mbuf);
1.1       benno     939:                close(*fileinfd);
                    940:                *fileinfd = -1;
1.21      benno     941:                LOG3("%s: mapped %jd B with %zu blocks",
1.19      deraadt   942:                    u->fl[u->idx].path, (intmax_t)blk.size,
                    943:                    blk.blksz);
1.1       benno     944:        } else {
1.4       deraadt   945:                if (*fileinfd != -1) {
1.1       benno     946:                        close(*fileinfd);
                    947:                        *fileinfd = -1;
                    948:                }
                    949:                blk.len = MAX_CHUNK; /* Doesn't matter. */
1.21      benno     950:                LOG3("%s: not mapped", u->fl[u->idx].path);
1.1       benno     951:        }
                    952:
1.4       deraadt   953:        assert(*fileinfd == -1);
1.1       benno     954:
                    955:        /* Make sure the block metadata buffer is big enough. */
                    956:
1.2       benno     957:        u->bufsz =
1.1       benno     958:             sizeof(int32_t) + /* identifier */
                    959:             sizeof(int32_t) + /* block count */
                    960:             sizeof(int32_t) + /* block length */
                    961:             sizeof(int32_t) + /* checksum length */
                    962:             sizeof(int32_t) + /* block remainder */
1.2       benno     963:             blk.blksz *
1.1       benno     964:             (sizeof(int32_t) + /* short checksum */
                    965:              blk.csum); /* long checksum */
                    966:
                    967:        if (u->bufsz > u->bufmax) {
1.4       deraadt   968:                if ((bufp = realloc(u->buf, u->bufsz)) == NULL) {
1.21      benno     969:                        ERR("realloc");
1.1       benno     970:                        return -1;
                    971:                }
                    972:                u->buf = bufp;
                    973:                u->bufmax = u->bufsz;
                    974:        }
                    975:
                    976:        u->bufpos = pos = 0;
1.22      benno     977:        io_buffer_int(u->buf, &pos, u->bufsz, u->idx);
                    978:        io_buffer_int(u->buf, &pos, u->bufsz, blk.blksz);
                    979:        io_buffer_int(u->buf, &pos, u->bufsz, blk.len);
                    980:        io_buffer_int(u->buf, &pos, u->bufsz, blk.csum);
                    981:        io_buffer_int(u->buf, &pos, u->bufsz, blk.rem);
1.1       benno     982:        for (i = 0; i < blk.blksz; i++) {
1.22      benno     983:                io_buffer_int(u->buf, &pos, u->bufsz,
1.1       benno     984:                        blk.blks[i].chksum_short);
1.22      benno     985:                io_buffer_buf(u->buf, &pos, u->bufsz,
1.1       benno     986:                        blk.blks[i].chksum_long, blk.csum);
                    987:        }
                    988:        assert(pos == u->bufsz);
                    989:
                    990:        /* Reenable the output poller and clean up. */
                    991:
                    992:        *fileoutfd = u->fdout;
                    993:        free(blk.blks);
                    994:        return 1;
                    995: }
                    996:
                    997: /*
                    998:  * Fix up the directory permissions and times post-order.
                    999:  * We can't fix up directory permissions in place because the server may
                   1000:  * want us to have overly-tight permissions---say, those that don't
                   1001:  * allow writing into the directory.
                   1002:  * We also need to do our directory times post-order because making
                   1003:  * files within the directory will change modification times.
                   1004:  * Returns zero on failure, non-zero on success.
                   1005:  */
                   1006: int
                   1007: rsync_uploader_tail(struct upload *u, struct sess *sess)
                   1008: {
                   1009:        size_t   i;
                   1010:
                   1011:
1.3       deraadt  1012:        if (!sess->opts->preserve_times &&
1.20      deraadt  1013:            !sess->opts->preserve_perms)
1.1       benno    1014:                return 1;
                   1015:
1.21      benno    1016:        LOG2("fixing up directory times and permissions");
1.1       benno    1017:
                   1018:        for (i = 0; i < u->flsz; i++)
                   1019:                if (S_ISDIR(u->fl[i].st.mode))
1.3       deraadt  1020:                        if (!post_dir(sess, u, i))
1.1       benno    1021:                                return 0;
                   1022:
                   1023:        return 1;
                   1024: }