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