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
27 /****************************************************************************
28 Set a fd into nonblocking mode. Uses POSIX O_NONBLOCK if available,
32 ****************************************************************************/
33 int set_nonblocking(int fd)
37 #define FLAG_TO_SET O_NONBLOCK
40 #define FLAG_TO_SET O_NDELAY
42 #define FLAG_TO_SET FNDELAY
46 if((val = fcntl(fd, F_GETFL, 0)) == -1)
49 return fcntl( fd, F_SETFL, val);
54 /* this is taken from CVS */
55 int piped_child(char **command,int *f_in,int *f_out)
59 int from_child_pipe[2];
61 if (pipe(to_child_pipe) < 0 ||
62 pipe(from_child_pipe) < 0) {
63 rprintf(FERROR,"pipe: %s\n",strerror(errno));
70 rprintf(FERROR,"fork: %s\n",strerror(errno));
76 extern int orig_umask;
77 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
78 close(to_child_pipe[1]) < 0 ||
79 close(from_child_pipe[0]) < 0 ||
80 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
81 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
84 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
85 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
87 execvp(command[0], command);
88 rprintf(FERROR,"Failed to exec %s : %s\n",
89 command[0],strerror(errno));
93 if (close(from_child_pipe[1]) < 0 ||
94 close(to_child_pipe[0]) < 0) {
95 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
99 *f_in = from_child_pipe[0];
100 *f_out = to_child_pipe[1];
105 int local_child(int argc, char **argv,int *f_in,int *f_out)
108 int to_child_pipe[2];
109 int from_child_pipe[2];
111 if (pipe(to_child_pipe) < 0 ||
112 pipe(from_child_pipe) < 0) {
113 rprintf(FERROR,"pipe: %s\n",strerror(errno));
120 rprintf(FERROR,"fork: %s\n",strerror(errno));
125 extern int am_sender;
126 extern int am_server;
128 am_sender = !am_sender;
131 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
132 close(to_child_pipe[1]) < 0 ||
133 close(from_child_pipe[0]) < 0 ||
134 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
135 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
138 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
139 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
140 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
143 if (close(from_child_pipe[1]) < 0 ||
144 close(to_child_pipe[0]) < 0) {
145 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
149 *f_in = from_child_pipe[0];
150 *f_out = to_child_pipe[1];
157 void out_of_memory(char *str)
159 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
163 void overflow(char *str)
165 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
171 int set_modtime(char *fname,time_t modtime)
174 if (dry_run) return 0;
178 tbuf.actime = time(NULL);
179 tbuf.modtime = modtime;
180 return utime(fname,&tbuf);
181 #elif defined(HAVE_UTIME)
185 return utime(fname,t);
188 t[0].tv_sec = time(NULL);
190 t[1].tv_sec = modtime;
192 return utimes(fname,t);
198 /****************************************************************************
199 create any necessary directories in fname. Unfortunately we don't know
200 what perms to give the directory when this is called so we need to rely
202 ****************************************************************************/
203 int create_directory_path(char *fname)
205 extern int orig_umask;
208 while (*fname == '/') fname++;
209 while (strncmp(fname,"./",2)==0) fname += 2;
212 while ((p=strchr(p,'/'))) {
214 do_mkdir(fname,0777 & ~orig_umask);
222 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
223 Return LEN upon success, write's (negative) error code otherwise.
225 derived from GNU C's cccp.c.
227 static int full_write(int desc, char *ptr, int len)
233 int written = write (desc, ptr, len);
241 total_written += written;
245 return total_written;
248 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
249 Return the actual number of bytes read, zero for EOF, or negative
252 derived from GNU C's cccp.c. */
253 int safe_read(int desc, char *ptr, int len)
262 n_chars = read(desc, ptr, len);
263 } while (n_chars < 0 && errno == EINTR);
265 n_chars = read(desc, ptr, len);
272 /* copy a file - this is used in conjunction with the --temp-dir option */
273 int copy_file(char *source, char *dest, mode_t mode)
278 int len; /* Number of bytes read into `buf'. */
280 ifd = open(source, O_RDONLY);
282 rprintf(FERROR,"open %s: %s\n",
283 source,strerror(errno));
287 if (do_unlink(dest) && errno != ENOENT) {
288 rprintf(FERROR,"unlink %s: %s\n",
289 dest,strerror(errno));
293 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
295 rprintf(FERROR,"open %s: %s\n",
296 dest,strerror(errno));
301 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
302 if (full_write(ofd, buf, len) < 0) {
303 rprintf(FERROR,"write %s: %s\n",
304 dest,strerror(errno));
315 rprintf(FERROR,"read %s: %s\n",
316 source,strerror(errno));
323 /* sleep for a while via select */
324 void u_sleep(int usec)
330 select(0, NULL, NULL, NULL, &tv);
334 static pid_t all_pids[10];
337 /* fork and record the pid of the child */
340 pid_t newpid = fork();
343 all_pids[num_pids++] = newpid;
348 /* kill all children */
349 void kill_all(int sig)
352 for (i=0;i<num_pids;i++) {
353 if (all_pids[i] != getpid())
354 kill(all_pids[i], sig);
358 /* like strncpy but does not 0 fill the buffer and always null
359 terminates (thus it can use maxlen+1 space in d) */
360 void strlcpy(char *d, char *s, int maxlen)
363 if (len > maxlen) len = maxlen;
368 /* like strncat but does not 0 fill the buffer and always null
369 terminates (thus it can use maxlen+1 space in d) */
370 void strlcat(char *d, char *s, int maxlen)
372 int len1 = strlen(d);
373 int len2 = strlen(s);
374 if (len1+len2 > maxlen) {
378 memcpy(d+len1, s, len2);
383 /* turn a user name into a uid */
384 int name_to_uid(char *name, uid_t *uid)
387 if (!name || !*name) return 0;
388 pass = getpwnam(name);
396 /* turn a group name into a gid */
397 int name_to_gid(char *name, gid_t *gid)
400 if (!name || !*name) return 0;
401 grp = getgrnam(name);
410 /****************************************************************************
411 check if a process exists.
412 ****************************************************************************/
413 int process_exists(int pid)
415 return(kill(pid,0) == 0 || errno != ESRCH);
418 /* lock a byte range in a open file */
419 int lock_range(int fd, int offset, int len)
423 lock.l_type = F_WRLCK;
424 lock.l_whence = SEEK_SET;
425 lock.l_start = offset;
429 return fcntl(fd,F_SETLK,&lock) == 0;
433 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
437 argv[*argc] = strdup(s);
446 argv[*argc] = strdup(s);
448 memset(&globbuf, 0, sizeof(globbuf));
449 glob(argv[*argc], 0, NULL, &globbuf);
450 if (globbuf.gl_pathc == 0) {
455 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
456 if (i == 0) free(argv[*argc]);
457 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
458 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
465 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
467 char *s = argv[*argc];
471 if (!s || !*s) return;
473 if (strncmp(s, base, strlen(base)) == 0) {
478 if (!s) out_of_memory("glob_expand");
480 base = (char *)malloc(strlen(base1)+3);
481 if (!base) out_of_memory("glob_expand");
483 sprintf(base," %s/", base1);
486 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
487 /* split it at this point */
489 glob_expand_one(q, argv, argc, maxargs);
493 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
499 /*******************************************************************
500 convert a string to lower case
501 ********************************************************************/
502 void strlower(char *s)
505 if (isupper(*s)) *s = tolower(*s);
510 /* this is like vsnprintf but the 'n' limit does not include
511 the terminating null. So if you have a 1024 byte buffer then
513 int vslprintf(char *str, int n, const char *format, va_list ap)
515 #ifdef HAVE_VSNPRINTF
516 int ret = vsnprintf(str, n, format, ap);
517 if (ret > n || ret < 0) {
525 static int len=MAXPATHLEN*8;
528 /* this code is NOT a proper vsnprintf() implementation. It
529 relies on the fact that all calls to slprintf() in rsync
530 pass strings which have already been checked to be less
531 than MAXPATHLEN in length and never more than 2 strings are
532 concatenated. This means the above buffer is absolutely
533 ample and can never be overflowed.
535 In the future we would like to replace this with a proper
536 vsnprintf() implementation but right now we need a solution
537 that is secure and portable. This is it. */
542 /* can't call debug or we would recurse */
547 vsprintf(buf, format, ap);
555 memcpy(str, buf, ret+1);
562 /* like snprintf but always null terminates */
563 int slprintf(char *str, int n, char *format, ...)
568 va_start(ap, format);
569 ret = vslprintf(str,n,format,ap);
575 void *Realloc(void *p, int size)
577 if (!p) return (void *)malloc(size);
578 return (void *)realloc(p, size);
582 void clean_fname(char *name)
593 if ((p=strstr(name,"/./"))) {
601 if ((p=strstr(name,"//"))) {
609 if (strncmp(p=name,"./",2) == 0) {
617 if (l > 1 && p[l-1] == '/') {
625 static char curr_dir[MAXPATHLEN];
627 /* like chdir() but can be reversed with pop_dir() if save is set. It
628 is also much faster as it remembers where we have been */
629 char *push_dir(char *dir, int save)
631 char *ret = curr_dir;
632 static int initialised;
636 getcwd(curr_dir, sizeof(curr_dir)-1);
639 if (chdir(dir)) return NULL;
642 ret = strdup(curr_dir);
646 strlcpy(curr_dir, dir, sizeof(curr_dir)-1);
648 strlcat(curr_dir,"/", sizeof(curr_dir)-1);
649 strlcat(curr_dir,dir, sizeof(curr_dir)-1);
652 clean_fname(curr_dir);
657 /* reverse a push_dir call */
658 int pop_dir(char *dir)
668 strlcpy(curr_dir, dir, sizeof(curr_dir)-1);
675 /* we need to supply our own strcmp function for file list comparisons
676 to ensure that signed/unsigned usage is consistent between machines. */
677 int u_strcmp(const char *cs1, const char *cs2)
679 const uchar *s1 = (uchar *)cs1;
680 const uchar *s2 = (uchar *)cs2;
682 while (*s1 && *s2 && (*s1 == *s2)) {
686 return (int)*s1 - (int)*s2;
689 static OFF_T last_ofs;
691 void end_progress(void)
693 extern int do_progress, am_server;
695 if (do_progress && !am_server) {
701 void show_progress(OFF_T ofs, OFF_T size)
703 extern int do_progress, am_server;
705 if (do_progress && !am_server) {
706 if (ofs > last_ofs + 1000) {
707 int pct = (int)((100.0*ofs)/size);
708 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
714 /* determine if a symlink points outside the current directory tree */
715 int unsafe_symlink(char *dest, char *src)
720 /* all absolute and null symlinks are unsafe */
721 if (!dest || !(*dest) || (*dest == '/')) return 1;
724 if (!src) out_of_memory("unsafe_symlink");
726 /* find out what our safety margin is */
727 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
728 if (strcmp(tok,"..") == 0) {
730 } else if (strcmp(tok,".") == 0) {
738 /* drop by one to account for the filename portion */
742 if (!dest) out_of_memory("unsafe_symlink");
744 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
745 if (strcmp(tok,"..") == 0) {
747 } else if (strcmp(tok,".") == 0) {
752 /* if at any point we go outside the current directory then
753 stop - it is unsafe */
754 if (depth < 0) break;