Added a description arg to check_exclude().
[rsync.git] / util.c
diff --git a/util.c b/util.c
index fd01283a5f5625b2ad14a4a389dc333aeb612948..fd6300992c2ef3fb88e4c6f50645606db9daf7b3 100644 (file)
--- a/util.c
+++ b/util.c
@@ -353,18 +353,33 @@ int robust_unlink(char *fname)
 #endif
 }
 
-int robust_rename(char *from, char *to)
+/* Returns 0 on success, -1 on most errors, and -2 if we got an error
+ * trying to copy the file across file systems. */
+int robust_rename(char *from, char *to, int mode)
 {
-#ifndef ETXTBSY
-       return do_rename(from, to);
-#else
-       int rc = do_rename(from, to);
-       if (rc == 0 || errno != ETXTBSY)
-               return rc;
-       if (robust_unlink(to) != 0)
-               return -1;
-       return do_rename(from, to);
+       int tries = 4;
+
+       while (tries--) {
+               if (do_rename(from, to) == 0)
+                       return 0;
+
+               switch (errno) {
+#ifdef ETXTBSY
+               case ETXTBSY:
+                       if (robust_unlink(to) != 0)
+                               return -1;
+                       break;
 #endif
+               case EXDEV:
+                       if (copy_file(from, to, mode) != 0)
+                               return -2;
+                       do_unlink(from);
+                       return 0;
+               default:
+                       return -1;
+               }
+       }
+       return -1;
 }
 
 
@@ -461,7 +476,8 @@ static int exclude_server_path(char *arg)
        if (server_exclude_list) {
                for (s = arg; (s = strchr(s, '/')) != NULL; ) {
                        *s = '\0';
-                       if (check_exclude(server_exclude_list, arg, 1)) {
+                       if (check_exclude(server_exclude_list, arg, 1,
+                           "server pattern")) {
                                /* We must leave arg truncated! */
                                return 1;
                        }
@@ -760,6 +776,34 @@ void sanitize_path(char *p, char *reldir)
        *sanp = '\0';
 }
 
+/* Works much like sanitize_path(), with these differences:  (1) a new buffer
+ * is allocated for the sanitized path rather than modifying it in-place; (2)
+ * a leading slash gets transformed into the rootdir value (which can be empty
+ * or NULL if you just want the slash to get dropped); (3) no "reldir" can be
+ * specified. */
+char *alloc_sanitize_path(const char *path, const char *rootdir)
+{
+       char *buf;
+       int rlen, plen = strlen(path);
+
+       if (*path == '/' && rootdir)
+               rlen = strlen(rootdir);
+       else
+               rlen = 0;
+       if (!(buf = new_array(char, rlen + plen + 1)))
+               out_of_memory("alloc_sanitize_path");
+       if (rlen)
+               memcpy(buf, rootdir, rlen);
+       memcpy(buf + rlen, path, plen + 1);
+
+       if (rlen)
+               rlen++;
+       sanitize_path(buf + rlen, NULL);
+       if (rlen && buf[rlen] == '.' && buf[rlen+1] == '\0')
+               buf[rlen-1] = '\0';
+
+       return buf;
+}
 
 char curr_dir[MAXPATHLEN];
 unsigned int curr_dir_len;