Applied a slightly-tweaked version of Oliver Braun's patch that
[rsync/rsync.git] / fileio.c
1 /* 
2    Copyright (C) Andrew Tridgell 1998
3    Copyright (C) 2002 by Martin Pool
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   File IO utilities used in rsync 
22   */
23 #include "rsync.h"
24
25 static char last_byte;
26 static int last_sparse;
27 extern int sparse_files;
28
29 int sparse_end(int f)
30 {
31         if (last_sparse) {
32                 do_lseek(f,-1,SEEK_CUR);
33                 return (write(f,&last_byte,1) == 1 ? 0 : -1);
34         }
35         last_sparse = 0;
36         return 0;
37 }
38
39
40 static int write_sparse(int f,char *buf,size_t len)
41 {
42         size_t l1=0, l2=0;
43         int ret;
44
45         for (l1=0;l1<len && buf[l1]==0;l1++) ;
46         for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
47
48         last_byte = buf[len-1];
49
50         if (l1 == len || l2 > 0)
51                 last_sparse=1;
52
53         if (l1 > 0) {
54                 do_lseek(f,l1,SEEK_CUR);  
55         }
56
57         if (l1 == len) 
58                 return len;
59
60         ret = write(f, buf + l1, len - (l1+l2));
61         if (ret == -1 || ret == 0)
62                 return ret;
63         else if (ret != (int) (len - (l1+l2))) 
64                 return (l1+ret);
65
66         if (l2 > 0)
67                 do_lseek(f,l2,SEEK_CUR);
68         
69         return len;
70 }
71
72
73 static char *wf_writeBuf;
74 static size_t wf_writeBufSize;
75 static size_t wf_writeBufCnt;
76
77 int flush_write_file(int f)
78 {
79         int ret = write(f, wf_writeBuf, wf_writeBufCnt);
80         if (ret < 0)
81                 return ret;
82         /* if (ret < wf_writeBufCnt) ??? */
83         wf_writeBufCnt = 0;
84         return ret;
85 }
86
87 /*
88  * write_file does not allow incomplete writes.  It loops internally
89  * until len bytes are written or errno is set.
90  */
91 int write_file(int f,char *buf,size_t len)
92 {
93         int ret = 0;
94
95         while (len>0) {
96                 int r1;
97                 if (sparse_files) {
98                         int len1 = MIN(len, SPARSE_WRITE_SIZE);
99                         r1 = write_sparse(f, buf, len1);
100                 } else {
101                         if (!wf_writeBuf) {
102                                 wf_writeBufSize = MAX_MAP_SIZE;
103                                 wf_writeBufCnt  = 0;
104                                 wf_writeBuf = new_array(char, MAX_MAP_SIZE);
105                                 if (!wf_writeBuf) out_of_memory("write_file");
106                         }
107                         r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
108                         if (r1) {
109                                 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
110                                 wf_writeBufCnt += r1;
111                         }
112                         if (wf_writeBufCnt == wf_writeBufSize) {
113                                 if (flush_write_file(f) < 0) return -1;
114                                 if (!r1 && len)
115                                         continue;
116                         }
117                 }
118                 if (r1 <= 0) {
119                         if (ret > 0) return ret;
120                         return r1;
121                 }
122                 len -= r1;
123                 buf += r1;
124                 ret += r1;
125         }
126         return ret;
127 }
128
129
130
131 /* this provides functionality somewhat similar to mmap() but using
132    read(). It gives sliding window access to a file. mmap() is not
133    used because of the possibility of another program (such as a
134    mailer) truncating the file thus giving us a SIGBUS */
135 struct map_struct *map_file(int fd,OFF_T len)
136 {
137         struct map_struct *map;
138         map = new(struct map_struct);
139         if (!map) out_of_memory("map_file");
140
141         map->fd = fd;
142         map->file_size = len;
143         map->p = NULL;
144         map->p_size = 0;
145         map->p_offset = 0;
146         map->p_fd_offset = 0;
147         map->p_len = 0;
148         map->status = 0;
149
150         return map;
151 }
152
153 /* slide the read window in the file */
154 char *map_ptr(struct map_struct *map,OFF_T offset,int len)
155 {
156         int nread;
157         OFF_T window_start, read_start;
158         int window_size, read_size, read_offset;
159
160         if (len == 0) {
161                 return NULL;
162         }
163
164         /* can't go beyond the end of file */
165         if (len > (map->file_size - offset)) {
166                 len = map->file_size - offset;
167         }
168
169         /* in most cases the region will already be available */
170         if (offset >= map->p_offset && 
171             offset+len <= map->p_offset+map->p_len) {
172                 return (map->p + (offset - map->p_offset));
173         }
174
175
176         /* nope, we are going to have to do a read. Work out our desired window */
177         if (offset > 2*CHUNK_SIZE) {
178                 window_start = offset - 2*CHUNK_SIZE;
179                 window_start &= ~((OFF_T)(CHUNK_SIZE-1)); /* assumes power of 2 */
180         } else {
181                 window_start = 0;
182         }
183         window_size = MAX_MAP_SIZE;
184         if (window_start + window_size > map->file_size) {
185                 window_size = map->file_size - window_start;
186         }
187         if (offset + len > window_start + window_size) {
188                 window_size = (offset+len) - window_start;
189         }
190
191         /* make sure we have allocated enough memory for the window */
192         if (window_size > map->p_size) {
193                 map->p = realloc_array(map->p, char, window_size);
194                 if (!map->p) out_of_memory("map_ptr");
195                 map->p_size = window_size;
196         }
197
198         /* now try to avoid re-reading any bytes by reusing any bytes from the previous
199            buffer. */
200         if (window_start >= map->p_offset &&
201             window_start < map->p_offset + map->p_len &&
202             window_start + window_size >= map->p_offset + map->p_len) {
203                 read_start = map->p_offset + map->p_len;
204                 read_offset = read_start - window_start;
205                 read_size = window_size - read_offset;
206                 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
207         } else {
208                 read_start = window_start;
209                 read_size = window_size;
210                 read_offset = 0;
211         }
212
213         if (read_size <= 0) {
214                 rprintf(FINFO,"Warning: unexpected read size of %d in map_ptr\n", read_size);
215         } else {
216                 if (map->p_fd_offset != read_start) {
217                         if (do_lseek(map->fd,read_start,SEEK_SET) != read_start) {
218                                 rprintf(FERROR,"lseek failed in map_ptr\n");
219                                 exit_cleanup(RERR_FILEIO);
220                         }
221                         map->p_fd_offset = read_start;
222                 }
223
224                 if ((nread=read(map->fd,map->p + read_offset,read_size)) != read_size) {
225                         if (nread < 0) {
226                                 nread = 0;
227                                 if (!map->status)
228                                         map->status = errno;
229                         }
230                         /* the best we can do is zero the buffer - the file
231                            has changed mid transfer! */
232                         memset(map->p+read_offset+nread, 0, read_size - nread);
233                 }
234                 map->p_fd_offset += nread;
235         }
236
237         map->p_offset = window_start;
238         map->p_len = window_size;
239   
240         return map->p + (offset - map->p_offset); 
241 }
242
243
244 int unmap_file(struct map_struct *map)
245 {
246         int     ret;
247
248         if (map->p) {
249                 free(map->p);
250                 map->p = NULL;
251         }
252         ret = map->status;
253         memset(map, 0, sizeof(*map));
254         free(map);
255
256         return ret;
257 }
258