2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Utilities used in rsync
29 /* this is taken from CVS */
30 int piped_child(char **command,int *f_in,int *f_out)
34 int from_child_pipe[2];
36 if (pipe(to_child_pipe) < 0 ||
37 pipe(from_child_pipe) < 0) {
38 rprintf(FERROR,"pipe: %s\n",strerror(errno));
39 exit_cleanup(RERR_IPC);
45 rprintf(FERROR,"fork: %s\n",strerror(errno));
46 exit_cleanup(RERR_IPC);
51 extern int orig_umask;
52 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
53 close(to_child_pipe[1]) < 0 ||
54 close(from_child_pipe[0]) < 0 ||
55 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
56 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
57 exit_cleanup(RERR_IPC);
59 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
60 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
62 execvp(command[0], command);
63 rprintf(FERROR,"Failed to exec %s : %s\n",
64 command[0],strerror(errno));
65 exit_cleanup(RERR_IPC);
68 if (close(from_child_pipe[1]) < 0 ||
69 close(to_child_pipe[0]) < 0) {
70 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
71 exit_cleanup(RERR_IPC);
74 *f_in = from_child_pipe[0];
75 *f_out = to_child_pipe[1];
80 int local_child(int argc, char **argv,int *f_in,int *f_out)
84 int from_child_pipe[2];
86 if (pipe(to_child_pipe) < 0 ||
87 pipe(from_child_pipe) < 0) {
88 rprintf(FERROR,"pipe: %s\n",strerror(errno));
89 exit_cleanup(RERR_IPC);
95 rprintf(FERROR,"fork: %s\n",strerror(errno));
96 exit_cleanup(RERR_IPC);
100 extern int am_sender;
101 extern int am_server;
103 am_sender = !am_sender;
106 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
107 close(to_child_pipe[1]) < 0 ||
108 close(from_child_pipe[0]) < 0 ||
109 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
110 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
111 exit_cleanup(RERR_IPC);
113 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
114 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
115 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
118 if (close(from_child_pipe[1]) < 0 ||
119 close(to_child_pipe[0]) < 0) {
120 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
121 exit_cleanup(RERR_IPC);
124 *f_in = from_child_pipe[0];
125 *f_out = to_child_pipe[1];
132 void out_of_memory(char *str)
134 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
135 exit_cleanup(RERR_MALLOC);
138 void overflow(char *str)
140 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
141 exit_cleanup(RERR_MALLOC);
146 int set_modtime(char *fname,time_t modtime)
149 if (dry_run) return 0;
153 tbuf.actime = time(NULL);
154 tbuf.modtime = modtime;
155 return utime(fname,&tbuf);
156 #elif defined(HAVE_UTIME)
160 return utime(fname,t);
163 t[0].tv_sec = time(NULL);
165 t[1].tv_sec = modtime;
167 return utimes(fname,t);
173 /****************************************************************************
174 create any necessary directories in fname. Unfortunately we don't know
175 what perms to give the directory when this is called so we need to rely
177 ****************************************************************************/
178 int create_directory_path(char *fname)
180 extern int orig_umask;
183 while (*fname == '/') fname++;
184 while (strncmp(fname,"./",2)==0) fname += 2;
187 while ((p=strchr(p,'/'))) {
189 do_mkdir(fname,0777 & ~orig_umask);
197 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
198 Return LEN upon success, write's (negative) error code otherwise.
200 derived from GNU C's cccp.c.
202 static int full_write(int desc, char *ptr, int len)
208 int written = write (desc, ptr, len);
216 total_written += written;
220 return total_written;
223 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
224 Return the actual number of bytes read, zero for EOF, or negative
227 derived from GNU C's cccp.c. */
228 static int safe_read(int desc, char *ptr, int len)
237 n_chars = read(desc, ptr, len);
238 } while (n_chars < 0 && errno == EINTR);
240 n_chars = read(desc, ptr, len);
247 /* copy a file - this is used in conjunction with the --temp-dir option */
248 int copy_file(char *source, char *dest, mode_t mode)
253 int len; /* Number of bytes read into `buf'. */
255 ifd = do_open(source, O_RDONLY, 0);
257 rprintf(FERROR,"open %s: %s\n",
258 source,strerror(errno));
262 if (robust_unlink(dest) && errno != ENOENT) {
263 rprintf(FERROR,"unlink %s: %s\n",
264 dest,strerror(errno));
268 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
270 rprintf(FERROR,"open %s: %s\n",
271 dest,strerror(errno));
276 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
277 if (full_write(ofd, buf, len) < 0) {
278 rprintf(FERROR,"write %s: %s\n",
279 dest,strerror(errno));
290 rprintf(FERROR,"read %s: %s\n",
291 source,strerror(errno));
299 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
300 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
301 will shuffle the filenames around a bit as long as the file is still
302 busy; this is because this function does not know if the unlink call
303 is due to a new file coming in, or --delete trying to remove old
304 .rsyncNNN files, hence it renames it each time.
306 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
307 #define MAX_RENAMES_DIGITS 3
308 #define MAX_RENAMES 1000
310 int robust_unlink(char *fname)
313 return do_unlink(fname);
315 static int counter = 1;
317 char path[MAXPATHLEN];
319 rc = do_unlink(fname);
320 if ((rc == 0) || (errno != ETXTBSY))
323 strlcpy(path, fname, MAXPATHLEN);
326 while((path[--pos] != '/') && (pos >= 0))
329 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
330 pos += sizeof(".rsync")-1;
332 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
337 /* start where the last one left off to reduce chance of clashes */
340 sprintf(&path[pos], "%03d", counter);
341 if (++counter >= MAX_RENAMES)
343 } while (((rc = access(path, 0)) == 0) && (counter != start));
346 rprintf(FINFO,"renaming %s to %s because of text busy\n",
349 /* maybe we should return rename()'s exit status? Nah. */
350 if (do_rename(fname, path) != 0) {
358 int robust_rename(char *from, char *to)
361 return do_rename(from, to);
363 int rc = do_rename(from, to);
364 if ((rc == 0) || (errno != ETXTBSY))
366 if (robust_unlink(to) != 0)
368 return do_rename(from, to);
373 /* sleep for a while via select */
374 void u_sleep(int usec)
380 select(0, NULL, NULL, NULL, &tv);
384 static pid_t all_pids[10];
387 /* fork and record the pid of the child */
390 pid_t newpid = fork();
393 all_pids[num_pids++] = newpid;
398 /* kill all children */
399 void kill_all(int sig)
402 for (i=0;i<num_pids;i++) {
403 if (all_pids[i] != getpid())
404 kill(all_pids[i], sig);
408 /* turn a user name into a uid */
409 int name_to_uid(char *name, uid_t *uid)
412 if (!name || !*name) return 0;
413 pass = getpwnam(name);
421 /* turn a group name into a gid */
422 int name_to_gid(char *name, gid_t *gid)
425 if (!name || !*name) return 0;
426 grp = getgrnam(name);
435 /* lock a byte range in a open file */
436 int lock_range(int fd, int offset, int len)
440 lock.l_type = F_WRLCK;
441 lock.l_whence = SEEK_SET;
442 lock.l_start = offset;
446 return fcntl(fd,F_SETLK,&lock) == 0;
450 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
452 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
454 argv[*argc] = strdup(s);
458 extern int sanitize_paths;
464 argv[*argc] = strdup(s);
465 if (sanitize_paths) {
466 sanitize_path(argv[*argc], NULL);
469 memset(&globbuf, 0, sizeof(globbuf));
470 glob(argv[*argc], 0, NULL, &globbuf);
471 if (globbuf.gl_pathc == 0) {
476 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
477 if (i == 0) free(argv[*argc]);
478 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
479 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
486 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
488 char *s = argv[*argc];
492 if (!s || !*s) return;
494 if (strncmp(s, base, strlen(base)) == 0) {
499 if (!s) out_of_memory("glob_expand");
501 base = (char *)malloc(strlen(base1)+3);
502 if (!base) out_of_memory("glob_expand");
504 sprintf(base," %s/", base1);
507 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
508 /* split it at this point */
510 glob_expand_one(q, argv, argc, maxargs);
514 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
520 /*******************************************************************
521 convert a string to lower case
522 ********************************************************************/
523 void strlower(char *s)
526 if (isupper(*s)) *s = tolower(*s);
531 /* this is like vsnprintf but it always null terminates, so you
532 can fit at most n-1 chars in */
533 int vslprintf(char *str, int n, const char *format, va_list ap)
535 int ret = vsnprintf(str, n, format, ap);
536 if (ret >= n || ret < 0) {
545 /* like snprintf but always null terminates */
546 int slprintf(char *str, int n, char *format, ...)
551 va_start(ap, format);
552 ret = vslprintf(str,n,format,ap);
558 void *Realloc(void *p, int size)
560 if (!p) return (void *)malloc(size);
561 return (void *)realloc(p, size);
565 void clean_fname(char *name)
576 if ((p=strstr(name,"/./"))) {
584 if ((p=strstr(name,"//"))) {
592 if (strncmp(p=name,"./",2) == 0) {
600 if (l > 1 && p[l-1] == '/') {
608 * Make path appear as if a chroot had occurred:
609 * 1. remove leading "/" (or replace with "." if at end)
610 * 2. remove leading ".." components (except those allowed by "reldir")
611 * 3. delete any other "<dir>/.." (recursively)
612 * Can only shrink paths, so sanitizes in place.
613 * While we're at it, remove double slashes and "." components like
614 * clean_fname does(), but DON'T remove a trailing slash because that
615 * is sometimes significant on command line arguments.
616 * If "reldir" is non-null, it is a sanitized directory that the path will be
617 * relative to, so allow as many ".." at the beginning of the path as
618 * there are components in reldir. This is used for symbolic link targets.
619 * If reldir is non-null and the path began with "/", to be completely like
620 * a chroot we should add in depth levels of ".." at the beginning of the
621 * path, but that would blow the assumption that the path doesn't grow and
622 * it is not likely to end up being a valid symlink anyway, so just do
623 * the normal removal of the leading "/" instead.
624 * Contributed by Dave Dykstra <dwd@bell-labs.com>
627 void sanitize_path(char *p, char *reldir)
636 if (*reldir++ == '/') {
644 /* remove leading slashes */
648 /* this loop iterates once per filename component in p.
649 * both p (and sanp if the original had a slash) should
650 * always be left pointing after a slash
652 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
653 /* skip "." component */
654 while (*++p == '/') {
655 /* skip following slashes */
661 if ((*p == '.') && (*(p+1) == '.') &&
662 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
663 /* ".." component followed by slash or end */
664 if ((depth > 0) && (sanp == start)) {
665 /* allow depth levels of .. at the beginning */
673 /* back up sanp one level */
674 --sanp; /* now pointing at slash */
675 while ((sanp > start) && (*(sanp - 1) != '/')) {
676 /* skip back up to slash */
684 /* copy one component through next slash */
686 if ((*p == '\0') || (*(p-1) == '/')) {
688 /* skip multiple slashes */
695 /* move the virtual beginning to leave the .. alone */
699 if ((sanp == start) && !allowdotdot) {
700 /* ended up with nothing, so put in "." component */
702 * note that the !allowdotdot doesn't prevent this from
703 * happening in all allowed ".." situations, but I didn't
704 * think it was worth putting in an extra variable to ensure
705 * it since an extra "." won't hurt in those situations.
713 static char curr_dir[MAXPATHLEN];
715 /* like chdir() but can be reversed with pop_dir() if save is set. It
716 is also much faster as it remembers where we have been */
717 char *push_dir(char *dir, int save)
719 char *ret = curr_dir;
720 static int initialised;
724 getcwd(curr_dir, sizeof(curr_dir)-1);
727 if (!dir) return NULL; /* this call was probably just to initialize */
729 if (chdir(dir)) return NULL;
732 ret = strdup(curr_dir);
736 strlcpy(curr_dir, dir, sizeof(curr_dir));
738 strlcat(curr_dir,"/", sizeof(curr_dir));
739 strlcat(curr_dir,dir, sizeof(curr_dir));
742 clean_fname(curr_dir);
747 /* reverse a push_dir call */
748 int pop_dir(char *dir)
758 strlcpy(curr_dir, dir, sizeof(curr_dir));
765 /* we need to supply our own strcmp function for file list comparisons
766 to ensure that signed/unsigned usage is consistent between machines. */
767 int u_strcmp(const char *cs1, const char *cs2)
769 const uchar *s1 = (const uchar *)cs1;
770 const uchar *s2 = (const uchar *)cs2;
772 while (*s1 && *s2 && (*s1 == *s2)) {
776 return (int)*s1 - (int)*s2;
779 static OFF_T last_ofs;
781 void end_progress(void)
783 extern int do_progress, am_server;
785 if (do_progress && !am_server) {
791 void show_progress(OFF_T ofs, OFF_T size)
793 extern int do_progress, am_server;
795 if (do_progress && !am_server) {
796 if (ofs > last_ofs + 1000) {
797 int pct = (int)((100.0*ofs)/size);
798 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
804 /* determine if a symlink points outside the current directory tree */
805 int unsafe_symlink(char *dest, char *src)
810 /* all absolute and null symlinks are unsafe */
811 if (!dest || !(*dest) || (*dest == '/')) return 1;
814 if (!src) out_of_memory("unsafe_symlink");
816 /* find out what our safety margin is */
817 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
818 if (strcmp(tok,"..") == 0) {
820 } else if (strcmp(tok,".") == 0) {
828 /* drop by one to account for the filename portion */
832 if (!dest) out_of_memory("unsafe_symlink");
834 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
835 if (strcmp(tok,"..") == 0) {
837 } else if (strcmp(tok,".") == 0) {
842 /* if at any point we go outside the current directory then
843 stop - it is unsafe */
844 if (depth < 0) break;
852 /****************************************************************************
853 return the date and time as a string
854 ****************************************************************************/
855 char *timestring(time_t t)
857 static char TimeBuf[200];
858 struct tm *tm = localtime(&t);
861 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
863 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
866 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
867 TimeBuf[strlen(TimeBuf)-1] = 0;