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 int num_waiting(int fd)
30 ioctl(fd,FIONREAD,&len);
36 /* this is taken from CVS */
37 int piped_child(char **command,int *f_in,int *f_out)
41 int from_child_pipe[2];
43 if (pipe(to_child_pipe) < 0 ||
44 pipe(from_child_pipe) < 0) {
45 rprintf(FERROR,"pipe: %s\n",strerror(errno));
52 rprintf(FERROR,"fork: %s\n",strerror(errno));
58 extern int orig_umask;
59 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
60 close(to_child_pipe[1]) < 0 ||
61 close(from_child_pipe[0]) < 0 ||
62 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
63 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
66 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
67 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
69 execvp(command[0], command);
70 rprintf(FERROR,"Failed to exec %s : %s\n",
71 command[0],strerror(errno));
75 if (close(from_child_pipe[1]) < 0 ||
76 close(to_child_pipe[0]) < 0) {
77 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
81 *f_in = from_child_pipe[0];
82 *f_out = to_child_pipe[1];
87 int local_child(int argc, char **argv,int *f_in,int *f_out)
91 int from_child_pipe[2];
93 if (pipe(to_child_pipe) < 0 ||
94 pipe(from_child_pipe) < 0) {
95 rprintf(FERROR,"pipe: %s\n",strerror(errno));
102 rprintf(FERROR,"fork: %s\n",strerror(errno));
107 extern int am_sender;
108 extern int am_server;
110 am_sender = !am_sender;
113 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
114 close(to_child_pipe[1]) < 0 ||
115 close(from_child_pipe[0]) < 0 ||
116 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
117 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
120 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
121 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
122 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
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));
131 *f_in = from_child_pipe[0];
132 *f_out = to_child_pipe[1];
139 void out_of_memory(char *str)
141 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
145 void overflow(char *str)
147 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
153 int set_modtime(char *fname,time_t modtime)
156 if (dry_run) return 0;
160 tbuf.actime = time(NULL);
161 tbuf.modtime = modtime;
162 return utime(fname,&tbuf);
163 #elif defined(HAVE_UTIME)
167 return utime(fname,t);
170 t[0].tv_sec = time(NULL);
172 t[1].tv_sec = modtime;
174 return utimes(fname,t);
180 /****************************************************************************
181 create any necessary directories in fname. Unfortunately we don't know
182 what perms to give the directory when this is called so we need to rely
184 ****************************************************************************/
185 int create_directory_path(char *fname)
187 extern int orig_umask;
190 while (*fname == '/') fname++;
191 while (strncmp(fname,"./",2)==0) fname += 2;
194 while ((p=strchr(p,'/'))) {
196 do_mkdir(fname,0777 & ~orig_umask);
204 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
205 Return LEN upon success, write's (negative) error code otherwise.
207 derived from GNU C's cccp.c.
209 int full_write(int desc, char *ptr, int len)
215 int written = write (desc, ptr, len);
223 total_written += written;
227 return total_written;
230 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
231 Return the actual number of bytes read, zero for EOF, or negative
234 derived from GNU C's cccp.c. */
235 int safe_read(int desc, char *ptr, int len)
244 n_chars = read(desc, ptr, len);
245 } while (n_chars < 0 && errno == EINTR);
247 n_chars = read(desc, ptr, len);
254 /* copy a file - this is used in conjunction with the --temp-dir option */
255 int copy_file(char *source, char *dest, mode_t mode)
260 int len; /* Number of bytes read into `buf'. */
262 ifd = open(source, O_RDONLY);
264 rprintf(FERROR,"open %s: %s\n",
265 source,strerror(errno));
269 if (do_unlink(dest) && errno != ENOENT) {
270 rprintf(FERROR,"unlink %s: %s\n",
271 dest,strerror(errno));
275 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
277 rprintf(FERROR,"open %s: %s\n",
278 dest,strerror(errno));
283 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
284 if (full_write(ofd, buf, len) < 0) {
285 rprintf(FERROR,"write %s: %s\n",
286 dest,strerror(errno));
297 rprintf(FERROR,"read %s: %s\n",
298 source,strerror(errno));
305 /* sleep for a while via select */
306 void u_sleep(int usec)
312 select(0, NULL, NULL, NULL, &tv);
316 static pid_t all_pids[10];
319 /* fork and record the pid of the child */
322 pid_t newpid = fork();
325 all_pids[num_pids++] = newpid;
330 /* kill all children */
331 void kill_all(int sig)
334 for (i=0;i<num_pids;i++) {
335 if (all_pids[i] != getpid())
336 kill(all_pids[i], sig);
340 /* like strncpy but does not 0 fill the buffer and always null
341 terminates (thus it can use maxlen+1 space in d) */
342 void strlcpy(char *d, char *s, int maxlen)
345 if (len > maxlen) len = maxlen;
350 /* like strncat but does not 0 fill the buffer and always null
351 terminates (thus it can use maxlen+1 space in d) */
352 void strlcat(char *d, char *s, int maxlen)
354 int len1 = strlen(d);
355 int len2 = strlen(s);
356 if (len1+len2 > maxlen) {
360 memcpy(d+len1, s, len2);
365 /* turn a user name into a uid */
366 int name_to_uid(char *name, uid_t *uid)
369 if (!name || !*name) return 0;
370 pass = getpwnam(name);
378 /* turn a group name into a gid */
379 int name_to_gid(char *name, gid_t *gid)
382 if (!name || !*name) return 0;
383 grp = getgrnam(name);
392 /****************************************************************************
393 check if a process exists.
394 ****************************************************************************/
395 int process_exists(int pid)
397 return(kill(pid,0) == 0 || errno != ESRCH);
400 /* lock a byte range in a open file */
401 int lock_range(int fd, int offset, int len)
405 lock.l_type = F_WRLCK;
406 lock.l_whence = SEEK_SET;
407 lock.l_start = offset;
411 return fcntl(fd,F_SETLK,&lock) == 0;
415 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
419 argv[*argc] = strdup(s);
428 argv[*argc] = strdup(s);
430 memset(&globbuf, 0, sizeof(globbuf));
431 glob(argv[*argc], 0, NULL, &globbuf);
432 if (globbuf.gl_pathc == 0) {
437 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
438 if (i == 0) free(argv[*argc]);
439 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
440 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
447 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
449 char *s = argv[*argc];
453 if (!s || !*s) return;
455 if (strncmp(s, base, strlen(base)) == 0) {
460 if (!s) out_of_memory("glob_expand");
462 base = (char *)malloc(strlen(base1)+3);
463 if (!base) out_of_memory("glob_expand");
465 sprintf(base," %s/", base1);
468 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
469 /* split it at this point */
471 glob_expand_one(q, argv, argc, maxargs);
475 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
481 /*******************************************************************
482 convert a string to lower case
483 ********************************************************************/
484 void strlower(char *s)
487 if (isupper(*s)) *s = tolower(*s);
492 /* this is like vsnprintf but the 'n' limit does not include
493 the terminating null. So if you have a 1024 byte buffer then
495 int vslprintf(char *str, int n, const char *format, va_list ap)
497 #ifdef HAVE_VSNPRINTF
498 int ret = vsnprintf(str, n, format, ap);
499 if (ret > n || ret < 0) {
507 static int len=MAXPATHLEN*8;
510 /* this code is NOT a proper vsnprintf() implementation. It
511 relies on the fact that all calls to slprintf() in rsync
512 pass strings which have already been checked to be less
513 than MAXPATHLEN in length and never more than 2 strings are
514 concatenated. This means the above buffer is absolutely
515 ample and can never be overflowed.
517 In the future we would like to replace this with a proper
518 vsnprintf() implementation but right now we need a solution
519 that is secure and portable. This is it. */
524 /* can't call debug or we would recurse */
529 vsprintf(buf, format, ap);
537 memcpy(str, buf, ret+1);
544 /* like snprintf but always null terminates */
545 int slprintf(char *str, int n, char *format, ...)
550 va_start(ap, format);
551 ret = vslprintf(str,n,format,ap);
557 void *Realloc(void *p, int size)
559 if (!p) return (void *)malloc(size);
560 return (void *)realloc(p, size);
564 void clean_fname(char *name)
575 if ((p=strstr(name,"/./"))) {
583 if ((p=strstr(name,"//"))) {
591 if (strncmp(p=name,"./",2) == 0) {
599 if (l > 1 && p[l-1] == '/') {
607 static char curr_dir[MAXPATHLEN];
609 /* like chdir() but can be reversed with pop_dir() if save is set. It
610 is also much faster as it remembers where we have been */
611 char *push_dir(char *dir, int save)
613 char *ret = curr_dir;
614 static int initialised;
618 getcwd(curr_dir, sizeof(curr_dir)-1);
621 if (chdir(dir)) return NULL;
624 ret = strdup(curr_dir);
628 strlcpy(curr_dir, dir, sizeof(curr_dir)-1);
630 strlcat(curr_dir,"/", sizeof(curr_dir)-1);
631 strlcat(curr_dir,dir, sizeof(curr_dir)-1);
634 clean_fname(curr_dir);
639 /* reverse a push_dir call */
640 int pop_dir(char *dir)
650 strlcpy(curr_dir, dir, sizeof(curr_dir)-1);
657 /* we need to supply our own strcmp function for file list comparisons
658 to ensure that signed/unsigned usage is consistent between machines. */
659 int u_strcmp(const char *cs1, const char *cs2)
661 const uchar *s1 = (uchar *)cs1;
662 const uchar *s2 = (uchar *)cs2;
664 while (*s1 && *s2 && (*s1 == *s2)) {
668 return (int)*s1 - (int)*s2;