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