+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 new_root_dir;
+extern int read_batch;
+extern int safe_symlinks;
+extern long block_size; /* "long" because popt can't set an int32. */
+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 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;
+#ifdef ICONV_OPTION
+extern int ic_ndx;
+#endif
+
+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 const char *solo_file = NULL;
+
+/* For calling delete_item() and delete_dir_contents(). */
+#define DEL_RECURSE (1<<1) /* recurse */
+#define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
+
+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, char *replace, 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 (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 (!replace && 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 (!replace)
+ 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;
+ }
+ }
+
+ check_ret:
+ if (replace && ret != DR_SUCCESS) {
+ rprintf(FERROR, "could not make way for new %s: %s\n",
+ replace, fbuf);
+ }
+ return ret;
+}
+
+/* The directory is about to be deleted: if DEL_RECURSE is given, delete all
+ * its contents, otherwise just checks for content. Returns DR_SUCCESS or
+ * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
+ * buffer is used for recursion, but returned unchanged.)
+ */
+static enum delret delete_dir_contents(char *fname, int flags)
+{
+ struct file_list *dirlist;
+ enum delret ret;
+ unsigned remainder;
+ void *save_filters;
+ int j, dlen;
+ char *p;
+
+ if (verbose > 3) {
+ rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
+ fname, flags);
+ }
+
+ dlen = strlen(fname);
+ save_filters = push_local_filters(fname, dlen);
+
+ non_perishable_cnt = 0;
+ dirlist = get_dirlist(fname, dlen, 0);
+ ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
+
+ if (!dirlist->count)
+ goto done;
+
+ if (!(flags & DEL_RECURSE)) {
+ ret = DR_NOT_EMPTY;
+ goto done;
+ }
+
+ p = fname + dlen;
+ if (dlen != 1 || *fname != '/')
+ *p++ = '/';
+ remainder = MAXPATHLEN - (p - fname);
+
+ /* We do our own recursion, so make delete_item() non-recursive. */
+ flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;