Files64.Mod 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  2. MODULE Files64; (* pjm *)
  3. (** Aos file system base. *)
  4. IMPORT SYSTEM, Streams := Streams64, KernelLog, Modules, Kernel, Commands;
  5. CONST
  6. (** Volume & file flags *)
  7. ReadOnly* = 0;
  8. (** Volume flags *)
  9. Removable* = 1;
  10. Boot* = 2;
  11. (** File flags *)
  12. Directory* = 1;
  13. Hidden* = 2;
  14. System* = 3;
  15. Archive* = 4;
  16. Temporary* = 5;
  17. Ok* = 0;
  18. (* Volume level errors *)
  19. ReadOnlyError = 2901; (* Tried to modify read-only volume. Causes HALT *)
  20. VolumeFull = 2902; (* Tried to allocate block on full volume. Causes HALT *)
  21. InvalidAdr= 2903; (* Block address outside of volume. Causes HALT *)
  22. (* File level errors *)
  23. VolumeReadOnly* = 2905; (** Cannot modify read-only volume *)
  24. FsNotFound* = 2906; (** File system not found *)
  25. FileAlreadyExists* = 2908; (** File already exists *)
  26. BadFileName* = 2909; (** Bad file name *)
  27. FileNotFound* = 2910; (** File not found *)
  28. EnumSize* = 0; EnumTime* = 1; (** Enumerate flags. *)
  29. PrefixLength* = 16; (** maximum length of a file system prefix. *)
  30. NameLength* = 256; (** maximum length of a file name. *)
  31. Trace = FALSE;
  32. WriteError = 2907;
  33. DefaultWriterSize = 4096;
  34. DefaultReaderSize = 4096;
  35. PathDelimiter* = "/"; (** Path delimiter *)
  36. BufferSize = 32*1024; (* Buffersize for file copy operation *)
  37. SetSize = MAX (SET) + 1;
  38. (* file system behaviour flags *)
  39. NeedsPrefix* = 0; (* if no prefix given, then this file system cannot handle it, to prevent file systems from being in the search path *)
  40. TYPE
  41. BESTSIZE* = Streams.BESTSIZE;
  42. (** All record fields are read-only for users, and read-write for extenders. *)
  43. FileName* = ARRAY PrefixLength+NameLength OF CHAR;
  44. (** A rider points to some location in a file, where reading and writing will be done. *)
  45. Rider* = RECORD (** not shareable between multiple processes *)
  46. (* the rider must be a record, otherwise the Oberon text system will not work *)
  47. eof*: BOOLEAN; (** has end of file been passed *)
  48. res*: LONGINT; (** leftover byte count for ReadBytes/WriteBytes *)
  49. (** private fields for implementors *)
  50. apos*: BESTSIZE;
  51. bpos*: LONGINT;
  52. hint*: Hint;
  53. file*: File;
  54. fs*: FileSystem;
  55. END;
  56. TYPE
  57. (** Reader for buffered reading of a file via Streams.Read* procedures. See OpenReader. *)
  58. Reader* = OBJECT (Streams.Reader) (** not sharable between multiple processes *)
  59. VAR
  60. file : File;
  61. r: Rider;
  62. PROCEDURE Receive(VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len, res: LONGINT);
  63. BEGIN
  64. file.ReadBytes(r, buf, ofs, size);
  65. len := size - r.res;
  66. IF len >= min THEN res := Streams.Ok ELSE res := Streams.EOF (* end of file *) END
  67. END Receive;
  68. PROCEDURE CanSetPos*() : BOOLEAN;
  69. BEGIN
  70. RETURN TRUE;
  71. END CanSetPos;
  72. PROCEDURE SetPos*(pos : BESTSIZE);
  73. BEGIN
  74. file.Set(r, pos);
  75. Reset;
  76. received := pos; (* this effects that Streams.Reader.Pos() returns the correct location in the file *)
  77. END SetPos;
  78. PROCEDURE &InitFileReader*(file : File; pos: BESTSIZE);
  79. BEGIN
  80. ASSERT(file # NIL);
  81. SELF.file := file;
  82. file.Set(r, pos);
  83. received := pos; (* this effects that Streams.Reader.Pos() returns the correct location in the file *)
  84. InitReader(SELF.Receive, DefaultReaderSize);
  85. END InitFileReader;
  86. END Reader;
  87. TYPE
  88. (** Writer for buffered writing of a file via Streams.Write* procedures. See OpenWriter. *)
  89. Writer* = OBJECT (Streams.Writer) (** not sharable between multiple processes *)
  90. VAR
  91. file : File;
  92. r: Rider;
  93. PROCEDURE Send(CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: LONGINT);
  94. BEGIN
  95. r.file.WriteBytes(r, buf, ofs, len);
  96. IF propagate THEN r.file.Update END;
  97. IF r.res = 0 THEN res := Streams.Ok ELSE res := WriteError (* not all bytes written *) END
  98. END Send;
  99. PROCEDURE CanSetPos*() : BOOLEAN;
  100. BEGIN
  101. RETURN TRUE;
  102. END CanSetPos;
  103. PROCEDURE SetPos*(pos : BESTSIZE);
  104. BEGIN
  105. Update;
  106. file.Set(r, pos);
  107. Reset;
  108. END SetPos;
  109. PROCEDURE Pos*(): BESTSIZE;
  110. BEGIN
  111. Update;
  112. RETURN file.Pos(r)
  113. END Pos;
  114. PROCEDURE &InitFileWriter*(file: File; pos: BESTSIZE);
  115. BEGIN
  116. ASSERT(file # NIL);
  117. SELF.file := file;
  118. file.Set(r, pos);
  119. InitWriter(SELF.Send, DefaultWriterSize);
  120. END InitFileWriter;
  121. END Writer;
  122. Prefix* = ARRAY PrefixLength OF CHAR;
  123. Address* = LONGINT; (** Volume block address [1..size] *)
  124. Hint* = POINTER TO RECORD END; (** for use by file system implementors. *)
  125. Bytes2 = ARRAY 2 OF CHAR;
  126. Bytes4 = ARRAY 4 OF CHAR;
  127. Bytes8 = ARRAY 8 OF CHAR;
  128. TYPE
  129. (** Volume is the base type of all volumes. It provides operations on an abstract array of file system data blocks of blockSize bytes, numbered from 1 to size. It is mainly used by file system implementations. *)
  130. Volume* = OBJECT (** shareable *)
  131. VAR
  132. size*: BESTSIZE; (** size in blocks *)
  133. blockSize*: LONGINT; (** block size in bytes *)
  134. flags*: SET; (** ReadOnly, Removable, Boot *)
  135. name*: ARRAY 32 OF CHAR; (** descriptive name - e.g. for matching with Partitions.Show *)
  136. map: POINTER TO ARRAY OF SET; (* Block allocation table *)
  137. used: BESTSIZE; (* used blocks *)
  138. reserved: BESTSIZE; (* blocks reserved for system *)
  139. PROCEDURE AllocBlock*(hint: BESTSIZE; VAR adr: BESTSIZE);
  140. BEGIN {EXCLUSIVE}
  141. IF ReadOnly IN flags THEN HALT(ReadOnlyError) END;
  142. IF size - used <= reserved THEN HALT(VolumeFull) END;
  143. ASSERT(hint >= 0);
  144. IF hint > size THEN hint := 0 END;
  145. adr := hint+1;
  146. LOOP
  147. IF adr > size THEN adr := 0 END;
  148. IF (adr MOD SetSize) IN map[adr DIV SetSize] THEN
  149. INC(adr) (* Block in use *)
  150. ELSE
  151. INCL(map[adr DIV SetSize], adr MOD SetSize);
  152. EXIT
  153. END;
  154. IF adr = hint THEN HALT(VolumeFull) END
  155. END;
  156. INC(used)
  157. END AllocBlock;
  158. PROCEDURE FreeBlock*(adr: BESTSIZE);
  159. BEGIN {EXCLUSIVE}
  160. IF (adr < 1) OR (adr > size) THEN HALT(InvalidAdr) END;
  161. IF ReadOnly IN flags THEN HALT(ReadOnlyError) END;
  162. EXCL(map[adr DIV SetSize], adr MOD SetSize);
  163. DEC(used)
  164. END FreeBlock;
  165. PROCEDURE MarkBlock*(adr: BESTSIZE);
  166. BEGIN {EXCLUSIVE}
  167. IF (adr < 1) OR (adr > size) THEN HALT(InvalidAdr) END;
  168. IF ReadOnly IN flags THEN HALT(ReadOnlyError) END;
  169. INCL(map[adr DIV SetSize], adr MOD SetSize);
  170. INC(used)
  171. END MarkBlock;
  172. PROCEDURE Marked*(adr: BESTSIZE): BOOLEAN;
  173. BEGIN {EXCLUSIVE}
  174. IF (adr < 1) OR (adr > size) THEN HALT(InvalidAdr) END;
  175. IF ReadOnly IN flags THEN HALT(ReadOnlyError) END;
  176. RETURN (adr MOD SetSize) IN map[adr DIV SetSize]
  177. END Marked;
  178. PROCEDURE Available*(): BESTSIZE;
  179. BEGIN {EXCLUSIVE}
  180. RETURN size - used
  181. END Available;
  182. PROCEDURE GetBlock*(adr: BESTSIZE; VAR blk: ARRAY OF CHAR);
  183. BEGIN HALT(301) END GetBlock; (* abstract *)
  184. PROCEDURE PutBlock*(adr: BESTSIZE; VAR blk: ARRAY OF CHAR);
  185. BEGIN HALT(301) END PutBlock; (* abstract *)
  186. (* FIX: This procedure can not be declared exclusive, because it will be overridden by an exclusive procedure in the actual implementation, from where it will be supercalled. This could be a good example for allowing recursive locks, or an example of where an alternative for overriding methods is needed. In this case the procedure is only called from the exclusive overridden procedure, so it is not a real problem (although it is ugly). *)
  187. PROCEDURE Finalize*;
  188. BEGIN
  189. map := NIL; size := 0; blockSize := 0
  190. END Finalize;
  191. (** Init procedure for private data of above methods only. If the above methods are not required, this procedure should not be called, and the volume fields should be initialized directly. The flags parameter defines the volume flags, the size parameter its size, and the reserved parameter says how many blocks are reserved for the system (out of disk space trap occurs when less than this amount of blocks are present). *)
  192. PROCEDURE Init*(flags: SET; size, reserved: BESTSIZE);
  193. VAR maplen: BESTSIZE;
  194. BEGIN
  195. SELF.flags := flags; SELF.size := size; SELF.reserved := reserved;
  196. IF ~(ReadOnly IN flags) THEN
  197. maplen := (size + SetSize) DIV SetSize;
  198. ASSERT( maplen = LONGINT( maplen)); (*!TODO 64 bit file access *)
  199. NEW(map, LONGINT( maplen));
  200. WHILE maplen > 0 DO DEC(maplen); map[maplen] := {} END;
  201. INCL(map[0], 0); (* reserve sector 0 (illegal to use) *)
  202. used := 0
  203. ELSE
  204. used := size
  205. END
  206. END Init;
  207. END Volume;
  208. TYPE
  209. FileSystem* = OBJECT (** shareable *)
  210. VAR
  211. next: FileSystem; (* list of known file systems *)
  212. prefix*: Prefix; (** mount prefix *)
  213. desc*: ARRAY 32 OF CHAR; (** description of file system *)
  214. vol*: Volume; (** underlying volume, if any (a boot FS must have a volume) *)
  215. flags*: SET; (* flags like propagate prefix / can process files without prefix *)
  216. (** Create a new file with the specified name. End users use Files.New instead. *)
  217. PROCEDURE New0*(name: ARRAY OF CHAR): File;
  218. BEGIN HALT(301) END New0; (* abstract *)
  219. (** Open an existing file. The same file descriptor is returned if a file is opened multiple times. End users use Files.Old instead. *)
  220. PROCEDURE Old0*(name: ARRAY OF CHAR): File;
  221. BEGIN HALT(301) END Old0; (* abstract *)
  222. (** Delete a file. res = 0 indicates success. End users use Files.Delete instead. *)
  223. PROCEDURE Delete0*(name: ARRAY OF CHAR; VAR key, res: LONGINT);
  224. BEGIN HALT(301) END Delete0; (* abstract *)
  225. (** Rename a file. res = 0 indicates success. End users use Files.Rename instead. *)
  226. PROCEDURE Rename0*(old, new: ARRAY OF CHAR; f: File; VAR res: LONGINT);
  227. BEGIN HALT(301) END Rename0; (* abstract *)
  228. (** Enumerate canonical file names. mask may contain * wildcards. For internal use only. End users use Enumerator instead. *)
  229. PROCEDURE Enumerate0*(mask: ARRAY OF CHAR; flags: SET; enum: Enumerator);
  230. BEGIN HALT(301) END Enumerate0; (* abstract *)
  231. (** Return the unique non-zero key of the named file, if it exists. *)
  232. PROCEDURE FileKey*(name: ARRAY OF CHAR): LONGINT;
  233. BEGIN HALT(301) END FileKey; (* abstract *)
  234. (** Create a new directory structure. May not be supported by the actual implementation.
  235. End users use Files.CreateDirectory instead.*)
  236. PROCEDURE CreateDirectory0*(name: ARRAY OF CHAR; VAR res: LONGINT);
  237. BEGIN res := -1 (* not supported *)
  238. END CreateDirectory0;
  239. (** Remove a directory. If force=TRUE, any subdirectories and files should be automatically deleted.
  240. End users use Files.RemoveDirectory instead. *)
  241. PROCEDURE RemoveDirectory0*(name: ARRAY OF CHAR; force: BOOLEAN; VAR key, res: LONGINT);
  242. BEGIN res := -1 (* not supported *)
  243. END RemoveDirectory0;
  244. (** Finalize the file system. *)
  245. PROCEDURE Finalize*;
  246. BEGIN (* see note in Volume.Finalize *)
  247. vol := NIL
  248. END Finalize;
  249. (* default implementation using an enumerator *)
  250. PROCEDURE Has*(CONST name: ARRAY OF CHAR; VAR fullName: ARRAY OF CHAR; VAR flags: SET): BOOLEAN;
  251. VAR enum: Enumerator; time, date: LONGINT; size: BESTSIZE;
  252. BEGIN
  253. NEW(enum);
  254. enum.Open(name,{});
  255. IF enum.HasMoreEntries() & enum.GetEntry(fullName, flags, time, date, size) THEN
  256. RETURN TRUE
  257. ELSE
  258. RETURN FALSE
  259. END;
  260. END Has;
  261. (* GC
  262. PROCEDURE Purge*(f: File); (* race! *)
  263. BEGIN HALT(301) END Purge; (* by default not allowed to purge files *)
  264. *)
  265. END FileSystem;
  266. FileSystemTable* = POINTER TO ARRAY OF FileSystem;
  267. TYPE
  268. File* = OBJECT (** sharable *)
  269. VAR
  270. (** private fields for implementors *)
  271. flags*: SET; (** (read-only!) file-specific flags, i.e. Directory. *)
  272. key*: LONGINT; (* unique id for registered file, never 0 *)
  273. fs*: FileSystem; (* file system containing file *)
  274. (** Position a Rider at a certain position in a file. Multiple Riders can be positioned at different locations in a file. A Rider cannot be positioned beyond the end of a file. *)
  275. PROCEDURE Set*(VAR r: Rider; pos: BESTSIZE);
  276. BEGIN HALT(301) END Set; (* abstract *)
  277. (** Return the offset of a Rider positioned on a file. *)
  278. PROCEDURE Pos*(VAR r: Rider): BESTSIZE;
  279. BEGIN HALT(301) END Pos; (* abstract *)
  280. (** Read a byte from a file, advancing the Rider one byte further. R.eof indicates if the end of the file has been passed. *)
  281. PROCEDURE Read*(VAR r: Rider; VAR x: CHAR);
  282. BEGIN HALT(301) END Read; (* abstract *)
  283. (** Read a sequence of len bytes into the buffer x at offset ofs, advancing the Rider. Less bytes will be read when reading over the end of the file. r.res indicates the number of unread bytes. x must be big enough to hold all the bytes. *)
  284. PROCEDURE ReadBytes*(VAR r: Rider; VAR x: ARRAY OF CHAR; ofs, len: LONGINT);
  285. BEGIN HALT(301) END ReadBytes; (* abstract *)
  286. (** Write a byte into the file at the Rider position, advancing the Rider by one. *)
  287. PROCEDURE Write*(VAR r: Rider; x: CHAR);
  288. BEGIN HALT(301) END Write; (* abstract *)
  289. (** Write the buffer x containing len bytes (starting at offset ofs) into a file at the Rider position. *)
  290. PROCEDURE WriteBytes*(VAR r: Rider; CONST x: ARRAY OF CHAR; ofs, len: LONGINT);
  291. BEGIN HALT(301) END WriteBytes; (* abstract *)
  292. (** Return the current length of a file. *)
  293. PROCEDURE Length*(): BESTSIZE;
  294. BEGIN HALT(301) END Length; (* abstract *)
  295. (** Return the time (t) and date (d) when a file was last modified. *)
  296. PROCEDURE GetDate*(VAR t, d: LONGINT);
  297. BEGIN HALT(301) END GetDate; (* abstract *)
  298. (** Set the modification time (t) and date (d) of a file. *)
  299. PROCEDURE SetDate*(t, d: LONGINT);
  300. BEGIN HALT(301) END SetDate; (* abstract *)
  301. (** Get file attributes. *)
  302. PROCEDURE GetAttributes*(): SET;
  303. BEGIN HALT(301) END GetAttributes; (* abstract *)
  304. (** Set file attributes. *)
  305. PROCEDURE SetAttributes*(flags: SET);
  306. BEGIN HALT(301) END SetAttributes; (* abstract *)
  307. (** Return the canonical name of a file. *)
  308. PROCEDURE GetName*(VAR name: ARRAY OF CHAR);
  309. BEGIN HALT(301) END GetName; (* abstract *)
  310. (** Register a file created with New in the directory, replacing the previous file in the directory with the same name. The file is automatically updated. End users use Files.Register instead. *)
  311. PROCEDURE Register0*(VAR res: LONGINT);
  312. BEGIN HALT(301) END Register0; (* abstract *)
  313. (** Flush the changes made to a file from its buffers. Register0 will automatically update a file. *)
  314. PROCEDURE Update*;
  315. BEGIN HALT(301) END Update; (* abstract *)
  316. (*
  317. Usually, in Oberon a file is not closed explicitly but by a call to finalizers by the garbage colloector.
  318. However, for systems running in a host environment such as WinAos or UnixAos, explicitly closing a file may release files/handles that otherwise are blocked by the system.
  319. Close is not necessarily implemented by a file system, therefore a call to Close may or may not have any effect.
  320. Implementers of Close have to make sure that a call to close does not possibly contradict with file finalization.
  321. *)
  322. PROCEDURE Close*;
  323. BEGIN
  324. (* abstract, potentially empty *)
  325. END Close;
  326. END File;
  327. TYPE
  328. Enumerator* = OBJECT (** not shareable *)
  329. VAR
  330. r: Rider; (* data is stored in an anonymous file, because it is potentially very long *)
  331. adding: BOOLEAN; (* prevent user calls of PutEntry *)
  332. size-: LONGINT; (** total number of entries *)
  333. (** Open an enumerator and enumerate the files that match mask. *)
  334. PROCEDURE Open*(mask: ARRAY OF CHAR; flags: SET);
  335. BEGIN
  336. r.file := New("");
  337. r.file.Set(r, 0);
  338. size := 0;
  339. adding := TRUE;
  340. Enumerate(mask, flags, SELF);
  341. adding := FALSE;
  342. r.file.Set(r, 0)
  343. END Open;
  344. (** reset the enumerator to the first entry *)
  345. PROCEDURE Reset*;
  346. BEGIN
  347. r.file.Set(r, 0)
  348. END Reset;
  349. (** returns TRUE if the enumerator contains more entries *)
  350. PROCEDURE HasMoreEntries*(): BOOLEAN;
  351. BEGIN
  352. RETURN r.file.Pos(r) < r.file.Length()
  353. END HasMoreEntries;
  354. (** Get one entry from the enumerator. *)
  355. PROCEDURE GetEntry*(VAR name: ARRAY OF CHAR; VAR flags: SET; VAR time, date: LONGINT; size: BESTSIZE): BOOLEAN;
  356. VAR len, temp: LONGINT;
  357. BEGIN
  358. ReadNum(r, len);
  359. IF ~r.eof THEN
  360. name[len] := 0X; (* index check *)
  361. r.file.ReadBytes(r, name, 0, len);
  362. ReadSet(r, flags); ReadNum(r, time); ReadNum(r, date);
  363. ReadBESTSIZE(r, size);
  364. ASSERT(~r.eof)
  365. END;
  366. RETURN ~r.eof
  367. END GetEntry;
  368. (** Close the enumerator. *)
  369. PROCEDURE Close*;
  370. BEGIN
  371. (*r.fs.Purge(r.file);*)
  372. r.hint := NIL; r.file := NIL; r.fs := NIL
  373. END Close;
  374. (** For internal use only. *)
  375. PROCEDURE PutEntry*(VAR name: ARRAY OF CHAR; flags: SET; time, date: LONGINT; size:BESTSIZE);
  376. VAR len: LONGINT;
  377. BEGIN
  378. ASSERT(adding);
  379. INC(SELF.size);
  380. len := 0; WHILE name[len] # 0X DO INC(len) END;
  381. WriteNum(r, len); r.file.WriteBytes(r, name, 0, len);
  382. WriteSet(r, flags); WriteNum(r, time); WriteNum(r, date); WriteBESTSIZE(r, size)
  383. END PutEntry;
  384. END Enumerator;
  385. TYPE
  386. (* FinalizedCollection enumerator searching for a file by (fs,key). *)
  387. FileSearcher = OBJECT
  388. VAR fs: FileSystem; key: LONGINT; found: File;
  389. PROCEDURE EnumFile(f: ANY; VAR cont: BOOLEAN);
  390. BEGIN
  391. WITH f: File DO
  392. IF (f.fs = fs) & (f.key = key) THEN
  393. found := f; cont := FALSE
  394. END
  395. END
  396. END EnumFile;
  397. END FileSearcher;
  398. TYPE
  399. (** Parameters passed to volume and file system generator commands. The str field contains a generic parameter string from the mount command. The vol field returns the new volume from volume generators and passes the volume parameter to file system generators. The prefix field contains the mount prefix, mainly for file system generators to add themselves with Files.Add. *)
  400. Parameters* = OBJECT(Commands.Context)
  401. VAR
  402. vol*: Volume; (** out parameter of volume generators and in parameter of file system generators. *)
  403. prefix*: Prefix;
  404. END Parameters;
  405. FileSystemFactory* = PROCEDURE(context : Parameters);
  406. VAR
  407. fsroot: FileSystem; (* list of known file systems *)
  408. files: Kernel.FinalizedCollection; (* all open files - cleaned up by GC *)
  409. seacher: FileSearcher; (* enumerator shared by various procedures, protected with module EXCLUSIVE *)
  410. fileClipboard : File; (* contains the pointer to the file in the clipboard opened with Copy *)
  411. (** Buffered reading and writing. *)
  412. (** Open a reader on a file at the specified position. *)
  413. PROCEDURE OpenReader*(VAR b: Reader; f: File; pos: BESTSIZE);
  414. BEGIN
  415. IF b = NIL THEN
  416. NEW(b, f, pos)
  417. ELSE
  418. b.InitFileReader(f,pos)
  419. END;
  420. END OpenReader;
  421. (** Open a writer on a file at the specified position. Remember to call Streams.Update before registering or closing the file! *)
  422. PROCEDURE OpenWriter*(VAR b: Writer; f: File; pos: BESTSIZE);
  423. BEGIN
  424. NEW(b, f, pos)
  425. END OpenWriter;
  426. (** File name prefix support. *)
  427. (** Split fullname = ( prefix ":" name ) into prefix and name *)
  428. PROCEDURE SplitName*(fullname: ARRAY OF CHAR; VAR prefix, name: ARRAY OF CHAR);
  429. VAR i, j, len: LONGINT;
  430. BEGIN
  431. i := 0; WHILE (fullname[i] # ":") & (fullname[i] # 0X) DO INC(i) END;
  432. IF (fullname[i] # ":") OR (i >= LEN(prefix)) THEN
  433. COPY("", prefix); COPY(fullname, name);
  434. ELSE
  435. j := 0; WHILE j # i DO prefix[j] := fullname[j]; INC(j) END;
  436. prefix[j] := 0X;
  437. j := 0; INC(i); len := LEN(name)-1;
  438. WHILE (j < len) & (fullname[i] # 0X) DO name[j] := fullname[i]; INC(j); INC(i) END;
  439. name[j] := 0X
  440. END
  441. END SplitName;
  442. (** Join prefix and name to fullname = ( prefix ":" name ) *)
  443. PROCEDURE JoinName*(prefix, name: ARRAY OF CHAR; VAR fullname: ARRAY OF CHAR);
  444. VAR i, j, len: LONGINT;
  445. BEGIN
  446. len := LEN(fullname)-1;
  447. i := 0; WHILE (i < len) & (prefix[i] # 0X) DO fullname[i] := prefix[i]; INC(i) END;
  448. IF (i < len) THEN fullname[i] := ":"; INC(i) END;
  449. j := 0; WHILE (i < len) & (name[j] # 0X) DO fullname[i] := name[j]; INC(i); INC(j) END;
  450. fullname[i] := 0X
  451. END JoinName;
  452. (** Split a pathname at the last PathDelimiter or ":" into path and filename = ( {path (PathDelimiter|":")} filename ) *)
  453. PROCEDURE SplitPath*(pathname: ARRAY OF CHAR; VAR path, name: ARRAY OF CHAR);
  454. VAR i,j,len: LONGINT;
  455. BEGIN
  456. i := 0; j := -1;
  457. WHILE pathname[i] # 0X DO
  458. IF (pathname[i] = PathDelimiter) OR (pathname[i] = ":") THEN j := i END;
  459. INC(i)
  460. END;
  461. i := 0; len := LEN(path)-1;
  462. WHILE (i < len) & (i < j) DO path[i] := pathname[i]; INC(i) END; path[i] := 0X;
  463. IF pathname[i] = ":" THEN path[i] := ":"; path[i+1] := 0X; END; (* fof for correct splitting of filenames such as c:test into c: and test*)
  464. INC(j); i := 0; len := LEN(name)-1;
  465. WHILE (i < len) & (pathname[j] # 0X) DO name[i] := pathname[j]; INC(i); INC(j) END;
  466. name[i] := 0X
  467. END SplitPath;
  468. (** Join path and file name = ( path PathDelimiter name ) *)
  469. PROCEDURE JoinPath*(path, name: ARRAY OF CHAR; VAR pathname: ARRAY OF CHAR);
  470. VAR i,j,len: LONGINT;
  471. BEGIN
  472. len := LEN(pathname)-1;
  473. i := 0; WHILE (i < len) & (path[i] # 0X) DO pathname[i] := path[i]; INC(i) END;
  474. IF ((i = 0) OR (pathname[i-1] # PathDelimiter)) & (i < len) THEN pathname[i] := PathDelimiter; INC(i) END;
  475. j := 0; WHILE (i < len) & (name[j] # 0X) DO pathname[i] := name[j]; INC(i); INC(j) END;
  476. pathname[i] := 0X
  477. END JoinPath;
  478. (** Split a filename at the last '.' into name and extension = ( name "." extension ) *)
  479. PROCEDURE SplitExtension*(filename: ARRAY OF CHAR; VAR name, extension: ARRAY OF CHAR);
  480. VAR i,j,len: LONGINT;
  481. BEGIN
  482. i := 0; j := -1;
  483. WHILE filename[i] # 0X DO
  484. IF filename[i] = "." THEN j := i END;
  485. INC(i)
  486. END;
  487. IF (j = -1) THEN (* no extension *)
  488. COPY(filename, name); COPY("", extension)
  489. ELSE
  490. i := 0; len := LEN(name)-1;
  491. WHILE (i < len) & (i < j) DO name[i] := filename[i]; INC(i) END; name[i] := 0X;
  492. INC(j); i := 0; len := LEN(extension)-1;
  493. WHILE (i < len) & (filename[j] # 0X) DO extension[i] := filename[j]; INC(i); INC(j) END;
  494. extension[i] := 0X
  495. END
  496. END SplitExtension;
  497. (** Join name and extension = ( name "." extension ) *)
  498. PROCEDURE JoinExtension*(name, extension: ARRAY OF CHAR; VAR filename: ARRAY OF CHAR);
  499. VAR i,j,len: LONGINT;
  500. BEGIN
  501. len := LEN(filename)-1;
  502. i := 0; WHILE (i < len) & (name[i] # 0X) DO filename[i] := name[i]; INC(i) END;
  503. IF ((i = 0) OR (filename[i-1] # ".")) & (i < len) THEN filename[i] := "."; INC(i) END;
  504. j := 0; IF extension[0] = "." THEN INC(j) END;
  505. WHILE (i < len) & (extension[j] # 0X) DO filename[i] := extension[j]; INC(i); INC(j) END;
  506. filename[i] := 0X
  507. END JoinExtension;
  508. (** Append the path delimiter to path if path does not contain one *)
  509. PROCEDURE ForceTrailingDelimiter*(VAR path: ARRAY OF CHAR);
  510. VAR i: LONGINT;
  511. BEGIN
  512. i := 0; WHILE path[i] # 0X DO INC(i) END;
  513. IF (i = 0) OR (path[i-1] # PathDelimiter) THEN
  514. path[i] := PathDelimiter;
  515. path[i+1] := 0X
  516. END
  517. END ForceTrailingDelimiter;
  518. (** File system list support. *)
  519. PROCEDURE WriteFS(fs: FileSystem);
  520. BEGIN
  521. IF Trace THEN
  522. IF fs.vol # NIL THEN KernelLog.String(fs.vol.name); KernelLog.Char(" ") END;
  523. KernelLog.String(fs.desc)
  524. END
  525. END WriteFS;
  526. (** Add file system at end of list, with specified prefix, which must be unique. *)
  527. PROCEDURE Add*(fs: FileSystem; prefix: ARRAY OF CHAR);
  528. VAR p, c: FileSystem;
  529. BEGIN {EXCLUSIVE}
  530. IF Trace THEN
  531. KernelLog.Enter; KernelLog.String("Files: Adding "); WriteFS(fs); KernelLog.Exit
  532. END;
  533. COPY(prefix, fs.prefix);
  534. p := NIL; c := fsroot;
  535. WHILE c # NIL DO
  536. ASSERT((c # fs) & (c.prefix # fs.prefix)); (* duplicate insertion not allowed *)
  537. p := c; c := c.next
  538. END;
  539. IF p = NIL THEN fsroot := fs ELSE p.next := fs END;
  540. fs.next := NIL
  541. END Add;
  542. PROCEDURE DeleteFS(fs: FileSystem);
  543. VAR p, c: FileSystem;
  544. BEGIN
  545. p := NIL; c := fsroot;
  546. WHILE c # fs DO p := c; c := c.next END; (* fs must be in list *)
  547. IF p = NIL THEN fsroot := c.next ELSE p.next := c.next END;
  548. c.next := NIL
  549. END DeleteFS;
  550. (** Promote fs to the start of the list. *)
  551. PROCEDURE Promote*(fs: FileSystem);
  552. BEGIN {EXCLUSIVE}
  553. DeleteFS(fs);
  554. fs.next := fsroot; fsroot := fs
  555. END Promote;
  556. (** Remove the file system and finalize it. *)
  557. PROCEDURE Remove*(fs: FileSystem);
  558. VAR
  559. enum: OBJECT
  560. VAR count: LONGINT; fs: FileSystem;
  561. PROCEDURE EnumFile(f: ANY; VAR cont: BOOLEAN);
  562. BEGIN
  563. WITH f: File DO
  564. IF f.fs = fs THEN INC(count); f.Update(); f.fs := NIL END
  565. (* if Update procedure calls back to this module deadlock can result *)
  566. END
  567. END EnumFile;
  568. END;
  569. BEGIN {EXCLUSIVE}
  570. IF Trace THEN
  571. KernelLog.Enter; KernelLog.String("Files: Removing "); WriteFS(fs); KernelLog.Exit
  572. END;
  573. NEW(enum); enum.count := 0; enum.fs := fs;
  574. files.Enumerate(enum.EnumFile);
  575. IF enum.count # 0 THEN
  576. KernelLog.Enter; KernelLog.String("Files: "); KernelLog.Int(enum.count, 1);
  577. KernelLog.String(" open files");
  578. IF fs.vol # NIL THEN
  579. KernelLog.String(" on "); KernelLog.String(fs.vol.name)
  580. END;
  581. KernelLog.Exit
  582. END;
  583. fs.Finalize(); (* potential deadlock *)
  584. DeleteFS(fs)
  585. END Remove;
  586. (* Find the file system with specified prefix. *)
  587. PROCEDURE FindFS(prefix: ARRAY OF CHAR): FileSystem;
  588. VAR fs: FileSystem;
  589. BEGIN
  590. fs := fsroot; WHILE (fs # NIL) & (fs.prefix # prefix) DO fs := fs.next END;
  591. RETURN fs
  592. END FindFS;
  593. (** Find file system with specified prefix. *)
  594. PROCEDURE This*(prefix: ARRAY OF CHAR): FileSystem;
  595. BEGIN {EXCLUSIVE}
  596. RETURN FindFS(prefix)
  597. END This;
  598. (** Get a list of file systems. *)
  599. PROCEDURE GetList*(VAR list: FileSystemTable);
  600. VAR fs: FileSystem; n, i: LONGINT;
  601. BEGIN {EXCLUSIVE}
  602. fs := fsroot; n := 0;
  603. WHILE (fs # NIL) DO fs := fs.next; INC(n) END;
  604. IF n # 0 THEN
  605. NEW(list, n);
  606. fs := fsroot;
  607. FOR i := 0 TO n-1 DO
  608. list[i] := fs; fs := fs.next
  609. END
  610. ELSE
  611. list := NIL
  612. END
  613. END GetList;
  614. (* GC
  615. PROCEDURE Collect(f: ANY);
  616. BEGIN
  617. WITH f: File DO
  618. IF (f.fs # NIL) & (f.fs.vol # NIL) & ~(ReadOnly IN f.fs.vol.flags) THEN
  619. IF ~f.fs.Registered(f) THEN f.fs.Purge(f) END
  620. END
  621. END
  622. END Collect;
  623. *)
  624. (* Find file in open file list, or open and add it. *)
  625. PROCEDURE OpenOld(enum: FileSearcher; fs: FileSystem; VAR fname: ARRAY OF CHAR): File;
  626. VAR f: File; key: LONGINT;
  627. BEGIN
  628. f := NIL;
  629. IF (fs # NIL) & (fname # "") THEN
  630. key := fs.FileKey(fname);
  631. IF key # 0 THEN f := FindOpenFile(enum, fs, key) END;
  632. IF f = NIL THEN (* not found *)
  633. f := fs.Old0(fname);
  634. IF f # NIL THEN
  635. ASSERT(f.key # 0); (* key must be set *)
  636. files.Add(f, NIL);
  637. (* GC
  638. Heaps.RegisterFinalizer(f, Collect); (* to do: use one finalizer for ordering *)
  639. *)
  640. END
  641. END
  642. END;
  643. RETURN f
  644. END OpenOld;
  645. (** Open an existing file, searching through the mounted file system list if no prefix is specified. *)
  646. PROCEDURE Old*(name: ARRAY OF CHAR): File;
  647. VAR fs: FileSystem; f: File; prefix: Prefix; fname: FileName;
  648. BEGIN {EXCLUSIVE}
  649. f := NIL;
  650. SplitName(name, prefix, fname);
  651. IF prefix = "" THEN
  652. fs := fsroot;
  653. WHILE (fs # NIL) & (f = NIL) DO
  654. IF ~(NeedsPrefix IN fs.flags) THEN (* fof *)
  655. f := OpenOld(seacher, fs, fname);
  656. END;
  657. fs := fs.next
  658. END
  659. ELSE
  660. f := OpenOld(seacher, FindFS(prefix), fname)
  661. END;
  662. RETURN f
  663. END Old;
  664. (** Create a new file. If no prefix is specified, create the file on the first file system in the mounted list. *)
  665. PROCEDURE New*(name: ARRAY OF CHAR): File;
  666. VAR fs: FileSystem; f: File; prefix: Prefix; fname: FileName;
  667. BEGIN {EXCLUSIVE}
  668. f := NIL; SplitName(name, prefix, fname);
  669. IF prefix = "" THEN
  670. fs := fsroot; (* use default file system *)
  671. IF fname = "" THEN (* anonymous file on unspecified file system *)
  672. WHILE (fs # NIL) & ((fs.vol = NIL) OR (fs.vol.flags * {Boot,ReadOnly} # {Boot})) DO
  673. fs := fs.next (* find a writable boot file system *)
  674. END;
  675. IF fs = NIL THEN fs := fsroot END (* none found, relapse to default *)
  676. END
  677. ELSE
  678. fs := FindFS(prefix);
  679. END;
  680. IF fs # NIL THEN
  681. IF (fs.vol = NIL) OR ~(ReadOnly IN fs.vol.flags) THEN
  682. f := fs.New0(fname);
  683. (* GC
  684. IF f # NIL THEN
  685. Heaps.RegisterFinalizer(f, Collect)
  686. END
  687. *)
  688. END
  689. END;
  690. RETURN f
  691. END New;
  692. (** Delete a file. res = 0 indicates success. *)
  693. PROCEDURE Delete*(VAR name: ARRAY OF CHAR; VAR res: LONGINT);
  694. VAR fs: FileSystem; f: File; key: LONGINT; prefix: Prefix; fname: FileName;
  695. BEGIN {EXCLUSIVE}
  696. SplitName(name, prefix, fname);
  697. IF prefix = "" THEN fs := fsroot ELSE fs := FindFS(prefix) END;
  698. IF fs # NIL THEN
  699. IF (fs.vol = NIL) OR ~(ReadOnly IN fs.vol.flags) THEN
  700. fs.Delete0(fname, key, res);
  701. IF key # 0 THEN
  702. LOOP (* remove all occurances of file (fs,key) from collection. *)
  703. f := FindOpenFile(seacher, fs, key);
  704. IF f = NIL THEN EXIT END;
  705. files.Remove(f)
  706. END
  707. END
  708. ELSE
  709. res := VolumeReadOnly (* can not modify read-only volume *)
  710. END
  711. ELSE
  712. res := FsNotFound (* file system not found *)
  713. END
  714. END Delete;
  715. (* copies the file with the given name to the fileClipboard *)
  716. PROCEDURE Copy*(name: ARRAY OF CHAR; VAR res: LONGINT);
  717. VAR file: File;
  718. BEGIN
  719. res := -1;
  720. file := Old(name);
  721. IF file = NIL THEN RETURN END;
  722. fileClipboard := file;
  723. res := 0;
  724. END Copy;
  725. (* pastes the fileClipboard into the file with the given name if it doesn't exist already *)
  726. PROCEDURE Paste*(name: ARRAY OF CHAR; VAR res: LONGINT);
  727. VAR writer : Writer;
  728. reader : Reader;
  729. file : File;
  730. chunk : ARRAY 4096 OF CHAR;
  731. len : LONGINT;
  732. BEGIN
  733. IF fileClipboard = NIL THEN RETURN END;
  734. IF Old(name) # NIL THEN res := FileAlreadyExists; (* File already exists *)
  735. ELSE
  736. file := New(name);
  737. IF file = NIL THEN res := BadFileName; RETURN END; (* Bad Filename *)
  738. NEW(writer, file, 0);
  739. NEW(reader, fileClipboard, 0);
  740. WHILE (reader.res = Streams.Ok) DO
  741. reader.Bytes(chunk, 0, LEN(chunk), len);
  742. writer.Bytes(chunk, 0, len);
  743. END;
  744. writer.Update;
  745. Register(file);
  746. res := 0;
  747. END;
  748. END Paste;
  749. (**
  750. * Make a copy of a file
  751. * @param source: Prefix, path and name of file to be copied
  752. * @param destination: Prefix, path and name of file to be created
  753. * @param overwrite: @in: Overwrite existing files? @out: Has an existing file been overwritten?
  754. * @param res: @out: Result code
  755. *)
  756. PROCEDURE CopyFile*(source, destination : ARRAY OF CHAR; VAR overwrite : BOOLEAN; VAR res : LONGINT);
  757. VAR
  758. sprefix, dprefix : Prefix;
  759. sname, dname, dpath, dfilename: FileName;
  760. sPos, dPos : Rider;
  761. sfs, dfs : FileSystem;
  762. sfile, dfile : File;
  763. buffer : ARRAY BufferSize OF CHAR;
  764. i : BESTSIZE;
  765. BEGIN
  766. SplitName(source, sprefix, sname);
  767. SplitName(destination, dprefix, dname);
  768. BEGIN {EXCLUSIVE}
  769. IF sprefix = "" THEN sfs := fsroot; ELSE sfs := FindFS(sprefix); END;
  770. IF dprefix = "" THEN dfs := fsroot; ELSE dfs := FindFS(dprefix); END;
  771. IF (sfs # NIL) & (dfs # NIL) THEN (* We found the file system *)
  772. IF (dfs.vol = NIL) OR ~(ReadOnly IN dfs.vol.flags) THEN (* We may write to the target volume *)
  773. sfile := OpenOld(seacher, sfs, sname);
  774. IF sfile # NIL THEN (* We found the source file *)
  775. SplitName(dname, dpath, dfilename);
  776. IF (dfilename # "") THEN
  777. dfile := OpenOld(seacher, dfs, dname);
  778. IF (dfile = NIL) OR overwrite THEN (* We may write to the target file *)
  779. IF dfile # NIL THEN overwrite := TRUE; ELSE overwrite := FALSE; END;
  780. IF overwrite THEN
  781. dfile.GetName(dname);
  782. SplitName(dname, dprefix, dname);
  783. END;
  784. dfile := dfs.New0(dname);
  785. IF dfile # NIL THEN (* We could create the target file *)
  786. res := Ok;
  787. ELSE res := BadFileName;
  788. END;
  789. ELSE res := FileAlreadyExists;
  790. END;
  791. ELSE res := BadFileName;
  792. END;
  793. ELSE res := FileNotFound;
  794. END;
  795. ELSE res :=VolumeReadOnly;
  796. END;
  797. ELSE res := FsNotFound;
  798. END;
  799. END;
  800. IF res # Ok THEN RETURN END;
  801. (* copy file content *)
  802. sfile.Set(sPos, 0); dfile.Set(dPos, 0);
  803. i := 0;
  804. WHILE i < (sfile.Length() DIV BufferSize) DO
  805. sfile.ReadBytes(sPos, buffer, 0, BufferSize);
  806. dfile.WriteBytes(dPos, buffer, 0, BufferSize);
  807. INC(i);
  808. END;
  809. sfile.ReadBytes(sPos, buffer, 0, LONGINT( sfile.Length() MOD BufferSize));
  810. dfile.WriteBytes(dPos, buffer, 0, LONGINT(sfile.Length() MOD BufferSize));
  811. dfile.Update;
  812. Register(dfile); (* Will enter exclusive region *)
  813. END CopyFile;
  814. (** Rename a file. res = 0 indicates success. *)
  815. PROCEDURE Rename*(CONST old, new: ARRAY OF CHAR; VAR res: LONGINT);
  816. VAR
  817. key: LONGINT; ofs, nfs: FileSystem; f: File; pold, pnew: Prefix;
  818. fold, fnew: FileName;
  819. BEGIN {EXCLUSIVE}
  820. SplitName(old, pold, fold);
  821. SplitName(new, pnew, fnew);
  822. IF pold = "" THEN ofs := fsroot ELSE ofs := FindFS(pold) END;
  823. IF pnew = "" THEN nfs := fsroot ELSE nfs := FindFS(pnew) END;
  824. IF (nfs # NIL) & (ofs = nfs) THEN
  825. IF (nfs.vol = NIL) OR ~(ReadOnly IN nfs.vol.flags) THEN
  826. key := nfs.FileKey(fold);
  827. IF key # 0 THEN f := FindOpenFile(seacher, nfs, key) ELSE f := NIL END;
  828. nfs.Rename0(fold, fnew, f, res)
  829. ELSE
  830. res := VolumeReadOnly (* can not modify read-only volume *)
  831. END
  832. ELSE
  833. res := FsNotFound (* file system not found *)
  834. END
  835. END Rename;
  836. (** Register a file created with New in the directory, replacing the previous file in the directory with the same name. The file is automatically closed. *)
  837. PROCEDURE Register*(f: File);
  838. VAR res: LONGINT;
  839. BEGIN {EXCLUSIVE}
  840. IF f # NIL THEN
  841. f.Register0(res);
  842. IF res = 0 THEN (* if register went ok (first time register) *)
  843. ASSERT(f.key # 0);
  844. files.Add(f, NIL)
  845. END
  846. END
  847. END Register;
  848. (** Create a directory structure. Directories are automatically registered. res=0 indicates success.
  849. Use Files.RemoveDirectory to delete a directory *)
  850. PROCEDURE CreateDirectory*(path: ARRAY OF CHAR; VAR res: LONGINT);
  851. VAR prefix: Prefix; fs: FileSystem; fpath: FileName;
  852. BEGIN {EXCLUSIVE}
  853. SplitName(path, prefix, fpath);
  854. IF prefix = "" THEN fs := fsroot
  855. ELSE fs := FindFS(prefix)
  856. END;
  857. IF fs # NIL THEN fs.CreateDirectory0(fpath, res)
  858. ELSE res := -1
  859. END
  860. END CreateDirectory;
  861. (** Remove a directory. res=0 indicates success. If force=TRUE, any files and subdirectories are automatically deleted. *)
  862. PROCEDURE RemoveDirectory*(path: ARRAY OF CHAR; force: BOOLEAN; VAR res: LONGINT);
  863. VAR prefix: Prefix; fs: FileSystem; f: File; key: LONGINT; fpath: FileName;
  864. BEGIN {EXCLUSIVE}
  865. SplitName(path, prefix, fpath);
  866. IF prefix = "" THEN fs := fsroot ELSE fs := FindFS(prefix) END;
  867. IF fs # NIL THEN
  868. IF (fs.vol = NIL) OR ~(ReadOnly IN fs.vol.flags) THEN
  869. fs.RemoveDirectory0(fpath, force, key, res);
  870. IF key # 0 THEN
  871. LOOP (* remove all aoccurances of file (fs,key) from collection. *)
  872. f := FindOpenFile(seacher, fs, key);
  873. IF f = NIL THEN EXIT END;
  874. files.Remove(f)
  875. END
  876. END
  877. ELSE
  878. res := VolumeReadOnly (* can not modify read-only volume *)
  879. END
  880. ELSE
  881. res := FsNotFound (* file system not found *)
  882. END
  883. END RemoveDirectory;
  884. (* Enumerates files matching mask *)
  885. PROCEDURE Enumerate(VAR mask: ARRAY OF CHAR; flags: SET; enum: Enumerator);
  886. VAR
  887. fs: FileSystem; ft: FileSystemTable; i: LONGINT;
  888. prefix: Prefix; fmask: FileName;
  889. BEGIN
  890. SplitName(mask, prefix, fmask);
  891. IF prefix = "" THEN
  892. GetList(ft);
  893. IF ft # NIL THEN
  894. (* FIX: deadlock possible if fs containing anonymous file does not allow concurrent Enumerate and Write *)
  895. FOR i := 0 TO LEN(ft^)-1 DO
  896. IF ~(NeedsPrefix IN ft[i].flags) THEN
  897. ft[i].Enumerate0(fmask, flags, enum)
  898. END;
  899. END
  900. END
  901. ELSE
  902. fs := This(prefix);
  903. IF fs # NIL THEN fs.Enumerate0(fmask, flags, enum) END
  904. END
  905. END Enumerate;
  906. PROCEDURE Exists*(CONST fileName: ARRAY OF CHAR; VAR fullName: ARRAY OF CHAR; VAR flags: SET): BOOLEAN;
  907. VAR
  908. fs: FileSystem; ft: FileSystemTable; i: LONGINT;
  909. prefix: Prefix;
  910. BEGIN
  911. IF prefix = "" THEN
  912. GetList(ft);
  913. IF ft # NIL THEN
  914. (* FIX: deadlock possible if fs containing anonymous file does not allow concurrent Enumerate and Write *)
  915. FOR i := 0 TO LEN(ft^)-1 DO
  916. IF ~(NeedsPrefix IN ft[i].flags) THEN
  917. IF ft[i].Has(fileName, fullName, flags) THEN RETURN TRUE END;
  918. END;
  919. END
  920. END
  921. ELSE
  922. fs := This(prefix);
  923. RETURN fs.Has(fileName, fullName, flags);
  924. END;
  925. RETURN FALSE;
  926. END Exists;
  927. (* add a search path to the system *)
  928. PROCEDURE AddSearchPath*(context: Commands.Context);
  929. VAR cmd: ARRAY 32 OF CHAR; msg: ARRAY 256 OF CHAR; res: LONGINT;
  930. BEGIN
  931. (* preliminary implementation until we know how to solve this generically *)
  932. IF Modules.ModuleByName("WinFS") # NIL THEN
  933. cmd := "WinFS.AddSearchPath";
  934. ELSIF Modules.ModuleByName("UnixFiles") # NIL THEN
  935. cmd := "UnixFiles.AddSearchPath";
  936. END;
  937. IF cmd # "" THEN
  938. Commands.Activate(cmd, context, {Commands.Wait}, res, msg);
  939. IF res # 0 THEN context.error.String(msg); context.error.Ln; END;
  940. END
  941. END AddSearchPath;
  942. (* Find an open file. *)
  943. PROCEDURE FindOpenFile(enum: FileSearcher; fs: FileSystem; key: LONGINT): File;
  944. BEGIN (* not exported, because of possible race condition *)
  945. enum.fs := fs; enum.key := key; enum.found := NIL;
  946. files.Enumerate(enum.EnumFile);
  947. RETURN enum.found
  948. END FindOpenFile;
  949. (** Portable routines to read the standard Oberon types. DEPRECATED, use Streams instead. *)
  950. PROCEDURE ReadSInt*(VAR r: Rider; VAR x: SHORTINT);
  951. BEGIN
  952. r.file.Read(r, SYSTEM.VAL(CHAR, x))
  953. END ReadSInt;
  954. PROCEDURE ReadInt*(VAR r: Rider; VAR x: INTEGER);
  955. BEGIN
  956. r.file.ReadBytes(r, SYSTEM.VAL(Bytes2, x), 0, 2)
  957. END ReadInt;
  958. PROCEDURE ReadLInt*(VAR r: Rider; VAR x: LONGINT);
  959. BEGIN
  960. r.file.ReadBytes(r, SYSTEM.VAL(Bytes4, x), 0, 4)
  961. END ReadLInt;
  962. PROCEDURE ReadBESTSIZE*(VAR r: Rider; VAR x: BESTSIZE);
  963. BEGIN
  964. r.file.ReadBytes(r, SYSTEM.VAL(Bytes8, x), 0, SIZEOF(BESTSIZE))
  965. END ReadBESTSIZE;
  966. PROCEDURE ReadSet*(VAR r: Rider; VAR x: SET);
  967. CONST Size = SIZEOF (SET);
  968. TYPE Bytes = ARRAY Size OF CHAR;
  969. BEGIN
  970. r.file.ReadBytes(r, SYSTEM.VAL(Bytes, x), 0, Size)
  971. END ReadSet;
  972. PROCEDURE ReadBool*(VAR r: Rider; VAR x: BOOLEAN);
  973. VAR ch: CHAR;
  974. BEGIN
  975. r.file.Read(r, ch); x := ch # 0X
  976. END ReadBool;
  977. PROCEDURE ReadReal*(VAR r: Rider; VAR x: REAL);
  978. BEGIN
  979. r.file.ReadBytes(r, SYSTEM.VAL(Bytes4, x), 0, 4)
  980. END ReadReal;
  981. PROCEDURE ReadLReal*(VAR r: Rider; VAR x: LONGREAL);
  982. BEGIN
  983. r.file.ReadBytes(r, SYSTEM.VAL(Bytes8, x), 0, 8)
  984. END ReadLReal;
  985. PROCEDURE ReadString*(VAR r: Rider; VAR x: ARRAY OF CHAR);
  986. VAR i: LONGINT; ch: CHAR; f: File;
  987. BEGIN
  988. i := 0; f := r.file;
  989. LOOP
  990. f.Read(r, ch); x[i] := ch; INC(i);
  991. IF ch = 0X THEN EXIT END;
  992. IF i = LEN(x) THEN
  993. x[i-1] := 0X;
  994. REPEAT f.Read(r, ch) UNTIL ch = 0X;
  995. EXIT
  996. END
  997. END
  998. END ReadString;
  999. (* Reads a number in compressed format. *)
  1000. PROCEDURE ReadNum*(VAR r: Rider; VAR x: LONGINT);
  1001. VAR ch: CHAR; n, y: LONGINT; f: File;
  1002. BEGIN
  1003. n := 0; y := 0; f := r.file;
  1004. f.Read(r, ch);
  1005. WHILE ch >= 80X DO
  1006. INC(y, LSH(LONG(ORD(ch)) - 128, n)); INC(n, 7);
  1007. f.Read(r, ch)
  1008. END;
  1009. x := ASH(LSH(LONG(ORD(ch)), 25), n-25) + y
  1010. END ReadNum;
  1011. (** Portable routines to write the standard Oberon types. DEPRECATED, used Streams instead. *)
  1012. PROCEDURE WriteSInt*(VAR r: Rider; x: SHORTINT);
  1013. BEGIN
  1014. r.file.Write(r, SYSTEM.VAL(CHAR, x))
  1015. END WriteSInt;
  1016. PROCEDURE WriteInt*(VAR r: Rider; x: INTEGER);
  1017. BEGIN
  1018. r.file.WriteBytes(r, SYSTEM.VAL(Bytes2, x), 0, 2)
  1019. END WriteInt;
  1020. PROCEDURE WriteLInt*(VAR r: Rider; x: LONGINT);
  1021. BEGIN
  1022. r.file.WriteBytes(r, SYSTEM.VAL(Bytes4, x), 0, 4)
  1023. END WriteLInt;
  1024. PROCEDURE WriteBESTSIZE*(VAR r: Rider; x: BESTSIZE);(*TODO BESTSIZE *)
  1025. BEGIN
  1026. r.file.WriteBytes(r, SYSTEM.VAL(Bytes8, x), 0, SIZEOF(BESTSIZE))
  1027. END WriteBESTSIZE;
  1028. PROCEDURE WriteSet*(VAR r: Rider; x: SET);
  1029. CONST Size = SIZEOF (SET);
  1030. TYPE Bytes = ARRAY Size OF CHAR;
  1031. BEGIN
  1032. r.file.WriteBytes(r, SYSTEM.VAL(Bytes, x), 0, Size)
  1033. END WriteSet;
  1034. PROCEDURE WriteBool*(VAR r: Rider; x: BOOLEAN);
  1035. BEGIN
  1036. IF x THEN r.file.Write(r, 1X) ELSE r.file.Write(r, 0X) END
  1037. END WriteBool;
  1038. PROCEDURE WriteReal*(VAR r: Rider; x: REAL);
  1039. BEGIN
  1040. r.file.WriteBytes(r, SYSTEM.VAL(Bytes4, x), 0, 4)
  1041. END WriteReal;
  1042. PROCEDURE WriteLReal*(VAR r: Rider; x: LONGREAL);
  1043. BEGIN
  1044. r.file.WriteBytes(r, SYSTEM.VAL(Bytes8, x), 0, 8)
  1045. END WriteLReal;
  1046. PROCEDURE WriteString*(VAR r: Rider; x: ARRAY OF CHAR);
  1047. VAR i: LONGINT;
  1048. BEGIN
  1049. i := 0; WHILE x[i] # 0X DO INC(i) END;
  1050. r.file.WriteBytes(r, x, 0, i+1)
  1051. END WriteString;
  1052. (* Writes a number in a compressed format. *)
  1053. PROCEDURE WriteNum*(VAR r: Rider; x: LONGINT);
  1054. VAR f: File;
  1055. BEGIN
  1056. f := r.file;
  1057. WHILE (x < - 64) OR (x > 63) DO
  1058. f.Write(r, CHR(x MOD 128 + 128)); x := x DIV 128
  1059. END;
  1060. f.Write(r, CHR(x MOD 128))
  1061. END WriteNum;
  1062. (** Help procedures. *)
  1063. (** Append first string to second string, truncating on overflow. *)
  1064. PROCEDURE AppendStr*(from: ARRAY OF CHAR; VAR to: ARRAY OF CHAR);
  1065. VAR i, j, m: LONGINT;
  1066. BEGIN
  1067. j := 0; WHILE to[j] # 0X DO INC(j) END;
  1068. m := LEN(to)-1;
  1069. i := 0; WHILE (from[i] # 0X) & (j # m) DO to[j] := from[i]; INC(i); INC(j) END;
  1070. to[j] := 0X
  1071. END AppendStr;
  1072. (** Append unsigned integer to string in ASCII format. *)
  1073. PROCEDURE AppendInt*(x: LONGINT; VAR to: ARRAY OF CHAR);
  1074. VAR i, m: LONGINT;
  1075. BEGIN
  1076. ASSERT(x >= 0);
  1077. i := 0; WHILE to[i] # 0X DO INC(i) END;
  1078. IF x # 0 THEN
  1079. m := 1000000000;
  1080. WHILE x < m DO m := m DIV 10 END;
  1081. REPEAT
  1082. to[i] := CHR(48 + (x DIV m) MOD 10); INC(i);
  1083. m := m DIV 10
  1084. UNTIL m = 0
  1085. ELSE
  1086. to[i] := "0"; INC(i)
  1087. END;
  1088. to[i] := 0X
  1089. END AppendInt;
  1090. (** Get the dev#part string from the stream *)
  1091. PROCEDURE GetDevPart*(arg : Streams.Reader; VAR deviceName : ARRAY OF CHAR; VAR partition : LONGINT);
  1092. VAR i : LONGINT; ch : CHAR;
  1093. BEGIN
  1094. arg.SkipWhitespace;
  1095. deviceName := ""; partition := 0;
  1096. i := 0;
  1097. ch := arg.Peek();
  1098. WHILE (i < LEN(deviceName)-1) & (ch > " ") & (ch # "#") & (ch # ",") & (arg.res = Streams.Ok) DO
  1099. arg.Char(ch); (* consume ch *)
  1100. deviceName[i] := ch; INC(i);
  1101. ch := arg.Peek();
  1102. END;
  1103. deviceName[i] := 0X;
  1104. IF (ch = "#") THEN
  1105. arg.Char(ch); (* consume "#" *)
  1106. arg.Int(partition, FALSE);
  1107. ELSE
  1108. partition := 0;
  1109. END;
  1110. END GetDevPart;
  1111. (* Clean up file systems when shutting down or unloading module. *)
  1112. PROCEDURE FSCleanup;
  1113. VAR ft: FileSystemTable; i: LONGINT;
  1114. BEGIN
  1115. GetList(ft);
  1116. IF ft # NIL THEN
  1117. FOR i := 0 TO LEN(ft^)-1 DO Remove(ft[i]) END
  1118. END
  1119. END FSCleanup;
  1120. (* debugging *)
  1121. (*
  1122. PROCEDURE ShowList*;
  1123. VAR
  1124. enum: OBJECT
  1125. VAR i: LONGINT;
  1126. PROCEDURE EnumFile(f: ANY; VAR cont: BOOLEAN);
  1127. VAR name: FileName;
  1128. BEGIN
  1129. WITH f: File DO
  1130. KernelLog.Int(i, 1); KernelLog.Char(" ");
  1131. (*KernelLog.String(f.fs.prefix); KernelLog.Char(" ");*)
  1132. KernelLog.Address(SYSTEM.VAL(ADDRESS, f)); KernelLog.Char(" ");
  1133. KernelLog.Int(f.key, 1); KernelLog.Char(" ");
  1134. KernelLog.Int(f.Length(), 1); KernelLog.Char(" ");
  1135. f.GetName(name);
  1136. KernelLog.String(name); KernelLog.Ln;
  1137. INC(i)
  1138. END
  1139. END EnumFile;
  1140. END;
  1141. BEGIN
  1142. NEW(enum); enum.i := 0; KernelLog.Ln;
  1143. files.Enumerate(enum.EnumFile)
  1144. END ShowList;
  1145. *)
  1146. BEGIN
  1147. fsroot := NIL; NEW(seacher); NEW(files);
  1148. Modules.InstallTermHandler(FSCleanup)
  1149. END Files64.
  1150. (**
  1151. Notes:
  1152. o A typical code pattern for reading a file is:
  1153. VAR f: Files.File; r: Files.Reader; ch: CHAR;
  1154. f := Files.Old(filename); (* open an existing file *)
  1155. IF f # NIL THEN
  1156. Files.OpenReader(r, f, 0); (* open a buffer on the file *)
  1157. LOOP
  1158. ch := r.Get(); (* read a character from the buffer *)
  1159. IF r.res # Streams.Ok THEN EXIT END; (* end-of-file, or other error *)
  1160. "do something with ch"
  1161. END
  1162. END
  1163. o A typical code pattern for writing a file is:
  1164. VAR f: Files.File; w: Files.Writer; ch: CHAR;
  1165. f := Files.New(filename); (* create a new file (not visible yet) *)
  1166. IF f # NIL THEN
  1167. Files.OpenWriter(w, f, 0); (* open a buffer on the file *)
  1168. WHILE "not done" DO
  1169. "assign ch"
  1170. w.Char(ch) (* write a character to the buffer (if the buffer is full, it is written to the file) *)
  1171. END;
  1172. w.Update; (* write the last buffer to the file *)
  1173. Files.Register(f) (* enter the file in the directory *)
  1174. o See the Streams module for more procedures operating on Reader and Writer buffers, e.g. ReadRawInt, WriteRawInt, etc.
  1175. o Never use an exported identifier with a name ending in "0", unless you are implementing a file system.
  1176. o Never use an exported identifier that is documented as "private".
  1177. o File system implementations must implement the FileKey procedure to assign a unique key value to every file in the file system. The key is used by the Files module to ensure that the Old procedure returns an existing file if it is already open. The key need not be persistent, but must stay unique during a whole session (between mount and unmount). The 0 key is reserved to indicate non-existent files.
  1178. *)
  1179. (*
  1180. On-the-fly GC by bsm
  1181. In order to be non-leaking, a file system must provide the following:
  1182. - FileSystem.Purge -- to reclaim blocks of an open (being closed) file
  1183. - FileSystem.Registered -- reports if a particular open file is registered in the file directory
  1184. The following procedures need to be modified to purge file blocks when appropriate.
  1185. - FileSystem.Register0 -- if an entry to a file, F, which is not open is replaced, purge F.
  1186. - FileSystem.Rename0 -- same as register.
  1187. - FileSystem.Delete0 -- if the entry being deleted refers to a file, F, which is not open, purge F.
  1188. *)
  1189. (*
  1190. Lock order: Files, File, FileSystem
  1191. *)
  1192. Files.File
  1193. Files.File
  1194. Files.Rider
  1195. Files.Reader
  1196. Files.Writer
  1197. Files.Old
  1198. Files.Old
  1199. Files.Set
  1200. Files.OpenReader
  1201. Files.OpenWriter
  1202. Files.ReadNum
  1203. Streams.ReadRawNum
  1204. Files.ReadInt
  1205. Streams.ReadRawInt
  1206. Files.ReadLInt
  1207. Streams.ReadRawLInt
  1208. Files.ReadString
  1209. Streams.ReadRawString
  1210. Files.ReadBytes
  1211. Streams.ReadBytes [add 0 ofs parameter, and len parameter]
  1212. Files.Read(
  1213. Streams.Read(
  1214. Files.ReadBool
  1215. Streams.ReadRawBool
  1216. Files.WriteInt
  1217. Streams.WriteRawInt
  1218. Files.Write(
  1219. Streams.Write(
  1220. Files.WriteBytes
  1221. Streams.WriteBytes [add 0 ofs parameter]