Use the new sanitize_path() calling syntax.
[rsync/rsync.git] / util.c
diff --git a/util.c b/util.c
index 8d05771..528efd4 100644 (file)
--- a/util.c
+++ b/util.c
@@ -31,6 +31,7 @@ extern int verbose;
 extern int dry_run;
 extern int module_id;
 extern int modify_window;
+extern char *partial_dir;
 extern struct exclude_list_struct server_exclude_list;
 
 int sanitize_paths = 0;
@@ -900,6 +901,30 @@ int pop_dir(char *dir)
        return 1;
 }
 
+/**
+ * Return the filename, turning any newlines into '?'s.  This ensures that
+ * outputting it on a line of its own cannot generate an empty line.  This
+ * function can handle only 2 names at a time!
+ **/
+const char *safe_fname(const char *fname)
+{
+       static char fbuf1[MAXPATHLEN], fbuf2[MAXPATHLEN];
+       static char *fbuf = fbuf2;
+       char *nl = strchr(fname, '\n');
+
+       if (!nl)
+               return fname;
+
+       fbuf = fbuf == fbuf1 ? fbuf2 : fbuf1;
+       strlcpy(fbuf, fname, MAXPATHLEN);
+       nl = fbuf + (nl - (char *)fname);
+       do {
+               *nl = '?';
+       } while ((nl = strchr(nl+1, '\n')) != NULL);
+
+       return fbuf;
+}
+
 /**
  * Return a quoted string with the full pathname of the indicated filename.
  * The string " (in MODNAME)" may also be appended.  The returned pointer
@@ -914,6 +939,7 @@ char *full_fname(const char *fn)
        if (result)
                free(result);
 
+       fn = safe_fname(fn);
        if (*fn == '/')
                p1 = p2 = "";
        else {
@@ -945,6 +971,69 @@ char *full_fname(const char *fn)
        return result;
 }
 
+static char partial_fname[MAXPATHLEN];
+
+char *partial_dir_fname(const char *fname)
+{
+       char *t = partial_fname;
+       int sz = sizeof partial_fname;
+       const char *fn;
+
+       if ((fn = strrchr(fname, '/')) != NULL) {
+               fn++;
+               if (*partial_dir != '/') {
+                       int len = fn - fname;
+                       strncpy(t, fname, len); /* safe */
+                       t += len;
+                       sz -= len;
+               }
+       } else
+               fn = fname;
+       if ((int)pathjoin(t, sz, partial_dir, fn) >= sz)
+               return NULL;
+       if (server_exclude_list.head
+           && check_exclude(&server_exclude_list, partial_fname, 0) < 0)
+               return NULL;
+
+       return partial_fname;
+}
+
+/* If no --partial-dir option was specified, we don't need to do anything
+ * (the partial-dir is essentially '.'), so just return success. */
+int handle_partial_dir(const char *fname, int create)
+{
+       char *fn, *dir;
+
+       if (fname != partial_fname)
+               return 1;
+       if (!create && *partial_dir == '/')
+               return 1;
+       if (!(fn = strrchr(partial_fname, '/')))
+               return 1;
+
+       *fn = '\0';
+       dir = partial_fname;
+       if (create) {
+               STRUCT_STAT st;
+#if SUPPORT_LINKS
+               int statret = do_lstat(dir, &st);
+#else
+               int statret = do_stat(dir, &st);
+#endif
+               if (statret == 0 && !S_ISDIR(st.st_mode)) {
+                       if (do_unlink(dir) < 0)
+                               return 0;
+                       statret = -1;
+               }
+               if (statret < 0 && do_mkdir(dir, 0700) < 0)
+                       return 0;
+       } else
+               do_rmdir(dir);
+       *fn = '/';
+
+       return 1;
+}
+
 /** We need to supply our own strcmp function for file list comparisons
    to ensure that signed/unsigned usage is consistent between machines. */
 int u_strcmp(const char *cs1, const char *cs2)