Martin gave his approval to use GPLv3 with this code.
[rsync/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-2007 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 version 3 as
10  * published by the Free Software Foundation.
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  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, visit the http://fsf.org website.
19  */
20
21 #include "rsync.h"
22
23 #ifndef ENODATA
24 #define ENODATA EAGAIN
25 #endif
26
27 extern int sparse_files;
28
29 static char last_byte;
30 static size_t sparse_seek = 0;
31
32 int sparse_end(int f)
33 {
34         int ret;
35
36         if (!sparse_seek)
37                 return 0;
38
39         do_lseek(f, sparse_seek-1, SEEK_CUR);
40         sparse_seek = 0;
41
42         do {
43                 ret = write(f, "", 1);
44         } while (ret < 0 && errno == EINTR);
45
46         return ret <= 0 ? -1 : 0;
47 }
48
49
50 static int write_sparse(int f, char *buf, size_t len)
51 {
52         size_t l1 = 0, l2 = 0;
53         int ret;
54
55         for (l1 = 0; l1 < len && buf[l1] == 0; l1++) {}
56         for (l2 = 0; l2 < len-l1 && buf[len-(l2+1)] == 0; l2++) {}
57
58         /* XXX Riddle me this: why does this function SLOW DOWN when I
59          * remove the following (unneeded) line?? Core Duo weirdness? */
60         last_byte = buf[len-1];
61
62         sparse_seek += l1;
63
64         if (l1 == len)
65                 return len;
66
67         if (sparse_seek)
68                 do_lseek(f, sparse_seek, SEEK_CUR);
69         sparse_seek = l2;
70
71         while ((ret = write(f, buf + l1, len - (l1+l2))) <= 0) {
72                 if (ret < 0 && errno == EINTR)
73                         continue;
74                 return ret;
75         }
76
77         if (ret != (int)(len - (l1+l2)))
78                 return l1+ret;
79
80         return len;
81 }
82
83
84 static char *wf_writeBuf;
85 static size_t wf_writeBufSize;
86 static size_t wf_writeBufCnt;
87
88 int flush_write_file(int f)
89 {
90         int ret = 0;
91         char *bp = wf_writeBuf;
92
93         while (wf_writeBufCnt > 0) {
94                 if ((ret = write(f, bp, wf_writeBufCnt)) < 0) {
95                         if (errno == EINTR)
96                                 continue;
97                         return ret;
98                 }
99                 wf_writeBufCnt -= ret;
100                 bp += ret;
101         }
102         return ret;
103 }
104
105
106 /*
107  * write_file does not allow incomplete writes.  It loops internally
108  * until len bytes are written or errno is set.
109  */
110 int write_file(int f,char *buf,size_t len)
111 {
112         int ret = 0;
113
114         while (len > 0) {
115                 int r1;
116                 if (sparse_files > 0) {
117                         int len1 = MIN(len, SPARSE_WRITE_SIZE);
118                         r1 = write_sparse(f, buf, len1);
119                 } else {
120                         if (!wf_writeBuf) {
121                                 wf_writeBufSize = WRITE_SIZE * 8;
122                                 wf_writeBufCnt  = 0;
123                                 wf_writeBuf = new_array(char, wf_writeBufSize);
124                                 if (!wf_writeBuf)
125                                         out_of_memory("write_file");
126                         }
127                         r1 = MIN(len, wf_writeBufSize - wf_writeBufCnt);
128                         if (r1) {
129                                 memcpy(wf_writeBuf + wf_writeBufCnt, buf, r1);
130                                 wf_writeBufCnt += r1;
131                         }
132                         if (wf_writeBufCnt == wf_writeBufSize) {
133                                 if (flush_write_file(f) < 0)
134                                         return -1;
135                                 if (!r1 && len)
136                                         continue;
137                         }
138                 }
139                 if (r1 <= 0) {
140                         if (ret > 0)
141                                 return ret;
142                         return r1;
143                 }
144                 len -= r1;
145                 buf += r1;
146                 ret += r1;
147         }
148         return ret;
149 }
150
151
152 /* This provides functionality somewhat similar to mmap() but using read().
153  * It gives sliding window access to a file.  mmap() is not used because of
154  * the possibility of another program (such as a mailer) truncating the
155  * file thus giving us a SIGBUS. */
156 struct map_struct *map_file(int fd, OFF_T len, int32 read_size,
157                             int32 blk_size)
158 {
159         struct map_struct *map;
160
161         if (!(map = new(struct map_struct)))
162                 out_of_memory("map_file");
163
164         if (blk_size && (read_size % blk_size))
165                 read_size += blk_size - (read_size % blk_size);
166
167         memset(map, 0, sizeof map[0]);
168         map->fd = fd;
169         map->file_size = len;
170         map->def_window_size = read_size;
171
172         return map;
173 }
174
175
176 /* slide the read window in the file */
177 char *map_ptr(struct map_struct *map, OFF_T offset, int32 len)
178 {
179         int32 nread;
180         OFF_T window_start, read_start;
181         int32 window_size, read_size, read_offset;
182
183         if (len == 0)
184                 return NULL;
185         if (len < 0) {
186                 rprintf(FERROR, "invalid len passed to map_ptr: %ld\n",
187                         (long)len);
188                 exit_cleanup(RERR_FILEIO);
189         }
190
191         /* in most cases the region will already be available */
192         if (offset >= map->p_offset && offset+len <= map->p_offset+map->p_len)
193                 return map->p + (offset - map->p_offset);
194
195         /* nope, we are going to have to do a read. Work out our desired window */
196         window_start = offset;
197         window_size = map->def_window_size;
198         if (window_start + window_size > map->file_size)
199                 window_size = (int32)(map->file_size - window_start);
200         if (len > window_size)
201                 window_size = len;
202
203         /* make sure we have allocated enough memory for the window */
204         if (window_size > map->p_size) {
205                 map->p = realloc_array(map->p, char, window_size);
206                 if (!map->p)
207                         out_of_memory("map_ptr");
208                 map->p_size = window_size;
209         }
210
211         /* Now try to avoid re-reading any bytes by reusing any bytes
212          * from the previous buffer. */
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;
217                 read_offset = (int32)(read_start - window_start);
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;
224         }
225
226         if (read_size <= 0) {
227                 rprintf(FERROR, "invalid read_size of %ld in map_ptr\n",
228                         (long)read_size);
229                 exit_cleanup(RERR_FILEIO);
230         }
231
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) {
235                         rsyserr(FERROR, errno, "lseek returned %.0f, not %.0f",
236                                 (double)ret, (double)read_start);
237                         exit_cleanup(RERR_FILEIO);
238                 }
239                 map->p_fd_offset = read_start;
240         }
241         map->p_offset = window_start;
242         map->p_len = window_size;
243
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                 }
254                 map->p_fd_offset += nread;
255                 read_offset += nread;
256                 read_size -= nread;
257         }
258
259         return map->p;
260 }
261
262
263 int unmap_file(struct map_struct *map)
264 {
265         int     ret;
266
267         if (map->p) {
268                 free(map->p);
269                 map->p = NULL;
270         }
271         ret = map->status;
272         memset(map, 0, sizeof map[0]);
273         free(map);
274
275         return ret;
276 }