+extern int checksum_len;
+extern char *partial_dir;
+extern char *basis_dir[];
+extern int compare_dest;
+extern int copy_dest;
+extern int link_dest;
+extern int whole_file;
+extern int list_only;
+extern int read_batch;
+extern int safe_symlinks;
+extern long block_size; /* "long" because popt can't set an int32. */
+extern int unsort_ndx;
+extern int max_delete;
+extern int force_delete;
+extern int one_file_system;
+extern struct stats stats;
+extern dev_t filesystem_dev;
+extern mode_t orig_umask;
+extern uid_t our_uid;
+extern char *backup_dir;
+extern char *backup_suffix;
+extern int backup_suffix_len;
+extern struct file_list *cur_flist, *first_flist, *dir_flist;
+extern struct filter_list_struct server_filter_list;
+
+int ignore_perishable = 0;
+int non_perishable_cnt = 0;
+int maybe_ATTRS_REPORT = 0;
+
+static dev_t dev_zero;
+static int deletion_count = 0; /* used to implement --max-delete */
+static int deldelay_size = 0, deldelay_cnt = 0;
+static char *deldelay_buf = NULL;
+static int deldelay_fd = -1;
+static int lull_mod;
+static int dir_tweaking;
+static int need_retouch_dir_times;
+static int need_retouch_dir_perms;
+static const char *solo_file = NULL;
+
+/* For calling delete_item() and delete_dir_contents(). */
+#define DEL_OWNED_BY_US (1<<0) /* file/dir has our uid */
+#define DEL_RECURSE (1<<1) /* if dir, delete all contents */
+#define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
+#define DEL_FOR_FILE (1<<3) /* making room for a replacement file */
+#define DEL_FOR_DIR (1<<4) /* making room for a replacement dir */
+#define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
+#define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
+#define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
+
+#define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
+
+enum nonregtype {
+ TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
+};
+
+enum delret {
+ DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
+};
+
+/* Forward declaration for delete_item(). */
+static enum delret delete_dir_contents(char *fname, int flags);
+
+static int is_backup_file(char *fn)
+{
+ int k = strlen(fn) - backup_suffix_len;
+ return k > 0 && strcmp(fn+k, backup_suffix) == 0;
+}
+
+/* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
+ * delete recursively.
+ *
+ * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
+ * a directory! (The buffer is used for recursion, but returned unchanged.)
+ */
+static enum delret delete_item(char *fbuf, int mode, int flags)
+{
+ enum delret ret;
+ char *what;
+ int ok;
+
+ if (verbose > 2) {
+ rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
+ fbuf, mode, flags);
+ }
+
+ if (!am_root && !(mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
+ do_chmod(fbuf, mode |= S_IWUSR);
+
+ if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
+ ignore_perishable = 1;
+ /* If DEL_RECURSE is not set, this just reports emptiness. */
+ ret = delete_dir_contents(fbuf, flags);
+ ignore_perishable = 0;
+ if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
+ goto check_ret;
+ /* OK: try to delete the directory. */
+ }
+
+ if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && ++deletion_count > max_delete)
+ return DR_AT_LIMIT;
+
+ if (S_ISDIR(mode)) {
+ what = "rmdir";
+ ok = do_rmdir(fbuf) == 0;
+ } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
+ what = "make_backup";
+ ok = make_backup(fbuf);
+ } else {
+ what = "unlink";
+ ok = robust_unlink(fbuf) == 0;
+ }