-@@ -228,7 +345,9 @@ static enum delret delete_dir_contents(c
- save_filters = push_local_filters(fname, dlen);
-
- non_perishable_cnt = 0;
-+ file_extra_cnt += SUM_EXTRA_CNT;
- dirlist = get_dirlist(fname, dlen, 0);
-+ file_extra_cnt -= SUM_EXTRA_CNT;
- ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
-
- if (!dirlist->count)
-@@ -265,6 +384,8 @@ static enum delret delete_dir_contents(c
- if (S_ISDIR(fp->mode)
- && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
- ret = DR_NOT_EMPTY;
-+ if (detect_renamed && S_ISREG(fp->mode))
-+ look_for_rename(fp, fname);
- if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
- ret = DR_NOT_EMPTY;
- }
-@@ -417,12 +538,17 @@ static void do_delayed_deletions(char *d
- * 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(char *fbuf, struct file_struct *file, dev_t *fs_dev)
-+ * on exit).
-+ *
-+ * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
-+ */
-+static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev,
-+ int flags)
- {
- static int already_warned = 0;
- struct file_list *dirlist;
-- char delbuf[MAXPATHLEN];
-+ char *p, delbuf[MAXPATHLEN];
-+ unsigned remainder;
- int dlen, i;
-
- if (!fbuf) {
-@@ -433,21 +559,28 @@ static void delete_in_dir(char *fbuf, st
- if (verbose > 2)
- rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
-
-+ flags |= DEL_RECURSE;
-+
- if (allowed_lull)
- maybe_send_keepalive();
-
- if (io_error && !ignore_errors) {
-- if (already_warned)
-+ if (!already_warned) {
-+ rprintf(FINFO,
-+ "IO error encountered -- skipping file deletion\n");
-+ already_warned = 1;
-+ }
-+ if (!detect_renamed)
- return;
-- rprintf(FINFO,
-- "IO error encountered -- skipping file deletion\n");
-- already_warned = 1;
-- return;
-+ flags |= DEL_NO_DELETIONS;
- }
-
- dlen = strlen(fbuf);
- change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
-
-+ if (detect_renamed)
-+ unexplored_dirs--;
-+
- if (one_file_system) {
- if (file->flags & FLAG_TOP_DIR)
- filesystem_dev = *fs_dev;
-@@ -457,6 +590,11 @@ static void delete_in_dir(char *fbuf, st
-
- dirlist = get_dirlist(fbuf, dlen, 0);
-
-+ p = fbuf + dlen;
-+ if (dlen != 1 || *fbuf != '/')
-+ *p++ = '/';
-+ remainder = MAXPATHLEN - (p - fbuf);
-+
- /* If an item in dirlist is not found in flist, delete it
- * from the filesystem. */
- for (i = dirlist->count; i--; ) {
-@@ -469,16 +607,23 @@ static void delete_in_dir(char *fbuf, st
- f_name(fp, NULL));
- continue;
- }
-+ if (detect_renamed && S_ISREG(fp->mode)) {
-+ strlcpy(p, fp->basename, remainder);
-+ look_for_rename(fp, fbuf);
-+ }
- if (flist_find(cur_flist, fp) < 0) {
- f_name(fp, delbuf);
-- if (delete_during == 2) {
-+ if (delete_during == 2 && !(flags & DEL_NO_DELETIONS)) {
- if (!remember_delete(fp, delbuf))
- break;
- } else
-- delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
-- }
-+ delete_item(delbuf, fp->mode, NULL, flags);
-+ } else if (detect_renamed && S_ISDIR(fp->mode))
-+ unexplored_dirs++;