Annotation of embedaddon/rsync/generator.c, revision 1.1.1.4

1.1       misho       1: /*
                      2:  * Routines that are exclusive to the generator process.
                      3:  *
                      4:  * Copyright (C) 1996-2000 Andrew Tridgell
                      5:  * Copyright (C) 1996 Paul Mackerras
                      6:  * Copyright (C) 2002 Martin Pool <mbp@samba.org>
1.1.1.4 ! misho       7:  * Copyright (C) 2003-2020 Wayne Davison
1.1       misho       8:  *
                      9:  * This program is free software; you can redistribute it and/or modify
                     10:  * it under the terms of the GNU General Public License as published by
                     11:  * the Free Software Foundation; either version 3 of the License, or
                     12:  * (at your option) any later version.
                     13:  *
                     14:  * This program is distributed in the hope that it will be useful,
                     15:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     16:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     17:  * GNU General Public License for more details.
                     18:  *
                     19:  * You should have received a copy of the GNU General Public License along
                     20:  * with this program; if not, visit the http://fsf.org website.
                     21:  */
                     22: 
                     23: #include "rsync.h"
1.1.1.2   misho      24: #include "inums.h"
                     25: #include "ifuncs.h"
1.1       misho      26: 
                     27: extern int dry_run;
                     28: extern int do_xfers;
                     29: extern int stdout_format_has_i;
                     30: extern int logfile_format_has_i;
                     31: extern int am_root;
                     32: extern int am_server;
                     33: extern int am_daemon;
                     34: extern int inc_recurse;
                     35: extern int relative_paths;
                     36: extern int implied_dirs;
                     37: extern int keep_dirlinks;
                     38: extern int preserve_acls;
                     39: extern int preserve_xattrs;
1.1.1.4 ! misho      40: extern int preserve_hfs_compression;
1.1       misho      41: extern int preserve_links;
                     42: extern int preserve_devices;
1.1.1.4 ! misho      43: extern int copy_devices;
        !            44: extern int write_devices;
1.1       misho      45: extern int preserve_specials;
                     46: extern int preserve_hard_links;
                     47: extern int preserve_executability;
1.1.1.4 ! misho      48: extern int preserve_fileflags;
1.1       misho      49: extern int preserve_perms;
                     50: extern int preserve_times;
1.1.1.4 ! misho      51: extern int force_change;
1.1       misho      52: extern int delete_mode;
                     53: extern int delete_before;
                     54: extern int delete_during;
                     55: extern int delete_after;
1.1.1.2   misho      56: extern int missing_args;
1.1       misho      57: extern int msgdone_cnt;
                     58: extern int ignore_errors;
1.1.1.4 ! misho      59: extern int checksum_files;
1.1       misho      60: extern int remove_source_files;
                     61: extern int delay_updates;
                     62: extern int update_only;
1.1.1.4 ! misho      63: extern int downdate_only;
1.1.1.2   misho      64: extern int human_readable;
1.1       misho      65: extern int ignore_existing;
                     66: extern int ignore_non_existing;
1.1.1.3   misho      67: extern int want_xattr_optim;
1.1.1.4 ! misho      68: extern int modify_window;
1.1       misho      69: extern int inplace;
1.1.1.4 ! misho      70: extern int use_db;
1.1       misho      71: extern int append_mode;
                     72: extern int make_backups;
                     73: extern int csum_length;
                     74: extern int ignore_times;
1.1.1.4 ! misho      75: extern int date_only;
1.1       misho      76: extern int size_only;
                     77: extern OFF_T max_size;
                     78: extern OFF_T min_size;
                     79: extern int io_error;
                     80: extern int flist_eof;
                     81: extern int allowed_lull;
                     82: extern int sock_f_out;
                     83: extern int protocol_version;
                     84: extern int file_total;
                     85: extern int fuzzy_basis;
                     86: extern int always_checksum;
1.1.1.4 ! misho      87: extern int flist_csum_len;
1.1       misho      88: extern char *partial_dir;
1.1.1.4 ! misho      89: extern int alt_dest_type;
        !            90: extern int detect_renamed;
1.1       misho      91: extern int whole_file;
                     92: extern int list_only;
                     93: extern int read_batch;
1.1.1.2   misho      94: extern int write_batch;
1.1       misho      95: extern int safe_symlinks;
1.1.1.4 ! misho      96: extern int32 block_size;
1.1       misho      97: extern int unsort_ndx;
                     98: extern int max_delete;
                     99: extern int force_delete;
                    100: extern int one_file_system;
1.1.1.2   misho     101: extern int skipped_deletes;
1.1       misho     102: extern dev_t filesystem_dev;
                    103: extern mode_t orig_umask;
                    104: extern uid_t our_uid;
1.1.1.2   misho     105: extern char *tmpdir;
                    106: extern char *basis_dir[MAX_BASIS_DIRS+1];
1.1       misho     107: extern struct file_list *cur_flist, *first_flist, *dir_flist;
1.1.1.2   misho     108: extern filter_rule_list filter_list, daemon_filter_list;
1.1.1.4 ! misho     109: extern struct file_list the_fattr_list;
1.1       misho     110: 
                    111: int maybe_ATTRS_REPORT = 0;
1.1.1.4 ! misho     112: int maybe_ATTRS_ACCURATE_TIME = 0;
1.1       misho     113: 
                    114: static dev_t dev_zero;
1.1.1.4 ! misho     115: static int unexplored_dirs = 1;
1.1       misho     116: static int deldelay_size = 0, deldelay_cnt = 0;
                    117: static char *deldelay_buf = NULL;
                    118: static int deldelay_fd = -1;
                    119: static int loopchk_limit;
                    120: static int dir_tweaking;
                    121: static int symlink_timeset_failed_flags;
                    122: static int need_retouch_dir_times;
                    123: static int need_retouch_dir_perms;
1.1.1.4 ! misho     124: static int started_whole_dir, upcoming_whole_dir;
1.1       misho     125: static const char *solo_file = NULL;
                    126: 
                    127: enum nonregtype {
1.1.1.4 ! misho     128:        TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
1.1       misho     129: };
                    130: 
                    131: /* Forward declarations. */
                    132: #ifdef SUPPORT_HARD_LINKS
                    133: static void handle_skipped_hlink(struct file_struct *file, int itemizing,
                    134:                                 enum logcode code, int f_out);
                    135: #endif
                    136: 
1.1.1.2   misho     137: #define EARLY_DELAY_DONE_MSG() (!delay_updates)
                    138: #define EARLY_DELETE_DONE_MSG() (!(delete_during == 2 || delete_after))
1.1       misho     139: 
                    140: static int start_delete_delay_temp(void)
                    141: {
                    142:        char fnametmp[MAXPATHLEN];
                    143:        int save_dry_run = dry_run;
                    144: 
                    145:        dry_run = 0;
1.1.1.2   misho     146:        if (!get_tmpname(fnametmp, "deldelay", False)
1.1       misho     147:         || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
                    148:                rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
                    149:                        inc_recurse ? "" : " -- switching to --delete-after");
                    150:                delete_during = 0;
                    151:                delete_after = !inc_recurse;
                    152:                dry_run = save_dry_run;
                    153:                return 0;
                    154:        }
                    155:        unlink(fnametmp);
                    156:        dry_run = save_dry_run;
                    157:        return 1;
                    158: }
                    159: 
                    160: static int flush_delete_delay(void)
                    161: {
                    162:        if (deldelay_fd < 0 && !start_delete_delay_temp())
                    163:                return 0;
                    164:        if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
                    165:                rsyserr(FERROR, errno, "flush of delete-delay buffer");
                    166:                delete_during = 0;
                    167:                delete_after = !inc_recurse;
                    168:                close(deldelay_fd);
                    169:                return 0;
                    170:        }
                    171:        deldelay_cnt = 0;
                    172:        return 1;
                    173: }
                    174: 
                    175: static int remember_delete(struct file_struct *file, const char *fname, int flags)
                    176: {
                    177:        int len;
                    178: 
                    179:        if (deldelay_cnt == deldelay_size && !flush_delete_delay())
                    180:                return 0;
                    181: 
                    182:        if (flags & DEL_NO_UID_WRITE)
                    183:                deldelay_buf[deldelay_cnt++] = '!';
                    184: 
                    185:        while (1) {
1.1.1.4 ! misho     186:                len = snprintf(deldelay_buf + deldelay_cnt, deldelay_size - deldelay_cnt,
        !           187:                               "%x %s%c", (int)file->mode, fname, '\0');
1.1       misho     188:                if ((deldelay_cnt += len) <= deldelay_size)
                    189:                        break;
                    190:                deldelay_cnt -= len;
                    191:                if (!flush_delete_delay())
                    192:                        return 0;
                    193:        }
                    194: 
                    195:        return 1;
                    196: }
                    197: 
                    198: static int read_delay_line(char *buf, int *flags_p)
                    199: {
                    200:        static int read_pos = 0;
                    201:        int j, len, mode;
                    202:        char *bp, *past_space;
                    203: 
                    204:        while (1) {
                    205:                for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
                    206:                if (j < deldelay_cnt)
                    207:                        break;
                    208:                if (deldelay_fd < 0) {
                    209:                        if (j > read_pos)
                    210:                                goto invalid_data;
                    211:                        return -1;
                    212:                }
                    213:                deldelay_cnt -= read_pos;
                    214:                if (deldelay_cnt == deldelay_size)
                    215:                        goto invalid_data;
                    216:                if (deldelay_cnt && read_pos) {
                    217:                        memmove(deldelay_buf, deldelay_buf + read_pos,
                    218:                                deldelay_cnt);
                    219:                }
                    220:                len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
                    221:                           deldelay_size - deldelay_cnt);
                    222:                if (len == 0) {
                    223:                        if (deldelay_cnt) {
1.1.1.4 ! misho     224:                                rprintf(FERROR, "ERROR: unexpected EOF in delete-delay file.\n");
1.1       misho     225:                        }
                    226:                        return -1;
                    227:                }
                    228:                if (len < 0) {
                    229:                        rsyserr(FERROR, errno,
                    230:                                "reading delete-delay file");
                    231:                        return -1;
                    232:                }
                    233:                deldelay_cnt += len;
                    234:                read_pos = 0;
                    235:        }
                    236: 
                    237:        bp = deldelay_buf + read_pos;
                    238:        if (*bp == '!') {
                    239:                bp++;
                    240:                *flags_p = DEL_NO_UID_WRITE;
                    241:        } else
                    242:                *flags_p = 0;
                    243: 
                    244:        if (sscanf(bp, "%x ", &mode) != 1) {
                    245:          invalid_data:
                    246:                rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
                    247:                return -1;
                    248:        }
                    249:        past_space = strchr(bp, ' ') + 1;
                    250:        len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
                    251:        read_pos = j + 1;
                    252: 
                    253:        if (len > MAXPATHLEN) {
                    254:                rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
                    255:                return -1;
                    256:        }
                    257: 
                    258:        /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
                    259:         * instead of returning a pointer to our buffer. */
                    260:        memcpy(buf, past_space, len);
                    261: 
                    262:        return mode;
                    263: }
                    264: 
                    265: static void do_delayed_deletions(char *delbuf)
                    266: {
                    267:        int mode, flags;
                    268: 
                    269:        if (deldelay_fd >= 0) {
                    270:                if (deldelay_cnt && !flush_delete_delay())
                    271:                        return;
                    272:                lseek(deldelay_fd, 0, 0);
                    273:        }
                    274:        while ((mode = read_delay_line(delbuf, &flags)) >= 0)
                    275:                delete_item(delbuf, mode, flags | DEL_RECURSE);
                    276:        if (deldelay_fd >= 0)
                    277:                close(deldelay_fd);
                    278: }
                    279: 
                    280: /* This function is used to implement per-directory deletion, and is used by
                    281:  * all the --delete-WHEN options.  Note that the fbuf pointer must point to a
                    282:  * MAXPATHLEN buffer with the name of the directory in it (the functions we
                    283:  * call will append names onto the end, but the old dir value will be restored
1.1.1.4 ! misho     284:  * on exit).
        !           285:  *
        !           286:  * Note:  --detect-rename may use this routine with DEL_NO_DELETIONS set!
        !           287:  */
        !           288: static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev,
        !           289:                          int del_flags)
1.1       misho     290: {
                    291:        static int already_warned = 0;
1.1.1.4 ! misho     292:        static struct hashtable *dev_tbl;
1.1       misho     293:        struct file_list *dirlist;
1.1.1.4 ! misho     294:        char *p, delbuf[MAXPATHLEN];
        !           295:        unsigned remainder;
        !           296:        int dlen, i, restore_dot = 0;
1.1       misho     297: 
                    298:        if (!fbuf) {
                    299:                change_local_filter_dir(NULL, 0, 0);
                    300:                return;
                    301:        }
                    302: 
1.1.1.2   misho     303:        if (DEBUG_GTE(DEL, 2))
1.1       misho     304:                rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
                    305: 
                    306:        if (allowed_lull)
1.1.1.2   misho     307:                maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
1.1       misho     308: 
1.1.1.2   misho     309:        if (io_error & IOERR_GENERAL && !ignore_errors) {
1.1.1.4 ! misho     310:                if (!already_warned) {
        !           311:                        rprintf(FINFO,
        !           312:                            "IO error encountered -- skipping file deletion\n");
        !           313:                        already_warned = 1;
        !           314:                }
        !           315:                if (!detect_renamed)
1.1       misho     316:                        return;
1.1.1.4 ! misho     317:                del_flags |= DEL_NO_DELETIONS;
1.1       misho     318:        }
                    319: 
                    320:        dlen = strlen(fbuf);
                    321:        change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
                    322: 
1.1.1.4 ! misho     323:        if (detect_renamed)
        !           324:                unexplored_dirs--;
        !           325: 
1.1       misho     326:        if (one_file_system) {
1.1.1.4 ! misho     327:                if (!dev_tbl)
        !           328:                        dev_tbl = hashtable_create(16, HT_KEY64);
        !           329:                if (file->flags & FLAG_TOP_DIR) {
        !           330:                        hashtable_find(dev_tbl, *fs_dev+1, "");
1.1       misho     331:                        filesystem_dev = *fs_dev;
1.1.1.4 ! misho     332:                } else if (filesystem_dev != *fs_dev) {
        !           333:                        if (!hashtable_find(dev_tbl, *fs_dev+1, NULL))
        !           334:                                return;
        !           335:                        filesystem_dev = *fs_dev; /* it's a prior top-dir dev */
        !           336:                }
1.1       misho     337:        }
                    338: 
                    339:        dirlist = get_dirlist(fbuf, dlen, 0);
                    340: 
1.1.1.4 ! misho     341:        p = fbuf + dlen;
        !           342:        if (dlen == 1 && *fbuf == '.') {
        !           343:                restore_dot = 1;
        !           344:                p = fbuf;
        !           345:        } else if (dlen != 1 || *fbuf != '/')
        !           346:                *p++ = '/';
        !           347:        remainder = MAXPATHLEN - (p - fbuf);
        !           348: 
1.1       misho     349:        /* If an item in dirlist is not found in flist, delete it
                    350:         * from the filesystem. */
                    351:        for (i = dirlist->used; i--; ) {
                    352:                struct file_struct *fp = dirlist->files[i];
                    353:                if (!F_IS_ACTIVE(fp))
                    354:                        continue;
                    355:                if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
1.1.1.2   misho     356:                        if (INFO_GTE(MOUNT, 1))
1.1       misho     357:                                rprintf(FINFO, "cannot delete mount point: %s\n",
                    358:                                        f_name(fp, NULL));
                    359:                        continue;
                    360:                }
1.1.1.4 ! misho     361:                if (detect_renamed && S_ISREG(fp->mode)) {
        !           362:                        strlcpy(p, fp->basename, remainder);
        !           363:                        look_for_rename(fp, fbuf);
        !           364:                }
1.1       misho     365:                /* Here we want to match regardless of file type.  Replacement
                    366:                 * of a file with one of another type is handled separately by
                    367:                 * a delete_item call with a DEL_MAKE_ROOM flag. */
                    368:                if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
                    369:                        int flags = DEL_RECURSE;
                    370:                        if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
                    371:                                flags |= DEL_NO_UID_WRITE;
                    372:                        f_name(fp, delbuf);
1.1.1.4 ! misho     373:                        if (delete_during == 2 && !(del_flags & DEL_NO_DELETIONS)) {
        !           374:                                if (!remember_delete(fp, delbuf, del_flags | flags))
1.1       misho     375:                                        break;
                    376:                        } else
1.1.1.4 ! misho     377:                                delete_item(delbuf, fp->mode, del_flags | flags);
        !           378:                } else if (detect_renamed && S_ISDIR(fp->mode))
        !           379:                        unexplored_dirs++;
