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 map->p_offset = offset;
93 if (do_lseek(map->fd,offset,SEEK_SET) != offset) {
94 rprintf(FERROR,"lseek failed in map_ptr\n");
98 if ((nread=read(map->fd,map->p,len)) != len) {
99 if (nread < 0) nread = 0;
100 /* the best we can do is zero the buffer - the file
101 has changed mid transfer! */
102 memset(map->p+nread, 0, len - nread);
109 void unmap_file(struct map_struct *map)
113 munmap(map->map,map->size);
115 if (map->p) free(map->p);
120 /* this is taken from CVS */
121 int piped_child(char **command,int *f_in,int *f_out)
124 int to_child_pipe[2];
125 int from_child_pipe[2];
127 if (pipe(to_child_pipe) < 0 ||
128 pipe(from_child_pipe) < 0) {
129 rprintf(FERROR,"pipe: %s\n",strerror(errno));
136 rprintf(FERROR,"fork: %s\n",strerror(errno));
142 extern int orig_umask;
143 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
144 close(to_child_pipe[1]) < 0 ||
145 close(from_child_pipe[0]) < 0 ||
146 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
147 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
150 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
151 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
153 execvp(command[0], command);
154 rprintf(FERROR,"Failed to exec %s : %s\n",
155 command[0],strerror(errno));
159 if (close(from_child_pipe[1]) < 0 ||
160 close(to_child_pipe[0]) < 0) {
161 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
165 *f_in = from_child_pipe[0];
166 *f_out = to_child_pipe[1];
171 int local_child(int argc, char **argv,int *f_in,int *f_out)
174 int to_child_pipe[2];
175 int from_child_pipe[2];
177 if (pipe(to_child_pipe) < 0 ||
178 pipe(from_child_pipe) < 0) {
179 rprintf(FERROR,"pipe: %s\n",strerror(errno));
186 rprintf(FERROR,"fork: %s\n",strerror(errno));
191 extern int am_sender;
192 extern int am_server;
194 am_sender = !am_sender;
197 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
198 close(to_child_pipe[1]) < 0 ||
199 close(from_child_pipe[0]) < 0 ||
200 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
201 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
204 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
205 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
206 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
209 if (close(from_child_pipe[1]) < 0 ||
210 close(to_child_pipe[0]) < 0) {
211 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
215 *f_in = from_child_pipe[0];
216 *f_out = to_child_pipe[1];
223 void out_of_memory(char *str)
225 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
229 void overflow(char *str)
231 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
237 int set_modtime(char *fname,time_t modtime)
240 if (dry_run) return 0;
244 tbuf.actime = time(NULL);
245 tbuf.modtime = modtime;
246 return utime(fname,&tbuf);
247 #elif defined(HAVE_UTIME)
251 return utime(fname,t);
254 t[0].tv_sec = time(NULL);
256 t[1].tv_sec = modtime;
258 return utimes(fname,t);
265 /****************************************************************************
266 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
270 ****************************************************************************/
271 int set_blocking(int fd, int set)
275 #define FLAG_TO_SET O_NONBLOCK
278 #define FLAG_TO_SET O_NDELAY
280 #define FLAG_TO_SET FNDELAY
284 if((val = fcntl(fd, F_GETFL, 0)) == -1)
286 if(set) /* Turn blocking on - ie. clear nonblock flag */
290 return fcntl( fd, F_SETFL, val);
294 /****************************************************************************
295 create any necessary directories in fname. Unfortunately we don't know
296 what perms to give the directory when this is called so we need to rely
298 ****************************************************************************/
299 int create_directory_path(char *fname)
301 extern int orig_umask;
304 while (*fname == '/') fname++;
305 while (strncmp(fname,"./",2)==0) fname += 2;
308 while ((p=strchr(p,'/'))) {
310 do_mkdir(fname,0777 & ~orig_umask);
318 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
319 Return LEN upon success, write's (negative) error code otherwise.
321 derived from GNU C's cccp.c.
323 int full_write(int desc, char *ptr, int len)
329 int written = write (desc, ptr, len);
337 total_written += written;
341 return total_written;
344 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
345 Return the actual number of bytes read, zero for EOF, or negative
348 derived from GNU C's cccp.c. */
349 int safe_read(int desc, char *ptr, int len)
358 n_chars = read(desc, ptr, len);
359 } while (n_chars < 0 && errno == EINTR);
361 n_chars = read(desc, ptr, len);
368 /* copy a file - this is used in conjunction with the --temp-dir option */
369 int copy_file(char *source, char *dest, mode_t mode)
374 int len; /* Number of bytes read into `buf'. */
376 ifd = open(source, O_RDONLY);
378 rprintf(FERROR,"open %s: %s\n",
379 source,strerror(errno));
383 if (do_unlink(dest) && errno != ENOENT) {
384 rprintf(FERROR,"unlink %s: %s\n",
385 dest,strerror(errno));
389 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
391 rprintf(FERROR,"open %s: %s\n",
392 dest,strerror(errno));
397 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
398 if (full_write(ofd, buf, len) < 0) {
399 rprintf(FERROR,"write %s: %s\n",
400 dest,strerror(errno));
411 rprintf(FERROR,"read %s: %s\n",
412 source,strerror(errno));
419 /* sleep for a while via select */
420 void u_sleep(int usec)
426 select(0, NULL, NULL, NULL, &tv);
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 /* like strncpy but does not 0 fill the buffer and always null
455 terminates (thus it can use maxlen+1 space in d) */
456 void strlcpy(char *d, char *s, int maxlen)
459 if (len > maxlen) len = maxlen;
464 /* like strncat but does not 0 fill the buffer and always null
465 terminates (thus it can use maxlen+1 space in d) */
466 void strlcat(char *d, char *s, int maxlen)
468 int len1 = strlen(d);
469 int len2 = strlen(s);
470 if (len1+len2 > maxlen) {
474 memcpy(d+len1, s, len2);
479 /* turn a user name into a uid */
480 int name_to_uid(char *name, uid_t *uid)
483 if (!name || !*name) return 0;
484 pass = getpwnam(name);
492 /* turn a group name into a gid */
493 int name_to_gid(char *name, gid_t *gid)
496 if (!name || !*name) return 0;
497 grp = getgrnam(name);
506 /****************************************************************************
507 check if a process exists.
508 ****************************************************************************/
509 int process_exists(int pid)
511 return(kill(pid,0) == 0 || errno != ESRCH);
514 /* lock a byte range in a open file */
515 int lock_range(int fd, int offset, int len)
519 lock.l_type = F_WRLCK;
520 lock.l_whence = SEEK_SET;
521 lock.l_start = offset;
525 return fcntl(fd,F_SETLK,&lock) == 0;
529 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
533 argv[*argc] = strdup(s);
542 argv[*argc] = strdup(s);
544 memset(&globbuf, 0, sizeof(globbuf));
545 glob(argv[*argc], 0, NULL, &globbuf);
546 if (globbuf.gl_pathc == 0) {
551 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
552 if (i == 0) free(argv[*argc]);
553 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
554 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
561 void glob_expand(char *base, char **argv, int *argc, int maxargs)
563 char *s = argv[*argc];
566 if (!s || !*s) return;
568 if (strncmp(s, base, strlen(base)) == 0) {
573 if (!s) out_of_memory("glob_expand");
576 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
577 if (p != q && *(p-1) == ' ' && p[strlen(base)] == '/') {
578 /* split it at this point */
580 glob_expand_one(q, argv, argc, maxargs);
581 q = p+strlen(base)+1;
587 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
592 /*******************************************************************
593 convert a string to lower case
594 ********************************************************************/
595 void strlower(char *s)
598 if (isupper(*s)) *s = tolower(*s);
603 /* this is like vsnprintf but the 'n' limit does not include
604 the terminating null. So if you have a 1024 byte buffer then
606 int vslprintf(char *str, int n, const char *format, va_list ap)
608 #ifdef HAVE_VSNPRINTF
609 int ret = vsnprintf(str, n, format, ap);
610 if (ret > n || ret < 0) {
618 static int len=MAXPATHLEN*8;
621 /* this code is NOT a proper vsnprintf() implementation. It
622 relies on the fact that all calls to slprintf() in rsync
623 pass strings which have already been checked to be less
624 than MAXPATHLEN in length and never more than 2 strings are
625 concatenated. This means the above buffer is absolutely
626 ample and can never be overflowed.
628 In the future we would like to replace this with a proper
629 vsnprintf() implementation but right now we need a solution
630 that is secure and portable. This is it. */
635 /* can't call debug or we would recurse */
640 vsprintf(buf, format, ap);
648 memcpy(str, buf, ret+1);
655 /* like snprintf but always null terminates */
656 int slprintf(char *str, int n, char *format, ...)
661 va_start(ap, format);
662 ret = vslprintf(str,n,format,ap);