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];
92 if (fd_pair(to_child_pipe) < 0 ||
93 fd_pair(from_child_pipe) < 0) {
94 rprintf(FERROR,"pipe: %s\n",strerror(errno));
95 exit_cleanup(RERR_IPC);
101 rprintf(FERROR,"fork: %s\n",strerror(errno));
102 exit_cleanup(RERR_IPC);
107 extern int orig_umask;
108 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
109 close(to_child_pipe[1]) < 0 ||
110 close(from_child_pipe[0]) < 0 ||
111 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
112 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
113 exit_cleanup(RERR_IPC);
115 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
116 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
118 set_blocking(STDIN_FILENO);
119 execvp(command[0], command);
120 rprintf(FERROR,"Failed to exec %s : %s\n",
121 command[0],strerror(errno));
122 exit_cleanup(RERR_IPC);
125 if (close(from_child_pipe[1]) < 0 ||
126 close(to_child_pipe[0]) < 0) {
127 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
128 exit_cleanup(RERR_IPC);
131 *f_in = from_child_pipe[0];
132 *f_out = to_child_pipe[1];
137 int local_child(int argc, char **argv,int *f_in,int *f_out)
140 int to_child_pipe[2];
141 int from_child_pipe[2];
143 if (fd_pair(to_child_pipe) < 0 ||
144 fd_pair(from_child_pipe) < 0) {
145 rprintf(FERROR,"pipe: %s\n",strerror(errno));
146 exit_cleanup(RERR_IPC);
152 rprintf(FERROR,"fork: %s\n",strerror(errno));
153 exit_cleanup(RERR_IPC);
157 extern int am_sender;
158 extern int am_server;
160 am_sender = !am_sender;
163 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
164 close(to_child_pipe[1]) < 0 ||
165 close(from_child_pipe[0]) < 0 ||
166 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
167 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
168 exit_cleanup(RERR_IPC);
170 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
171 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
172 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
175 if (close(from_child_pipe[1]) < 0 ||
176 close(to_child_pipe[0]) < 0) {
177 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
178 exit_cleanup(RERR_IPC);
181 *f_in = from_child_pipe[0];
182 *f_out = to_child_pipe[1];
189 void out_of_memory(char *str)
191 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
192 exit_cleanup(RERR_MALLOC);
195 void overflow(char *str)
197 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
198 exit_cleanup(RERR_MALLOC);
203 int set_modtime(char *fname,time_t modtime)
206 if (dry_run) return 0;
210 tbuf.actime = time(NULL);
211 tbuf.modtime = modtime;
212 return utime(fname,&tbuf);
213 #elif defined(HAVE_UTIME)
217 return utime(fname,t);
220 t[0].tv_sec = time(NULL);
222 t[1].tv_sec = modtime;
224 return utimes(fname,t);
230 /****************************************************************************
231 create any necessary directories in fname. Unfortunately we don't know
232 what perms to give the directory when this is called so we need to rely
234 ****************************************************************************/
235 int create_directory_path(char *fname)
237 extern int orig_umask;
240 while (*fname == '/') fname++;
241 while (strncmp(fname,"./",2)==0) fname += 2;
244 while ((p=strchr(p,'/'))) {
246 do_mkdir(fname,0777 & ~orig_umask);
254 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
255 Return LEN upon success, write's (negative) error code otherwise.
257 derived from GNU C's cccp.c.
259 static int full_write(int desc, char *ptr, int len)
265 int written = write (desc, ptr, len);
273 total_written += written;
277 return total_written;
280 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
281 Return the actual number of bytes read, zero for EOF, or negative
284 derived from GNU C's cccp.c. */
285 static int safe_read(int desc, char *ptr, int len)
294 n_chars = read(desc, ptr, len);
295 } while (n_chars < 0 && errno == EINTR);
297 n_chars = read(desc, ptr, len);
304 /* copy a file - this is used in conjunction with the --temp-dir option */
305 int copy_file(char *source, char *dest, mode_t mode)
310 int len; /* Number of bytes read into `buf'. */
312 ifd = do_open(source, O_RDONLY, 0);
314 rprintf(FERROR,"open %s: %s\n",
315 source,strerror(errno));
319 if (robust_unlink(dest) && errno != ENOENT) {
320 rprintf(FERROR,"unlink %s: %s\n",
321 dest,strerror(errno));
325 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
327 rprintf(FERROR,"open %s: %s\n",
328 dest,strerror(errno));
333 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
334 if (full_write(ofd, buf, len) < 0) {
335 rprintf(FERROR,"write %s: %s\n",
336 dest,strerror(errno));
347 rprintf(FERROR,"read %s: %s\n",
348 source,strerror(errno));
356 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
357 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
358 will shuffle the filenames around a bit as long as the file is still
359 busy; this is because this function does not know if the unlink call
360 is due to a new file coming in, or --delete trying to remove old
361 .rsyncNNN files, hence it renames it each time.
363 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
364 #define MAX_RENAMES_DIGITS 3
365 #define MAX_RENAMES 1000
367 int robust_unlink(char *fname)
370 return do_unlink(fname);
372 static int counter = 1;
374 char path[MAXPATHLEN];
376 rc = do_unlink(fname);
377 if ((rc == 0) || (errno != ETXTBSY))
380 strlcpy(path, fname, MAXPATHLEN);
383 while((path[--pos] != '/') && (pos >= 0))
386 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
387 pos += sizeof(".rsync")-1;
389 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
394 /* start where the last one left off to reduce chance of clashes */
397 sprintf(&path[pos], "%03d", counter);
398 if (++counter >= MAX_RENAMES)
400 } while (((rc = access(path, 0)) == 0) && (counter != start));
403 rprintf(FINFO,"renaming %s to %s because of text busy\n",
406 /* maybe we should return rename()'s exit status? Nah. */
407 if (do_rename(fname, path) != 0) {
415 int robust_rename(char *from, char *to)
418 return do_rename(from, to);
420 int rc = do_rename(from, to);
421 if ((rc == 0) || (errno != ETXTBSY))
423 if (robust_unlink(to) != 0)
425 return do_rename(from, to);
430 static pid_t all_pids[10];
433 /* fork and record the pid of the child */
436 pid_t newpid = fork();
439 all_pids[num_pids++] = newpid;
444 /* kill all children */
445 void kill_all(int sig)
448 for (i=0;i<num_pids;i++) {
449 if (all_pids[i] != getpid())
450 kill(all_pids[i], sig);
454 /* turn a user name into a uid */
455 int name_to_uid(char *name, uid_t *uid)
458 if (!name || !*name) return 0;
459 pass = getpwnam(name);
467 /* turn a group name into a gid */
468 int name_to_gid(char *name, gid_t *gid)
471 if (!name || !*name) return 0;
472 grp = getgrnam(name);
481 /* lock a byte range in a open file */
482 int lock_range(int fd, int offset, int len)
486 lock.l_type = F_WRLCK;
487 lock.l_whence = SEEK_SET;
488 lock.l_start = offset;
492 return fcntl(fd,F_SETLK,&lock) == 0;
496 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
498 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
500 argv[*argc] = strdup(s);
504 extern int sanitize_paths;
510 argv[*argc] = strdup(s);
511 if (sanitize_paths) {
512 sanitize_path(argv[*argc], NULL);
515 memset(&globbuf, 0, sizeof(globbuf));
516 glob(argv[*argc], 0, NULL, &globbuf);
517 if (globbuf.gl_pathc == 0) {
522 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
523 if (i == 0) free(argv[*argc]);
524 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
525 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
532 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
534 char *s = argv[*argc];
538 if (!s || !*s) return;
540 if (strncmp(s, base, strlen(base)) == 0) {
545 if (!s) out_of_memory("glob_expand");
547 base = (char *)malloc(strlen(base1)+3);
548 if (!base) out_of_memory("glob_expand");
550 sprintf(base," %s/", base1);
553 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
554 /* split it at this point */
556 glob_expand_one(q, argv, argc, maxargs);
560 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
566 /*******************************************************************
567 convert a string to lower case
568 ********************************************************************/
569 void strlower(char *s)
572 if (isupper(*s)) *s = tolower(*s);
577 /* this is like vsnprintf but it always null terminates, so you
578 can fit at most n-1 chars in */
579 int vslprintf(char *str, int n, const char *format, va_list ap)
581 int ret = vsnprintf(str, n, format, ap);
582 if (ret >= n || ret < 0) {
591 /* like snprintf but always null terminates */
592 int slprintf(char *str, int n, char *format, ...)
597 va_start(ap, format);
598 ret = vslprintf(str,n,format,ap);
604 void *Realloc(void *p, int size)
606 if (!p) return (void *)malloc(size);
607 return (void *)realloc(p, size);
611 void clean_fname(char *name)
622 if ((p=strstr(name,"/./"))) {
630 if ((p=strstr(name,"//"))) {
638 if (strncmp(p=name,"./",2) == 0) {
646 if (l > 1 && p[l-1] == '/') {
654 * Make path appear as if a chroot had occurred:
655 * 1. remove leading "/" (or replace with "." if at end)
656 * 2. remove leading ".." components (except those allowed by "reldir")
657 * 3. delete any other "<dir>/.." (recursively)
658 * Can only shrink paths, so sanitizes in place.
659 * While we're at it, remove double slashes and "." components like
660 * clean_fname does(), but DON'T remove a trailing slash because that
661 * is sometimes significant on command line arguments.
662 * If "reldir" is non-null, it is a sanitized directory that the path will be
663 * relative to, so allow as many ".." at the beginning of the path as
664 * there are components in reldir. This is used for symbolic link targets.
665 * If reldir is non-null and the path began with "/", to be completely like
666 * a chroot we should add in depth levels of ".." at the beginning of the
667 * path, but that would blow the assumption that the path doesn't grow and
668 * it is not likely to end up being a valid symlink anyway, so just do
669 * the normal removal of the leading "/" instead.
670 * Contributed by Dave Dykstra <dwd@bell-labs.com>
673 void sanitize_path(char *p, char *reldir)
682 if (*reldir++ == '/') {
690 /* remove leading slashes */
694 /* this loop iterates once per filename component in p.
695 * both p (and sanp if the original had a slash) should
696 * always be left pointing after a slash
698 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
699 /* skip "." component */
700 while (*++p == '/') {
701 /* skip following slashes */
707 if ((*p == '.') && (*(p+1) == '.') &&
708 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
709 /* ".." component followed by slash or end */
710 if ((depth > 0) && (sanp == start)) {
711 /* allow depth levels of .. at the beginning */
719 /* back up sanp one level */
720 --sanp; /* now pointing at slash */
721 while ((sanp > start) && (*(sanp - 1) != '/')) {
722 /* skip back up to slash */
730 /* copy one component through next slash */
732 if ((*p == '\0') || (*(p-1) == '/')) {
734 /* skip multiple slashes */
741 /* move the virtual beginning to leave the .. alone */
745 if ((sanp == start) && !allowdotdot) {
746 /* ended up with nothing, so put in "." component */
748 * note that the !allowdotdot doesn't prevent this from
749 * happening in all allowed ".." situations, but I didn't
750 * think it was worth putting in an extra variable to ensure
751 * it since an extra "." won't hurt in those situations.
759 static char curr_dir[MAXPATHLEN];
761 /* like chdir() but can be reversed with pop_dir() if save is set. It
762 is also much faster as it remembers where we have been */
763 char *push_dir(char *dir, int save)
765 char *ret = curr_dir;
766 static int initialised;
770 getcwd(curr_dir, sizeof(curr_dir)-1);
773 if (!dir) return NULL; /* this call was probably just to initialize */
775 if (chdir(dir)) return NULL;
778 ret = strdup(curr_dir);
782 strlcpy(curr_dir, dir, sizeof(curr_dir));
784 strlcat(curr_dir,"/", sizeof(curr_dir));
785 strlcat(curr_dir,dir, sizeof(curr_dir));
788 clean_fname(curr_dir);
793 /* reverse a push_dir call */
794 int pop_dir(char *dir)
804 strlcpy(curr_dir, dir, sizeof(curr_dir));
811 /* we need to supply our own strcmp function for file list comparisons
812 to ensure that signed/unsigned usage is consistent between machines. */
813 int u_strcmp(const char *cs1, const char *cs2)
815 const uchar *s1 = (const uchar *)cs1;
816 const uchar *s2 = (const uchar *)cs2;
818 while (*s1 && *s2 && (*s1 == *s2)) {
822 return (int)*s1 - (int)*s2;
825 static OFF_T last_ofs;
827 void end_progress(OFF_T size)
829 extern int do_progress, am_server;
831 if (do_progress && !am_server) {
832 rprintf(FINFO,"%.0f (100%%)\n", (double)size);
837 void show_progress(OFF_T ofs, OFF_T size)
839 extern int do_progress, am_server;
841 if (do_progress && !am_server) {
842 if (ofs > last_ofs + 1000) {
843 int pct = (int)((100.0*ofs)/size);
844 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
850 /* determine if a symlink points outside the current directory tree */
851 int unsafe_symlink(char *dest, char *src)
856 /* all absolute and null symlinks are unsafe */
857 if (!dest || !(*dest) || (*dest == '/')) return 1;
860 if (!src) out_of_memory("unsafe_symlink");
862 /* find out what our safety margin is */
863 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
864 if (strcmp(tok,"..") == 0) {
866 } else if (strcmp(tok,".") == 0) {
874 /* drop by one to account for the filename portion */
878 if (!dest) out_of_memory("unsafe_symlink");
880 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
881 if (strcmp(tok,"..") == 0) {
883 } else if (strcmp(tok,".") == 0) {
888 /* if at any point we go outside the current directory then
889 stop - it is unsafe */
890 if (depth < 0) break;
898 /****************************************************************************
899 return the date and time as a string
900 ****************************************************************************/
901 char *timestring(time_t t)
903 static char TimeBuf[200];
904 struct tm *tm = localtime(&t);
907 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
909 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
912 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
913 TimeBuf[strlen(TimeBuf)-1] = 0;
920 /****************************************************************************
921 like waitpid but does the WEXITSTATUS
922 ****************************************************************************/
924 #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
926 void wait_process(pid_t pid, int *status)
928 waitpid(pid, status, 0);
929 *status = WEXITSTATUS(*status);