Skip to content

Commit

Permalink
Eventually add write permission when setting extended attributes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TomasKorbar committed Aug 23, 2021
1 parent 592c6bc commit a6a72eb
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion xattrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "ifuncs.h"
#include "inums.h"
#include "lib/sysxattrs.h"
#include <unistd.h>
#include <sys/stat.h>

#ifdef SUPPORT_XATTRS

Expand Down Expand Up @@ -1056,6 +1058,8 @@ 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 added_write_perm = 0;
struct stat current_stat;

if (dry_run)
return 1; /* FIXME: --dry-run needs to compute this value */
Expand Down Expand Up @@ -1084,10 +1088,34 @@ int set_xattr(const char *fname, const struct file_struct *file, const char *fna
}
#endif

// if target file has not write permission
// then add it temporarily so we can change extended attributes
if (access(fname, W_OK) != 0) {
if (do_stat(fname, &current_stat) != 0) {
rsyserr(FERROR_XFER, errno, "set_xattr: was not able to access file %s",
full_fname(fname));
}
if (current_stat.st_uid == (uid_t)F_OWNER(file)) {
do_chmod(fname, current_stat.st_mode | S_IWUSR);
added_write_perm = 1;
} else if (current_stat.st_gid == (gid_t)F_GROUP(file)) {
do_chmod(fname, current_stat.st_mode | S_IWGRP);
added_write_perm = 1;
} else {
rprintf(FERROR_XFER, "set_xattr: file %s is not accessible for writing",
full_fname(fname));
}
}

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, current_stat.st_mode);
}
return return_value;
}

#ifdef SUPPORT_ACLS
Expand Down

0 comments on commit a6a72eb

Please sign in to comment.