Files.Mod 43 KB

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