+static int deletion_count = 0; /* used to implement --max-delete */
+static int can_link_symlinks = 1; /* start out optimistic */
+static int can_link_devices = 1;
+
+/* For calling delete_file() */
+#define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */
+#define DEL_TERSE (1<<3)
+
+
+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_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.)
+ */
+static int delete_item(char *fname, int mode, int flags)
+{
+ struct file_list *dirlist;
+ int j, dlen, zap_dir, ok;
+ unsigned remainder;
+ void *save_filters;
+ char *p;
+
+ if (!S_ISDIR(mode)) {
+ if (max_delete && ++deletion_count > max_delete)
+ return 0;
+ if (make_backups && (backup_dir || !is_backup_file(fname)))
+ ok = make_backup(fname);
+ else
+ ok = robust_unlink(fname) == 0;
+ if (ok) {
+ if (!(flags & DEL_TERSE))
+ log_delete(fname, mode);
+ return 0;
+ }
+ if (errno == ENOENT) {
+ deletion_count--;
+ return 0;
+ }
+ rsyserr(FERROR, errno, "delete_file: unlink %s failed",
+ full_fname(fname));
+ return -1;
+ }
+
+ zap_dir = flags & DEL_FORCE_RECURSE || force_delete;
+ if ((max_delete && ++deletion_count > max_delete)
+ || (dry_run && zap_dir)) {
+ ok = 0;
+ errno = ENOTEMPTY;
+ } else if (make_backups && !backup_dir && !is_backup_file(fname)
+ && !(flags & DEL_FORCE_RECURSE))
+ ok = make_backup(fname);
+ else
+ ok = do_rmdir(fname) == 0;
+ if (ok) {
+ if (!(flags & DEL_TERSE))
+ log_delete(fname, mode);
+ return 0;
+ }
+ if (errno == ENOENT) {
+ deletion_count--;
+ return 0;
+ }
+ if (!zap_dir) {
+ rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
+ full_fname(fname));
+ return -1;
+ }
+ flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
+ deletion_count--;
+
+ dlen = strlen(fname);
+ save_filters = push_local_filters(fname, dlen);
+
+ dirlist = get_dirlist(fname, dlen, 0);
+
+ p = fname + dlen;
+ if (dlen != 1 || *fname != '/')
+ *p++ = '/';
+ remainder = MAXPATHLEN - (p - fname);
+
+ for (j = dirlist->count; j--; ) {
+ struct file_struct *fp = dirlist->files[j];
+
+ if (fp->flags & FLAG_MOUNT_POINT)
+ continue;
+
+ strlcpy(p, fp->basename, remainder);
+ delete_item(fname, fp->mode, flags & ~DEL_TERSE);
+ }
+ flist_free(dirlist);
+
+ fname[dlen] = '\0';
+
+ pop_local_filters(save_filters);
+
+ if (max_delete && ++deletion_count > max_delete)
+ return 0;
+
+ if (do_rmdir(fname) == 0) {
+ if (!(flags & DEL_TERSE))
+ log_delete(fname, mode);
+ } else if (errno != ENOTEMPTY && errno != EEXIST && errno != ENOENT) {
+ rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
+ full_fname(fname));
+ return -1;
+ }
+
+ return 0;
+}
+
+
+/* This function is used to implement per-directory deletion, and is used by
+ * 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). */
+static void delete_in_dir(struct file_list *flist, char *fbuf,
+ struct file_struct *file, STRUCT_STAT *stp)
+{
+ 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];
+ int dlen, i;
+
+ if (!flist) {
+ while (cur_depth >= min_depth)
+ pop_local_filters(filt_array[cur_depth--]);
+ min_depth = MAXPATHLEN;
+ cur_depth = -1;
+ return;
+ }
+
+ if (verbose > 2)
+ rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
+
+ if (allowed_lull)
+ maybe_send_keepalive();
+
+ if (file->dir.depth >= MAXPATHLEN/2+1)
+ return; /* Impossible... */
+
+ if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
+ if (already_warned)
+ return;
+ rprintf(FINFO,
+ "IO error encountered -- skipping file deletion\n");
+ already_warned = 1;
+ return;
+ }
+
+ while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
+ pop_local_filters(filt_array[cur_depth--]);
+ cur_depth = file->dir.depth;
+ if (min_depth > cur_depth)
+ min_depth = cur_depth;
+ dlen = strlen(fbuf);
+ filt_array[cur_depth] = push_local_filters(fbuf, dlen);
+
+ if (one_file_system) {
+ if (file->flags & FLAG_TOP_DIR)
+ filesystem_dev = stp->st_dev;
+ else if (filesystem_dev != stp->st_dev)
+ return;
+ }
+
+ dirlist = get_dirlist(fbuf, dlen, 0);
+
+ /* 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 (flist_find(flist, fp) < 0) {
+ f_name(fp, delbuf);
+ delete_item(delbuf, fp->mode, DEL_FORCE_RECURSE);
+ }
+ }
+
+ flist_free(dirlist);
+}
+
+/* This deletes any files on the receiving side that are not present on the
+ * sending side. This is used by --delete-before and --delete-after. */
+static void do_delete_pass(struct file_list *flist)
+{
+ char fbuf[MAXPATHLEN];
+ STRUCT_STAT st;
+ int j;
+
+ if (dry_run > 1 /* destination doesn't exist yet */
+ || list_only)
+ return;
+
+ for (j = 0; j < flist->count; j++) {
+ struct file_struct *file = flist->files[j];
+
+ if (!(file->flags & FLAG_DEL_HERE))
+ continue;
+
+ f_name(file, fbuf);
+ if (verbose > 1 && file->flags & FLAG_TOP_DIR)
+ rprintf(FINFO, "deleting in %s\n", fbuf);
+
+ if (link_stat(fbuf, &st, keep_dirlinks) < 0
+ || !S_ISDIR(st.st_mode))
+ continue;
+
+ delete_in_dir(flist, fbuf, file, &st);
+ }
+ delete_in_dir(NULL, NULL, NULL, NULL);
+
+ if (do_progress && !am_server)
+ rprintf(FINFO, " \r");
+}
+
+int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)