Put in fix for read-prediction extending files bug. Hard to test, can't
[kai/samba.git] / source3 / smbd / predict.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    file read prediction routines
5    Copyright (C) Andrew Tridgell 1992-1997
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
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
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26
27 /* variables used by the read prediction module */
28 static int rp_fd = -1;
29 static int rp_offset = 0;
30 static int rp_length = 0;
31 static int rp_alloced = 0;
32 static int rp_predict_fd = -1;
33 static int rp_predict_offset = 0;
34 static int rp_predict_length = 0;
35 static int rp_timeout = 5;
36 static time_t rp_time = 0;
37 static char *rp_buffer = NULL;
38 static BOOL predict_skip=False;
39 time_t smb_last_time=(time_t)0;
40
41 /****************************************************************************
42 handle read prediction on a file
43 ****************************************************************************/
44 int read_predict(int fd,int offset,char *buf,char **ptr,int num)
45 {
46   int ret = 0;
47   int possible = rp_length - (offset - rp_offset);
48
49   possible = MIN(possible,num);
50
51   /* give data if possible */
52   if (fd == rp_fd && 
53       offset >= rp_offset && 
54       possible>0 &&
55       smb_last_time-rp_time < rp_timeout)
56   {
57     ret = possible;
58     if (buf)
59       memcpy(buf,rp_buffer + (offset-rp_offset),possible);
60     else
61       *ptr = rp_buffer + (offset-rp_offset);
62     DEBUG(5,("read-prediction gave %d bytes of %d\n",ret,num));
63   }
64
65   if (ret == num) {
66     predict_skip = True;
67   } else {
68     struct stat rp_stat;
69
70     /* Find the end of the file - ensure we don't
71        read predict beyond it. */
72     if(fstat(fd,&rp_stat) < 0)
73     {
74       DEBUG(0,("read-prediction failed on fstat. Error was %s\n", strerror(errno)));
75       predict_skip = True;
76     }
77     else
78     {
79       predict_skip = False;
80
81       /* prepare the next prediction */
82       rp_predict_fd = fd;
83       /* Make sure we don't seek beyond the end of the file. */
84       rp_predict_offset = MIN((offset + num),rp_stat.st_size);
85       rp_predict_length = num;
86     }
87   }
88
89   if (ret < 0) ret = 0;
90
91   return(ret);
92 }
93
94 /****************************************************************************
95 pre-read some data
96 ****************************************************************************/
97 void do_read_prediction()
98 {
99   static int readsize = 0;
100
101   if (predict_skip) return;
102
103   if (rp_predict_fd == -1) 
104     return;
105
106   rp_fd = rp_predict_fd;
107   rp_offset = rp_predict_offset;
108   rp_length = 0;
109
110   rp_predict_fd = -1;
111
112   if (readsize == 0) {
113     readsize = lp_readsize();
114     readsize = MAX(readsize,1024);
115   }
116
117   rp_predict_length = MIN(rp_predict_length,2*readsize);
118   rp_predict_length = MAX(rp_predict_length,1024);
119   rp_offset = (rp_offset/1024)*1024;
120   rp_predict_length = (rp_predict_length/1024)*1024;
121
122   if (rp_predict_length > rp_alloced)
123     {
124       rp_buffer = Realloc(rp_buffer,rp_predict_length);
125       rp_alloced = rp_predict_length;
126       if (!rp_buffer)
127         {
128           DEBUG(0,("can't allocate read-prediction buffer\n"));
129           rp_predict_fd = -1;
130           rp_fd = -1;
131           rp_alloced = 0;
132           return;
133         }
134     }
135
136   if (lseek(rp_fd,rp_offset,SEEK_SET) != rp_offset) {
137     rp_fd = -1;
138     rp_predict_fd = -1;
139     return;
140   }
141
142   rp_length = read(rp_fd,rp_buffer,rp_predict_length);
143   rp_time = time(NULL);
144   if (rp_length < 0)
145     rp_length = 0;
146 }
147
148 /****************************************************************************
149 invalidate read-prediction on a fd
150 ****************************************************************************/
151 void invalidate_read_prediction(int fd)
152 {
153  if (rp_fd == fd) 
154    rp_fd = -1;
155  if (rp_predict_fd == fd)
156    rp_predict_fd = -1;
157 }
158