2 Copyright (C) Andrew Tridgell 1998
3 Copyright (C) 2002 by Martin Pool
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.
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.
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.
21 File IO utilities used in rsync
25 static char last_byte;
26 static int last_sparse;
27 extern int sparse_files;
32 do_lseek(f,-1,SEEK_CUR);
33 return (write(f,&last_byte,1) == 1 ? 0 : -1);
40 static int write_sparse(int f,char *buf,size_t len)
45 for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
46 for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
48 last_byte = buf[len-1];
50 if (l1 == len || l2 > 0)
54 do_lseek(f,l1,SEEK_CUR);
60 ret = write(f, buf + l1, len - (l1+l2));
61 if (ret == -1 || ret == 0)
63 else if (ret != (int) (len - (l1+l2)))
67 do_lseek(f,l2,SEEK_CUR);
73 static char *wf_writeBuf;
74 static size_t wf_writeBufSize;
75 static size_t wf_writeBufCnt;
77 int flush_write_file(int f)
80 char *bp = wf_writeBuf;
82 while (wf_writeBufCnt > 0) {
83 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
88 wf_writeBufCnt -= ret;
95 * write_file does not allow incomplete writes. It loops internally
96 * until len bytes are written or errno is set.
98 int write_file(int f,char *buf,size_t len)
105 int len1 = MIN(len, SPARSE_WRITE_SIZE);
106 r1 = write_sparse(f, buf, len1);
109 wf_writeBufSize = MAX_MAP_SIZE;
111 wf_writeBuf = new_array(char, MAX_MAP_SIZE);
113 out_of_memory("write_file");
115 r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
117 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
118 wf_writeBufCnt += r1;
120 if (wf_writeBufCnt == wf_writeBufSize) {
121 if (flush_write_file(f) < 0)
128 if (ret > 0) return ret;
140 /* this provides functionality somewhat similar to mmap() but using
141 read(). It gives sliding window access to a file. mmap() is not
142 used because of the possibility of another program (such as a
143 mailer) truncating the file thus giving us a SIGBUS */
144 struct map_struct *map_file(int fd,OFF_T len)
146 struct map_struct *map;
147 map = new(struct map_struct);
148 if (!map) out_of_memory("map_file");
151 map->file_size = len;
155 map->p_fd_offset = 0;
162 /* slide the read window in the file */
163 char *map_ptr(struct map_struct *map,OFF_T offset,int len)
166 OFF_T window_start, read_start;
167 int window_size, read_size, read_offset;
173 /* can't go beyond the end of file */
174 if (len > (map->file_size - offset)) {
175 len = map->file_size - offset;
178 /* in most cases the region will already be available */
179 if (offset >= map->p_offset &&
180 offset+len <= map->p_offset+map->p_len) {
181 return (map->p + (offset - map->p_offset));
185 /* nope, we are going to have to do a read. Work out our desired window */
186 if (offset > 2*CHUNK_SIZE) {
187 window_start = offset - 2*CHUNK_SIZE;
188 window_start &= ~((OFF_T)(CHUNK_SIZE-1)); /* assumes power of 2 */
192 window_size = MAX_MAP_SIZE;
193 if (window_start + window_size > map->file_size) {
194 window_size = map->file_size - window_start;
196 if (offset + len > window_start + window_size) {
197 window_size = (offset+len) - window_start;
200 /* make sure we have allocated enough memory for the window */
201 if (window_size > map->p_size) {
202 map->p = realloc_array(map->p, char, window_size);
203 if (!map->p) out_of_memory("map_ptr");
204 map->p_size = window_size;
207 /* now try to avoid re-reading any bytes by reusing any bytes from the previous
209 if (window_start >= map->p_offset &&
210 window_start < map->p_offset + map->p_len &&
211 window_start + window_size >= map->p_offset + map->p_len) {
212 read_start = map->p_offset + map->p_len;
213 read_offset = read_start - window_start;
214 read_size = window_size - read_offset;
215 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
217 read_start = window_start;
218 read_size = window_size;
222 if (read_size <= 0) {
223 rprintf(FINFO,"Warning: unexpected read size of %d in map_ptr\n", read_size);
225 if (map->p_fd_offset != read_start) {
226 if (do_lseek(map->fd,read_start,SEEK_SET) != read_start) {
227 rprintf(FERROR,"lseek failed in map_ptr\n");
228 exit_cleanup(RERR_FILEIO);
230 map->p_fd_offset = read_start;
233 if ((nread=read(map->fd,map->p + read_offset,read_size)) != read_size) {
239 /* the best we can do is zero the buffer - the file
240 has changed mid transfer! */
241 memset(map->p+read_offset+nread, 0, read_size - nread);
243 map->p_fd_offset += nread;
246 map->p_offset = window_start;
247 map->p_len = window_size;
249 return map->p + (offset - map->p_offset);
253 int unmap_file(struct map_struct *map)
262 memset(map, 0, sizeof(*map));