If using gcc, then also turn on -W to get even more warnings.
[rsync/rsync.git] / fileio.c
1 /* 
2    Copyright (C) Andrew Tridgell 1998
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8    
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13    
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 /*
20   File IO utilities used in rsync 
21   */
22 #include "rsync.h"
23
24 static char last_byte;
25 static int last_sparse;
26 extern int sparse_files;
27
28 int sparse_end(int f)
29 {
30         if (last_sparse) {
31                 do_lseek(f,-1,SEEK_CUR);
32                 return (write(f,&last_byte,1) == 1 ? 0 : -1);
33         }
34         last_sparse = 0;
35         return 0;
36 }
37
38
39 static int write_sparse(int f,char *buf,int len)
40 {
41         int l1=0,l2=0;
42         int ret;
43
44         for (l1=0;l1<len && buf[l1]==0;l1++) ;
45         for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
46
47         last_byte = buf[len-1];
48
49         if (l1 == len || l2 > 0)
50                 last_sparse=1;
51
52         if (l1 > 0) {
53                 do_lseek(f,l1,SEEK_CUR);  
54         }
55
56         if (l1 == len) 
57                 return len;
58
59         if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
60                 if (ret == -1 || ret == 0) return ret;
61                 return (l1+ret);
62         }
63
64         if (l2 > 0)
65                 do_lseek(f,l2,SEEK_CUR);
66         
67         return len;
68 }
69
70
71
72 int write_file(int f,char *buf,int len)
73 {
74         int ret = 0;
75
76         if (!sparse_files) {
77                 return write(f,buf,len);
78         }
79
80         while (len>0) {
81                 int len1 = MIN(len, SPARSE_WRITE_SIZE);
82                 int r1 = write_sparse(f, buf, len1);
83                 if (r1 <= 0) {
84                         if (ret > 0) return ret;
85                         return r1;
86                 }
87                 len -= r1;
88                 buf += r1;
89                 ret += r1;
90         }
91         return ret;
92 }
93
94
95
96 /* this provides functionality somewhat similar to mmap() but using
97    read(). It gives sliding window access to a file. mmap() is not
98    used because of the possibility of another program (such as a
99    mailer) truncating the file thus giving us a SIGBUS */
100 struct map_struct *map_file(int fd,OFF_T len)
101 {
102         struct map_struct *map;
103         map = (struct map_struct *)malloc(sizeof(*map));
104         if (!map) out_of_memory("map_file");
105
106         map->fd = fd;
107         map->file_size = len;
108         map->p = NULL;
109         map->p_size = 0;
110         map->p_offset = 0;
111         map->p_fd_offset = 0;
112         map->p_len = 0;
113
114         return map;
115 }
116
117 /* slide the read window in the file */
118 char *map_ptr(struct map_struct *map,OFF_T offset,int len)
119 {
120         int nread;
121         OFF_T window_start, read_start;
122         int window_size, read_size, read_offset;
123
124         if (len == 0) {
125                 return NULL;
126         }
127
128         /* can't go beyond the end of file */
129         if (len > (map->file_size - offset)) {
130                 len = map->file_size - offset;
131         }
132
133         /* in most cases the region will already be available */
134         if (offset >= map->p_offset && 
135             offset+len <= map->p_offset+map->p_len) {
136                 return (map->p + (offset - map->p_offset));
137         }
138
139
140         /* nope, we are going to have to do a read. Work out our desired window */
141         if (offset > 2*CHUNK_SIZE) {
142                 window_start = offset - 2*CHUNK_SIZE;
143                 window_start &= ~((OFF_T)(CHUNK_SIZE-1)); /* assumes power of 2 */
144         } else {
145                 window_start = 0;
146         }
147         window_size = MAX_MAP_SIZE;
148         if (window_start + window_size > map->file_size) {
149                 window_size = map->file_size - window_start;
150         }
151         if (offset + len > window_start + window_size) {
152                 window_size = (offset+len) - window_start;
153         }
154
155         /* make sure we have allocated enough memory for the window */
156         if (window_size > map->p_size) {
157                 map->p = (char *)Realloc(map->p, window_size);
158                 if (!map->p) out_of_memory("map_ptr");
159                 map->p_size = window_size;
160         }
161
162         /* now try to avoid re-reading any bytes by reusing any bytes from the previous
163            buffer. */
164         if (window_start >= map->p_offset &&
165             window_start < map->p_offset + map->p_len &&
166             window_start + window_size >= map->p_offset + map->p_len) {
167                 read_start = map->p_offset + map->p_len;
168                 read_offset = read_start - window_start;
169                 read_size = window_size - read_offset;
170                 memmove(map->p, map->p + (map->p_len - read_offset), read_offset);
171         } else {
172                 read_start = window_start;
173                 read_size = window_size;
174                 read_offset = 0;
175         }
176
177         if (read_size <= 0) {
178                 rprintf(FINFO,"Warning: unexpected read size of %d in map_ptr\n", read_size);
179         } else {
180                 if (map->p_fd_offset != read_start) {
181                         if (do_lseek(map->fd,read_start,SEEK_SET) != read_start) {
182                                 rprintf(FERROR,"lseek failed in map_ptr\n");
183                                 exit_cleanup(RERR_FILEIO);
184                         }
185                         map->p_fd_offset = read_start;
186                 }
187
188                 if ((nread=read(map->fd,map->p + read_offset,read_size)) != read_size) {
189                         if (nread < 0) nread = 0;
190                         /* the best we can do is zero the buffer - the file
191                            has changed mid transfer! */
192                         memset(map->p+read_offset+nread, 0, read_size - nread);
193                 }
194                 map->p_fd_offset += nread;
195         }
196
197         map->p_offset = window_start;
198         map->p_len = window_size;
199   
200         return map->p + (offset - map->p_offset); 
201 }
202
203
204 void unmap_file(struct map_struct *map)
205 {
206         if (map->p) {
207                 free(map->p);
208                 map->p = NULL;
209         }
210         memset(map, 0, sizeof(*map));
211         free(map);
212 }
213