Debian Codegraph @

gcc/m2/gm2-libs-log/FileSystem.mod

raw
1 (* FileSystem.mod provides a PIM [234] FileSystem module.
2
3 Copyright (C) 2004-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE FileSystem ;
28
29 FROM M2RTS IMPORT InstallTerminationProcedure ;
30 FROM Storage IMPORT ALLOCATE ;
31 FROM SYSTEM IMPORT ADR, COFF_T ;
32 IMPORT SFIO, libc, wrapc, StrLib ;
33
34 FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar,
35 KillString, string, Dup ;
36
37 FROM FormatStrings IMPORT Sprintf2 ;
38
39 CONST
40 TMPDIR = '/tmp' ;
41 DIRSEP = '/' ;
42 SEEK_SET = 0 ; (* seek relative to from beginning of the file *)
43
44 TYPE
45 FileList = POINTER TO RECORD
46 next : FileList ;
47 n : String ;
48 stillTemp: BOOLEAN ;
49 END ;
50
51 VAR
52 HeadOfTemp: FileList ;
53 tempNo : CARDINAL ;
54
55
56 (*
57 Create - creates a temporary file. To make the file perminant
58 the file must be renamed.
59 *)
60
61 PROCEDURE Create (VAR f: File) ;
62 BEGIN
63 WITH f DO
64 flags := FlagSet{write, temporary} ;
65 eof := FALSE ;
66 lastWord := WORD(0) ;
67 lastByte := CHAR(0) ;
68 name := MakeTemporary() ;
69 fio := SFIO.OpenToWrite(name) ;
70 IF FIO.IsNoError(fio)
71 THEN
72 res := done
73 ELSE
74 res := notdone
75 END ;
76 highpos := 0 ;
77 lowpos := 0
78 END
79 END Create ;
80
81
82 (*
83 Close - closes an open file.
84 *)
85
86 PROCEDURE Close (f: File) ;
87 BEGIN
88 WITH f DO
89 eof := TRUE ;
90 FIO.Close(fio) ;
91 IF FIO.IsNoError(fio)
92 THEN
93 res := done
94 ELSE
95 res := notdone
96 END ;
97 IF temporary IN flags
98 THEN
99 deleteFile(name, f)
100 END ;
101 name := KillString(name)
102 END
103 END Close ;
104
105
106 (*
107 Lookup - looks for a file, filename. If the file is found
108 then, f, is opened. If it is not found and, newFile,
109 is TRUE then a new file is created and attached to, f.
110 If, newFile, is FALSE and no file was found then f.res
111 is set to notdone.
112 *)
113
114 PROCEDURE Lookup (VAR f: File; filename: ARRAY OF CHAR; newFile: BOOLEAN) ;
115 BEGIN
116 WITH f DO
117 flags := FlagSet{} ;
118 IF FIO.Exists(filename)
119 THEN
120 fio := FIO.OpenToRead(filename) ;
121 INCL(flags, read) ;
122 res := done
123 ELSIF newFile
124 THEN
125 fio := FIO.OpenToWrite(filename) ;
126 INCL(flags, write) ;
127 res := done
128 ELSE
129 res := notdone
130 END ;
131 name := InitString(filename) ;
132 eof := FALSE ;
133 highpos := 0 ;
134 lowpos := 0
135 END
136 END Lookup ;
137
138
139 (*
140 Rename - rename a file and change a temporary file to a permanent
141 file. f.res is set appropriately.
142 *)
143
144 PROCEDURE Rename (VAR f: File; newname: ARRAY OF CHAR) ;
145 VAR
146 s: String ;
147 r: INTEGER ;
148 BEGIN
149 s := InitString(newname) ;
150 WITH f DO
151 r := libc.rename(string(name), string(s)) ;
152 IF r=0
153 THEN
154 res := done
155 ELSE
156 res := notdone
157 END ;
158 EXCL(flags, temporary) ;
159 name := KillString(name) ;
160 name := s
161 END
162 END Rename ;
163
164
165 (*
166 deleteFile - deletes file, name. It also kills the string, name.
167 *)
168
169 PROCEDURE deleteFile (VAR name: String; VAR f: File) ;
170 VAR
171 r: INTEGER ;
172 BEGIN
173 r := libc.unlink(string(name)) ;
174 IF r=0
175 THEN
176 f.res := done
177 ELSE
178 f.res := notdone
179 END ;
180 name := KillString(name) ;
181 name := NIL
182 END deleteFile ;
183
184
185 (*
186 Delete - deletes a file, name, and sets the f.res field.
187 f.res is set appropriately.
188 *)
189
190 PROCEDURE Delete (name: ARRAY OF CHAR; VAR f: File) ;
191 VAR
192 s: String ;
193 BEGIN
194 s := InitString(name) ;
195 deleteFile(s, f) ;
196 s := KillString(s)
197 END Delete ;
198
199
200 (*
201 ReadWord - reads a WORD, w, from file, f.
202 f.res is set appropriately.
203 *)
204
205 PROCEDURE ReadWord (VAR f: File; VAR w: WORD) ;
206 VAR
207 n: CARDINAL ;
208 BEGIN
209 WITH f DO
210 IF again IN flags
211 THEN
212 w := lastWord ;
213 EXCL(flags, again)
214 ELSE
215 ReadNBytes(f, ADR(w), SIZE(w), n) ;
216 IF n=SIZE(w)
217 THEN
218 res := done
219 ELSE
220 res := notdone ;
221 eof := TRUE
222 END
223 END
224 END
225 END ReadWord ;
226
227
228 (*
229 WriteWord - writes one word to a file, f.
230 f.res is set appropriately.
231 *)
232
233 PROCEDURE WriteWord (VAR f: File; w: WORD) ;
234 VAR
235 n: CARDINAL ;
236 BEGIN
237 WriteNBytes(f, ADR(w), SIZE(w), n) ;
238 WITH f DO
239 IF n=SIZE(w)
240 THEN
241 res := done
242 ELSE
243 res := notdone
244 END
245 END
246 END WriteWord ;
247
248
249 (*
250 ReadChar - reads one character from a file, f.
251 *)
252
253 PROCEDURE ReadChar (VAR f: File; VAR ch: CHAR) ;
254 VAR
255 n: CARDINAL ;
256 BEGIN
257 WITH f DO
258 IF again IN flags
259 THEN
260 ch := CHAR(lastByte) ;
261 EXCL(flags, again)
262 ELSE
263 ReadNBytes(f, ADR(ch), SIZE(ch), n) ;
264 IF n=SIZE(ch)
265 THEN
266 res := done ;
267 lastByte := BYTE(ch)
268 ELSE
269 res := notdone ;
270 eof := TRUE
271 END
272 END
273 END
274 END ReadChar ;
275
276
277 (*
278 WriteChar - writes a character, ch, to a file, f.
279 f.res is set appropriately.
280 *)
281
282 PROCEDURE WriteChar (VAR f: File; ch: CHAR) ;
283 VAR
284 n: CARDINAL ;
285 BEGIN
286 WriteNBytes(f, ADR(ch), SIZE(ch), n) ;
287 WITH f DO
288 IF n=SIZE(ch)
289 THEN
290 res := done
291 ELSE
292 res := notdone
293 END
294 END
295 END WriteChar ;
296
297
298 (*
299 ReadByte - reads a BYTE, b, from file, f.
300 f.res is set appropriately.
301 *)
302
303 PROCEDURE ReadByte (VAR f: File; VAR b: BYTE) ;
304 VAR
305 n: CARDINAL ;
306 BEGIN
307 WITH f DO
308 IF again IN flags
309 THEN
310 b := lastByte ;
311 EXCL(flags, again)
312 ELSE
313 ReadNBytes(f, ADR(b), SIZE(b), n) ;
314 IF n=SIZE(b)
315 THEN
316 res := done ;
317 lastByte := b
318 ELSE
319 res := notdone ;
320 eof := TRUE
321 END
322 END
323 END
324 END ReadByte ;
325
326
327 (*
328 WriteByte - writes one BYTE, b, to a file, f.
329 f.res is set appropriately.
330 *)
331
332 PROCEDURE WriteByte (VAR f: File; b: BYTE) ;
333 VAR
334 n: CARDINAL ;
335 BEGIN
336 WriteNBytes(f, ADR(b), SIZE(b), n) ;
337 WITH f DO
338 IF n=SIZE(b)
339 THEN
340 res := done
341 ELSE
342 res := notdone
343 END
344 END
345 END WriteByte ;
346
347
348 (*
349 ReadNBytes - reads a sequence of bytes from a file, f.
350 *)
351
352 PROCEDURE ReadNBytes (VAR f: File; a: ADDRESS; amount: CARDINAL;
353 VAR actuallyRead: CARDINAL) ;
354 BEGIN
355 WITH f DO
356 IF amount>0
357 THEN
358 actuallyRead := FIO.ReadNBytes(fio, amount, a) ;
359 IF FIO.IsNoError(fio)
360 THEN
361 res := done ;
362 IF MAX(CARDINAL)-lowpos<actuallyRead
363 THEN
364 INC(highpos)
365 END ;
366 INC(lowpos, actuallyRead)
367 ELSE
368 res := notdone ;
369 eof := TRUE
370 END
371 END
372 END
373 END ReadNBytes ;
374
375
376 (*
377 WriteNBytes - writes a sequence of bytes to file, f.
378 *)
379
380 PROCEDURE WriteNBytes (VAR f: File; a: ADDRESS; amount: CARDINAL;
381 VAR actuallyWritten: CARDINAL) ;
382 BEGIN
383 actuallyWritten := 0 ;
384 WITH f DO
385 IF amount>0
386 THEN
387 actuallyWritten := FIO.WriteNBytes(fio, amount, a) ;
388 IF FIO.IsNoError(fio)
389 THEN
390 res := done ;
391 IF MAX(CARDINAL)-lowpos<actuallyWritten
392 THEN
393 INC(highpos)
394 END ;
395 INC(lowpos, actuallyWritten)
396 ELSE
397 res := notdone
398 END
399 END
400 END
401 END WriteNBytes ;
402
403
404 (*
405 Again - returns the last character read to the internal buffer
406 so that it can be read again.
407 *)
408
409 PROCEDURE Again (VAR f: File) ;
410 BEGIN
411 INCL(f.flags, again)
412 END Again ;
413
414
415 (*
416 doModeChange -
417 *)
418
419 PROCEDURE doModeChange (VAR f: File; mode: Flag) ;
420 VAR
421 r: COFF_T ;
422 BEGIN
423 WITH f DO
424 IF NOT (mode IN flags)
425 THEN
426 INCL(flags, mode) ;
427 IF mode=read
428 THEN
429 EXCL(flags, write)
430 ELSIF mode=write
431 THEN
432 EXCL(flags, read)
433 END ;
434 IF opened IN flags
435 THEN
436 FIO.Close(fio)
437 END ;
438 IF read IN flags
439 THEN
440 fio := SFIO.OpenToRead(name)
441 ELSIF write IN flags
442 THEN
443 fio := SFIO.OpenToWrite(name)
444 END ;
445 INCL (flags, opened) ;
446 r := libc.lseek (fio,
447 VAL (COFF_T, lowpos) + VAL (COFF_T, highpos) * VAL (COFF_T, MAX (CARDINAL)),
448 SEEK_SET)
449 END
450 END
451 END doModeChange ;
452
453
454 (*
455 SetRead - puts the file, f, into the read state.
456 The file position is unchanged.
457 *)
458
459 PROCEDURE SetRead (VAR f: File) ;
460 BEGIN
461 doModeChange(f, read)
462 END SetRead ;
463
464
465 (*
466 SetWrite - puts the file, f, into the write state.
467 The file position is unchanged.
468 *)
469
470 PROCEDURE SetWrite (VAR f: File) ;
471 BEGIN
472 doModeChange(f, write)
473 END SetWrite ;
474
475
476 (*
477 SetModify - puts the file, f, into the modify state.
478 The file position is unchanged but the file can be
479 read and written.
480 *)
481
482 PROCEDURE SetModify (VAR f: File) ;
483 BEGIN
484 doModeChange(f, modify)
485 END SetModify ;
486
487
488 (*
489 SetOpen - places a file, f, into the open state. The file may
490 have been in the read/write/modify state before and
491 in which case the previous buffer contents are flushed
492 and the file state is reset to open. The position is
493 unaltered.
494 *)
495
496 PROCEDURE SetOpen (VAR f: File) ;
497 BEGIN
498 doModeChange(f, opened)
499 END SetOpen ;
500
501
502 (*
503 Reset - places a file, f, into the open state and reset the
504 position to the start of the file.
505 *)
506
507 PROCEDURE Reset (VAR f: File) ;
508 BEGIN
509 SetOpen(f) ;
510 SetPos(f, 0, 0)
511 END Reset ;
512
513
514 (*
515 SetPos - lseek to a position within a file.
516 *)
517
518 PROCEDURE SetPos (VAR f: File; high, low: CARDINAL) ;
519 VAR
520 r: COFF_T ;
521 BEGIN
522 WITH f DO
523 r := libc.lseek(fio, VAL(COFF_T, low) +
524 (VAL(COFF_T, MAX(CARDINAL)) * VAL(COFF_T, high)),
525 SEEK_SET) ;
526 highpos := high ;
527 lowpos := low ;
528 END
529 END SetPos ;
530
531
532 (*
533 GetPos - return the position within a file.
534 *)
535
536 PROCEDURE GetPos (VAR f: File; VAR high, low: CARDINAL) ;
537 BEGIN
538 WITH f DO
539 high := highpos ;
540 low := lowpos
541 END
542 END GetPos ;
543
544
545 (*
546 Length - returns the length of file, in, high, and, low.
547 *)
548
549 PROCEDURE Length (VAR f: File; VAR high, low: CARDINAL) ;
550 VAR
551 i: INTEGER ;
552 BEGIN
553 WITH f DO
554 i := wrapc.filesize(FIO.GetUnixFileDescriptor(fio), high, low)
555 END
556 END Length ;
557
558
559 (*
560 Doio - effectively flushes a file in write mode, rereads the
561 current buffer from disk if in read mode and writes
562 and rereads the buffer if in modify mode.
563 *)
564
565 PROCEDURE Doio (VAR f: File) ;
566 BEGIN
567 WITH f DO
568 IF opened IN flags
569 THEN
570 FIO.Close(fio) ;
571 EXCL(flags, opened)
572 END ;
573 IF read IN flags
574 THEN
575 fio := SFIO.OpenToRead(name) ;
576 INCL(flags, opened) ;
577 SetPos(f, lowpos, highpos)
578 ELSIF write IN flags
579 THEN
580 fio := SFIO.OpenToWrite(name) ;
581 INCL(flags, opened) ;
582 SetPos(f, lowpos, highpos)
583 END
584 END
585 END Doio ;
586
587
588 (*
589 FileNameChar - checks to see whether the character, ch, is
590 legal in a filename. nul is returned if the
591 character was illegal.
592 *)
593
594 PROCEDURE FileNameChar (ch: CHAR) : CHAR ;
595 BEGIN
596 RETURN ch
597 END FileNameChar ;
598
599
600 (*
601 GetFileName - return a new string containing the name of the file.
602 The string should be killed by the caller.
603 *)
604
605 PROCEDURE GetFileName (file: File) : String ;
606 BEGIN
607 RETURN Dup (file.name)
608 END GetFileName ;
609
610
611 (*
612 WriteString - writes contents to file. The nul char
613 will terminate the contents string otherwise
614 all characters 0..HIGH (contents) are written.
615 *)
616
617 PROCEDURE WriteString (file: File; contents: ARRAY OF CHAR) ;
618 VAR
619 ch : CHAR ;
620 i, high: CARDINAL ;
621 BEGIN
622 i := 0 ;
623 high := StrLib.StrLen (contents) ;
624 WHILE i <= high DO
625 WriteChar (file, contents[i]) ;
626 INC (i)
627 END
628 END WriteString ;
629
630
631 (*
632 MakeTemporary - creates a temporary file and returns its name.
633 *)
634
635 PROCEDURE MakeTemporary () : String ;
636 VAR
637 p: FileList ;
638 i: INTEGER ;
639 BEGIN
640 NEW(p) ;
641 INC(tempNo) ;
642 i := libc.getpid() ;
643 WITH p^ DO
644 next := HeadOfTemp ;
645 n := Sprintf2(InitString('fs-%d-%d'), i, tempNo) ;
646 n := ConCat(ConCatChar(InitString(TMPDIR), DIRSEP), n) ;
647 RETURN n
648 END
649 END MakeTemporary ;
650
651
652 (*
653 CleanUp - deletes all temporary files.
654 *)
655
656 PROCEDURE CleanUp ;
657 VAR
658 p: FileList ;
659 r: INTEGER ;
660 BEGIN
661 p := HeadOfTemp ;
662 WHILE p#NIL DO
663 WITH p^ DO
664 IF stillTemp
665 THEN
666 stillTemp := FALSE ;
667 r := libc.unlink(string(n))
668 END
669 END ;
670 p := p^.next
671 END
672 END CleanUp ;
673
674
675 (*
676 Init - installs the termination procedure to tidy up any temporary files.
677 *)
678
679 PROCEDURE Init ;
680 BEGIN
681 tempNo := 0 ;
682 HeadOfTemp := NIL ;
683 IF NOT InstallTerminationProcedure(CleanUp)
684 THEN
685 HALT
686 END
687 END Init ;
688
689
690 BEGIN
691 Init
692 END FileSystem.