home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / samba-1.9.18p7.tar.gz / samba-1.9.18p7.tar / samba-1.9.18p7 / source / predict.c < prev    next >
C/C++ Source or Header  |  1998-05-04  |  4KB  |  163 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    file read prediction routines
  5.    Copyright (C) Andrew Tridgell 1992-1998
  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. #if USE_READ_PREDICTION
  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. extern time_t smb_last_time;
  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.     struct stat rp_stat;
  70.  
  71.     /* Find the end of the file - ensure we don't
  72.        read predict beyond it. */
  73.     if(fstat(fd,&rp_stat) < 0)
  74.     {
  75.       DEBUG(0,("read-prediction failed on fstat. Error was %s\n", strerror(errno)));
  76.       predict_skip = True;
  77.     }
  78.     else
  79.     {
  80.       predict_skip = False;
  81.  
  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;
  87.     }
  88.   }
  89.  
  90.   if (ret < 0) ret = 0;
  91.  
  92.   return(ret);
  93. }
  94.  
  95. /****************************************************************************
  96. pre-read some data
  97. ****************************************************************************/
  98. void do_read_prediction(void)
  99. {
  100.   static int readsize = 0;
  101.  
  102.   if (predict_skip) return;
  103.  
  104.   if (rp_predict_fd == -1) 
  105.     return;
  106.  
  107.   rp_fd = rp_predict_fd;
  108.   rp_offset = rp_predict_offset;
  109.   rp_length = 0;
  110.  
  111.   rp_predict_fd = -1;
  112.  
  113.   if (readsize == 0) {
  114.     readsize = lp_readsize();
  115.     readsize = MAX(readsize,1024);
  116.   }
  117.  
  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;
  122.  
  123.   if (rp_predict_length > rp_alloced)
  124.     {
  125.       rp_buffer = Realloc(rp_buffer,rp_predict_length);
  126.       rp_alloced = rp_predict_length;
  127.       if (!rp_buffer)
  128.     {
  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;
  134.     }
  135.     }
  136.  
  137.   if (lseek(rp_fd,rp_offset,SEEK_SET) != rp_offset) {
  138.     rp_fd = -1;
  139.     rp_predict_fd = -1;
  140.     return;
  141.   }
  142.  
  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;
  147. }
  148.  
  149. /****************************************************************************
  150. invalidate read-prediction on a fd
  151. ****************************************************************************/
  152. void invalidate_read_prediction(int fd)
  153. {
  154.  if (rp_fd == fd) 
  155.    rp_fd = -1;
  156.  if (rp_predict_fd == fd)
  157.    rp_predict_fd = -1;
  158. }
  159.  
  160. #else
  161.  void read_prediction_dummy(void) ;
  162. #endif
  163.