DIRECTORY CIFS USING [OpenFile], Environment USING [bytesPerPage], File USING [Capability], IO USING [STREAM], Juniper USING [LFH, Transaction], Rope USING [ROPE], Transaction USING [Handle, nullHandle]; FileIO: CEDAR DEFINITIONS IMPORTS Transaction = BEGIN ROPE: TYPE = Rope.ROPE; STREAM: TYPE = IO.STREAM; bytesPerPage: CARDINAL = Environment.bytesPerPage; FileSystem: TYPE = { pilot, juniper }; Trans: TYPE = RECORD [ body: SELECT type: FileSystem FROM pilot => [trans: Transaction.Handle], juniper => [trans: Juniper.Transaction _ NIL], ENDCASE]; OpenFailed: SIGNAL [why: OpenFailure, fileName: ROPE] RETURNS [retryFileName: ROPE]; OpenFailure: TYPE = { fileNotFound, illegalFileName, fileAlreadyExists, cantUpdateTiogaFile, wrongTransactionType, unknownFileCapability, notImplementedYet }; CreateOptions: TYPE = {none, newOnly, oldOnly}; AccessOptions: TYPE = {read, append, write, overwrite}; CloseOptions: TYPE = CARDINAL; noCloseOptions: CloseOptions = 0; commitAndReopenTransOnFlush: CloseOptions = 1; truncatePagesOnClose: CloseOptions = 2; finishTransOnClose: CloseOptions = 4; defaultCloseOptions: CloseOptions = truncatePagesOnClose + finishTransOnClose; RawOption: TYPE = BOOL; StreamBufferParms: TYPE = RECORD [ bufferSize: INT [2 .. 127], bufferSwapUnitSize: INT [1 .. 32]]; defaultStreamBufferParms: StreamBufferParms = [bufferSize: 25, bufferSwapUnitSize: 5]; minimumStreamBufferParms: StreamBufferParms = [bufferSize: 2, bufferSwapUnitSize: 1]; Open: PROC [ fileName: ROPE, accessOptions: AccessOptions _ read, createOptions: CreateOptions _ none, closeOptions: CloseOptions _ defaultCloseOptions, transaction: Trans _ [juniper[]], raw: RawOption _ FALSE, createLength: INT _ 5 * bytesPerPage, streamBufferParms: StreamBufferParms _ defaultStreamBufferParms] RETURNS [STREAM]; StreamFromOpenFile: PROC [ openFile: CIFS.OpenFile, accessOptions: AccessOptions _ read, closeOptions: CloseOptions _ defaultCloseOptions, transaction: Transaction.Handle _ Transaction.nullHandle, raw: RawOption _ FALSE, streamBufferParms: StreamBufferParms _ defaultStreamBufferParms] RETURNS [STREAM]; StreamFromCapability: PROC [ capability: File.Capability, accessOptions: AccessOptions _ read, closeOptions: CloseOptions _ defaultCloseOptions, fileName: ROPE _ NIL, transaction: Transaction.Handle _ Transaction.nullHandle, raw: RawOption _ FALSE, streamBufferParms: StreamBufferParms _ defaultStreamBufferParms] RETURNS [STREAM]; CapabilityFromStream: PROC [ self: STREAM] RETURNS [File.Capability]; StreamFromLFH: PROC [ lfh: Juniper.LFH, accessOptions: AccessOptions _ read, closeOptions: CloseOptions _ defaultCloseOptions, fileName: ROPE _ NIL, transaction: Juniper.Transaction, raw: RawOption _ FALSE] RETURNS [STREAM]; END. IO.GetChar: PROC [self: STREAM] RETURNS [CHAR] IO.PutChar: PROC [self: STREAM, char: CHAR] IO.GetBlock: PROC [self: STREAM, block: REF TEXT, startIndex: NAT _ 0, stopIndexPlusOne: NAT _ LAST[NAT]] RETURNS [nBytesRead: NAT] IO.PutBlock: PROC [self: STREAM, block: REF READONLY TEXT, startIndex: NAT _ 0, stopIndexPlusOne: NAT _ LAST[NAT]] IO.UnsafeGetBlock: UNSAFE PROC [self: STREAM, block: IO.UnsafeBlock] RETURNS [nBytesRead: INT] IO.UnsafePutBlock: PROC [self: STREAM, block: IO.UnsafeBlock] IO.CharsAvail: PROC [self: STREAM] RETURNS [BOOL] IO.EndOf: PROC [self: STREAM] RETURNS [BOOL] IO.Flush: PROC [self: STREAM] IO.Reset: PROC [self: STREAM] IO.Close: PROC [self: STREAM, abort: BOOL _ FALSE] IO.PutBack: PROC [self: STREAM, char: CHAR] IO.PeekChar: PROC [self: STREAM] RETURNS [char: CHAR]; IO.GetIndex: PROC [self: STREAM] RETURNS [INT] IO.SetIndex: PROC [self: STREAM, index: INT] IO.GetLength: PROC [self: STREAM] RETURNS [INT] IO.SetLength: PROC [self: STREAM, length: INT] Created by MBrown on 7-Dec-81 10:33:20 Changed by MBrown on March 26, 1982 4:41 pm Changed by MBrown on August 23, 1982 10:25 pm Changed by MBrown on October 23, 1982 9:59 pm Changed by MBrown on January 6, 1983 8:15 pm 0`FileIO.mesa This interface contains procs to create file streams. It also specifies a model for the behavior of file streams. Last edited by: MBrown on January 6, 1983 8:21 pm File streams Basic usage Most programs that create file streams can do so with a very simple call to FileIO.Open. Suppose that r is a Rope.ROPE containing a file name, and your program needs to read characters from the file named by r (local file name or a full path name) using an IO.STREAM s. The call s _ FileIO.Open[r]; in your program will accomplish this. If your program will completely rewrite the file named by r (ignoring the old contents and creating a file of that name if none exists), the call is s _ FileIO.Open[r, overwrite]; If your program is simply logging output to the file named by r (it does not read the file, but simply adds new characters to the end, and creates a new file of that name if none exists), it calls s _ FileIO.Open[r, append]; Finally, if your program will both read and write the file named by r (treats the file as an extendible, random-access sequence of bytes that it updates "in place"), it uses s _ FileIO.Open[r, write]; (The last three forms only work for local files unless you have the Pine package loaded and started, in which case they also work for files on the Juniper server. Note that it is an unusual program that requires write mode, while overwrite mode is used frequently.) If you do not wish to land in the debugger if the file name r is misspelled or otherwise garbled, your program should catch FileIO.OpenFailed with why = fileNotFound or why = illegalFileName. The moral of this tale: doing simple things is simple. You don't need to understand the multitude of parameters to the stream creation procs, since they default correctly for most purposes. You don't need to use any proc from this interface except Open unless you are doing something special. There is only one signal to catch. At the same time, it is possible to do more ambitious things. If you want to know more, read on. File stream model A file is a sequence [0..fileLen) of mutable bytes; the length (fileLen) of a file can be changed. The state of a file stream is a file file (perhaps opened under a transaction) with length fileLen, an index streamIndex IN [0..fileLen], plus a number of readonly flags (accessOptions, closeOptions) and a boolean variable closed which jointly determine the effect of stream procs. In a section below, "What STREAM operations do when applied to a file STREAM", we describe each stream proc defined in the IO interface (GetChar, PutChar, etc.) in terms of these quantities. Concurrency File streams provide no interlocks to control concurrent access to files. Instead, they rely on the underlying file system for concurrency control. Juniper uses locking to provide concurrency control. The Pilot file system used on the local disk provides no concurrency control, though if all of its clients used CIFS and played strictly by the rules there would be no problems. Until that happy day, clients of file streams for local files should take care that if a file stream is open for writing a file, no other readers or writers of that file exist. It is particularly dangerous to have two file streams open for writing the same file. Individual file streams are independent objects, so there is never any need to synchronize calls to their procedures. If two processes are to share a single file stream, they must synchronize their accesses to that stream at a level above the file stream calls; individual file stream calls (PutChar, PutBlock, etc.) are not guaranteed to be atomic. Pragmatics A Pilot file is represented as a sequence of disk pages containing bytesPerPage bytes. There is a single leader page (overhead) that is used to hold file properties such as the length and create date. There is no functional relationship between the length of a file (in bytes) and the size of that file (in pages). Allocating disk pages to a file is an expensive operation, especially when done incrementally (one page at a time). If a client is creating a file whose eventual length it can estimate, it should use the createLength parameter to Open to allocate enough pages to hold the entire length all at once; if the file already exists the client should create a stream and then extend the file using SetLength. If the file stream implementation must extend a file it will always extend it by several pages; if asked to shorten a file it will simply adjust the length field in the leader page without freeing any pages. To free extra pages when a stream is closed, use the truncatePagesOnClose CloseOption in the call that creates the stream. Creating a file STREAM Used to unify the two types of transaction, for Open only. This signal is raised by the various file stream creation procedures. See description of Open below. Used at the time a stream is created to specify the set of operations allowed on a stream. Disallowed operations raise IO.Error[NotImplementedForThisStream] when called. The characteristics of each option: read: PutChar, PutBlock, SetLength are disallowed, and the initial streamIndex is 0. overwrite: all operations are allowed, the file is truncated to zero length at stream creation time, and the initial streamIndex is 0. append: GetChar, GetBlock, SetLength, SetIndex are disallowed, and the initial streamIndex is fileLen. write: all operations are allowed, and the initial streamIndex is 0. CloseOptions determine optional processing during Flush and Close calls. If commitAndReopenTransOnFlush, then Flush "checkpoints" the transaction being used by the stream. If truncatePagesOnClose, then Close causes extra pages of the file to be freed. If finishTransOnClose, then Close causes the transaction to be committed or aborted, according to the abort flag to Close. This parameter determines the mode of access to Tioga format files. If raw = FALSE and file is in Tioga format, then if accessOptions = read, read only the plain text portion of the file (ignore "looks" and nodes with the comment property); if accessOptions # read or overwrite, raise OpenFailure [cantUpdateTiogaFile]. If raw = TRUE or file is not in Tioga format, then operate on the entire file. Specifies the number of pages of VM used by the stream for buffering. Juniper streams always use 1 page of buffering. Specifies the number of pages in each uniform swap unit of the stream buffer. Good for most purposes. Good when opening a stream just to create a file or set its length. ! OpenFailed with why = fileNotFound: createOptions = oldOnly and file does not exist (including server not found when fileName is a full path name). illegalFileName: syntax or other error caused directory lookup to fail. fileAlreadyExists: createOptions = newOnly and file already exists. cantUpdateTiogaFile: raw = FALSE, accessOptions # read, and file is in Tioga format. wrongTransactionType: transaction is a non-null Pilot transaction but fileName is a Juniper file, or vice-versa. notImplementedYet: CIFS access is implied but accessOptions # read. ! Juniper.Error with why = transactionReset, notDone: don't know how to handle these. ! CIFS.Error with code # illegalFileName, noSuchFile, noSuchHost: don't know how to handle these. ! Volume.InsufficientSpace: couldn't create file on local volume as requested. Create a new stream on the file specified by fileName (a full path name). If accessOptions = read then createOptions = oldOnly are assumed. If createOptions = none or newOnly and the file specified by fileName does not exist, then create it, with initial size (in pages excluding leader page) of createLength/bytesPerPage (rounded up), and initial length (in bytes) zero. If fileName specifies the server "Juniper" and transaction either has pilot variant and contains Transaction.nullHandle or has juniper variant but contains NIL ([juniper[]] produces this), then call Juniper.UserInit[], and create a new transaction by calling Juniper.BeginTransaction. ! OpenFailed with why = notImplementedYet: accessOptions # read. Create a new file stream on the open file. ! OpenFailed with why = cantUpdateTiogaFile: raw = FALSE, accessOptions # read, and file is in Tioga format. unknownFileCapability: no file identified by capability is present on the local disk volume. Create a new stream on the file identified by capability (note that StreamFromCapability adds permissions to capability as required to perform the accesses as specified in accessOptions). fileName is stored in the stream, for diagnostic purposes when a stream error occurs. ! IO.Error[NotImplementedForThisStream]: self is not a stream on a Pilot file Return the file capability for the file underlying self. This capability has the permissions that are necessary to perform stream operations. ! OpenFailed with why = cantUpdateTiogaFile: raw = FALSE, accessOptions # read, and file is in Tioga format. unknownFileCapability: no file with the given LFH is present on Juniper. Create a new stream on the Juniper file identified by lfh. fileName is a debugging aid, for diagnostic purposes when a stream error occurs. What STREAM operations do when applied to a file STREAM Basic STREAM procs If streamIndex = fileLen then ERROR IO.EndOfStream. Else return file[streamIndex], and set streamIndex _ streamIndex + 1. If streamIndex = fileLen then fileLen _ fileLen + 1. Then set file[streamIndex] _ char, streamIndex _ streamIndex + 1. Equivalent to (but faster than) stopIndexPlusOne _ MIN [block.maxLength, stopIndexPlusOne]; nBytesRead: NAT _ MIN[fileLen-streamIndex, stopIndexPlusOne-startIndex]; FOR i: NAT IN [0..nBytesRead) DO block[startIndex+i] _ GetChar[self] ENDLOOP; IF nBytesRead # 0 THEN block.length _ startIndex + nBytesRead; RETURN[nBytesRead] Equivalent to (but faster than) IF stopIndexPlusOne > block.maxLength THEN stopIndexPlusOne _ block.length; FOR i: NAT IN [startIndex..stopIndexPlusOne) DO PutChar[self, block[i]] ENDLOOP; Equivalent to (but faster than) IF block.startIndex < 0 OR block.stopIndexPlusOne < 0 THEN ERROR IO.Error[BadIndex]; nBytesRead: INT _ MIN[fileLen-streamIndex, block.stopIndexPlusOne-block.startIndex]; FOR i: INT IN [0..nBytesRead) DO block.base^[block.startIndex+i] _ GetChar[self] ENDLOOP; RETURN[nBytesRead] Equivalent to (but faster than) IF block.startIndex < 0 OR block.stopIndexPlusOne < 0 THEN ERROR IO.Error[BadIndex]; FOR i: INT IN [block.startIndex..block.stopIndexPlusOne) DO PutChar[self, block.base^[i]] ENDLOOP; Return TRUE. Return streamIndex = fileLen. Force all stream writes since the time of stream creation or the preceding Flush to be written to disk. If commitAndReopenTransOnFlush then first commit trans, then begin a new transaction as trans. If accessOptions # append then set streamIndex _ 0. If NOT abort, then force all stream actions since stream creation or the preceding Flush to be written to disk; otherwise discard them. If truncatePagesOnClose, then discard unused pages from end of file. If finishTransOnClose, then commit or abort trans depending on the state of abort. Invalidate self (all operations on self other than Flush, Reset, and Close will raise ERROR IO.Error[StreamClosed]; these three do nothing). Less basic STREAM procs If streamIndex = 0 then ERROR IO.Error[IllegalPutBack]. Otherwise, set streamIndex _ streamIndex - 1, and if file[streamIndex] # char then ERROR IO.Error[IllegalPutBack]. Equivalent to: c: CHAR _ self.GetChar[]; self.PutBack[c]; RETURN [c]; File-specific STREAM procs Return streamIndex. ERROR IO.EndOfStream if index > fileLen. Set streamIndex _ index. Return fileLen. Set fileLen _ l, then set streamIndex _ MIN[streamIndex, fileLen]. The contents of file[oldFileLen .. fileLen) are undefined. Change Log By editing FileByteStream. Added "raw" parm to stream create operations. Handle -> STREAM (-> Stream in proc names), introduce FileIO.OpenFailed, CIFS access in readonly mode, initial file size in Open, buffer pages and swap units. Format this file using Tioga nodes. Default transaction to Open is now [juniper[]], since this does not require the average user to import Transaction (to use nullHandle). Format to use current Cedar style. Attempted to reduce the confusion over what is comment and what is not comment in the interface, and to improve the explanation of AccessOptions. Κ ά– "Cedar" style˜headšœ ™ Ibodyšœr™runitšΟr™Icodeš!™!—šΟk ˜ Nšžœžœ ˜Nšœ žœ˜!Nšœžœ˜Nšžœžœžœ˜Nšœžœžœ˜!Nšœžœžœ˜Nšœ žœ˜'—šœ žœž ˜šž˜N˜ —Nšž˜Nšžœžœžœ˜Nšžœžœžœžœ˜Nšœžœ˜2——šœ ™ šœ ™ šœsžœ‹žœžœ ™˜Iindentšœ™—šœ»™»Ošœ™—šœΔ™ΔOšœ™—šœ­™­Ošœ™—LšœΛ™ΛLšœΚ™ΚLšœa™a—šœ™Lšœb™bLšœmΟe œžœ0Ÿ œŸ œŸœPžœ&žœ/žœA™Ω—™ LšœΌžœΖΟc™‡Lšœή™ή—™ LšœΌ™ΌLšœΰ™ΰ——šœž™MšΟn œžœ˜&š‘œžœžœ˜šœžœž˜"N˜'Nšœ)žœ˜.Nšžœ˜ —I continuationšœ:™:—Mš ‘ œžœžœžœžœ˜Tš‘ œžœ˜žPšœE™E—š‘ œžœ˜/Pšœ™—š‘ œžœ$˜7šœxžœU™ΟIitemšœU™UQšœ†™†Qšœf™fQšœD™D——š‘ œžœžœ˜Pšœω™ωN˜!N˜.N˜'N˜%N˜N—š‘ œžœžœ˜PšœNžœψžœA™—š‘œžœžœ˜"šœ žœ ˜Jšv™v—šœžœ ˜#JšM™M—˜VJš™—˜UJšC™C——š‘œžœ˜ Nšœ žœ˜N˜$N˜$N˜1N˜!Nšœžœ˜Nšœžœ˜%N˜@Nšžœžœ˜š ™Jš _œ ™}Jš G™GJš C™CJš Πck 4™TJš p™pJš ’ ,™C—JšœU™UJšœžœ[™aš N™NLšœI™ILšœA™ALšœη™ηLšœœ™œ——š‘œžœ˜Nšœ žœ ˜N˜$N˜1N˜9Nšœžœ˜N˜@Nšžœžœ˜š ™Jš (™(Lšœ*™*——š‘œžœ˜N˜N˜$N˜1Nšœ žœžœ˜N˜9Nšœžœ˜N˜@Nšžœžœ˜š ™Jš Πcr ’ £  %™TJš -£  %™\Lšœ’™’——š‘œžœ˜Nšœžœ˜ Nšžœ˜š ’ %£  ™MLšœŽ™Ž——š‘ œžœ˜Nšœ žœ˜N˜$N˜1Nšœ žœžœ˜N˜!Nšœžœ˜Nšžœžœ˜š ™Jš £ ’ £  %™TJš .ž ™HLšœŒ™Œ——Mšžœ˜—šœžœ&ž™7šœžœ™š ΡknzΠnzœžœžœžœžœ˜.JšΠkr¦T™z—š €₯œžœžœžœ˜+Jšw™w—š€₯ œžœžœ žœžœžœžœžœžœžœ˜ƒJš™Jš¦%™;Jš ¦¦3™HJš ¦¦¦¦%¦™MJš¦¦(™>Jš¦ ™—š€₯ œžœžœ žœžœžœžœžœ˜rJš™Jš¦$¦!™KJš ¦¦¦ ¦¦™P—š €₯œž œžœ žœžœžœ˜^Jš™Jš¦4¦™:Jš¦¦™Jš ¦¦?™TJš¦¦¦¦™ Jš2¦™:Jš¦ ™—š €₯œžœžœ žœ ˜=Jš™Jš¦4¦™:Jš¦¦™Jš¦¦¦,¦™;Jš ¦™(—š €₯ œžœžœžœžœ˜1Jš¦™ —š €₯œžœžœžœžœ˜,Jš™—š€₯œžœžœ˜JšΗ™Η—š€₯œžœžœ˜Jš3™3—š €₯œžœžœ žœžœ˜2Jš¦σ¦¦.™―——šœ žœ™š Πkn‘œžœžœžœ˜+Jš ¦¦l¦¦™«—š §‘ œžœžœžœžœ˜6Jš™Jš¦%¦™7——šœžœ™š §‘ œžœžœžœžœ˜.Jš™—š §‘ œžœžœ žœ˜,Jš¦¦:™B—š §‘ œžœžœžœžœ˜/Jš™—š §‘ œžœžœ žœ˜.Jš(¦S™~———šœ ™ ˜'Jšœ™—˜+Jšœ-™-—˜-Jšœ žœ9žœv™Γ—˜-Jšœ«™«—˜,J™‘———…—hI€