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
30 /****************************************************************************
31 Set a fd into nonblocking mode
32 ****************************************************************************/
33 void set_nonblocking(int fd)
37 if((val = fcntl(fd, F_GETFL, 0)) == -1)
39 if (!(val & NONBLOCK_FLAG)) {
41 fcntl(fd, F_SETFL, val);
45 /****************************************************************************
46 Set a fd into blocking mode
47 ****************************************************************************/
48 void set_blocking(int fd)
52 if((val = fcntl(fd, F_GETFL, 0)) == -1)
54 if (val & NONBLOCK_FLAG) {
55 val &= ~NONBLOCK_FLAG;
56 fcntl(fd, F_SETFL, val);
61 /* create a file descriptor pair - like pipe() but use socketpair if
62 possible (because of blocking issues on pipes)
64 always set non-blocking
66 int fd_pair(int fd[2])
71 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
77 set_nonblocking(fd[0]);
78 set_nonblocking(fd[1]);
85 /* this is derived from CVS code */
86 int piped_child(char **command,int *f_in,int *f_out)
90 int from_child_pipe[2];
91 extern int blocking_io;
93 if (fd_pair(to_child_pipe) < 0 ||
94 fd_pair(from_child_pipe) < 0) {
95 rprintf(FERROR,"pipe: %s\n",strerror(errno));
96 exit_cleanup(RERR_IPC);
102 rprintf(FERROR,"fork: %s\n",strerror(errno));
103 exit_cleanup(RERR_IPC);
108 extern int orig_umask;
109 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
110 close(to_child_pipe[1]) < 0 ||
111 close(from_child_pipe[0]) < 0 ||
112 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
113 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
114 exit_cleanup(RERR_IPC);
116 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
117 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
119 set_blocking(STDIN_FILENO);
121 set_blocking(STDOUT_FILENO);
123 execvp(command[0], command);
124 rprintf(FERROR,"Failed to exec %s : %s\n",
125 command[0],strerror(errno));
126 exit_cleanup(RERR_IPC);
129 if (close(from_child_pipe[1]) < 0 ||
130 close(to_child_pipe[0]) < 0) {
131 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
132 exit_cleanup(RERR_IPC);
135 *f_in = from_child_pipe[0];
136 *f_out = to_child_pipe[1];
141 int local_child(int argc, char **argv,int *f_in,int *f_out)
144 int to_child_pipe[2];
145 int from_child_pipe[2];
147 if (fd_pair(to_child_pipe) < 0 ||
148 fd_pair(from_child_pipe) < 0) {
149 rprintf(FERROR,"pipe: %s\n",strerror(errno));
150 exit_cleanup(RERR_IPC);
156 rprintf(FERROR,"fork: %s\n",strerror(errno));
157 exit_cleanup(RERR_IPC);
161 extern int am_sender;
162 extern int am_server;
164 am_sender = !am_sender;
167 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
168 close(to_child_pipe[1]) < 0 ||
169 close(from_child_pipe[0]) < 0 ||
170 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
171 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
172 exit_cleanup(RERR_IPC);
174 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
175 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
176 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
179 if (close(from_child_pipe[1]) < 0 ||
180 close(to_child_pipe[0]) < 0) {
181 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
182 exit_cleanup(RERR_IPC);
185 *f_in = from_child_pipe[0];
186 *f_out = to_child_pipe[1];
193 void out_of_memory(char *str)
195 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
196 exit_cleanup(RERR_MALLOC);
199 void overflow(char *str)
201 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
202 exit_cleanup(RERR_MALLOC);
207 int set_modtime(char *fname,time_t modtime)
210 if (dry_run) return 0;
214 tbuf.actime = time(NULL);
215 tbuf.modtime = modtime;
216 return utime(fname,&tbuf);
217 #elif defined(HAVE_UTIME)
221 return utime(fname,t);
224 t[0].tv_sec = time(NULL);
226 t[1].tv_sec = modtime;
228 return utimes(fname,t);
234 /****************************************************************************
235 create any necessary directories in fname. Unfortunately we don't know
236 what perms to give the directory when this is called so we need to rely
238 ****************************************************************************/
239 int create_directory_path(char *fname)
241 extern int orig_umask;
244 while (*fname == '/') fname++;
245 while (strncmp(fname,"./",2)==0) fname += 2;
248 while ((p=strchr(p,'/'))) {
250 do_mkdir(fname,0777 & ~orig_umask);
258 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
259 Return LEN upon success, write's (negative) error code otherwise.
261 derived from GNU C's cccp.c.
263 static int full_write(int desc, char *ptr, int len)
269 int written = write (desc, ptr, len);
277 total_written += written;
281 return total_written;
284 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
285 Return the actual number of bytes read, zero for EOF, or negative
288 derived from GNU C's cccp.c. */
289 static int safe_read(int desc, char *ptr, int len)
298 n_chars = read(desc, ptr, len);
299 } while (n_chars < 0 && errno == EINTR);
301 n_chars = read(desc, ptr, len);
308 /* copy a file - this is used in conjunction with the --temp-dir option */
309 int copy_file(char *source, char *dest, mode_t mode)
314 int len; /* Number of bytes read into `buf'. */
316 ifd = do_open(source, O_RDONLY, 0);
318 rprintf(FERROR,"open %s: %s\n",
319 source,strerror(errno));
323 if (robust_unlink(dest) && errno != ENOENT) {
324 rprintf(FERROR,"unlink %s: %s\n",
325 dest,strerror(errno));
329 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
331 rprintf(FERROR,"open %s: %s\n",
332 dest,strerror(errno));
337 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
338 if (full_write(ofd, buf, len) < 0) {
339 rprintf(FERROR,"write %s: %s\n",
340 dest,strerror(errno));
351 rprintf(FERROR,"read %s: %s\n",
352 source,strerror(errno));
360 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
361 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
362 will shuffle the filenames around a bit as long as the file is still
363 busy; this is because this function does not know if the unlink call
364 is due to a new file coming in, or --delete trying to remove old
365 .rsyncNNN files, hence it renames it each time.
367 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
368 #define MAX_RENAMES_DIGITS 3
369 #define MAX_RENAMES 1000
371 int robust_unlink(char *fname)
374 return do_unlink(fname);
376 static int counter = 1;
378 char path[MAXPATHLEN];
380 rc = do_unlink(fname);
381 if ((rc == 0) || (errno != ETXTBSY))
384 strlcpy(path, fname, MAXPATHLEN);
387 while((path[--pos] != '/') && (pos >= 0))
390 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
391 pos += sizeof(".rsync")-1;
393 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
398 /* start where the last one left off to reduce chance of clashes */
401 sprintf(&path[pos], "%03d", counter);
402 if (++counter >= MAX_RENAMES)
404 } while (((rc = access(path, 0)) == 0) && (counter != start));
407 rprintf(FINFO,"renaming %s to %s because of text busy\n",
410 /* maybe we should return rename()'s exit status? Nah. */
411 if (do_rename(fname, path) != 0) {
419 int robust_rename(char *from, char *to)
422 return do_rename(from, to);
424 int rc = do_rename(from, to);
425 if ((rc == 0) || (errno != ETXTBSY))
427 if (robust_unlink(to) != 0)
429 return do_rename(from, to);
434 static pid_t all_pids[10];
437 /* fork and record the pid of the child */
440 pid_t newpid = fork();
443 all_pids[num_pids++] = newpid;
448 /* kill all children */
449 void kill_all(int sig)
452 for (i=0;i<num_pids;i++) {
453 if (all_pids[i] != getpid())
454 kill(all_pids[i], sig);
458 /* turn a user name into a uid */
459 int name_to_uid(char *name, uid_t *uid)
462 if (!name || !*name) return 0;
463 pass = getpwnam(name);
471 /* turn a group name into a gid */
472 int name_to_gid(char *name, gid_t *gid)
475 if (!name || !*name) return 0;
476 grp = getgrnam(name);
485 /* lock a byte range in a open file */
486 int lock_range(int fd, int offset, int len)
490 lock.l_type = F_WRLCK;
491 lock.l_whence = SEEK_SET;
492 lock.l_start = offset;
496 return fcntl(fd,F_SETLK,&lock) == 0;
500 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
502 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
504 argv[*argc] = strdup(s);
508 extern int sanitize_paths;
514 argv[*argc] = strdup(s);
515 if (sanitize_paths) {
516 sanitize_path(argv[*argc], NULL);
519 memset(&globbuf, 0, sizeof(globbuf));
520 glob(argv[*argc], 0, NULL, &globbuf);
521 if (globbuf.gl_pathc == 0) {
526 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
527 if (i == 0) free(argv[*argc]);
528 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
529 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
536 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
538 char *s = argv[*argc];
542 if (!s || !*s) return;
544 if (strncmp(s, base, strlen(base)) == 0) {
549 if (!s) out_of_memory("glob_expand");
551 base = (char *)malloc(strlen(base1)+3);
552 if (!base) out_of_memory("glob_expand");
554 sprintf(base," %s/", base1);
557 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
558 /* split it at this point */
560 glob_expand_one(q, argv, argc, maxargs);
564 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
570 /*******************************************************************
571 convert a string to lower case
572 ********************************************************************/
573 void strlower(char *s)
576 if (isupper(*s)) *s = tolower(*s);
581 /* this is like vsnprintf but it always null terminates, so you
582 can fit at most n-1 chars in */
583 int vslprintf(char *str, int n, const char *format, va_list ap)
585 int ret = vsnprintf(str, n, format, ap);
586 if (ret >= n || ret < 0) {
595 /* like snprintf but always null terminates */
596 int slprintf(char *str, int n, char *format, ...)
601 va_start(ap, format);
602 ret = vslprintf(str,n,format,ap);
608 void *Realloc(void *p, int size)
610 if (!p) return (void *)malloc(size);
611 return (void *)realloc(p, size);
615 void clean_fname(char *name)
626 if ((p=strstr(name,"/./"))) {
634 if ((p=strstr(name,"//"))) {
642 if (strncmp(p=name,"./",2) == 0) {
650 if (l > 1 && p[l-1] == '/') {
658 * Make path appear as if a chroot had occurred:
659 * 1. remove leading "/" (or replace with "." if at end)
660 * 2. remove leading ".." components (except those allowed by "reldir")
661 * 3. delete any other "<dir>/.." (recursively)
662 * Can only shrink paths, so sanitizes in place.
663 * While we're at it, remove double slashes and "." components like
664 * clean_fname does(), but DON'T remove a trailing slash because that
665 * is sometimes significant on command line arguments.
666 * If "reldir" is non-null, it is a sanitized directory that the path will be
667 * relative to, so allow as many ".." at the beginning of the path as
668 * there are components in reldir. This is used for symbolic link targets.
669 * If reldir is non-null and the path began with "/", to be completely like
670 * a chroot we should add in depth levels of ".." at the beginning of the
671 * path, but that would blow the assumption that the path doesn't grow and
672 * it is not likely to end up being a valid symlink anyway, so just do
673 * the normal removal of the leading "/" instead.
674 * Contributed by Dave Dykstra <dwd@bell-labs.com>
677 void sanitize_path(char *p, char *reldir)
686 if (*reldir++ == '/') {
694 /* remove leading slashes */
698 /* this loop iterates once per filename component in p.
699 * both p (and sanp if the original had a slash) should
700 * always be left pointing after a slash
702 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
703 /* skip "." component */
704 while (*++p == '/') {
705 /* skip following slashes */
711 if ((*p == '.') && (*(p+1) == '.') &&
712 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
713 /* ".." component followed by slash or end */
714 if ((depth > 0) && (sanp == start)) {
715 /* allow depth levels of .. at the beginning */
723 /* back up sanp one level */
724 --sanp; /* now pointing at slash */
725 while ((sanp > start) && (*(sanp - 1) != '/')) {
726 /* skip back up to slash */
734 /* copy one component through next slash */
736 if ((*p == '\0') || (*(p-1) == '/')) {
738 /* skip multiple slashes */
745 /* move the virtual beginning to leave the .. alone */
749 if ((sanp == start) && !allowdotdot) {
750 /* ended up with nothing, so put in "." component */
752 * note that the !allowdotdot doesn't prevent this from
753 * happening in all allowed ".." situations, but I didn't
754 * think it was worth putting in an extra variable to ensure
755 * it since an extra "." won't hurt in those situations.
763 static char curr_dir[MAXPATHLEN];
765 /* like chdir() but can be reversed with pop_dir() if save is set. It
766 is also much faster as it remembers where we have been */
767 char *push_dir(char *dir, int save)
769 char *ret = curr_dir;
770 static int initialised;
774 getcwd(curr_dir, sizeof(curr_dir)-1);
777 if (!dir) return NULL; /* this call was probably just to initialize */
779 if (chdir(dir)) return NULL;
782 ret = strdup(curr_dir);
786 strlcpy(curr_dir, dir, sizeof(curr_dir));
788 strlcat(curr_dir,"/", sizeof(curr_dir));
789 strlcat(curr_dir,dir, sizeof(curr_dir));
792 clean_fname(curr_dir);
797 /* reverse a push_dir call */
798 int pop_dir(char *dir)
808 strlcpy(curr_dir, dir, sizeof(curr_dir));
815 /* we need to supply our own strcmp function for file list comparisons
816 to ensure that signed/unsigned usage is consistent between machines. */
817 int u_strcmp(const char *cs1, const char *cs2)
819 const uchar *s1 = (const uchar *)cs1;
820 const uchar *s2 = (const uchar *)cs2;
822 while (*s1 && *s2 && (*s1 == *s2)) {
826 return (int)*s1 - (int)*s2;
829 static OFF_T last_ofs;
831 void end_progress(OFF_T size)
833 extern int do_progress, am_server;
835 if (do_progress && !am_server) {
836 rprintf(FINFO,"%.0f (100%%)\n", (double)size);
841 void show_progress(OFF_T ofs, OFF_T size)
843 extern int do_progress, am_server;
845 if (do_progress && !am_server) {
846 if (ofs > last_ofs + 1000) {
847 int pct = (int)((100.0*ofs)/size);
848 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
854 /* determine if a symlink points outside the current directory tree */
855 int unsafe_symlink(char *dest, char *src)
860 /* all absolute and null symlinks are unsafe */
861 if (!dest || !(*dest) || (*dest == '/')) return 1;
864 if (!src) out_of_memory("unsafe_symlink");
866 /* find out what our safety margin is */
867 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
868 if (strcmp(tok,"..") == 0) {
870 } else if (strcmp(tok,".") == 0) {
878 /* drop by one to account for the filename portion */
882 if (!dest) out_of_memory("unsafe_symlink");
884 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
885 if (strcmp(tok,"..") == 0) {
887 } else if (strcmp(tok,".") == 0) {
892 /* if at any point we go outside the current directory then
893 stop - it is unsafe */
894 if (depth < 0) break;
902 /****************************************************************************
903 return the date and time as a string
904 ****************************************************************************/
905 char *timestring(time_t t)
907 static char TimeBuf[200];
908 struct tm *tm = localtime(&t);
911 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
913 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
916 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
917 TimeBuf[strlen(TimeBuf)-1] = 0;
924 /****************************************************************************
925 like waitpid but does the WEXITSTATUS
926 ****************************************************************************/
928 #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
930 void wait_process(pid_t pid, int *status)
932 waitpid(pid, status, 0);
933 *status = WEXITSTATUS(*status);