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 static char *read_buffer = NULL;
72 static char *read_buffer_p = NULL;
73 static int read_buffer_len = 0;
74 static int read_buffer_size = 0;
77 void write_flush(int f)
81 void read_check(int f)
85 if (read_buffer_len == 0) {
86 read_buffer_p = read_buffer;
89 if ((n=num_waiting(f)) <= 0)
92 if (read_buffer_p != read_buffer) {
93 memmove(read_buffer,read_buffer_p,read_buffer_len);
94 read_buffer_p = read_buffer;
97 if (n > (read_buffer_size - read_buffer_len)) {
98 read_buffer_size += n;
100 read_buffer = (char *)malloc(read_buffer_size);
102 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
103 if (!read_buffer) out_of_memory("read check");
104 read_buffer_p = read_buffer;
107 n = read(f,read_buffer+read_buffer_len,n);
109 read_buffer_len += n;
114 static int readfd(int fd,char *buffer,int N)
121 if (read_buffer_len > 0) {
122 ret = MIN(read_buffer_len,N-total);
123 memcpy(buffer+total,read_buffer_p,ret);
124 read_buffer_p += ret;
125 read_buffer_len -= ret;
127 ret = read(fd,buffer + total,N - total);
141 if (readfd(f,b,4) != 4) {
143 fprintf(stderr,"Error reading %d bytes : %s\n",4,strerror(errno));
150 void read_buf(int f,char *buf,int len)
152 if (readfd(f,buf,len) != len) {
154 fprintf(stderr,"Error reading %d bytes : %s\n",len,strerror(errno));
161 char *map_file(int fd,off_t len)
163 char *ret = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
167 void unmap_file(char *buf,off_t len)
174 int read_write(int fd_in,int fd_out,int size)
176 static char *buf=NULL;
177 static int bufsize = WRITE_BLOCK_SIZE;
181 buf = (char *)malloc(bufsize);
182 if (!buf) out_of_memory("read_write");
185 while (total < size) {
186 int n = MIN(size-total,bufsize);
187 read_buf(fd_in,buf,n);
188 if (write(fd_out,buf,n) != n)
196 /* this is taken from CVS */
197 int piped_child(char **command,int *f_in,int *f_out)
200 int to_child_pipe[2];
201 int from_child_pipe[2];
203 if (pipe(to_child_pipe) < 0 ||
204 pipe(from_child_pipe) < 0) {
205 fprintf(stderr,"pipe: %s\n",strerror(errno));
212 fprintf(stderr,"fork: %s\n",strerror(errno));
218 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
219 close(to_child_pipe[1]) < 0 ||
220 close(from_child_pipe[0]) < 0 ||
221 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
222 fprintf(stderr,"Failed to dup/close : %s\n",strerror(errno));
225 execvp(command[0], command);
226 fprintf(stderr,"Failed to exec %s : %s\n",
227 command[0],strerror(errno));
231 if (close(from_child_pipe[1]) < 0 ||
232 close(to_child_pipe[0]) < 0) {
233 fprintf(stderr,"Failed to close : %s\n",strerror(errno));
237 *f_in = from_child_pipe[0];
238 *f_out = to_child_pipe[1];
244 void out_of_memory(char *str)
246 fprintf(stderr,"out of memory in %s\n",str);
252 char *strdup(char *s)
254 int l = strlen(s) + 1;
255 char *ret = (char *)malloc(l);
263 int set_modtime(char *fname,time_t modtime)
267 tbuf.actime = time(NULL);
268 tbuf.modtime = modtime;
269 return utime(fname,&tbuf);
270 #elif defined(HAVE_UTIME)
274 return utime(fname,t);
277 t[0].tv_sec = time(NULL);
279 t[1].tv_sec = modtime;
281 return utimes(fname,t);