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