added some more debug info to the "EOF in map_ptr" error message to
[rsync/rsync.git] / util.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
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.
9    
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.
14    
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.
18 */
19
20 /*
21   Utilities used in rsync 
22
23   tridge, June 1996
24   */
25 #include "rsync.h"
26
27 int num_waiting(int fd)
28 {
29   int len=0;
30   ioctl(fd,FIONREAD,&len);
31   return(len);
32 }
33
34
35 struct map_struct *map_file(int fd,off_t len)
36 {
37   struct map_struct *ret;
38   ret = (struct map_struct *)malloc(sizeof(*ret));
39   if (!ret) out_of_memory("map_file");
40
41   ret->map = NULL;
42   ret->fd = fd;
43   ret->size = len;
44   ret->p = NULL;
45   ret->p_size = 0;
46   ret->p_offset = 0;
47   ret->p_len = 0;
48
49 #ifdef HAVE_MMAP
50   if (len < MAX_MAP_SIZE)
51     ret->map = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
52 #endif
53   return ret;
54 }
55
56 char *map_ptr(struct map_struct *map,off_t offset,int len)
57 {
58   int nread = -2;
59
60   if (map->map)
61     return map->map+offset;
62
63   if (len == 0) 
64     return NULL;
65
66   len = MIN(len,map->size-offset);
67
68   if (offset >= map->p_offset && 
69       offset+len <= map->p_offset+map->p_len) {
70     return (map->p + (offset - map->p_offset));
71   }
72
73   len = MAX(len,CHUNK_SIZE);
74   len = MIN(len,map->size - offset);  
75
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");
80     map->p_size = len;
81   }
82
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);
87     exit_cleanup(1);
88   }
89
90   map->p_offset = offset;
91   map->p_len = len;
92
93   return map->p; 
94 }
95
96
97 void unmap_file(struct map_struct *map)
98 {
99 #ifdef HAVE_MMAP
100   if (map->map)
101     munmap(map->map,map->size);
102 #endif
103   if (map->p) free(map->p);
104   free(map);
105 }
106
107
108 /* this is taken from CVS */
109 int piped_child(char **command,int *f_in,int *f_out)
110 {
111   int pid;
112   int to_child_pipe[2];
113   int from_child_pipe[2];
114
115   if (pipe(to_child_pipe) < 0 ||
116       pipe(from_child_pipe) < 0) {
117     fprintf(FERROR,"pipe: %s\n",strerror(errno));
118     exit_cleanup(1);
119   }
120
121
122   pid = fork();
123   if (pid < 0) {
124     fprintf(FERROR,"fork: %s\n",strerror(errno));
125     exit_cleanup(1);
126   }
127
128   if (pid == 0)
129     {
130       if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
131           close(to_child_pipe[1]) < 0 ||
132           close(from_child_pipe[0]) < 0 ||
133           dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
134         fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
135         exit_cleanup(1);
136       }
137       if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
138       if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
139       execvp(command[0], command);
140       fprintf(FERROR,"Failed to exec %s : %s\n",
141               command[0],strerror(errno));
142       exit_cleanup(1);
143     }
144
145   if (close(from_child_pipe[1]) < 0 ||
146       close(to_child_pipe[0]) < 0) {
147     fprintf(FERROR,"Failed to close : %s\n",strerror(errno));   
148     exit_cleanup(1);
149   }
150
151   *f_in = from_child_pipe[0];
152   *f_out = to_child_pipe[1];
153   
154   return pid;
155 }
156
157
158 void out_of_memory(char *str)
159 {
160   fprintf(FERROR,"out of memory in %s\n",str);
161   exit_cleanup(1);
162 }
163
164
165 #ifndef HAVE_STRDUP
166  char *strdup(char *s)
167 {
168   int l = strlen(s) + 1;
169   char *ret = (char *)malloc(l);
170   if (ret)
171     strcpy(ret,s);
172   return ret;
173 }
174 #endif
175
176
177 int set_modtime(char *fname,time_t modtime)
178 {
179 #ifdef HAVE_UTIME_H
180   struct utimbuf tbuf;  
181   tbuf.actime = time(NULL);
182   tbuf.modtime = modtime;
183   return utime(fname,&tbuf);
184 #elif defined(HAVE_UTIME)
185   time_t t[2];
186   t[0] = time(NULL);
187   t[1] = modtime;
188   return utime(fname,t);
189 #else
190   struct timeval t[2];
191   t[0].tv_sec = time(NULL);
192   t[0].tv_usec = 0;
193   t[1].tv_sec = modtime;
194   t[1].tv_usec = 0;
195   return utimes(fname,t);
196 #endif
197 }
198
199
200
201 /****************************************************************************
202 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
203 else
204 if SYSV use O_NDELAY
205 if BSD use FNDELAY
206 ****************************************************************************/
207 int set_blocking(int fd, int set)
208 {
209   int val;
210 #ifdef O_NONBLOCK
211 #define FLAG_TO_SET O_NONBLOCK
212 #else
213 #ifdef SYSV
214 #define FLAG_TO_SET O_NDELAY
215 #else /* BSD */
216 #define FLAG_TO_SET FNDELAY
217 #endif
218 #endif
219
220   if((val = fcntl(fd, F_GETFL, 0)) == -1)
221         return -1;
222   if(set) /* Turn blocking on - ie. clear nonblock flag */
223         val &= ~FLAG_TO_SET;
224   else
225     val |= FLAG_TO_SET;
226   return fcntl( fd, F_SETFL, val);
227 #undef FLAG_TO_SET
228 }