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