+--- old/flist.c
++++ new/flist.c
+@@ -40,6 +40,7 @@ extern int filesfrom_fd;
+ extern int one_file_system;
+ extern int copy_dirlinks;
+ extern int keep_dirlinks;
++extern int preserve_acls;
+ extern int preserve_links;
+ extern int preserve_hard_links;
+ extern int preserve_devices;
+@@ -133,6 +134,8 @@ static void list_file_entry(struct file_
+
+ permstring(permbuf, f->mode);
+
++ /* TODO: indicate '+' if the entry has an ACL. */
++
+ #ifdef SUPPORT_LINKS
+ if (preserve_links && S_ISLNK(f->mode)) {
+ rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
+@@ -491,6 +494,9 @@ static struct file_struct *receive_file_
+ char thisname[MAXPATHLEN];
+ unsigned int l1 = 0, l2 = 0;
+ int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
++#ifdef SUPPORT_ACLS
++ int xtra_len;
++#endif
+ OFF_T file_length;
+ char *basename, *dirname, *bp;
+ struct file_struct *file;
+@@ -594,13 +600,27 @@ static struct file_struct *receive_file_
+
+ sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0;
+
++#ifdef SUPPORT_ACLS
++ /* We need one or two index int32s when we're preserving ACLs. */
++ if (preserve_acls)
++ xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
++ else
++ xtra_len = 0;
++#endif
++
+ alloc_len = file_struct_len + dirname_len + basename_len
++#ifdef SUPPORT_ACLS
++ + xtra_len
++#endif
+ + linkname_len + sum_len;
+ bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry");
+
+ file = (struct file_struct *)bp;
+ memset(bp, 0, file_struct_len);
+ bp += file_struct_len;
++#ifdef SUPPORT_ACLS
++ bp += xtra_len;
++#endif
+
+ file->modtime = modtime;
+ file->length = file_length;
+@@ -695,6 +715,11 @@ static struct file_struct *receive_file_
+ read_buf(f, sum, checksum_len);
+ }
+
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ receive_acl(file, f);
++#endif
++
+ return file;
+ }
+
+@@ -944,6 +969,9 @@ static struct file_struct *send_file_nam
+ unsigned short flags)
+ {
+ struct file_struct *file;
++#ifdef SUPPORT_ACLS
++ statx sx;
++#endif
+
+ file = make_file(fname, flist, stp, flags,
+ f == -2 ? SERVER_FILTERS : ALL_FILTERS);
+@@ -953,6 +981,15 @@ static struct file_struct *send_file_nam
+ if (chmod_modes && !S_ISLNK(file->mode))
+ file->mode = tweak_mode(file->mode, chmod_modes);
+
++#ifdef SUPPORT_ACLS
++ if (preserve_acls) {
++ sx.st.st_mode = file->mode;
++ sx.acc_acl = sx.def_acl = NULL;
++ if (get_acl(fname, &sx) < 0)
++ return NULL;
++ }
++#endif
++
+ maybe_emit_filelist_progress(flist->count + flist_count_offset);
+
+ flist_expand(flist);
+@@ -960,6 +997,15 @@ static struct file_struct *send_file_nam
+ if (file->basename[0]) {
+ flist->files[flist->count++] = file;
+ send_file_entry(file, f);
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ send_acl(&sx, f);
++#endif
++ } else {
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ free_acl(&sx);
++#endif
+ }
+ return file;
+ }
+--- old/generator.c
++++ new/generator.c
+@@ -35,6 +35,7 @@ extern int do_progress;
+ extern int relative_paths;
+ extern int implied_dirs;
+ extern int keep_dirlinks;
++extern int preserve_acls;
+ extern int preserve_links;
+ extern int preserve_devices;
+ extern int preserve_specials;
+@@ -85,6 +86,7 @@ extern long block_size; /* "long" becaus
+ extern int max_delete;
+ extern int force_delete;
+ extern int one_file_system;
++extern mode_t orig_umask;
+ extern struct stats stats;
+ extern dev_t filesystem_dev;
+ extern char *backup_dir;
+@@ -317,22 +319,27 @@ static void do_delete_pass(struct file_l
+ rprintf(FINFO, " \r");
+ }
+
+-int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
++int unchanged_attrs(struct file_struct *file, statx *sxp)
+ {
+ if (preserve_perms
+- && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
++ && (sxp->st.st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
+ return 0;
+
+- if (am_root && preserve_uid && st->st_uid != file->uid)
++ if (am_root && preserve_uid && sxp->st.st_uid != file->uid)
+ return 0;
+
+- if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
++ if (preserve_gid && file->gid != GID_NONE && sxp->st.st_gid != file->gid)
+ return 0;
+
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && set_acl(NULL, file, sxp) == 0)
++ return 0;
++#endif
++
+ return 1;
+ }
+
+-void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
++void itemize(struct file_struct *file, int ndx, int statret, statx *sxp,
+ int32 iflags, uchar fnamecmp_type, char *xname)
+ {
+ if (statret >= 0) { /* A from-dest-dir statret can == 1! */
+@@ -340,19 +347,23 @@ void itemize(struct file_struct *file, i
+ : S_ISDIR(file->mode) ? !omit_dir_times
+ : !S_ISLNK(file->mode);
+
+- if (S_ISREG(file->mode) && file->length != st->st_size)
++ if (S_ISREG(file->mode) && file->length != sxp->st.st_size)
+ iflags |= ITEM_REPORT_SIZE;
+ if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
+ && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
+- || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
++ || (keep_time && cmp_time(file->modtime, sxp->st.st_mtime) != 0))
+ iflags |= ITEM_REPORT_TIME;
+- if ((file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
++ if ((file->mode & CHMOD_BITS) != (sxp->st.st_mode & CHMOD_BITS))
+ iflags |= ITEM_REPORT_PERMS;
+- if (preserve_uid && am_root && file->uid != st->st_uid)
++ if (preserve_uid && am_root && file->uid != sxp->st.st_uid)
+ iflags |= ITEM_REPORT_OWNER;
+ if (preserve_gid && file->gid != GID_NONE
+- && st->st_gid != file->gid)
++ && sxp->st.st_gid != file->gid)
+ iflags |= ITEM_REPORT_GROUP;
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && set_acl(NULL, file, sxp) == 0)
++ iflags |= ITEM_REPORT_ACL;
++#endif
+ } else
+ iflags |= ITEM_IS_NEW;
+
+@@ -605,7 +616,7 @@ void check_for_finished_hlinks(int itemi
+ * handling the file, -1 if no dest-linking occurred, or a non-negative
+ * value if we found an alternate basis file. */
+ static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
+- char *cmpbuf, STRUCT_STAT *stp, int itemizing,
++ char *cmpbuf, statx *sxp, int itemizing,
+ int maybe_ATTRS_REPORT, enum logcode code)
+ {
+ int best_match = -1;
+@@ -614,7 +625,7 @@ static int try_dests_reg(struct file_str
+
+ do {
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+- if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
++ if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
+ continue;
+ switch (match_level) {
+ case 0:
+@@ -622,16 +633,20 @@ static int try_dests_reg(struct file_str
+ match_level = 1;
+ /* FALL THROUGH */
+ case 1:
+- if (!unchanged_file(cmpbuf, file, stp))
++ if (!unchanged_file(cmpbuf, file, &sxp->st))
+ continue;
+ best_match = j;
+ match_level = 2;
+ /* FALL THROUGH */
+ case 2:
+- if (!unchanged_attrs(file, stp))
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ get_acl(cmpbuf, sxp);
++#endif
++ if (!unchanged_attrs(file, sxp))
+ continue;
+ if (always_checksum && preserve_times
+- && cmp_time(stp->st_mtime, file->modtime))
++ && cmp_time(sxp->st.st_mtime, file->modtime))
+ continue;
+ best_match = j;
+ match_level = 3;
+@@ -646,14 +661,14 @@ static int try_dests_reg(struct file_str
+ if (j != best_match) {
+ j = best_match;
+ pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
+- if (link_stat(cmpbuf, stp, 0) < 0)
++ if (link_stat(cmpbuf, &sxp->st, 0) < 0)
+ match_level = 0;
+ }
+
+ if (match_level == 3 && !copy_dest) {
+ #ifdef SUPPORT_HARD_LINKS
+ if (link_dest) {
+- if (hard_link_one(file, ndx, fname, 0, stp,
++ if (hard_link_one(file, ndx, fname, 0, sxp,
+ cmpbuf, 1,
+ itemizing && verbose > 1,
+ code) < 0)
+@@ -665,8 +680,13 @@ static int try_dests_reg(struct file_str
+ }
+ } else
+ #endif
+- if (itemizing)
+- itemize(file, ndx, 0, stp, 0, 0, NULL);
++ if (itemizing) {
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && !ACL_READY(*sxp))
++ get_acl(fname, sxp);
++#endif
++ itemize(file, ndx, 0, sxp, 0, 0, NULL);
++ }
+ if (verbose > 1 && maybe_ATTRS_REPORT) {
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+ }
+@@ -682,8 +702,13 @@ static int try_dests_reg(struct file_str
+ }
+ return -1;
+ }
+- if (itemizing)
+- itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
++ if (itemizing) {
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && !ACL_READY(*sxp))
++ get_acl(fname, sxp);
++#endif
++ itemize(file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
++ }
+ set_file_attrs(fname, file, NULL, 0);
+ if (maybe_ATTRS_REPORT
+ && ((!itemizing && verbose && match_level == 2)
+@@ -707,13 +732,18 @@ static int try_dests_non(struct file_str
+ enum logcode code)
+ {
+ char fnamebuf[MAXPATHLEN];
+- STRUCT_STAT st;
++ statx sx;
+ int i = 0;
+
+ do {
+ pathjoin(fnamebuf, MAXPATHLEN, basis_dir[i], fname);
+- if (link_stat(fnamebuf, &st, 0) < 0 || S_ISDIR(st.st_mode)
+- || !unchanged_attrs(file, &st))
++ if (link_stat(fnamebuf, &sx.st, 0) < 0 || S_ISDIR(sx.st.st_mode))
++ continue;
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ get_acl(fnamebuf, &sx);
++#endif
++ if (!unchanged_attrs(file, &sx))
+ continue;
+ if (S_ISLNK(file->mode)) {
+ #ifdef SUPPORT_LINKS
+@@ -726,10 +756,10 @@ static int try_dests_non(struct file_str
+ #endif
+ continue;
+ } else if (IS_SPECIAL(file->mode)) {
+- if (!IS_SPECIAL(st.st_mode) || st.st_rdev != file->u.rdev)
++ if (!IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev != file->u.rdev)
+ continue;
+ } else if (IS_DEVICE(file->mode)) {
+- if (!IS_DEVICE(st.st_mode) || st.st_rdev != file->u.rdev)
++ if (!IS_DEVICE(sx.st.st_mode) || sx.st.st_rdev != file->u.rdev)
+ continue;
+ } else {
+ rprintf(FERROR,
+@@ -760,7 +790,15 @@ static int try_dests_non(struct file_str
+ int changes = compare_dest ? 0 : ITEM_LOCAL_CHANGE
+ + (link_dest ? ITEM_XNAME_FOLLOWS : 0);
+ char *lp = link_dest ? "" : NULL;
+- itemize(file, ndx, 0, &st, changes, 0, lp);
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ get_acl(fname, &sx);
++#endif
++ itemize(file, ndx, 0, &sx, changes, 0, lp);
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ free_acl(&sx);
++#endif
+ }
+ if (verbose > 1 && maybe_ATTRS_REPORT) {
+ rprintf(FCLIENT, "%s is uptodate\n", fname);
+@@ -772,6 +810,7 @@ static int try_dests_non(struct file_str
+ }
+
+ static int phase = 0;
++static int dflt_perms;
+
+ /* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
+ * make sure it exists, and has the right permissions/timestamp info. For
+@@ -793,7 +832,8 @@ static void recv_generator(char *fname,
+ static int need_fuzzy_dirlist = 0;
+ struct file_struct *fuzzy_file = NULL;
+ int fd = -1, f_copy = -1;
+- STRUCT_STAT st, real_st, partial_st;
++ statx sx, real_sx;
++ STRUCT_STAT partial_st;
+ struct file_struct *back_file = NULL;
+ int statret, real_ret, stat_errno;
+ char *fnamecmp, *partialptr, *backupptr = NULL;
+@@ -849,6 +889,9 @@ static void recv_generator(char *fname,
+ } else if (!dry_run)
+ return;
+ }
++#ifdef SUPPORT_ACLS
++ sx.acc_acl = sx.def_acl = NULL;
++#endif
+ if (dry_run > 1) {
+ statret = -1;
+ stat_errno = ENOENT;
+@@ -856,7 +899,7 @@ static void recv_generator(char *fname,
+ char *dn = file->dirname ? file->dirname : ".";
+ if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
+ if (relative_paths && !implied_dirs
+- && do_stat(dn, &st) < 0
++ && do_stat(dn, &sx.st) < 0
+ && create_directory_path(fname) < 0) {
+ rsyserr(FERROR, errno,
+ "recv_generator: mkdir %s failed",
+@@ -868,6 +911,10 @@ static void recv_generator(char *fname,
+ }
+ if (fuzzy_basis)
+ need_fuzzy_dirlist = 1;
++#ifdef SUPPORT_ACLS
++ if (!preserve_perms)
++ dflt_perms = default_perms_for_dir(dn);
++#endif
+ }
+ parent_dirname = dn;
+
+@@ -876,7 +923,7 @@ static void recv_generator(char *fname,
+ need_fuzzy_dirlist = 0;
+ }
+
+- statret = link_stat(fname, &st,
++ statret = link_stat(fname, &sx.st,
+ keep_dirlinks && S_ISDIR(file->mode));
+ stat_errno = errno;
+ }
+@@ -894,8 +941,9 @@ static void recv_generator(char *fname,
+ * mode based on the local permissions and some heuristics. */
+ if (!preserve_perms) {
+ int exists = statret == 0
+- && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
+- file->mode = dest_mode(file->mode, st.st_mode, exists);
++ && S_ISDIR(sx.st.st_mode) == S_ISDIR(file->mode);
++ file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
++ exists);
+ }
+
+ if (S_ISDIR(file->mode)) {
+@@ -904,8 +952,8 @@ static void recv_generator(char *fname,
+ * file of that name and it is *not* a directory, then
+ * we need to delete it. If it doesn't exist, then
+ * (perhaps recursively) create it. */
+- if (statret == 0 && !S_ISDIR(st.st_mode)) {
+- if (delete_item(fname, st.st_mode, del_opts) < 0)
++ if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
++ if (delete_item(fname, sx.st.st_mode, del_opts) < 0)
+ return;
+ statret = -1;
+ }
+@@ -920,7 +968,11 @@ static void recv_generator(char *fname,
+ sr = -1;
+ new_root_dir = 0;
+ }
+- itemize(file, ndx, sr, &st,
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && sr == 0)
++ get_acl(fname, &sx);
++#endif
++ itemize(file, ndx, sr, &sx,
+ sr ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
+ }
+ if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
+@@ -940,19 +992,19 @@ static void recv_generator(char *fname,
+ return;
+ }
+ }
+- if (set_file_attrs(fname, file, statret ? NULL : &st, 0)
++ if (set_file_attrs(fname, file, statret ? NULL : &sx, 0)
+ && verbose && code != FNONE && f_out != -1)
+ rprintf(code, "%s/\n", fname);
+ if (delete_during && f_out != -1 && !phase && dry_run < 2
+ && (file->flags & FLAG_DEL_HERE))
+- delete_in_dir(the_file_list, fname, file, &st);
+- return;
++ delete_in_dir(the_file_list, fname, file, &sx.st);
++ goto cleanup;
+ }
+
+ if (preserve_hard_links && file->link_u.links
+- && hard_link_check(file, ndx, fname, statret, &st,
++ && hard_link_check(file, ndx, fname, statret, &sx,
+ itemizing, code, HL_CHECK_MASTER))
+- return;
++ goto cleanup;