+ int new_last_ndx;
+ struct file_list *save_flist = cur_flist;
+
+ /* If we skip the last item in a chain of links and there was a
+ * prior non-skipped hard-link waiting to finish, finish it now. */
+ if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
+ return;
+
+ file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
+ cur_flist->in_progress--; /* undo prior increment */
+ f_name(file, fbuf);
+ recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
+
+ cur_flist = save_flist;
+}
+#endif
+
+static void touch_up_dirs(struct file_list *flist, int ndx)
+{
+ static int counter = 0;
+ struct file_struct *file;
+ char *fname;
+ int i, start, end;
+
+ if (ndx < 0) {
+ start = 0;
+ end = flist->used - 1;
+ } else
+ start = end = ndx;
+
+ /* Fix any directory permissions that were modified during the
+ * transfer and/or re-set any tweaked modified-time values. */
+ for (i = start; i <= end; i++, counter++) {
+ file = flist->files[i];
+ if (!S_ISDIR(file->mode)
+ || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
+ continue;
+ if (DEBUG_GTE(TIME, 2)) {
+ fname = f_name(file, NULL);
+ rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
+ NS(fname), i);
+ }
+ if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
+ || (!need_retouch_dir_times && file->mode & S_IWUSR))
+ continue;
+ fname = f_name(file, NULL);
+ if (!(file->mode & S_IWUSR))
+ do_chmod(fname, file->mode);
+ if (need_retouch_dir_times) {
+ STRUCT_STAT st;
+ if (link_stat(fname, &st, 0) == 0
+ && cmp_time(st.st_mtime, file->modtime) != 0)
+ set_modtime(fname, file->modtime, file->mode);
+ }
+ if (counter >= loopchk_limit) {
+ if (allowed_lull)
+ maybe_send_keepalive();
+ else
+ maybe_flush_socket(0);
+ counter = 0;
+ }
+ }
+}
+
+void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
+{
+ struct file_struct *file;
+ struct file_list *flist;
+ char fbuf[MAXPATHLEN];
+ int ndx;
+
+ while (1) {
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
+ flist = flist_for_ndx(ndx, "check_for_finished_files.1");
+ file = flist->files[ndx - flist->ndx_start];
+ assert(file->flags & FLAG_HLINKED);
+ finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
+ flist->in_progress--;
+ continue;
+ }
+#endif
+
+ if (check_redo && (ndx = get_redo_num()) != -1) {
+ csum_length = SUM_LENGTH;
+ max_size = -max_size;
+ min_size = -min_size;
+ ignore_existing = -ignore_existing;
+ ignore_non_existing = -ignore_non_existing;
+ update_only = -update_only;
+ always_checksum = -always_checksum;
+ size_only = -size_only;
+ append_mode = -append_mode;
+ make_backups = -make_backups; /* avoid dup backup w/inplace */
+ ignore_times++;
+
+ flist = cur_flist;
+ cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
+
+ file = cur_flist->files[ndx - cur_flist->ndx_start];
+ if (solo_file)
+ strlcpy(fbuf, solo_file, sizeof fbuf);
+ else
+ f_name(file, fbuf);
+ recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
+ cur_flist->to_redo--;
+
+ cur_flist = flist;
+
+ csum_length = SHORT_SUM_LENGTH;
+ max_size = -max_size;
+ min_size = -min_size;
+ ignore_existing = -ignore_existing;
+ ignore_non_existing = -ignore_non_existing;
+ update_only = -update_only;
+ always_checksum = -always_checksum;
+ size_only = -size_only;
+ append_mode = -append_mode;
+ make_backups = -make_backups;
+ ignore_times--;
+ continue;
+ }
+
+ if (cur_flist == first_flist)
+ break;
+
+ /* We only get here if inc_recurse is enabled. */
+ if (first_flist->in_progress || first_flist->to_redo)
+ break;
+
+ if (!read_batch) {
+ write_ndx(sock_f_out, NDX_DONE);
+ maybe_flush_socket(1);
+ }
+
+ if (delete_during == 2 || !dir_tweaking) {
+ /* Skip directory touch-up. */
+ } else if (first_flist->parent_ndx >= 0)
+ touch_up_dirs(dir_flist, first_flist->parent_ndx);
+
+ flist_free(first_flist); /* updates first_flist */
+ }
+}
+
+void generate_files(int f_out, const char *local_name)
+{
+ int i, ndx, next_loopchk = 0;
+ char fbuf[MAXPATHLEN];
+ int itemizing;