2 * File IO utilities used in rsync.
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2004-2013 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 boundries. */
30 #define ALIGN_BOUNDRY 1024
31 /* How far past the boundary is an offset? */
32 #define ALIGNED_OVERSHOOT(oft) ((oft) & (ALIGN_BOUNDRY-1))
33 /* Round up a length to the next boundary */
34 #define ALIGNED_LENGTH(len) ((((len) - 1) | (ALIGN_BOUNDRY-1)) + 1)
36 extern int sparse_files
;
38 static OFF_T sparse_seek
= 0;
40 int sparse_end(int f
, OFF_T size
)
48 ret
= do_ftruncate(f
, size
);
50 if (do_lseek(f
, sparse_seek
-1, SEEK_CUR
) != size
-1)
54 ret
= write(f
, "", 1);
55 } while (ret
< 0 && errno
== EINTR
);
57 ret
= ret
<= 0 ? -1 : 0;
67 static int write_sparse(int f
, char *buf
, int len
)
72 for (l1
= 0; l1
< len
&& buf
[l1
] == 0; l1
++) {}
73 for (l2
= 0; l2
< len
-l1
&& buf
[len
-(l2
+1)] == 0; l2
++) {}
81 do_lseek(f
, sparse_seek
, SEEK_CUR
);
84 while ((ret
= write(f
, buf
+ l1
, len
- (l1
+l2
))) <= 0) {
85 if (ret
< 0 && errno
== EINTR
)
90 if (ret
!= (int)(len
- (l1
+l2
)))
97 static char *wf_writeBuf
;
98 static size_t wf_writeBufSize
;
99 static size_t wf_writeBufCnt
;
101 int flush_write_file(int f
)
104 char *bp
= wf_writeBuf
;
106 while (wf_writeBufCnt
> 0) {
107 if ((ret
= write(f
, bp
, wf_writeBufCnt
)) < 0) {
112 wf_writeBufCnt
-= ret
;
120 * write_file does not allow incomplete writes. It loops internally
121 * until len bytes are written or errno is set.
123 int write_file(int f
, char *buf
, int len
)
129 if (sparse_files
> 0) {
130 int len1
= MIN(len
, SPARSE_WRITE_SIZE
);
131 r1
= write_sparse(f
, buf
, len1
);
134 wf_writeBufSize
= WRITE_SIZE
* 8;
136 wf_writeBuf
= new_array(char, wf_writeBufSize
);
138 out_of_memory("write_file");
140 r1
= (int)MIN((size_t)len
, wf_writeBufSize
- wf_writeBufCnt
);
142 memcpy(wf_writeBuf
+ wf_writeBufCnt
, buf
, r1
);
143 wf_writeBufCnt
+= r1
;
145 if (wf_writeBufCnt
== wf_writeBufSize
) {
146 if (flush_write_file(f
) < 0)
165 /* This provides functionality somewhat similar to mmap() but using read().
166 * It gives sliding window access to a file. mmap() is not used because of
167 * the possibility of another program (such as a mailer) truncating the
168 * file thus giving us a SIGBUS. */
169 struct map_struct
*map_file(int fd
, OFF_T len
, int32 read_size
, int32 blk_size
)
171 struct map_struct
*map
;
173 if (!(map
= new0(struct map_struct
)))
174 out_of_memory("map_file");
176 if (blk_size
&& (read_size
% blk_size
))
177 read_size
+= blk_size
- (read_size
% blk_size
);
180 map
->file_size
= len
;
181 map
->def_window_size
= ALIGNED_LENGTH(read_size
);
187 /* slide the read window in the file */
188 char *map_ptr(struct map_struct
*map
, OFF_T offset
, int32 len
)
190 OFF_T window_start
, read_start
;
191 int32 window_size
, read_size
, read_offset
, align_fudge
;
196 rprintf(FERROR
, "invalid len passed to map_ptr: %ld\n",
198 exit_cleanup(RERR_FILEIO
);
201 /* in most cases the region will already be available */
202 if (offset
>= map
->p_offset
&& offset
+len
<= map
->p_offset
+map
->p_len
)
203 return map
->p
+ (offset
- map
->p_offset
);
205 /* nope, we are going to have to do a read. Work out our desired window */
206 align_fudge
= (int32
)ALIGNED_OVERSHOOT(offset
);
207 window_start
= offset
- align_fudge
;
208 window_size
= map
->def_window_size
;
209 if (window_start
+ window_size
> map
->file_size
)
210 window_size
= (int32
)(map
->file_size
- window_start
);
211 if (window_size
< len
+ align_fudge
)
212 window_size
= ALIGNED_LENGTH(len
+ align_fudge
);
214 /* make sure we have allocated enough memory for the window */
215 if (window_size
> map
->p_size
) {
216 map
->p
= realloc_array(map
->p
, char, window_size
);
218 out_of_memory("map_ptr");
219 map
->p_size
= window_size
;
222 /* Now try to avoid re-reading any bytes by reusing any bytes from the previous buffer. */
223 if (window_start
>= map
->p_offset
&& window_start
< map
->p_offset
+ map
->p_len
224 && window_start
+ window_size
>= map
->p_offset
+ map
->p_len
) {
225 read_start
= map
->p_offset
+ map
->p_len
;
226 read_offset
= (int32
)(read_start
- window_start
);
227 read_size
= window_size
- read_offset
;
228 memmove(map
->p
, map
->p
+ (map
->p_len
- read_offset
), read_offset
);
230 read_start
= window_start
;
231 read_size
= window_size
;
235 if (read_size
<= 0) {
236 rprintf(FERROR
, "invalid read_size of %ld in map_ptr\n",
238 exit_cleanup(RERR_FILEIO
);
241 if (map
->p_fd_offset
!= read_start
) {
242 OFF_T ret
= do_lseek(map
->fd
, read_start
, SEEK_SET
);
243 if (ret
!= read_start
) {
244 rsyserr(FERROR
, errno
, "lseek returned %s, not %s",
245 big_num(ret
), big_num(read_start
));
246 exit_cleanup(RERR_FILEIO
);
248 map
->p_fd_offset
= read_start
;
250 map
->p_offset
= window_start
;
251 map
->p_len
= window_size
;
253 while (read_size
> 0) {
254 int32 nread
= read(map
->fd
, map
->p
+ read_offset
, read_size
);
257 map
->status
= nread
? errno
: ENODATA
;
258 /* The best we can do is zero the buffer -- the file
259 * has changed mid transfer! */
260 memset(map
->p
+ read_offset
, 0, read_size
);
263 map
->p_fd_offset
+= nread
;
264 read_offset
+= nread
;
268 return map
->p
+ align_fudge
;
272 int unmap_file(struct map_struct
*map
)
281 memset(map
, 0, sizeof map
[0]);