1.1       misho     380:        }
                    381: 
1.1.1.4 ! misho     382:        if (restore_dot)
        !           383:                fbuf[0] = '.';
        !           384:        fbuf[dlen] = '\0';
        !           385: 
1.1       misho     386:        flist_free(dirlist);
                    387: }
                    388: 
                    389: /* This deletes any files on the receiving side that are not present on the
                    390:  * sending side.  This is used by --delete-before and --delete-after. */
                    391: static void do_delete_pass(void)
                    392: {
                    393:        char fbuf[MAXPATHLEN];
                    394:        STRUCT_STAT st;
                    395:        int j;
                    396: 
                    397:        /* dry_run is incremented when the destination doesn't exist yet. */
                    398:        if (dry_run > 1 || list_only)
                    399:                return;
                    400: 
                    401:        for (j = 0; j < cur_flist->used; j++) {
                    402:                struct file_struct *file = cur_flist->sorted[j];
                    403: 
1.1.1.3   misho     404:                if (!F_IS_ACTIVE(file))
                    405:                        continue;
                    406: 
1.1       misho     407:                f_name(file, fbuf);
                    408: 
                    409:                if (!(file->flags & FLAG_CONTENT_DIR)) {
                    410:                        change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
                    411:                        continue;
                    412:                }
                    413: 
1.1.1.2   misho     414:                if (DEBUG_GTE(DEL, 1) && file->flags & FLAG_TOP_DIR)
1.1       misho     415:                        rprintf(FINFO, "deleting in %s\n", fbuf);
                    416: 
                    417:                if (link_stat(fbuf, &st, keep_dirlinks) < 0
                    418:                 || !S_ISDIR(st.st_mode))
                    419:                        continue;
                    420: 
1.1.1.4 ! misho     421:                delete_in_dir(fbuf, file, &st.st_dev, 0);
1.1       misho     422:        }
1.1.1.4 ! misho     423:        delete_in_dir(NULL, NULL, &dev_zero, 0);
1.1       misho     424: 
1.1.1.2   misho     425:        if (INFO_GTE(FLIST, 2) && !am_server)
1.1       misho     426:                rprintf(FINFO, "                    \r");
                    427: }
                    428: 
1.1.1.4 ! misho     429: /* Search for a regular file that matches either (1) the size & modified
        !           430:  * time (plus the basename, if possible) or (2) the size & checksum.  If
        !           431:  * we find an exact match down to the dirname, return -1 because we found
        !           432:  * an up-to-date file in the transfer, not a renamed file. */
        !           433: static int fattr_find(struct file_struct *f, char *fname)
        !           434: {
        !           435:        int low = the_fattr_list.low, high = the_fattr_list.high;
        !           436:        int mid, ok_match = -1, good_match = -1;
        !           437:        struct file_struct *fmid;
        !           438:        int diff;
        !           439: 
        !           440:        while (low <= high) {
        !           441:                mid = (low + high) / 2;
        !           442:                fmid = the_fattr_list.files[mid];
        !           443:                if (F_LENGTH(fmid) != F_LENGTH(f)) {
        !           444:                        if (F_LENGTH(fmid) < F_LENGTH(f))
        !           445:                                low = mid + 1;
        !           446:                        else
        !           447:                                high = mid - 1;
        !           448:                        continue;
        !           449:                }
        !           450:                if (always_checksum) {
        !           451:                        /* We use the FLAG_FILE_SENT flag to indicate when we
        !           452:                         * have computed the checksum for an entry. */
        !           453:                        if (!(f->flags & FLAG_FILE_SENT)) {
        !           454:                                STRUCT_STAT st;
        !           455:                                if (fmid->modtime == f->modtime
        !           456:                                 && f_name_cmp(fmid, f) == 0)
        !           457:                                        return -1; /* assume we can't help */
        !           458:                                st.st_size = F_LENGTH(f);
        !           459:                                st.st_mtime = f->modtime;
        !           460:                                file_checksum(fname, &st, F_SUM(f));
        !           461:                                f->flags |= FLAG_FILE_SENT;
        !           462:                        }
        !           463:                        diff = u_memcmp(F_SUM(fmid), F_SUM(f), flist_csum_len);
        !           464:                        if (diff) {
        !           465:                                if (diff < 0)
        !           466:                                        low = mid + 1;
        !           467:                                else
        !           468:                                        high = mid - 1;
        !           469:                                continue;
        !           470:                        }
        !           471:                } else {
        !           472:                        if (fmid->modtime != f->modtime) {
        !           473:                                if (fmid->modtime < f->modtime)
        !           474:                                        low = mid + 1;
        !           475:                                else
        !           476:                                        high = mid - 1;
        !           477:                                continue;
        !           478:                        }
        !           479:                }
        !           480:                ok_match = mid;
        !           481:                diff = u_strcmp(fmid->basename, f->basename);
        !           482:                if (diff == 0) {
        !           483:                        good_match = mid;
        !           484:                        if (fmid->dirname == f->dirname)
        !           485:                                return -1; /* file is up-to-date */
        !           486:                        if (!fmid->dirname) {
        !           487:                                low = mid + 1;
        !           488:                                continue;
        !           489:                        }
        !           490:                        if (!f->dirname) {
        !           491:                                high = mid - 1;
        !           492:                                continue;
        !           493:                        }
        !           494:                        diff = u_strcmp(fmid->dirname, f->dirname);
        !           495:                        if (diff == 0)
        !           496:                                return -1; /* file is up-to-date */
        !           497:                }
        !           498:                if (diff < 0)
        !           499:                        low = mid + 1;
        !           500:                else
        !           501:                        high = mid - 1;
        !           502:        }
        !           503: 
        !           504:        return good_match >= 0 ? good_match : ok_match;
        !           505: }
        !           506: 
        !           507: void look_for_rename(struct file_struct *file, char *fname)
        !           508: {
        !           509:        struct file_struct *fp;
        !           510:        char *partialptr, *fn;
        !           511:        STRUCT_STAT st;
        !           512:        int ndx;
        !           513: 
        !           514:        if (!partial_dir || (ndx = fattr_find(file, fname)) < 0)
        !           515:                return;
        !           516: 
        !           517:        fp = the_fattr_list.files[ndx];
        !           518:        fn = f_name(fp, NULL);
        !           519:        /* We don't provide an alternate-basis file if there is a basis file. */
        !           520:        if (link_stat(fn, &st, 0) == 0)
        !           521:                return;
        !           522: 
        !           523:        if (!dry_run) {
        !           524:                if ((partialptr = partial_dir_fname(fn)) == NULL
        !           525:                 || !handle_partial_dir(partialptr, PDIR_CREATE))
        !           526:                        return;
        !           527:                /* We only use the file if we can hard-link it into our tmp dir. */
        !           528:                if (link(fname, partialptr) != 0) {
        !           529:                        if (errno != EEXIST)
        !           530:                                handle_partial_dir(partialptr, PDIR_DELETE);
        !           531:                        return;
        !           532:                }
        !           533:        }
        !           534: 
        !           535:        /* I think this falls into the -vv category with "%s is uptodate", etc. */
        !           536:        if (INFO_GTE(MISC, 2))
        !           537:                rprintf(FINFO, "found renamed: %s => %s\n", fname, fn);
        !           538: }
        !           539: 
        !           540: static inline int mtime_differs(STRUCT_STAT *stp, struct file_struct *file)
        !           541: {
        !           542: #ifdef ST_MTIME_NSEC
        !           543:        return !same_time(stp->st_mtime, stp->ST_MTIME_NSEC, file->modtime, F_MOD_NSEC_or_0(file));
        !           544: #else
        !           545:        return !same_time(stp->st_mtime, 0, file->modtime, 0);
        !           546: #endif
        !           547: }
        !           548: 
        !           549: static inline int any_time_differs(stat_x *sxp, struct file_struct *file, UNUSED(const char *fname))
