1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
5 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 Utilities used in rsync
32 /****************************************************************************
33 Set a fd into nonblocking mode
34 ****************************************************************************/
35 void set_nonblocking(int fd)
39 if((val = fcntl(fd, F_GETFL, 0)) == -1)
41 if (!(val & NONBLOCK_FLAG)) {
43 fcntl(fd, F_SETFL, val);
47 /****************************************************************************
48 Set a fd into blocking mode
49 ****************************************************************************/
50 void set_blocking(int fd)
54 if((val = fcntl(fd, F_GETFL, 0)) == -1)
56 if (val & NONBLOCK_FLAG) {
57 val &= ~NONBLOCK_FLAG;
58 fcntl(fd, F_SETFL, val);
63 /* create a file descriptor pair - like pipe() but use socketpair if
64 possible (because of blocking issues on pipes)
66 always set non-blocking
68 int fd_pair(int fd[2])
73 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
79 set_nonblocking(fd[0]);
80 set_nonblocking(fd[1]);
87 /* this is derived from CVS code
89 note that in the child STDIN is set to blocking and STDOUT
90 is set to non-blocking. This is necessary as rsh relies on stdin being blocking
91 and ssh relies on stdout being non-blocking
93 if blocking_io is set then use blocking io on both fds. That can be
94 used to cope with badly broken rsh implementations like the one on
97 pid_t piped_child(char **command,int *f_in,int *f_out)
100 int to_child_pipe[2];
101 int from_child_pipe[2];
102 extern int blocking_io;
104 if (fd_pair(to_child_pipe) < 0 ||
105 fd_pair(from_child_pipe) < 0) {
106 rprintf(FERROR,"pipe: %s\n",strerror(errno));
107 exit_cleanup(RERR_IPC);
113 rprintf(FERROR,"fork: %s\n",strerror(errno));
114 exit_cleanup(RERR_IPC);
119 extern int orig_umask;
120 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
121 close(to_child_pipe[1]) < 0 ||
122 close(from_child_pipe[0]) < 0 ||
123 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
124 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
125 exit_cleanup(RERR_IPC);
127 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
128 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
130 set_blocking(STDIN_FILENO);
132 set_blocking(STDOUT_FILENO);
134 execvp(command[0], command);
135 rprintf(FERROR,"Failed to exec %s : %s\n",
136 command[0],strerror(errno));
137 exit_cleanup(RERR_IPC);
140 if (close(from_child_pipe[1]) < 0 ||
141 close(to_child_pipe[0]) < 0) {
142 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
143 exit_cleanup(RERR_IPC);
146 *f_in = from_child_pipe[0];
147 *f_out = to_child_pipe[1];
152 pid_t local_child(int argc, char **argv,int *f_in,int *f_out)
155 int to_child_pipe[2];
156 int from_child_pipe[2];
157 extern int read_batch; /* dw */
159 if (fd_pair(to_child_pipe) < 0 ||
160 fd_pair(from_child_pipe) < 0) {
161 rprintf(FERROR,"pipe: %s\n",strerror(errno));
162 exit_cleanup(RERR_IPC);
168 rprintf(FERROR,"fork: %s\n",strerror(errno));
169 exit_cleanup(RERR_IPC);
173 extern int am_sender;
174 extern int am_server;
179 am_sender = !am_sender;
182 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
183 close(to_child_pipe[1]) < 0 ||
184 close(from_child_pipe[0]) < 0 ||
185 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
186 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
187 exit_cleanup(RERR_IPC);
189 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
190 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
191 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
194 if (close(from_child_pipe[1]) < 0 ||
195 close(to_child_pipe[0]) < 0) {
196 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
197 exit_cleanup(RERR_IPC);
200 *f_in = from_child_pipe[0];
201 *f_out = to_child_pipe[1];
208 void out_of_memory(char *str)
210 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
211 exit_cleanup(RERR_MALLOC);
214 void overflow(char *str)
216 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
217 exit_cleanup(RERR_MALLOC);
222 int set_modtime(char *fname,time_t modtime)
225 if (dry_run) return 0;
229 tbuf.actime = time(NULL);
230 tbuf.modtime = modtime;
231 return utime(fname,&tbuf);
232 #elif defined(HAVE_UTIME)
236 return utime(fname,t);
239 t[0].tv_sec = time(NULL);
241 t[1].tv_sec = modtime;
243 return utimes(fname,t);
249 /****************************************************************************
250 create any necessary directories in fname. Unfortunately we don't know
251 what perms to give the directory when this is called so we need to rely
253 ****************************************************************************/
254 int create_directory_path(char *fname)
256 extern int orig_umask;
259 while (*fname == '/') fname++;
260 while (strncmp(fname,"./",2)==0) fname += 2;
263 while ((p=strchr(p,'/'))) {
265 do_mkdir(fname,0777 & ~orig_umask);
273 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
274 Return LEN upon success, write's (negative) error code otherwise.
276 derived from GNU C's cccp.c.
278 static int full_write(int desc, char *ptr, size_t len)
284 int written = write (desc, ptr, len);
292 total_written += written;
296 return total_written;
299 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
300 Return the actual number of bytes read, zero for EOF, or negative
303 derived from GNU C's cccp.c. */
304 static int safe_read(int desc, char *ptr, size_t len)
313 n_chars = read(desc, ptr, len);
314 } while (n_chars < 0 && errno == EINTR);
316 n_chars = read(desc, ptr, len);
323 /* copy a file - this is used in conjunction with the --temp-dir option */
324 int copy_file(char *source, char *dest, mode_t mode)
329 int len; /* Number of bytes read into `buf'. */
331 ifd = do_open(source, O_RDONLY, 0);
333 rprintf(FERROR,"open %s: %s\n",
334 source,strerror(errno));
338 if (robust_unlink(dest) && errno != ENOENT) {
339 rprintf(FERROR,"unlink %s: %s\n",
340 dest,strerror(errno));
344 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
346 rprintf(FERROR,"open %s: %s\n",
347 dest,strerror(errno));
352 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
353 if (full_write(ofd, buf, len) < 0) {
354 rprintf(FERROR,"write %s: %s\n",
355 dest,strerror(errno));
366 rprintf(FERROR,"read %s: %s\n",
367 source,strerror(errno));
375 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
376 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
377 will shuffle the filenames around a bit as long as the file is still
378 busy; this is because this function does not know if the unlink call
379 is due to a new file coming in, or --delete trying to remove old
380 .rsyncNNN files, hence it renames it each time.
382 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
383 #define MAX_RENAMES_DIGITS 3
384 #define MAX_RENAMES 1000
386 int robust_unlink(char *fname)
389 return do_unlink(fname);
391 static int counter = 1;
393 char path[MAXPATHLEN];
395 rc = do_unlink(fname);
396 if ((rc == 0) || (errno != ETXTBSY))
399 strlcpy(path, fname, MAXPATHLEN);
402 while((path[--pos] != '/') && (pos >= 0))
405 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
406 pos += sizeof(".rsync")-1;
408 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
413 /* start where the last one left off to reduce chance of clashes */
416 sprintf(&path[pos], "%03d", counter);
417 if (++counter >= MAX_RENAMES)
419 } while (((rc = access(path, 0)) == 0) && (counter != start));
422 rprintf(FINFO,"renaming %s to %s because of text busy\n",
425 /* maybe we should return rename()'s exit status? Nah. */
426 if (do_rename(fname, path) != 0) {
434 int robust_rename(char *from, char *to)
437 return do_rename(from, to);
439 int rc = do_rename(from, to);
440 if ((rc == 0) || (errno != ETXTBSY))
442 if (robust_unlink(to) != 0)
444 return do_rename(from, to);
449 static pid_t all_pids[10];
452 /* fork and record the pid of the child */
455 pid_t newpid = fork();
458 all_pids[num_pids++] = newpid;
463 /* kill all children */
464 void kill_all(int sig)
467 for (i=0;i<num_pids;i++) {
468 if (all_pids[i] != getpid())
469 kill(all_pids[i], sig);
473 /* turn a user name into a uid */
474 int name_to_uid(char *name, uid_t *uid)
477 if (!name || !*name) return 0;
478 pass = getpwnam(name);
486 /* turn a group name into a gid */
487 int name_to_gid(char *name, gid_t *gid)
490 if (!name || !*name) return 0;
491 grp = getgrnam(name);
500 /* lock a byte range in a open file */
501 int lock_range(int fd, int offset, int len)
505 lock.l_type = F_WRLCK;
506 lock.l_whence = SEEK_SET;
507 lock.l_start = offset;
511 return fcntl(fd,F_SETLK,&lock) == 0;
515 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
517 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
519 argv[*argc] = strdup(s);
523 extern int sanitize_paths;
529 argv[*argc] = strdup(s);
530 if (sanitize_paths) {
531 sanitize_path(argv[*argc], NULL);
534 memset(&globbuf, 0, sizeof(globbuf));
535 glob(argv[*argc], 0, NULL, &globbuf);
536 if (globbuf.gl_pathc == 0) {
541 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
542 if (i == 0) free(argv[*argc]);
543 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
544 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
551 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
553 char *s = argv[*argc];
557 if (!s || !*s) return;
559 if (strncmp(s, base, strlen(base)) == 0) {
564 if (!s) out_of_memory("glob_expand");
566 if (asprintf(&base," %s/", base1) <= 0) out_of_memory("glob_expand");
569 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
570 /* split it at this point */
572 glob_expand_one(q, argv, argc, maxargs);
576 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
582 /*******************************************************************
583 convert a string to lower case
584 ********************************************************************/
585 void strlower(char *s)
588 if (isupper(*s)) *s = tolower(*s);
593 void *Realloc(void *p, int size)
595 if (!p) return (void *)malloc(size);
596 return (void *)realloc(p, size);
600 void clean_fname(char *name)
611 if ((p=strstr(name,"/./"))) {
619 if ((p=strstr(name,"//"))) {
627 if (strncmp(p=name,"./",2) == 0) {
635 if (l > 1 && p[l-1] == '/') {
643 * Make path appear as if a chroot had occurred:
644 * 1. remove leading "/" (or replace with "." if at end)
645 * 2. remove leading ".." components (except those allowed by "reldir")
646 * 3. delete any other "<dir>/.." (recursively)
647 * Can only shrink paths, so sanitizes in place.
648 * While we're at it, remove double slashes and "." components like
649 * clean_fname does(), but DON'T remove a trailing slash because that
650 * is sometimes significant on command line arguments.
651 * If "reldir" is non-null, it is a sanitized directory that the path will be
652 * relative to, so allow as many ".." at the beginning of the path as
653 * there are components in reldir. This is used for symbolic link targets.
654 * If reldir is non-null and the path began with "/", to be completely like
655 * a chroot we should add in depth levels of ".." at the beginning of the
656 * path, but that would blow the assumption that the path doesn't grow and
657 * it is not likely to end up being a valid symlink anyway, so just do
658 * the normal removal of the leading "/" instead.
659 * Contributed by Dave Dykstra <dwd@bell-labs.com>
662 void sanitize_path(char *p, char *reldir)
671 if (*reldir++ == '/') {
679 /* remove leading slashes */
683 /* this loop iterates once per filename component in p.
684 * both p (and sanp if the original had a slash) should
685 * always be left pointing after a slash
687 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
688 /* skip "." component */
689 while (*++p == '/') {
690 /* skip following slashes */
696 if ((*p == '.') && (*(p+1) == '.') &&
697 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
698 /* ".." component followed by slash or end */
699 if ((depth > 0) && (sanp == start)) {
700 /* allow depth levels of .. at the beginning */
708 /* back up sanp one level */
709 --sanp; /* now pointing at slash */
710 while ((sanp > start) && (*(sanp - 1) != '/')) {
711 /* skip back up to slash */
719 /* copy one component through next slash */
721 if ((*p == '\0') || (*(p-1) == '/')) {
723 /* skip multiple slashes */
730 /* move the virtual beginning to leave the .. alone */
734 if ((sanp == start) && !allowdotdot) {
735 /* ended up with nothing, so put in "." component */
737 * note that the !allowdotdot doesn't prevent this from
738 * happening in all allowed ".." situations, but I didn't
739 * think it was worth putting in an extra variable to ensure
740 * it since an extra "." won't hurt in those situations.
748 static char curr_dir[MAXPATHLEN];
750 /* like chdir() but can be reversed with pop_dir() if save is set. It
751 is also much faster as it remembers where we have been */
752 char *push_dir(char *dir, int save)
754 char *ret = curr_dir;
755 static int initialised;
759 getcwd(curr_dir, sizeof(curr_dir)-1);
762 if (!dir) return NULL; /* this call was probably just to initialize */
764 if (chdir(dir)) return NULL;
767 ret = strdup(curr_dir);
771 strlcpy(curr_dir, dir, sizeof(curr_dir));
773 strlcat(curr_dir,"/", sizeof(curr_dir));
774 strlcat(curr_dir,dir, sizeof(curr_dir));
777 clean_fname(curr_dir);
782 /* reverse a push_dir call */
783 int pop_dir(char *dir)
793 strlcpy(curr_dir, dir, sizeof(curr_dir));
800 /* we need to supply our own strcmp function for file list comparisons
801 to ensure that signed/unsigned usage is consistent between machines. */
802 int u_strcmp(const char *cs1, const char *cs2)
804 const uchar *s1 = (const uchar *)cs1;
805 const uchar *s2 = (const uchar *)cs2;
807 while (*s1 && *s2 && (*s1 == *s2)) {
811 return (int)*s1 - (int)*s2;
814 static OFF_T last_ofs;
815 static struct timeval print_time;
816 static struct timeval start_time;
817 static OFF_T start_ofs;
819 static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
821 return (t2->tv_sec - t1->tv_sec) * 1000
822 + (t2->tv_usec - t1->tv_usec) / 1000;
827 * @param ofs Current position in file
828 * @param size Total size of file
829 * @param is_last True if this is the last time progress will be
830 * printed for this file, so we should output a newline. (Not
831 * necessarily the same as all bytes being received.)
833 static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
836 int pct = (ofs == size) ? 100 : (int)((100.0*ofs)/size);
837 unsigned long diff = msdiff(&start_time, now);
838 double rate = diff ? (double) (ofs-start_ofs) * 1000.0 / diff / 1024.0 : 0;
840 double remain = rate ? (double) (size-ofs) / rate / 1000.0: 0.0;
841 int remain_h, remain_m, remain_s;
843 if (rate > 1024*1024) {
844 rate /= 1024.0 * 1024.0;
846 } else if (rate > 1024) {
853 remain_s = (int) remain % 60;
854 remain_m = (int) (remain / 60.0) % 60;
855 remain_h = (int) (remain / 3600.0);
857 rprintf(FINFO, "%12.0f %3d%% %7.2f%s %4d:%02d:%02d%s",
858 (double) ofs, pct, rate, units,
859 remain_h, remain_m, remain_s,
860 is_last ? "\n" : "\r");
863 void end_progress(OFF_T size)
865 extern int do_progress, am_server;
867 if (do_progress && !am_server) {
869 gettimeofday(&now, NULL);
870 rprint_progress(size, size, &now, True);
874 print_time.tv_sec = print_time.tv_usec = 0;
875 start_time.tv_sec = start_time.tv_usec = 0;
878 void show_progress(OFF_T ofs, OFF_T size)
880 extern int do_progress, am_server;
883 gettimeofday(&now, NULL);
885 if (!start_time.tv_sec && !start_time.tv_usec) {
886 start_time.tv_sec = now.tv_sec;
887 start_time.tv_usec = now.tv_usec;
893 && ofs > last_ofs + 1000
894 && msdiff(&print_time, &now) > 250) {
895 rprint_progress(ofs, size, &now, False);
897 print_time.tv_sec = now.tv_sec;
898 print_time.tv_usec = now.tv_usec;
902 /* determine if a symlink points outside the current directory tree */
903 int unsafe_symlink(char *dest, char *src)
908 /* all absolute and null symlinks are unsafe */
909 if (!dest || !(*dest) || (*dest == '/')) return 1;
912 if (!src) out_of_memory("unsafe_symlink");
914 /* find out what our safety margin is */
915 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
916 if (strcmp(tok,"..") == 0) {
918 } else if (strcmp(tok,".") == 0) {
926 /* drop by one to account for the filename portion */
930 if (!dest) out_of_memory("unsafe_symlink");
932 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
933 if (strcmp(tok,"..") == 0) {
935 } else if (strcmp(tok,".") == 0) {
940 /* if at any point we go outside the current directory then
941 stop - it is unsafe */
942 if (depth < 0) break;
950 /****************************************************************************
951 return the date and time as a string
952 ****************************************************************************/
953 char *timestring(time_t t)
955 static char TimeBuf[200];
956 struct tm *tm = localtime(&t);
959 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
961 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
964 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
965 TimeBuf[strlen(TimeBuf)-1] = 0;
973 * Sleep for a specified number of milliseconds.
975 * Always returns TRUE. (In the future it might return FALSE if
981 struct timeval tval,t1,t2;
983 gettimeofday(&t1, NULL);
984 gettimeofday(&t2, NULL);
987 tval.tv_sec = (t-tdiff)/1000;
988 tval.tv_usec = 1000*((t-tdiff)%1000);
991 select(0,NULL,NULL, NULL, &tval);
993 gettimeofday(&t2, NULL);
994 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
995 (t2.tv_usec - t1.tv_usec)/1000;
1002 /*******************************************************************
1003 Determine if two file modification times are equivalent (either exact
1004 or in the modification timestamp window established by --modify-window)
1005 Returns 0 if the times should be treated as the same, 1 if the
1006 first is later and -1 if the 2nd is later
1007 *******************************************************************/
1008 int cmp_modtime(time_t file1, time_t file2)
1010 extern int modify_window;
1012 if (file2 > file1) {
1013 if (file2 - file1 <= modify_window) return 0;
1016 if (file1 - file2 <= modify_window) return 0;
1024 /*******************************************************************
1025 This routine is a trick to immediately catch errors when debugging
1026 with insure. A xterm with a gdb is popped up when insure catches
1027 a error. It is Linux specific.
1028 ********************************************************************/
1029 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
1035 asprintf(&cmd, "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'",
1036 getpid(), getpid(), getpid());
1040 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
1041 fn = dlsym(h, "_Insure_trap_error");
1044 ret = fn(a1, a2, a3, a4, a5, a6);