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