1.1       misho     550: {
1.1.1.4 ! misho     551:        int differs = mtime_differs(&sxp->st, file);
        !           552: #ifdef SUPPORT_CRTIMES
        !           553:        if (!differs && crtimes_ndx) {
        !           554:                if (sxp->crtime == 0)
        !           555:                        sxp->crtime = get_create_time(fname);
        !           556:                differs = !same_time(sxp->crtime, 0, F_CRTIME(file), 0);
        !           557:        }
        !           558: #endif
        !           559:        return differs;
1.1       misho     560: }
                    561: 
                    562: static inline int perms_differ(struct file_struct *file, stat_x *sxp)
                    563: {
                    564:        if (preserve_perms)
                    565:                return !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS);
                    566: 
                    567:        if (preserve_executability)
                    568:                return (sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0);
                    569: 
                    570:        return 0;
                    571: }
                    572: 
                    573: static inline int ownership_differs(struct file_struct *file, stat_x *sxp)
                    574: {
                    575:        if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
                    576:                return 1;
                    577: 
                    578:        if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
                    579:                return 1;
                    580: 
                    581:        return 0;
                    582: }
                    583: 
                    584: #ifdef SUPPORT_ACLS
                    585: static inline int acls_differ(const char *fname, struct file_struct *file, stat_x *sxp)
                    586: {
                    587:        if (preserve_acls) {
                    588:                if (!ACL_READY(*sxp))
                    589:                        get_acl(fname, sxp);
                    590:                if (set_acl(NULL, file, sxp, file->mode))
                    591:                        return 1;
                    592:        }
                    593: 
                    594:        return 0;
                    595: }
                    596: #endif
                    597: 
                    598: #ifdef SUPPORT_XATTRS
                    599: static inline int xattrs_differ(const char *fname, struct file_struct *file, stat_x *sxp)
                    600: {
                    601:        if (preserve_xattrs) {
                    602:                if (!XATTR_READY(*sxp))
                    603:                        get_xattr(fname, sxp);
                    604:                if (xattr_diff(file, sxp, 0))
                    605:                        return 1;
                    606:        }
                    607: 
                    608:        return 0;
                    609: }
                    610: #endif
                    611: 
                    612: int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
                    613: {
                    614:        if (S_ISLNK(file->mode)) {
                    615: #ifdef CAN_SET_SYMLINK_TIMES
1.1.1.4 ! misho     616:                if (preserve_times & PRESERVE_LINK_TIMES && any_time_differs(sxp, file, fname))
1.1       misho     617:                        return 0;
                    618: #endif
                    619: #ifdef CAN_CHMOD_SYMLINK
                    620:                if (perms_differ(file, sxp))
                    621:                        return 0;
                    622: #endif
                    623: #ifdef CAN_CHOWN_SYMLINK
                    624:                if (ownership_differs(file, sxp))
                    625:                        return 0;
                    626: #endif
                    627: #if defined SUPPORT_ACLS && 0 /* no current symlink-ACL support */
                    628:                if (acls_differ(fname, file, sxp))
                    629:                        return 0;
                    630: #endif
                    631: #if defined SUPPORT_XATTRS && !defined NO_SYMLINK_XATTRS
                    632:                if (xattrs_differ(fname, file, sxp))
                    633:                        return 0;
                    634: #endif
                    635:        } else {
1.1.1.4 ! misho     636:                if (preserve_times && any_time_differs(sxp, file, fname))
1.1       misho     637:                        return 0;
                    638:                if (perms_differ(file, sxp))
                    639:                        return 0;
1.1.1.4 ! misho     640: #ifdef SUPPORT_FILEFLAGS
        !           641:                if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
        !           642:                        return 0;
        !           643: #endif
1.1       misho     644:                if (ownership_differs(file, sxp))
                    645:                        return 0;
                    646: #ifdef SUPPORT_ACLS
                    647:                if (acls_differ(fname, file, sxp))
                    648:                        return 0;
                    649: #endif
                    650: #ifdef SUPPORT_XATTRS
                    651:                if (xattrs_differ(fname, file, sxp))
                    652:                        return 0;
                    653: #endif
                    654:        }
                    655: 
                    656:        return 1;
                    657: }
                    658: 
                    659: void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
                    660:             stat_x *sxp, int32 iflags, uchar fnamecmp_type,
                    661:             const char *xname)
                    662: {
                    663:        if (statret >= 0) { /* A from-dest-dir statret can == 1! */
1.1.1.4 ! misho     664:                int omit_changes = omit_dir_changes && S_ISDIR(sxp->st.st_mode);
1.1       misho     665:                int keep_time = !preserve_times ? 0
                    666:                    : S_ISDIR(file->mode) ? preserve_times & PRESERVE_DIR_TIMES
                    667:                    : S_ISLNK(file->mode) ? preserve_times & PRESERVE_LINK_TIMES
                    668:                    : 1;
                    669: 
                    670:                if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
                    671:                        iflags |= ITEM_REPORT_SIZE;
                    672:                if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
                    673:                        if (iflags & ITEM_LOCAL_CHANGE)
                    674:                                iflags |= symlink_timeset_failed_flags;
                    675:                } else if (keep_time
1.1.1.4 ! misho     676:                 ? mtime_differs(&sxp->st, file)
1.1       misho     677:                 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
                    678:                  && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
                    679:                        iflags |= ITEM_REPORT_TIME;
1.1.1.4 ! misho     680:                if (atimes_ndx && !S_ISDIR(file->mode) && !S_ISLNK(file->mode)
        !           681:                 && !same_time(F_ATIME(file), 0, sxp->st.st_atime, 0))
        !           682:                        iflags |= ITEM_REPORT_ATIME;
        !           683: #ifdef SUPPORT_CRTIMES
        !           684:                if (crtimes_ndx) {
        !           685:                        if (sxp->crtime == 0)
        !           686:                                sxp->crtime = get_create_time(fnamecmp);
        !           687:                        if (!same_time(sxp->crtime, 0, F_CRTIME(file), 0))
        !           688:                                iflags |= ITEM_REPORT_CRTIME;
        !           689:                }
        !           690: #endif
1.1       misho     691: #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
                    692:                if (S_ISLNK(file->mode)) {
                    693:                        ;
                    694:                } else
                    695: #endif
                    696:                if (preserve_perms) {
                    697:                        if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
                    698:                                iflags |= ITEM_REPORT_PERMS;
                    699:                } else if (preserve_executability
                    700:                 && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
                    701:                        iflags |= ITEM_REPORT_PERMS;
1.1.1.4 ! misho     702:                if (uid_ndx && am_root && !omit_changes && (uid_t)F_OWNER(file) != sxp->st.st_uid)
1.1       misho     703:                        iflags |= ITEM_REPORT_OWNER;
1.1.1.4 ! misho     704:                if (gid_ndx && !omit_changes && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
1.1       misho     705:                        iflags |= ITEM_REPORT_GROUP;
1.1.1.4 ! misho     706: #ifdef SUPPORT_FILEFLAGS
        !           707:                if (preserve_fileflags && !S_ISLNK(file->mode)
        !           708:                 && sxp->st.st_flags != F_FFLAGS(file))
        !           709:                        iflags |= ITEM_REPORT_FFLAGS;
        !           710: #endif
1.1       misho     711: #ifdef SUPPORT_ACLS
                    712:                if (preserve_acls && !S_ISLNK(file->mode)) {
                    713:                        if (!ACL_READY(*sxp))
                    714:                                get_acl(fnamecmp, sxp);
                    715:                        if (set_acl(NULL, file, sxp, file->mode))
                    716:                                iflags |= ITEM_REPORT_ACL;
                    717:                }
                    718: #endif
                    719: #ifdef SUPPORT_XATTRS
                    720:                if (preserve_xattrs) {
                    721:                        if (!XATTR_READY(*sxp))
                    722:                                get_xattr(fnamecmp, sxp);
                    723:                        if (xattr_diff(file, sxp, 1))
                    724:                                iflags |= ITEM_REPORT_XATTR;
                    725:                }
                    726: #endif
                    727:        } else {
                    728: #ifdef SUPPORT_XATTRS
                    729:                if (preserve_xattrs && xattr_diff(file, NULL, 1))
                    730:                        iflags |= ITEM_REPORT_XATTR;
                    731: #endif
                    732:                iflags |= ITEM_IS_NEW;
                    733:        }
                    734: 
                    735:        iflags &= 0xffff;
1.1.1.2   misho     736:        if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
1.1       misho     737:          || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
                    738:                if (protocol_version >= 29) {
                    739:                        if (ndx >= 0)
                    740:                                write_ndx(sock_f_out, ndx);
                    741:                        write_shortint(sock_f_out, iflags);
                    742:                        if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
                    743:                                write_byte(sock_f_out, fnamecmp_type);
                    744:                        if (iflags & ITEM_XNAME_FOLLOWS)
                    745:                                write_vstring(sock_f_out, xname, strlen(xname));
                    746: #ifdef SUPPORT_XATTRS
                    747:                        if (preserve_xattrs && do_xfers
                    748:                         && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
1.1.1.2   misho     749:                                int fd = iflags & ITEM_REPORT_XATTR
1.1.1.3   misho     750:                                      && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
1.1.1.2   misho     751:                                       ? sock_f_out : -1;
                    752:                                send_xattr_request(NULL, file, fd);
1.1       misho     753:                        }
                    754: #endif
                    755:                } else if (ndx >= 0) {
                    756:                        enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
1.1.1.2   misho     757:                        log_item(code, file, iflags, xname);
1.1       misho     758:                }
                    759:        }
                    760: }
                    761: 
                    762: 
                    763: /* Perform our quick-check heuristic for determining if a file is unchanged. */
1.1.1.4 ! misho     764: int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st, int slot)
1.1       misho     765: {
1.1.1.4 ! misho     766:        if (date_only)
        !           767:                return !mtime_differs(st, file);
        !           768: 
1.1       misho     769:        if (st->st_size != F_LENGTH(file))
                    770:                return 0;
                    771: 
                    772:        /* if always checksum is set then we use the checksum instead
                    773:           of the file time to determine whether to sync */
                    774:        if (always_checksum > 0 && S_ISREG(st->st_mode)) {
                    775:                char sum[MAX_DIGEST_LEN];
1.1.1.4 ! misho     776:                if (checksum_files && slot >= 0)
        !           777:                        get_cached_checksum(slot, fn, file, -1, st, sum);
        !           778:                else
        !           779:                        file_checksum(fn, st, sum);
        !           780:                return memcmp(sum, F_SUM(file), flist_csum_len) == 0;
1.1       misho     781:        }
                    782: 
                    783:        if (size_only > 0)
                    784:                return 1;
                    785: 
                    786:        if (ignore_times)
                    787:                return 0;
                    788: 
1.1.1.4 ! misho     789:        return !mtime_differs(st, file);
1.1       misho     790: }
                    791: 
                    792: 
                    793: /*
                    794:  * set (initialize) the size entries in the per-file sum_struct
                    795:  * calculating dynamic block and checksum sizes.
                    796:  *
                    797:  * This is only called from generate_and_send_sums() but is a separate
                    798:  * function to encapsulate the logic.
                    799:  *
                    800:  * The block size is a rounded square root of file length.
                    801:  *
                    802:  * The checksum size is determined according to:
                    803:  *     blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
                    804:  * provided by Donovan Baarda which gives a probability of rsync
                    805:  * algorithm corrupting data and falling back using the whole md4
                    806:  * checksums.
                    807:  *
                    808:  * This might be made one of several selectable heuristics.
                    809:  */
                    810: static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
                    811: {
                    812:        int32 blength;
                    813:        int s2length;
                    814:        int64 l;
                    815: 
                    816:        if (len < 0) {
                    817:                /* The file length overflowed our int64 var, so we can't process this file. */
                    818:                sum->count = -1; /* indicate overflow error */
                    819:                return;
                    820:        }
                    821: 
                    822:        if (block_size)
                    823:                blength = block_size;
                    824:        else if (len <= BLOCK_SIZE * BLOCK_SIZE)
                    825:                blength = BLOCK_SIZE;
                    826:        else {
                    827:                int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
                    828:                int32 c;
                    829:                int cnt;
                    830:                for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
                    831:                if (c < 0 || c >= max_blength)
                    832:                        blength = max_blength;
                    833:                else {
1.1.1.4 ! misho     834:                        blength = 0;
        !           835:                        do {
        !           836:                                blength |= c;
        !           837:                                if (len < (int64)blength * blength)
        !           838:                                        blength &= ~c;
        !           839:                                c >>= 1;
        !           840:                        } while (c >= 8);       /* round to multiple of 8 */
        !           841:                        blength = MAX(blength, BLOCK_SIZE);
1.1       misho     842:                }
                    843:        }
                    844: 
                    845:        if (protocol_version < 27) {
                    846:                s2length = csum_length;
                    847:        } else if (csum_length == SUM_LENGTH) {
                    848:                s2length = SUM_LENGTH;
                    849:        } else {
                    850:                int32 c;
                    851:                int b = BLOCKSUM_BIAS;
                    852:                for (l = len; l >>= 1; b += 2) {}
                    853:                for (c = blength; (c >>= 1) && b; b--) {}
                    854:                /* add a bit, subtract rollsum, round up. */
                    855:                s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
                    856:                s2length = MAX(s2length, csum_length);
                    857:                s2length = MIN(s2length, SUM_LENGTH);
                    858:        }
                    859: 
                    860:        sum->flength    = len;
                    861:        sum->blength    = blength;
                    862:        sum->s2length   = s2length;
                    863:        sum->remainder  = (int32)(len % blength);
                    864:        sum->count      = (int32)(l = (len / blength) + (sum->remainder != 0));
                    865: 
                    866:        if ((int64)sum->count != l)
                    867:                sum->count = -1;
                    868: 
1.1.1.2   misho     869:        if (sum->count && DEBUG_GTE(DELTASUM, 2)) {
1.1       misho     870:                rprintf(FINFO,
1.1.1.2   misho     871:                        "count=%s rem=%ld blength=%ld s2length=%d flength=%s\n",
                    872:                        big_num(sum->count), (long)sum->remainder, (long)sum->blength,
                    873:                        sum->s2length, big_num(sum->flength));
1.1       misho     874:        }
                    875: }
                    876: 
                    877: 
                    878: /*
                    879:  * Generate and send a stream of signatures/checksums that describe a buffer
                    880:  *
                    881:  * Generate approximately one checksum every block_len bytes.
                    882:  */
                    883: static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
                    884: {
                    885:        int32 i;
                    886:        struct map_struct *mapbuf;
                    887:        struct sum_struct sum;
                    888:        OFF_T offset = 0;
                    889: 
                    890:        sum_sizes_sqroot(&sum, len);
                    891:        if (sum.count < 0)
                    892:                return -1;
                    893:        write_sum_head(f_out, &sum);
                    894: 
                    895:        if (append_mode > 0 && f_copy < 0)
                    896:                return 0;
                    897: 
1.1.1.4 ! misho     898:        if (len > 0) {
1.1       misho     899:                mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
1.1.1.4 ! misho     900:                checksum2_enable_prefetch(mapbuf, len, sum.blength);
        !           901:        } else {
1.1       misho     902:                mapbuf = NULL;
1.1.1.4 ! misho     903:        }
1.1       misho     904: 
                    905:        for (i = 0; i < sum.count; i++) {
                    906:                int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
                    907:                char *map = map_ptr(mapbuf, offset, n1);
                    908:                char sum2[SUM_LENGTH];
                    909:                uint32 sum1;
                    910: 
                    911:                len -= n1;
                    912:                offset += n1;
                    913: 
                    914:                if (f_copy >= 0) {
                    915:                        full_write(f_copy, map, n1);
                    916:                        if (append_mode > 0)
                    917:                                continue;
                    918:                }
                    919: 
                    920:                sum1 = get_checksum1(map, n1);
1.1.1.4 ! misho     921:                get_checksum2(map, n1, sum2, offset - n1);
1.1       misho     922: 
1.1.1.2   misho     923:                if (DEBUG_GTE(DELTASUM, 3)) {
1.1       misho     924:                        rprintf(FINFO,
1.1.1.2   misho     925:                                "chunk[%s] offset=%s len=%ld sum1=%08lx\n",
                    926:                                big_num(i), big_num(offset - n1), (long)n1,
1.1       misho     927:                                (unsigned long)sum1);
                    928:                }
                    929:                write_int(f_out, sum1);
                    930:                write_buf(f_out, sum2, sum.s2length);
                    931:        }
                    932: 
1.1.1.4 ! misho     933:        if (mapbuf) {
1.1       misho     934:                unmap_file(mapbuf);
1.1.1.4 ! misho     935:                checksum2_disable_prefetch();
        !           936:        }
1.1       misho     937: 
                    938:        return 0;
                    939: }
                    940: 
                    941: 
                    942: /* Try to find a filename in the same dir as "fname" with a similar name. */
1.1.1.2   misho     943: static struct file_struct *find_fuzzy(struct file_struct *file, struct file_list *dirlist_array[], uchar *fnamecmp_type_ptr)
1.1       misho     944: {
                    945:        int fname_len, fname_suf_len;
                    946:        const char *fname_suf, *fname = file->basename;
                    947:        uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
1.1.1.2   misho     948:        int i, j;
                    949:        struct file_struct *lowest_fp = NULL;
1.1       misho     950: 
                    951:        fname_len = strlen(fname);
                    952:        fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
                    953: 
1.1.1.2   misho     954:        /* Try to find an exact size+mtime match first. */
                    955:        for (i = 0; i < fuzzy_basis; i++) {
                    956:                struct file_list *dirlist = dirlist_array[i];
1.1       misho     957: 
1.1.1.2   misho     958:                if (!dirlist)
1.1       misho     959:                        continue;
                    960: 
1.1.1.2   misho     961:                for (j = 0; j < dirlist->used; j++) {
                    962:                        struct file_struct *fp = dirlist->files[j];
1.1       misho     963: 
1.1.1.3   misho     964:                        if (!F_IS_ACTIVE(fp))
                    965:                                continue;
                    966: 
1.1.1.2   misho     967:                        if (!S_ISREG(fp->mode) || !F_LENGTH(fp) || fp->flags & FLAG_FILE_SENT)
                    968:                                continue;
                    969: 
1.1.1.4 ! misho     970:                        if (F_LENGTH(fp) == F_LENGTH(file) && same_time(fp->modtime, 0, file->modtime, 0)) {
1.1.1.2   misho     971:                                if (DEBUG_GTE(FUZZY, 2))
                    972:                                        rprintf(FINFO, "fuzzy size/modtime match for %s\n", f_name(fp, NULL));
                    973:                                *fnamecmp_type_ptr = FNAMECMP_FUZZY + i;
                    974:                                return fp;
1.1       misho     975:                        }
1.1.1.2   misho     976: 
1.1       misho     977:                }
1.1.1.2   misho     978:        }
1.1       misho     979: 
1.1.1.2   misho     980:        for (i = 0; i < fuzzy_basis; i++) {
                    981:                struct file_list *dirlist = dirlist_array[i];
1.1       misho     982: 
1.1.1.2   misho     983:                if (!dirlist)
                    984:                        continue;
                    985: 
                    986:                for (j = 0; j < dirlist->used; j++) {
                    987:                        struct file_struct *fp = dirlist->files[j];
                    988:                        const char *suf, *name;
                    989:                        int len, suf_len;
                    990:                        uint32 dist;
                    991: 
1.1.1.3   misho     992:                        if (!F_IS_ACTIVE(fp))
                    993:                                continue;
                    994: 
1.1.1.2   misho     995:                        if (!S_ISREG(fp->mode) || !F_LENGTH(fp) || fp->flags & FLAG_FILE_SENT)
                    996:                                continue;
                    997: 
                    998:                        name = fp->basename;
                    999:                        len = strlen(name);
                   1000:                        suf = find_filename_suffix(name, len, &suf_len);
                   1001: 
                   1002:                        dist = fuzzy_distance(name, len, fname, fname_len);
                   1003:                        /* Add some extra weight to how well the suffixes match. */
                   1004:                        dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len) * 10;
                   1005:                        if (DEBUG_GTE(FUZZY, 2)) {
                   1006:                                rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
                   1007:                                        f_name(fp, NULL), (int)(dist>>16), (int)(dist&0xFFFF));
                   1008:                        }
                   1009:                        if (dist <= lowest_dist) {
                   1010:                                lowest_dist = dist;
                   1011:                                lowest_fp = fp;
                   1012:                                *fnamecmp_type_ptr = FNAMECMP_FUZZY + i;
                   1013:                        }
1.1       misho    1014:                }
                   1015:        }
                   1016: 
1.1.1.2   misho    1017:        return lowest_fp;
1.1       misho    1018: }
                   1019: 
                   1020: /* Copy a file found in our --copy-dest handling. */
                   1021: static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
                   1022: {
                   1023:        char buf[MAXPATHLEN];
                   1024:        const char *copy_to, *partialptr;
                   1025:        int save_preserve_xattrs = preserve_xattrs;
                   1026:        int ok, fd_w;
                   1027: 
                   1028:        if (inplace) {
                   1029:                /* Let copy_file open the destination in place. */
                   1030:                fd_w = -1;
                   1031:                copy_to = dest;
                   1032:        } else {
                   1033:                fd_w = open_tmpfile(buf, dest, file);
                   1034:                if (fd_w < 0)
                   1035:                        return -1;
                   1036:                copy_to = buf;
                   1037:        }
                   1038:        cleanup_set(copy_to, NULL, NULL, -1, -1);
1.1.1.2   misho    1039:        if (copy_file(src, copy_to, fd_w, file->mode) < 0) {
                   1040:                if (INFO_GTE(COPY, 1)) {
1.1       misho    1041:                        rsyserr(FINFO, errno, "copy_file %s => %s",
                   1042:                                full_fname(src), copy_to);
                   1043:                }
                   1044:                /* Try to clean up. */
                   1045:                unlink(copy_to);
                   1046:                cleanup_disable();
                   1047:                return -1;
                   1048:        }
                   1049:        partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
                   1050:        preserve_xattrs = 0; /* xattrs were copied with file */
                   1051:        ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
                   1052:        preserve_xattrs = save_preserve_xattrs;
                   1053:        cleanup_disable();
                   1054:        return ok ? 0 : -1;
                   1055: }
                   1056: 
                   1057: /* This is only called for regular files.  We return -2 if we've finished
                   1058:  * handling the file, -1 if no dest-linking occurred, or a non-negative
1.1.1.2   misho    1059:  * value if we found an alternate basis file.  If we're called with the
                   1060:  * find_exact_for_existing flag, the destination file already exists, so
                   1061:  * we only try to find an exact alt-dest match.  In this case, the returns
                   1062:  * are only -2 & -1 (both as above). */
1.1       misho    1063: static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
1.1.1.2   misho    1064:                         char *cmpbuf, stat_x *sxp, int find_exact_for_existing,
                   1065:                         int itemizing, enum logcode code)
