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 /****************************************************************************
30 Set a fd into nonblocking mode. Uses POSIX O_NONBLOCK if available,
34 ****************************************************************************/
35 int set_nonblocking(int fd)
39 #define FLAG_TO_SET O_NONBLOCK
42 #define FLAG_TO_SET O_NDELAY
44 #define FLAG_TO_SET FNDELAY
48 if((val = fcntl(fd, F_GETFL, 0)) == -1)
51 return fcntl( fd, F_SETFL, val);
56 /* this is taken from CVS */
57 int piped_child(char **command,int *f_in,int *f_out)
61 int from_child_pipe[2];
63 if (pipe(to_child_pipe) < 0 ||
64 pipe(from_child_pipe) < 0) {
65 rprintf(FERROR,"pipe: %s\n",strerror(errno));
66 exit_cleanup(RERR_IPC);
72 rprintf(FERROR,"fork: %s\n",strerror(errno));
73 exit_cleanup(RERR_IPC);
78 extern int orig_umask;
79 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
80 close(to_child_pipe[1]) < 0 ||
81 close(from_child_pipe[0]) < 0 ||
82 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
83 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
84 exit_cleanup(RERR_IPC);
86 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
87 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
89 execvp(command[0], command);
90 rprintf(FERROR,"Failed to exec %s : %s\n",
91 command[0],strerror(errno));
92 exit_cleanup(RERR_IPC);
95 if (close(from_child_pipe[1]) < 0 ||
96 close(to_child_pipe[0]) < 0) {
97 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
98 exit_cleanup(RERR_IPC);
101 *f_in = from_child_pipe[0];
102 *f_out = to_child_pipe[1];
104 set_nonblocking(*f_in);
105 set_nonblocking(*f_out);
110 int local_child(int argc, char **argv,int *f_in,int *f_out)
113 int to_child_pipe[2];
114 int from_child_pipe[2];
116 if (pipe(to_child_pipe) < 0 ||
117 pipe(from_child_pipe) < 0) {
118 rprintf(FERROR,"pipe: %s\n",strerror(errno));
119 exit_cleanup(RERR_IPC);
125 rprintf(FERROR,"fork: %s\n",strerror(errno));
126 exit_cleanup(RERR_IPC);
130 extern int am_sender;
131 extern int am_server;
133 am_sender = !am_sender;
136 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
137 close(to_child_pipe[1]) < 0 ||
138 close(from_child_pipe[0]) < 0 ||
139 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
140 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
141 exit_cleanup(RERR_IPC);
143 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
144 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
145 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
148 if (close(from_child_pipe[1]) < 0 ||
149 close(to_child_pipe[0]) < 0) {
150 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
151 exit_cleanup(RERR_IPC);
154 *f_in = from_child_pipe[0];
155 *f_out = to_child_pipe[1];
162 void out_of_memory(char *str)
164 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
165 exit_cleanup(RERR_MALLOC);
168 void overflow(char *str)
170 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
171 exit_cleanup(RERR_MALLOC);
176 int set_modtime(char *fname,time_t modtime)
179 if (dry_run) return 0;
183 tbuf.actime = time(NULL);
184 tbuf.modtime = modtime;
185 return utime(fname,&tbuf);
186 #elif defined(HAVE_UTIME)
190 return utime(fname,t);
193 t[0].tv_sec = time(NULL);
195 t[1].tv_sec = modtime;
197 return utimes(fname,t);
203 /****************************************************************************
204 create any necessary directories in fname. Unfortunately we don't know
205 what perms to give the directory when this is called so we need to rely
207 ****************************************************************************/
208 int create_directory_path(char *fname)
210 extern int orig_umask;
213 while (*fname == '/') fname++;
214 while (strncmp(fname,"./",2)==0) fname += 2;
217 while ((p=strchr(p,'/'))) {
219 do_mkdir(fname,0777 & ~orig_umask);
227 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
228 Return LEN upon success, write's (negative) error code otherwise.
230 derived from GNU C's cccp.c.
232 static int full_write(int desc, char *ptr, int len)
238 int written = write (desc, ptr, len);
246 total_written += written;
250 return total_written;
253 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
254 Return the actual number of bytes read, zero for EOF, or negative
257 derived from GNU C's cccp.c. */
258 static int safe_read(int desc, char *ptr, int len)
267 n_chars = read(desc, ptr, len);
268 } while (n_chars < 0 && errno == EINTR);
270 n_chars = read(desc, ptr, len);
277 /* copy a file - this is used in conjunction with the --temp-dir option */
278 int copy_file(char *source, char *dest, mode_t mode)
283 int len; /* Number of bytes read into `buf'. */
285 ifd = do_open(source, O_RDONLY, 0);
287 rprintf(FERROR,"open %s: %s\n",
288 source,strerror(errno));
292 if (robust_unlink(dest) && errno != ENOENT) {
293 rprintf(FERROR,"unlink %s: %s\n",
294 dest,strerror(errno));
298 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
300 rprintf(FERROR,"open %s: %s\n",
301 dest,strerror(errno));
306 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
307 if (full_write(ofd, buf, len) < 0) {
308 rprintf(FERROR,"write %s: %s\n",
309 dest,strerror(errno));
320 rprintf(FERROR,"read %s: %s\n",
321 source,strerror(errno));
329 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
330 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
331 will shuffle the filenames around a bit as long as the file is still
332 busy; this is because this function does not know if the unlink call
333 is due to a new file coming in, or --delete trying to remove old
334 .rsyncNNN files, hence it renames it each time.
336 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
337 #define MAX_RENAMES_DIGITS 3
338 #define MAX_RENAMES 1000
340 int robust_unlink(char *fname)
343 return do_unlink(fname);
345 static int counter = 1;
347 char path[MAXPATHLEN];
349 rc = do_unlink(fname);
350 if ((rc == 0) || (errno != ETXTBSY))
353 strlcpy(path, fname, MAXPATHLEN);
356 while((path[--pos] != '/') && (pos >= 0))
359 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
360 pos += sizeof(".rsync")-1;
362 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
367 /* start where the last one left off to reduce chance of clashes */
370 sprintf(&path[pos], "%03d", counter);
371 if (++counter >= MAX_RENAMES)
373 } while (((rc = access(path, 0)) == 0) && (counter != start));
376 rprintf(FINFO,"renaming %s to %s because of text busy\n",
379 /* maybe we should return rename()'s exit status? Nah. */
380 if (do_rename(fname, path) != 0) {
388 int robust_rename(char *from, char *to)
391 return do_rename(from, to);
393 int rc = do_rename(from, to);
394 if ((rc == 0) || (errno != ETXTBSY))
396 if (robust_unlink(to) != 0)
398 return do_rename(from, to);
403 /* sleep for a while via select */
404 void u_sleep(int usec)
410 select(0, NULL, NULL, NULL, &tv);
414 static pid_t all_pids[10];
417 /* fork and record the pid of the child */
420 pid_t newpid = fork();
423 all_pids[num_pids++] = newpid;
428 /* kill all children */
429 void kill_all(int sig)
432 for (i=0;i<num_pids;i++) {
433 if (all_pids[i] != getpid())
434 kill(all_pids[i], sig);
438 /* turn a user name into a uid */
439 int name_to_uid(char *name, uid_t *uid)
442 if (!name || !*name) return 0;
443 pass = getpwnam(name);
451 /* turn a group name into a gid */
452 int name_to_gid(char *name, gid_t *gid)
455 if (!name || !*name) return 0;
456 grp = getgrnam(name);
465 /* lock a byte range in a open file */
466 int lock_range(int fd, int offset, int len)
470 lock.l_type = F_WRLCK;
471 lock.l_whence = SEEK_SET;
472 lock.l_start = offset;
476 return fcntl(fd,F_SETLK,&lock) == 0;
480 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
482 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
484 argv[*argc] = strdup(s);
488 extern int sanitize_paths;
494 argv[*argc] = strdup(s);
495 if (sanitize_paths) {
496 sanitize_path(argv[*argc], NULL);
499 memset(&globbuf, 0, sizeof(globbuf));
500 glob(argv[*argc], 0, NULL, &globbuf);
501 if (globbuf.gl_pathc == 0) {
506 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
507 if (i == 0) free(argv[*argc]);
508 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
509 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
516 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
518 char *s = argv[*argc];
522 if (!s || !*s) return;
524 if (strncmp(s, base, strlen(base)) == 0) {
529 if (!s) out_of_memory("glob_expand");
531 base = (char *)malloc(strlen(base1)+3);
532 if (!base) out_of_memory("glob_expand");
534 sprintf(base," %s/", base1);
537 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
538 /* split it at this point */
540 glob_expand_one(q, argv, argc, maxargs);
544 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
550 /*******************************************************************
551 convert a string to lower case
552 ********************************************************************/
553 void strlower(char *s)
556 if (isupper(*s)) *s = tolower(*s);
561 /* this is like vsnprintf but it always null terminates, so you
562 can fit at most n-1 chars in */
563 int vslprintf(char *str, int n, const char *format, va_list ap)
565 int ret = vsnprintf(str, n, format, ap);
566 if (ret >= n || ret < 0) {
575 /* like snprintf but always null terminates */
576 int slprintf(char *str, int n, char *format, ...)
581 va_start(ap, format);
582 ret = vslprintf(str,n,format,ap);
588 void *Realloc(void *p, int size)
590 if (!p) return (void *)malloc(size);
591 return (void *)realloc(p, size);
595 void clean_fname(char *name)
606 if ((p=strstr(name,"/./"))) {
614 if ((p=strstr(name,"//"))) {
622 if (strncmp(p=name,"./",2) == 0) {
630 if (l > 1 && p[l-1] == '/') {
638 * Make path appear as if a chroot had occurred:
639 * 1. remove leading "/" (or replace with "." if at end)
640 * 2. remove leading ".." components (except those allowed by "reldir")
641 * 3. delete any other "<dir>/.." (recursively)
642 * Can only shrink paths, so sanitizes in place.
643 * While we're at it, remove double slashes and "." components like
644 * clean_fname does(), but DON'T remove a trailing slash because that
645 * is sometimes significant on command line arguments.
646 * If "reldir" is non-null, it is a sanitized directory that the path will be
647 * relative to, so allow as many ".." at the beginning of the path as
648 * there are components in reldir. This is used for symbolic link targets.
649 * If reldir is non-null and the path began with "/", to be completely like
650 * a chroot we should add in depth levels of ".." at the beginning of the
651 * path, but that would blow the assumption that the path doesn't grow and
652 * it is not likely to end up being a valid symlink anyway, so just do
653 * the normal removal of the leading "/" instead.
654 * Contributed by Dave Dykstra <dwd@bell-labs.com>
657 void sanitize_path(char *p, char *reldir)
666 if (*reldir++ == '/') {
674 /* remove leading slashes */
678 /* this loop iterates once per filename component in p.
679 * both p (and sanp if the original had a slash) should
680 * always be left pointing after a slash
682 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
683 /* skip "." component */
684 while (*++p == '/') {
685 /* skip following slashes */
691 if ((*p == '.') && (*(p+1) == '.') &&
692 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
693 /* ".." component followed by slash or end */
694 if ((depth > 0) && (sanp == start)) {
695 /* allow depth levels of .. at the beginning */
703 /* back up sanp one level */
704 --sanp; /* now pointing at slash */
705 while ((sanp > start) && (*(sanp - 1) != '/')) {
706 /* skip back up to slash */
714 /* copy one component through next slash */
716 if ((*p == '\0') || (*(p-1) == '/')) {
718 /* skip multiple slashes */
725 /* move the virtual beginning to leave the .. alone */
729 if ((sanp == start) && !allowdotdot) {
730 /* ended up with nothing, so put in "." component */
732 * note that the !allowdotdot doesn't prevent this from
733 * happening in all allowed ".." situations, but I didn't
734 * think it was worth putting in an extra variable to ensure
735 * it since an extra "." won't hurt in those situations.
743 static char curr_dir[MAXPATHLEN];
745 /* like chdir() but can be reversed with pop_dir() if save is set. It
746 is also much faster as it remembers where we have been */
747 char *push_dir(char *dir, int save)
749 char *ret = curr_dir;
750 static int initialised;
754 getcwd(curr_dir, sizeof(curr_dir)-1);
757 if (!dir) return NULL; /* this call was probably just to initialize */
759 if (chdir(dir)) return NULL;
762 ret = strdup(curr_dir);
766 strlcpy(curr_dir, dir, sizeof(curr_dir));
768 strlcat(curr_dir,"/", sizeof(curr_dir));
769 strlcat(curr_dir,dir, sizeof(curr_dir));
772 clean_fname(curr_dir);
777 /* reverse a push_dir call */
778 int pop_dir(char *dir)
788 strlcpy(curr_dir, dir, sizeof(curr_dir));
795 /* we need to supply our own strcmp function for file list comparisons
796 to ensure that signed/unsigned usage is consistent between machines. */
797 int u_strcmp(const char *cs1, const char *cs2)
799 const uchar *s1 = (const uchar *)cs1;
800 const uchar *s2 = (const uchar *)cs2;
802 while (*s1 && *s2 && (*s1 == *s2)) {
806 return (int)*s1 - (int)*s2;
809 static OFF_T last_ofs;
811 void end_progress(void)
813 extern int do_progress, am_server;
815 if (do_progress && !am_server) {
821 void show_progress(OFF_T ofs, OFF_T size)
823 extern int do_progress, am_server;
825 if (do_progress && !am_server) {
826 if (ofs > last_ofs + 1000) {
827 int pct = (int)((100.0*ofs)/size);
828 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
834 /* determine if a symlink points outside the current directory tree */
835 int unsafe_symlink(char *dest, char *src)
840 /* all absolute and null symlinks are unsafe */
841 if (!dest || !(*dest) || (*dest == '/')) return 1;
844 if (!src) out_of_memory("unsafe_symlink");
846 /* find out what our safety margin is */
847 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
848 if (strcmp(tok,"..") == 0) {
850 } else if (strcmp(tok,".") == 0) {
858 /* drop by one to account for the filename portion */
862 if (!dest) out_of_memory("unsafe_symlink");
864 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
865 if (strcmp(tok,"..") == 0) {
867 } else if (strcmp(tok,".") == 0) {
872 /* if at any point we go outside the current directory then
873 stop - it is unsafe */
874 if (depth < 0) break;
882 /****************************************************************************
883 return the date and time as a string
884 ****************************************************************************/
885 char *timestring(time_t t)
887 static char TimeBuf[200];
888 struct tm *tm = localtime(&t);
891 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
893 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
896 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
897 TimeBuf[strlen(TimeBuf)-1] = 0;