--- rsync.c 15 May 2004 19:31:10 -0000 1.139 +++ rsync.c 15 May 2004 20:17:41 -0000 @@ -236,6 +236,9 @@ void finish_transfer(char *fname, char * if (make_backups && !make_backup(fname)) return; + /* Change permissions before putting the file into place. */ + set_perms(fnametmp, file, NULL, 0); + /* move tmp file over real file */ ret = robust_rename(fnametmp, fname, file->mode & INITACCESSPERMS); if (ret < 0) { @@ -243,7 +246,8 @@ void finish_transfer(char *fname, char * ret == -2 ? "copy" : "rename", full_fname(fnametmp), fname); do_unlink(fnametmp); - } else { + } else if (ret == 1) { + /* The file got copied, so set the permissions again. */ set_perms(fname, file, NULL, ok_to_set_time ? 0 : PERMS_SKIP_MTIME); } --- t_stub.c 15 May 2004 19:31:05 -0000 1.8 +++ t_stub.c 15 May 2004 20:17:41 -0000 @@ -26,6 +26,7 @@ * functions, so that module test harnesses can run standalone. **/ +int am_root = 0; int modify_window = 0; int module_id = -1; struct exclude_list_struct server_exclude_list; --- util.c 15 May 2004 19:31:10 -0000 1.143 +++ util.c 15 May 2004 20:17:41 -0000 @@ -28,6 +28,7 @@ #include "rsync.h" extern int verbose; +extern int am_root; extern struct exclude_list_struct server_exclude_list; int sanitize_paths = 0; @@ -261,6 +262,8 @@ int copy_file(char *source, char *dest, return -1; } + if (!am_root && !(mode & S_IWUSR)) + mode |= S_IWUSR; ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode); if (ofd == -1) { rsyserr(FERROR, errno, "open %s", full_fname(dest)); @@ -359,8 +362,8 @@ int robust_unlink(char *fname) #endif } -/* Returns 0 on success, -1 on most errors, and -2 if we got an error - * trying to copy the file across file systems. */ +/* Returns 0 on successful rename, 1 if we successfully copied the file + * across filesystems, -2 if copy_file() failed, and -1 on other errors. */ int robust_rename(char *from, char *to, int mode) { int tries = 4; @@ -377,10 +380,12 @@ int robust_rename(char *from, char *to, break; #endif case EXDEV: + if (!am_root && !(mode & S_IRUSR)) + do_chmod(from, (mode & CHMOD_BITS) | S_IRUSR); if (copy_file(from, to, mode) != 0) return -2; do_unlink(from); - return 0; + return 1; default: return -1; }