X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/b5bd5542eb99f97561848113887d100b83e9bc43..fe1c19dcdfa000c2461e85ed7bf712de49904377:/util.c diff --git a/util.c b/util.c index f2158a02..141342cf 100644 --- a/util.c +++ b/util.c @@ -31,7 +31,8 @@ extern int verbose; extern int dry_run; extern int module_id; extern int modify_window; -extern struct exclude_list_struct server_exclude_list; +extern char *partial_dir; +extern struct filter_list_struct server_filter_list; int sanitize_paths = 0; @@ -104,9 +105,9 @@ void print_child_argv(char **cmd) "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" ",.-_=+@/") != strlen(*cmd)) { - rprintf(FINFO, "\"%s\" ", *cmd); + rprintf(FINFO, "\"%s\" ", safe_fname(*cmd)); } else { - rprintf(FINFO, "%s ", *cmd); + rprintf(FINFO, "%s ", safe_fname(*cmd)); } } rprintf(FINFO, "\n"); @@ -129,22 +130,22 @@ void overflow(char *str) int set_modtime(char *fname, time_t modtime) { - if (dry_run) - return 0; - if (verbose > 2) { rprintf(FINFO, "set modtime of %s to (%ld) %s", - fname, (long)modtime, + safe_fname(fname), (long)modtime, asctime(localtime(&modtime))); } + if (dry_run) + return 0; + { -#ifdef HAVE_UTIMBUF +#if HAVE_UTIMBUF struct utimbuf tbuf; tbuf.actime = time(NULL); tbuf.modtime = modtime; return utime(fname,&tbuf); -#elif defined(HAVE_UTIME) +#elif HAVE_UTIME time_t t[2]; t[0] = time(NULL); t[1] = modtime; @@ -196,7 +197,7 @@ int create_directory_path(char *fname, int base_umask) * * Derived from GNU C's cccp.c. */ -static int full_write(int desc, char *ptr, size_t len) +int full_write(int desc, char *ptr, size_t len) { int total_written; @@ -244,7 +245,7 @@ static int safe_read(int desc, char *ptr, size_t len) /** Copy a file. * - * This is used in conjunction with the --temp-dir option */ + * This is used in conjunction with the --temp-dir and --backup options */ int copy_file(char *source, char *dest, mode_t mode) { int ifd; @@ -349,7 +350,7 @@ int robust_unlink(char *fname) if (verbose > 0) { rprintf(FINFO,"renaming %s to %s because of text busy\n", - fname, path); + safe_fname(fname), safe_fname(path)); } /* maybe we should return rename()'s exit status? Nah. */ @@ -361,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; @@ -382,7 +383,7 @@ int robust_rename(char *from, char *to, int mode) if (copy_file(from, to, mode) != 0) return -2; do_unlink(from); - return 0; + return 1; default: return -1; } @@ -478,14 +479,14 @@ int lock_range(int fd, int offset, int len) return fcntl(fd,F_SETLK,&lock) == 0; } -static int exclude_server_path(char *arg) +static int filter_server_path(char *arg) { char *s; - if (server_exclude_list.head) { + if (server_filter_list.head) { for (s = arg; (s = strchr(s, '/')) != NULL; ) { *s = '\0'; - if (check_exclude(&server_exclude_list, arg, 1) < 0) { + if (check_filter(&server_filter_list, arg, 1) < 0) { /* We must leave arg truncated! */ return 1; } @@ -495,16 +496,24 @@ static int exclude_server_path(char *arg) return 0; } -static void glob_expand_one(char *s, char **argv, int *argc_ptr, int maxargs) +static void glob_expand_one(char *s, char ***argv_ptr, int *argc_ptr, + int *maxargs_ptr) { + char **argv = *argv_ptr; int argc = *argc_ptr; -#if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H)) - if (maxargs <= argc) - return; + int maxargs = *maxargs_ptr; +#if !(HAVE_GLOB && HAVE_GLOB_H) + if (argc == maxargs) { + maxargs += MAX_ARGS; + if (!(argv = realloc_array(argv, char *, maxargs))) + out_of_memory("glob_expand_one"); + *argv_ptr = argv; + *maxargs_ptr = maxargs; + } if (!*s) s = "."; s = argv[argc++] = strdup(s); - exclude_server_path(s); + filter_server_path(s); #else glob_t globbuf; int i; @@ -514,19 +523,25 @@ static void glob_expand_one(char *s, char **argv, int *argc_ptr, int maxargs) if (!*s) s = "."; - s = strdup(s); if (sanitize_paths) - sanitize_path(s, NULL); + s = sanitize_path(NULL, s, "", 0); + else + s = strdup(s); memset(&globbuf, 0, sizeof globbuf); - if (!exclude_server_path(s)) + if (!filter_server_path(s)) glob(s, 0, NULL, &globbuf); + if (MAX((int)globbuf.gl_pathc, 1) > maxargs - argc) { + maxargs += globbuf.gl_pathc + MAX_ARGS; + if (!(argv = realloc_array(argv, char *, maxargs))) + out_of_memory("glob_expand_one"); + *argv_ptr = argv; + *maxargs_ptr = maxargs; + } if (globbuf.gl_pathc == 0) argv[argc++] = s; else { int j = globbuf.gl_pathc; - if (j > maxargs - argc) - j = maxargs - argc; free(s); for (i = 0; i < j; i++) { if (!(argv[argc++] = strdup(globbuf.gl_pathv[i]))) @@ -539,9 +554,9 @@ static void glob_expand_one(char *s, char **argv, int *argc_ptr, int maxargs) } /* This routine is only used in daemon mode. */ -void glob_expand(char *base1, char **argv, int *argc_ptr, int maxargs) +void glob_expand(char *base1, char ***argv_ptr, int *argc_ptr, int *maxargs_ptr) { - char *s = argv[*argc_ptr]; + char *s = (*argv_ptr)[*argc_ptr]; char *p, *q; char *base = base1; int base_len = strlen(base); @@ -562,7 +577,7 @@ void glob_expand(char *base1, char **argv, int *argc_ptr, int maxargs) for (q = s; *q; q = p + base_len) { if ((p = strstr(q, base)) != NULL) *p = '\0'; /* split it at this point */ - glob_expand_one(q, argv, argc_ptr, maxargs); + glob_expand_one(q, argv_ptr, argc_ptr, maxargs_ptr); if (!p) break; } @@ -635,122 +650,143 @@ size_t stringjoin(char *dest, size_t destsize, ...) return ret; } -void clean_fname(char *name) +int count_dir_elements(const char *p) { - char *p; - int l; - int modified = 1; + int cnt = 0, new_component = 1; + while (*p) { + if (*p++ == '/') + new_component = 1; + else if (new_component) { + new_component = 0; + cnt++; + } + } + return cnt; +} - if (!name) - return; +/* Turns multiple adjacent slashes into a single slash, gets rid of "./" + * elements (but not a trailing dot dir), removes a trailing slash, and + * optionally collapses ".." elements (except for those at the start of the + * string). If the resulting name would be empty, change it into a ".". */ +unsigned int clean_fname(char *name, BOOL collapse_dot_dot) +{ + char *limit = name - 1, *t = name, *f = name; + int anchored; - while (modified) { - modified = 0; + if (!name) + return 0; - if ((p = strstr(name,"/./")) != NULL) { - modified = 1; - while (*p) { - p[0] = p[2]; - p++; - } + if ((anchored = *f == '/') != 0) + *t++ = *f++; + while (*f) { + /* discard extra slashes */ + if (*f == '/') { + f++; + continue; } - - if ((p = strstr(name,"//")) != NULL) { - modified = 1; - while (*p) { - p[0] = p[1]; - p++; + if (*f == '.') { + /* discard "." dirs (but NOT a trailing '.'!) */ + if (f[1] == '/') { + f += 2; + continue; + } + /* collapse ".." dirs */ + if (collapse_dot_dot + && f[1] == '.' && (f[2] == '/' || !f[2])) { + char *s = t - 1; + if (s == name && anchored) { + f += 2; + continue; + } + while (s > limit && *--s != '/') {} + if (s != t - 1 && (s < name || *s == '/')) { + t = s + 1; + f += 2; + continue; + } + limit = t + 2; } } + while (*f && (*t++ = *f++) != '/') {} + } - if (strncmp(p = name, "./", 2) == 0) { - modified = 1; - do { - p[0] = p[2]; - } while (*p++); - } + if (t > name+anchored && t[-1] == '/') + t--; + if (t == name) + *t++ = '.'; + *t = '\0'; - l = strlen(p = name); - if (l > 1 && p[l-1] == '/') { - modified = 1; - p[l-1] = 0; - } - } + return t - name; } -/** - * Make path appear as if a chroot had occurred: +/* Make path appear as if a chroot had occurred. This handles a leading + * "/" (either removing it or expanding it) and any leading or embedded + * ".." components that attempt to escape past the module's top dir. * - * @li 1. remove leading "/" (or replace with "." if at end) + * If dest is NULL, a buffer is allocated to hold the result. It is legal + * to call with the dest and the path (p) pointing to the same buffer, but + * rootdir will be ignored to avoid expansion of the string. * - * @li 2. remove leading ".." components (except those allowed by @p reldir) + * The rootdir string contains a value to use in place of a leading slash. + * Specify NULL to get the default of lp_path(module_id). * - * @li 3. delete any other "