added file from 2.0 branch
[Samba/gebeck_regimport.git] / source / smbd / predict.c
blob5f015139cf3abd214a8ececd424c79870b2b0dd1
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #include "includes.h"
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 time_t 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)
47 ssize_t ret = 0;
48 ssize_t possible = rp_length - (offset - rp_offset);
50 possible = MIN(possible,num);
52 /* give data if possible */
53 if (fd == rp_fd &&
54 offset >= rp_offset &&
55 possible>0 &&
56 smb_last_time-rp_time < rp_timeout)
58 ret = possible;
59 if (buf)
60 memcpy(buf,rp_buffer + (offset-rp_offset),possible);
61 else
62 *ptr = rp_buffer + (offset-rp_offset);
63 DEBUG(5,("read-prediction gave %d bytes of %d\n",ret,num));
66 if (ret == num) {
67 predict_skip = True;
68 } else {
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)));
76 predict_skip = True;
78 else
80 predict_skip = False;
82 /* prepare the next prediction */
83 rp_predict_fd = fd;
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;
90 if (ret < 0) ret = 0;
92 return(ret);
95 /****************************************************************************
96 pre-read some data
97 ****************************************************************************/
98 void do_read_prediction(void)
100 static size_t readsize = 0;
102 if (predict_skip) return;
104 if (rp_predict_fd == -1)
105 return;
107 rp_fd = rp_predict_fd;
108 rp_offset = rp_predict_offset;
109 rp_length = 0;
111 rp_predict_fd = -1;
113 if (readsize == 0) {
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;
127 if (!rp_buffer)
129 DEBUG(0,("can't allocate read-prediction buffer\n"));
130 rp_predict_fd = -1;
131 rp_fd = -1;
132 rp_alloced = 0;
133 return;
137 if (sys_lseek(rp_fd,rp_offset,SEEK_SET) != rp_offset) {
138 rp_fd = -1;
139 rp_predict_fd = -1;
140 return;
143 rp_length = read(rp_fd,rp_buffer,rp_predict_length);
144 rp_time = time(NULL);
145 if (rp_length < 0)
146 rp_length = 0;
149 /****************************************************************************
150 invalidate read-prediction on a fd
151 ****************************************************************************/
152 void invalidate_read_prediction(int fd)
154 if (rp_fd == fd)
155 rp_fd = -1;
156 if (rp_predict_fd == fd)
157 rp_predict_fd = -1;
160 #else
161 void read_prediction_dummy(void) ;
162 #endif