2 * File IO utilities used in rsync.
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2004-2007 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
25 #define ENODATA EAGAIN
28 extern int sparse_files;
30 static char last_byte;
31 static int last_sparse;
36 do_lseek(f,-1,SEEK_CUR);
37 return (write(f,&last_byte,1) == 1 ? 0 : -1);
44 static int write_sparse(int f,char *buf,size_t len)
49 for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
50 for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
52 last_byte = buf[len-1];
54 if (l1 == len || l2 > 0)
58 do_lseek(f,l1,SEEK_CUR);
64 ret = write(f, buf + l1, len - (l1+l2));
65 if (ret == -1 || ret == 0)
67 else if (ret != (int) (len - (l1+l2)))
71 do_lseek(f,l2,SEEK_CUR);
77 static char *wf_writeBuf;
78 static size_t wf_writeBufSize;
79 static size_t wf_writeBufCnt;
81 int flush_write_file(int f)
84 char *bp = wf_writeBuf;
86 while (wf_writeBufCnt > 0) {
87 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
92 wf_writeBufCnt -= ret;
100 * write_file does not allow incomplete writes. It loops internally
101 * until len bytes are written or errno is set.
103 int write_file(int f,char *buf,size_t len)
109 if (sparse_files > 0) {
110 int len1 = MIN(len, SPARSE_WRITE_SIZE);
111 r1 = write_sparse(f, buf, len1);
114 wf_writeBufSize = WRITE_SIZE * 8;
116 wf_writeBuf = new_array(char, wf_writeBufSize);
118 out_of_memory("write_file");
120 r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
122 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
123 wf_writeBufCnt += r1;
125 if (wf_writeBufCnt == wf_writeBufSize) {
126 if (flush_write_file(f) < 0)
145 /* This provides functionality somewhat similar to mmap() but using read().
146 * It gives sliding window access to a file. mmap() is not used because of
147 * the possibility of another program (such as a mailer) truncating the
148 * file thus giving us a SIGBUS. */
149 struct map_struct *map_file(int fd, OFF_T len, int32 read_size,
152 struct map_struct *map;
154 if (!(map = new(struct map_struct)))
155 out_of_memory("map_file");
157 if (blk_size && (read_size % blk_size))
158 read_size += blk_size - (read_size % blk_size);
160 memset(map, 0, sizeof map[0]);
162 map->file_size = len;
163 map->def_window_size = read_size;
169 /* slide the read window in the file */
170 char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
173 OFF_T window_start, read_start;
174 int32 window_size, read_size, read_offset;
179 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
181 exit_cleanup(RERR_FILEIO);
184 /* in most cases the region will already be available */
185 if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
186 return map->p + (offset - map->p_offset);
188 /* nope, we are going to have to do a read. Work out our desired window */
189 window_start = offset;
190 window_size = map->def_window_size;
191 if (window_start + window_size > map->file_size)
192 window_size = (int32)(map->file_size - window_start);
193 if (len > window_size)
196 /* make sure we have allocated enough memory for the window */
197 if (window_size > map->p_size) {
198 map->p = realloc_array(map->p, char, window_size);
200 out_of_memory("map_ptr");
201 map->p_size = window_size;
204 /* Now try to avoid re-reading any bytes by reusing any bytes
205 * from the previous buffer. */
206 if (window_start >= map->p_offset &&
207 window_start < map->p_offset + map->p_len &&
208 window_start + window_size >= map->p_offset + map->p_len) {
209 read_start = map->p_offset + map->p_len;
210 read_offset = (int32)(read_start - window_start);
211 read_size = window_size - read_offset;
212 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
214 read_start = window_start;
215 read_size = window_size;
219 if (read_size <= 0) {
220 rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
222 exit_cleanup(RERR_FILEIO);
225 if (map->p_fd_offset != read_start) {
226 OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
227 if (ret != read_start) {
228 rsyserr(FERROR, errno, "lseek returned %.0f, not %.0f",
229 (double)ret, (double)read_start);
230 exit_cleanup(RERR_FILEIO);
232 map->p_fd_offset = read_start;
234 map->p_offset = window_start;
235 map->p_len = window_size;
237 while (read_size > 0) {
238 nread = read(map->fd, map->p + read_offset, read_size);
241 map->status = nread ? errno : ENODATA;
242 /* The best we can do is zero the buffer -- the file
243 * has changed mid transfer! */
244 memset(map->p + read_offset, 0, read_size);
247 map->p_fd_offset += nread;
248 read_offset += nread;
256 int unmap_file(struct map_struct *map)
265 memset(map, 0, sizeof map[0]);