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 || fd_pair(from_child_pipe) < 0) {
105 rprintf(FERROR, "pipe: %s\n", strerror(errno));
106 exit_cleanup(RERR_IPC);
112 rprintf(FERROR, "fork: %s\n", strerror(errno));
113 exit_cleanup(RERR_IPC);
117 extern int orig_umask;
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",
124 exit_cleanup(RERR_IPC);
126 if (to_child_pipe[0] != STDIN_FILENO)
127 close(to_child_pipe[0]);
128 if (from_child_pipe[1] != STDOUT_FILENO)
129 close(from_child_pipe[1]);
131 set_blocking(STDIN_FILENO);
133 set_blocking(STDOUT_FILENO);
135 execvp(command[0], command);
136 rprintf(FERROR, "Failed to exec %s : %s\n",
137 command[0], strerror(errno));
138 exit_cleanup(RERR_IPC);
141 if (close(from_child_pipe[1]) < 0 || 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;
176 am_sender = read_batch ? 0 : !am_sender;
179 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
180 close(to_child_pipe[1]) < 0 ||
181 close(from_child_pipe[0]) < 0 ||
182 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
183 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
184 exit_cleanup(RERR_IPC);
186 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
187 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
188 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
191 if (close(from_child_pipe[1]) < 0 ||
192 close(to_child_pipe[0]) < 0) {
193 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
194 exit_cleanup(RERR_IPC);
197 *f_in = from_child_pipe[0];
198 *f_out = to_child_pipe[1];
205 void out_of_memory(char *str)
207 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
208 exit_cleanup(RERR_MALLOC);
211 void overflow(char *str)
213 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
214 exit_cleanup(RERR_MALLOC);
219 int set_modtime(char *fname,time_t modtime)
222 if (dry_run) return 0;
226 tbuf.actime = time(NULL);
227 tbuf.modtime = modtime;
228 return utime(fname,&tbuf);
229 #elif defined(HAVE_UTIME)
233 return utime(fname,t);
236 t[0].tv_sec = time(NULL);
238 t[1].tv_sec = modtime;
240 return utimes(fname,t);
246 /****************************************************************************
247 create any necessary directories in fname. Unfortunately we don't know
248 what perms to give the directory when this is called so we need to rely
250 ****************************************************************************/
251 int create_directory_path(char *fname)
253 extern int orig_umask;
256 while (*fname == '/') fname++;
257 while (strncmp(fname,"./",2)==0) fname += 2;
260 while ((p=strchr(p,'/'))) {
262 do_mkdir(fname,0777 & ~orig_umask);
270 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
271 Return LEN upon success, write's (negative) error code otherwise.
273 derived from GNU C's cccp.c.
275 static int full_write(int desc, char *ptr, size_t len)
281 int written = write (desc, ptr, len);
289 total_written += written;
293 return total_written;
296 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
297 Return the actual number of bytes read, zero for EOF, or negative
300 derived from GNU C's cccp.c. */
301 static int safe_read(int desc, char *ptr, size_t len)
310 n_chars = read(desc, ptr, len);
311 } while (n_chars < 0 && errno == EINTR);
313 n_chars = read(desc, ptr, len);
320 /* copy a file - this is used in conjunction with the --temp-dir option */
321 int copy_file(char *source, char *dest, mode_t mode)
326 int len; /* Number of bytes read into `buf'. */
328 ifd = do_open(source, O_RDONLY, 0);
330 rprintf(FERROR,"open %s: %s\n",
331 source,strerror(errno));
335 if (robust_unlink(dest) && errno != ENOENT) {
336 rprintf(FERROR,"unlink %s: %s\n",
337 dest,strerror(errno));
341 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
343 rprintf(FERROR,"open %s: %s\n",
344 dest,strerror(errno));
349 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
350 if (full_write(ofd, buf, len) < 0) {
351 rprintf(FERROR,"write %s: %s\n",
352 dest,strerror(errno));
363 rprintf(FERROR,"read %s: %s\n",
364 source,strerror(errno));
372 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
373 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
374 will shuffle the filenames around a bit as long as the file is still
375 busy; this is because this function does not know if the unlink call
376 is due to a new file coming in, or --delete trying to remove old
377 .rsyncNNN files, hence it renames it each time.
379 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
380 #define MAX_RENAMES_DIGITS 3
381 #define MAX_RENAMES 1000
383 int robust_unlink(char *fname)
386 return do_unlink(fname);
388 static int counter = 1;
390 char path[MAXPATHLEN];
392 rc = do_unlink(fname);
393 if ((rc == 0) || (errno != ETXTBSY))
396 strlcpy(path, fname, MAXPATHLEN);
399 while((path[--pos] != '/') && (pos >= 0))
402 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
403 pos += sizeof(".rsync")-1;
405 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
410 /* start where the last one left off to reduce chance of clashes */
413 sprintf(&path[pos], "%03d", counter);
414 if (++counter >= MAX_RENAMES)
416 } while (((rc = access(path, 0)) == 0) && (counter != start));
419 rprintf(FINFO,"renaming %s to %s because of text busy\n",
422 /* maybe we should return rename()'s exit status? Nah. */
423 if (do_rename(fname, path) != 0) {
431 int robust_rename(char *from, char *to)
434 return do_rename(from, to);
436 int rc = do_rename(from, to);
437 if ((rc == 0) || (errno != ETXTBSY))
439 if (robust_unlink(to) != 0)
441 return do_rename(from, to);
446 static pid_t all_pids[10];
449 /* fork and record the pid of the child */
452 pid_t newpid = fork();
455 all_pids[num_pids++] = newpid;
460 /* kill all children */
461 void kill_all(int sig)
464 for (i=0;i<num_pids;i++) {
465 if (all_pids[i] != getpid())
466 kill(all_pids[i], sig);
470 /* turn a user name into a uid */
471 int name_to_uid(char *name, uid_t *uid)
474 if (!name || !*name) return 0;
475 pass = getpwnam(name);
483 /* turn a group name into a gid */
484 int name_to_gid(char *name, gid_t *gid)
487 if (!name || !*name) return 0;
488 grp = getgrnam(name);
497 /* lock a byte range in a open file */
498 int lock_range(int fd, int offset, int len)
502 lock.l_type = F_WRLCK;
503 lock.l_whence = SEEK_SET;
504 lock.l_start = offset;
508 return fcntl(fd,F_SETLK,&lock) == 0;
512 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
514 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
516 argv[*argc] = strdup(s);
520 extern int sanitize_paths;
526 argv[*argc] = strdup(s);
527 if (sanitize_paths) {
528 sanitize_path(argv[*argc], NULL);
531 memset(&globbuf, 0, sizeof(globbuf));
532 glob(argv[*argc], 0, NULL, &globbuf);
533 if (globbuf.gl_pathc == 0) {
538 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
539 if (i == 0) free(argv[*argc]);
540 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
541 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
548 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
550 char *s = argv[*argc];
554 if (!s || !*s) return;
556 if (strncmp(s, base, strlen(base)) == 0) {
561 if (!s) out_of_memory("glob_expand");
563 if (asprintf(&base," %s/", base1) <= 0) out_of_memory("glob_expand");
566 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
567 /* split it at this point */
569 glob_expand_one(q, argv, argc, maxargs);
573 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
579 /*******************************************************************
580 convert a string to lower case
581 ********************************************************************/
582 void strlower(char *s)
585 if (isupper(*s)) *s = tolower(*s);
590 void *Realloc(void *p, int size)
592 if (!p) return (void *)malloc(size);
593 return (void *)realloc(p, size);
597 void clean_fname(char *name)
608 if ((p=strstr(name,"/./"))) {
616 if ((p=strstr(name,"//"))) {
624 if (strncmp(p=name,"./",2) == 0) {
632 if (l > 1 && p[l-1] == '/') {
640 * Make path appear as if a chroot had occurred:
641 * 1. remove leading "/" (or replace with "." if at end)
642 * 2. remove leading ".." components (except those allowed by "reldir")
643 * 3. delete any other "<dir>/.." (recursively)
644 * Can only shrink paths, so sanitizes in place.
645 * While we're at it, remove double slashes and "." components like
646 * clean_fname does(), but DON'T remove a trailing slash because that
647 * is sometimes significant on command line arguments.
648 * If "reldir" is non-null, it is a sanitized directory that the path will be
649 * relative to, so allow as many ".." at the beginning of the path as
650 * there are components in reldir. This is used for symbolic link targets.
651 * If reldir is non-null and the path began with "/", to be completely like
652 * a chroot we should add in depth levels of ".." at the beginning of the
653 * path, but that would blow the assumption that the path doesn't grow and
654 * it is not likely to end up being a valid symlink anyway, so just do
655 * the normal removal of the leading "/" instead.
656 * Contributed by Dave Dykstra <dwd@bell-labs.com>
659 void sanitize_path(char *p, char *reldir)
668 if (*reldir++ == '/') {
676 /* remove leading slashes */
680 /* this loop iterates once per filename component in p.
681 * both p (and sanp if the original had a slash) should
682 * always be left pointing after a slash
684 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
685 /* skip "." component */
686 while (*++p == '/') {
687 /* skip following slashes */
693 if ((*p == '.') && (*(p+1) == '.') &&
694 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
695 /* ".." component followed by slash or end */
696 if ((depth > 0) && (sanp == start)) {
697 /* allow depth levels of .. at the beginning */
705 /* back up sanp one level */
706 --sanp; /* now pointing at slash */
707 while ((sanp > start) && (*(sanp - 1) != '/')) {
708 /* skip back up to slash */
716 /* copy one component through next slash */
718 if ((*p == '\0') || (*(p-1) == '/')) {
720 /* skip multiple slashes */
727 /* move the virtual beginning to leave the .. alone */
731 if ((sanp == start) && !allowdotdot) {
732 /* ended up with nothing, so put in "." component */
734 * note that the !allowdotdot doesn't prevent this from
735 * happening in all allowed ".." situations, but I didn't
736 * think it was worth putting in an extra variable to ensure
737 * it since an extra "." won't hurt in those situations.
745 static char curr_dir[MAXPATHLEN];
747 /* like chdir() but can be reversed with pop_dir() if save is set. It
748 is also much faster as it remembers where we have been */
749 char *push_dir(char *dir, int save)
751 char *ret = curr_dir;
752 static int initialised;
756 getcwd(curr_dir, sizeof(curr_dir)-1);
759 if (!dir) return NULL; /* this call was probably just to initialize */
761 if (chdir(dir)) return NULL;
764 ret = strdup(curr_dir);
768 strlcpy(curr_dir, dir, sizeof(curr_dir));
770 strlcat(curr_dir,"/", sizeof(curr_dir));
771 strlcat(curr_dir,dir, sizeof(curr_dir));
774 clean_fname(curr_dir);
779 /* reverse a push_dir call */
780 int pop_dir(char *dir)
790 strlcpy(curr_dir, dir, sizeof(curr_dir));
797 /* we need to supply our own strcmp function for file list comparisons
798 to ensure that signed/unsigned usage is consistent between machines. */
799 int u_strcmp(const char *cs1, const char *cs2)
801 const uchar *s1 = (const uchar *)cs1;
802 const uchar *s2 = (const uchar *)cs2;
804 while (*s1 && *s2 && (*s1 == *s2)) {
808 return (int)*s1 - (int)*s2;
811 static OFF_T last_ofs;
812 static struct timeval print_time;
813 static struct timeval start_time;
814 static OFF_T start_ofs;
816 static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
818 return (t2->tv_sec - t1->tv_sec) * 1000
819 + (t2->tv_usec - t1->tv_usec) / 1000;
824 * @param ofs Current position in file
825 * @param size Total size of file
826 * @param is_last True if this is the last time progress will be
827 * printed for this file, so we should output a newline. (Not
828 * necessarily the same as all bytes being received.)
830 static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
833 int pct = (ofs == size) ? 100 : (int)((100.0*ofs)/size);
834 unsigned long diff = msdiff(&start_time, now);
835 double rate = diff ? (double) (ofs-start_ofs) * 1000.0 / diff / 1024.0 : 0;
837 double remain = rate ? (double) (size-ofs) / rate / 1000.0: 0.0;
838 int remain_h, remain_m, remain_s;
840 if (rate > 1024*1024) {
841 rate /= 1024.0 * 1024.0;
843 } else if (rate > 1024) {
850 remain_s = (int) remain % 60;
851 remain_m = (int) (remain / 60.0) % 60;
852 remain_h = (int) (remain / 3600.0);
854 rprintf(FINFO, "%12.0f %3d%% %7.2f%s %4d:%02d:%02d%s",
855 (double) ofs, pct, rate, units,
856 remain_h, remain_m, remain_s,
857 is_last ? "\n" : "\r");
860 void end_progress(OFF_T size)
862 extern int do_progress, am_server;
864 if (do_progress && !am_server) {
866 gettimeofday(&now, NULL);
867 rprint_progress(size, size, &now, True);
871 print_time.tv_sec = print_time.tv_usec = 0;
872 start_time.tv_sec = start_time.tv_usec = 0;
875 void show_progress(OFF_T ofs, OFF_T size)
877 extern int do_progress, am_server;
880 gettimeofday(&now, NULL);
882 if (!start_time.tv_sec && !start_time.tv_usec) {
883 start_time.tv_sec = now.tv_sec;
884 start_time.tv_usec = now.tv_usec;
890 && ofs > last_ofs + 1000
891 && msdiff(&print_time, &now) > 250) {
892 rprint_progress(ofs, size, &now, False);
894 print_time.tv_sec = now.tv_sec;
895 print_time.tv_usec = now.tv_usec;
899 /* determine if a symlink points outside the current directory tree */
900 int unsafe_symlink(char *dest, char *src)
905 /* all absolute and null symlinks are unsafe */
906 if (!dest || !(*dest) || (*dest == '/')) return 1;
909 if (!src) out_of_memory("unsafe_symlink");
911 /* find out what our safety margin is */
912 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
913 if (strcmp(tok,"..") == 0) {
915 } else if (strcmp(tok,".") == 0) {
923 /* drop by one to account for the filename portion */
927 if (!dest) out_of_memory("unsafe_symlink");
929 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
930 if (strcmp(tok,"..") == 0) {
932 } else if (strcmp(tok,".") == 0) {
937 /* if at any point we go outside the current directory then
938 stop - it is unsafe */
939 if (depth < 0) break;
947 /****************************************************************************
948 return the date and time as a string
949 ****************************************************************************/
950 char *timestring(time_t t)
952 static char TimeBuf[200];
953 struct tm *tm = localtime(&t);
956 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
958 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
961 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
962 TimeBuf[strlen(TimeBuf)-1] = 0;
970 * Sleep for a specified number of milliseconds.
972 * Always returns TRUE. (In the future it might return FALSE if
978 struct timeval tval,t1,t2;
980 gettimeofday(&t1, NULL);
981 gettimeofday(&t2, NULL);
984 tval.tv_sec = (t-tdiff)/1000;
985 tval.tv_usec = 1000*((t-tdiff)%1000);
988 select(0,NULL,NULL, NULL, &tval);
990 gettimeofday(&t2, NULL);
991 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
992 (t2.tv_usec - t1.tv_usec)/1000;
999 /*******************************************************************
1000 Determine if two file modification times are equivalent (either exact
1001 or in the modification timestamp window established by --modify-window)
1002 Returns 0 if the times should be treated as the same, 1 if the
1003 first is later and -1 if the 2nd is later
1004 *******************************************************************/
1005 int cmp_modtime(time_t file1, time_t file2)
1007 extern int modify_window;
1009 if (file2 > file1) {
1010 if (file2 - file1 <= modify_window) return 0;
1013 if (file1 - file2 <= modify_window) return 0;
1021 /*******************************************************************
1022 This routine is a trick to immediately catch errors when debugging
1023 with insure. A xterm with a gdb is popped up when insure catches
1024 a error. It is Linux specific.
1025 ********************************************************************/
1026 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
1032 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'",
1033 getpid(), getpid(), getpid());
1037 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
1038 fn = dlsym(h, "_Insure_trap_error");
1041 ret = fn(a1, a2, a3, a4, a5, a6);