From 29a89172f77ed76dcf53612e96dc35daf15d7362 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sun, 30 Mar 2008 15:40:34 -0700 Subject: [PATCH] Improved the chdir() code: - Renamed push_dir() to change_dir() and revised it a little so that it can chdir() to a relative path without an intervening chdir() back to the staring path. - Renamed push_pathname() to change_pathname() and revised it to take different args and to only call path_is_daemon_excluded() on a new path (not a revisit of a file's already-checked path). - Fixed change_pathname() to set the right pathname value when a chdir() call fails. - Set orig_dir once outside of the change_pathname() function. - Got rid of pop_dir(). --- clientserver.c | 10 ++++---- flist.c | 68 +++++++++++++++++++++++++++++++------------------- main.c | 25 +++++++++---------- rsync.h | 3 +++ sender.c | 4 +-- util.c | 54 ++++++++++++++------------------------- 6 files changed, 84 insertions(+), 80 deletions(-) diff --git a/clientserver.c b/clientserver.c index 39a8cedf..52913567 100644 --- a/clientserver.c +++ b/clientserver.c @@ -509,15 +509,15 @@ static int rsync_module(int f_in, int f_out, int i, char *addr, char *host) out_of_memory("rsync_module"); } - /* We do a push_dir() that doesn't actually call chdir() + /* We do a change_dir() that doesn't actually call chdir() * just to make a relative path absolute. */ strlcpy(line, curr_dir, sizeof line); - if (!push_dir(module_dir, 1)) + if (!change_dir(module_dir, CD_SKIP_CHDIR)) goto chdir_failed; if (strcmp(curr_dir, module_dir) != 0 && (module_dir = strdup(curr_dir)) == NULL) out_of_memory("rsync_module"); - push_dir(line, 1); /* Restore curr_dir. */ + change_dir(line, CD_SKIP_CHDIR); /* Restore curr_dir. */ if (use_chroot) { chroot_path = module_dir; @@ -673,12 +673,12 @@ static int rsync_module(int f_in, int f_out, int i, char *addr, char *host) io_printf(f_out, "@ERROR: chroot failed\n"); return -1; } - if (!push_dir(module_dir, 0)) + if (!change_dir(module_dir, CD_NORMAL)) goto chdir_failed; if (module_dirlen) sanitize_paths = 1; } else { - if (!push_dir(module_dir, 0)) { + if (!change_dir(module_dir, CD_NORMAL)) { chdir_failed: rsyserr(FLOG, errno, "chdir %s failed\n", module_dir); io_printf(f_out, "@ERROR: chdir failed\n"); diff --git a/flist.c b/flist.c index f8e809a6..4e302aa6 100644 --- a/flist.c +++ b/flist.c @@ -242,10 +242,10 @@ static inline int is_daemon_excluded(const char *fname, int is_dir) return 0; } -static inline int path_is_daemon_excluded(const char *path, int ignore_filename) +static inline int path_is_daemon_excluded(char *path, int ignore_filename) { if (daemon_filter_list.head) { - char *slash = (char*)path; + char *slash = path; while ((slash = strchr(slash+1, '/')) != NULL) { int ret; @@ -293,7 +293,6 @@ static void send_directory(int f, struct file_list *flist, static const char *pathname, *orig_dir; static int pathname_len; - /* Make sure flist can hold at least flist->used + extra entries. */ static void flist_expand(struct file_list *flist, int extra) { @@ -339,28 +338,46 @@ static void flist_done_allocating(struct file_list *flist) flist->pool_boundary = ptr; } -int push_pathname(const char *dir, int len) +/* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's + * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir, + * with dir == NULL taken to be the starting directory, and dirlen < 0 + * indicating that strdup(dir) should be called and then the -dirlen length + * value checked to ensure that it is not daemon-excluded. */ +int change_pathname(struct file_struct *file, const char *dir, int dirlen) { - if (dir == pathname) - return 1; - - if (!orig_dir) - orig_dir = strdup(curr_dir); - - if (pathname && !pop_dir(orig_dir)) { - rsyserr(FERROR, errno, "pop_dir %s failed", - full_fname(orig_dir)); - exit_cleanup(RERR_FILESELECT); + if (dirlen < 0) { + char *cpy = strdup(dir); + if (*cpy != '/') + change_dir(orig_dir, CD_SKIP_CHDIR); + if (path_is_daemon_excluded(cpy, 0)) + goto chdir_error; + dir = cpy; + dirlen = -dirlen; + } else { + if (file) { + if (pathname == F_PATHNAME(file)) + return 1; + dir = F_PATHNAME(file); + if (dir) + dirlen = strlen(dir); + } else if (pathname == dir) + return 1; + if (dir && *dir != '/') + change_dir(orig_dir, CD_SKIP_CHDIR); } - if (dir && (path_is_daemon_excluded(dir, 0) || !push_dir(dir, 0))) { + if (!change_dir(dir ? dir : orig_dir, CD_NORMAL)) { + chdir_error: io_error |= IOERR_GENERAL; - rsyserr(FERROR, errno, "push_dir %s failed", full_fname(dir)); + rsyserr(FERROR, errno, "change_dir %s failed", full_fname(dir)); + change_dir(orig_dir, CD_NORMAL); + pathname = NULL; + pathname_len = 0; return 0; } pathname = dir; - pathname_len = len >= 0 ? len : dir ? (int)strlen(dir) : 0; + pathname_len = dirlen; return 1; } @@ -1666,10 +1683,8 @@ static void send1extra(int f, struct file_struct *file, struct file_list *flist) f_name(file, fbuf); dlen = strlen(fbuf); - if (F_PATHNAME(file) != pathname) { - if (!push_pathname(F_PATHNAME(file), -1)) - exit_cleanup(RERR_FILESELECT); - } + if (!change_pathname(file, NULL, 0)) + exit_cleanup(RERR_FILESELECT); change_local_filter_dir(fbuf, dlen, send_dir_depth); @@ -1852,6 +1867,9 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) start_write = stats.total_written; gettimeofday(&start_tv, NULL); + if (!orig_dir) + orig_dir = strdup(curr_dir); + if (relative_paths && protocol_version >= 30) implied_dirs = 1; /* We send flagged implied dirs */ @@ -1869,8 +1887,8 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) disable_buffering = io_start_buffering_out(f); if (filesfrom_fd >= 0) { - if (argv[0] && !push_dir(argv[0], 0)) { - rsyserr(FERROR_XFER, errno, "push_dir %s failed", + if (argv[0] && !change_dir(argv[0], CD_NORMAL)) { + rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(argv[0])); exit_cleanup(RERR_FILESELECT); } @@ -1994,11 +2012,11 @@ struct file_list *send_file_list(int f, int argc, char *argv[]) dirlen = dir ? strlen(dir) : 0; if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) { - if (!push_pathname(dir ? strdup(dir) : NULL, dirlen)) + if (!change_pathname(NULL, dir, -dirlen)) continue; lastdir = pathname; lastdir_len = pathname_len; - } else if (!push_pathname(lastdir, lastdir_len)) + } else if (!change_pathname(NULL, lastdir, lastdir_len)) continue; if (fn != fbuf) diff --git a/main.c b/main.c index d9a6c818..f08d359f 100644 --- a/main.c +++ b/main.c @@ -519,8 +519,8 @@ static char *get_local_name(struct file_list *flist, char *dest_path) if ((statret = do_stat(dest_path, &st)) == 0) { /* If the destination is a dir, enter it and use mode 1. */ if (S_ISDIR(st.st_mode)) { - if (!push_dir(dest_path, 0)) { - rsyserr(FERROR, errno, "push_dir#1 %s failed", + if (!change_dir(dest_path, CD_NORMAL)) { + rsyserr(FERROR, errno, "change_dir#1 %s failed", full_fname(dest_path)); exit_cleanup(RERR_FILESELECT); } @@ -579,8 +579,8 @@ static char *get_local_name(struct file_list *flist, char *dest_path) dry_run++; } - if (!push_dir(dest_path, dry_run > 1)) { - rsyserr(FERROR, errno, "push_dir#2 %s failed", + if (!change_dir(dest_path, dry_run > 1 ? CD_SKIP_CHDIR : CD_NORMAL)) { + rsyserr(FERROR, errno, "change_dir#2 %s failed", full_fname(dest_path)); exit_cleanup(RERR_FILESELECT); } @@ -599,8 +599,8 @@ static char *get_local_name(struct file_list *flist, char *dest_path) dest_path = "/"; *cp = '\0'; - if (!push_dir(dest_path, 0)) { - rsyserr(FERROR, errno, "push_dir#3 %s failed", + if (!change_dir(dest_path, CD_NORMAL)) { + rsyserr(FERROR, errno, "change_dir#3 %s failed", full_fname(dest_path)); exit_cleanup(RERR_FILESELECT); } @@ -692,8 +692,8 @@ static void do_server_sender(int f_in, int f_out, int argc, char *argv[]) } if (!relative_paths) { - if (!push_dir(dir, 0)) { - rsyserr(FERROR, errno, "push_dir#3 %s failed", + if (!change_dir(dir, CD_NORMAL)) { + rsyserr(FERROR, errno, "change_dir#3 %s failed", full_fname(dir)); exit_cleanup(RERR_FILESELECT); } @@ -862,8 +862,8 @@ static void do_server_recv(int f_in, int f_out, int argc, char *argv[]) char *dir = argv[0]; argc--; argv++; - if (!am_daemon && !push_dir(dir, 0)) { - rsyserr(FERROR, errno, "push_dir#4 %s failed", + if (!am_daemon && !change_dir(dir, CD_NORMAL)) { + rsyserr(FERROR, errno, "change_dir#4 %s failed", full_fname(dir)); exit_cleanup(RERR_FILESELECT); } @@ -1428,11 +1428,11 @@ int main(int argc,char *argv[]) SIGACTION(SIGXFSZ, SIG_IGN); #endif - /* Initialize push_dir here because on some old systems getcwd + /* Initialize change_dir() here because on some old systems getcwd * (implemented by forking "pwd" and reading its output) doesn't * work when there are other child processes. Also, on all systems * that implement getcwd that way "pwd" can't be found after chroot. */ - push_dir(NULL, 0); + change_dir(NULL, CD_NORMAL); init_flist(); @@ -1456,7 +1456,6 @@ int main(int argc,char *argv[]) read_stream_flags(batch_fd); else write_stream_flags(batch_fd); - } if (write_batch < 0) dry_run = 1; diff --git a/rsync.h b/rsync.h index 5d153610..e52e66aa 100644 --- a/rsync.h +++ b/rsync.h @@ -199,6 +199,9 @@ #define SP_DEFAULT 0 #define SP_KEEP_DOT_DIRS (1<<0) +#define CD_NORMAL 0 +#define CD_SKIP_CHDIR 1 + /* Log-message categories. FLOG only goes to the log file, not the client; * FCLIENT is the opposite. */ enum logcode { diff --git a/sender.c b/sender.c index 07887d9b..f7728772 100644 --- a/sender.c +++ b/sender.c @@ -135,7 +135,7 @@ void successful_send(int ndx) } file = flist->files[ndx - flist->ndx_start]; - if (!push_pathname(F_PATHNAME(file), -1)) + if (!change_pathname(file, NULL, 0)) return; f_name(file, fname); @@ -221,7 +221,7 @@ void send_files(int f_in, int f_out) } else { path = slash = ""; } - if (!push_pathname(F_PATHNAME(file), -1)) + if (!change_pathname(file, NULL, 0)) continue; f_name(file, fname); diff --git a/util.c b/util.c index 27894a1c..a40ce7b5 100644 --- a/util.c +++ b/util.c @@ -943,7 +943,7 @@ char *sanitize_path(char *dest, const char *p, const char *rootdir, int depth, /* Like chdir(), but it keeps track of the current directory (in the * global "curr_dir"), and ensures that the path size doesn't overflow. * Also cleans the path using the clean_fname() function. */ -int push_dir(const char *dir, int set_path_only) +int change_dir(const char *dir, int set_path_only) { static int initialised; unsigned int len; @@ -961,21 +961,26 @@ int push_dir(const char *dir, int set_path_only) if (len == 1 && *dir == '.') return 1; - if ((*dir == '/' ? len : curr_dir_len + 1 + len) >= sizeof curr_dir) { - errno = ENAMETOOLONG; - return 0; - } - - if (!set_path_only && chdir(dir)) - return 0; - if (*dir == '/') { + if (len >= sizeof curr_dir) { + errno = ENAMETOOLONG; + return 0; + } + if (!set_path_only && chdir(dir)) + return 0; memcpy(curr_dir, dir, len + 1); - curr_dir_len = len; } else { - curr_dir[curr_dir_len++] = '/'; - memcpy(curr_dir + curr_dir_len, dir, len + 1); - curr_dir_len += len; + if (curr_dir_len + 1 + len >= sizeof curr_dir) { + errno = ENAMETOOLONG; + return 0; + } + curr_dir[curr_dir_len] = '/'; + memcpy(curr_dir + curr_dir_len + 1, dir, len + 1); + + if (!set_path_only && chdir(curr_dir)) { + curr_dir[curr_dir_len] = '\0'; + return 0; + } } curr_dir_len = clean_fname(curr_dir, CFN_COLLAPSE_DOT_DOT_DIRS); @@ -986,28 +991,7 @@ int push_dir(const char *dir, int set_path_only) } if (verbose >= 5 && !set_path_only) - rprintf(FINFO, "[%s] push_dir(%s)\n", who_am_i(), curr_dir); - - return 1; -} - -/** - * Reverse a push_dir() call. You must pass in an absolute path - * that was copied from a prior value of "curr_dir". - **/ -int pop_dir(const char *dir) -{ - if (chdir(dir)) - return 0; - - curr_dir_len = strlcpy(curr_dir, dir, sizeof curr_dir); - if (curr_dir_len >= sizeof curr_dir) - curr_dir_len = sizeof curr_dir - 1; - if (sanitize_paths) - curr_dir_depth = count_dir_elements(curr_dir + module_dirlen); - - if (verbose >= 5) - rprintf(FINFO, "[%s] pop_dir(%s)\n", who_am_i(), curr_dir); + rprintf(FINFO, "[%s] change_dir(%s)\n", who_am_i(), curr_dir); return 1; } -- 2.34.1