2 Unix SMB/Netbios implementation.
4 file read prediction routines
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
24 extern int DEBUGLEVEL;
26 #if USE_READ_PREDICTION
28 /* variables used by the read prediction module */
29 static int rp_fd = -1;
30 static SMB_OFF_T rp_offset = 0;
31 static ssize_t rp_length = 0;
32 static ssize_t rp_alloced = 0;
33 static int rp_predict_fd = -1;
34 static SMB_OFF_T rp_predict_offset = 0;
35 static size_t rp_predict_length = 0;
36 static int rp_timeout = 5;
37 static time_t rp_time = 0;
38 static char *rp_buffer = NULL;
39 static BOOL predict_skip=False;
40 extern struct timeval smb_last_time;
42 /****************************************************************************
43 handle read prediction on a file
44 ****************************************************************************/
45 ssize_t read_predict(int fd,SMB_OFF_T offset,char *buf,char **ptr,size_t num)
48 ssize_t possible = rp_length - (offset - rp_offset);
50 possible = MIN(possible,num);
52 /* give data if possible */
54 offset >= rp_offset &&
56 smb_last_time.tv_secs - rp_time < rp_timeout)
60 memcpy(buf,rp_buffer + (offset-rp_offset),possible);
62 *ptr = rp_buffer + (offset-rp_offset);
63 DEBUG(5,("read-prediction gave %d bytes of %d\n",ret,num));
69 SMB_STRUCT_STAT rp_stat;
71 /* Find the end of the file - ensure we don't
72 read predict beyond it. */
73 if(sys_fstat(fd,&rp_stat) < 0)
75 DEBUG(0,("read-prediction failed on fstat. Error was %s\n", strerror(errno)));
82 /* prepare the next prediction */
84 /* Make sure we don't seek beyond the end of the file. */
85 rp_predict_offset = MIN((offset + num),rp_stat.st_size);
86 rp_predict_length = num;
95 /****************************************************************************
97 ****************************************************************************/
98 void do_read_prediction(void)
100 static size_t readsize = 0;
102 if (predict_skip) return;
104 if (rp_predict_fd == -1)
107 rp_fd = rp_predict_fd;
108 rp_offset = rp_predict_offset;
114 readsize = lp_readsize();
115 readsize = MAX(readsize,1024);
118 rp_predict_length = MIN(rp_predict_length,2*readsize);
119 rp_predict_length = MAX(rp_predict_length,1024);
120 rp_offset = (rp_offset/1024)*1024;
121 rp_predict_length = (rp_predict_length/1024)*1024;
123 if (rp_predict_length > rp_alloced)
125 rp_buffer = Realloc(rp_buffer,rp_predict_length);
126 rp_alloced = rp_predict_length;
129 DEBUG(0,("can't allocate read-prediction buffer\n"));
137 if (sys_lseek(rp_fd,rp_offset,SEEK_SET) != rp_offset) {
143 rp_length = read(rp_fd,rp_buffer,rp_predict_length);
144 rp_time = time(NULL);
149 /****************************************************************************
150 invalidate read-prediction on a fd
151 ****************************************************************************/
152 void invalidate_read_prediction(int fd)
156 if (rp_predict_fd == fd)
161 void read_prediction_dummy(void) ;