Commit | Line | Data |
---|---|---|
13aefa13 | 1 | /* |
0f78b815 WD |
2 | * File IO utilities used in rsync. |
3 | * | |
4 | * Copyright (C) 1998 Andrew Tridgell | |
5 | * Copyright (C) 2002 Martin Pool | |
b3bf9b9d | 6 | * Copyright (C) 2004-2009 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
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. | |
0f78b815 WD |
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 | * | |
e7c67065 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
13aefa13 | 21 | |
4c36ddbe | 22 | #include "rsync.h" |
5dd14f0c | 23 | #include "inums.h" |
4c36ddbe | 24 | |
13482675 WD |
25 | #ifndef ENODATA |
26 | #define ENODATA EAGAIN | |
27 | #endif | |
28 | ||
c7e11bfd WD |
29 | extern int sparse_files; |
30 | ||
4c36ddbe | 31 | static char last_byte; |
faf980ff | 32 | static OFF_T sparse_seek = 0; |
4c36ddbe AT |
33 | |
34 | int sparse_end(int f) | |
35 | { | |
6aa27a7c WD |
36 | int ret; |
37 | ||
38 | if (!sparse_seek) | |
39 | return 0; | |
40 | ||
41 | do_lseek(f, sparse_seek-1, SEEK_CUR); | |
42 | sparse_seek = 0; | |
43 | ||
44 | do { | |
45 | ret = write(f, "", 1); | |
46 | } while (ret < 0 && errno == EINTR); | |
47 | ||
48 | return ret <= 0 ? -1 : 0; | |
4c36ddbe AT |
49 | } |
50 | ||
51 | ||
e4ed195b | 52 | static int write_sparse(int f, char *buf, int len) |
4c36ddbe | 53 | { |
e4ed195b | 54 | int l1 = 0, l2 = 0; |
4c36ddbe AT |
55 | int ret; |
56 | ||
13aefa13 WD |
57 | for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {} |
58 | for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {} | |
4c36ddbe | 59 | |
6aa27a7c WD |
60 | /* XXX Riddle me this: why does this function SLOW DOWN when I |
61 | * remove the following (unneeded) line?? Core Duo weirdness? */ | |
4c36ddbe AT |
62 | last_byte = buf[len-1]; |
63 | ||
6aa27a7c | 64 | sparse_seek += l1; |
4c36ddbe | 65 | |
13aefa13 | 66 | if (l1 == len) |
4c36ddbe AT |
67 | return len; |
68 | ||
6aa27a7c WD |
69 | if (sparse_seek) |
70 | do_lseek(f, sparse_seek, SEEK_CUR); | |
71 | sparse_seek = l2; | |
72 | ||
73 | while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) { | |
74 | if (ret < 0 && errno == EINTR) | |
75 | continue; | |
a261989c | 76 | return ret; |
6aa27a7c | 77 | } |
4c36ddbe | 78 | |
6aa27a7c WD |
79 | if (ret != (int)(len - (l1+l2))) |
80 | return l1+ret; | |
13aefa13 | 81 | |
4c36ddbe AT |
82 | return len; |
83 | } | |
84 | ||
76c21947 WD |
85 | |
86 | static char *wf_writeBuf; | |
87 | static size_t wf_writeBufSize; | |
88 | static size_t wf_writeBufCnt; | |
89 | ||
90 | int flush_write_file(int f) | |
91 | { | |
9b9c8aaf WD |
92 | int ret = 0; |
93 | char *bp = wf_writeBuf; | |
13aefa13 | 94 | |
9b9c8aaf WD |
95 | while (wf_writeBufCnt > 0) { |
96 | if ((ret = write(f, bp, wf_writeBufCnt)) < 0) { | |
97 | if (errno == EINTR) | |
98 | continue; | |
99 | return ret; | |
100 | } | |
101 | wf_writeBufCnt -= ret; | |
102 | bp += ret; | |
103 | } | |
76c21947 WD |
104 | return ret; |
105 | } | |
106 | ||
c7e11bfd | 107 | |
9533e15a S |
108 | /* |
109 | * write_file does not allow incomplete writes. It loops internally | |
110 | * until len bytes are written or errno is set. | |
111 | */ | |
e4ed195b | 112 | int write_file(int f, char *buf, int len) |
4c36ddbe AT |
113 | { |
114 | int ret = 0; | |
115 | ||
9b9c8aaf | 116 | while (len > 0) { |
9533e15a | 117 | int r1; |
18233a17 | 118 | if (sparse_files > 0) { |
9533e15a S |
119 | int len1 = MIN(len, SPARSE_WRITE_SIZE); |
120 | r1 = write_sparse(f, buf, len1); | |
121 | } else { | |
76c21947 | 122 | if (!wf_writeBuf) { |
c7e11bfd | 123 | wf_writeBufSize = WRITE_SIZE * 8; |
76c21947 | 124 | wf_writeBufCnt = 0; |
c7e11bfd | 125 | wf_writeBuf = new_array(char, wf_writeBufSize); |
9b9c8aaf WD |
126 | if (!wf_writeBuf) |
127 | out_of_memory("write_file"); | |
76c21947 | 128 | } |
e4ed195b | 129 | r1 = (int)MIN((size_t)len, wf_writeBufSize - wf_writeBufCnt); |
76c21947 WD |
130 | if (r1) { |
131 | memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1); | |
132 | wf_writeBufCnt += r1; | |
133 | } | |
134 | if (wf_writeBufCnt == wf_writeBufSize) { | |
9b9c8aaf WD |
135 | if (flush_write_file(f) < 0) |
136 | return -1; | |
76c21947 WD |
137 | if (!r1 && len) |
138 | continue; | |
139 | } | |
9533e15a | 140 | } |
4c36ddbe | 141 | if (r1 <= 0) { |
7f290d5c WD |
142 | if (ret > 0) |
143 | return ret; | |
4c36ddbe AT |
144 | return r1; |
145 | } | |
146 | len -= r1; | |
147 | buf += r1; | |
148 | ret += r1; | |
149 | } | |
150 | return ret; | |
151 | } | |
152 | ||
153 | ||
6e8a1782 WD |
154 | /* This provides functionality somewhat similar to mmap() but using read(). |
155 | * It gives sliding window access to a file. mmap() is not used because of | |
156 | * the possibility of another program (such as a mailer) truncating the | |
157 | * file thus giving us a SIGBUS. */ | |
54281fe7 | 158 | struct map_struct *map_file(int fd, OFF_T len, int32 read_size, |
7aac6604 | 159 | int32 blk_size) |
4c36ddbe | 160 | { |
4440b8aa | 161 | struct map_struct *map; |
4440b8aa | 162 | |
7e4b6b7b | 163 | if (!(map = new0(struct map_struct))) |
bf2b7ddf WD |
164 | out_of_memory("map_file"); |
165 | ||
54281fe7 WD |
166 | if (blk_size && (read_size % blk_size)) |
167 | read_size += blk_size - (read_size % blk_size); | |
6e8a1782 | 168 | |
4440b8aa AT |
169 | map->fd = fd; |
170 | map->file_size = len; | |
54281fe7 | 171 | map->def_window_size = read_size; |
4440b8aa AT |
172 | |
173 | return map; | |
4c36ddbe AT |
174 | } |
175 | ||
c7e11bfd | 176 | |
4440b8aa | 177 | /* slide the read window in the file */ |
7aac6604 | 178 | char *map_ptr(struct map_struct *map, OFF_T offset, int32 len) |
4c36ddbe | 179 | { |
54281fe7 | 180 | int32 nread; |
4440b8aa | 181 | OFF_T window_start, read_start; |
54281fe7 | 182 | int32 window_size, read_size, read_offset; |
4c36ddbe | 183 | |
7f290d5c | 184 | if (len == 0) |
4c36ddbe | 185 | return NULL; |
54281fe7 WD |
186 | if (len < 0) { |
187 | rprintf(FERROR, "invalid len passed to map_ptr: %ld\n", | |
188 | (long)len); | |
189 | exit_cleanup(RERR_FILEIO); | |
190 | } | |
4c36ddbe | 191 | |
4440b8aa | 192 | /* in most cases the region will already be available */ |
7aac6604 WD |
193 | if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len) |
194 | return map->p + (offset - map->p_offset); | |
4c36ddbe | 195 | |
4440b8aa | 196 | /* nope, we are going to have to do a read. Work out our desired window */ |
6e8a1782 | 197 | window_start = offset; |
7f290d5c | 198 | window_size = map->def_window_size; |
54281fe7 | 199 | if (window_start + window_size > map->file_size) |
26404276 | 200 | window_size = (int32)(map->file_size - window_start); |
54281fe7 WD |
201 | if (len > window_size) |
202 | window_size = len; | |
4c36ddbe | 203 | |
4440b8aa AT |
204 | /* make sure we have allocated enough memory for the window */ |
205 | if (window_size > map->p_size) { | |
58cadc86 | 206 | map->p = realloc_array(map->p, char, window_size); |
7f290d5c WD |
207 | if (!map->p) |
208 | out_of_memory("map_ptr"); | |
4440b8aa AT |
209 | map->p_size = window_size; |
210 | } | |
4c36ddbe | 211 | |
54281fe7 WD |
212 | /* Now try to avoid re-reading any bytes by reusing any bytes |
213 | * from the previous buffer. */ | |
4440b8aa AT |
214 | if (window_start >= map->p_offset && |
215 | window_start < map->p_offset + map->p_len && | |
216 | window_start + window_size >= map->p_offset + map->p_len) { | |
217 | read_start = map->p_offset + map->p_len; | |
26404276 | 218 | read_offset = (int32)(read_start - window_start); |
4440b8aa AT |
219 | read_size = window_size - read_offset; |
220 | memmove(map->p, map->p + (map->p_len - read_offset), read_offset); | |
221 | } else { | |
222 | read_start = window_start; | |
223 | read_size = window_size; | |
224 | read_offset = 0; | |
4c36ddbe AT |
225 | } |
226 | ||
4440b8aa | 227 | if (read_size <= 0) { |
54281fe7 WD |
228 | rprintf(FERROR, "invalid read_size of %ld in map_ptr\n", |
229 | (long)read_size); | |
230 | exit_cleanup(RERR_FILEIO); | |
13482675 | 231 | } |
4440b8aa | 232 | |
13482675 WD |
233 | if (map->p_fd_offset != read_start) { |
234 | OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET); | |
235 | if (ret != read_start) { | |
6d56efa6 | 236 | rsyserr(FERROR, errno, "lseek returned %s, not %s", |
adc2476f | 237 | big_num(ret), big_num(read_start)); |
13482675 | 238 | exit_cleanup(RERR_FILEIO); |
4440b8aa | 239 | } |
13482675 | 240 | map->p_fd_offset = read_start; |
4c36ddbe | 241 | } |
4440b8aa AT |
242 | map->p_offset = window_start; |
243 | map->p_len = window_size; | |
13aefa13 | 244 | |
13482675 WD |
245 | while (read_size > 0) { |
246 | nread = read(map->fd, map->p + read_offset, read_size); | |
247 | if (nread <= 0) { | |
248 | if (!map->status) | |
249 | map->status = nread ? errno : ENODATA; | |
250 | /* The best we can do is zero the buffer -- the file | |
251 | * has changed mid transfer! */ | |
252 | memset(map->p + read_offset, 0, read_size); | |
253 | break; | |
254 | } | |
e3db43ff | 255 | map->p_fd_offset += nread; |
13482675 WD |
256 | read_offset += nread; |
257 | read_size -= nread; | |
258 | } | |
259 | ||
54281fe7 | 260 | return map->p; |
4c36ddbe AT |
261 | } |
262 | ||
263 | ||
6a7cc46c | 264 | int unmap_file(struct map_struct *map) |
4c36ddbe | 265 | { |
6a7cc46c S |
266 | int ret; |
267 | ||
4c36ddbe AT |
268 | if (map->p) { |
269 | free(map->p); | |
270 | map->p = NULL; | |
271 | } | |
6a7cc46c | 272 | ret = map->status; |
bf2b7ddf | 273 | memset(map, 0, sizeof map[0]); |
4c36ddbe | 274 | free(map); |
6a7cc46c S |
275 | |
276 | return ret; | |
4c36ddbe | 277 | } |