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 void write_flush(int f)
67 int readfd(int fd,char *buffer,int N)
74 ret = read(fd,buffer + total,N - total);
87 if (readfd(f,b,4) != 4) {
89 fprintf(stderr,"Error reading %d bytes : %s\n",4,strerror(errno));
96 void read_buf(int f,char *buf,int len)
98 if (readfd(f,buf,len) != len) {
100 fprintf(stderr,"Error reading %d bytes : %s\n",len,strerror(errno));
107 char *map_file(int fd,off_t len)
109 char *ret = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
113 void unmap_file(char *buf,off_t len)
120 int read_write(int fd_in,int fd_out,int size)
122 static char *buf=NULL;
123 static int bufsize = WRITE_BLOCK_SIZE;
127 buf = (char *)malloc(bufsize);
128 if (!buf) out_of_memory("read_write");
131 while (total < size) {
132 int n = MIN(size-total,bufsize);
133 read_buf(fd_in,buf,n);
134 if (write(fd_out,buf,n) != n)
142 /* this is taken from CVS */
143 int piped_child(char **command,int *f_in,int *f_out)
146 int to_child_pipe[2];
147 int from_child_pipe[2];
149 if (pipe(to_child_pipe) < 0 ||
150 pipe(from_child_pipe) < 0) {
151 fprintf(stderr,"pipe: %s\n",strerror(errno));
158 fprintf(stderr,"fork: %s\n",strerror(errno));
164 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
165 close(to_child_pipe[1]) < 0 ||
166 close(from_child_pipe[0]) < 0 ||
167 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
168 fprintf(stderr,"Failed to dup/close : %s\n",strerror(errno));
171 execvp(command[0], command);
172 fprintf(stderr,"Failed to exec %s : %s\n",
173 command[0],strerror(errno));
177 if (close(from_child_pipe[1]) < 0 ||
178 close(to_child_pipe[0]) < 0) {
179 fprintf(stderr,"Failed to close : %s\n",strerror(errno));
183 *f_in = from_child_pipe[0];
184 *f_out = to_child_pipe[1];
190 void out_of_memory(char *str)
192 fprintf(stderr,"out of memory in %s\n",str);
198 char *strdup(char *s)
200 int l = strlen(s) + 1;
201 char *ret = (char *)malloc(l);
209 int set_modtime(char *fname,time_t modtime)
213 tbuf.actime = time(NULL);
214 tbuf.modtime = modtime;
215 return utime(fname,&tbuf);
216 #elif defined(HAVE_UTIME)
220 return utime(fname,t);
223 t[0].tv_sec = time(NULL);
225 t[1].tv_sec = modtime;
227 return utimes(fname,t);