1.1       misho    1066: {
1.1.1.2   misho    1067:        STRUCT_STAT real_st = sxp->st;
1.1       misho    1068:        int best_match = -1;
                   1069:        int match_level = 0;
                   1070:        int j = 0;
                   1071: 
                   1072:        do {
                   1073:                pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
                   1074:                if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
                   1075:                        continue;
1.1.1.4 ! misho    1076:                if (match_level == 0) {
1.1       misho    1077:                        best_match = j;
                   1078:                        match_level = 1;
1.1.1.4 ! misho    1079:                }
        !          1080:                if (!unchanged_file(cmpbuf, file, &sxp->st, j+1))
        !          1081:                        continue;
        !          1082:                if (match_level == 1) {
1.1       misho    1083:                        best_match = j;
                   1084:                        match_level = 2;
1.1.1.4 ! misho    1085:                }
        !          1086:                if (alt_dest_type == CLONE_DEST || unchanged_attrs(cmpbuf, file, sxp)) {
1.1       misho    1087:                        best_match = j;
                   1088:                        match_level = 3;
                   1089:                        break;
                   1090:                }
1.1.1.4 ! misho    1091:                free_stat_x(sxp);
1.1       misho    1092:        } while (basis_dir[++j] != NULL);
                   1093: 
                   1094:        if (!match_level)
1.1.1.3   misho    1095:                goto got_nothing_for_ya;
1.1       misho    1096: 
                   1097:        if (j != best_match) {
                   1098:                j = best_match;
                   1099:                pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
                   1100:                if (link_stat(cmpbuf, &sxp->st, 0) < 0)
1.1.1.3   misho    1101:                        goto got_nothing_for_ya;
1.1       misho    1102:        }
                   1103: 
1.1.1.4 ! misho    1104:        if (match_level == 3 && alt_dest_type != COPY_DEST) {
1.1.1.2   misho    1105:                if (find_exact_for_existing) {
1.1.1.4 ! misho    1106:                        if (alt_dest_type == LINK_DEST && real_st.st_dev == sxp->st.st_dev && real_st.st_ino == sxp->st.st_ino)
1.1.1.2   misho    1107:                                return -1;
1.1.1.3   misho    1108:                        if (do_unlink(fname) < 0 && errno != ENOENT)
                   1109:                                goto got_nothing_for_ya;
1.1.1.2   misho    1110:                }
1.1       misho    1111: #ifdef SUPPORT_HARD_LINKS
1.1.1.4 ! misho    1112:                if (alt_dest_type == LINK_DEST) {
1.1       misho    1113:                        if (!hard_link_one(file, fname, cmpbuf, 1))
                   1114:                                goto try_a_copy;
1.1.1.4 ! misho    1115:                        if (atimes_ndx)
        !          1116:                                set_file_attrs(fname, file, sxp, NULL, 0);
1.1       misho    1117:                        if (preserve_hard_links && F_IS_HLINKED(file))
                   1118:                                finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
1.1.1.2   misho    1119:                        if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1.1       misho    1120:                                itemize(cmpbuf, file, ndx, 1, sxp,
                   1121:                                        ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
                   1122:                                        0, "");
                   1123:                        }
                   1124:                } else
                   1125: #endif
1.1.1.4 ! misho    1126:                if (alt_dest_type == CLONE_DEST) {
        !          1127:                        if (do_clone(cmpbuf, fname, file->mode) < 0) {
        !          1128:                                rsyserr(FERROR_XFER, errno, "failed to clone %s to %s", cmpbuf, fname);
        !          1129:                                exit_cleanup(RERR_UNSUPPORTED);
        !          1130:                        }
        !          1131:                } else {
1.1.1.2   misho    1132:                        if (itemizing)
                   1133:                                itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
                   1134:                }
                   1135:                if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
1.1       misho    1136:                        rprintf(FCLIENT, "%s is uptodate\n", fname);
                   1137:                return -2;
                   1138:        }
                   1139: 
1.1.1.3   misho    1140:        if (find_exact_for_existing)
                   1141:                goto got_nothing_for_ya;
1.1.1.2   misho    1142: 
1.1       misho    1143:        if (match_level >= 2) {
                   1144: #ifdef SUPPORT_HARD_LINKS
                   1145:          try_a_copy: /* Copy the file locally. */
                   1146: #endif
1.1.1.2   misho    1147:                if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0) {
                   1148:                        if (find_exact_for_existing) /* Can get here via hard-link failure */
1.1.1.3   misho    1149:                                goto got_nothing_for_ya;
1.1       misho    1150:                        return -1;
1.1.1.2   misho    1151:                }
1.1       misho    1152:                if (itemizing)
                   1153:                        itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
                   1154:                if (maybe_ATTRS_REPORT
1.1.1.2   misho    1155:                 && ((!itemizing && INFO_GTE(NAME, 1) && match_level == 2)
                   1156:                  || (INFO_GTE(NAME, 2) && match_level == 3))) {
1.1       misho    1157:                        code = match_level == 3 ? FCLIENT : FINFO;
                   1158:                        rprintf(code, "%s%s\n", fname,
                   1159:                                match_level == 3 ? " is uptodate" : "");
                   1160:                }
                   1161: #ifdef SUPPORT_HARD_LINKS
                   1162:                if (preserve_hard_links && F_IS_HLINKED(file))
                   1163:                        finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
                   1164: #endif
                   1165:                return -2;
                   1166:        }
                   1167: 
                   1168:        return FNAMECMP_BASIS_DIR_LOW + j;
1.1.1.3   misho    1169: 
                   1170: got_nothing_for_ya:
                   1171:        sxp->st = real_st;
                   1172:        return -1;
1.1       misho    1173: }
                   1174: 
                   1175: /* This is only called for non-regular files.  We return -2 if we've finished
                   1176:  * handling the file, or -1 if no dest-linking occurred, or a non-negative
                   1177:  * value if we found an alternate basis file. */
                   1178: static int try_dests_non(struct file_struct *file, char *fname, int ndx,
                   1179:                         char *cmpbuf, stat_x *sxp, int itemizing,
                   1180:                         enum logcode code)
                   1181: {
                   1182:        int best_match = -1;
                   1183:        int match_level = 0;
                   1184:        enum nonregtype type;
                   1185:        uint32 *devp;
1.1.1.2   misho    1186: #ifdef SUPPORT_LINKS
                   1187:        char lnk[MAXPATHLEN];
                   1188:        int len;
                   1189: #endif
                   1190:        int j = 0;
1.1       misho    1191: 
                   1192: #ifndef SUPPORT_LINKS
                   1193:        if (S_ISLNK(file->mode))
                   1194:                return -1;
                   1195: #endif
                   1196:        if (S_ISDIR(file->mode)) {
                   1197:                type = TYPE_DIR;
                   1198:        } else if (IS_SPECIAL(file->mode))
                   1199:                type = TYPE_SPECIAL;
                   1200:        else if (IS_DEVICE(file->mode))
                   1201:                type = TYPE_DEVICE;
                   1202: #ifdef SUPPORT_LINKS
                   1203:        else if (S_ISLNK(file->mode))
                   1204:                type = TYPE_SYMLINK;
                   1205: #endif
                   1206:        else {
                   1207:                rprintf(FERROR,
                   1208:                        "internal: try_dests_non() called with invalid mode (%o)\n",
                   1209:                        (int)file->mode);
                   1210:                exit_cleanup(RERR_UNSUPPORTED);
                   1211:        }
                   1212: 
                   1213:        do {
                   1214:                pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
                   1215:                if (link_stat(cmpbuf, &sxp->st, 0) < 0)
                   1216:                        continue;
                   1217:                switch (type) {
                   1218:                case TYPE_DIR:
                   1219:                        if (!S_ISDIR(sxp->st.st_mode))
                   1220:                                continue;
                   1221:                        break;
                   1222:                case TYPE_SPECIAL:
                   1223:                        if (!IS_SPECIAL(sxp->st.st_mode))
                   1224:                                continue;
                   1225:                        break;
                   1226:                case TYPE_DEVICE:
                   1227:                        if (!IS_DEVICE(sxp->st.st_mode))
                   1228:                                continue;
                   1229:                        break;
                   1230:                case TYPE_SYMLINK:
1.1.1.2   misho    1231: #ifdef SUPPORT_LINKS
1.1       misho    1232:                        if (!S_ISLNK(sxp->st.st_mode))
                   1233:                                continue;
                   1234:                        break;
1.1.1.2   misho    1235: #else
                   1236:                        return -1;
1.1       misho    1237: #endif
                   1238:                }
                   1239:                if (match_level < 1) {
                   1240:                        match_level = 1;
                   1241:                        best_match = j;
                   1242:                }
                   1243:                switch (type) {
                   1244:                case TYPE_DIR:
                   1245:                case TYPE_SPECIAL:
                   1246:                        break;
                   1247:                case TYPE_DEVICE:
                   1248:                        devp = F_RDEV_P(file);
                   1249:                        if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
                   1250:                                continue;
                   1251:                        break;
                   1252:                case TYPE_SYMLINK:
1.1.1.2   misho    1253: #ifdef SUPPORT_LINKS
                   1254:                        if ((len = do_readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
1.1       misho    1255:                                continue;
                   1256:                        lnk[len] = '\0';
                   1257:                        if (strcmp(lnk, F_SYMLINK(file)) != 0)
                   1258:                                continue;
                   1259:                        break;
1.1.1.2   misho    1260: #else
                   1261:                        return -1;
1.1       misho    1262: #endif
                   1263:                }
                   1264:                if (match_level < 2) {
                   1265:                        match_level = 2;
                   1266:                        best_match = j;
                   1267:                }
                   1268:                if (unchanged_attrs(cmpbuf, file, sxp)) {
                   1269:                        match_level = 3;
                   1270:                        best_match = j;
                   1271:                        break;
                   1272:                }
                   1273:        } while (basis_dir[++j] != NULL);
                   1274: 
                   1275:        if (!match_level)
                   1276:                return -1;
                   1277: 
                   1278:        if (j != best_match) {
                   1279:                j = best_match;
                   1280:                pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
                   1281:                if (link_stat(cmpbuf, &sxp->st, 0) < 0)
                   1282:                        return -1;
                   1283:        }
                   1284: 
                   1285:        if (match_level == 3) {
                   1286: #ifdef SUPPORT_HARD_LINKS
1.1.1.4 ! misho    1287:                if ((alt_dest_type == LINK_DEST || alt_dest_type == CLONE_DEST)
1.1       misho    1288: #ifndef CAN_HARDLINK_SYMLINK
                   1289:                 && !S_ISLNK(file->mode)
                   1290: #endif
                   1291: #ifndef CAN_HARDLINK_SPECIAL
                   1292:                 && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
                   1293: #endif
                   1294:                 && !S_ISDIR(file->mode)) {
                   1295:                        if (do_link(cmpbuf, fname) < 0) {
                   1296:                                rsyserr(FERROR_XFER, errno,
                   1297:                                        "failed to hard-link %s with %s",
                   1298:                                        cmpbuf, fname);
                   1299:                                return j;
                   1300:                        }
                   1301:                        if (preserve_hard_links && F_IS_HLINKED(file))
                   1302:                                finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
                   1303:                } else
                   1304: #endif
                   1305:                        match_level = 2;
                   1306:                if (itemizing && stdout_format_has_i
1.1.1.2   misho    1307:                 && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
1.1.1.4 ! misho    1308:                        int chg = alt_dest_type == COMPARE_DEST && type != TYPE_DIR ? 0
1.1       misho    1309:                            : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
                   1310:                        char *lp = match_level == 3 ? "" : NULL;
                   1311:                        itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
                   1312:                }
1.1.1.2   misho    1313:                if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT) {
1.1       misho    1314:                        rprintf(FCLIENT, "%s%s is uptodate\n",
                   1315:                                fname, type == TYPE_DIR ? "/" : "");
                   1316:                }
                   1317:                return -2;
                   1318:        }
                   1319: 
                   1320:        return j;
                   1321: }
                   1322: 
                   1323: static void list_file_entry(struct file_struct *f)
                   1324: {
                   1325:        char permbuf[PERMSTRING_SIZE];
1.1.1.4 ! misho    1326:        const char *mtime_str = timestring(f->modtime);
        !          1327:        int size_width = human_readable ? 14 : 11;
        !          1328:        int mtime_width = 1 + strlen(mtime_str);
        !          1329:        int atime_width = atimes_ndx ? mtime_width : 0;
        !          1330:        int crtime_width = crtimes_ndx ? mtime_width : 0;
1.1       misho    1331: 
                   1332:        if (!F_IS_ACTIVE(f)) {
                   1333:                /* this can happen if duplicate names were removed */
                   1334:                return;
                   1335:        }
                   1336: 
                   1337:        /* TODO: indicate '+' if the entry has an ACL. */
                   1338: 
1.1.1.2   misho    1339:        if (missing_args == 2 && f->mode == 0) {
                   1340:                rprintf(FINFO, "%-*s %s\n",
1.1.1.4 ! misho    1341:                        10 + 1 + size_width + mtime_width + atime_width + crtime_width, "*missing",
1.1       misho    1342:                        f_name(f, NULL));
1.1.1.2   misho    1343:        } else {
1.1.1.4 ! misho    1344:                const char *atime_str = atimes_ndx && !S_ISDIR(f->mode) ? timestring(F_ATIME(f)) : "";
        !          1345:                const char *crtime_str = crtimes_ndx ? timestring(F_CRTIME(f)) : "";
        !          1346:                const char *arrow, *lnk;
        !          1347: 
        !          1348:                permstring(permbuf, f->mode);
        !          1349: 
        !          1350: #ifdef SUPPORT_LINKS
        !          1351:                if (preserve_links && S_ISLNK(f->mode)) {
        !          1352:                        arrow = " -> ";
        !          1353:                        lnk = F_SYMLINK(f);
        !          1354:                } else
        !          1355: #endif
        !          1356:                        arrow = lnk = "";
        !          1357: 
        !          1358:                rprintf(FINFO, "%s %*s %s%*s%*s %s%s%s\n",
        !          1359:                        permbuf, size_width, human_num(F_LENGTH(f)),
        !          1360:                        timestring(f->modtime), atime_width, atime_str, crtime_width, crtime_str,
        !          1361:                        f_name(f, NULL), arrow, lnk);
1.1       misho    1362:        }
                   1363: }
                   1364: 
1.1.1.4 ! misho    1365: static struct bitbag *delayed_bits = NULL;
1.1       misho    1366: static int phase = 0;
                   1367: static int dflt_perms;
                   1368: 
                   1369: static int implied_dirs_are_missing;
                   1370: /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
                   1371: static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
                   1372: {
                   1373:        return F_DEPTH(file) > F_DEPTH(subtree)
                   1374:                && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
                   1375: }
                   1376: 
                   1377: /* Acts on the indicated item in cur_flist whose name is fname.  If a dir,
                   1378:  * make sure it exists, and has the right permissions/timestamp info.  For
                   1379:  * all other non-regular files (symlinks, etc.) we create them here.  For
                   1380:  * regular files that have changed, we try to find a basis file and then
                   1381:  * start sending checksums.  The ndx is the file's unique index value.
                   1382:  *
                   1383:  * The fname parameter must point to a MAXPATHLEN buffer!  (e.g it gets
                   1384:  * passed to delete_item(), which can use it during a recursive delete.)
                   1385:  *
                   1386:  * Note that f_out is set to -1 when doing final directory-permission and
                   1387:  * modification-time repair. */
                   1388: static void recv_generator(char *fname, struct file_struct *file, int ndx,
                   1389:                           int itemizing, enum logcode code, int f_out)
                   1390: {
                   1391:        static const char *parent_dirname = "";
1.1.1.3   misho    1392:        static struct file_struct *prior_dir_file = NULL;
1.1       misho    1393:        /* Missing dir not created due to --dry-run; will still be scanned. */
                   1394:        static struct file_struct *dry_missing_dir = NULL;
                   1395:        /* Missing dir whose contents are skipped altogether due to
                   1396:         * --ignore-non-existing, daemon exclude, or mkdir failure. */
                   1397:        static struct file_struct *skip_dir = NULL;
1.1.1.2   misho    1398:        static struct file_list *fuzzy_dirlist[MAX_BASIS_DIRS+1];
1.1.1.4 ! misho    1399:        static int need_new_dirscan = 0;
1.1       misho    1400:        struct file_struct *fuzzy_file = NULL;
                   1401:        int fd = -1, f_copy = -1;
                   1402:        stat_x sx, real_sx;
                   1403:        STRUCT_STAT partial_st;
                   1404:        struct file_struct *back_file = NULL;
                   1405:        int statret, real_ret, stat_errno;
                   1406:        char *fnamecmp, *partialptr, *backupptr = NULL;
                   1407:        char fnamecmpbuf[MAXPATHLEN];
                   1408:        uchar fnamecmp_type;
                   1409:        int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
                   1410:        int is_dir = !S_ISDIR(file->mode) ? 0
                   1411:                   : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
                   1412:                   : 1;
                   1413: 
1.1.1.2   misho    1414:        if (DEBUG_GTE(GENR, 1))
1.1       misho    1415:                rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
                   1416: 
                   1417:        if (list_only) {
                   1418:                if (is_dir < 0
                   1419:                 || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
                   1420:                        return;
                   1421:                list_file_entry(file);
                   1422:                return;
                   1423:        }
                   1424: 
1.1.1.4 ! misho    1425:        maybe_ATTRS_ACCURATE_TIME = always_checksum ? ATTRS_ACCURATE_TIME : 0;
        !          1426: 
1.1       misho    1427:        if (skip_dir) {
                   1428:                if (is_below(file, skip_dir)) {
                   1429:                        if (is_dir)
                   1430:                                file->flags |= FLAG_MISSING_DIR;
                   1431: #ifdef SUPPORT_HARD_LINKS
                   1432:                        else if (F_IS_HLINKED(file))
                   1433:                                handle_skipped_hlink(file, itemizing, code, f_out);
                   1434: #endif
                   1435:                        return;
                   1436:                }
                   1437:                skip_dir = NULL;
                   1438:        }
                   1439: 
1.1.1.2   misho    1440:        init_stat_x(&sx);
1.1       misho    1441:        if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
                   1442:                if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
                   1443:                        if (is_dir < 0)
                   1444:                                return;
                   1445: #ifdef SUPPORT_HARD_LINKS
                   1446:                        if (F_IS_HLINKED(file))
                   1447:                                handle_skipped_hlink(file, itemizing, code, f_out);
                   1448: #endif
                   1449:                        rprintf(FERROR_XFER,
                   1450:                                "ERROR: daemon refused to receive %s \"%s\"\n",
                   1451:                                is_dir ? "directory" : "file", fname);
                   1452:                        if (is_dir)
                   1453:                                goto skipping_dir_contents;
                   1454:                        return;
                   1455:                }
                   1456:        }
1.1.1.4 ! misho    1457:        sx.crtime = 0;
1.1       misho    1458: 
                   1459:        if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
1.1.1.2   misho    1460:                int i;
1.1       misho    1461:          parent_is_dry_missing:
1.1.1.2   misho    1462:                for (i = 0; i < fuzzy_basis; i++) {
                   1463:                        if (fuzzy_dirlist[i]) {
                   1464:                                flist_free(fuzzy_dirlist[i]);
                   1465:                                fuzzy_dirlist[i] = NULL;
                   1466:                        }
1.1       misho    1467:                }
                   1468:                parent_dirname = "";
                   1469:                statret = -1;
                   1470:                stat_errno = ENOENT;
                   1471:        } else {
                   1472:                const char *dn = file->dirname ? file->dirname : ".";
                   1473:                dry_missing_dir = NULL;
                   1474:                if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1.1.1.3   misho    1475:                        /* Each parent dir must be in the file list or the flist data is bad.
                   1476:                         * Optimization: most of the time the parent dir will be the last dir
                   1477:                         * this function was asked to process in the file list. */
                   1478:                        if (!inc_recurse
                   1479:                         && (*dn != '.' || dn[1]) /* Avoid an issue with --relative and the "." dir. */
1.1.1.4 ! misho    1480:                         && (!prior_dir_file || strcmp(dn, f_name(prior_dir_file, NULL)) != 0)) {
        !          1481:                                int ok = 0, j = flist_find_name(cur_flist, dn, -1);
        !          1482:                                if (j >= 0) {
        !          1483:                                        struct file_struct *f = cur_flist->sorted[j];
        !          1484:                                        if (S_ISDIR(f->mode) || (missing_args == 2 && !file->mode && !f->mode))
        !          1485:                                                ok = 1;
        !          1486:                                }
        !          1487:                                /* The --delete-missing-args option can actually put invalid entries into
        !          1488:                                 * the file list, so if that option was specified, we'll just complain about
        !          1489:                                 * it and allow it. */
        !          1490:                                if (!ok && missing_args == 2 && file->mode == 0 && j < 0)
        !          1491:                                        rprintf(FERROR, "WARNING: parent dir is absent in the file list: %s\n", dn);
        !          1492:                                else if (!ok) {
        !          1493:                                        rprintf(FERROR, "ABORTING due to invalid path from sender: %s/%s\n",
        !          1494:                                                dn, file->basename);
        !          1495:                                        exit_cleanup(RERR_PROTOCOL);
        !          1496:                                }
1.1.1.3   misho    1497:                        }
1.1.1.4 ! misho    1498:                        if (relative_paths && !implied_dirs && file->mode != 0
1.1       misho    1499:                         && do_stat(dn, &sx.st) < 0) {
                   1500:                                if (dry_run)
                   1501:                                        goto parent_is_dry_missing;
1.1.1.4 ! misho    1502:                                if (make_path(fname, ACCESSPERMS, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0) {
1.1       misho    1503:                                        rsyserr(FERROR_XFER, errno,
                   1504:                                                "recv_generator: mkdir %s failed",
                   1505:                                                full_fname(dn));
                   1506:                                }
                   1507:                        }
1.1.1.2   misho    1508:                        if (fuzzy_basis) {
                   1509:                                int i;
                   1510:                                for (i = 0; i < fuzzy_basis; i++) {
                   1511:                                        if (fuzzy_dirlist[i]) {
                   1512:                                                flist_free(fuzzy_dirlist[i]);
                   1513:                                                fuzzy_dirlist[i] = NULL;
                   1514:                                        }
                   1515:                                }
1.1.1.4 ! misho    1516:                                need_new_dirscan = 1;
        !          1517:                        } else if (checksum_files)
        !          1518:                                need_new_dirscan = 1;
1.1       misho    1519: #ifdef SUPPORT_ACLS
                   1520:                        if (!preserve_perms)
                   1521:                                dflt_perms = default_perms_for_dir(dn);
                   1522: #endif
                   1523:                }
                   1524:                parent_dirname = dn;
                   1525: 
1.1.1.4 ! misho    1526:                if (need_new_dirscan && S_ISREG(file->mode)) {
1.1.1.2   misho    1527:                        int i;
1.1       misho    1528:                        strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1.1.1.2   misho    1529:                        for (i = 0; i < fuzzy_basis; i++) {
                   1530:                                if (i && pathjoin(fnamecmpbuf, MAXPATHLEN, basis_dir[i-1], dn) >= MAXPATHLEN)
                   1531:                                        continue;
1.1.1.4 ! misho    1532:                                fuzzy_dirlist[i] = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES | GDL_PERHAPS_DIR);
1.1.1.2   misho    1533:                                if (fuzzy_dirlist[i] && fuzzy_dirlist[i]->used == 0) {
                   1534:                                        flist_free(fuzzy_dirlist[i]);
                   1535:                                        fuzzy_dirlist[i] = NULL;
                   1536:                                }
                   1537:                        }
1.1.1.4 ! misho    1538:                        if (checksum_files) {
        !          1539:                                reset_checksum_cache(started_whole_dir);
        !          1540:                                started_whole_dir = upcoming_whole_dir;
        !          1541:                        }
        !          1542:                        need_new_dirscan = 0;
1.1       misho    1543:                }
                   1544: 
                   1545:                statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
                   1546:                stat_errno = errno;
                   1547:        }
                   1548: 
