The match_gid() function needed to affect the *flags_ptr value
[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
ba2133d6 6 * Copyright (C) 2004-2007 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
0f78b815
WD
11 *
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.
16 *
e7c67065
WD
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.
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
AT
30static char last_byte;
31static int last_sparse;
4c36ddbe
AT
32
33int sparse_end(int f)
34{
35 if (last_sparse) {
36 do_lseek(f,-1,SEEK_CUR);
37 return (write(f,&last_byte,1) == 1 ? 0 : -1);
38 }
39 last_sparse = 0;
40 return 0;
41}
42
43
9dd891bb 44static int write_sparse(int f,char *buf,size_t len)
4c36ddbe 45{
a261989c 46 size_t l1=0, l2=0;
4c36ddbe
AT
47 int ret;
48
13aefa13
WD
49 for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
50 for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
4c36ddbe
AT
51
52 last_byte = buf[len-1];
53
54 if (l1 == len || l2 > 0)
55 last_sparse=1;
56
4440b8aa 57 if (l1 > 0) {
13aefa13 58 do_lseek(f,l1,SEEK_CUR);
4440b8aa 59 }
4c36ddbe 60
13aefa13 61 if (l1 == len)
4c36ddbe
AT
62 return len;
63
a261989c
MP
64 ret = write(f, buf + l1, len - (l1+l2));
65 if (ret == -1 || ret == 0)
66 return ret;
13aefa13 67 else if (ret != (int) (len - (l1+l2)))
4c36ddbe 68 return (l1+ret);
4c36ddbe
AT
69
70 if (l2 > 0)
71 do_lseek(f,l2,SEEK_CUR);
13aefa13 72
4c36ddbe
AT
73 return len;
74}
75
76c21947
WD
76
77static char *wf_writeBuf;
78static size_t wf_writeBufSize;
79static size_t wf_writeBufCnt;
80
81int flush_write_file(int f)
82{
9b9c8aaf
WD
83 int ret = 0;
84 char *bp = wf_writeBuf;
13aefa13 85
9b9c8aaf
WD
86 while (wf_writeBufCnt > 0) {
87 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
88 if (errno == EINTR)
89 continue;
90 return ret;
91 }
92 wf_writeBufCnt -= ret;
93 bp += ret;
94 }
76c21947
WD
95 return ret;
96}
97
c7e11bfd 98
9533e15a
S
99/*
100 * write_file does not allow incomplete writes. It loops internally
101 * until len bytes are written or errno is set.
102 */
9dd891bb 103int write_file(int f,char *buf,size_t len)
4c36ddbe
AT
104{
105 int ret = 0;
106
9b9c8aaf 107 while (len > 0) {
9533e15a 108 int r1;
18233a17 109 if (sparse_files > 0) {
9533e15a
S
110 int len1 = MIN(len, SPARSE_WRITE_SIZE);
111 r1 = write_sparse(f, buf, len1);
112 } else {
76c21947 113 if (!wf_writeBuf) {
c7e11bfd 114 wf_writeBufSize = WRITE_SIZE * 8;
76c21947 115 wf_writeBufCnt = 0;
c7e11bfd 116 wf_writeBuf = new_array(char, wf_writeBufSize);
9b9c8aaf
WD
117 if (!wf_writeBuf)
118 out_of_memory("write_file");
76c21947
WD
119 }
120 r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
121 if (r1) {
122 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
123 wf_writeBufCnt += r1;
124 }
125 if (wf_writeBufCnt == wf_writeBufSize) {
9b9c8aaf
WD
126 if (flush_write_file(f) < 0)
127 return -1;
76c21947
WD
128 if (!r1 && len)
129 continue;
130 }
9533e15a 131 }
4c36ddbe 132 if (r1 <= 0) {
7f290d5c
WD
133 if (ret > 0)
134 return ret;
4c36ddbe
AT
135 return r1;
136 }
137 len -= r1;
138 buf += r1;
139 ret += r1;
140 }
141 return ret;
142}
143
144
6e8a1782
WD
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. */
54281fe7 149struct map_struct *map_file(int fd, OFF_T len, int32 read_size,
7aac6604 150 int32 blk_size)
4c36ddbe 151{
4440b8aa 152 struct map_struct *map;
4440b8aa 153
bf2b7ddf
WD
154 if (!(map = new(struct map_struct)))
155 out_of_memory("map_file");
156
54281fe7
WD
157 if (blk_size && (read_size % blk_size))
158 read_size += blk_size - (read_size % blk_size);
6e8a1782 159
bf2b7ddf 160 memset(map, 0, sizeof map[0]);
4440b8aa
AT
161 map->fd = fd;
162 map->file_size = len;
54281fe7 163 map->def_window_size = read_size;
4440b8aa
AT
164
165 return map;
4c36ddbe
AT
166}
167
c7e11bfd 168
4440b8aa 169/* slide the read window in the file */
7aac6604 170char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
4c36ddbe 171{
54281fe7 172 int32 nread;
4440b8aa 173 OFF_T window_start, read_start;
54281fe7 174 int32 window_size, read_size, read_offset;
4c36ddbe 175
7f290d5c 176 if (len == 0)
4c36ddbe 177 return NULL;
54281fe7
WD
178 if (len < 0) {
179 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
180 (long)len);
181 exit_cleanup(RERR_FILEIO);
182 }
4c36ddbe 183
4440b8aa 184 /* in most cases the region will already be available */
7aac6604
WD
185 if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
186 return map->p + (offset - map->p_offset);
4c36ddbe 187
4440b8aa 188 /* nope, we are going to have to do a read. Work out our desired window */
6e8a1782 189 window_start = offset;
7f290d5c 190 window_size = map->def_window_size;
54281fe7 191 if (window_start + window_size > map->file_size)
26404276 192 window_size = (int32)(map->file_size - window_start);
54281fe7
WD
193 if (len > window_size)
194 window_size = len;
4c36ddbe 195
4440b8aa
AT
196 /* make sure we have allocated enough memory for the window */
197 if (window_size > map->p_size) {
58cadc86 198 map->p = realloc_array(map->p, char, window_size);
7f290d5c
WD
199 if (!map->p)
200 out_of_memory("map_ptr");
4440b8aa
AT
201 map->p_size = window_size;
202 }
4c36ddbe 203
54281fe7
WD
204 /* Now try to avoid re-reading any bytes by reusing any bytes
205 * from the previous buffer. */
4440b8aa
AT
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;
26404276 210 read_offset = (int32)(read_start - window_start);
4440b8aa
AT
211 read_size = window_size - read_offset;
212 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
213 } else {
214 read_start = window_start;
215 read_size = window_size;
216 read_offset = 0;
4c36ddbe
AT
217 }
218
4440b8aa 219 if (read_size <= 0) {
54281fe7
WD
220 rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
221 (long)read_size);
222 exit_cleanup(RERR_FILEIO);
13482675 223 }
4440b8aa 224
13482675
WD
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);
4440b8aa 231 }
13482675 232 map->p_fd_offset = read_start;
4c36ddbe 233 }
4440b8aa
AT
234 map->p_offset = window_start;
235 map->p_len = window_size;
13aefa13 236
13482675
WD
237 while (read_size > 0) {
238 nread = read(map->fd, map->p + read_offset, read_size);
239 if (nread <= 0) {
240 if (!map->status)
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);
245 break;
246 }
e3db43ff 247 map->p_fd_offset += nread;
13482675
WD
248 read_offset += nread;
249 read_size -= nread;
250 }
251
54281fe7 252 return map->p;
4c36ddbe
AT
253}
254
255
6a7cc46c 256int unmap_file(struct map_struct *map)
4c36ddbe 257{
6a7cc46c
S
258 int ret;
259
4c36ddbe
AT
260 if (map->p) {
261 free(map->p);
262 map->p = NULL;
263 }
6a7cc46c 264 ret = map->status;
bf2b7ddf 265 memset(map, 0, sizeof map[0]);
4c36ddbe 266 free(map);
6a7cc46c
S
267
268 return ret;
4c36ddbe 269}