+@@ -906,8 +954,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;
+ }
+@@ -916,7 +964,11 @@ static void recv_generator(char *fname,
+ dry_run++;
+ }
+ if (itemizing && f_out != -1) {
+- itemize(file, ndx, statret, &st,
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && statret == 0)
++ get_acl(fname, &sx);
++#endif
++ itemize(file, ndx, statret, &sx,
+ statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
+ }
+ if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
+@@ -928,19 +980,19 @@ static void recv_generator(char *fname,
+ full_fname(fname));
+ }
+ }
+- if (set_file_attrs(fname, file, statret ? NULL : &st, 0)
++ if (set_file_attrs(fname, file, statret ? NULL : &sx, 0)
+ && verbose && code && 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;
+
+ if (preserve_links && S_ISLNK(file->mode)) {
+ #ifdef SUPPORT_LINKS
+@@ -958,7 +1010,7 @@ static void recv_generator(char *fname,
+ char lnk[MAXPATHLEN];
+ int len;
+
+- if (!S_ISDIR(st.st_mode)
++ if (!S_ISDIR(sx.st.st_mode)
+ && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
+ lnk[len] = 0;
+ /* A link already pointing to the
+@@ -966,10 +1018,10 @@ static void recv_generator(char *fname,
+ * required. */
+ if (strcmp(lnk, file->u.link) == 0) {
+ if (itemizing) {
+- itemize(file, ndx, 0, &st, 0,
++ itemize(file, ndx, 0, &sx, 0,
+ 0, NULL);
+ }
+- set_file_attrs(fname, file, &st,
++ set_file_attrs(fname, file, &sx,
+ maybe_ATTRS_REPORT);
+ if (preserve_hard_links
+ && file->link_u.links) {
+@@ -982,9 +1034,9 @@ static void recv_generator(char *fname,
+ }
+ /* Not the right symlink (or not a symlink), so
+ * delete it. */
+- if (delete_item(fname, st.st_mode, del_opts) < 0)
++ if (delete_item(fname, sx.st.st_mode, del_opts) < 0)
+ return;
+- if (!S_ISLNK(st.st_mode))
++ if (!S_ISLNK(sx.st.st_mode))
+ statret = -1;
+ } else if (basis_dir[0] != NULL) {
+ if (try_dests_non(file, fname, ndx, itemizing,
+@@ -1000,7 +1052,7 @@ static void recv_generator(char *fname,
+ }
+ }
+ if (preserve_hard_links && file->link_u.links
+- && hard_link_check(file, ndx, fname, -1, &st,
++ && hard_link_check(file, ndx, fname, -1, &sx,
+ itemizing, code, HL_SKIP))
+ return;
+ if (do_symlink(file->u.link,fname) != 0) {
+@@ -1009,7 +1061,7 @@ static void recv_generator(char *fname,
+ } else {
+ set_file_attrs(fname, file, NULL, 0);
+ if (itemizing) {
+- itemize(file, ndx, statret, &st,
++ itemize(file, ndx, statret, &sx,
+ ITEM_LOCAL_CHANGE, 0, NULL);
+ }
+ if (code && verbose) {
+@@ -1043,18 +1095,22 @@ static void recv_generator(char *fname,
+ itemizing = code = 0;
+ }
+ }
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && statret == 0)
++ get_acl(fname, &sx);
++#endif
+ if (statret != 0
+- || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
+- || st.st_rdev != file->u.rdev) {
++ || (sx.st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
++ || sx.st.st_rdev != file->u.rdev) {
+ if (statret == 0
+- && delete_item(fname, st.st_mode, del_opts) < 0)
+- return;
++ && delete_item(fname, sx.st.st_mode, del_opts) < 0)
++ goto cleanup;
+ if (preserve_hard_links && file->link_u.links
+- && hard_link_check(file, ndx, fname, -1, &st,
++ && hard_link_check(file, ndx, fname, -1, &sx,
+ itemizing, code, HL_SKIP))
+- return;
+- if ((IS_DEVICE(file->mode) && !IS_DEVICE(st.st_mode))
+- || (IS_SPECIAL(file->mode) && !IS_SPECIAL(st.st_mode)))
++ goto cleanup;
++ if ((IS_DEVICE(file->mode) && !IS_DEVICE(sx.st.st_mode))
++ || (IS_SPECIAL(file->mode) && !IS_SPECIAL(sx.st.st_mode)))
+ statret = -1;
+ if (verbose > 2) {
+ rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
+@@ -1067,7 +1123,7 @@ static void recv_generator(char *fname,
+ } else {
+ set_file_attrs(fname, file, NULL, 0);
+ if (itemizing) {
+- itemize(file, ndx, statret, &st,
++ itemize(file, ndx, statret, &sx,
+ ITEM_LOCAL_CHANGE, 0, NULL);
+ }
+ if (code && verbose)
+@@ -1079,12 +1135,12 @@ static void recv_generator(char *fname,
+ }
+ } else {
+ if (itemizing)
+- itemize(file, ndx, statret, &st, 0, 0, NULL);
+- set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
++ itemize(file, ndx, statret, &sx, 0, 0, NULL);
++ set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
+ if (preserve_hard_links && file->link_u.links)
+ hard_link_cluster(file, ndx, itemizing, code);
+ }
+- return;
++ goto cleanup;
+ }
+
+ if (!S_ISREG(file->mode)) {
+@@ -1118,7 +1174,7 @@ static void recv_generator(char *fname,
+ }
+
+ if (update_only && statret == 0
+- && cmp_time(st.st_mtime, file->modtime) > 0) {
++ && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
+ if (verbose > 1)
+ rprintf(FINFO, "%s is newer\n", fname);
+ return;
+@@ -1127,18 +1183,18 @@ static void recv_generator(char *fname,
+ fnamecmp = fname;
+ fnamecmp_type = FNAMECMP_FNAME;
+
+- if (statret == 0 && !S_ISREG(st.st_mode)) {
+- if (delete_item(fname, st.st_mode, del_opts) != 0)
++ if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
++ if (delete_item(fname, sx.st.st_mode, del_opts) != 0)
+ return;
+ statret = -1;
+ stat_errno = ENOENT;
+ }
+
+ if (statret != 0 && basis_dir[0] != NULL) {
+- int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
++ int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
+ itemizing, maybe_ATTRS_REPORT, code);
+ if (j == -2)
+- return;
++ goto cleanup;
+ if (j != -1) {
+ fnamecmp = fnamecmpbuf;
+ fnamecmp_type = j;
+@@ -1147,7 +1203,7 @@ static void recv_generator(char *fname,
+ }
+
+ real_ret = statret;
+- real_st = st;
++ real_sx = sx;
+
+ if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
+ && link_stat(partialptr, &partial_st, 0) == 0
+@@ -1166,7 +1222,7 @@ static void recv_generator(char *fname,
+ rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
+ fname, fnamecmpbuf);
+ }
+- st.st_size = fuzzy_file->length;
++ sx.st.st_size = fuzzy_file->length;
+ statret = 0;
+ fnamecmp = fnamecmpbuf;
+ fnamecmp_type = FNAMECMP_FUZZY;
+@@ -1175,7 +1231,7 @@ static void recv_generator(char *fname,
+
+ if (statret != 0) {
+ 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_SKIP))
+ return;
+ if (stat_errno == ENOENT)
+@@ -1185,31 +1241,44 @@ static void recv_generator(char *fname,
+ return;
+ }
+
+- if (append_mode && st.st_size > file->length)
++ if (append_mode && sx.st.st_size > file->length)
+ return;
+
+ if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
+ ;
+ else if (fnamecmp_type == FNAMECMP_FUZZY)
+ ;
+- else if (unchanged_file(fnamecmp, file, &st)) {
++ else if (unchanged_file(fnamecmp, file, &sx.st)) {
+ if (partialptr) {
+ do_unlink(partialptr);
+ handle_partial_dir(partialptr, PDIR_DELETE);
+ }
+ if (itemizing) {
+- itemize(file, ndx, real_ret, &real_st,
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && real_ret == 0)
++ get_acl(fname, &real_sx);
++#endif
++ itemize(file, ndx, real_ret, &real_sx,
+ 0, 0, NULL);
++#ifdef SUPPORT_ACLS
++ if (preserve_acls) {
++ if (fnamecmp_type == FNAMECMP_FNAME) {
++ sx.acc_acl = real_sx.acc_acl;
++ sx.def_acl = real_sx.def_acl;
++ } else
++ free_acl(&real_sx);
++ }
++#endif
+ }
+- set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
++ set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
+ if (preserve_hard_links && file->link_u.links)
+ hard_link_cluster(file, ndx, itemizing, code);
+- return;
++ goto cleanup;
+ }
+
+ prepare_to_open:
+ if (partialptr) {
+- st = partial_st;
++ sx.st = partial_st;
+ fnamecmp = partialptr;
+ fnamecmp_type = FNAMECMP_PARTIAL_DIR;
+ statret = 0;
+@@ -1233,17 +1302,21 @@ static void recv_generator(char *fname,
+ pretend_missing:
+ /* pretend the file didn't exist */
+ 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_SKIP))
+- return;
++ goto cleanup;
+ statret = real_ret = -1;
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && ACL_READY(sx))
++ free_acl(&sx);
++#endif
+ goto notify_others;
+ }
+
+ if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
+ if (!(backupptr = get_backup_name(fname))) {
+ close(fd);
+- return;
++ goto cleanup;
+ }
+ if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
+ close(fd);
+@@ -1254,7 +1327,7 @@ static void recv_generator(char *fname,
+ full_fname(backupptr));
+ free(back_file);
+ close(fd);
+- return;
++ goto cleanup;
+ }
+ if ((f_copy = do_open(backupptr,
+ O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
+@@ -1262,14 +1335,14 @@ static void recv_generator(char *fname,
+ full_fname(backupptr));
+ free(back_file);
+ close(fd);
+- return;
++ goto cleanup;
+ }
+ fnamecmp_type = FNAMECMP_BACKUP;
+ }
+
+ if (verbose > 3) {
+ rprintf(FINFO, "gen mapped %s of size %.0f\n",
+- fnamecmp, (double)st.st_size);
++ fnamecmp, (double)sx.st.st_size);
+ }
+
+ if (verbose > 2)
+@@ -1287,24 +1360,32 @@ static void recv_generator(char *fname,
+ iflags |= ITEM_BASIS_TYPE_FOLLOWS;
+ if (fnamecmp_type == FNAMECMP_FUZZY)
+ iflags |= ITEM_XNAME_FOLLOWS;
+- itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
++#ifdef SUPPORT_ACLS
++ if (preserve_acls && real_ret == 0)
++ get_acl(fname, &real_sx);
++#endif
++ itemize(file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
+ fuzzy_file ? fuzzy_file->basename : NULL);
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ free_acl(&real_sx);
++#endif
+ }
+
+ if (!do_xfers) {
+ if (preserve_hard_links && file->link_u.links)
+ hard_link_cluster(file, ndx, itemizing, code);
+- return;
++ goto cleanup;
+ }
+ if (read_batch)
+- return;
++ goto cleanup;
+
+ if (statret != 0 || whole_file) {
+ write_sum_head(f_out, NULL);
+- return;
++ goto cleanup;
+ }
+
+- generate_and_send_sums(fd, st.st_size, f_out, f_copy);
++ generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
+
+ if (f_copy >= 0) {
+ close(f_copy);
+@@ -1317,6 +1398,13 @@ static void recv_generator(char *fname,
+ }
+
+ close(fd);
++
++ cleanup:
++#ifdef SUPPORT_ACLS
++ if (preserve_acls)
++ free_acl(&sx);
++#endif
++ return;
+ }
+
+ void generate_files(int f_out, struct file_list *flist, char *local_name)
+@@ -1376,6 +1464,8 @@ void generate_files(int f_out, struct fi