1.1.1.2   misho    1549:        if (missing_args == 2 && file->mode == 0) {
                   1550:                if (filter_list.head && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
                   1551:                        return;
                   1552:                if (statret == 0)
                   1553:                        delete_item(fname, sx.st.st_mode, del_opts);
                   1554:                return;
                   1555:        }
                   1556: 
1.1       misho    1557:        if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
                   1558:                if (is_dir) {
                   1559:                        if (is_dir < 0)
                   1560:                                return;
                   1561:                        skip_dir = file;
                   1562:                        file->flags |= FLAG_MISSING_DIR;
                   1563:                }
                   1564: #ifdef SUPPORT_HARD_LINKS
                   1565:                else if (F_IS_HLINKED(file))
                   1566:                        handle_skipped_hlink(file, itemizing, code, f_out);
                   1567: #endif
1.1.1.2   misho    1568:                if (INFO_GTE(SKIP, 1)) {
1.1       misho    1569:                        rprintf(FINFO, "not creating new %s \"%s\"\n",
                   1570:                                is_dir ? "directory" : "file", fname);
                   1571:                }
                   1572:                return;
                   1573:        }
                   1574: 
                   1575:        if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
                   1576:         && !am_root && sx.st.st_uid == our_uid)
                   1577:                del_opts |= DEL_NO_UID_WRITE;
                   1578: 
                   1579:        if (ignore_existing > 0 && statret == 0
                   1580:         && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
1.1.1.2   misho    1581:                if (INFO_GTE(SKIP, 1) && is_dir >= 0)
1.1       misho    1582:                        rprintf(FINFO, "%s exists\n", fname);
                   1583: #ifdef SUPPORT_HARD_LINKS
                   1584:                if (F_IS_HLINKED(file))
                   1585:                        handle_skipped_hlink(file, itemizing, code, f_out);
                   1586: #endif
                   1587:                goto cleanup;
                   1588:        }
                   1589: 
                   1590:        fnamecmp = fname;
                   1591: 
                   1592:        if (is_dir) {
                   1593:                mode_t added_perms;
                   1594:                if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
                   1595:                        goto cleanup;
                   1596:                if (am_root < 0) {
                   1597:                        /* For --fake-super, the dir must be useable by the copying
                   1598:                         * user, just like it would be for root. */
                   1599:                        added_perms = S_IRUSR|S_IWUSR|S_IXUSR;
                   1600:                } else
                   1601:                        added_perms = 0;
                   1602:                if (is_dir < 0) {
1.1.1.3   misho    1603:                        if (!(preserve_times & PRESERVE_DIR_TIMES))
1.1.1.4 ! misho    1604:                                goto cleanup;
1.1       misho    1605:                        /* In inc_recurse mode we want to make sure any missing
                   1606:                         * directories get created while we're still processing
                   1607:                         * the parent dir (which allows us to touch the parent
                   1608:                         * dir's mtime right away).  We will handle the dir in
                   1609:                         * full later (right before we handle its contents). */
                   1610:                        if (statret == 0
                   1611:                         && (S_ISDIR(sx.st.st_mode)
                   1612:                          || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
                   1613:                                goto cleanup; /* Any errors get reported later. */
                   1614:                        if (do_mkdir(fname, (file->mode|added_perms) & 0700) == 0)
                   1615:                                file->flags |= FLAG_DIR_CREATED;
                   1616:                        goto cleanup;
                   1617:                }
                   1618:                /* The file to be received is a directory, so we need
                   1619:                 * to prepare appropriately.  If there is already a
                   1620:                 * file of that name and it is *not* a directory, then
                   1621:                 * we need to delete it.  If it doesn't exist, then
                   1622:                 * (perhaps recursively) create it. */
                   1623:                if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
                   1624:                        if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
                   1625:                                goto skipping_dir_contents;
                   1626:                        statret = -1;
                   1627:                }
                   1628:                if (dry_run && statret != 0) {
                   1629:                        if (!dry_missing_dir)
                   1630:                                dry_missing_dir = file;
                   1631:                        file->flags |= FLAG_MISSING_DIR;
                   1632:                }
1.1.1.2   misho    1633:                init_stat_x(&real_sx);
                   1634:                real_sx.st = sx.st;
1.1       misho    1635:                real_ret = statret;
                   1636:                if (file->flags & FLAG_DIR_CREATED)
                   1637:                        statret = -1;
1.1.1.4 ! misho    1638:                if (!preserve_perms || omit_dir_changes) { /* See comment in non-dir code below. */
        !          1639:                        file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms, statret == 0);
1.1       misho    1640:                }
1.1.1.4 ! misho    1641: #ifdef SUPPORT_FORCE_CHANGE
        !          1642:                if (force_change && !preserve_fileflags)
        !          1643:                        F_FFLAGS(file) = sx.st.st_flags;
        !          1644: #endif
1.1       misho    1645:                if (statret != 0 && basis_dir[0] != NULL) {
1.1.1.4 ! misho    1646:                        int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
1.1       misho    1647:                        if (j == -2) {
                   1648:                                itemizing = 0;
                   1649:                                code = FNONE;
                   1650:                                statret = 1;
                   1651:                        } else if (j >= 0) {
                   1652:                                statret = 1;
                   1653:                                fnamecmp = fnamecmpbuf;
                   1654:                        }
                   1655:                }
                   1656:                if (itemizing && f_out != -1) {
                   1657:                        itemize(fnamecmp, file, ndx, statret, &sx,
                   1658:                                statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
                   1659:                }
                   1660:                if (real_ret != 0 && do_mkdir(fname,file->mode|added_perms) < 0 && errno != EEXIST) {
                   1661:                        if (!relative_paths || errno != ENOENT
1.1.1.4 ! misho    1662:                         || make_path(fname, ACCESSPERMS, MKP_DROP_NAME | MKP_SKIP_SLASH) < 0
1.1       misho    1663:                         || (do_mkdir(fname, file->mode|added_perms) < 0 && errno != EEXIST)) {
                   1664:                                rsyserr(FERROR_XFER, errno,
                   1665:                                        "recv_generator: mkdir %s failed",
                   1666:                                        full_fname(fname));
                   1667:                          skipping_dir_contents:
1.1.1.4 ! misho    1668:                                rprintf(FERROR, "*** Skipping any contents from this failed directory ***\n");
1.1       misho    1669:                                skip_dir = file;
                   1670:                                file->flags |= FLAG_MISSING_DIR;
                   1671:                                goto cleanup;
                   1672:                        }
                   1673:                }
                   1674: 
                   1675: #ifdef SUPPORT_XATTRS
                   1676:                if (preserve_xattrs && statret == 1)
                   1677:                        copy_xattrs(fnamecmpbuf, fname);
                   1678: #endif
                   1679:                if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
1.1.1.4 ! misho    1680:                 && INFO_GTE(NAME, 1) && code != FNONE && f_out != -1)
1.1       misho    1681:                        rprintf(code, "%s/\n", fname);
                   1682: 
                   1683:                /* We need to ensure that the dirs in the transfer have both
                   1684:                 * readable and writable permissions during the time we are
                   1685:                 * putting files within them.  This is then restored to the
                   1686:                 * former permissions after the transfer is done. */
1.1.1.4 ! misho    1687: #ifdef SUPPORT_FORCE_CHANGE
        !          1688:                if (force_change && F_FFLAGS(file) & force_change
        !          1689:                 && make_mutable(fname, file->mode, F_FFLAGS(file), force_change))
        !          1690:                        need_retouch_dir_perms = 1;
        !          1691: #endif
1.1       misho    1692: #ifdef HAVE_CHMOD
                   1693:                if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
                   1694:                        mode_t mode = file->mode | S_IRWXU;
1.1.1.4 ! misho    1695:                        if (do_chmod(fname, mode, 0) < 0) {
1.1       misho    1696:                                rsyserr(FERROR_XFER, errno,
                   1697:                                        "failed to modify permissions on %s",
                   1698:                                        full_fname(fname));
                   1699:                        }
                   1700:                        need_retouch_dir_perms = 1;
                   1701:                }
                   1702: #endif
                   1703: 
                   1704:                if (real_ret != 0 && one_file_system)
                   1705:                        real_sx.st.st_dev = filesystem_dev;
                   1706:                if (inc_recurse) {
                   1707:                        if (one_file_system) {
                   1708:                                uint32 *devp = F_DIR_DEV_P(file);
                   1709:                                DEV_MAJOR(devp) = major(real_sx.st.st_dev);
                   1710:                                DEV_MINOR(devp) = minor(real_sx.st.st_dev);
                   1711:                        }
                   1712:                }
                   1713:                else if (delete_during && f_out != -1 && !phase
                   1714:                    && !(file->flags & FLAG_MISSING_DIR)) {
1.1.1.4 ! misho    1715:                        if (file->flags & FLAG_CONTENT_DIR) {
        !          1716:                                if (detect_renamed && real_ret != 0)
        !          1717:                                        unexplored_dirs++;
        !          1718:                                delete_in_dir(fname, file, &real_sx.st.st_dev,
        !          1719:                                              delete_during < 0 ? DEL_NO_DELETIONS : 0);
        !          1720:                        } else
1.1       misho    1721:                                change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
                   1722:                }
1.1.1.4 ! misho    1723:                upcoming_whole_dir = file->flags & FLAG_CONTENT_DIR && f_out != -1 ? 1 : 0;
1.1.1.3   misho    1724:                prior_dir_file = file;
1.1       misho    1725:                goto cleanup;
                   1726:        }
                   1727: 
                   1728:        /* If we're not preserving permissions, change the file-list's
                   1729:         * mode based on the local permissions and some heuristics. */
                   1730:        if (!preserve_perms) {
                   1731:                int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1.1.1.4 ! misho    1732:                file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms, exists);
