]> git.ipfire.org Git - thirdparty/rsync.git/commitdiff
Eventually add write permission when setting extended attributes (#212)
authorTomasKorbar <tomas.korb@seznam.cz>
Sun, 26 Sep 2021 23:34:15 +0000 (01:34 +0200)
committerGitHub <noreply@github.com>
Sun, 26 Sep 2021 23:34:15 +0000 (16:34 -0700)
* 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 #208

Co-authored-by: Wayne Davison <wayne@opencoder.net>
xattrs.c

index 508649c000244b792b1a45ac55ebbbc398ea3635..3c5491920c917de9d5fa181d971ecacbf5daef52 100644 (file)
--- a/xattrs.c
+++ b/xattrs.c
@@ -1055,7 +1055,7 @@ int set_xattr(const char *fname, const struct file_struct *file, const char *fna
 {
        rsync_xa_list *glst = rsync_xal_l.items;
        item_list *lst;
-       int ndx;
+       int ndx, added_write_perm = 0;
 
        if (dry_run)
                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
        }
 #endif
 
+       /* If the target file lacks write permission, we try to add it
+        * temporarily so we can change the extended attributes. */
+       if (!am_root
+#ifdef SUPPORT_LINKS
+        && !S_ISLNK(sxp->st.st_mode)
+#endif
+        && access(fname, W_OK) < 0
+        && do_chmod(fname, (sxp->st.st_mode & CHMOD_BITS) | S_IWUSR) == 0)
+               added_write_perm = 1;
+
        ndx = F_XATTR(file);
        glst += ndx;
        lst = &glst->xa_items;
-       return rsync_xal_set(fname, lst, fnamecmp, sxp);
+       int return_value = rsync_xal_set(fname, lst, fnamecmp, sxp);
+       if (added_write_perm) /* remove the temporary write permission */
+               do_chmod(fname, sxp->st.st_mode);
+       return return_value;
 }
 
 #ifdef SUPPORT_ACLS