Files.Mod 45 KB

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