1.1       misho    1733:        }
1.1.1.4 ! misho    1734: #ifdef SUPPORT_FORCE_CHANGE
        !          1735:        if (force_change && !preserve_fileflags)
        !          1736:                F_FFLAGS(file) = sx.st.st_flags;
        !          1737: #endif
1.1       misho    1738: 
                   1739: #ifdef SUPPORT_HARD_LINKS
                   1740:        if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
                   1741:         && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
                   1742:                goto cleanup;
                   1743: #endif
                   1744: 
                   1745:        if (preserve_links && S_ISLNK(file->mode)) {
                   1746: #ifdef SUPPORT_LINKS
                   1747:                const char *sl = F_SYMLINK(file);
                   1748:                if (safe_symlinks && unsafe_symlink(sl, fname)) {
1.1.1.2   misho    1749:                        if (INFO_GTE(NAME, 1)) {
1.1       misho    1750:                                if (solo_file) {
                   1751:                                        /* fname contains the destination path, but we
                   1752:                                         * want to report the source path. */
                   1753:                                        fname = f_name(file, NULL);
                   1754:                                }
                   1755:                                rprintf(FINFO,
                   1756:                                        "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
                   1757:                                        fname, sl);
                   1758:                        }
1.1.1.4 ! misho    1759:                        goto cleanup;
1.1       misho    1760:                }
                   1761:                if (statret == 0) {
                   1762:                        char lnk[MAXPATHLEN];
                   1763:                        int len;
                   1764: 
1.1.1.2   misho    1765:                        if (S_ISLNK(sx.st.st_mode)
                   1766:                         && (len = do_readlink(fname, lnk, MAXPATHLEN-1)) > 0
                   1767:                         && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1.1       misho    1768:                                /* The link is pointing to the right place. */
                   1769:                                set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
                   1770:                                if (itemizing)
                   1771:                                        itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1.1.1.3   misho    1772: #ifdef SUPPORT_HARD_LINKS
1.1       misho    1773:                                if (preserve_hard_links && F_IS_HLINKED(file))
                   1774:                                        finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
                   1775: #endif
                   1776:                                if (remove_source_files == 1)
                   1777:                                        goto return_with_success;
                   1778:                                goto cleanup;
                   1779:                        }
                   1780:                } else if (basis_dir[0] != NULL) {
1.1.1.4 ! misho    1781:                        int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
1.1       misho    1782:                        if (j == -2) {
                   1783: #ifndef CAN_HARDLINK_SYMLINK
1.1.1.4 ! misho    1784:                                if (alt_dest_type == LINK_DEST) {
1.1       misho    1785:                                        /* Resort to --copy-dest behavior. */
                   1786:                                } else
                   1787: #endif
1.1.1.4 ! misho    1788:                                if (alt_dest_type != COPY_DEST)
1.1       misho    1789:                                        goto cleanup;
                   1790:                                itemizing = 0;
                   1791:                                code = FNONE;
1.1.1.3   misho    1792:                        } else if (j >= 0) {
1.1       misho    1793:                                statret = 1;
1.1.1.3   misho    1794:                                fnamecmp = fnamecmpbuf;
                   1795:                        }
1.1       misho    1796:                }
1.1.1.3   misho    1797:                if (atomic_create(file, fname, sl, NULL, MAKEDEV(0, 0), &sx, statret == 0 ? DEL_FOR_SYMLINK : 0)) {
1.1       misho    1798:                        set_file_attrs(fname, file, NULL, NULL, 0);
                   1799:                        if (itemizing) {
1.1.1.2   misho    1800:                                if (statret == 0 && !S_ISLNK(sx.st.st_mode))
                   1801:                                        statret = -1;
1.1.1.3   misho    1802:                                itemize(fnamecmp, file, ndx, statret, &sx,
1.1       misho    1803:                                        ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
                   1804:                        }
1.1.1.2   misho    1805:                        if (code != FNONE && INFO_GTE(NAME, 1))
1.1       misho    1806:                                rprintf(code, "%s -> %s\n", fname, sl);
                   1807: #ifdef SUPPORT_HARD_LINKS
                   1808:                        if (preserve_hard_links && F_IS_HLINKED(file))
                   1809:                                finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
                   1810: #endif
                   1811:                        /* This does not check remove_source_files == 1
                   1812:                         * because this is one of the items that the old
                   1813:                         * --remove-sent-files option would remove. */
                   1814:                        if (remove_source_files)
                   1815:                                goto return_with_success;
                   1816:                }
                   1817: #endif
                   1818:                goto cleanup;
                   1819:        }
                   1820: 
                   1821:        if ((am_root && preserve_devices && IS_DEVICE(file->mode))
                   1822:         || (preserve_specials && IS_SPECIAL(file->mode))) {
                   1823:                dev_t rdev;
1.1.1.2   misho    1824:                int del_for_flag = 0;
1.1       misho    1825:                if (IS_DEVICE(file->mode)) {
                   1826:                        uint32 *devp = F_RDEV_P(file);
                   1827:                        rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
                   1828:                } else
                   1829:                        rdev = 0;
                   1830:                if (statret == 0) {
                   1831:                        if (IS_DEVICE(file->mode)) {
                   1832:                                if (!IS_DEVICE(sx.st.st_mode))
                   1833:                                        statret = -1;
                   1834:                                del_for_flag = DEL_FOR_DEVICE;
                   1835:                        } else {
                   1836:                                if (!IS_SPECIAL(sx.st.st_mode))
                   1837:                                        statret = -1;
                   1838:                                del_for_flag = DEL_FOR_SPECIAL;
                   1839:                        }
                   1840:                        if (statret == 0
                   1841:                         && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
                   1842:                         && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
                   1843:                                /* The device or special file is identical. */
                   1844:                                set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
                   1845:                                if (itemizing)
                   1846:                                        itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
                   1847: #ifdef SUPPORT_HARD_LINKS
                   1848:                                if (preserve_hard_links && F_IS_HLINKED(file))
                   1849:                                        finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
                   1850: #endif
                   1851:                                if (remove_source_files == 1)
                   1852:                                        goto return_with_success;
                   1853:                                goto cleanup;
                   1854:                        }
                   1855:                } else if (basis_dir[0] != NULL) {
1.1.1.4 ! misho    1856:                        int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
1.1       misho    1857:                        if (j == -2) {
                   1858: #ifndef CAN_HARDLINK_SPECIAL
1.1.1.4 ! misho    1859:                                if (alt_dest_type == LINK_DEST) {
1.1       misho    1860:                                        /* Resort to --copy-dest behavior. */
                   1861:                                } else
                   1862: #endif
1.1.1.4 ! misho    1863:                                if (alt_dest_type != COPY_DEST)
1.1       misho    1864:                                        goto cleanup;
                   1865:                                itemizing = 0;
                   1866:                                code = FNONE;
1.1.1.3   misho    1867:                        } else if (j >= 0) {
1.1       misho    1868:                                statret = 1;
1.1.1.3   misho    1869:                                fnamecmp = fnamecmpbuf;
                   1870:                        }
1.1       misho    1871:                }
1.1.1.2   misho    1872:                if (DEBUG_GTE(GENR, 1)) {
1.1       misho    1873:                        rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
                   1874:                                fname, (int)file->mode,
                   1875:                                (long)major(rdev), (long)minor(rdev));
                   1876:                }
1.1.1.3   misho    1877:                if (atomic_create(file, fname, NULL, NULL, rdev, &sx, del_for_flag)) {
1.1       misho    1878:                        set_file_attrs(fname, file, NULL, NULL, 0);
                   1879:                        if (itemizing) {
1.1.1.3   misho    1880:                                itemize(fnamecmp, file, ndx, statret, &sx,
1.1       misho    1881:                                        ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
                   1882:                        }
1.1.1.2   misho    1883:                        if (code != FNONE && INFO_GTE(NAME, 1))
1.1       misho    1884:                                rprintf(code, "%s\n", fname);
                   1885: #ifdef SUPPORT_HARD_LINKS
                   1886:                        if (preserve_hard_links && F_IS_HLINKED(file))
                   1887:                                finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
                   1888: #endif
                   1889:                        if (remove_source_files == 1)
                   1890:                                goto return_with_success;
                   1891:                }
                   1892:                goto cleanup;
                   1893:        }
                   1894: 
1.1.1.4 ! misho    1895:        if (!(S_ISREG(file->mode) || (copy_devices && IS_DEVICE(file->mode)))) {
1.1       misho    1896:                if (solo_file)
                   1897:                        fname = f_name(file, NULL);
                   1898:                rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
                   1899:                goto cleanup;
                   1900:        }
                   1901: 
1.1.1.2   misho    1902:        if (max_size >= 0 && F_LENGTH(file) > max_size) {
                   1903:                if (INFO_GTE(SKIP, 1)) {
1.1       misho    1904:                        if (solo_file)
                   1905:                                fname = f_name(file, NULL);
                   1906:                        rprintf(FINFO, "%s is over max-size\n", fname);
                   1907:                }
                   1908:                goto cleanup;
                   1909:        }
1.1.1.2   misho    1910:        if (min_size >= 0 && F_LENGTH(file) < min_size) {
                   1911:                if (INFO_GTE(SKIP, 1)) {
1.1       misho    1912:                        if (solo_file)
                   1913:                                fname = f_name(file, NULL);
                   1914:                        rprintf(FINFO, "%s is under min-size\n", fname);
                   1915:                }
                   1916:                goto cleanup;
                   1917:        }
                   1918: 
1.1.1.4 ! misho    1919:        if (update_only > 0 && statret == 0 && file->modtime - sx.st.st_mtime <= modify_window) {
1.1.1.2   misho    1920:                if (INFO_GTE(SKIP, 1))
1.1       misho    1921:                        rprintf(FINFO, "%s is newer\n", fname);
                   1922: #ifdef SUPPORT_HARD_LINKS
                   1923:                if (F_IS_HLINKED(file))
                   1924:                        handle_skipped_hlink(file, itemizing, code, f_out);
                   1925: #endif
                   1926:                goto cleanup;
                   1927:        }
                   1928: 
1.1.1.4 ! misho    1929:        if (downdate_only > 0 && statret == 0 && file->modtime - sx.st.st_mtime >= modify_window) {
        !          1930:                if (INFO_GTE(SKIP, 1))
        !          1931:                        rprintf(FINFO, "%s is older\n", fname);
        !          1932:                return;
        !          1933:        }
        !          1934: 
1.1       misho    1935:        fnamecmp_type = FNAMECMP_FNAME;
                   1936: 
1.1.1.4 ! misho    1937:        if (statret == 0 && !(S_ISREG(sx.st.st_mode) || (write_devices && IS_DEVICE(sx.st.st_mode)))) {
1.1       misho    1938:                if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
                   1939:                        goto cleanup;
                   1940:                statret = -1;
                   1941:                stat_errno = ENOENT;
                   1942:        }
                   1943: 
1.1.1.4 ! misho    1944:        if (basis_dir[0] != NULL && (statret != 0 || alt_dest_type != COPY_DEST)) {
        !          1945:                int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx, statret == 0, itemizing, code);
1.1       misho    1946:                if (j == -2) {
                   1947:                        if (remove_source_files == 1)
                   1948:                                goto return_with_success;
                   1949:                        goto cleanup;
                   1950:                }
                   1951:                if (j >= 0) {
                   1952:                        fnamecmp = fnamecmpbuf;
                   1953:                        fnamecmp_type = j;
                   1954:                        statret = 0;
                   1955:                }
                   1956:        }
                   1957: 
1.1.1.2   misho    1958:        init_stat_x(&real_sx);
                   1959:        real_sx.st = sx.st; /* Don't copy xattr/acl pointers, as they would free wrong. */
1.1       misho    1960:        real_ret = statret;
                   1961: 
                   1962:        if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1.1.1.4 ! misho    1963:         && link_stat(partialptr, &partial_st, 0) == 0
        !          1964:         && S_ISREG(partial_st.st_mode)) {
1.1       misho    1965:                if (statret != 0)
                   1966:                        goto prepare_to_open;
                   1967:        } else
                   1968:                partialptr = NULL;
                   1969: 
1.1.1.2   misho    1970:        if (statret != 0 && fuzzy_basis) {
                   1971:                /* Sets fnamecmp_type to FNAMECMP_FUZZY or above. */
                   1972:                fuzzy_file = find_fuzzy(file, fuzzy_dirlist, &fnamecmp_type);
                   1973:                if (fuzzy_file) {
1.1       misho    1974:                        f_name(fuzzy_file, fnamecmpbuf);
1.1.1.2   misho    1975:                        if (DEBUG_GTE(FUZZY, 1)) {
1.1       misho    1976:                                rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
                   1977:                                        fname, fnamecmpbuf);
                   1978:                        }
                   1979:                        sx.st.st_size = F_LENGTH(fuzzy_file);
1.1.1.4 ! misho    1980: #ifdef SUPPORT_HFS_COMPRESSION
        !          1981:                        if (sx.st.st_flags & UF_COMPRESSED) {
        !          1982:                                if (preserve_hfs_compression)
        !          1983:                                        sx.st.st_size = 0;
        !          1984:                                else
        !          1985:                                        sx.st.st_flags &= ~UF_COMPRESSED;
        !          1986:                        }
        !          1987: #endif
1.1       misho    1988:                        statret = 0;
                   1989:                        fnamecmp = fnamecmpbuf;
                   1990:                }
                   1991:        }
                   1992: 
                   1993:        if (statret != 0) {
                   1994: #ifdef SUPPORT_HARD_LINKS
                   1995:                if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
                   1996:                        cur_flist->in_progress++;
                   1997:                        goto cleanup;
                   1998:                }
                   1999: #endif
1.1.1.4 ! misho    2000:                if (stat_errno == ENOENT) {
        !          2001:                        if (detect_renamed && unexplored_dirs > 0
        !          2002:                         && F_LENGTH(file)) {
        !          2003:                                bitbag_set_bit(delayed_bits, ndx);
        !          2004:                                return;
        !          2005:                        }
1.1       misho    2006:                        goto notify_others;
1.1.1.4 ! misho    2007:                }
1.1       misho    2008:                rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
                   2009:                        full_fname(fname));
                   2010:                goto cleanup;
                   2011:        }
                   2012: 
                   2013:        if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
                   2014:                ;
1.1.1.4 ! misho    2015:        else if (fnamecmp_type >= FNAMECMP_FUZZY)
1.1       misho    2016:                ;
1.1.1.4 ! misho    2017:        else if (unchanged_file(fnamecmp, file, &sx.st, fnamecmp_type == FNAMECMP_FNAME ? 0 : -1)) {
1.1       misho    2018:                if (partialptr) {
                   2019:                        do_unlink(partialptr);
                   2020:                        handle_partial_dir(partialptr, PDIR_DELETE);
                   2021:                }
1.1.1.4 ! misho    2022:                set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT | maybe_ATTRS_ACCURATE_TIME);
        !          2023:                if (checksum_files & CSF_UPDATE)
        !          2024:                        set_cached_checksum(cur_flist, file);
