-@@ -249,6 +366,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, dirlist->file_pool);
- if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
- ret = DR_NOT_EMPTY;
- }
-@@ -333,15 +452,19 @@ static void delayed_deletions(char *delb
- * 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). */
-+ * on exit).
-+ *
-+ * Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
-+ */
- static void delete_in_dir(struct file_list *flist, char *fbuf,
-- struct file_struct *file, STRUCT_STAT *stp)
-+ struct file_struct *file, STRUCT_STAT *stp, int flags)
- {
- 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];
-+ char *p, delbuf[MAXPATHLEN];
-+ unsigned remainder;
- int dlen, i;
-
- if (!flist) {
-@@ -355,6 +478,8 @@ static void delete_in_dir(struct file_li
- if (verbose > 2)
- rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
-
-+ flags |= DEL_RECURSE;
-+
- if (allowed_lull)
- maybe_send_keepalive();
-
-@@ -362,12 +487,14 @@ static void delete_in_dir(struct file_li
- return; /* Impossible... */
-
- if (io_error && !(lp_ignore_errors(module_id) || 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;
- }
-
- while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
-@@ -378,6 +505,9 @@ static void delete_in_dir(struct file_li
- dlen = strlen(fbuf);
- filt_array[cur_depth] = push_local_filters(fbuf, dlen);
-
-+ if (detect_renamed)
-+ unexplored_dirs--;
-+
- if (one_file_system) {
- if (file->flags & FLAG_TOP_DIR)
- filesystem_dev = stp->st_dev;
-@@ -387,6 +517,11 @@ static void delete_in_dir(struct file_li
-
- 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--; ) {
-@@ -399,15 +534,22 @@ static void delete_in_dir(struct file_li
- f_name(fp, NULL));
- continue;
- }
-+ if (detect_renamed && S_ISREG(fp->mode)) {
-+ strlcpy(p, fp->basename, remainder);
-+ look_for_rename(fp, fbuf, dirlist->file_pool);
-+ }
- if (flist_find(flist, fp) < 0) {
- f_name(fp, delbuf);
-- if (delete_delay_fp)
-+ if (delete_delay_fp && !(flags & DEL_NO_DELETIONS))
- fprintf(delete_delay_fp, "%o %s%c", fp->mode, delbuf, '\0');
- 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++;
- }
-
-+ fbuf[dlen] = '\0';
-+
- flist_free(dirlist);
- }
-
-@@ -437,9 +579,9 @@ static void do_delete_pass(struct file_l
- || !S_ISDIR(st.st_mode))
- continue;
-
-- delete_in_dir(flist, fbuf, file, &st);
-+ delete_in_dir(flist, fbuf, file, &st, 0);