+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 daemon_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 symlink_timeset_failed_flags;
+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_NO_UID_WRITE (1<<0) /* file/dir has our uid w/o write perm */
+#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 declarations. */
+static enum delret delete_dir_contents(char *fname, uint16 flags);
+#ifdef SUPPORT_HARD_LINKS
+static void handle_skipped_hlink(struct file_struct *file, int itemizing,
+ enum logcode code, int f_out);
+#endif
+
+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, uint16 mode, uint16 flags)
+{
+ enum delret ret;
+ char *what;
+ int ok;
+
+ if (verbose > 2) {
+ rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
+ fbuf, (int)mode, (int)flags);
+ }
+
+ if (flags & DEL_NO_UID_WRITE)
+ do_chmod(fbuf, mode | S_IWUSR);
+
+ if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
+ int save_uid_ndx = uid_ndx;
+ /* This only happens on the first call to delete_item() since
+ * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
+ if (!uid_ndx)
+ uid_ndx = ++file_extra_cnt;
+ ignore_perishable = 1;
+ /* If DEL_RECURSE is not set, this just reports emptiness. */
+ ret = delete_dir_contents(fbuf, flags);
+ ignore_perishable = 0;
+ if (!save_uid_ndx) {
+ --file_extra_cnt;
+ uid_ndx = 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;
+ }
+
+ if (ok) {
+ if (!(flags & DEL_MAKE_ROOM))
+ log_delete(fbuf, mode);
+ ret = DR_SUCCESS;
+ } else {
+ if (S_ISDIR(mode) && errno == ENOTEMPTY) {
+ rprintf(FINFO, "cannot delete non-empty directory: %s\n",
+ fbuf);
+ ret = DR_NOT_EMPTY;
+ } else if (errno != ENOENT) {
+ rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
+ what, fbuf);
+ ret = DR_FAILURE;
+ } else {
+ deletion_count--;
+ ret = DR_SUCCESS;
+ }
+ }