1.1       misho    2025:                if (itemizing)
                   2026:                        itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
                   2027: #ifdef SUPPORT_HARD_LINKS
                   2028:                if (preserve_hard_links && F_IS_HLINKED(file))
                   2029:                        finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
                   2030: #endif
                   2031:                if (remove_source_files != 1)
                   2032:                        goto cleanup;
                   2033:          return_with_success:
                   2034:                if (!dry_run)
                   2035:                        send_msg_int(MSG_SUCCESS, ndx);
                   2036:                goto cleanup;
                   2037:        }
                   2038: 
                   2039:        if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
                   2040: #ifdef SUPPORT_HARD_LINKS
                   2041:                if (F_IS_HLINKED(file))
                   2042:                        handle_skipped_hlink(file, itemizing, code, f_out);
                   2043: #endif
                   2044:                goto cleanup;
                   2045:        }
                   2046: 
                   2047:   prepare_to_open:
                   2048:        if (partialptr) {
                   2049:                sx.st = partial_st;
                   2050:                fnamecmp = partialptr;
                   2051:                fnamecmp_type = FNAMECMP_PARTIAL_DIR;
                   2052:                statret = 0;
1.1.1.4 ! misho    2053:                if (detect_renamed > 1 && unchanged_file(fnamecmp, file, &sx.st)) {
        !          2054:                        /* Adopt the partial file. */
        !          2055:                        finish_transfer(fname, fnamecmp, NULL, NULL, file, 1, 1);
        !          2056:                        handle_partial_dir(partialptr, PDIR_DELETE);
        !          2057:                        if (itemizing)
        !          2058:                                itemize(fnamecmp, file, ndx, -1, &sx,
        !          2059:                                                ITEM_LOCAL_CHANGE, fnamecmp_type, NULL);
        !          2060: #ifdef SUPPORT_HARD_LINKS
        !          2061:                        if (preserve_hard_links && F_IS_HLINKED(file))
        !          2062:                                finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
        !          2063: #endif
        !          2064:                        if (remove_source_files == 1)
        !          2065:                                goto return_with_success;
        !          2066:                        goto cleanup;
        !          2067:                }
1.1       misho    2068:        }
                   2069: 
                   2070:        if (!do_xfers)
                   2071:                goto notify_others;
                   2072: 
                   2073:        if (read_batch || whole_file) {
1.1.1.4 ! misho    2074:                if (inplace && make_backups > 1 && fnamecmp_type == FNAMECMP_FNAME) {
1.1       misho    2075:                        if (!(backupptr = get_backup_name(fname)))
                   2076:                                goto cleanup;
                   2077:                        if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
                   2078:                                goto pretend_missing;
1.1.1.2   misho    2079:                        if (copy_file(fname, backupptr, -1, back_file->mode) < 0) {
1.1       misho    2080:                                unmake_file(back_file);
                   2081:                                back_file = NULL;
                   2082:                                goto cleanup;
                   2083:                        }
                   2084:                }
                   2085:                goto notify_others;
                   2086:        }
                   2087: 
1.1.1.2   misho    2088:        if (fuzzy_dirlist[0]) {
                   2089:                int j = flist_find(fuzzy_dirlist[0], file);
1.1       misho    2090:                if (j >= 0) /* don't use changing file as future fuzzy basis */
1.1.1.2   misho    2091:                        fuzzy_dirlist[0]->files[j]->flags |= FLAG_FILE_SENT;
1.1       misho    2092:        }
                   2093: 
                   2094:        /* open the file */
                   2095:        if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
                   2096:                rsyserr(FERROR, errno, "failed to open %s, continuing",
                   2097:                        full_fname(fnamecmp));
                   2098:          pretend_missing:
                   2099:                /* pretend the file didn't exist */
                   2100: #ifdef SUPPORT_HARD_LINKS
                   2101:                if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
                   2102:                        cur_flist->in_progress++;
                   2103:                        goto cleanup;
                   2104:                }
                   2105: #endif
                   2106:                statret = real_ret = -1;
                   2107:                goto notify_others;
                   2108:        }
                   2109: 
1.1.1.4 ! misho    2110:        if (inplace && make_backups > 1 && fnamecmp_type == FNAMECMP_FNAME) {
1.1       misho    2111:                if (!(backupptr = get_backup_name(fname))) {
                   2112:                        close(fd);
                   2113:                        goto cleanup;
                   2114:                }
                   2115:                if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
                   2116:                        close(fd);
                   2117:                        goto pretend_missing;
                   2118:                }
                   2119:                if (robust_unlink(backupptr) && errno != ENOENT) {
                   2120:                        rsyserr(FERROR_XFER, errno, "unlink %s",
                   2121:                                full_fname(backupptr));
                   2122:                        unmake_file(back_file);
                   2123:                        back_file = NULL;
                   2124:                        close(fd);
                   2125:                        goto cleanup;
                   2126:                }
                   2127:                if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1.1.1.2   misho    2128:                        rsyserr(FERROR_XFER, errno, "open %s", full_fname(backupptr));
                   2129:                        unmake_file(back_file);
                   2130:                        back_file = NULL;
                   2131:                        close(fd);
                   2132:                        goto cleanup;
1.1       misho    2133:                }
                   2134:                fnamecmp_type = FNAMECMP_BACKUP;
                   2135:        }
                   2136: 
1.1.1.4 ! misho    2137:        if (IS_DEVICE(sx.st.st_mode) && sx.st.st_size == 0)
        !          2138:                sx.st.st_size = get_device_size(fd, fnamecmp);
        !          2139: 
1.1.1.2   misho    2140:        if (DEBUG_GTE(DELTASUM, 3)) {
                   2141:                rprintf(FINFO, "gen mapped %s of size %s\n",
                   2142:                        fnamecmp, big_num(sx.st.st_size));
1.1       misho    2143:        }
                   2144: 
1.1.1.2   misho    2145:        if (DEBUG_GTE(DELTASUM, 2))
1.1       misho    2146:                rprintf(FINFO, "generating and sending sums for %d\n", ndx);
                   2147: 
                   2148:   notify_others:
                   2149:        if (remove_source_files && !delay_updates && !phase && !dry_run)
                   2150:                increment_active_files(ndx, itemizing, code);
1.1.1.2   misho    2151:        if (inc_recurse && (!dry_run || write_batch < 0))
1.1       misho    2152:                cur_flist->in_progress++;
                   2153: #ifdef SUPPORT_HARD_LINKS
                   2154:        if (preserve_hard_links && F_IS_HLINKED(file))
                   2155:                file->flags |= FLAG_FILE_SENT;
                   2156: #endif
                   2157:        write_ndx(f_out, ndx);
                   2158:        if (itemizing) {
                   2159:                int iflags = ITEM_TRANSFER;
                   2160:                if (always_checksum > 0)
                   2161:                        iflags |= ITEM_REPORT_CHANGE;
                   2162:                if (fnamecmp_type != FNAMECMP_FNAME)
                   2163:                        iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1.1.1.2   misho    2164:                if (fnamecmp_type >= FNAMECMP_FUZZY)
1.1       misho    2165:                        iflags |= ITEM_XNAME_FOLLOWS;
                   2166:                itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
                   2167:                        fuzzy_file ? fuzzy_file->basename : NULL);
1.1.1.2   misho    2168:                free_stat_x(&real_sx);
1.1       misho    2169:        }
                   2170: 
                   2171:        if (!do_xfers) {
                   2172: #ifdef SUPPORT_HARD_LINKS
                   2173:                if (preserve_hard_links && F_IS_HLINKED(file))
                   2174:                        finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
                   2175: #endif
                   2176:                goto cleanup;
                   2177:        }
                   2178:        if (read_batch)
                   2179:                goto cleanup;
                   2180: 
1.1.1.4 ! misho    2181: #ifdef SUPPORT_HFS_COMPRESSION
        !          2182:        if (F_FFLAGS(file) & UF_COMPRESSED) {
        !          2183:                /* At this point the attrs have already been copied, we don't need to transfer a data fork
        !          2184:                 * If my filesystem doesn't support HFS compression, the existing file's content
        !          2185:                 * will not be automatically truncated, so we'll do that manually here */
        !          2186:                if (preserve_hfs_compression && sx.st.st_size > 0) {
        !          2187:                        if (ftruncate(fd, 0) == 0)
        !          2188:                                sx.st.st_size = 0;
        !          2189:                }
        !          2190:        }
        !          2191: #endif
        !          2192: 
1.1       misho    2193:        if (statret != 0 || whole_file)
                   2194:                write_sum_head(f_out, NULL);
                   2195:        else if (sx.st.st_size <= 0) {
                   2196:                write_sum_head(f_out, NULL);
                   2197:                close(fd);
                   2198:        } else {
                   2199:                if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
                   2200:                        rprintf(FWARNING,
1.1.1.4 ! misho    2201:                                "WARNING: file is too large for checksum sending: %s\n",
        !          2202:                                fnamecmp);
1.1       misho    2203:                        write_sum_head(f_out, NULL);
                   2204:                }
                   2205:                close(fd);
                   2206:        }
                   2207: 
                   2208:   cleanup:
                   2209:        if (back_file) {
                   2210:                int save_preserve_xattrs = preserve_xattrs;
                   2211:                if (f_copy >= 0)
                   2212:                        close(f_copy);
                   2213: #ifdef SUPPORT_XATTRS
                   2214:                if (preserve_xattrs) {
                   2215:                        copy_xattrs(fname, backupptr);
                   2216:                        preserve_xattrs = 0;
                   2217:                }
                   2218: #endif
                   2219:                set_file_attrs(backupptr, back_file, NULL, NULL, 0);
                   2220:                preserve_xattrs = save_preserve_xattrs;
1.1.1.2   misho    2221:                if (INFO_GTE(BACKUP, 1)) {
1.1       misho    2222:                        rprintf(FINFO, "backed up %s to %s\n",
                   2223:                                fname, backupptr);
                   2224:                }
                   2225:                unmake_file(back_file);
                   2226:        }
                   2227: 
1.1.1.2   misho    2228:        free_stat_x(&sx);
                   2229: }
                   2230: 
                   2231: /* If we are replacing an existing hard link, symlink, device, or special file,
1.1.1.3   misho    2232:  * create a temp-name item and rename it into place.  A symlimk specifies slnk,
                   2233:  * a hard link specifies hlnk, otherwise we create a device based on rdev.
                   2234:  * Specify 0 for the del_for_flag if there is not a file to replace.  This
                   2235:  * returns 1 on success and 0 on failure. */
                   2236: int atomic_create(struct file_struct *file, char *fname, const char *slnk, const char *hlnk,
1.1.1.2   misho    2237:                  dev_t rdev, stat_x *sxp, int del_for_flag)
                   2238: {
                   2239:        char tmpname[MAXPATHLEN];
                   2240:        const char *create_name;
                   2241:        int skip_atomic, dir_in_the_way = del_for_flag && S_ISDIR(sxp->st.st_mode);
                   2242: 
                   2243:        if (!del_for_flag || dir_in_the_way || tmpdir || !get_tmpname(tmpname, fname, True))
                   2244:                skip_atomic = 1;
                   2245:        else
                   2246:                skip_atomic = 0;
                   2247: 
                   2248:        if (del_for_flag) {
1.1.1.4 ! misho    2249:                if (make_backups > 1 && !dir_in_the_way) {
1.1.1.2   misho    2250:                        if (!make_backup(fname, skip_atomic))
                   2251:                                return 0;
                   2252:                } else if (skip_atomic) {
                   2253:                        int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
                   2254:                        if (delete_item(fname, sxp->st.st_mode, del_opts | del_for_flag) != 0)
                   2255:                                return 0;
                   2256:                }
                   2257:        }
                   2258: 
                   2259:        create_name = skip_atomic ? fname : tmpname;
                   2260: 
1.1.1.3   misho    2261:        if (slnk) {
1.1.1.2   misho    2262: #ifdef SUPPORT_LINKS
1.1.1.3   misho    2263:                if (do_symlink(slnk, create_name) < 0) {
                   2264:                        rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
                   2265:                                full_fname(create_name), slnk);
                   2266:                        return 0;
                   2267:                }
                   2268: #else
                   2269:                return 0;
1.1       misho    2270: #endif
1.1.1.3   misho    2271:        } else if (hlnk) {
1.1.1.2   misho    2272: #ifdef SUPPORT_HARD_LINKS
1.1.1.3   misho    2273:                if (!hard_link_one(file, create_name, hlnk, 0))
1.1.1.2   misho    2274:                        return 0;
1.1.1.3   misho    2275: #else
                   2276:                return 0;
1.1       misho    2277: #endif
1.1.1.2   misho    2278:        } else {
                   2279:                if (do_mknod(create_name, file->mode, rdev) < 0) {
                   2280:                        rsyserr(FERROR_XFER, errno, "mknod %s failed",
                   2281:                                full_fname(create_name));
                   2282:                        return 0;
                   2283:                }
                   2284:        }
                   2285: 
                   2286:        if (!skip_atomic) {
                   2287:                if (do_rename(tmpname, fname) < 0) {
                   2288:                        rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\" failed",
                   2289:                                full_fname(tmpname), full_fname(fname));
                   2290:                        do_unlink(tmpname);
                   2291:                        return 0;
                   2292:                }
                   2293:        }
                   2294: 
                   2295:        return 1;
