Prepare for future release of XXH3 & XXH128.
[rsync.git] / fileio.c
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-2020 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)
44 {
45         int ret;
46
47         sparse_past_write = 0;
48
49         if (!sparse_seek)
50                 return 0;
51
52 #ifdef HAVE_FTRUNCATE
53         ret = do_ftruncate(f, size);
54 #else
55         if (do_lseek(f, sparse_seek-1, SEEK_CUR) != size-1)
56                 ret = -1;
57         else {
58                 do {
59                         ret = write(f, "", 1);
60                 } while (ret < 0 && errno == EINTR);
61
62                 ret = ret <= 0 ? -1 : 0;
63         }
64 #endif
65
66         sparse_seek = 0;
67
68         return ret;
69 }
70
71 /* Note that the offset is just the caller letting us know where
72  * the current file position is in the file. The use_seek arg tells
73  * us that we should seek over matching data instead of writing it. */
74 static int write_sparse(int f, int use_seek, OFF_T offset, const char *buf, int len)
75 {
76         int l1 = 0, l2 = 0;
77         int ret;
78
79         for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
80         for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
81
82         sparse_seek += l1;
83
84         if (l1 == len)
85                 return len;
86
87         if (sparse_seek) {
88                 if (sparse_past_write >= preallocated_len) {
89                         if (do_lseek(f, sparse_seek, SEEK_CUR) < 0)
90                                 return -1;
91                 } else if (do_punch_hole(f, sparse_past_write, sparse_seek) < 0) {
92                         sparse_seek = 0;
93                         return -1;
94                 }
95         }
96         sparse_seek = l2;
97         sparse_past_write = offset + len - l2;
98
99         if (use_seek) {
100                 /* The in-place data already matches. */
101                 if (do_lseek(f, len - (l1+l2), SEEK_CUR) < 0)
102                         return -1;
103                 return len;
104         }
105
106         while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) {
107                 if (ret < 0 && errno == EINTR)
108                         continue;
109                 sparse_seek = 0;
110                 return ret;
111         }
112
113         if (ret != (int)(len - (l1+l2))) {
114                 sparse_seek = 0;
115                 return l1+ret;
116         }
117
118         return len;
119 }
120
121 static char *wf_writeBuf;
122 static size_t wf_writeBufSize;
123 static size_t wf_writeBufCnt;
124
125 int flush_write_file(int f)
126 {
127         int ret = 0;
128         char *bp = wf_writeBuf;
129
130         while (wf_writeBufCnt > 0) {
131                 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
132                         if (errno == EINTR)
133                                 continue;
134                         return ret;
135                 }
136                 wf_writeBufCnt -= ret;
137                 bp += ret;
138         }
139         return ret;
140 }
141
142 /* write_file does not allow incomplete writes.  It loops internally
143  * until len bytes are written or errno is set.  Note that use_seek and
144  * offset are only used in sparse processing (see write_sparse()). */
145 int write_file(int f, int use_seek, OFF_T offset, const char *buf, int len)
146 {
147         int ret = 0;
148
149         while (len > 0) {
150                 int r1;
151                 if (sparse_files > 0) {
152                         int len1 = MIN(len, SPARSE_WRITE_SIZE);
153                         r1 = write_sparse(f, use_seek, offset, buf, len1);
154                         offset += r1;
155                 } else {
156                         if (!wf_writeBuf) {
157                                 wf_writeBufSize = WRITE_SIZE * 8;
158                                 wf_writeBufCnt  = 0;
159                                 wf_writeBuf = new_array(char, wf_writeBufSize);
160                                 if (!wf_writeBuf)
161                                         out_of_memory("write_file");
162                         }
163                         r1 = (int)MIN((size_t)len, wf_writeBufSize - wf_writeBufCnt);
164                         if (r1) {
165                                 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
166                                 wf_writeBufCnt += r1;
167                         }
168                         if (wf_writeBufCnt == wf_writeBufSize) {
169                                 if (flush_write_file(f) < 0)
170                                         return -1;
171                                 if (!r1 && len)
172                                         continue;
173                         }
174                 }
175                 if (r1 <= 0) {
176                         if (ret > 0)
177                                 return ret;
178                         return r1;
179                 }
180                 len -= r1;
181                 buf += r1;
182                 ret += r1;
183         }
184         return ret;
185 }
186
187 /* An in-place update found identical data at an identical location. We either
188  * just seek past it, or (for an in-place sparse update), we give the data to
189  * the sparse processor with the use_seek flag set. */
190 int skip_matched(int fd, OFF_T offset, const char *buf, int len)
191 {
192         OFF_T pos;
193
194         if (sparse_files > 0) {
195                 if (write_file(fd, 1, offset, buf, len) != len)
196                         return -1;
197                 return 0;
198         }
199
200         if (flush_write_file(fd) < 0)
201                 return -1;
202
203         if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset + len) {
204                 rsyserr(FERROR_XFER, errno, "lseek returned %s, not %s",
205                         big_num(pos), big_num(offset));
206                 return -1;
207         }
208
209         return 0;
210 }
211
212 /* This provides functionality somewhat similar to mmap() but using read().
213  * It gives sliding window access to a file.  mmap() is not used because of
214  * the possibility of another program (such as a mailer) truncating the
215  * file thus giving us a SIGBUS. */
216 struct map_struct *map_file(int fd, OFF_T len, int32 read_size, int32 blk_size)
217 {
218         struct map_struct *map;
219
220         if (!(map = new0(struct map_struct)))
221                 out_of_memory("map_file");
222
223         if (blk_size && (read_size % blk_size))
224                 read_size += blk_size - (read_size % blk_size);
225
226         map->fd = fd;
227         map->file_size = len;
228         map->def_window_size = ALIGNED_LENGTH(read_size);
229
230         return map;
231 }
232
233
234 /* slide the read window in the file */
235 char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
236 {
237         OFF_T window_start, read_start;
238         int32 window_size, read_size, read_offset, align_fudge;
239
240         if (len == 0)
241                 return NULL;
242         if (len < 0) {
243                 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
244                         (long)len);
245                 exit_cleanup(RERR_FILEIO);
246         }
247
248         /* in most cases the region will already be available */
249         if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
250                 return map->p + (offset - map->p_offset);
251
252         /* nope, we are going to have to do a read. Work out our desired window */
253         align_fudge = (int32)ALIGNED_OVERSHOOT(offset);
254         window_start = offset - align_fudge;
255         window_size = map->def_window_size;
256         if (window_start + window_size > map->file_size)
257                 window_size = (int32)(map->file_size - window_start);
258         if (window_size < len + align_fudge)
259                 window_size = ALIGNED_LENGTH(len + align_fudge);
260
261         /* make sure we have allocated enough memory for the window */
262         if (window_size > map->p_size) {
263                 map->p = realloc_array(map->p, char, window_size);
264                 if (!map->p)
265                         out_of_memory("map_ptr");
266                 map->p_size = window_size;
267         }
268
269         /* Now try to avoid re-reading any bytes by reusing any bytes from the previous buffer. */
270         if (window_start >= map->p_offset && window_start < map->p_offset + map->p_len
271          && window_start + window_size >= map->p_offset + map->p_len) {
272                 read_start = map->p_offset + map->p_len;
273                 read_offset = (int32)(read_start - window_start);
274                 read_size = window_size - read_offset;
275                 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
276         } else {
277                 read_start = window_start;
278                 read_size = window_size;
279                 read_offset = 0;
280         }
281
282         if (read_size <= 0) {
283                 rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
284                         (long)read_size);
285                 exit_cleanup(RERR_FILEIO);
286         }
287
288         if (map->p_fd_offset != read_start) {
289                 OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
290                 if (ret != read_start) {
291                         rsyserr(FERROR, errno, "lseek returned %s, not %s",
292                                 big_num(ret), big_num(read_start));
293                         exit_cleanup(RERR_FILEIO);
294                 }
295                 map->p_fd_offset = read_start;
296         }
297         map->p_offset = window_start;
298         map->p_len = window_size;
299
300         while (read_size > 0) {
301                 int32 nread = read(map->fd, map->p + read_offset, read_size);
302                 if (nread <= 0) {
303                         if (!map->status)
304                                 map->status = nread ? errno : ENODATA;
305                         /* The best we can do is zero the buffer -- the file
306                          * has changed mid transfer! */
307                         memset(map->p + read_offset, 0, read_size);
308                         break;
309                 }
310                 map->p_fd_offset += nread;
311                 read_offset += nread;
312                 read_size -= nread;
313         }
314
315         return map->p + align_fudge;
316 }
317
318 int unmap_file(struct map_struct *map)
319 {
320         int     ret;
321
322         if (map->p) {
323                 free(map->p);
324                 map->p = NULL;
325         }
326         ret = map->status;
327 #if 0 /* I don't think we really need this. */
328         force_memzero(map, sizeof map[0]);
329 #endif
330         free(map);
331
332         return ret;
333 }