+static int read_delay_line(char *buf)
+{
+ static int read_pos = 0;
+ int j, len, mode;
+ char *bp, *past_space;
+
+ while (1) {
+ for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
+ if (j < deldelay_cnt)
+ break;
+ if (deldelay_fd < 0) {
+ if (j > read_pos)
+ goto invalid_data;
+ return -1;
+ }
+ deldelay_cnt -= read_pos;
+ if (deldelay_cnt == deldelay_size)
+ goto invalid_data;
+ if (deldelay_cnt && read_pos) {
+ memmove(deldelay_buf, deldelay_buf + read_pos,
+ deldelay_cnt);
+ }
+ len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
+ deldelay_size - deldelay_cnt);
+ if (len == 0) {
+ if (deldelay_cnt) {
+ rprintf(FERROR,
+ "ERROR: unexpected EOF in delete-delay file.\n");
+ }
+ return -1;
+ }
+ if (len < 0) {
+ rsyserr(FERROR, errno,
+ "reading delete-delay file");
+ return -1;
+ }
+ deldelay_cnt += len;
+ read_pos = 0;
+ }
+
+ bp = deldelay_buf + read_pos;
+
+ if (sscanf(bp, "%x ", &mode) != 1) {
+ invalid_data:
+ rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
+ return -1;
+ }
+ past_space = strchr(bp, ' ') + 1;
+ len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
+ read_pos = j + 1;
+
+ if (len > MAXPATHLEN) {
+ rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
+ return -1;
+ }
+
+ /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
+ * instead of returning a pointer to our buffer. */
+ memcpy(buf, past_space, len);
+
+ return mode;
+}
+
+static void do_delayed_deletions(char *delbuf)
+{
+ int mode;
+
+ if (deldelay_fd >= 0) {
+ if (deldelay_cnt && !flush_delete_delay())
+ return;
+ lseek(deldelay_fd, 0, 0);
+ }
+ while ((mode = read_delay_line(delbuf)) >= 0)
+ delete_item(delbuf, mode, NULL, DEL_RECURSE);
+ if (deldelay_fd >= 0)
+ close(deldelay_fd);
+}
+
+/* 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(char *fbuf, struct file_struct *file, dev_t *fs_dev)
+{
+ static int already_warned = 0;
+ struct file_list *dirlist;
+ char delbuf[MAXPATHLEN];
+ int dlen, i;
+
+ if (!fbuf) {
+ change_local_filter_dir(NULL, 0, 0);
+ return;
+ }
+
+ if (verbose > 2)
+ rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
+
+ if (allowed_lull)
+ maybe_send_keepalive();
+
+ if (io_error && !ignore_errors) {
+ if (already_warned)
+ return;
+ rprintf(FINFO,
+ "IO error encountered -- skipping file deletion\n");
+ already_warned = 1;
+ return;
+ }
+
+ dlen = strlen(fbuf);
+ change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
+
+ if (one_file_system) {
+ if (file->flags & FLAG_TOP_DIR)
+ filesystem_dev = *fs_dev;
+ else if (filesystem_dev != *fs_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 (!F_IS_ACTIVE(fp))
+ 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(cur_flist, fp) < 0) {
+ f_name(fp, delbuf);
+ if (delete_during == 2) {
+ if (!remember_delete(fp, delbuf))
+ break;
+ } 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(void)
+{
+ 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 < cur_flist->count; j++) {
+ struct file_struct *file = cur_flist->sorted[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(fbuf, file, &st.st_dev);
+ }
+ delete_in_dir(NULL, NULL, &dev_zero);
+
+ if (do_progress && !am_server)
+ rprintf(FINFO, " \r");
+}
+
+int unchanged_attrs(const char *fname, struct file_struct *file, statx *sxp)
+{
+ if (preserve_perms && !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
+ return 0;
+
+ if (am_root && preserve_uid && sxp->st.st_uid != (uid_t)F_OWNER(file))
+ return 0;
+
+ if (preserve_gid && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
+ return 0;