+static int read_delay_line(char *buf, int *flags_p)
+{
+ 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 (*bp == '!') {
+ bp++;
+ *flags_p = DEL_NO_UID_WRITE;
+ } else
+ *flags_p = 0;
+
+ 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, flags;
+
+ if (deldelay_fd >= 0) {
+ if (deldelay_cnt && !flush_delete_delay())
+ return;
+ lseek(deldelay_fd, 0, 0);
+ }
+ while ((mode = read_delay_line(delbuf, &flags)) >= 0)
+ delete_item(delbuf, mode, flags | 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;
+ int save_uid_ndx = uid_ndx;
+
+ if (!fbuf) {
+ change_local_filter_dir(NULL, 0, 0);
+ return;
+ }
+
+ if (DEBUG_GTE(DEL, 2))
+ rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
+
+ if (allowed_lull)
+ maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
+
+ 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;
+ }
+
+ if (!uid_ndx)
+ uid_ndx = ++file_extra_cnt;
+
+ dirlist = get_dirlist(fbuf, dlen, 0);
+
+ /* If an item in dirlist is not found in flist, delete it
+ * from the filesystem. */
+ for (i = dirlist->used; i--; ) {
+ struct file_struct *fp = dirlist->files[i];
+ if (!F_IS_ACTIVE(fp))
+ continue;
+ if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
+ if (INFO_GTE(MOUNT, 1))
+ rprintf(FINFO, "cannot delete mount point: %s\n",
+ f_name(fp, NULL));
+ continue;
+ }
+ /* Here we want to match regardless of file type. Replacement
+ * of a file with one of another type is handled separately by
+ * a delete_item call with a DEL_MAKE_ROOM flag. */
+ if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
+ int flags = DEL_RECURSE;
+ if (!(fp->mode & S_IWUSR) && !am_root && (uid_t)F_OWNER(fp) == our_uid)
+ flags |= DEL_NO_UID_WRITE;
+ f_name(fp, delbuf);
+ if (delete_during == 2) {
+ if (!remember_delete(fp, delbuf, flags))
+ break;
+ } else
+ delete_item(delbuf, fp->mode, flags);
+ }
+ }
+
+ flist_free(dirlist);
+
+ if (!save_uid_ndx) {
+ --file_extra_cnt;
+ uid_ndx = 0;
+ }
+}
+
+/* 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->used; j++) {
+ struct file_struct *file = cur_flist->sorted[j];
+
+ f_name(file, fbuf);
+
+ if (!(file->flags & FLAG_CONTENT_DIR)) {
+ change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
+ continue;
+ }
+
+ if (DEBUG_GTE(DEL, 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 (INFO_GTE(FLIST, 2) && !am_server)
+ rprintf(FINFO, " \r");
+}
+
+static inline int time_differs(struct file_struct *file, stat_x *sxp)
+{
+ return cmp_time(sxp->st.st_mtime, file->modtime);
+}
+
+static inline int perms_differ(struct file_struct *file, stat_x *sxp)
+{
+ if (preserve_perms)
+ return !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS);
+
+ if (preserve_executability)
+ return (sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0);
+
+ return 0;
+}
+
+static inline int ownership_differs(struct file_struct *file, stat_x *sxp)
+{
+ if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
+ return 1;
+
+ if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
+ return 1;
+
+ return 0;
+}
+
+#ifdef SUPPORT_ACLS
+static inline int acls_differ(const char *fname, struct file_struct *file, stat_x *sxp)
+{
+ if (preserve_acls) {
+ if (!ACL_READY(*sxp))
+ get_acl(fname, sxp);
+ if (set_acl(NULL, file, sxp, file->mode))
+ return 1;
+ }
+
+ return 0;
+}
+#endif
+
+#ifdef SUPPORT_XATTRS
+static inline int xattrs_differ(const char *fname, struct file_struct *file, stat_x *sxp)
+{
+ if (preserve_xattrs) {
+ if (!XATTR_READY(*sxp))
+ get_xattr(fname, sxp);
+ if (xattr_diff(file, sxp, 0))
+ return 1;
+ }
+
+ return 0;
+}
+#endif
+
+int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
+{
+ if (S_ISLNK(file->mode)) {
+#ifdef CAN_SET_SYMLINK_TIMES
+ if (preserve_times & PRESERVE_LINK_TIMES && time_differs(file, sxp))
+ return 0;
+#endif
+#ifdef CAN_CHMOD_SYMLINK
+ if (perms_differ(file, sxp))
+ return 0;
+#endif
+#ifndef CAN_CHOWN_SYMLINK
+ if (ownership_differs(file, sxp))
+ return 0;
+#endif
+#if defined SUPPORT_ACLS && 0 /* no current symlink-ACL support */
+ if (acls_differ(fname, file, sxp))
+ return 0;
+#endif
+#if defined SUPPORT_XATTRS && !defined NO_SYMLINK_XATTRS
+ if (xattrs_differ(fname, file, sxp))
+ return 0;
+#endif
+ } else {
+ if (preserve_times && time_differs(file, sxp))
+ return 0;
+ if (perms_differ(file, sxp))
+ return 0;
+ if (ownership_differs(file, sxp))
+ return 0;
+#ifdef SUPPORT_ACLS
+ if (acls_differ(fname, file, sxp))
+ return 0;
+#endif
+#ifdef SUPPORT_XATTRS
+ if (xattrs_differ(fname, file, sxp))
+ return 0;
+#endif
+ }
+
+ return 1;
+}
+
+void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
+ stat_x *sxp, int32 iflags, uchar fnamecmp_type,
+ const char *xname)
+{
+ if (statret >= 0) { /* A from-dest-dir statret can == 1! */
+ int keep_time = !preserve_times ? 0
+ : S_ISDIR(file->mode) ? preserve_times & PRESERVE_DIR_TIMES
+ : S_ISLNK(file->mode) ? preserve_times & PRESERVE_LINK_TIMES
+ : 1;
+
+ if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
+ iflags |= ITEM_REPORT_SIZE;
+ if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
+ if (iflags & ITEM_LOCAL_CHANGE)
+ iflags |= symlink_timeset_failed_flags;
+ } else if (keep_time
+ ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
+ : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
+ && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
+ iflags |= ITEM_REPORT_TIME;
+#if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
+ if (S_ISLNK(file->mode)) {
+ ;
+ } else
+#endif
+ if (preserve_perms) {
+ if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
+ iflags |= ITEM_REPORT_PERMS;
+ } else if (preserve_executability
+ && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
+ iflags |= ITEM_REPORT_PERMS;
+ if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
+ iflags |= ITEM_REPORT_OWNER;
+ if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
+ && sxp->st.st_gid != (gid_t)F_GROUP(file))
+ iflags |= ITEM_REPORT_GROUP;
+#ifdef SUPPORT_ACLS
+ if (preserve_acls && !S_ISLNK(file->mode)) {
+ if (!ACL_READY(*sxp))
+ get_acl(fnamecmp, sxp);
+ if (set_acl(NULL, file, sxp, file->mode))
+ iflags |= ITEM_REPORT_ACL;
+ }
+#endif
+#ifdef SUPPORT_XATTRS
+ if (preserve_xattrs) {
+ if (!XATTR_READY(*sxp))
+ get_xattr(fnamecmp, sxp);
+ if (xattr_diff(file, sxp, 1))
+ iflags |= ITEM_REPORT_XATTR;
+ }
+#endif
+ } else {
+#ifdef SUPPORT_XATTRS
+ if (preserve_xattrs && xattr_diff(file, NULL, 1))
+ iflags |= ITEM_REPORT_XATTR;
+#endif
+ iflags |= ITEM_IS_NEW;
+ }
+
+ iflags &= 0xffff;
+ if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || INFO_GTE(NAME, 2)
+ || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
+ if (protocol_version >= 29) {
+ if (ndx >= 0)
+ write_ndx(sock_f_out, ndx);
+ write_shortint(sock_f_out, iflags);
+ if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
+ write_byte(sock_f_out, fnamecmp_type);
+ if (iflags & ITEM_XNAME_FOLLOWS)
+ write_vstring(sock_f_out, xname, strlen(xname));
+#ifdef SUPPORT_XATTRS
+ if (preserve_xattrs && do_xfers
+ && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
+ send_xattr_request(NULL, file,
+ iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
+ }
+#endif
+ } else if (ndx >= 0) {
+ enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
+ log_item(code, file, iflags, xname);
+ }
+ }
+}
+
+