Debian Codegraph @

fileio.c

raw symbols SCIP
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-2023 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 as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22 #include "rsync.h"
23 #include "inums.h"
24
25 #ifndef ENODATA
26 #define ENODATA EAGAIN
27 #endif
28
29 /* We want all reads to be aligned on 1K boundaries. */
30 #define ALIGN_BOUNDARY 1024
31 /* How far past the boundary is an offset? */
32 #define ALIGNED_OVERSHOOT(oft) ((oft) & (ALIGN_BOUNDARY-1))
33 /* Round up a length to the next boundary */
34 #define ALIGNED_LENGTH(len) ((((len) - 1) | (ALIGN_BOUNDARY-1)) + 1)
35
36 extern int sparse_files;
37
38 OFF_T preallocated_len = 0;
39
40 static OFF_T sparse_seek = 0;
41 static OFF_T sparse_past_write = 0;
42
43 int sparse_end(int f, OFF_T size, int updating_basis_or_equiv)
44 {
45 int ret = 0;
46
47 if (updating_basis_or_equiv) {
48 if (sparse_seek && do_punch_hole(f, sparse_past_write, sparse_seek) < 0)
49 ret = -1;
50 #ifdef HAVE_FTRUNCATE /* A compilation formality -- in-place requires ftruncate() */
51 else /* Just in case the original file was longer */
52 ret = do_ftruncate(f, size);
53 #endif
54 } else if (sparse_seek) {
55 #ifdef HAVE_FTRUNCATE
56 ret = do_ftruncate(f, size);
57 #else
58 if (do_lseek(f, sparse_seek-1, SEEK_CUR) != size-1)
59 ret = -1;
60 else {
61 do {
62 ret = write(f, "", 1);
63 } while (ret < 0 && errno == EINTR);
64
65 ret = ret <= 0 ? -1 : 0;
66 }
67 #endif
68 }
69
70 sparse_past_write = sparse_seek = 0;
71
72 return ret;
73 }
74
75 /* Note that the offset is just the caller letting us know where
76 * the current file position is in the file. The use_seek arg tells
77 * us that we should seek over matching data instead of writing it. */
78 /* Flush any deferred run of zero bytes as a hole, advancing the file
79 * position past it (both do_lseek() and do_punch_hole() move the offset). */
80 static int flush_sparse_hole(int f)
81 {
82 if (!sparse_seek)
83 return 0;
84 if (sparse_past_write >= preallocated_len) {
85 if (do_lseek(f, sparse_seek, SEEK_CUR) < 0) {
86 sparse_seek = 0;
87 return -1;
88 }
89 } else if (do_punch_hole(f, sparse_past_write, sparse_seek) < 0) {
90 sparse_seek = 0;
91 return -1;
92 }
93 sparse_seek = 0;
94 return 0;
95 }
96
97 static int full_sparse_write(int f, const char *buf, int len)
98 {
99 while (len > 0) {
100 int ret = write(f, buf, len);
101 if (ret <= 0) {
102 if (ret < 0 && errno == EINTR)
103 continue;
104 sparse_seek = 0;
105 return -1;
106 }
107 buf += ret;
108 len -= ret;
109 }
110 return 0;
111 }
112
113 /* Emit one span of data that is not being turned into a hole. For an in-place
114 * update (use_seek) the bytes on disk already match, so we only need to move
115 * past them; otherwise we write them out. Either way a deferred hole is
116 * flushed first so that the span lands at the right offset. */
117 static int emit_sparse_span(int f, int use_seek, const char *buf, int len)
118 {
119 if (flush_sparse_hole(f) < 0)
120 return -1;
121 if (use_seek)
122 return do_lseek(f, len, SEEK_CUR) < 0 ? -1 : 0;
123 return full_sparse_write(f, buf, len);
124 }
125
126 static int write_sparse(int f, int use_seek, OFF_T offset, const char *buf, int len)
127 {
128 int l1, l2, i, start, end;
129
130 /* Always treat a leading and trailing run of zeros as a (deferred)
131 * hole, since they may merge with holes in the adjacent write calls. */
132 for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
133 for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
134
135 sparse_seek += l1;
136
137 if (l1 == len)
138 return len;
139
140 /* Scan the middle [l1, len-l2) for interior runs of zeros that are at
141 * least SPARSE_WRITE_SIZE long (the hole granularity rsync has always
142 * used) and defer those as holes. Everything in between -- which may
143 * include shorter zero runs not worth a hole -- is emitted in one go,
144 * rather than being chopped into SPARSE_WRITE_SIZE-byte pieces, which
145 * made copying a large non-sparse file cost ~one write() per KiB.
146 *
147 * The matched (use_seek) case runs through the same scan: its interior
148 * zero runs still have to be punched out, which is what --inplace
149 * --sparse relies on to keep a hole-y basis file sparse. */
150 start = l1;
151 end = len - l2;
152 for (i = l1; i < end; ) {
153 int z;
154 if (buf[i] != 0) {
155 i++;
156 continue;
157 }
158 for (z = 1; i + z < end && buf[i+z] == 0; z++) {}
159 if (z < SPARSE_WRITE_SIZE) {
160 i += z;
161 continue;
162 }
163 if (i > start) {
164 if (emit_sparse_span(f, use_seek, buf + start, i - start) < 0)
165 return -1;
166 sparse_past_write = offset + i;
167 }
168 sparse_seek += z;
169 i += z;
170 start = i;
171 }
172 if (end > start) {
173 if (emit_sparse_span(f, use_seek, buf + start, end - start) < 0)
174 return -1;
175 }
176
177 sparse_seek = l2;
178 sparse_past_write = offset + len - l2;
179
180 return len;
181 }
182
183 static char *wf_writeBuf;
184 static size_t wf_writeBufSize;
185 static size_t wf_writeBufCnt;
186
187 int flush_write_file(int f)
188 {
189 int ret = 0;
190 char *bp = wf_writeBuf;
191
192 while (wf_writeBufCnt > 0) {
193 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
194 if (errno == EINTR)
195 continue;
196 return ret;
197 }
198 wf_writeBufCnt -= ret;
199 bp += ret;
200 }
201 return ret;
202 }
203
204 /* write_file does not allow incomplete writes. It loops internally
205 * until len bytes are written or errno is set. Note that use_seek and
206 * offset are only used in sparse processing (see write_sparse()). */
207 int write_file(int f, int use_seek, OFF_T offset, const char *buf, int len)
208 {
209 int ret = 0;
210
211 while (len > 0) {
212 int r1;
213 if (sparse_files > 0) {
214 /* write_sparse() handles the whole span itself, scanning
215 * for holes and coalescing the non-zero data into large
216 * write()s instead of SPARSE_WRITE_SIZE-byte dribbles. */
217 r1 = write_sparse(f, use_seek, offset, buf, len);
218 offset += r1;
219 } else {
220 if (!wf_writeBuf) {
221 wf_writeBufSize = WRITE_SIZE * 8;
222 wf_writeBufCnt = 0;
223 wf_writeBuf = new_array(char, wf_writeBufSize);
224 }
225 r1 = (int)MIN((size_t)len, wf_writeBufSize - wf_writeBufCnt);
226 if (r1) {
227 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
228 wf_writeBufCnt += r1;
229 }
230 if (wf_writeBufCnt == wf_writeBufSize) {
231 if (flush_write_file(f) < 0)
232 return -1;
233 if (!r1 && len)
234 continue;
235 }
236 }
237 if (r1 <= 0) {
238 if (ret > 0)
239 return ret;
240 return r1;
241 }
242 len -= r1;
243 buf += r1;
244 ret += r1;
245 }
246 return ret;
247 }
248
249 /* An in-place update found identical data at an identical location. We either
250 * just seek past it, or (for an in-place sparse update), we give the data to
251 * the sparse processor with the use_seek flag set. */
252 int skip_matched(int fd, OFF_T offset, const char *buf, int len)
253 {
254 OFF_T pos;
255
256 if (sparse_files > 0) {
257 if (write_file(fd, 1, offset, buf, len) != len)
258 return -1;
259 return 0;
260 }
261
262 if (flush_write_file(fd) < 0)
263 return -1;
264
265 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset + len) {
266 rsyserr(FERROR_XFER, errno, "lseek returned %s, not %s",
267 big_num(pos), big_num(offset));
268 return -1;
269 }
270
271 return 0;
272 }
273
274 /* This provides functionality somewhat similar to mmap() but using read().
275 * It gives sliding window access to a file. mmap() is not used because of
276 * the possibility of another program (such as a mailer) truncating the
277 * file thus giving us a SIGBUS. */
278 struct map_struct *map_file(int fd, OFF_T len, int32 read_size, int32 blk_size)
279 {
280 struct map_struct *map;
281
282 map = new0(struct map_struct);
283
284 if (blk_size && (read_size % blk_size))
285 read_size += blk_size - (read_size % blk_size);
286
287 map->fd = fd;
288 map->file_size = len;
289 map->def_window_size = ALIGNED_LENGTH(read_size);
290
291 return map;
292 }
293
294
295 /* slide the read window in the file */
296 char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
297 {
298 OFF_T window_start, read_start;
299 int32 window_size, read_size, read_offset, align_fudge;
300
301 if (len == 0)
302 return NULL;
303 if (len < 0) {
304 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
305 (long)len);
306 exit_cleanup(RERR_FILEIO);
307 }
308
309 /* in most cases the region will already be available */
310 if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
311 return map->p + (offset - map->p_offset);
312
313 /* nope, we are going to have to do a read. Work out our desired window */
314 align_fudge = (int32)ALIGNED_OVERSHOOT(offset);
315 window_start = offset - align_fudge;
316 window_size = map->def_window_size;
317 if (window_start + window_size > map->file_size)
318 window_size = (int32)(map->file_size - window_start);
319 if (window_size < len + align_fudge)
320 window_size = ALIGNED_LENGTH(len + align_fudge);
321
322 /* make sure we have allocated enough memory for the window */
323 if (window_size > map->p_size) {
324 map->p = realloc_array(map->p, char, window_size);
325 map->p_size = window_size;
326 }
327
328 /* Now try to avoid re-reading any bytes by reusing any bytes from the previous buffer. */
329 if (window_start >= map->p_offset && window_start < map->p_offset + map->p_len
330 && window_start + window_size >= map->p_offset + map->p_len) {
331 read_start = map->p_offset + map->p_len;
332 read_offset = (int32)(read_start - window_start);
333 read_size = window_size - read_offset;
334 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
335 } else {
336 read_start = window_start;
337 read_size = window_size;
338 read_offset = 0;
339 }
340
341 if (read_size <= 0) {
342 rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
343 (long)read_size);
344 exit_cleanup(RERR_FILEIO);
345 }
346
347 if (map->p_fd_offset != read_start) {
348 OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
349 if (ret != read_start) {
350 rsyserr(FERROR, errno, "lseek returned %s, not %s",
351 big_num(ret), big_num(read_start));
352 exit_cleanup(RERR_FILEIO);
353 }
354 map->p_fd_offset = read_start;
355 }
356 map->p_offset = window_start;
357 map->p_len = window_size;
358
359 while (read_size > 0) {
360 int32 nread = read(map->fd, map->p + read_offset, read_size);
361 if (nread <= 0) {
362 if (!map->status)
363 map->status = nread ? errno : ENODATA;
364 /* The best we can do is zero the buffer -- the file
365 * has changed mid transfer! */
366 memset(map->p + read_offset, 0, read_size);
367 break;
368 }
369 map->p_fd_offset += nread;
370 read_offset += nread;
371 read_size -= nread;
372 }
373
374 return map->p + align_fudge;
375 }
376
377 int unmap_file(struct map_struct *map)
378 {
379 int ret;
380
381 if (map->p) {
382 free(map->p);
383 map->p = NULL;
384 }
385 ret = map->status;
386 #if 0 /* I don't think we really need this. */
387 force_memzero(map, sizeof map[0]);
388 #endif
389 free(map);
390
391 return ret;
392 }