2 * File IO utilities used in rsync.
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2004-2020 Wayne Davison
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.
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.
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.
26 #define ENODATA EAGAIN
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)
36 extern int sparse_files
;
38 OFF_T preallocated_len
= 0;
40 static OFF_T sparse_seek
= 0;
41 static OFF_T sparse_past_write
= 0;
43 int sparse_end(int f
, OFF_T size
)
51 ret
= do_ftruncate(f
, size
);
53 if (do_lseek(f
, sparse_seek
-1, SEEK_CUR
) != size
-1)
57 ret
= write(f
, "", 1);
58 } while (ret
< 0 && errno
== EINTR
);
60 ret
= ret
<= 0 ? -1 : 0;
69 /* Note that the offset is just the caller letting us know where
70 * the current file position is in the file. The use_seek arg tells
71 * us that we should seek over matching data instead of writing it. */
72 static int write_sparse(int f
, int use_seek
, OFF_T offset
, const char *buf
, int len
)
77 for (l1
= 0; l1
< len
&& buf
[l1
] == 0; l1
++) {}
78 for (l2
= 0; l2
< len
-l1
&& buf
[len
-(l2
+1)] == 0; l2
++) {}
86 if (sparse_past_write
>= preallocated_len
) {
87 if (do_lseek(f
, sparse_seek
, SEEK_CUR
) < 0)
89 } else if (do_punch_hole(f
, sparse_past_write
, sparse_seek
) < 0) {
95 sparse_past_write
= offset
+ len
- l2
;
98 /* The in-place data already matches. */
99 if (do_lseek(f
, len
- (l1
+l2
), SEEK_CUR
) < 0)
104 while ((ret
= write(f
, buf
+ l1
, len
- (l1
+l2
))) <= 0) {
105 if (ret
< 0 && errno
== EINTR
)
111 if (ret
!= (int)(len
- (l1
+l2
))) {
119 static char *wf_writeBuf
;
120 static size_t wf_writeBufSize
;
121 static size_t wf_writeBufCnt
;
123 int flush_write_file(int f
)
126 char *bp
= wf_writeBuf
;
128 while (wf_writeBufCnt
> 0) {
129 if ((ret
= write(f
, bp
, wf_writeBufCnt
)) < 0) {
134 wf_writeBufCnt
-= ret
;
140 /* write_file does not allow incomplete writes. It loops internally
141 * until len bytes are written or errno is set. Note that use_seek and
142 * offset are only used in sparse processing (see write_sparse()). */
143 int write_file(int f
, int use_seek
, OFF_T offset
, const char *buf
, int len
)
149 if (sparse_files
> 0) {
150 int len1
= MIN(len
, SPARSE_WRITE_SIZE
);
151 r1
= write_sparse(f
, use_seek
, offset
, buf
, len1
);
155 wf_writeBufSize
= WRITE_SIZE
* 8;
157 wf_writeBuf
= new_array(char, wf_writeBufSize
);
159 out_of_memory("write_file");
161 r1
= (int)MIN((size_t)len
, wf_writeBufSize
- wf_writeBufCnt
);
163 memcpy(wf_writeBuf
+ wf_writeBufCnt
, buf
, r1
);
164 wf_writeBufCnt
+= r1
;
166 if (wf_writeBufCnt
== wf_writeBufSize
) {
167 if (flush_write_file(f
) < 0)
185 /* An in-place update found identical data at an identical location. We either
186 * just seek past it, or (for an in-place sparse update), we give the data to
187 * the sparse processor with the use_seek flag set. */
188 int skip_matched(int fd
, OFF_T offset
, const char *buf
, int len
)
192 if (sparse_files
> 0) {
193 if (write_file(fd
, 1, offset
, buf
, len
) != len
)
198 if (flush_write_file(fd
) < 0)
201 if ((pos
= do_lseek(fd
, len
, SEEK_CUR
)) != offset
+ len
) {
202 rsyserr(FERROR_XFER
, errno
, "lseek returned %s, not %s",
203 big_num(pos
), big_num(offset
));
210 /* This provides functionality somewhat similar to mmap() but using read().
211 * It gives sliding window access to a file. mmap() is not used because of
212 * the possibility of another program (such as a mailer) truncating the
213 * file thus giving us a SIGBUS. */
214 struct map_struct
*map_file(int fd
, OFF_T len
, int32 read_size
, int32 blk_size
)
216 struct map_struct
*map
;
218 if (!(map
= new0(struct map_struct
)))
219 out_of_memory("map_file");
221 if (blk_size
&& (read_size
% blk_size
))
222 read_size
+= blk_size
- (read_size
% blk_size
);
225 map
->file_size
= len
;
226 map
->def_window_size
= ALIGNED_LENGTH(read_size
);
232 /* slide the read window in the file */
233 char *map_ptr(struct map_struct
*map
, OFF_T offset
, int32 len
)
235 OFF_T window_start
, read_start
;
236 int32 window_size
, read_size
, read_offset
, align_fudge
;
241 rprintf(FERROR
, "invalid len passed to map_ptr: %ld\n",
243 exit_cleanup(RERR_FILEIO
);
246 /* in most cases the region will already be available */
247 if (offset
>= map
->p_offset
&& offset
+len
<= map
->p_offset
+map
->p_len
)
248 return map
->p
+ (offset
- map
->p_offset
);
250 /* nope, we are going to have to do a read. Work out our desired window */
251 align_fudge
= (int32
)ALIGNED_OVERSHOOT(offset
);
252 window_start
= offset
- align_fudge
;
253 window_size
= map
->def_window_size
;
254 if (window_start
+ window_size
> map
->file_size
)
255 window_size
= (int32
)(map
->file_size
- window_start
);
256 if (window_size
< len
+ align_fudge
)
257 window_size
= ALIGNED_LENGTH(len
+ align_fudge
);
259 /* make sure we have allocated enough memory for the window */
260 if (window_size
> map
->p_size
) {
261 map
->p
= realloc_array(map
->p
, char, window_size
);
263 out_of_memory("map_ptr");
264 map
->p_size
= window_size
;
267 /* Now try to avoid re-reading any bytes by reusing any bytes from the previous buffer. */
268 if (window_start
>= map
->p_offset
&& window_start
< map
->p_offset
+ map
->p_len
269 && window_start
+ window_size
>= map
->p_offset
+ map
->p_len
) {
270 read_start
= map
->p_offset
+ map
->p_len
;
271 read_offset
= (int32
)(read_start
- window_start
);
272 read_size
= window_size
- read_offset
;
273 memmove(map
->p
, map
->p
+ (map
->p_len
- read_offset
), read_offset
);
275 read_start
= window_start
;
276 read_size
= window_size
;
280 if (read_size
<= 0) {
281 rprintf(FERROR
, "invalid read_size of %ld in map_ptr\n",
283 exit_cleanup(RERR_FILEIO
);
286 if (map
->p_fd_offset
!= read_start
) {
287 OFF_T ret
= do_lseek(map
->fd
, read_start
, SEEK_SET
);
288 if (ret
!= read_start
) {
289 rsyserr(FERROR
, errno
, "lseek returned %s, not %s",
290 big_num(ret
), big_num(read_start
));
291 exit_cleanup(RERR_FILEIO
);
293 map
->p_fd_offset
= read_start
;
295 map
->p_offset
= window_start
;
296 map
->p_len
= window_size
;
298 while (read_size
> 0) {
299 int32 nread
= read(map
->fd
, map
->p
+ read_offset
, read_size
);
302 map
->status
= nread
? errno
: ENODATA
;
303 /* The best we can do is zero the buffer -- the file
304 * has changed mid transfer! */
305 memset(map
->p
+ read_offset
, 0, read_size
);
308 map
->p_fd_offset
+= nread
;
309 read_offset
+= nread
;
313 return map
->p
+ align_fudge
;
316 int unmap_file(struct map_struct
*map
)
325 #if 0 /* I don't think we really need this. */
326 force_memzero(map
, sizeof map
[0]);