- DIR *d;
- struct dirent *di;
- char buf[MAXPATHLEN];
- STRUCT_STAT st;
-
- if (!(flags & DEL_DIR)) {
- if (robust_unlink(fname) == 0) {
- if (verbose && !(flags & DEL_TERSE)) {
- rprintf(FINFO, "deleting %s\n",
- safe_fname(fname));
- }
- return 0;
- }
- if (errno == ENOENT)
- return 0;
- rsyserr(FERROR, errno, "delete_file: unlink %s failed",
- full_fname(fname));
- return -1;
- }
-
- if (do_rmdir(fname) == 0) {
- if (verbose && !(flags & DEL_TERSE)) {
- rprintf(FINFO, "deleting %s/\n",
- safe_fname(fname));
- }
- return 0;
- }
- if (errno == ENOENT)
- return 0;
- if ((flags & DEL_NO_RECURSE) || !force_delete || !recurse
- || (errno != ENOTEMPTY && errno != EEXIST)) {
- rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
- full_fname(fname));
- return -1;
- }
-
- /* now we do a recsursive delete on the directory ... */
- if (!(d = opendir(fname))) {
- rsyserr(FERROR, errno, "delete_file: opendir %s failed",
- full_fname(fname));
- return -1;
- }
-
- if (!(flags & DEL_TERSE)) {
- if (verbose)
- rprintf(FINFO, "deleting %s/\n", safe_fname(fname));
- flags |= DEL_TERSE;
- }
-
- for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
- char *dname = d_name(di);
- if (dname[0] == '.' && (dname[1] == '\0'
- || (dname[1] == '.' && dname[2] == '\0')))
- continue;
- pathjoin(buf, sizeof buf, fname, dname);
-
- if (do_lstat(buf, &st) < 0)
- continue;
- if (S_ISDIR(st.st_mode))
- flags |= DEL_DIR;
- else
- flags &= ~DEL_DIR;
-
- if (verbose)
- rprintf(FINFO, "deleting %s%s\n", safe_fname(buf),
- flags & DEL_DIR ? "/" : "");
- if (delete_file(buf, flags) != 0) {
- closedir(d);
- return -1;
+ /* If the file already exists, we'll return the local permissions,
+ * possibly tweaked by the --executability option. */
+ if (exists) {
+ if (preserve_executability && S_ISREG(flist_mode)) {
+ /* If the source file is executable, grant execute
+ * rights to everyone who can read, but ONLY if the
+ * file isn't already executable. */
+ if (!(flist_mode & 0111))
+ dest_mode &= ~0111;
+ else if (!(dest_mode & 0111))
+ dest_mode |= (dest_mode & 0444) >> 2;