+- if (do_lstat(backup_dir_buf, &sx.st) < 0) {
++ if (do_lstat(buf, &sx.st) < 0) {
+ if (errno == ENOENT)
+ break;
+- rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
++ rsyserr(FERROR, errno, "backup lstat %s failed", buf);
+ *name = '\0';
+ return False;
+ }
+ if (!S_ISDIR(sx.st.st_mode)) {
+ flags = get_del_for_flag(sx.st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
+- if (delete_item(backup_dir_buf, sx.st.st_mode, flags) == 0)
++ if (delete_item(buf, sx.st.st_mode, flags) == 0)
+ break;
+ *name = '\0';
+ return False;
+@@ -85,8 +92,8 @@ static BOOL copy_valid_path(const char *fname)
+ for ( ; b; name = b + 1, b = strchr(name, '/')) {
+ *b = '\0';
+
+- if (mkdir_defmode(backup_dir_buf) < 0) {
+- rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf);
++ if (mkdir_defmode(buf) < 0) {
++ rsyserr(FERROR, errno, "backup mkdir %s failed", buf);
+ *name = '\0';
+ ret = False;
+ break;
+@@ -114,7 +121,7 @@ static BOOL copy_valid_path(const char *fname)
+ free_xattr(&sx);
+ }
+ #endif
+- set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
++ set_file_attrs(buf, file, NULL, NULL, 0);
+ unmake_file(file);
+ }
+
+@@ -134,15 +141,20 @@ static BOOL copy_valid_path(const char *fname)
+ /* Make a complete pathname for backup file and verify any new path elements. */
+ char *get_backup_name(const char *fname)
+ {
++ char *buf = deleting ? backup_dir_dels_buf : backup_dir_buf;
++ char *suffix = deleting ? backup_suffix_dels : backup_suffix;
++
+ if (backup_dir) {
++ int prefix_len = deleting ? backup_dir_dels_len : backup_dir_len;
++ unsigned int remainder = deleting ? backup_dir_dels_remainder : backup_dir_remainder;
+ /* copy fname into backup_dir_buf while validating the dirs. */
+- if (copy_valid_path(fname))
+- return backup_dir_buf;
++ if (copy_valid_path(fname, buf, prefix_len, remainder, suffix))
++ return buf;
+ return NULL;
+ } else {
+ if (stringjoin(backup_dir_buf, MAXPATHLEN,
+- fname, backup_suffix, NULL) < MAXPATHLEN)
+- return backup_dir_buf;
++ fname, suffix, NULL) < MAXPATHLEN)
++ return buf;
+ }