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 static int total_written = 0;
28 static int total_read = 0;
42 void write_int(int f,int x)
46 if (write(f,b,4) != 4) {
47 fprintf(stderr,"write_int failed : %s\n",strerror(errno));
53 void write_buf(int f,char *buf,int len)
55 if (write(f,buf,len) != len) {
56 fprintf(stderr,"write_buf failed : %s\n",strerror(errno));
62 static int num_waiting(int fd)
66 ioctl(fd,FIONREAD,&len);
71 void write_flush(int f)
76 static char *read_buffer = NULL;
77 static char *read_buffer_p = NULL;
78 static int read_buffer_len = 0;
79 static int read_buffer_size = 0;
82 /* This function was added to overcome a deadlock problem when using
83 * ssh. It looks like we can't allow our receive queue to get full or
84 * ssh will clag up. Uggh. */
85 void read_check(int f)
89 if (read_buffer_len == 0) {
90 read_buffer_p = read_buffer;
93 if ((n=num_waiting(f)) <= 0)
96 if (read_buffer_p != read_buffer) {
97 memmove(read_buffer,read_buffer_p,read_buffer_len);
98 read_buffer_p = read_buffer;
101 if (n > (read_buffer_size - read_buffer_len)) {
102 read_buffer_size += n; /* deliberately overdo it a bit */
104 read_buffer = (char *)malloc(read_buffer_size);
106 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
107 if (!read_buffer) out_of_memory("read check");
108 read_buffer_p = read_buffer;
111 n = read(f,read_buffer+read_buffer_len,n);
113 read_buffer_len += n;
118 static int readfd(int fd,char *buffer,int N)
125 if (read_buffer_len > 0) {
126 ret = MIN(read_buffer_len,N-total);
127 memcpy(buffer+total,read_buffer_p,ret);
128 read_buffer_p += ret;
129 read_buffer_len -= ret;
131 ret = read(fd,buffer + total,N - total);
145 if (readfd(f,b,4) != 4) {
147 fprintf(stderr,"Error reading %d bytes : %s\n",4,strerror(errno));
154 void read_buf(int f,char *buf,int len)
156 if (readfd(f,buf,len) != len) {
158 fprintf(stderr,"Error reading %d bytes : %s\n",len,strerror(errno));
165 char *map_file(int fd,off_t len)
167 char *ret = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
171 void unmap_file(char *buf,off_t len)
178 int read_write(int fd_in,int fd_out,int size)
180 static char *buf=NULL;
181 static int bufsize = WRITE_BLOCK_SIZE;
185 buf = (char *)malloc(bufsize);
186 if (!buf) out_of_memory("read_write");
189 while (total < size) {
190 int n = MIN(size-total,bufsize);
191 read_buf(fd_in,buf,n);
192 if (write(fd_out,buf,n) != n)
200 /* this is taken from CVS */
201 int piped_child(char **command,int *f_in,int *f_out)
204 int to_child_pipe[2];
205 int from_child_pipe[2];
207 if (pipe(to_child_pipe) < 0 ||
208 pipe(from_child_pipe) < 0) {
209 fprintf(stderr,"pipe: %s\n",strerror(errno));
216 fprintf(stderr,"fork: %s\n",strerror(errno));
222 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
223 close(to_child_pipe[1]) < 0 ||
224 close(from_child_pipe[0]) < 0 ||
225 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
226 fprintf(stderr,"Failed to dup/close : %s\n",strerror(errno));
229 execvp(command[0], command);
230 fprintf(stderr,"Failed to exec %s : %s\n",
231 command[0],strerror(errno));
235 if (close(from_child_pipe[1]) < 0 ||
236 close(to_child_pipe[0]) < 0) {
237 fprintf(stderr,"Failed to close : %s\n",strerror(errno));
241 *f_in = from_child_pipe[0];
242 *f_out = to_child_pipe[1];
248 void out_of_memory(char *str)
250 fprintf(stderr,"out of memory in %s\n",str);
256 char *strdup(char *s)
258 int l = strlen(s) + 1;
259 char *ret = (char *)malloc(l);
267 int set_modtime(char *fname,time_t modtime)
271 tbuf.actime = time(NULL);
272 tbuf.modtime = modtime;
273 return utime(fname,&tbuf);
274 #elif defined(HAVE_UTIME)
278 return utime(fname,t);
281 t[0].tv_sec = time(NULL);
283 t[1].tv_sec = modtime;
285 return utimes(fname,t);