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);
35 struct map_struct *map_file(int fd,OFF_T len)
37 struct map_struct *ret;
38 ret = (struct map_struct *)malloc(sizeof(*ret));
39 if (!ret) out_of_memory("map_file");
50 if (len < MAX_MAP_SIZE) {
51 ret->map = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
52 if (ret->map == (char *)-1) {
61 char *map_ptr(struct map_struct *map,OFF_T offset,int len)
66 return map->map+offset;
71 if (len > (map->size-offset))
72 len = map->size-offset;
74 if (offset >= map->p_offset &&
75 offset+len <= map->p_offset+map->p_len) {
76 return (map->p + (offset - map->p_offset));
79 len = MAX(len,CHUNK_SIZE);
80 if (len > (map->size-offset))
81 len = map->size-offset;
83 if (len > map->p_size) {
84 if (map->p) free(map->p);
85 map->p = (char *)malloc(len);
86 if (!map->p) out_of_memory("map_ptr");
90 if (do_lseek(map->fd,offset,SEEK_SET) != offset ||
91 (nread=read(map->fd,map->p,len)) != len) {
92 rprintf(FERROR,"EOF in map_ptr! (offset=%d len=%d nread=%d errno=%d)\n",
93 (int)offset, len, nread, errno);
97 map->p_offset = offset;
104 void unmap_file(struct map_struct *map)
108 munmap(map->map,map->size);
110 if (map->p) free(map->p);
115 /* this is taken from CVS */
116 int piped_child(char **command,int *f_in,int *f_out)
119 int to_child_pipe[2];
120 int from_child_pipe[2];
122 if (pipe(to_child_pipe) < 0 ||
123 pipe(from_child_pipe) < 0) {
124 rprintf(FERROR,"pipe: %s\n",strerror(errno));
131 rprintf(FERROR,"fork: %s\n",strerror(errno));
137 extern int orig_umask;
138 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
139 close(to_child_pipe[1]) < 0 ||
140 close(from_child_pipe[0]) < 0 ||
141 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
142 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
145 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
146 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
148 execvp(command[0], command);
149 rprintf(FERROR,"Failed to exec %s : %s\n",
150 command[0],strerror(errno));
154 if (close(from_child_pipe[1]) < 0 ||
155 close(to_child_pipe[0]) < 0) {
156 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
160 *f_in = from_child_pipe[0];
161 *f_out = to_child_pipe[1];
166 int local_child(int argc, char **argv,int *f_in,int *f_out)
169 int to_child_pipe[2];
170 int from_child_pipe[2];
172 if (pipe(to_child_pipe) < 0 ||
173 pipe(from_child_pipe) < 0) {
174 rprintf(FERROR,"pipe: %s\n",strerror(errno));
181 rprintf(FERROR,"fork: %s\n",strerror(errno));
186 extern int am_sender;
187 extern int am_server;
189 am_sender = !am_sender;
192 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
193 close(to_child_pipe[1]) < 0 ||
194 close(from_child_pipe[0]) < 0 ||
195 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
196 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
199 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
200 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
201 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
204 if (close(from_child_pipe[1]) < 0 ||
205 close(to_child_pipe[0]) < 0) {
206 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
210 *f_in = from_child_pipe[0];
211 *f_out = to_child_pipe[1];
218 void out_of_memory(char *str)
220 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
224 void overflow(char *str)
226 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
232 int set_modtime(char *fname,time_t modtime)
235 if (dry_run) return 0;
239 tbuf.actime = time(NULL);
240 tbuf.modtime = modtime;
241 return utime(fname,&tbuf);
242 #elif defined(HAVE_UTIME)
246 return utime(fname,t);
249 t[0].tv_sec = time(NULL);
251 t[1].tv_sec = modtime;
253 return utimes(fname,t);
260 /****************************************************************************
261 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
265 ****************************************************************************/
266 int set_blocking(int fd, int set)
270 #define FLAG_TO_SET O_NONBLOCK
273 #define FLAG_TO_SET O_NDELAY
275 #define FLAG_TO_SET FNDELAY
279 if((val = fcntl(fd, F_GETFL, 0)) == -1)
281 if(set) /* Turn blocking on - ie. clear nonblock flag */
285 return fcntl( fd, F_SETFL, val);
289 /****************************************************************************
290 create any necessary directories in fname. Unfortunately we don't know
291 what perms to give the directory when this is called so we need to rely
293 ****************************************************************************/
294 int create_directory_path(char *fname)
296 extern int orig_umask;
299 while (*fname == '/') fname++;
300 while (strncmp(fname,"./",2)==0) fname += 2;
303 while ((p=strchr(p,'/'))) {
305 do_mkdir(fname,0777 & ~orig_umask);
313 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
314 Return LEN upon success, write's (negative) error code otherwise.
316 derived from GNU C's cccp.c.
318 int full_write(int desc, char *ptr, int len)
324 int written = write (desc, ptr, len);
332 total_written += written;
336 return total_written;
339 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
340 Return the actual number of bytes read, zero for EOF, or negative
343 derived from GNU C's cccp.c. */
344 int safe_read(int desc, char *ptr, int len)
353 n_chars = read(desc, ptr, len);
354 } while (n_chars < 0 && errno == EINTR);
356 n_chars = read(desc, ptr, len);
363 /* copy a file - this is used in conjunction with the --temp-dir option */
364 int copy_file(char *source, char *dest, mode_t mode)
369 int len; /* Number of bytes read into `buf'. */
371 ifd = open(source, O_RDONLY);
373 rprintf(FERROR,"open %s: %s\n",
374 source,strerror(errno));
378 if (do_unlink(dest) && errno != ENOENT) {
379 rprintf(FERROR,"unlink %s: %s\n",
380 dest,strerror(errno));
384 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
386 rprintf(FERROR,"open %s: %s\n",
387 dest,strerror(errno));
392 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
393 if (full_write(ofd, buf, len) < 0) {
394 rprintf(FERROR,"write %s: %s\n",
395 dest,strerror(errno));
406 rprintf(FERROR,"read %s: %s\n",
407 source,strerror(errno));
414 /* sleep for a while via select */
415 void u_sleep(int usec)
421 select(0, NULL, NULL, NULL, &tv);
425 static pid_t all_pids[10];
428 /* fork and record the pid of the child */
431 pid_t newpid = fork();
434 all_pids[num_pids++] = newpid;
439 /* kill all children */
440 void kill_all(int sig)
443 for (i=0;i<num_pids;i++) {
444 if (all_pids[i] != getpid())
445 kill(all_pids[i], sig);
449 /* like strncpy but does not 0 fill the buffer and always null
450 terminates (thus it can use maxlen+1 space in d) */
451 void strlcpy(char *d, char *s, int maxlen)
454 if (len > maxlen) len = maxlen;
459 /* like strncat but does not 0 fill the buffer and always null
460 terminates (thus it can use maxlen+1 space in d) */
461 void strlcat(char *d, char *s, int maxlen)
463 int len1 = strlen(d);
464 int len2 = strlen(s);
465 if (len1+len2 > maxlen) {
469 memcpy(d+len1, s, len2);
474 /* turn a user name into a uid */
475 int name_to_uid(char *name, uid_t *uid)
478 if (!name || !*name) return 0;
479 pass = getpwnam(name);
487 /* turn a group name into a gid */
488 int name_to_gid(char *name, gid_t *gid)
491 if (!name || !*name) return 0;
492 grp = getgrnam(name);
501 /****************************************************************************
502 check if a process exists.
503 ****************************************************************************/
504 int process_exists(int pid)
506 return(kill(pid,0) == 0 || errno != ESRCH);
509 /* lock a byte range in a open file */
510 int lock_range(int fd, int offset, int len)
514 lock.l_type = F_WRLCK;
515 lock.l_whence = SEEK_SET;
516 lock.l_start = offset;
520 return fcntl(fd,F_SETLK,&lock) == 0;
524 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
528 argv[*argc] = strdup(s);
537 argv[*argc] = strdup(s);
539 memset(&globbuf, 0, sizeof(globbuf));
540 glob(argv[*argc], 0, NULL, &globbuf);
541 if (globbuf.gl_pathc == 0) {
546 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
547 if (i == 0) free(argv[*argc]);
548 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
549 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
556 void glob_expand(char *base, char **argv, int *argc, int maxargs)
558 char *s = argv[*argc];
561 if (!s || !*s) return;
563 if (strncmp(s, base, strlen(base)) == 0) {
568 if (!s) out_of_memory("glob_expand");
571 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
572 if (p != q && *(p-1) == ' ' && p[strlen(base)] == '/') {
573 /* split it at this point */
575 glob_expand_one(q, argv, argc, maxargs);
576 q = p+strlen(base)+1;
582 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
587 /*******************************************************************
588 convert a string to lower case
589 ********************************************************************/
590 void strlower(char *s)
593 if (isupper(*s)) *s = tolower(*s);
598 /* this is like vsnprintf but the 'n' limit does not include
599 the terminating null. So if you have a 1024 byte buffer then
601 int vslprintf(char *str, int n, const char *format, va_list ap)
603 #ifdef HAVE_VSNPRINTF
604 int ret = vsnprintf(str, n, format, ap);
605 if (ret > n || ret < 0) {
613 static int len=MAXPATHLEN*8;
616 /* this code is NOT a proper vsnprintf() implementation. It
617 relies on the fact that all calls to slprintf() in rsync
618 pass strings which have already been checked to be less
619 than MAXPATHLEN in length and never more than 2 strings are
620 concatenated. This means the above buffer is absolutely
621 ample and can never be overflowed.
623 In the future we would like to replace this with a proper
624 vsnprintf() implementation but right now we need a solution
625 that is secure and portable. This is it. */
630 /* can't call debug or we would recurse */
635 ret = vsprintf(buf, format, ap);
644 } else if (ret > n) {
650 memcpy(str, buf, n+1);
657 /* like snprintf but always null terminates */
658 int slprintf(char *str, int n, char *format, ...)
663 va_start(ap, format);
664 ret = vslprintf(str,n,format,ap);