1.1       misho    2296: }
                   2297: 
                   2298: #ifdef SUPPORT_HARD_LINKS
                   2299: static void handle_skipped_hlink(struct file_struct *file, int itemizing,
                   2300:                                 enum logcode code, int f_out)
                   2301: {
                   2302:        char fbuf[MAXPATHLEN];
                   2303:        int new_last_ndx;
                   2304:        struct file_list *save_flist = cur_flist;
                   2305: 
                   2306:        /* If we skip the last item in a chain of links and there was a
                   2307:         * prior non-skipped hard-link waiting to finish, finish it now. */
                   2308:        if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
                   2309:                return;
                   2310: 
                   2311:        file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
                   2312:        cur_flist->in_progress--; /* undo prior increment */
                   2313:        f_name(file, fbuf);
                   2314:        recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
                   2315: 
                   2316:        cur_flist = save_flist;
                   2317: }
                   2318: #endif
                   2319: 
                   2320: static void touch_up_dirs(struct file_list *flist, int ndx)
                   2321: {
                   2322:        static int counter = 0;
                   2323:        struct file_struct *file;
                   2324:        char *fname;
                   2325:        BOOL fix_dir_perms;
                   2326:        int i, start, end;
                   2327: 
                   2328:        if (ndx < 0) {
                   2329:                start = 0;
                   2330:                end = flist->used - 1;
                   2331:        } else
                   2332:                start = end = ndx;
                   2333: 
                   2334:        /* Fix any directory permissions that were modified during the
                   2335:         * transfer and/or re-set any tweaked modified-time values. */
                   2336:        for (i = start; i <= end; i++, counter++) {
                   2337:                file = flist->files[i];
1.1.1.3   misho    2338:                if (!F_IS_ACTIVE(file))
                   2339:                        continue;
1.1       misho    2340:                if (!S_ISDIR(file->mode)
                   2341:                 || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
                   2342:                        continue;
1.1.1.2   misho    2343:                if (DEBUG_GTE(TIME, 2)) {
1.1       misho    2344:                        fname = f_name(file, NULL);
                   2345:                        rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
                   2346:                                NS(fname), i);
                   2347:                }
                   2348:                /* Be sure not to retouch permissions with --fake-super. */
                   2349:                fix_dir_perms = !am_root && !(file->mode & S_IWUSR);
1.1.1.3   misho    2350:                if (file->flags & FLAG_MISSING_DIR || !(need_retouch_dir_times || fix_dir_perms))
1.1       misho    2351:                        continue;
                   2352:                fname = f_name(file, NULL);
                   2353:                if (fix_dir_perms)
1.1.1.4 ! misho    2354:                        do_chmod(fname, file->mode, 0);
1.1       misho    2355:                if (need_retouch_dir_times) {
                   2356:                        STRUCT_STAT st;
1.1.1.4 ! misho    2357:                        if (link_stat(fname, &st, 0) == 0 && mtime_differs(&st, file)) {
        !          2358:                                st.st_mtime = file->modtime;
        !          2359: #ifdef ST_MTIME_NSEC
        !          2360:                                st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
        !          2361: #endif
        !          2362: #ifdef SUPPORT_FORCE_CHANGE
        !          2363:                                st.st_mode = file->mode;
        !          2364:                                st.st_flags = 0;
        !          2365: #endif
        !          2366:                                set_times(fname, &st);
        !          2367:                        }
1.1       misho    2368:                }
1.1.1.4 ! misho    2369: #ifdef SUPPORT_FORCE_CHANGE
        !          2370:                if (force_change && F_FFLAGS(file) & force_change)
        !          2371:                        undo_make_mutable(fname, F_FFLAGS(file));
        !          2372: #endif
1.1       misho    2373:                if (counter >= loopchk_limit) {
                   2374:                        if (allowed_lull)
1.1.1.2   misho    2375:                                maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
1.1       misho    2376:                        else
                   2377:                                maybe_flush_socket(0);
                   2378:                        counter = 0;
                   2379:                }
                   2380:        }
                   2381: }
                   2382: 
                   2383: void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
                   2384: {
                   2385:        struct file_struct *file;
                   2386:        struct file_list *flist;
                   2387:        char fbuf[MAXPATHLEN];
                   2388:        int ndx;
                   2389: 
                   2390:        while (1) {
                   2391: #ifdef SUPPORT_HARD_LINKS
                   2392:                if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
                   2393:                        int send_failed = (ndx == -2);
                   2394:                        if (send_failed)
                   2395:                                ndx = get_hlink_num();
                   2396:                        flist = flist_for_ndx(ndx, "check_for_finished_files.1");
                   2397:                        file = flist->files[ndx - flist->ndx_start];
                   2398:                        assert(file->flags & FLAG_HLINKED);
                   2399:                        if (send_failed)
                   2400:                                handle_skipped_hlink(file, itemizing, code, sock_f_out);
                   2401:                        else
                   2402:                                finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
                   2403:                        flist->in_progress--;
                   2404:                        continue;
                   2405:                }
                   2406: #endif
                   2407: 
                   2408:                if (check_redo && (ndx = get_redo_num()) != -1) {
1.1.1.2   misho    2409:                        OFF_T save_max_size = max_size;
                   2410:                        OFF_T save_min_size = min_size;
1.1       misho    2411:                        csum_length = SUM_LENGTH;
1.1.1.2   misho    2412:                        max_size = -1;
                   2413:                        min_size = -1;
1.1       misho    2414:                        ignore_existing = -ignore_existing;
                   2415:                        ignore_non_existing = -ignore_non_existing;
                   2416:                        update_only = -update_only;
1.1.1.4 ! misho    2417:                        downdate_only = -downdate_only;
        !          2418:                        downdate_only = -downdate_only;
1.1       misho    2419:                        always_checksum = -always_checksum;
                   2420:                        size_only = -size_only;
                   2421:                        append_mode = -append_mode;
                   2422:                        make_backups = -make_backups; /* avoid dup backup w/inplace */
                   2423:                        ignore_times++;
                   2424: 
                   2425:                        flist = cur_flist;
                   2426:                        cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
                   2427: 
                   2428:                        file = cur_flist->files[ndx - cur_flist->ndx_start];
                   2429:                        if (solo_file)
                   2430:                                strlcpy(fbuf, solo_file, sizeof fbuf);
                   2431:                        else
                   2432:                                f_name(file, fbuf);
                   2433:                        recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
                   2434:                        cur_flist->to_redo--;
                   2435: 
                   2436:                        cur_flist = flist;
                   2437: 
                   2438:                        csum_length = SHORT_SUM_LENGTH;
1.1.1.2   misho    2439:                        max_size = save_max_size;
                   2440:                        min_size = save_min_size;
1.1       misho    2441:                        ignore_existing = -ignore_existing;
                   2442:                        ignore_non_existing = -ignore_non_existing;
                   2443:                        update_only = -update_only;
                   2444:                        always_checksum = -always_checksum;
                   2445:                        size_only = -size_only;
                   2446:                        append_mode = -append_mode;
                   2447:                        make_backups = -make_backups;
                   2448:                        ignore_times--;
                   2449:                        continue;
                   2450:                }
                   2451: 
                   2452:                if (cur_flist == first_flist)
                   2453:                        break;
                   2454: 
                   2455:                /* We only get here if inc_recurse is enabled. */
                   2456:                if (first_flist->in_progress || first_flist->to_redo)
                   2457:                        break;
                   2458: 
                   2459:                write_ndx(sock_f_out, NDX_DONE);
1.1.1.2   misho    2460:                if (!read_batch && !flist_eof) {
                   2461:                        int old_total = 0;
                   2462:                        for (flist = first_flist; flist != cur_flist; flist = flist->next)
                   2463:                                old_total += flist->used;
                   2464:                        maybe_flush_socket(!flist_eof && file_total - old_total < MIN_FILECNT_LOOKAHEAD/2);
                   2465:                }
1.1       misho    2466: 
                   2467:                if (delete_during == 2 || !dir_tweaking) {
                   2468:                        /* Skip directory touch-up. */
                   2469:                } else if (first_flist->parent_ndx >= 0)
                   2470:                        touch_up_dirs(dir_flist, first_flist->parent_ndx);
                   2471: 
                   2472:                flist_free(first_flist); /* updates first_flist */
                   2473:        }
                   2474: }
                   2475: 
                   2476: void generate_files(int f_out, const char *local_name)
                   2477: {
                   2478:        int i, ndx, next_loopchk = 0;
                   2479:        char fbuf[MAXPATHLEN];
                   2480:        int itemizing;
                   2481:        enum logcode code;
1.1.1.2   misho    2482:        int save_info_flist = info_levels[INFO_FLIST];
                   2483:        int save_info_progress = info_levels[INFO_PROGRESS];
1.1       misho    2484: 
                   2485:        if (protocol_version >= 29) {
                   2486:                itemizing = 1;
                   2487:                maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
                   2488:                code = logfile_format_has_i ? FNONE : FLOG;
                   2489:        } else if (am_daemon) {
                   2490:                itemizing = logfile_format_has_i && do_xfers;
                   2491:                maybe_ATTRS_REPORT = ATTRS_REPORT;
                   2492:                code = itemizing || !do_xfers ? FCLIENT : FINFO;
                   2493:        } else if (!am_server) {
                   2494:                itemizing = stdout_format_has_i;
                   2495:                maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
                   2496:                code = itemizing ? FNONE : FINFO;
                   2497:        } else {
                   2498:                itemizing = 0;
                   2499:                maybe_ATTRS_REPORT = ATTRS_REPORT;
                   2500:                code = FINFO;
                   2501:        }
                   2502:        solo_file = local_name;
                   2503:        dir_tweaking = !(list_only || solo_file || dry_run);
                   2504:        need_retouch_dir_times = preserve_times & PRESERVE_DIR_TIMES;
                   2505:        loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
                   2506:        symlink_timeset_failed_flags = ITEM_REPORT_TIME
                   2507:            | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
                   2508:        implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
                   2509: 
1.1.1.2   misho    2510:        if (DEBUG_GTE(GENR, 1))
                   2511:                rprintf(FINFO, "generator starting pid=%d\n", (int)getpid());
1.1       misho    2512: 
1.1.1.4 ! misho    2513:        if (detect_renamed) {
        !          2514:                delayed_bits = bitbag_create(cur_flist->used);
        !          2515:                if (!delete_before && !delete_during)
        !          2516:                        delete_during = -1;
        !          2517:        }
        !          2518: 
1.1       misho    2519:        if (delete_before && !solo_file && cur_flist->used > 0)
                   2520:                do_delete_pass();
                   2521:        if (delete_during == 2) {
                   2522:                deldelay_size = BIGPATHBUFLEN * 4;
                   2523:                deldelay_buf = new_array(char, deldelay_size);
                   2524:        }
1.1.1.2   misho    2525:        info_levels[INFO_FLIST] = info_levels[INFO_PROGRESS] = 0;
1.1       misho    2526: 
1.1.1.4 ! misho    2527:        if (append_mode > 0 || detect_renamed || whole_file < 0)
1.1       misho    2528:                whole_file = 0;
1.1.1.2   misho    2529:        if (DEBUG_GTE(FLIST, 1)) {
1.1       misho    2530:                rprintf(FINFO, "delta-transmission %s\n",
                   2531:                        whole_file
                   2532:                        ? "disabled for local transfer or --whole-file"
                   2533:                        : "enabled");
                   2534:        }
                   2535: 
1.1.1.4 ! misho    2536:        if (use_db) {
        !          2537:                if (always_checksum || (append_mode != 1 && protocol_version >= 30))
        !          2538:                        db_connect(0); /* Will reset use_db on error. */
        !          2539:                else
        !          2540:                        use_db = 0;
        !          2541:        }
        !          2542: 
1.1       misho    2543:        dflt_perms = (ACCESSPERMS & ~orig_umask);
                   2544: 
                   2545:        do {
                   2546: #ifdef SUPPORT_HARD_LINKS
                   2547:                if (preserve_hard_links && inc_recurse) {
1.1.1.2   misho    2548:                        while (!flist_eof && file_total < MIN_FILECNT_LOOKAHEAD/2)
1.1       misho    2549:                                wait_for_receiver();
                   2550:                }
                   2551: #endif
                   2552: 
                   2553:                if (inc_recurse && cur_flist->parent_ndx >= 0) {
                   2554:                        struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
                   2555:                        if (solo_file)
                   2556:                                strlcpy(fbuf, solo_file, sizeof fbuf);
                   2557:                        else
                   2558:                                f_name(fp, fbuf);
                   2559:                        ndx = cur_flist->ndx_start - 1;
                   2560:                        recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
                   2561:                        if (delete_during && dry_run < 2 && !list_only
                   2562:                         && !(fp->flags & FLAG_MISSING_DIR)) {
                   2563:                                if (fp->flags & FLAG_CONTENT_DIR) {
                   2564:                                        dev_t dirdev;
                   2565:                                        if (one_file_system) {
                   2566:                                                uint32 *devp = F_DIR_DEV_P(fp);
                   2567:                                                dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
                   2568:                                        } else
                   2569:                                                dirdev = MAKEDEV(0, 0);
1.1.1.4 ! misho    2570:                                        delete_in_dir(fbuf, fp, &dirdev, 0);
1.1       misho    2571:                                } else
                   2572:                                        change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
                   2573:                        }
1.1.1.4 ! misho    2574:                        upcoming_whole_dir = fp->flags & FLAG_CONTENT_DIR ? 1 : 0;
1.1       misho    2575:                }
                   2576:                for (i = cur_flist->low; i <= cur_flist->high; i++) {
                   2577:                        struct file_struct *file = cur_flist->sorted[i];
                   2578: 
                   2579:                        if (!F_IS_ACTIVE(file))
                   2580:                                continue;
                   2581: 
                   2582:                        if (unsort_ndx)
                   2583:                                ndx = F_NDX(file);
                   2584:                        else
                   2585:                                ndx = i + cur_flist->ndx_start;
                   2586: 
                   2587:                        if (solo_file)
                   2588:                                strlcpy(fbuf, solo_file, sizeof fbuf);
                   2589:                        else
                   2590:                                f_name(file, fbuf);
                   2591:                        recv_generator(fbuf, file, ndx, itemizing, code, f_out);
                   2592: 
                   2593:                        check_for_finished_files(itemizing, code, 0);
                   2594: 
                   2595:                        if (i + cur_flist->ndx_start >= next_loopchk) {
                   2596:                                if (allowed_lull)
1.1.1.2   misho    2597:                                        maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
1.1       misho    2598:                                else
                   2599:                                        maybe_flush_socket(0);
                   2600:                                next_loopchk += loopchk_limit;
                   2601:                        }
                   2602:                }
                   2603: 
                   2604:                if (!inc_recurse) {
                   2605:                        write_ndx(f_out, NDX_DONE);
                   2606:                        break;
                   2607:                }
                   2608: 
                   2609:                while (1) {
                   2610:                        check_for_finished_files(itemizing, code, 1);
                   2611:                        if (cur_flist->next || flist_eof)
                   2612:                                break;
                   2613:                        wait_for_receiver();
                   2614:                }
                   2615:        } while ((cur_flist = cur_flist->next) != NULL);
                   2616: 
                   2617:        if (delete_during)
1.1.1.4 ! misho    2618:                delete_in_dir(NULL, NULL, &dev_zero, 0);
        !          2619:        if (detect_renamed) {
        !          2620:                if (delete_during < 0)
        !          2621:                        delete_during = 0;
        !          2622:                detect_renamed = 0;
        !          2623: 
        !          2624:                for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
        !          2625:                        struct file_struct *file = cur_flist->files[i];
        !          2626:                        if (local_name)
        !          2627:                                strlcpy(fbuf, local_name, sizeof fbuf);
        !          2628:                        else
        !          2629:                                f_name(file, fbuf);
        !          2630:                        recv_generator(fbuf, file, i, itemizing, code, f_out);
        !          2631:                }
        !          2632:        }
1.1       misho    2633:        phase++;
1.1.1.2   misho    2634:        if (DEBUG_GTE(GENR, 1))
1.1       misho    2635:                rprintf(FINFO, "generate_files phase=%d\n", phase);
                   2636: 
                   2637:        while (1) {
                   2638:                check_for_finished_files(itemizing, code, 1);
                   2639:                if (msgdone_cnt)
                   2640:                        break;
                   2641:                wait_for_receiver();
                   2642:        }
                   2643: 
                   2644:        phase++;
1.1.1.2   misho    2645:        if (DEBUG_GTE(GENR, 1))
1.1       misho    2646:                rprintf(FINFO, "generate_files phase=%d\n", phase);
                   2647: 
                   2648:        write_ndx(f_out, NDX_DONE);
                   2649: 
                   2650:        /* Reduce round-trip lag-time for a useless delay-updates phase. */
1.1.1.2   misho    2651:        if (protocol_version >= 29 && EARLY_DELAY_DONE_MSG())
1.1       misho    2652:                write_ndx(f_out, NDX_DONE);
                   2653: 
1.1.1.2   misho    2654:        if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG()) {
                   2655:                if ((INFO_GTE(STATS, 2) && (delete_mode || force_delete)) || read_batch)
                   2656:                        write_del_stats(f_out);
                   2657:                if (EARLY_DELAY_DONE_MSG()) /* Can't send this before delay */
                   2658:                        write_ndx(f_out, NDX_DONE);
                   2659:        }
                   2660: 
1.1       misho    2661:        /* Read MSG_DONE for the redo phase (and any prior messages). */
                   2662:        while (1) {
                   2663:                check_for_finished_files(itemizing, code, 0);
                   2664:                if (msgdone_cnt > 1)
                   2665:                        break;
                   2666:                wait_for_receiver();
                   2667:        }
                   2668: 
                   2669:        if (protocol_version >= 29) {
                   2670:                phase++;
1.1.1.2   misho    2671:                if (DEBUG_GTE(GENR, 1))
1.1       misho    2672:                        rprintf(FINFO, "generate_files phase=%d\n", phase);
1.1.1.2   misho    2673:                if (!EARLY_DELAY_DONE_MSG()) {
1.1       misho    2674:                        write_ndx(f_out, NDX_DONE);
1.1.1.2   misho    2675:                        if (protocol_version >= 31 && EARLY_DELETE_DONE_MSG())
                   2676:                                write_ndx(f_out, NDX_DONE);
                   2677:                }
1.1       misho    2678:                /* Read MSG_DONE for delay-updates phase & prior messages. */
                   2679:                while (msgdone_cnt == 2)
                   2680:                        wait_for_receiver();
                   2681:        }
                   2682: 
1.1.1.4 ! misho    2683:        if (checksum_files)
        !          2684:                reset_checksum_cache(started_whole_dir);
        !          2685: 
        !          2686:        if (use_db)
        !          2687:                db_disconnect(True);
        !          2688: 
1.1.1.2   misho    2689:        info_levels[INFO_FLIST] = save_info_flist;
                   2690:        info_levels[INFO_PROGRESS] = save_info_progress;
                   2691: 
1.1       misho    2692:        if (delete_during == 2)
                   2693:                do_delayed_deletions(fbuf);
                   2694:        if (delete_after && !solo_file && file_total > 0)
                   2695:                do_delete_pass();
                   2696: 
1.1.1.2   misho    2697:        if (max_delete >= 0 && skipped_deletes) {
1.1       misho    2698:                rprintf(FWARNING,
                   2699:                        "Deletions stopped due to --max-delete limit (%d skipped)\n",
1.1.1.2   misho    2700:                        skipped_deletes);
1.1       misho    2701:                io_error |= IOERR_DEL_LIMIT;
                   2702:        }
                   2703: 
1.1.1.2   misho    2704:        if (protocol_version >= 31) {
                   2705:                if (!EARLY_DELETE_DONE_MSG()) {
                   2706:                        if (INFO_GTE(STATS, 2) || read_batch)
                   2707:                                write_del_stats(f_out);
                   2708:                        write_ndx(f_out, NDX_DONE);
                   2709:                }
                   2710: 
                   2711:                /* Read MSG_DONE for late-delete phase & prior messages. */
                   2712:                while (msgdone_cnt == 3)
                   2713:                        wait_for_receiver();
                   2714:        }
                   2715: 
                   2716:        if ((need_retouch_dir_perms || need_retouch_dir_times)
                   2717:         && dir_tweaking && (!inc_recurse || delete_during == 2))
                   2718:                touch_up_dirs(dir_flist, -1);
                   2719: 
                   2720:        if (DEBUG_GTE(GENR, 1))
1.1       misho    2721:                rprintf(FINFO, "generate_files finished\n");
                   2722: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>