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);
34 static int map_fd = -1;
35 static off_t map_size = 0;
36 static char *p = NULL;
37 static int p_size = 0;
38 static int p_offset = 0;
42 char *map_file(int fd,off_t len)
46 if (len < MAX_MAP_SIZE)
47 ret = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
54 char *map_ptr(char *buf,off_t offset,int len)
62 len = MIN(len,map_size-offset);
64 if (offset >= p_offset &&
65 offset+len <= p_offset+p_len) {
66 return (p + (offset - p_offset));
69 len = MAX(len,WRITE_BLOCK_SIZE);
70 len = MIN(len,map_size - offset);
74 p = (char *)malloc(len);
75 if (!p) out_of_memory("map_ptr");
79 if (lseek(map_fd,offset,SEEK_SET) != offset ||
80 read(map_fd,p,len) != len) {
81 fprintf(stderr,"EOF in map_ptr!\n");
92 void unmap_file(char *buf,off_t len)
102 /* this is taken from CVS */
103 int piped_child(char **command,int *f_in,int *f_out)
106 int to_child_pipe[2];
107 int from_child_pipe[2];
109 if (pipe(to_child_pipe) < 0 ||
110 pipe(from_child_pipe) < 0) {
111 fprintf(stderr,"pipe: %s\n",strerror(errno));
118 fprintf(stderr,"fork: %s\n",strerror(errno));
124 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
125 close(to_child_pipe[1]) < 0 ||
126 close(from_child_pipe[0]) < 0 ||
127 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
128 fprintf(stderr,"Failed to dup/close : %s\n",strerror(errno));
131 execvp(command[0], command);
132 fprintf(stderr,"Failed to exec %s : %s\n",
133 command[0],strerror(errno));
137 if (close(from_child_pipe[1]) < 0 ||
138 close(to_child_pipe[0]) < 0) {
139 fprintf(stderr,"Failed to close : %s\n",strerror(errno));
143 *f_in = from_child_pipe[0];
144 *f_out = to_child_pipe[1];
150 void out_of_memory(char *str)
152 fprintf(stderr,"out of memory in %s\n",str);
158 char *strdup(char *s)
160 int l = strlen(s) + 1;
161 char *ret = (char *)malloc(l);
169 int set_modtime(char *fname,time_t modtime)
173 tbuf.actime = time(NULL);
174 tbuf.modtime = modtime;
175 return utime(fname,&tbuf);
176 #elif defined(HAVE_UTIME)
180 return utime(fname,t);
183 t[0].tv_sec = time(NULL);
185 t[1].tv_sec = modtime;
187 return utimes(fname,t);
193 /****************************************************************************
194 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
198 ****************************************************************************/
199 int set_blocking(int fd, int set)
203 #define FLAG_TO_SET O_NONBLOCK
206 #define FLAG_TO_SET O_NDELAY
208 #define FLAG_TO_SET FNDELAY
212 if((val = fcntl(fd, F_GETFL, 0)) == -1)
214 if(set) /* Turn blocking on - ie. clear nonblock flag */
218 return fcntl( fd, F_SETFL, val);