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);
56 char *map_ptr(struct map_struct *map,off_t offset,int len)
59 return map->map+offset;
64 len = MIN(len,map->size-offset);
66 if (offset >= map->p_offset &&
67 offset+len <= map->p_offset+map->p_len) {
68 return (map->p + (offset - map->p_offset));
71 len = MAX(len,CHUNK_SIZE);
72 len = MIN(len,map->size - offset);
74 if (len > map->p_size) {
75 if (map->p) free(map->p);
76 map->p = (char *)malloc(len);
77 if (!map->p) out_of_memory("map_ptr");
81 if (lseek(map->fd,offset,SEEK_SET) != offset ||
82 read(map->fd,map->p,len) != len) {
83 fprintf(FERROR,"EOF in map_ptr!\n");
87 map->p_offset = offset;
94 void unmap_file(struct map_struct *map)
98 munmap(map->map,map->size);
100 if (map->p) free(map->p);
105 /* this is taken from CVS */
106 int piped_child(char **command,int *f_in,int *f_out)
109 int to_child_pipe[2];
110 int from_child_pipe[2];
112 if (pipe(to_child_pipe) < 0 ||
113 pipe(from_child_pipe) < 0) {
114 fprintf(FERROR,"pipe: %s\n",strerror(errno));
121 fprintf(FERROR,"fork: %s\n",strerror(errno));
127 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
128 close(to_child_pipe[1]) < 0 ||
129 close(from_child_pipe[0]) < 0 ||
130 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
131 fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
134 execvp(command[0], command);
135 fprintf(FERROR,"Failed to exec %s : %s\n",
136 command[0],strerror(errno));
140 if (close(from_child_pipe[1]) < 0 ||
141 close(to_child_pipe[0]) < 0) {
142 fprintf(FERROR,"Failed to close : %s\n",strerror(errno));
146 *f_in = from_child_pipe[0];
147 *f_out = to_child_pipe[1];
153 void out_of_memory(char *str)
155 fprintf(FERROR,"out of memory in %s\n",str);
161 char *strdup(char *s)
163 int l = strlen(s) + 1;
164 char *ret = (char *)malloc(l);
172 int set_modtime(char *fname,time_t modtime)
176 tbuf.actime = time(NULL);
177 tbuf.modtime = modtime;
178 return utime(fname,&tbuf);
179 #elif defined(HAVE_UTIME)
183 return utime(fname,t);
186 t[0].tv_sec = time(NULL);
188 t[1].tv_sec = modtime;
190 return utimes(fname,t);
196 /****************************************************************************
197 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
201 ****************************************************************************/
202 int set_blocking(int fd, int set)
206 #define FLAG_TO_SET O_NONBLOCK
209 #define FLAG_TO_SET O_NDELAY
211 #define FLAG_TO_SET FNDELAY
215 if((val = fcntl(fd, F_GETFL, 0)) == -1)
217 if(set) /* Turn blocking on - ie. clear nonblock flag */
221 return fcntl( fd, F_SETFL, val);