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);
57 char *map_ptr(struct map_struct *map,off_t offset,int len)
62 return map->map+offset;
67 if (len > (map->size-offset))
68 len = map->size-offset;
70 if (offset >= map->p_offset &&
71 offset+len <= map->p_offset+map->p_len) {
72 return (map->p + (offset - map->p_offset));
75 len = MAX(len,CHUNK_SIZE);
76 if (len > (map->size-offset))
77 len = map->size-offset;
79 if (len > map->p_size) {
80 if (map->p) free(map->p);
81 map->p = (char *)malloc(len);
82 if (!map->p) out_of_memory("map_ptr");
86 if (lseek(map->fd,offset,SEEK_SET) != offset ||
87 (nread=read(map->fd,map->p,len)) != len) {
88 fprintf(FERROR,"EOF in map_ptr! (offset=%d len=%d nread=%d errno=%d)\n",
89 (int)offset, len, nread, errno);
93 map->p_offset = offset;
100 void unmap_file(struct map_struct *map)
104 munmap(map->map,map->size);
106 if (map->p) free(map->p);
111 /* this is taken from CVS */
112 int piped_child(char **command,int *f_in,int *f_out)
115 int to_child_pipe[2];
116 int from_child_pipe[2];
118 if (pipe(to_child_pipe) < 0 ||
119 pipe(from_child_pipe) < 0) {
120 fprintf(FERROR,"pipe: %s\n",strerror(errno));
127 fprintf(FERROR,"fork: %s\n",strerror(errno));
133 extern int orig_umask;
134 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
135 close(to_child_pipe[1]) < 0 ||
136 close(from_child_pipe[0]) < 0 ||
137 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
138 fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
141 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
142 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
144 execvp(command[0], command);
145 fprintf(FERROR,"Failed to exec %s : %s\n",
146 command[0],strerror(errno));
150 if (close(from_child_pipe[1]) < 0 ||
151 close(to_child_pipe[0]) < 0) {
152 fprintf(FERROR,"Failed to close : %s\n",strerror(errno));
156 *f_in = from_child_pipe[0];
157 *f_out = to_child_pipe[1];
162 int local_child(int argc, char **argv,int *f_in,int *f_out)
165 int to_child_pipe[2];
166 int from_child_pipe[2];
168 if (pipe(to_child_pipe) < 0 ||
169 pipe(from_child_pipe) < 0) {
170 fprintf(FERROR,"pipe: %s\n",strerror(errno));
177 fprintf(FERROR,"fork: %s\n",strerror(errno));
182 extern int am_sender;
183 extern int am_server;
185 am_sender = !am_sender;
188 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
189 close(to_child_pipe[1]) < 0 ||
190 close(from_child_pipe[0]) < 0 ||
191 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
192 fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
195 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
196 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
197 start_server(argc, argv);
200 if (close(from_child_pipe[1]) < 0 ||
201 close(to_child_pipe[0]) < 0) {
202 fprintf(FERROR,"Failed to close : %s\n",strerror(errno));
206 *f_in = from_child_pipe[0];
207 *f_out = to_child_pipe[1];
214 void out_of_memory(char *str)
216 fprintf(FERROR,"ERROR: out of memory in %s\n",str);
220 void overflow(char *str)
222 fprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
228 int set_modtime(char *fname,time_t modtime)
231 if (dry_run) return 0;
235 tbuf.actime = time(NULL);
236 tbuf.modtime = modtime;
237 return utime(fname,&tbuf);
238 #elif defined(HAVE_UTIME)
242 return utime(fname,t);
245 t[0].tv_sec = time(NULL);
247 t[1].tv_sec = modtime;
249 return utimes(fname,t);
256 /****************************************************************************
257 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
261 ****************************************************************************/
262 int set_blocking(int fd, int set)
266 #define FLAG_TO_SET O_NONBLOCK
269 #define FLAG_TO_SET O_NDELAY
271 #define FLAG_TO_SET FNDELAY
275 if((val = fcntl(fd, F_GETFL, 0)) == -1)
277 if(set) /* Turn blocking on - ie. clear nonblock flag */
281 return fcntl( fd, F_SETFL, val);
285 /****************************************************************************
286 create any necessary directories in fname. Unfortunately we don't know
287 what perms to give the directory when this is called so we need to rely
289 ****************************************************************************/
290 int create_directory_path(char *fname)
292 extern int orig_umask;
295 while (*fname == '/') fname++;
296 while (strncmp(fname,"./",2)==0) fname += 2;
299 while ((p=strchr(p,'/'))) {
301 do_mkdir(fname,0777 & ~orig_umask);
309 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
310 Return LEN upon success, write's (negative) error code otherwise.
312 derived from GNU C's cccp.c.
314 int full_write(int desc, char *ptr, int len)
320 int written = write (desc, ptr, len);
328 total_written += written;
332 return total_written;
335 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
336 Return the actual number of bytes read, zero for EOF, or negative
339 derived from GNU C's cccp.c. */
340 int safe_read(int desc, char *ptr, int len)
349 n_chars = read(desc, ptr, len);
350 } while (n_chars < 0 && errno == EINTR);
352 n_chars = read(desc, ptr, len);
359 /* copy a file - this is used in conjunction with the --temp-dir option */
360 int copy_file(char *source, char *dest, mode_t mode)
365 int len; /* Number of bytes read into `buf'. */
367 ifd = open(source, O_RDONLY);
369 fprintf(FERROR,"open %s: %s\n",
370 source,strerror(errno));
374 if (do_unlink(dest) && errno != ENOENT) {
375 fprintf(FERROR,"unlink %s: %s\n",
376 dest,strerror(errno));
380 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
382 fprintf(FERROR,"open %s: %s\n",
383 dest,strerror(errno));
388 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
389 if (full_write(ofd, buf, len) < 0) {
390 fprintf(FERROR,"write %s: %s\n",
391 dest,strerror(errno));
402 fprintf(FERROR,"read %s: %s\n",
403 source,strerror(errno));
410 /* sleep for a while via select */
411 void u_sleep(int usec)
417 select(0, NULL, NULL, NULL, &tv);
421 static pid_t all_pids[10];
424 /* fork and record the pid of the child */
427 pid_t newpid = fork();
430 all_pids[num_pids++] = newpid;
435 /* kill all children */
436 void kill_all(int sig)
439 for (i=0;i<num_pids;i++) {
440 if (all_pids[i] != getpid())
441 kill(all_pids[i], sig);