+ if (deldelay_fd >= 0) {
+ if (deldelay_cnt && !flush_delete_delay())
+ return;
+ lseek(deldelay_fd, 0, 0);
+ }
+ while ((mode = read_delay_line(delbuf)) >= 0)
+ delete_item(delbuf, mode, NULL, DEL_RECURSE);
+ if (deldelay_fd >= 0)
+ close(deldelay_fd);
+}
+
+/* This function is used to implement per-directory deletion, and is used by
+ * all the --delete-WHEN options. Note that the fbuf pointer must point to a
+ * MAXPATHLEN buffer with the name of the directory in it (the functions we
+ * call will append names onto the end, but the old dir value will be restored
+ * on exit). */
+static void delete_in_dir(struct file_list *flist, char *fbuf,
+ struct file_struct *file, STRUCT_STAT *stp)
+{
+ static int min_depth = MAXPATHLEN, cur_depth = -1;
+ static void *filt_array[MAXPATHLEN/2+1];
+ static int already_warned = 0;
+ struct file_list *dirlist;
+ char delbuf[MAXPATHLEN];
+ int dlen, i;
+
+ if (!flist) {
+ while (cur_depth >= min_depth)
+ pop_local_filters(filt_array[cur_depth--]);
+ min_depth = MAXPATHLEN;
+ cur_depth = -1;
+ return;
+ }
+
+ if (verbose > 2)
+ rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
+
+ if (allowed_lull)
+ maybe_send_keepalive();
+
+ if (file->dir.depth >= MAXPATHLEN/2+1)
+ return; /* Impossible... */
+
+ if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
+ if (already_warned)
+ return;
+ rprintf(FINFO,
+ "IO error encountered -- skipping file deletion\n");
+ already_warned = 1;
+ return;
+ }
+
+ while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
+ pop_local_filters(filt_array[cur_depth--]);
+ cur_depth = file->dir.depth;
+ if (min_depth > cur_depth)
+ min_depth = cur_depth;
+ dlen = strlen(fbuf);
+ filt_array[cur_depth] = push_local_filters(fbuf, dlen);
+
+ if (one_file_system) {
+ if (file->flags & FLAG_TOP_DIR)
+ filesystem_dev = stp->st_dev;
+ else if (filesystem_dev != stp->st_dev)
+ return;
+ }
+
+ dirlist = get_dirlist(fbuf, dlen, 0);
+
+ /* If an item in dirlist is not found in flist, delete it
+ * from the filesystem. */
+ for (i = dirlist->count; i--; ) {
+ struct file_struct *fp = dirlist->files[i];
+ if (!F_IS_ACTIVE(fp))
+ continue;
+ if (fp->flags & FLAG_MOUNT_DIR) {
+ if (verbose > 1)
+ rprintf(FINFO, "cannot delete mount point: %s\n",
+ f_name(fp, NULL));
+ continue;
+ }
+ if (flist_find(flist, fp) < 0) {
+ f_name(fp, delbuf);
+ if (delete_during == 2) {
+ if (!remember_delete(fp, delbuf))
+ break;
+ } else
+ delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
+ }
+ }
+
+ flist_free(dirlist);
+}
+
+/* This deletes any files on the receiving side that are not present on the
+ * sending side. This is used by --delete-before and --delete-after. */
+static void do_delete_pass(struct file_list *flist)
+{
+ char fbuf[MAXPATHLEN];
+ STRUCT_STAT st;
+ int j;
+
+ /* dry_run is incremented when the destination doesn't exist yet. */
+ if (dry_run > 1 || list_only)
+ return;
+
+ for (j = 0; j < flist->count; j++) {
+ struct file_struct *file = flist->files[j];
+
+ if (!(file->flags & FLAG_XFER_DIR))
+ continue;
+
+ f_name(file, fbuf);
+ if (verbose > 1 && file->flags & FLAG_TOP_DIR)
+ rprintf(FINFO, "deleting in %s\n", fbuf);
+
+ if (link_stat(fbuf, &st, keep_dirlinks) < 0
+ || !S_ISDIR(st.st_mode))
+ continue;
+
+ delete_in_dir(flist, fbuf, file, &st);
+ }
+ delete_in_dir(NULL, NULL, NULL, NULL);
+
+ if (do_progress && !am_server)
+ rprintf(FINFO, " \r");
+}
+
+int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
+{
+ if (preserve_perms
+ && (unsigned)(st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
+ return 0;
+
+ if (am_root && preserve_uid && st->st_uid != F_UID(file))
+ return 0;
+
+ if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file))
+ return 0;
+
+ return 1;
+}
+
+void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
+ int32 iflags, uchar fnamecmp_type, const char *xname)
+{
+ if (statret >= 0) { /* A from-dest-dir statret can == 1! */
+ int keep_time = !preserve_times ? 0
+ : S_ISDIR(file->mode) ? !omit_dir_times
+ : !S_ISLNK(file->mode);
+
+ if (S_ISREG(file->mode) && F_LENGTH(file) != st->st_size)
+ iflags |= ITEM_REPORT_SIZE;
+ if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
+ && !(iflags & ITEM_MATCHED)
+ && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
+ || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
+ iflags |= ITEM_REPORT_TIME;
+ if ((unsigned)(st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
+ iflags |= ITEM_REPORT_PERMS;
+ if (preserve_uid && am_root && F_UID(file) != st->st_uid)
+ iflags |= ITEM_REPORT_OWNER;
+ if (preserve_gid && F_GID(file) != GID_NONE
+ && st->st_gid != F_GID(file))
+ iflags |= ITEM_REPORT_GROUP;
+ } else
+ iflags |= ITEM_IS_NEW;
+
+ iflags &= 0xffff;
+ if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
+ || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
+ if (protocol_version >= 29) {
+ if (ndx >= 0)
+ write_int(sock_f_out, ndx);
+ write_shortint(sock_f_out, iflags);
+ if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
+ write_byte(sock_f_out, fnamecmp_type);
+ if (iflags & ITEM_XNAME_FOLLOWS)
+ write_vstring(sock_f_out, xname, strlen(xname));
+ } else if (ndx >= 0) {
+ enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
+ log_item(code, file, &stats, iflags, xname);
+ }
+ }
+}
+
+
+/* Perform our quick-check heuristic for determining if a file is unchanged. */
+int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)