- added predict.c, moving the routines from util.c
[samba.git] / source / smbd / predict.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    file read prediction routines
5    Copyright (C) Andrew Tridgell 1992-1995
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 #include "loadparm.h"
24
25 extern int DEBUGLEVEL;
26
27
28 /* variables used by the read prediction module */
29 static int rp_fd = -1;
30 static int rp_offset = 0;
31 static int rp_length = 0;
32 static int rp_alloced = 0;
33 static int rp_predict_fd = -1;
34 static int rp_predict_offset = 0;
35 static int 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 time_t smb_last_time=(time_t)0;
41
42 /****************************************************************************
43 handle read prediction on a file
44 ****************************************************************************/
45 int read_predict(int fd,int offset,char *buf,char **ptr,int num)
46 {
47   int ret = 0;
48   int possible = rp_length - (offset - rp_offset);
49
50   possible = MIN(possible,num);
51
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)
57     {
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));
64     }
65
66   if (ret == num) {
67     predict_skip = True;
68   } else {
69     predict_skip = False;
70
71     /* prepare the next prediction */
72     rp_predict_fd = fd;
73     rp_predict_offset = offset + num;
74     rp_predict_length = num;
75   }
76
77   if (ret < 0) ret = 0;
78
79   return(ret);
80 }
81
82 /****************************************************************************
83 pre-read some data
84 ****************************************************************************/
85 void do_read_prediction()
86 {
87   static int readsize = 0;
88
89   if (predict_skip) return;
90
91   if (rp_predict_fd == -1) 
92     return;
93
94   rp_fd = rp_predict_fd;
95   rp_offset = rp_predict_offset;
96   rp_length = 0;
97
98   rp_predict_fd = -1;
99
100   if (readsize == 0) {
101     readsize = lp_readsize();
102     readsize = MAX(readsize,1024);
103   }
104
105   rp_predict_length = MIN(rp_predict_length,2*readsize);
106   rp_predict_length = MAX(rp_predict_length,1024);
107   rp_offset = (rp_offset/1024)*1024;
108   rp_predict_length = (rp_predict_length/1024)*1024;
109
110   if (rp_predict_length > rp_alloced)
111     {
112       rp_buffer = Realloc(rp_buffer,rp_predict_length);
113       rp_alloced = rp_predict_length;
114       if (!rp_buffer)
115         {
116           DEBUG(0,("can't allocate read-prediction buffer\n"));
117           rp_predict_fd = -1;
118           rp_fd = -1;
119           rp_alloced = 0;
120           return;
121         }
122     }
123
124   if (lseek(rp_fd,rp_offset,SEEK_SET) != rp_offset) {
125     rp_fd = -1;
126     rp_predict_fd = -1;
127     return;
128   }
129
130   rp_length = read(rp_fd,rp_buffer,rp_predict_length);
131   rp_time = time(NULL);
132   if (rp_length < 0)
133     rp_length = 0;
134 }
135
136 /****************************************************************************
137 invalidate read-prediction on a fd
138 ****************************************************************************/
139 void invalidate_read_prediction(int fd)
140 {
141  if (rp_fd == fd) 
142    rp_fd = -1;
143  if (rp_predict_fd == fd)
144    rp_predict_fd = -1;
145 }
146