Skip to content

Commit b2e16fa

Browse files
TomasKorbarWayneD
andauthored
Eventually add write permission when setting extended attributes (RsyncProject#212)
* Eventually add write permission when setting extended attributes When we need to set extended atributes of file which does not allow write then temporarily add write permission and after attributes are set, remove it again. Resolves RsyncProject#208 Co-authored-by: Wayne Davison <wayne@opencoder.net>
1 parent 4fd662f commit b2e16fa

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

xattrs.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ int set_xattr(const char *fname, const struct file_struct *file, const char *fna
10551055
{
10561056
rsync_xa_list *glst = rsync_xal_l.items;
10571057
item_list *lst;
1058-
int ndx;
1058+
int ndx, added_write_perm = 0;
10591059

10601060
if (dry_run)
10611061
return 1; /* FIXME: --dry-run needs to compute this value */
@@ -1084,10 +1084,23 @@ int set_xattr(const char *fname, const struct file_struct *file, const char *fna
10841084
}
10851085
#endif
10861086

1087+
/* If the target file lacks write permission, we try to add it
1088+
* temporarily so we can change the extended attributes. */
1089+
if (!am_root
1090+
#ifdef SUPPORT_LINKS
1091+
&& !S_ISLNK(sxp->st.st_mode)
1092+
#endif
1093+
&& access(fname, W_OK) < 0
1094+
&& do_chmod(fname, (sxp->st.st_mode & CHMOD_BITS) | S_IWUSR) == 0)
1095+
added_write_perm = 1;
1096+
10871097
ndx = F_XATTR(file);
10881098
glst += ndx;
10891099
lst = &glst->xa_items;
1090-
return rsync_xal_set(fname, lst, fnamecmp, sxp);
1100+
int return_value = rsync_xal_set(fname, lst, fnamecmp, sxp);
1101+
if (added_write_perm) /* remove the temporary write permission */
1102+
do_chmod(fname, sxp->st.st_mode);
1103+
return return_value;
10911104
}
10921105

10931106
#ifdef SUPPORT_ACLS

0 commit comments

Comments
 (0)