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)
61 return map->map+offset;
66 len = MIN(len,map->size-offset);
68 if (offset >= map->p_offset &&
69 offset+len <= map->p_offset+map->p_len) {
70 return (map->p + (offset - map->p_offset));
73 len = MAX(len,CHUNK_SIZE);
74 len = MIN(len,map->size - offset);
76 if (len > map->p_size) {
77 if (map->p) free(map->p);
78 map->p = (char *)malloc(len);
79 if (!map->p) out_of_memory("map_ptr");
83 if (lseek(map->fd,offset,SEEK_SET) != offset ||
84 (nread=read(map->fd,map->p,len)) != len) {
85 fprintf(FERROR,"EOF in map_ptr! (offset=%d len=%d nread=%d errno=%d)\n",
86 (int)offset, len, nread, errno);
90 map->p_offset = offset;
97 void unmap_file(struct map_struct *map)
101 munmap(map->map,map->size);
103 if (map->p) free(map->p);
108 /* this is taken from CVS */
109 int piped_child(char **command,int *f_in,int *f_out)
112 int to_child_pipe[2];
113 int from_child_pipe[2];
115 if (pipe(to_child_pipe) < 0 ||
116 pipe(from_child_pipe) < 0) {
117 fprintf(FERROR,"pipe: %s\n",strerror(errno));
124 fprintf(FERROR,"fork: %s\n",strerror(errno));
130 extern int orig_umask;
131 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
132 close(to_child_pipe[1]) < 0 ||
133 close(from_child_pipe[0]) < 0 ||
134 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
135 fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
138 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
139 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
141 execvp(command[0], command);
142 fprintf(FERROR,"Failed to exec %s : %s\n",
143 command[0],strerror(errno));
147 if (close(from_child_pipe[1]) < 0 ||
148 close(to_child_pipe[0]) < 0) {
149 fprintf(FERROR,"Failed to close : %s\n",strerror(errno));
153 *f_in = from_child_pipe[0];
154 *f_out = to_child_pipe[1];
160 void out_of_memory(char *str)
162 fprintf(FERROR,"out of memory in %s\n",str);
168 char *strdup(char *s)
170 int l = strlen(s) + 1;
171 char *ret = (char *)malloc(l);
179 int set_modtime(char *fname,time_t modtime)
183 tbuf.actime = time(NULL);
184 tbuf.modtime = modtime;
185 return utime(fname,&tbuf);
186 #elif defined(HAVE_UTIME)
190 return utime(fname,t);
193 t[0].tv_sec = time(NULL);
195 t[1].tv_sec = modtime;
197 return utimes(fname,t);
203 /****************************************************************************
204 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
208 ****************************************************************************/
209 int set_blocking(int fd, int set)
213 #define FLAG_TO_SET O_NONBLOCK
216 #define FLAG_TO_SET O_NDELAY
218 #define FLAG_TO_SET FNDELAY
222 if((val = fcntl(fd, F_GETFL, 0)) == -1)
224 if(set) /* Turn blocking on - ie. clear nonblock flag */
228 return fcntl( fd, F_SETFL, val);
232 /****************************************************************************
233 create any necessary directories in fname. Unfortunately we don't know
234 what perms to give the directory when this is called so we need to rely
236 ****************************************************************************/
237 int create_directory_path(char *fname)
239 extern int orig_umask;
242 while (*fname == '/') fname++;
243 while (strncmp(fname,"./",2)==0) fname += 2;
246 while ((p=strchr(p,'/'))) {
248 mkdir(fname,0777 & ~orig_umask);
256 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
257 Return LEN upon success, write's (negative) error code otherwise.
259 derived from GNU C's cccp.c.
261 int full_write(int desc, char *ptr, int len)
267 int written = write (desc, ptr, len);
275 total_written += written;
279 return total_written;
282 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
283 Return the actual number of bytes read, zero for EOF, or negative
286 derived from GNU C's cccp.c. */
287 int safe_read(int desc, char *ptr, int len)
296 n_chars = read(desc, ptr, len);
297 } while (n_chars < 0 && errno == EINTR);
299 n_chars = read(desc, ptr, len);
306 /* copy a file - this is used in conjunction with the --temp-dir option */
307 int copy_file(char *source, char *dest, mode_t mode)
312 int len; /* Number of bytes read into `buf'. */
314 ifd = open(source, O_RDONLY);
316 fprintf(FERROR,"open %s: %s\n",
317 source,strerror(errno));
321 if (unlink(dest) && errno != ENOENT) {
322 fprintf(FERROR,"unlink %s: %s\n",
323 dest,strerror(errno));
327 ofd = open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
329 fprintf(FERROR,"open %s: %s\n",
330 dest,strerror(errno));
335 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
336 if (full_write(ofd, buf, len) < 0) {
337 fprintf(FERROR,"write %s: %s\n",
338 dest,strerror(errno));
349 fprintf(FERROR,"read %s: %s\n",
350 source,strerror(errno));