(FILECREATED "30-Oct-87 16:07:03" {ERIS}<DANIELS>LISP>KOTO>LFEQFIX.;1 3981   

      changes to:  (VARS LFEQFIXCOMS)
		   (FNS \LFReadPages))


(* Copyright (c) 1987 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT LFEQFIXCOMS)

(RPAQQ LFEQFIXCOMS ((FNS \LFReadPages \VFMCorrectBufferP \VFMMarkBufferDirty)))
(DEFINEQ

(\LFReadPages
  [LAMBDA (stream streamFirstPage buffers)                   (* edited: "30-Oct-87 16:04")

          (* * Reads a bunch of pages from stream, starting at firstPage. Returns number of bytes read.)



          (* * Modified " 4-Jul-85 04:47:22" by HTS to extend the backing file whenever it tries to read past the end of the 
	  backing file. This generally ensures that data subsequently written on these buffer pages will not be lost if you 
	  run out of disk space)



          (* * If asked to read a page which is off the end of the stream, it will zero the page. Odd though it may seem, 
	  reading off the end of the file is reasonable behavior for copybytes: buffer pages must come from somewhere, and 
	  copybytes may not have to write the whole page, and in general copybytes does not know whether a page is actually 
	  in a file or off the end of it. Seems inefficient, but since reading past eof does not actually require disk 
	  access, its not that bad.)



          (* * Extend backing file if necessary to accomodate buffers.)


    (\LFExtendFileIfNecessary stream streamFirstPage buffers)

          (* * Write out the buffers to the backing file.)


    (for buffer inside buffers as streamPageNumber from streamFirstPage as 
									    backingFilePageNumber
       from (ADD1 streamFirstPage) bind (file ←(fetch (DLIONSTREAM FILEDESC) of stream))
					      lastStreamPage offset
       first (\UPDATEOF stream)
	       (SETQ lastStreamPage (PLUS (fetch (DLIONSTREAM EPAGE) of stream)
					      (if (EQ 0 (fetch (DLIONSTREAM EOFFSET)
							       of stream))
						  then -1
						else 0)))
       sum (if (LEQ streamPageNumber lastStreamPage)
		 then 

          (* * If page inside stream, then it has presumably already been written; read it in.)


			(\PFGetPage file backingFilePageNumber (\PFFindPageAddr file 
									    backingFilePageNumber)
				      buffer)

          (* * If this was the last page in the file, then fill in the trailing bytes with nulls.)


			(if (EQP streamPageNumber lastStreamPage)
			    then (SETQ offset (fetch (DLIONSTREAM EOFFSET) of stream))
				   (if (EQ offset 0)
				       then (SETQ offset BYTESPERPAGE)
				     else (\CLEARBYTES buffer offset (DIFFERENCE BYTESPERPAGE 
										       offset)))
				   offset
			  else BYTESPERPAGE)
	       else 

          (* * If this was outside the stream, clear the buffer.)


		      (\CLEARWORDS buffer WORDSPERPAGE)
		      0])

(\VFMCorrectBufferP
  [LAMBDA (BUFFER VOLPAGENUM)                                (* edited: "30-Oct-87 16:05")

          (* * True iff BUFFER is the right buffer for VOLPAGENUM)


    (AND (EQ (fetch (\BTREEBUF VOLUME) of BUFFER)
		 \VFMvolumeHandle)
	   (EQP (fetch (\BTREEBUF VOLPAGENUM) of BUFFER)
		  VOLPAGENUM])

(\VFMMarkBufferDirty
  [DLAMBDA ((BUFFERPAGE Buffer)
            (RETURNS NIL))
                                                             (* edited: "30-Oct-87 16:05")

          (* * Note that the specified buffer has been written into and will have to be flushed out to disk.)


    (replace (\BTREEBUF DIRTY) of (for BUF in \VFMbufferPool
					 thereis (EQP BUFFERPAGE (fetch (\BTREEBUF PAGE)
									of BUF)))
       with T)
    NIL])
)
(PUTPROPS LFEQFIX COPYRIGHT ("Xerox Corporation" 1987))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (331 3903 (\LFReadPages 341 . 3049) (\VFMCorrectBufferP 3051 . 3409) (
\VFMMarkBufferDirty 3411 . 3901)))))
STOP