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 /* create a file descriptor - like pipe() but use socketpair if
30 possible (because of blocking issues on pipes */
31 int fd_pair(int fd[2])
34 return socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
41 /* this is taken from CVS */
42 int piped_child(char **command,int *f_in,int *f_out)
46 int from_child_pipe[2];
48 if (fd_pair(to_child_pipe) < 0 ||
49 fd_pair(from_child_pipe) < 0) {
50 rprintf(FERROR,"pipe: %s\n",strerror(errno));
51 exit_cleanup(RERR_IPC);
57 rprintf(FERROR,"fork: %s\n",strerror(errno));
58 exit_cleanup(RERR_IPC);
63 extern int orig_umask;
64 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
65 close(to_child_pipe[1]) < 0 ||
66 close(from_child_pipe[0]) < 0 ||
67 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
68 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
69 exit_cleanup(RERR_IPC);
71 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
72 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
74 execvp(command[0], command);
75 rprintf(FERROR,"Failed to exec %s : %s\n",
76 command[0],strerror(errno));
77 exit_cleanup(RERR_IPC);
80 if (close(from_child_pipe[1]) < 0 ||
81 close(to_child_pipe[0]) < 0) {
82 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
83 exit_cleanup(RERR_IPC);
86 *f_in = from_child_pipe[0];
87 *f_out = to_child_pipe[1];
92 int local_child(int argc, char **argv,int *f_in,int *f_out)
96 int from_child_pipe[2];
98 if (fd_pair(to_child_pipe) < 0 ||
99 fd_pair(from_child_pipe) < 0) {
100 rprintf(FERROR,"pipe: %s\n",strerror(errno));
101 exit_cleanup(RERR_IPC);
107 rprintf(FERROR,"fork: %s\n",strerror(errno));
108 exit_cleanup(RERR_IPC);
112 extern int am_sender;
113 extern int am_server;
115 am_sender = !am_sender;
118 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
119 close(to_child_pipe[1]) < 0 ||
120 close(from_child_pipe[0]) < 0 ||
121 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
122 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
123 exit_cleanup(RERR_IPC);
125 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
126 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
127 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
130 if (close(from_child_pipe[1]) < 0 ||
131 close(to_child_pipe[0]) < 0) {
132 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
133 exit_cleanup(RERR_IPC);
136 *f_in = from_child_pipe[0];
137 *f_out = to_child_pipe[1];
144 void out_of_memory(char *str)
146 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
147 exit_cleanup(RERR_MALLOC);
150 void overflow(char *str)
152 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
153 exit_cleanup(RERR_MALLOC);
158 int set_modtime(char *fname,time_t modtime)
161 if (dry_run) return 0;
165 tbuf.actime = time(NULL);
166 tbuf.modtime = modtime;
167 return utime(fname,&tbuf);
168 #elif defined(HAVE_UTIME)
172 return utime(fname,t);
175 t[0].tv_sec = time(NULL);
177 t[1].tv_sec = modtime;
179 return utimes(fname,t);
185 /****************************************************************************
186 create any necessary directories in fname. Unfortunately we don't know
187 what perms to give the directory when this is called so we need to rely
189 ****************************************************************************/
190 int create_directory_path(char *fname)
192 extern int orig_umask;
195 while (*fname == '/') fname++;
196 while (strncmp(fname,"./",2)==0) fname += 2;
199 while ((p=strchr(p,'/'))) {
201 do_mkdir(fname,0777 & ~orig_umask);
209 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
210 Return LEN upon success, write's (negative) error code otherwise.
212 derived from GNU C's cccp.c.
214 static int full_write(int desc, char *ptr, int len)
220 int written = write (desc, ptr, len);
228 total_written += written;
232 return total_written;
235 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
236 Return the actual number of bytes read, zero for EOF, or negative
239 derived from GNU C's cccp.c. */
240 static int safe_read(int desc, char *ptr, int len)
249 n_chars = read(desc, ptr, len);
250 } while (n_chars < 0 && errno == EINTR);
252 n_chars = read(desc, ptr, len);
259 /* copy a file - this is used in conjunction with the --temp-dir option */
260 int copy_file(char *source, char *dest, mode_t mode)
265 int len; /* Number of bytes read into `buf'. */
267 ifd = do_open(source, O_RDONLY, 0);
269 rprintf(FERROR,"open %s: %s\n",
270 source,strerror(errno));
274 if (robust_unlink(dest) && errno != ENOENT) {
275 rprintf(FERROR,"unlink %s: %s\n",
276 dest,strerror(errno));
280 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
282 rprintf(FERROR,"open %s: %s\n",
283 dest,strerror(errno));
288 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
289 if (full_write(ofd, buf, len) < 0) {
290 rprintf(FERROR,"write %s: %s\n",
291 dest,strerror(errno));
302 rprintf(FERROR,"read %s: %s\n",
303 source,strerror(errno));
311 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
312 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
313 will shuffle the filenames around a bit as long as the file is still
314 busy; this is because this function does not know if the unlink call
315 is due to a new file coming in, or --delete trying to remove old
316 .rsyncNNN files, hence it renames it each time.
318 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
319 #define MAX_RENAMES_DIGITS 3
320 #define MAX_RENAMES 1000
322 int robust_unlink(char *fname)
325 return do_unlink(fname);
327 static int counter = 1;
329 char path[MAXPATHLEN];
331 rc = do_unlink(fname);
332 if ((rc == 0) || (errno != ETXTBSY))
335 strlcpy(path, fname, MAXPATHLEN);
338 while((path[--pos] != '/') && (pos >= 0))
341 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
342 pos += sizeof(".rsync")-1;
344 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
349 /* start where the last one left off to reduce chance of clashes */
352 sprintf(&path[pos], "%03d", counter);
353 if (++counter >= MAX_RENAMES)
355 } while (((rc = access(path, 0)) == 0) && (counter != start));
358 rprintf(FINFO,"renaming %s to %s because of text busy\n",
361 /* maybe we should return rename()'s exit status? Nah. */
362 if (do_rename(fname, path) != 0) {
370 int robust_rename(char *from, char *to)
373 return do_rename(from, to);
375 int rc = do_rename(from, to);
376 if ((rc == 0) || (errno != ETXTBSY))
378 if (robust_unlink(to) != 0)
380 return do_rename(from, to);
385 static pid_t all_pids[10];
388 /* fork and record the pid of the child */
391 pid_t newpid = fork();
394 all_pids[num_pids++] = newpid;
399 /* kill all children */
400 void kill_all(int sig)
403 for (i=0;i<num_pids;i++) {
404 if (all_pids[i] != getpid())
405 kill(all_pids[i], sig);
409 /* turn a user name into a uid */
410 int name_to_uid(char *name, uid_t *uid)
413 if (!name || !*name) return 0;
414 pass = getpwnam(name);
422 /* turn a group name into a gid */
423 int name_to_gid(char *name, gid_t *gid)
426 if (!name || !*name) return 0;
427 grp = getgrnam(name);
436 /* lock a byte range in a open file */
437 int lock_range(int fd, int offset, int len)
441 lock.l_type = F_WRLCK;
442 lock.l_whence = SEEK_SET;
443 lock.l_start = offset;
447 return fcntl(fd,F_SETLK,&lock) == 0;
451 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
453 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
455 argv[*argc] = strdup(s);
459 extern int sanitize_paths;
465 argv[*argc] = strdup(s);
466 if (sanitize_paths) {
467 sanitize_path(argv[*argc], NULL);
470 memset(&globbuf, 0, sizeof(globbuf));
471 glob(argv[*argc], 0, NULL, &globbuf);
472 if (globbuf.gl_pathc == 0) {
477 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
478 if (i == 0) free(argv[*argc]);
479 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
480 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
487 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
489 char *s = argv[*argc];
493 if (!s || !*s) return;
495 if (strncmp(s, base, strlen(base)) == 0) {
500 if (!s) out_of_memory("glob_expand");
502 base = (char *)malloc(strlen(base1)+3);
503 if (!base) out_of_memory("glob_expand");
505 sprintf(base," %s/", base1);
508 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
509 /* split it at this point */
511 glob_expand_one(q, argv, argc, maxargs);
515 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
521 /*******************************************************************
522 convert a string to lower case
523 ********************************************************************/
524 void strlower(char *s)
527 if (isupper(*s)) *s = tolower(*s);
532 /* this is like vsnprintf but it always null terminates, so you
533 can fit at most n-1 chars in */
534 int vslprintf(char *str, int n, const char *format, va_list ap)
536 int ret = vsnprintf(str, n, format, ap);
537 if (ret >= n || ret < 0) {
546 /* like snprintf but always null terminates */
547 int slprintf(char *str, int n, char *format, ...)
552 va_start(ap, format);
553 ret = vslprintf(str,n,format,ap);
559 void *Realloc(void *p, int size)
561 if (!p) return (void *)malloc(size);
562 return (void *)realloc(p, size);
566 void clean_fname(char *name)
577 if ((p=strstr(name,"/./"))) {
585 if ((p=strstr(name,"//"))) {
593 if (strncmp(p=name,"./",2) == 0) {
601 if (l > 1 && p[l-1] == '/') {
609 * Make path appear as if a chroot had occurred:
610 * 1. remove leading "/" (or replace with "." if at end)
611 * 2. remove leading ".." components (except those allowed by "reldir")
612 * 3. delete any other "<dir>/.." (recursively)
613 * Can only shrink paths, so sanitizes in place.
614 * While we're at it, remove double slashes and "." components like
615 * clean_fname does(), but DON'T remove a trailing slash because that
616 * is sometimes significant on command line arguments.
617 * If "reldir" is non-null, it is a sanitized directory that the path will be
618 * relative to, so allow as many ".." at the beginning of the path as
619 * there are components in reldir. This is used for symbolic link targets.
620 * If reldir is non-null and the path began with "/", to be completely like
621 * a chroot we should add in depth levels of ".." at the beginning of the
622 * path, but that would blow the assumption that the path doesn't grow and
623 * it is not likely to end up being a valid symlink anyway, so just do
624 * the normal removal of the leading "/" instead.
625 * Contributed by Dave Dykstra <dwd@bell-labs.com>
628 void sanitize_path(char *p, char *reldir)
637 if (*reldir++ == '/') {
645 /* remove leading slashes */
649 /* this loop iterates once per filename component in p.
650 * both p (and sanp if the original had a slash) should
651 * always be left pointing after a slash
653 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
654 /* skip "." component */
655 while (*++p == '/') {
656 /* skip following slashes */
662 if ((*p == '.') && (*(p+1) == '.') &&
663 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
664 /* ".." component followed by slash or end */
665 if ((depth > 0) && (sanp == start)) {
666 /* allow depth levels of .. at the beginning */
674 /* back up sanp one level */
675 --sanp; /* now pointing at slash */
676 while ((sanp > start) && (*(sanp - 1) != '/')) {
677 /* skip back up to slash */
685 /* copy one component through next slash */
687 if ((*p == '\0') || (*(p-1) == '/')) {
689 /* skip multiple slashes */
696 /* move the virtual beginning to leave the .. alone */
700 if ((sanp == start) && !allowdotdot) {
701 /* ended up with nothing, so put in "." component */
703 * note that the !allowdotdot doesn't prevent this from
704 * happening in all allowed ".." situations, but I didn't
705 * think it was worth putting in an extra variable to ensure
706 * it since an extra "." won't hurt in those situations.
714 static char curr_dir[MAXPATHLEN];
716 /* like chdir() but can be reversed with pop_dir() if save is set. It
717 is also much faster as it remembers where we have been */
718 char *push_dir(char *dir, int save)
720 char *ret = curr_dir;
721 static int initialised;
725 getcwd(curr_dir, sizeof(curr_dir)-1);
728 if (!dir) return NULL; /* this call was probably just to initialize */
730 if (chdir(dir)) return NULL;
733 ret = strdup(curr_dir);
737 strlcpy(curr_dir, dir, sizeof(curr_dir));
739 strlcat(curr_dir,"/", sizeof(curr_dir));
740 strlcat(curr_dir,dir, sizeof(curr_dir));
743 clean_fname(curr_dir);
748 /* reverse a push_dir call */
749 int pop_dir(char *dir)
759 strlcpy(curr_dir, dir, sizeof(curr_dir));
766 /* we need to supply our own strcmp function for file list comparisons
767 to ensure that signed/unsigned usage is consistent between machines. */
768 int u_strcmp(const char *cs1, const char *cs2)
770 const uchar *s1 = (const uchar *)cs1;
771 const uchar *s2 = (const uchar *)cs2;
773 while (*s1 && *s2 && (*s1 == *s2)) {
777 return (int)*s1 - (int)*s2;
780 static OFF_T last_ofs;
782 void end_progress(OFF_T size)
784 extern int do_progress, am_server;
786 if (do_progress && !am_server) {
787 rprintf(FINFO,"%.0f (100%%)\n", (double)size);
792 void show_progress(OFF_T ofs, OFF_T size)
794 extern int do_progress, am_server;
796 if (do_progress && !am_server) {
797 if (ofs > last_ofs + 1000) {
798 int pct = (int)((100.0*ofs)/size);
799 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
805 /* determine if a symlink points outside the current directory tree */
806 int unsafe_symlink(char *dest, char *src)
811 /* all absolute and null symlinks are unsafe */
812 if (!dest || !(*dest) || (*dest == '/')) return 1;
815 if (!src) out_of_memory("unsafe_symlink");
817 /* find out what our safety margin is */
818 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
819 if (strcmp(tok,"..") == 0) {
821 } else if (strcmp(tok,".") == 0) {
829 /* drop by one to account for the filename portion */
833 if (!dest) out_of_memory("unsafe_symlink");
835 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
836 if (strcmp(tok,"..") == 0) {
838 } else if (strcmp(tok,".") == 0) {
843 /* if at any point we go outside the current directory then
844 stop - it is unsafe */
845 if (depth < 0) break;
853 /****************************************************************************
854 return the date and time as a string
855 ****************************************************************************/
856 char *timestring(time_t t)
858 static char TimeBuf[200];
859 struct tm *tm = localtime(&t);
862 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
864 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
867 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
868 TimeBuf[strlen(TimeBuf)-1] = 0;
875 /****************************************************************************
876 like waitpid but does the WEXITSTATUS
877 ****************************************************************************/
879 #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
881 void wait_process(pid_t pid, int *status)
883 waitpid(pid, status, 0);
884 *status = WEXITSTATUS(*status);