Handle EINTR in a couple places where we handle the --file-from I/O.
[rsync/rsync.git] / fileio.c
... / ...
CommitLineData
1/*
2 * File IO utilities used in rsync.
3 *
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2004-2007 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "rsync.h"
23
24#ifndef ENODATA
25#define ENODATA EAGAIN
26#endif
27
28extern int sparse_files;
29
30static char last_byte;
31static int last_sparse;
32
33int sparse_end(int f)
34{
35 if (last_sparse) {
36 do_lseek(f,-1,SEEK_CUR);
37 return (write(f,&last_byte,1) == 1 ? 0 : -1);
38 }
39 last_sparse = 0;
40 return 0;
41}
42
43
44static int write_sparse(int f,char *buf,size_t len)
45{
46 size_t l1=0, l2=0;
47 int ret;
48
49 for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
50 for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
51
52 last_byte = buf[len-1];
53
54 if (l1 == len || l2 > 0)
55 last_sparse=1;
56
57 if (l1 > 0) {
58 do_lseek(f,l1,SEEK_CUR);
59 }
60
61 if (l1 == len)
62 return len;
63
64 ret = write(f, buf + l1, len - (l1+l2));
65 if (ret == -1 || ret == 0)
66 return ret;
67 else if (ret != (int) (len - (l1+l2)))
68 return (l1+ret);
69
70 if (l2 > 0)
71 do_lseek(f,l2,SEEK_CUR);
72
73 return len;
74}
75
76
77static char *wf_writeBuf;
78static size_t wf_writeBufSize;
79static size_t wf_writeBufCnt;
80
81int flush_write_file(int f)
82{
83 int ret = 0;
84 char *bp = wf_writeBuf;
85
86 while (wf_writeBufCnt > 0) {
87 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
88 if (errno == EINTR)
89 continue;
90 return ret;
91 }
92 wf_writeBufCnt -= ret;
93 bp += ret;
94 }
95 return ret;
96}
97
98
99/*
100 * write_file does not allow incomplete writes. It loops internally
101 * until len bytes are written or errno is set.
102 */
103int write_file(int f,char *buf,size_t len)
104{
105 int ret = 0;
106
107 while (len > 0) {
108 int r1;
109 if (sparse_files > 0) {
110 int len1 = MIN(len, SPARSE_WRITE_SIZE);
111 r1 = write_sparse(f, buf, len1);
112 } else {
113 if (!wf_writeBuf) {
114 wf_writeBufSize = WRITE_SIZE * 8;
115 wf_writeBufCnt = 0;
116 wf_writeBuf = new_array(char, wf_writeBufSize);
117 if (!wf_writeBuf)
118 out_of_memory("write_file");
119 }
120 r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
121 if (r1) {
122 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
123 wf_writeBufCnt += r1;
124 }
125 if (wf_writeBufCnt == wf_writeBufSize) {
126 if (flush_write_file(f) < 0)
127 return -1;
128 if (!r1 && len)
129 continue;
130 }
131 }
132 if (r1 <= 0) {
133 if (ret > 0)
134 return ret;
135 return r1;
136 }
137 len -= r1;
138 buf += r1;
139 ret += r1;
140 }
141 return ret;
142}
143
144
145/* This provides functionality somewhat similar to mmap() but using read().
146 * It gives sliding window access to a file. mmap() is not used because of
147 * the possibility of another program (such as a mailer) truncating the
148 * file thus giving us a SIGBUS. */
149struct map_struct *map_file(int fd, OFF_T len, int32 read_size,
150 int32 blk_size)
151{
152 struct map_struct *map;
153
154 if (!(map = new(struct map_struct)))
155 out_of_memory("map_file");
156
157 if (blk_size && (read_size % blk_size))
158 read_size += blk_size - (read_size % blk_size);
159
160 memset(map, 0, sizeof map[0]);
161 map->fd = fd;
162 map->file_size = len;
163 map->def_window_size = read_size;
164
165 return map;
166}
167
168
169/* slide the read window in the file */
170char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
171{
172 int32 nread;
173 OFF_T window_start, read_start;
174 int32 window_size, read_size, read_offset;
175
176 if (len == 0)
177 return NULL;
178 if (len < 0) {
179 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
180 (long)len);
181 exit_cleanup(RERR_FILEIO);
182 }
183
184 /* in most cases the region will already be available */
185 if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
186 return map->p + (offset - map->p_offset);
187
188 /* nope, we are going to have to do a read. Work out our desired window */
189 window_start = offset;
190 window_size = map->def_window_size;
191 if (window_start + window_size > map->file_size)
192 window_size = (int32)(map->file_size - window_start);
193 if (len > window_size)
194 window_size = len;
195
196 /* make sure we have allocated enough memory for the window */
197 if (window_size > map->p_size) {
198 map->p = realloc_array(map->p, char, window_size);
199 if (!map->p)
200 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
205 * from the previous 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 = (int32)(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(FERROR, "invalid read_size of %ld in map_ptr\n",
221 (long)read_size);
222 exit_cleanup(RERR_FILEIO);
223 }
224
225 if (map->p_fd_offset != read_start) {
226 OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
227 if (ret != read_start) {
228 rsyserr(FERROR, errno, "lseek returned %.0f, not %.0f",
229 (double)ret, (double)read_start);
230 exit_cleanup(RERR_FILEIO);
231 }
232 map->p_fd_offset = read_start;
233 }
234 map->p_offset = window_start;
235 map->p_len = window_size;
236
237 while (read_size > 0) {
238 nread = read(map->fd, map->p + read_offset, read_size);
239 if (nread <= 0) {
240 if (!map->status)
241 map->status = nread ? errno : ENODATA;
242 /* The best we can do is zero the buffer -- the file
243 * has changed mid transfer! */
244 memset(map->p + read_offset, 0, read_size);
245 break;
246 }
247 map->p_fd_offset += nread;
248 read_offset += nread;
249 read_size -= nread;
250 }
251
252 return map->p;
253}
254
255
256int unmap_file(struct map_struct *map)
257{
258 int ret;
259
260 if (map->p) {
261 free(map->p);
262 map->p = NULL;
263 }
264 ret = map->status;
265 memset(map, 0, sizeof map[0]);
266 free(map);
267
268 return ret;
269}