-
- /* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
- * force_delete is set, this will delete recursively.
- *
- * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
- * a directory! (The buffer is used for recursion, but returned unchanged.)
-+ *
-+ * Also Note: --detect-rename may use this routine with DEL_NO_DELETIONS set!
- */
- static int delete_item(char *fname, int mode, int flags)
- {
-@@ -125,6 +237,8 @@ static int delete_item(char *fname, int
- char *p;
-
- if (!S_ISDIR(mode)) {
-+ if (flags & DEL_NO_DELETIONS)
-+ return 0;
- if (max_delete && ++deletion_count > max_delete)
- return 0;
- if (make_backups && (backup_dir || !is_backup_file(fname)))
-@@ -147,6 +261,7 @@ static int delete_item(char *fname, int
-
- zap_dir = flags & DEL_FORCE_RECURSE || force_delete;
- if ((max_delete && ++deletion_count > max_delete)
-+ || flags & DEL_NO_DELETIONS
- || (dry_run && zap_dir)) {
- ok = 0;
- errno = ENOTEMPTY;
-@@ -189,6 +304,8 @@ static int delete_item(char *fname, int
- continue;
-
- strlcpy(p, fp->basename, remainder);
-+ if (detect_renamed && S_ISREG(fp->mode))
-+ look_for_rename(fp, fname, dirlist->file_pool);
- delete_item(fname, fp->mode, flags & ~DEL_TERSE);
- }
- flist_free(dirlist);
-@@ -197,7 +314,8 @@ static int delete_item(char *fname, int
-
- pop_local_filters(save_filters);
-
-- if (max_delete && ++deletion_count > max_delete)
-+ if (flags & DEL_NO_DELETIONS
-+ || (max_delete && ++deletion_count > max_delete))
- return 0;
-
- if (do_rmdir(fname) == 0) {
-@@ -217,15 +335,19 @@ static int delete_item(char *fname, int
- * 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) {
-@@ -239,6 +361,8 @@ static void delete_in_dir(struct file_li
- if (verbose > 2)
- rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
-
-+ flags |= DEL_FORCE_RECURSE;
-+
- if (allowed_lull)
- maybe_send_keepalive();
-
-@@ -246,12 +370,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)
-@@ -262,6 +388,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;
-@@ -271,18 +400,30 @@ 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--; ) {
- struct file_struct *fp = dirlist->files[i];
- if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
- 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);
-- delete_item(delbuf, fp->mode, DEL_FORCE_RECURSE);
-- }
-+ delete_item(delbuf, fp->mode, flags);
-+ } else if (detect_renamed && S_ISDIR(fp->mode))
-+ unexplored_dirs++;
- }
-
-+ fbuf[dlen] = '\0';