]> git.ipfire.org Git - thirdparty/rsync.git/blob - fileio.c
Reformat a few things for wider lines.
[thirdparty/rsync.git] / fileio.c
1 /*
2 * File IO utilities used in rsync.
3 *
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2004-2009 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22 #include "rsync.h"
23 #include "inums.h"
24
25 #ifndef ENODATA
26 #define ENODATA EAGAIN
27 #endif
28
29 extern int sparse_files;
30
31 static OFF_T sparse_seek = 0;
32
33 int sparse_end(int f, OFF_T size)
34 {
35 int ret;
36
37 if (!sparse_seek)
38 return 0;
39
40 #ifdef HAVE_FTRUNCATE
41 ret = do_ftruncate(f, size);
42 #else
43 if (do_lseek(f, sparse_seek-1, SEEK_CUR) != size-1)
44 ret = -1;
45 else {
46 do {
47 ret = write(f, "", 1);
48 } while (ret < 0 && errno == EINTR);
49
50 ret = ret <= 0 ? -1 : 0;
51 }
52 #endif
53
54 sparse_seek = 0;
55
56 return ret;
57 }
58
59
60 static int write_sparse(int f, char *buf, int len)
61 {
62 int l1 = 0, l2 = 0;
63 int ret;
64
65 for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
66 for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
67
68 sparse_seek += l1;
69
70 if (l1 == len)
71 return len;
72
73 if (sparse_seek)
74 do_lseek(f, sparse_seek, SEEK_CUR);
75 sparse_seek = l2;
76
77 while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) {
78 if (ret < 0 && errno == EINTR)
79 continue;
80 return ret;
81 }
82
83 if (ret != (int)(len - (l1+l2)))
84 return l1+ret;
85
86 return len;
87 }
88
89
90 static char *wf_writeBuf;
91 static size_t wf_writeBufSize;
92 static size_t wf_writeBufCnt;
93
94 int flush_write_file(int f)
95 {
96 int ret = 0;
97 char *bp = wf_writeBuf;
98
99 while (wf_writeBufCnt > 0) {
100 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
101 if (errno == EINTR)
102 continue;
103 return ret;
104 }
105 wf_writeBufCnt -= ret;
106 bp += ret;
107 }
108 return ret;
109 }
110
111
112 /*
113 * write_file does not allow incomplete writes. It loops internally
114 * until len bytes are written or errno is set.
115 */
116 int write_file(int f, char *buf, int len)
117 {
118 int ret = 0;
119
120 while (len > 0) {
121 int r1;
122 if (sparse_files > 0) {
123 int len1 = MIN(len, SPARSE_WRITE_SIZE);
124 r1 = write_sparse(f, buf, len1);
125 } else {
126 if (!wf_writeBuf) {
127 wf_writeBufSize = WRITE_SIZE * 8;
128 wf_writeBufCnt = 0;
129 wf_writeBuf = new_array(char, wf_writeBufSize);
130 if (!wf_writeBuf)
131 out_of_memory("write_file");
132 }
133 r1 = (int)MIN((size_t)len, wf_writeBufSize - wf_writeBufCnt);
134 if (r1) {
135 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
136 wf_writeBufCnt += r1;
137 }
138 if (wf_writeBufCnt == wf_writeBufSize) {
139 if (flush_write_file(f) < 0)
140 return -1;
141 if (!r1 && len)
142 continue;
143 }
144 }
145 if (r1 <= 0) {
146 if (ret > 0)
147 return ret;
148 return r1;
149 }
150 len -= r1;
151 buf += r1;
152 ret += r1;
153 }
154 return ret;
155 }
156
157
158 /* This provides functionality somewhat similar to mmap() but using read().
159 * It gives sliding window access to a file. mmap() is not used because of
160 * the possibility of another program (such as a mailer) truncating the
161 * file thus giving us a SIGBUS. */
162 struct map_struct *map_file(int fd, OFF_T len, int32 read_size, int32 blk_size)
163 {
164 struct map_struct *map;
165
166 if (!(map = new0(struct map_struct)))
167 out_of_memory("map_file");
168
169 if (blk_size && (read_size % blk_size))
170 read_size += blk_size - (read_size % blk_size);
171
172 map->fd = fd;
173 map->file_size = len;
174 map->def_window_size = read_size;
175
176 return map;
177 }
178
179
180 /* slide the read window in the file */
181 char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
182 {
183 OFF_T window_start, read_start;
184 int32 window_size, read_size, read_offset;
185
186 if (len == 0)
187 return NULL;
188 if (len < 0) {
189 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
190 (long)len);
191 exit_cleanup(RERR_FILEIO);
192 }
193
194 /* in most cases the region will already be available */
195 if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
196 return map->p + (offset - map->p_offset);
197
198 /* nope, we are going to have to do a read. Work out our desired window */
199 window_start = offset;
200 window_size = map->def_window_size;
201 if (window_start + window_size > map->file_size)
202 window_size = (int32)(map->file_size - window_start);
203 if (len > window_size)
204 window_size = len;
205
206 /* make sure we have allocated enough memory for the window */
207 if (window_size > map->p_size) {
208 map->p = realloc_array(map->p, char, window_size);
209 if (!map->p)
210 out_of_memory("map_ptr");
211 map->p_size = window_size;
212 }
213
214 /* Now try to avoid re-reading any bytes by reusing any bytes from the previous buffer. */
215 if (window_start >= map->p_offset && window_start < map->p_offset + map->p_len
216 && window_start + window_size >= map->p_offset + map->p_len) {
217 read_start = map->p_offset + map->p_len;
218 read_offset = (int32)(read_start - window_start);
219 read_size = window_size - read_offset;
220 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
221 } else {
222 read_start = window_start;
223 read_size = window_size;
224 read_offset = 0;
225 }
226
227 if (read_size <= 0) {
228 rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
229 (long)read_size);
230 exit_cleanup(RERR_FILEIO);
231 }
232
233 if (map->p_fd_offset != read_start) {
234 OFF_T ret = do_lseek(map->fd, read_start, SEEK_SET);
235 if (ret != read_start) {
236 rsyserr(FERROR, errno, "lseek returned %s, not %s",
237 big_num(ret), big_num(read_start));
238 exit_cleanup(RERR_FILEIO);
239 }
240 map->p_fd_offset = read_start;
241 }
242 map->p_offset = window_start;
243 map->p_len = window_size;
244
245 while (read_size > 0) {
246 int32 nread = read(map->fd, map->p + read_offset, read_size);
247 if (nread <= 0) {
248 if (!map->status)
249 map->status = nread ? errno : ENODATA;
250 /* The best we can do is zero the buffer -- the file
251 * has changed mid transfer! */
252 memset(map->p + read_offset, 0, read_size);
253 break;
254 }
255 map->p_fd_offset += nread;
256 read_offset += nread;
257 read_size -= nread;
258 }
259
260 return map->p;
261 }
262
263
264 int unmap_file(struct map_struct *map)
265 {
266 int ret;
267
268 if (map->p) {
269 free(map->p);
270 map->p = NULL;
271 }
272 ret = map->status;
273 memset(map, 0, sizeof map[0]);
274 free(map);
275
276 return ret;
277 }