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