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