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