tests: update sav because line changes in kernel
[nit.git] / lib / standard / file.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
5 # Copyright 2008 Jean-Sébastien Gélinas <calestar@gmail.com>
6 #
7 # This file is free software, which comes along with NIT. This software is
8 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
11 # is kept unaltered, and a notification of the changes is added.
12 # You are allowed to redistribute it and sell it, alone or is a part of
13 # another product.
14
15 # File manipulations (create, read, write, etc.)
16 module file
17
18 intrude import stream
19 intrude import text::ropes
20 import text
21 import time
22 import gc
23
24 in "C Header" `{
25 #include <dirent.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <poll.h>
32 #include <errno.h>
33 `}
34
35 # `Stream` used to interact with a File or FileDescriptor
36 abstract class FileStream
37 super Stream
38 # The path of the file.
39 var path: nullable String = null
40
41 # The FILE *.
42 private var file: nullable NativeFile = null
43
44 # The status of a file. see POSIX stat(2).
45 #
46 # var f = new FileReader.open("/etc/issue")
47 # assert f.file_stat.is_file
48 #
49 # Return null in case of error
50 fun file_stat: nullable FileStat
51 do
52 var stat = _file.file_stat
53 if stat.address_is_null then return null
54 return new FileStat(stat)
55 end
56
57 # File descriptor of this file
58 fun fd: Int do return _file.fileno
59
60 redef fun close
61 do
62 if _file == null then return
63 if _file.address_is_null then
64 if last_error != null then return
65 last_error = new IOError("Cannot close unopened file")
66 return
67 end
68 var i = _file.io_close
69 if i != 0 then
70 last_error = new IOError("Close failed due to error {sys.errno.strerror}")
71 end
72 _file = null
73 end
74
75 # Sets the buffering mode for the current FileStream
76 #
77 # If the buf_size is <= 0, its value will be 512 by default
78 #
79 # The mode is any of the buffer_mode enumeration in `Sys`:
80 #
81 # * `buffer_mode_full`
82 # * `buffer_mode_line`
83 # * `buffer_mode_none`
84 fun set_buffering_mode(buf_size, mode: Int) do
85 if buf_size <= 0 then buf_size = 512
86 if _file.set_buffering_type(buf_size, mode) != 0 then
87 last_error = new IOError("Error while changing buffering type for FileStream, returned error {sys.errno.strerror}")
88 end
89 end
90 end
91
92 # `Stream` that can read from a File
93 class FileReader
94 super FileStream
95 super BufferedReader
96 super PollableReader
97 # Misc
98
99 # Open the same file again.
100 # The original path is reused, therefore the reopened file can be a different file.
101 #
102 # var f = new FileReader.open("/etc/issue")
103 # var l = f.read_line
104 # f.reopen
105 # assert l == f.read_line
106 fun reopen
107 do
108 if not eof and not _file.address_is_null then close
109 last_error = null
110 _file = new NativeFile.io_open_read(path.to_cstring)
111 if _file.address_is_null then
112 last_error = new IOError("Cannot open `{path.as(not null)}`: {sys.errno.strerror}")
113 end_reached = true
114 return
115 end
116 end_reached = false
117 buffer_reset
118 end
119
120 redef fun close
121 do
122 super
123 buffer_reset
124 end_reached = true
125 end
126
127 redef fun fill_buffer
128 do
129 var nb = _file.io_read(_buffer, _buffer_capacity)
130 if last_error == null and _file.ferror then
131 last_error = new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
132 end_reached = true
133 end
134 if nb <= 0 then
135 end_reached = true
136 nb = 0
137 end
138 _buffer_length = nb
139 _buffer_pos = 0
140 end
141
142 # End of file?
143 redef var end_reached = false
144
145 # Open the file at `path` for reading.
146 #
147 # var f = new FileReader.open("/etc/issue")
148 # assert not f.end_reached
149 # f.close
150 #
151 # In case of error, `last_error` is set
152 #
153 # f = new FileReader.open("/fail/does not/exist")
154 # assert f.end_reached
155 # assert f.last_error != null
156 init open(path: String)
157 do
158 self.path = path
159 prepare_buffer(10)
160 _file = new NativeFile.io_open_read(path.to_cstring)
161 if _file.address_is_null then
162 last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
163 end_reached = true
164 end
165 end
166
167 # Creates a new File stream from a file descriptor
168 #
169 # This is a low-level method.
170 init from_fd(fd: Int) do
171 self.path = ""
172 prepare_buffer(1)
173 _file = fd.fd_to_stream(read_only)
174 if _file.address_is_null then
175 last_error = new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
176 end_reached = true
177 end
178 end
179 end
180
181 # `Stream` that can write to a File
182 class FileWriter
183 super FileStream
184 super Writer
185
186 redef fun write_bytes(s) do
187 if last_error != null then return
188 if not _is_writable then
189 last_error = new IOError("cannot write to non-writable stream")
190 return
191 end
192 write_native(s.items, 0, s.length)
193 end
194
195 redef fun write(s)
196 do
197 if last_error != null then return
198 if not _is_writable then
199 last_error = new IOError("cannot write to non-writable stream")
200 return
201 end
202 s.write_native_to(self)
203 end
204
205 redef fun write_byte(value)
206 do
207 if last_error != null then return
208 if not _is_writable then
209 last_error = new IOError("Cannot write to non-writable stream")
210 return
211 end
212 if _file.address_is_null then
213 last_error = new IOError("Writing on a null stream")
214 _is_writable = false
215 return
216 end
217
218 var err = _file.write_byte(value)
219 if err != 1 then
220 # Big problem
221 last_error = new IOError("Problem writing a byte: {err}")
222 end
223 end
224
225 redef fun close
226 do
227 super
228 _is_writable = false
229 end
230 redef var is_writable = false
231
232 # Write `len` bytes from `native`.
233 private fun write_native(native: NativeString, from, len: Int)
234 do
235 if last_error != null then return
236 if not _is_writable then
237 last_error = new IOError("Cannot write to non-writable stream")
238 return
239 end
240 if _file.address_is_null then
241 last_error = new IOError("Writing on a null stream")
242 _is_writable = false
243 return
244 end
245 var err = _file.io_write(native, from, len)
246 if err != len then
247 # Big problem
248 last_error = new IOError("Problem in writing : {err} {len} \n")
249 end
250 end
251
252 # Open the file at `path` for writing.
253 init open(path: String)
254 do
255 _file = new NativeFile.io_open_write(path.to_cstring)
256 self.path = path
257 _is_writable = true
258 if _file.address_is_null then
259 last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
260 is_writable = false
261 end
262 end
263
264 # Creates a new File stream from a file descriptor
265 init from_fd(fd: Int) do
266 self.path = ""
267 _file = fd.fd_to_stream(wipe_write)
268 _is_writable = true
269 if _file.address_is_null then
270 last_error = new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
271 _is_writable = false
272 end
273 end
274 end
275
276 redef class Int
277 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
278 #
279 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
280 private fun fd_to_stream(mode: NativeString): NativeFile `{
281 return fdopen(self, mode);
282 `}
283 end
284
285 # Constant for read-only file streams
286 private fun read_only: NativeString do return once "r".to_cstring
287
288 # Constant for write-only file streams
289 #
290 # If a stream is opened on a file with this method,
291 # it will wipe the previous file if any.
292 # Else, it will create the file.
293 private fun wipe_write: NativeString do return once "w".to_cstring
294
295 ###############################################################################
296
297 # Standard input stream.
298 #
299 # The class of the default value of `sys.stdin`.
300 class Stdin
301 super FileReader
302
303 init do
304 _file = new NativeFile.native_stdin
305 path = "/dev/stdin"
306 prepare_buffer(1)
307 end
308
309 redef fun poll_in `{
310 struct pollfd fd = {0, POLLIN, 0};
311 int res = poll(&fd, 1, 0);
312 if (res == -1) {
313 perror("Error poll stdin");
314 exit(EXIT_FAILURE);
315 }
316 return res > 0;
317 `}
318 end
319
320 # Standard output stream.
321 #
322 # The class of the default value of `sys.stdout`.
323 class Stdout
324 super FileWriter
325 init do
326 _file = new NativeFile.native_stdout
327 path = "/dev/stdout"
328 _is_writable = true
329 set_buffering_mode(256, sys.buffer_mode_line)
330 end
331 end
332
333 # Standard error stream.
334 #
335 # The class of the default value of `sys.stderr`.
336 class Stderr
337 super FileWriter
338 init do
339 _file = new NativeFile.native_stderr
340 path = "/dev/stderr"
341 _is_writable = true
342 end
343 end
344
345 ###############################################################################
346
347 redef class Writable
348 # Like `write_to` but take care of creating the file
349 fun write_to_file(filepath: String)
350 do
351 var stream = new FileWriter.open(filepath)
352 write_to(stream)
353 stream.close
354 end
355 end
356
357 # Utility class to access file system services.
358 #
359 # Usually created with `Text::to_path`.
360 #
361 # `Path` objects does not necessarily represent existing files in a file system.
362 # They are sate-less objects that efficiently represent path information.
363 # They also provide an easy to use API on file-system services and are used to store their error status (see `last_error`)
364 class Path
365
366 private var path: String
367
368 # Path to this file
369 redef fun to_s do return path
370
371 # Short name of the file at `to_s`
372 #
373 # ~~~
374 # var path = "/tmp/somefile".to_path
375 # assert path.filename == "somefile"
376 # ~~~
377 #
378 # The result does not depend of the file system, thus is cached for efficiency.
379 var filename: String = path.basename is lazy
380
381 # The path simplified by removing useless `.`, removing `//`, and resolving `..`
382 #
383 # ~~~
384 # var path = "somedir/./tmp/../somefile".to_path
385 # assert path.simplified.to_s == "somedir/somefile"
386 # ~~~
387 #
388 # See `String:simplify_path` for details.
389 #
390 # The result does not depend of the file system, thus is cached for efficiency.
391 var simplified: Path is lazy do
392 var res = path.simplify_path.to_path
393 res.simplified = res
394 return res
395 end
396
397 # Return the directory part of the path.
398 #
399 # ~~~
400 # var path = "/foo/bar/baz".to_path
401 # assert path.dir.to_s == "/foo/bar"
402 # assert path.dir.dir.to_s == "/foo"
403 # assert path.dir.dir.dir.to_s == "/"
404 # ~~~
405 #
406 # See `String:dirname` for details.
407 #
408 # The result does not depend of the file system, thus is cached for efficiency.
409 var dir: Path is lazy do
410 return path.dirname.to_path
411 end
412
413 # Last error produced by I/O operations.
414 #
415 # ~~~
416 # var path = "/does/not/exists".to_path
417 # assert path.last_error == null
418 # path.read_all
419 # assert path.last_error != null
420 # ~~~
421 #
422 # Since `Path` objects are stateless, `last_error` is reset on most operations and reflect its status.
423 var last_error: nullable IOError = null is writable
424
425 # Does the file at `path` exists?
426 #
427 # If the file does not exists, `last_error` is set to the information.
428 fun exists: Bool do return stat != null
429
430 # Information on the file at `self` following symbolic links
431 #
432 # Returns `null` if there is no file at `self`.
433 # `last_error` is updated to contains the error information on error, and null on success.
434 #
435 # assert "/etc/".to_path.stat.is_dir
436 # assert "/etc/issue".to_path.stat.is_file
437 # assert "/fail/does not/exist".to_path.stat == null
438 #
439 # ~~~
440 # var p = "/tmp/".to_path
441 # var stat = p.stat
442 # if stat != null then # Does `p` exist?
443 # print "It's size is {stat.size}"
444 # if stat.is_dir then print "It's a directory"
445 # else
446 # print p.last_error.to_s
447 # end
448 # ~~~
449 fun stat: nullable FileStat
450 do
451 var stat = path.to_cstring.file_stat
452 if stat.address_is_null then
453 last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
454 return null
455 end
456 last_error = null
457 return new FileStat(stat)
458 end
459
460 # Information on the file or link at `self`
461 #
462 # Do not follow symbolic links.
463 fun link_stat: nullable FileStat
464 do
465 var stat = path.to_cstring.file_lstat
466 if stat.address_is_null then
467 last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
468 return null
469 end
470 last_error = null
471 return new FileStat(stat)
472 end
473
474 # Delete a file from the file system.
475 #
476 # `last_error` is updated to contains the error information on error, and null on success.
477 fun delete
478 do
479 var res = path.to_cstring.file_delete
480 if not res then
481 last_error = new IOError("Cannot delete `{path}`: {sys.errno.strerror}")
482 else
483 last_error = null
484 end
485 end
486
487 # Copy content of file at `path` to `dest`.
488 #
489 # `last_error` is updated to contains the error information on error, and null on success.
490 fun copy(dest: Path)
491 do
492 last_error = null
493 var input = open_ro
494 var output = dest.open_wo
495
496 while not input.eof do
497 var buffer = input.read(1024)
498 output.write buffer
499 end
500
501 input.close
502 output.close
503 last_error = input.last_error or else output.last_error
504 end
505
506 # Open this file for reading.
507 #
508 # ~~~
509 # var file = "/etc/issue".to_path.open_ro
510 # print file.read_line
511 # file.close
512 # ~~~
513 #
514 # Note that it is the user's responsibility to close the stream.
515 # Therefore, for simple use case, look at `read_all` or `each_line`.
516 #
517 # ENSURE `last_error == result.last_error`
518 fun open_ro: FileReader
519 do
520 var res = new FileReader.open(path)
521 last_error = res.last_error
522 return res
523 end
524
525 # Open this file for writing
526 #
527 # ~~~
528 # var file = "bla.log".to_path.open_wo
529 # file.write "Blabla\n"
530 # file.close
531 # ~~~
532 #
533 # Note that it is the user's responsibility to close the stream.
534 # Therefore, for simple use case, look at `Writable::write_to_file`.
535 #
536 # ENSURE `last_error == result.last_error`
537 fun open_wo: FileWriter
538 do
539 var res = new FileWriter.open(path)
540 last_error = res.last_error
541 return res
542 end
543
544 # Read all the content of the file as a string.
545 #
546 # ~~~
547 # var content = "/etc/issue".to_path.read_all
548 # print content
549 # ~~~
550 #
551 # `last_error` is updated to contains the error information on error, and null on success.
552 # In case of error, the result might be empty or truncated.
553 #
554 # See `Reader::read_all` for details.
555 fun read_all: String do return read_all_bytes.to_s
556
557 # Read all the content on the file as a raw sequence of bytes.
558 #
559 # ~~~
560 # var content = "/etc/issue".to_path.read_all_bytes
561 # print content.to_s
562 # ~~~
563 #
564 # `last_error` is updated to contains the error information on error, and null on success.
565 # In case of error, the result might be empty or truncated.
566 fun read_all_bytes: Bytes
567 do
568 var s = open_ro
569 var res = s.read_all_bytes
570 s.close
571 last_error = s.last_error
572 return res
573 end
574
575 # Read all the lines of the file
576 #
577 # ~~~
578 # var lines = "/etc/passwd".to_path.read_lines
579 #
580 # print "{lines.length} users"
581 #
582 # for l in lines do
583 # var fields = l.split(":")
584 # print "name={fields[0]} uid={fields[2]}"
585 # end
586 # ~~~
587 #
588 # `last_error` is updated to contains the error information on error, and null on success.
589 # In case of error, the result might be empty or truncated.
590 #
591 # See `Reader::read_lines` for details.
592 fun read_lines: Array[String]
593 do
594 var s = open_ro
595 var res = s.read_lines
596 s.close
597 last_error = s.last_error
598 return res
599 end
600
601 # Return an iterator on each line of the file
602 #
603 # ~~~
604 # for l in "/etc/passwd".to_path.each_line do
605 # var fields = l.split(":")
606 # print "name={fields[0]} uid={fields[2]}"
607 # end
608 # ~~~
609 #
610 # Note: the stream is automatically closed at the end of the file (see `LineIterator::close_on_finish`)
611 #
612 # `last_error` is updated to contains the error information on error, and null on success.
613 #
614 # See `Reader::each_line` for details.
615 fun each_line: LineIterator
616 do
617 var s = open_ro
618 var res = s.each_line
619 res.close_on_finish = true
620 last_error = s.last_error
621 return res
622 end
623
624
625 # Lists the files contained within the directory at `path`.
626 #
627 # var files = "/etc".to_path.files
628 # assert files.has("/etc/issue".to_path)
629 #
630 # `last_error` is updated to contains the error information on error, and null on success.
631 # In case of error, the result might be empty or truncated.
632 #
633 # var path = "/etc/issue".to_path
634 # files = path.files
635 # assert files.is_empty
636 # assert path.last_error != null
637 fun files: Array[Path]
638 do
639 last_error = null
640 var res = new Array[Path]
641 var d = new NativeDir.opendir(path.to_cstring)
642 if d.address_is_null then
643 last_error = new IOError("Cannot list directory `{path}`: {sys.errno.strerror}")
644 return res
645 end
646
647 loop
648 var de = d.readdir
649 if de.address_is_null then
650 # readdir cannot fail, so null means end of list
651 break
652 end
653 var name = de.to_s_with_copy
654 if name == "." or name == ".." then continue
655 res.add new Path(path / name)
656 end
657 d.closedir
658
659 return res
660 end
661
662 # Delete a directory and all of its content
663 #
664 # Does not go through symbolic links and may get stuck in a cycle if there
665 # is a cycle in the file system.
666 #
667 # `last_error` is updated to contains the error information on error, and null on success.
668 # The method does not stop on the first error and try to remove most file and directories.
669 #
670 # ~~~
671 # var path = "/does/not/exists/".to_path
672 # path.rmdir
673 # assert path.last_error != null
674 # ~~~
675 fun rmdir
676 do
677 last_error = null
678 for file in self.files do
679 var stat = file.link_stat
680 if stat == null then
681 last_error = file.last_error
682 continue
683 end
684 if stat.is_dir then
685 # Recursively rmdir
686 file.rmdir
687 else
688 file.delete
689 end
690 if last_error == null then last_error = file.last_error
691 end
692
693 # Delete the directory itself if things are fine
694 if last_error == null then
695 if path.to_cstring.rmdir then
696 last_error = new IOError("Cannot remove `{self}`: {sys.errno.strerror}")
697 end
698 end
699 end
700
701 redef fun ==(other) do return other isa Path and simplified.path == other.simplified.path
702 redef fun hash do return simplified.path.hash
703 end
704
705 # Information on a file
706 #
707 # Created by `Path::stat` and `Path::link_stat`.
708 #
709 # The information within this class is gathered when the instance is initialized
710 # it will not be updated if the targeted file is modified.
711 class FileStat
712 super Finalizable
713
714 # TODO private init
715
716 # The low-level status of a file
717 #
718 # See: POSIX stat(2)
719 private var stat: NativeFileStat
720
721 private var finalized = false
722
723 redef fun finalize
724 do
725 if not finalized then
726 stat.free
727 finalized = true
728 end
729 end
730
731 # Returns the last access time in seconds since Epoch
732 fun last_access_time: Int
733 do
734 assert not finalized
735 return stat.atime
736 end
737
738 # Returns the last access time
739 #
740 # alias for `last_access_time`
741 fun atime: Int do return last_access_time
742
743 # Returns the last modification time in seconds since Epoch
744 fun last_modification_time: Int
745 do
746 assert not finalized
747 return stat.mtime
748 end
749
750 # Returns the last modification time
751 #
752 # alias for `last_modification_time`
753 fun mtime: Int do return last_modification_time
754
755
756 # Size of the file at `path`
757 fun size: Int
758 do
759 assert not finalized
760 return stat.size
761 end
762
763 # Is self a regular file and not a device file, pipe, socket, etc.?
764 fun is_file: Bool
765 do
766 assert not finalized
767 return stat.is_reg
768 end
769
770 # Alias for `is_file`
771 fun is_reg: Bool do return is_file
772
773 # Is this a directory?
774 fun is_dir: Bool
775 do
776 assert not finalized
777 return stat.is_dir
778 end
779
780 # Is this a symbolic link?
781 fun is_link: Bool
782 do
783 assert not finalized
784 return stat.is_lnk
785 end
786
787 # FIXME Make the following POSIX only? or implement in some other way on Windows
788
789 # Returns the last status change time in seconds since Epoch
790 fun last_status_change_time: Int
791 do
792 assert not finalized
793 return stat.ctime
794 end
795
796 # Returns the last status change time
797 #
798 # alias for `last_status_change_time`
799 fun ctime: Int do return last_status_change_time
800
801 # Returns the permission bits of file
802 fun mode: Int
803 do
804 assert not finalized
805 return stat.mode
806 end
807
808 # Is this a character device?
809 fun is_chr: Bool
810 do
811 assert not finalized
812 return stat.is_chr
813 end
814
815 # Is this a block device?
816 fun is_blk: Bool
817 do
818 assert not finalized
819 return stat.is_blk
820 end
821
822 # Is this a FIFO pipe?
823 fun is_fifo: Bool
824 do
825 assert not finalized
826 return stat.is_fifo
827 end
828
829 # Is this a UNIX socket
830 fun is_sock: Bool
831 do
832 assert not finalized
833 return stat.is_sock
834 end
835 end
836
837 redef class Text
838 # Access file system related services on the path at `self`
839 fun to_path: Path do return new Path(to_s)
840
841 private fun write_native_to(s: FileWriter)
842 do
843 for i in substrings do s.write_native(i.to_cstring, 0, i.length)
844 end
845 end
846
847 redef class String
848 # return true if a file with this names exists
849 fun file_exists: Bool do return to_cstring.file_exists
850
851 # The status of a file. see POSIX stat(2).
852 fun file_stat: nullable FileStat
853 do
854 var stat = to_cstring.file_stat
855 if stat.address_is_null then return null
856 return new FileStat(stat)
857 end
858
859 # The status of a file or of a symlink. see POSIX lstat(2).
860 fun file_lstat: nullable FileStat
861 do
862 var stat = to_cstring.file_lstat
863 if stat.address_is_null then return null
864 return new FileStat(stat)
865 end
866
867 # Remove a file, return true if success
868 fun file_delete: Bool do return to_cstring.file_delete
869
870 # Copy content of file at `self` to `dest`
871 fun file_copy_to(dest: String) do to_path.copy(dest.to_path)
872
873 # Remove the trailing `extension`.
874 #
875 # `extension` usually starts with a dot but could be anything.
876 #
877 # assert "file.txt".strip_extension(".txt") == "file"
878 # assert "file.txt".strip_extension("le.txt") == "fi"
879 # assert "file.txt".strip_extension("xt") == "file.t"
880 #
881 # If `extension == null`, the rightmost extension is stripped, including the last dot.
882 #
883 # assert "file.txt".strip_extension == "file"
884 #
885 # If `extension` is not present, `self` is returned unmodified.
886 #
887 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
888 fun strip_extension(extension: nullable String): String
889 do
890 if extension == null then
891 extension = file_extension
892 if extension == null then
893 return self
894 else extension = ".{extension}"
895 end
896
897 if has_suffix(extension) then
898 return substring(0, length - extension.length)
899 end
900 return self
901 end
902
903 # Extract the basename of a path and strip the `extension`
904 #
905 # The extension is stripped only if `extension != null`.
906 #
907 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
908 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
909 # assert "path/to/a_file.ext".basename == "a_file.ext"
910 # assert "path/to".basename(".ext") == "to"
911 # assert "path/to/".basename(".ext") == "to"
912 # assert "path/to".basename == "to"
913 # assert "path".basename("") == "path"
914 # assert "/path".basename("") == "path"
915 # assert "/".basename("") == "/"
916 # assert "".basename("") == ""
917 fun basename(extension: nullable String): String
918 do
919 var l = length - 1 # Index of the last char
920 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
921 if l == 0 then return "/"
922 var pos = chars.last_index_of_from('/', l)
923 var n = self
924 if pos >= 0 then
925 n = substring(pos+1, l-pos)
926 end
927
928 if extension != null then
929 return n.strip_extension(extension)
930 else return n
931 end
932
933 # Extract the dirname of a path
934 #
935 # assert "/path/to/a_file.ext".dirname == "/path/to"
936 # assert "path/to/a_file.ext".dirname == "path/to"
937 # assert "path/to".dirname == "path"
938 # assert "path/to/".dirname == "path"
939 # assert "path".dirname == "."
940 # assert "/path".dirname == "/"
941 # assert "/".dirname == "/"
942 # assert "".dirname == "."
943 fun dirname: String
944 do
945 var l = length - 1 # Index of the last char
946 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
947 var pos = chars.last_index_of_from('/', l)
948 if pos > 0 then
949 return substring(0, pos)
950 else if pos == 0 then
951 return "/"
952 else
953 return "."
954 end
955 end
956
957 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
958 fun realpath: String do
959 var cs = to_cstring.file_realpath
960 var res = cs.to_s_with_copy
961 # cs.free_malloc # FIXME memory leak
962 return res
963 end
964
965 # Simplify a file path by remove useless `.`, removing `//`, and resolving `..`
966 #
967 # * `..` are not resolved if they start the path
968 # * starting `.` is simplified unless the path is empty
969 # * starting `/` is not removed
970 # * trailing `/` is removed
971 #
972 # Note that the method only work on the string:
973 #
974 # * no I/O access is performed
975 # * the validity of the path is not checked
976 #
977 # ~~~
978 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
979 # assert "../dir/file".simplify_path == "../dir/file"
980 # assert "dir/../../".simplify_path == ".."
981 # assert "dir/..".simplify_path == "."
982 # assert "//absolute//path/".simplify_path == "/absolute/path"
983 # assert "//absolute//../".simplify_path == "/"
984 # assert "/".simplify_path == "/"
985 # assert "../".simplify_path == ".."
986 # assert "./".simplify_path == "."
987 # assert "././././././".simplify_path == "."
988 # assert "./../dir".simplify_path == "../dir"
989 # assert "./dir".simplify_path == "dir"
990 # ~~~
991 fun simplify_path: String
992 do
993 var a = self.split_with("/")
994 var a2 = new Array[String]
995 for x in a do
996 if x == "." and not a2.is_empty then continue # skip `././`
997 if x == "" and not a2.is_empty then continue # skip `//`
998 if x == ".." and not a2.is_empty and a2.last != ".." then
999 if a2.last == "." then # do not skip `./../`
1000 a2.pop # reduce `./../` in `../`
1001 else # reduce `dir/../` in `/`
1002 a2.pop
1003 continue
1004 end
1005 else if not a2.is_empty and a2.last == "." then
1006 a2.pop # reduce `./dir` in `dir`
1007 end
1008 a2.push(x)
1009 end
1010 if a2.is_empty then return "."
1011 if a2.length == 1 and a2.first == "" then return "/"
1012 return a2.join("/")
1013 end
1014
1015 # Correctly join two path using the directory separator.
1016 #
1017 # Using a standard "{self}/{path}" does not work in the following cases:
1018 #
1019 # * `self` is empty.
1020 # * `path` starts with `'/'`.
1021 #
1022 # This method ensures that the join is valid.
1023 #
1024 # assert "hello".join_path("world") == "hello/world"
1025 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
1026 # assert "".join_path("world") == "world"
1027 # assert "hello".join_path("/world") == "/world"
1028 # assert "hello/".join_path("world") == "hello/world"
1029 # assert "hello/".join_path("/world") == "/world"
1030 #
1031 # Note: You may want to use `simplify_path` on the result.
1032 #
1033 # Note: This method works only with POSIX paths.
1034 fun join_path(path: String): String
1035 do
1036 if path.is_empty then return self
1037 if self.is_empty then return path
1038 if path.chars[0] == '/' then return path
1039 if self.last == '/' then return "{self}{path}"
1040 return "{self}/{path}"
1041 end
1042
1043 # Convert the path (`self`) to a program name.
1044 #
1045 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
1046 # used as a program name. In order to do that, prepend `./` if needed.
1047 #
1048 # assert "foo".to_program_name == "./foo"
1049 # assert "/foo".to_program_name == "/foo"
1050 # assert "".to_program_name == "./" # At least, your shell will detect the error.
1051 fun to_program_name: String do
1052 if self.has_prefix("/") then
1053 return self
1054 else
1055 return "./{self}"
1056 end
1057 end
1058
1059 # Alias for `join_path`
1060 #
1061 # assert "hello" / "world" == "hello/world"
1062 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
1063 # assert "" / "world" == "world"
1064 # assert "/hello" / "/world" == "/world"
1065 #
1066 # This operator is quite useful for chaining changes of path.
1067 # The next one being relative to the previous one.
1068 #
1069 # var a = "foo"
1070 # var b = "/bar"
1071 # var c = "baz/foobar"
1072 # assert a/b/c == "/bar/baz/foobar"
1073 fun /(path: String): String do return join_path(path)
1074
1075 # Returns the relative path needed to go from `self` to `dest`.
1076 #
1077 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
1078 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
1079 #
1080 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
1081 #
1082 # In some cases, the result is still independent of the current directory:
1083 #
1084 # assert "foo/bar".relpath("..") == "../../.."
1085 #
1086 # In other cases, parts of the current directory may be exhibited:
1087 #
1088 # var p = "../foo/bar".relpath("baz")
1089 # var c = getcwd.basename
1090 # assert p == "../../{c}/baz"
1091 #
1092 # For path resolution independent of the current directory (eg. for paths in URL),
1093 # or to use an other starting directory than the current directory,
1094 # just force absolute paths:
1095 #
1096 # var start = "/a/b/c/d"
1097 # var p2 = (start/"../foo/bar").relpath(start/"baz")
1098 # assert p2 == "../../d/baz"
1099 #
1100 #
1101 # Neither `self` or `dest` has to be real paths or to exist in directories since
1102 # the resolution is only done with string manipulations and without any access to
1103 # the underlying file system.
1104 #
1105 # If `self` and `dest` are the same directory, the empty string is returned:
1106 #
1107 # assert "foo".relpath("foo") == ""
1108 # assert "foo/../bar".relpath("bar") == ""
1109 #
1110 # The empty string and "." designate both the current directory:
1111 #
1112 # assert "".relpath("foo/bar") == "foo/bar"
1113 # assert ".".relpath("foo/bar") == "foo/bar"
1114 # assert "foo/bar".relpath("") == "../.."
1115 # assert "/" + "/".relpath(".") == getcwd
1116 fun relpath(dest: String): String
1117 do
1118 var cwd = getcwd
1119 var from = (cwd/self).simplify_path.split("/")
1120 if from.last.is_empty then from.pop # case for the root directory
1121 var to = (cwd/dest).simplify_path.split("/")
1122 if to.last.is_empty then to.pop # case for the root directory
1123
1124 # Remove common prefixes
1125 while not from.is_empty and not to.is_empty and from.first == to.first do
1126 from.shift
1127 to.shift
1128 end
1129
1130 # Result is going up in `from` with ".." then going down following `to`
1131 var from_len = from.length
1132 if from_len == 0 then return to.join("/")
1133 var up = "../"*(from_len-1) + ".."
1134 if to.is_empty then return up
1135 var res = up + "/" + to.join("/")
1136 return res
1137 end
1138
1139 # Create a directory (and all intermediate directories if needed)
1140 #
1141 # Return an error object in case of error.
1142 #
1143 # assert "/etc/".mkdir != null
1144 fun mkdir: nullable Error
1145 do
1146 var dirs = self.split_with("/")
1147 var path = new FlatBuffer
1148 if dirs.is_empty then return null
1149 if dirs[0].is_empty then
1150 # it was a starting /
1151 path.add('/')
1152 end
1153 var error: nullable Error = null
1154 for d in dirs do
1155 if d.is_empty then continue
1156 path.append(d)
1157 path.add('/')
1158 var res = path.to_s.to_cstring.file_mkdir
1159 if not res and error == null then
1160 error = new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
1161 end
1162 end
1163 return error
1164 end
1165
1166 # Delete a directory and all of its content, return `true` on success
1167 #
1168 # Does not go through symbolic links and may get stuck in a cycle if there
1169 # is a cycle in the filesystem.
1170 #
1171 # Return an error object in case of error.
1172 #
1173 # assert "/fail/does not/exist".rmdir != null
1174 fun rmdir: nullable Error
1175 do
1176 var p = to_path
1177 p.rmdir
1178 return p.last_error
1179 end
1180
1181 # Change the current working directory
1182 #
1183 # "/etc".chdir
1184 # assert getcwd == "/etc"
1185 # "..".chdir
1186 # assert getcwd == "/"
1187 #
1188 # Return an error object in case of error.
1189 #
1190 # assert "/etc".chdir == null
1191 # assert "/fail/does no/exist".chdir != null
1192 # assert getcwd == "/etc" # unchanger
1193 fun chdir: nullable Error
1194 do
1195 var res = to_cstring.file_chdir
1196 if res then return null
1197 var error = new IOError("Cannot change directory to `{self}`: {sys.errno.strerror}")
1198 return error
1199 end
1200
1201 # Return right-most extension (without the dot)
1202 #
1203 # Only the last extension is returned.
1204 # There is no special case for combined extensions.
1205 #
1206 # assert "file.txt".file_extension == "txt"
1207 # assert "file.tar.gz".file_extension == "gz"
1208 #
1209 # For file without extension, `null` is returned.
1210 # Hoever, for trailing dot, `""` is returned.
1211 #
1212 # assert "file".file_extension == null
1213 # assert "file.".file_extension == ""
1214 #
1215 # The starting dot of hidden files is never considered.
1216 #
1217 # assert ".file.txt".file_extension == "txt"
1218 # assert ".file".file_extension == null
1219 fun file_extension: nullable String
1220 do
1221 var last_slash = chars.last_index_of('.')
1222 if last_slash > 0 then
1223 return substring( last_slash+1, length )
1224 else
1225 return null
1226 end
1227 end
1228
1229 # Returns entries contained within the directory represented by self.
1230 #
1231 # var files = "/etc".files
1232 # assert files.has("issue")
1233 #
1234 # Returns an empty array in case of error
1235 #
1236 # files = "/etc/issue".files
1237 # assert files.is_empty
1238 #
1239 # TODO find a better way to handle errors and to give them back to the user.
1240 fun files: Array[String]
1241 do
1242 var res = new Array[String]
1243 var d = new NativeDir.opendir(to_cstring)
1244 if d.address_is_null then return res
1245
1246 loop
1247 var de = d.readdir
1248 if de.address_is_null then break
1249 var name = de.to_s_with_copy
1250 if name == "." or name == ".." then continue
1251 res.add name
1252 end
1253 d.closedir
1254
1255 return res
1256 end
1257 end
1258
1259 redef class FlatString
1260 redef fun write_native_to(s)
1261 do
1262 s.write_native(items, index_from, length)
1263 end
1264 end
1265
1266 redef class NativeString
1267 private fun file_exists: Bool `{
1268 FILE *hdl = fopen(self,"r");
1269 if(hdl != NULL){
1270 fclose(hdl);
1271 }
1272 return hdl != NULL;
1273 `}
1274
1275 private fun file_stat: NativeFileStat `{
1276 struct stat buff;
1277 if(stat(self, &buff) != -1) {
1278 struct stat* stat_element;
1279 stat_element = malloc(sizeof(struct stat));
1280 return memcpy(stat_element, &buff, sizeof(struct stat));
1281 }
1282 return 0;
1283 `}
1284
1285 private fun file_lstat: NativeFileStat `{
1286 struct stat* stat_element;
1287 int res;
1288 stat_element = malloc(sizeof(struct stat));
1289 res = lstat(self, stat_element);
1290 if (res == -1) return NULL;
1291 return stat_element;
1292 `}
1293
1294 private fun file_mkdir: Bool `{ return !mkdir(self, 0777); `}
1295
1296 private fun rmdir: Bool `{ return !rmdir(self); `}
1297
1298 private fun file_delete: Bool `{
1299 return remove(self) == 0;
1300 `}
1301
1302 private fun file_chdir: Bool `{ return !chdir(self); `}
1303
1304 private fun file_realpath: NativeString `{ return realpath(self, NULL); `}
1305 end
1306
1307 # This class is system dependent ... must reify the vfs
1308 private extern class NativeFileStat `{ struct stat * `}
1309
1310 # Returns the permission bits of file
1311 fun mode: Int `{ return self->st_mode; `}
1312
1313 # Returns the last access time
1314 fun atime: Int `{ return self->st_atime; `}
1315
1316 # Returns the last status change time
1317 fun ctime: Int `{ return self->st_ctime; `}
1318
1319 # Returns the last modification time
1320 fun mtime: Int `{ return self->st_mtime; `}
1321
1322 # Returns the size
1323 fun size: Int `{ return self->st_size; `}
1324
1325 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
1326 fun is_reg: Bool `{ return S_ISREG(self->st_mode); `}
1327
1328 # Returns true if it is a directory
1329 fun is_dir: Bool `{ return S_ISDIR(self->st_mode); `}
1330
1331 # Returns true if it is a character device
1332 fun is_chr: Bool `{ return S_ISCHR(self->st_mode); `}
1333
1334 # Returns true if it is a block device
1335 fun is_blk: Bool `{ return S_ISBLK(self->st_mode); `}
1336
1337 # Returns true if the type is fifo
1338 fun is_fifo: Bool `{ return S_ISFIFO(self->st_mode); `}
1339
1340 # Returns true if the type is a link
1341 fun is_lnk: Bool `{ return S_ISLNK(self->st_mode); `}
1342
1343 # Returns true if the type is a socket
1344 fun is_sock: Bool `{ return S_ISSOCK(self->st_mode); `}
1345 end
1346
1347 # Instance of this class are standard FILE * pointers
1348 private extern class NativeFile `{ FILE* `}
1349 fun io_read(buf: NativeString, len: Int): Int `{
1350 return fread(buf, 1, len, self);
1351 `}
1352
1353 fun io_write(buf: NativeString, from, len: Int): Int `{
1354 return fwrite(buf+from, 1, len, self);
1355 `}
1356
1357 fun write_byte(value: Byte): Int `{
1358 unsigned char b = (unsigned char)value;
1359 return fwrite(&b, 1, 1, self);
1360 `}
1361
1362 fun io_close: Int `{ return fclose(self); `}
1363
1364 fun file_stat: NativeFileStat `{
1365 struct stat buff;
1366 if(fstat(fileno(self), &buff) != -1) {
1367 struct stat* stat_element;
1368 stat_element = malloc(sizeof(struct stat));
1369 return memcpy(stat_element, &buff, sizeof(struct stat));
1370 }
1371 return 0;
1372 `}
1373
1374 fun ferror: Bool `{ return ferror(self); `}
1375
1376 fun fileno: Int `{ return fileno(self); `}
1377
1378 # Flushes the buffer, forcing the write operation
1379 fun flush: Int `{ return fflush(self); `}
1380
1381 # Used to specify how the buffering will be handled for the current stream.
1382 fun set_buffering_type(buf_length: Int, mode: Int): Int `{
1383 return setvbuf(self, NULL, mode, buf_length);
1384 `}
1385
1386 new io_open_read(path: NativeString) `{ return fopen(path, "r"); `}
1387
1388 new io_open_write(path: NativeString) `{ return fopen(path, "w"); `}
1389
1390 new native_stdin `{ return stdin; `}
1391
1392 new native_stdout `{ return stdout; `}
1393
1394 new native_stderr `{ return stderr; `}
1395 end
1396
1397 # Standard `DIR*` pointer
1398 private extern class NativeDir `{ DIR* `}
1399
1400 # Open a directory
1401 new opendir(path: NativeString) `{ return opendir(path); `}
1402
1403 # Close a directory
1404 fun closedir `{ closedir(self); `}
1405
1406 # Read the next directory entry
1407 fun readdir: NativeString `{
1408 struct dirent *de;
1409 de = readdir(self);
1410 if (!de) return NULL;
1411 return de->d_name;
1412 `}
1413 end
1414
1415 redef class Sys
1416
1417 # Standard input
1418 var stdin: PollableReader = new Stdin is protected writable, lazy
1419
1420 # Standard output
1421 var stdout: Writer = new Stdout is protected writable, lazy
1422
1423 # Standard output for errors
1424 var stderr: Writer = new Stderr is protected writable, lazy
1425
1426 # Enumeration for buffer mode full (flushes when buffer is full)
1427 fun buffer_mode_full: Int `{ return _IOFBF; `}
1428
1429 # Enumeration for buffer mode line (flushes when a `\n` is encountered)
1430 fun buffer_mode_line: Int `{ return _IONBF; `}
1431
1432 # Enumeration for buffer mode none (flushes ASAP when something is written)
1433 fun buffer_mode_none: Int `{ return _IOLBF; `}
1434
1435 # returns first available stream to read or write to
1436 # return null on interruption (possibly a signal)
1437 protected fun poll( streams : Sequence[FileStream] ) : nullable FileStream
1438 do
1439 var in_fds = new Array[Int]
1440 var out_fds = new Array[Int]
1441 var fd_to_stream = new HashMap[Int,FileStream]
1442 for s in streams do
1443 var fd = s.fd
1444 if s isa FileReader then in_fds.add( fd )
1445 if s isa FileWriter then out_fds.add( fd )
1446
1447 fd_to_stream[fd] = s
1448 end
1449
1450 var polled_fd = intern_poll( in_fds, out_fds )
1451
1452 if polled_fd == null then
1453 return null
1454 else
1455 return fd_to_stream[polled_fd]
1456 end
1457 end
1458
1459 private fun intern_poll(in_fds: Array[Int], out_fds: Array[Int]): nullable Int
1460 import Array[Int].length, Array[Int].[], Int.as(nullable Int) `{
1461 int in_len, out_len, total_len;
1462 struct pollfd *c_fds;
1463 int i;
1464 int first_polled_fd = -1;
1465 int result;
1466
1467 in_len = Array_of_Int_length( in_fds );
1468 out_len = Array_of_Int_length( out_fds );
1469 total_len = in_len + out_len;
1470 c_fds = malloc( sizeof(struct pollfd) * total_len );
1471
1472 /* input streams */
1473 for ( i=0; i<in_len; i ++ ) {
1474 int fd;
1475 fd = Array_of_Int__index( in_fds, i );
1476
1477 c_fds[i].fd = fd;
1478 c_fds[i].events = POLLIN;
1479 }
1480
1481 /* output streams */
1482 for ( i=0; i<out_len; i ++ ) {
1483 int fd;
1484 fd = Array_of_Int__index( out_fds, i );
1485
1486 c_fds[i].fd = fd;
1487 c_fds[i].events = POLLOUT;
1488 }
1489
1490 /* poll all fds, unlimited timeout */
1491 result = poll( c_fds, total_len, -1 );
1492
1493 if ( result > 0 ) {
1494 /* analyse results */
1495 for ( i=0; i<total_len; i++ )
1496 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
1497 c_fds[i].revents & POLLHUP ) /* closed */
1498 {
1499 first_polled_fd = c_fds[i].fd;
1500 break;
1501 }
1502
1503 return Int_as_nullable( first_polled_fd );
1504 }
1505 else if ( result < 0 )
1506 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
1507
1508 return null_Int();
1509 `}
1510
1511 end
1512
1513 # Print `objects` on the standard output (`stdout`).
1514 fun printn(objects: Object...)
1515 do
1516 sys.stdout.write(objects.plain_to_s)
1517 end
1518
1519 # Print an `object` on the standard output (`stdout`) and add a newline.
1520 fun print(object: Object)
1521 do
1522 sys.stdout.write(object.to_s)
1523 sys.stdout.write("\n")
1524 end
1525
1526 # Print `object` on the error output (`stderr` or a log system)
1527 fun print_error(object: Object)
1528 do
1529 sys.stderr.write object.to_s
1530 sys.stderr.write "\n"
1531 end
1532
1533 # Read a character from the standard input (`stdin`).
1534 fun getc: Char
1535 do
1536 var c = sys.stdin.read_char
1537 if c == null then return '\1'
1538 return c
1539 end
1540
1541 # Read a line from the standard input (`stdin`).
1542 fun gets: String
1543 do
1544 return sys.stdin.read_line
1545 end
1546
1547 # Return the working (current) directory
1548 fun getcwd: String do return native_getcwd.to_s
1549
1550 private fun native_getcwd: NativeString `{ return getcwd(NULL, 0); `}