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