+int ignore_perishable = 0;
+int non_perishable_cnt = 0;
+
+static int deletion_count = 0; /* used to implement --max-delete */
+static FILE *delete_delay_fp = 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 && (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;
+
+ for (j = dirlist->count; j--; ) {
+ struct file_struct *fp = dirlist->files[j];
+
+ if (fp->flags & FLAG_MOUNT_DIR) {
+ if (verbose > 1) {
+ rprintf(FINFO,
+ "mount point, %s, pins parent directory\n",
+ f_name(fp, NULL));
+ }
+ ret = DR_NOT_EMPTY;
+ continue;
+ }
+
+ strlcpy(p, fp->basename, remainder);
+ /* Save stack by recursing to ourself directly. */
+ if (S_ISDIR(fp->mode)
+ && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
+ ret = DR_NOT_EMPTY;
+ if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
+ ret = DR_NOT_EMPTY;
+ }
+
+ fname[dlen] = '\0';
+
+ done:
+ flist_free(dirlist);
+ pop_local_filters(save_filters);
+
+ if (ret == DR_NOT_EMPTY) {
+ rprintf(FINFO, "cannot delete non-empty directory: %s\n",
+ fname);
+ }
+ return ret;
+}
+
+static void start_delete_temp(void)
+{
+ char fnametmp[MAXPATHLEN];
+ int fd, save_dry_run = dry_run;
+
+ dry_run = 0;
+ if (!get_tmpname(fnametmp, "deldelay")
+ || (fd = do_mkstemp(fnametmp, 0600)) < 0
+ || !(delete_delay_fp = fdopen(fd, "w+"))) {
+ rprintf(FERROR, "Unable to create delete-delay temp file.\n");
+ exit_cleanup(RERR_FILEIO);
+ }
+ dry_run = save_dry_run;
+ unlink(fnametmp);
+}
+
+static int read_delay_line(FILE *fp, char *buf, int bsize)
+{
+ int ch, mode = 0;
+
+ if ((ch = fgetc(fp)) == EOF)
+ return -1;
+
+ while (1) {
+ if (ch == ' ')
+ break;
+ if (ch > '7' || ch < '0') {
+ rprintf(FERROR, "invalid data in delete-delay file.\n");
+ exit_cleanup(RERR_FILEIO);
+ }
+ mode = mode*8 + ch - '0';
+ if ((ch = fgetc(fp)) == EOF) {
+ unexpected_eof:
+ rprintf(FERROR, "unexpected EOF in delete-delay file.\n");
+ exit_cleanup(RERR_FILEIO);
+ }
+ }
+
+ while (1) {
+ if ((ch = fgetc(fp)) == EOF)
+ goto unexpected_eof;
+ if (bsize-- <= 0) {
+ rprintf(FERROR, "filename too long in delete-delay file.\n");
+ exit_cleanup(RERR_FILEIO);
+ }
+ *buf++ = (char)ch;
+ if (ch == '\0')
+ break;
+ }
+
+ return mode;
+}
+
+static void delayed_deletions(char *delbuf)
+{
+ int mode;
+
+ fseek(delete_delay_fp, 0, 0);
+ while ((mode = read_delay_line(delete_delay_fp, delbuf, MAXPATHLEN)) >= 0)
+ delete_item(delbuf, mode, NULL, DEL_RECURSE);
+ fclose(delete_delay_fp);
+}
+
+/* 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)
+ continue;
+ if (fp->flags & FLAG_MOUNT_DIR) {
+ if (verbose > 1)
+ rprintf(FINFO, "cannot delete mount point: %s\n",
+ f_name(fp, NULL));
+ continue;
+ }
+ if (flist_find(flist, fp) < 0) {
+ f_name(fp, delbuf);
+ if (delete_delay_fp)
+ fprintf(delete_delay_fp, "%o %s%c", (short)fp->mode, delbuf, '\0');
+ else
+ delete_item(delbuf, fp->mode, NULL, DEL_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;
+
+ /* dry_run is incremented when the destination doesn't exist yet. */
+ if (dry_run > 1 || list_only)
+ return;
+
+ for (j = 0; j < flist->count; j++) {
+ struct file_struct *file = flist->files[j];
+
+ if (!(file->flags & FLAG_XFER_DIR))
+ 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)