1 # This file is part of NIT ( http://www.nitlanguage.org ).
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>
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
15 # File manipulations (create, read, write, etc.)
19 intrude import text
::ropes
27 #include <sys/types.h>
35 # `Stream` used to interact with a File or FileDescriptor
36 abstract class FileStream
38 # The path of the file.
39 var path
: nullable String = null
42 private var file
: nullable NativeFile = null
44 # The status of a file. see POSIX stat(2).
46 # var f = new FileReader.open("/etc/issue")
47 # assert f.file_stat.is_file
49 # Return null in case of error
50 fun file_stat
: nullable FileStat
52 var stat
= _file
.as(not null).file_stat
53 if stat
.address_is_null
then return null
54 return new FileStat(stat
)
57 # File descriptor of this file
58 fun fd
: Int do return _file
.as(not null).fileno
63 if file
== null then return
64 if file
.address_is_null
then
65 if last_error
!= null then return
66 last_error
= new IOError("Cannot close unopened file")
71 last_error
= new IOError("Close failed due to error {sys.errno.strerror}")
76 # Sets the buffering mode for the current FileStream
78 # If the buf_size is <= 0, its value will be 512 by default
80 # The mode is any of the buffer_mode enumeration in `Sys`:
82 # * `buffer_mode_full`
83 # * `buffer_mode_line`
84 # * `buffer_mode_none`
85 fun set_buffering_mode
(buf_size
, mode
: Int) do
86 if buf_size
<= 0 then buf_size
= 512
87 if _file
.as(not null).set_buffering_type
(buf_size
, mode
) != 0 then
88 last_error
= new IOError("Error while changing buffering type for FileStream, returned error {sys.errno.strerror}")
93 # `Stream` that can read from a File
100 # Open the same file again.
101 # The original path is reused, therefore the reopened file can be a different file.
103 # var f = new FileReader.open("/etc/issue")
104 # var l = f.read_line
106 # assert l == f.read_line
109 if not eof
and not _file
.as(not null).address_is_null
then close
111 _file
= new NativeFile.io_open_read
(path
.as(not null).to_cstring
)
112 if _file
.as(not null).address_is_null
then
113 last_error
= new IOError("Cannot open `{path.as(not null)}`: {sys.errno.strerror}")
128 redef fun fill_buffer
130 var nb
= _file
.as(not null).io_read
(_buffer
, _buffer_capacity
)
131 if last_error
== null and _file
.as(not null).ferror
then
132 last_error
= new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
144 redef var end_reached
= false
146 # Open the file at `path` for reading.
148 # var f = new FileReader.open("/etc/issue")
149 # assert not f.end_reached
152 # In case of error, `last_error` is set
154 # f = new FileReader.open("/fail/does not/exist")
155 # assert f.end_reached
156 # assert f.last_error != null
157 init open
(path
: String)
161 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
162 if _file
.as(not null).address_is_null
then
163 last_error
= new IOError("Cannot open `{path}`: {sys.errno.strerror}")
168 # Creates a new File stream from a file descriptor
170 # This is a low-level method.
171 init from_fd
(fd
: Int) do
174 _file
= fd
.fd_to_stream
(read_only
)
175 if _file
.as(not null).address_is_null
then
176 last_error
= new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
183 var res
= native_poll_in
(fd
)
185 last_error
= new IOError(errno
.to_s
)
190 private fun native_poll_in
(fd
: Int): Int `{
191 struct pollfd fds = {(int)fd, POLLIN, 0};
192 return poll(&fds, 1, 0);
196 # `Stream` that can write to a File
201 redef fun write_bytes
(s
) do
202 if last_error
!= null then return
203 if not _is_writable
then
204 last_error
= new IOError("cannot write to non-writable stream")
207 write_native
(s
.items
, 0, s
.length
)
212 if last_error
!= null then return
213 if not _is_writable
then
214 last_error
= new IOError("cannot write to non-writable stream")
217 s
.write_native_to
(self)
220 redef fun write_byte
(value
)
222 if last_error
!= null then return
223 if not _is_writable
then
224 last_error
= new IOError("Cannot write to non-writable stream")
227 if _file
.as(not null).address_is_null
then
228 last_error
= new IOError("Writing on a null stream")
233 var err
= _file
.as(not null).write_byte
(value
)
236 last_error
= new IOError("Problem writing a byte: {err}")
245 redef var is_writable
= false
247 # Write `len` bytes from `native`.
248 private fun write_native
(native
: NativeString, from
, len
: Int)
250 if last_error
!= null then return
251 if not _is_writable
then
252 last_error
= new IOError("Cannot write to non-writable stream")
255 if _file
.as(not null).address_is_null
then
256 last_error
= new IOError("Writing on a null stream")
260 var err
= _file
.as(not null).io_write
(native
, from
, len
)
263 last_error
= new IOError("Problem in writing : {err} {len} \n")
267 # Open the file at `path` for writing.
268 init open
(path
: String)
270 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
273 if _file
.as(not null).address_is_null
then
274 last_error
= new IOError("Cannot open `{path}`: {sys.errno.strerror}")
279 # Creates a new File stream from a file descriptor
280 init from_fd
(fd
: Int) do
282 _file
= fd
.fd_to_stream
(wipe_write
)
284 if _file
.as(not null).address_is_null
then
285 last_error
= new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
292 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
294 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
295 private fun fd_to_stream
(mode
: NativeString): NativeFile `{
296 return fdopen((int)self, mode);
300 # Constant for read-only file streams
301 private fun read_only
: NativeString do return once
"r".to_cstring
303 # Constant for write-only file streams
305 # If a stream is opened on a file with this method,
306 # it will wipe the previous file if any.
307 # Else, it will create the file.
308 private fun wipe_write
: NativeString do return once
"w".to_cstring
310 ###############################################################################
312 # Standard input stream.
314 # The class of the default value of `sys.stdin`.
319 _file
= new NativeFile.native_stdin
325 # Standard output stream.
327 # The class of the default value of `sys.stdout`.
331 _file
= new NativeFile.native_stdout
334 set_buffering_mode
(256, sys
.buffer_mode_line
)
338 # Standard error stream.
340 # The class of the default value of `sys.stderr`.
344 _file
= new NativeFile.native_stderr
350 ###############################################################################
353 # Like `write_to` but take care of creating the file
354 fun write_to_file
(filepath
: String)
356 var stream
= new FileWriter.open
(filepath
)
362 # Utility class to access file system services.
364 # Usually created with `Text::to_path`.
366 # `Path` objects does not necessarily represent existing files in a file system.
367 # They are sate-less objects that efficiently represent path information.
368 # They also provide an easy to use API on file-system services and are used to store their error status (see `last_error`)
371 private var path
: String
374 redef fun to_s
do return path
376 # Short name of the file at `to_s`
379 # var path = "/tmp/somefile".to_path
380 # assert path.filename == "somefile"
383 # The result does not depend of the file system, thus is cached for efficiency.
384 var filename
: String = path
.basename
is lazy
386 # The path simplified by removing useless `.`, removing `//`, and resolving `..`
389 # var path = "somedir/./tmp/../somefile".to_path
390 # assert path.simplified.to_s == "somedir/somefile"
393 # See `String:simplify_path` for details.
395 # The result does not depend of the file system, thus is cached for efficiency.
396 var simplified
: Path is lazy
do
397 var res
= path
.simplify_path
.to_path
402 # Return the directory part of the path.
405 # var path = "/foo/bar/baz".to_path
406 # assert path.dir.to_s == "/foo/bar"
407 # assert path.dir.dir.to_s == "/foo"
408 # assert path.dir.dir.dir.to_s == "/"
411 # See `String:dirname` for details.
413 # The result does not depend of the file system, thus is cached for efficiency.
414 var dir
: Path is lazy
do
415 return path
.dirname
.to_path
418 # Last error produced by I/O operations.
421 # var path = "/does/not/exists".to_path
422 # assert path.last_error == null
424 # assert path.last_error != null
427 # Since `Path` objects are stateless, `last_error` is reset on most operations and reflect its status.
428 var last_error
: nullable IOError = null is writable
430 # Does the file at `path` exists?
432 # If the file does not exists, `last_error` is set to the information.
433 fun exists
: Bool do return stat
!= null
435 # Information on the file at `self` following symbolic links
437 # Returns `null` if there is no file at `self`.
438 # `last_error` is updated to contains the error information on error, and null on success.
440 # assert "/etc/".to_path.stat.is_dir
441 # assert "/etc/issue".to_path.stat.is_file
442 # assert "/fail/does not/exist".to_path.stat == null
445 # var p = "/tmp/".to_path
447 # if stat != null then # Does `p` exist?
448 # print "It's size is {stat.size}"
449 # if stat.is_dir then print "It's a directory"
451 # print p.last_error.to_s
454 fun stat
: nullable FileStat
456 var stat
= path
.to_cstring
.file_stat
457 if stat
.address_is_null
then
458 last_error
= new IOError("Cannot open `{path}`: {sys.errno.strerror}")
462 return new FileStat(stat
)
465 # Information on the file or link at `self`
467 # Do not follow symbolic links.
468 fun link_stat
: nullable FileStat
470 var stat
= path
.to_cstring
.file_lstat
471 if stat
.address_is_null
then
472 last_error
= new IOError("Cannot open `{path}`: {sys.errno.strerror}")
476 return new FileStat(stat
)
479 # Delete a file from the file system.
481 # `last_error` is updated to contains the error information on error, and null on success.
484 var res
= path
.to_cstring
.file_delete
486 last_error
= new IOError("Cannot delete `{path}`: {sys.errno.strerror}")
492 # Copy content of file at `path` to `dest`.
494 # `last_error` is updated to contains the error information on error, and null on success.
499 var output
= dest
.open_wo
501 while not input
.eof
do
502 var buffer
= input
.read_bytes
(1024)
503 output
.write_bytes buffer
508 last_error
= input
.last_error
or else output
.last_error
511 # Open this file for reading.
514 # var file = "/etc/issue".to_path.open_ro
515 # print file.read_line
519 # Note that it is the user's responsibility to close the stream.
520 # Therefore, for simple use case, look at `read_all` or `each_line`.
522 # ENSURE `last_error == result.last_error`
523 fun open_ro
: FileReader
525 var res
= new FileReader.open
(path
)
526 last_error
= res
.last_error
530 # Open this file for writing
533 # var file = "bla.log".to_path.open_wo
534 # file.write "Blabla\n"
538 # Note that it is the user's responsibility to close the stream.
539 # Therefore, for simple use case, look at `Writable::write_to_file`.
541 # ENSURE `last_error == result.last_error`
542 fun open_wo
: FileWriter
544 var res
= new FileWriter.open
(path
)
545 last_error
= res
.last_error
549 # Read all the content of the file as a string.
552 # var content = "/etc/issue".to_path.read_all
556 # `last_error` is updated to contains the error information on error, and null on success.
557 # In case of error, the result might be empty or truncated.
559 # See `Reader::read_all` for details.
560 fun read_all
: String do return read_all_bytes
.to_s
562 # Read all the content on the file as a raw sequence of bytes.
565 # var content = "/etc/issue".to_path.read_all_bytes
569 # `last_error` is updated to contains the error information on error, and null on success.
570 # In case of error, the result might be empty or truncated.
571 fun read_all_bytes
: Bytes
574 var res
= s
.read_all_bytes
576 last_error
= s
.last_error
580 # Read all the lines of the file
583 # var lines = "/etc/passwd".to_path.read_lines
585 # print "{lines.length} users"
588 # var fields = l.split(":")
589 # print "name={fields[0]} uid={fields[2]}"
593 # `last_error` is updated to contains the error information on error, and null on success.
594 # In case of error, the result might be empty or truncated.
596 # See `Reader::read_lines` for details.
597 fun read_lines
: Array[String]
600 var res
= s
.read_lines
602 last_error
= s
.last_error
606 # Return an iterator on each line of the file
609 # for l in "/etc/passwd".to_path.each_line do
610 # var fields = l.split(":")
611 # print "name={fields[0]} uid={fields[2]}"
615 # Note: the stream is automatically closed at the end of the file (see `LineIterator::close_on_finish`)
617 # `last_error` is updated to contains the error information on error, and null on success.
619 # See `Reader::each_line` for details.
620 fun each_line
: LineIterator
623 var res
= s
.each_line
624 res
.close_on_finish
= true
625 last_error
= s
.last_error
630 # Lists the files contained within the directory at `path`.
632 # var files = "/etc".to_path.files
633 # assert files.has("/etc/issue".to_path)
635 # `last_error` is updated to contains the error information on error, and null on success.
636 # In case of error, the result might be empty or truncated.
638 # var path = "/etc/issue".to_path
640 # assert files.is_empty
641 # assert path.last_error != null
642 fun files
: Array[Path]
645 var res
= new Array[Path]
646 var d
= new NativeDir.opendir
(path
.to_cstring
)
647 if d
.address_is_null
then
648 last_error
= new IOError("Cannot list directory `{path}`: {sys.errno.strerror}")
654 if de
.address_is_null
then
655 # readdir cannot fail, so null means end of list
658 var name
= de
.to_s_with_copy
659 if name
== "." or name
== ".." then continue
660 res
.add
new Path(path
/ name
)
667 # Is `self` the path to an existing directory ?
670 # assert ".".to_path.is_dir
671 # assert not "/etc/issue".to_path.is_dir
672 # assert not "/should/not/exist".to_path.is_dir
676 if st
== null then return false
680 # Delete a directory and all of its content
682 # Does not go through symbolic links and may get stuck in a cycle if there
683 # is a cycle in the file system.
685 # `last_error` is updated to contains the error information on error, and null on success.
686 # The method does not stop on the first error and try to remove most file and directories.
689 # var path = "/does/not/exists/".to_path
691 # assert path.last_error != null
696 for file
in self.files
do
697 var stat
= file
.link_stat
699 last_error
= file
.last_error
708 if last_error
== null then last_error
= file
.last_error
711 # Delete the directory itself if things are fine
712 if last_error
== null then
713 if path
.to_cstring
.rmdir
then
714 last_error
= new IOError("Cannot remove `{self}`: {sys.errno.strerror}")
719 redef fun ==(other
) do return other
isa Path and simplified
.path
== other
.simplified
.path
720 redef fun hash
do return simplified
.path
.hash
723 # Information on a file
725 # Created by `Path::stat` and `Path::link_stat`.
727 # The information within this class is gathered when the instance is initialized
728 # it will not be updated if the targeted file is modified.
734 # The low-level status of a file
737 private var stat
: NativeFileStat
739 private var finalized
= false
743 if not finalized
then
749 # Returns the last access time in seconds since Epoch
750 fun last_access_time
: Int
756 # Returns the last access time
758 # alias for `last_access_time`
759 fun atime
: Int do return last_access_time
761 # Returns the last modification time in seconds since Epoch
762 fun last_modification_time
: Int
768 # Returns the last modification time
770 # alias for `last_modification_time`
771 fun mtime
: Int do return last_modification_time
774 # Size of the file at `path`
781 # Is self a regular file and not a device file, pipe, socket, etc.?
788 # Alias for `is_file`
789 fun is_reg
: Bool do return is_file
791 # Is this a directory?
798 # Is this a symbolic link?
805 # FIXME Make the following POSIX only? or implement in some other way on Windows
807 # Returns the last status change time in seconds since Epoch
808 fun last_status_change_time
: Int
814 # Returns the last status change time
816 # alias for `last_status_change_time`
817 fun ctime
: Int do return last_status_change_time
819 # Returns the permission bits of file
826 # Is this a character device?
833 # Is this a block device?
840 # Is this a FIFO pipe?
847 # Is this a UNIX socket
856 # Access file system related services on the path at `self`
857 fun to_path
: Path do return new Path(to_s
)
859 private fun write_native_to
(s
: FileWriter)
861 for i
in substrings
do s
.write_native
(i
.to_cstring
, 0, i
.byte_length
)
866 # return true if a file with this names exists
867 fun file_exists
: Bool do return to_cstring
.file_exists
869 # The status of a file. see POSIX stat(2).
870 fun file_stat
: nullable FileStat
872 var stat
= to_cstring
.file_stat
873 if stat
.address_is_null
then return null
874 return new FileStat(stat
)
877 # The status of a file or of a symlink. see POSIX lstat(2).
878 fun file_lstat
: nullable FileStat
880 var stat
= to_cstring
.file_lstat
881 if stat
.address_is_null
then return null
882 return new FileStat(stat
)
885 # Remove a file, return true if success
886 fun file_delete
: Bool do return to_cstring
.file_delete
888 # Copy content of file at `self` to `dest`
889 fun file_copy_to
(dest
: String) do to_path
.copy
(dest
.to_path
)
891 # Remove the trailing `extension`.
893 # `extension` usually starts with a dot but could be anything.
895 # assert "file.txt".strip_extension(".txt") == "file"
896 # assert "file.txt".strip_extension("le.txt") == "fi"
897 # assert "file.txt".strip_extension("xt") == "file.t"
899 # If `extension == null`, the rightmost extension is stripped, including the last dot.
901 # assert "file.txt".strip_extension == "file"
903 # If `extension` is not present, `self` is returned unmodified.
905 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
906 fun strip_extension
(extension
: nullable String): String
908 if extension
== null then
909 extension
= file_extension
910 if extension
== null then
912 else extension
= ".{extension}"
915 if has_suffix
(extension
) then
916 return substring
(0, length
- extension
.length
)
921 # Extract the basename of a path and strip the `extension`
923 # The extension is stripped only if `extension != null`.
925 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
926 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
927 # assert "path/to/a_file.ext".basename == "a_file.ext"
928 # assert "path/to".basename(".ext") == "to"
929 # assert "path/to/".basename(".ext") == "to"
930 # assert "path/to".basename == "to"
931 # assert "path".basename("") == "path"
932 # assert "/path".basename("") == "path"
933 # assert "/".basename("") == "/"
934 # assert "".basename("") == ""
935 fun basename
(extension
: nullable String): String
937 var l
= length
- 1 # Index of the last char
938 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
939 if l
== 0 then return "/"
940 var pos
= chars
.last_index_of_from
('/', l
)
943 n
= substring
(pos
+1, l-pos
)
946 if extension
!= null then
947 return n
.strip_extension
(extension
)
951 # Extract the dirname of a path
953 # assert "/path/to/a_file.ext".dirname == "/path/to"
954 # assert "path/to/a_file.ext".dirname == "path/to"
955 # assert "path/to".dirname == "path"
956 # assert "path/to/".dirname == "path"
957 # assert "path".dirname == "."
958 # assert "/path".dirname == "/"
959 # assert "/".dirname == "/"
960 # assert "".dirname == "."
963 var l
= length
- 1 # Index of the last char
964 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
965 var pos
= chars
.last_index_of_from
('/', l
)
967 return substring
(0, pos
)
968 else if pos
== 0 then
975 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
977 # Require: `file_exists`
978 fun realpath
: String do
979 var cs
= to_cstring
.file_realpath
981 var res
= cs
.to_s_with_copy
986 # Simplify a file path by remove useless `.`, removing `//`, and resolving `..`
988 # * `..` are not resolved if they start the path
989 # * starting `.` is simplified unless the path is empty
990 # * starting `/` is not removed
991 # * trailing `/` is removed
993 # Note that the method only work on the string:
995 # * no I/O access is performed
996 # * the validity of the path is not checked
999 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
1000 # assert "../dir/file".simplify_path == "../dir/file"
1001 # assert "dir/../../".simplify_path == ".."
1002 # assert "dir/..".simplify_path == "."
1003 # assert "//absolute//path/".simplify_path == "/absolute/path"
1004 # assert "//absolute//../".simplify_path == "/"
1005 # assert "/".simplify_path == "/"
1006 # assert "../".simplify_path == ".."
1007 # assert "./".simplify_path == "."
1008 # assert "././././././".simplify_path == "."
1009 # assert "./../dir".simplify_path == "../dir"
1010 # assert "./dir".simplify_path == "dir"
1012 fun simplify_path
: String
1014 var a
= self.split_with
("/")
1015 var a2
= new Array[String]
1017 if x
== "." and not a2
.is_empty
then continue # skip `././`
1018 if x
== "" and not a2
.is_empty
then continue # skip `//`
1019 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
1020 if a2
.last
== "." then # do not skip `./../`
1021 a2
.pop
# reduce `./../` in `../`
1022 else # reduce `dir/../` in `/`
1026 else if not a2
.is_empty
and a2
.last
== "." then
1027 a2
.pop
# reduce `./dir` in `dir`
1031 if a2
.is_empty
then return "."
1032 if a2
.length
== 1 and a2
.first
== "" then return "/"
1036 # Correctly join two path using the directory separator.
1038 # Using a standard "{self}/{path}" does not work in the following cases:
1040 # * `self` is empty.
1041 # * `path` starts with `'/'`.
1043 # This method ensures that the join is valid.
1045 # assert "hello".join_path("world") == "hello/world"
1046 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
1047 # assert "".join_path("world") == "world"
1048 # assert "hello".join_path("/world") == "/world"
1049 # assert "hello/".join_path("world") == "hello/world"
1050 # assert "hello/".join_path("/world") == "/world"
1052 # Note: You may want to use `simplify_path` on the result.
1054 # Note: This method works only with POSIX paths.
1055 fun join_path
(path
: String): String
1057 if path
.is_empty
then return self
1058 if self.is_empty
then return path
1059 if path
.chars
[0] == '/' then return path
1060 if self.last
== '/' then return "{self}{path}"
1061 return "{self}/{path}"
1064 # Convert the path (`self`) to a program name.
1066 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
1067 # used as a program name. In order to do that, prepend `./` if needed.
1069 # assert "foo".to_program_name == "./foo"
1070 # assert "/foo".to_program_name == "/foo"
1071 # assert "".to_program_name == "./" # At least, your shell will detect the error.
1072 fun to_program_name
: String do
1073 if self.has_prefix
("/") then
1080 # Alias for `join_path`
1082 # assert "hello" / "world" == "hello/world"
1083 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
1084 # assert "" / "world" == "world"
1085 # assert "/hello" / "/world" == "/world"
1087 # This operator is quite useful for chaining changes of path.
1088 # The next one being relative to the previous one.
1092 # var c = "baz/foobar"
1093 # assert a/b/c == "/bar/baz/foobar"
1094 fun /(path
: String): String do return join_path
(path
)
1096 # Returns the relative path needed to go from `self` to `dest`.
1098 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
1099 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
1101 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
1103 # In some cases, the result is still independent of the current directory:
1105 # assert "foo/bar".relpath("..") == "../../.."
1107 # In other cases, parts of the current directory may be exhibited:
1109 # var p = "../foo/bar".relpath("baz")
1110 # var c = getcwd.basename
1111 # assert p == "../../{c}/baz"
1113 # For path resolution independent of the current directory (eg. for paths in URL),
1114 # or to use an other starting directory than the current directory,
1115 # just force absolute paths:
1117 # var start = "/a/b/c/d"
1118 # var p2 = (start/"../foo/bar").relpath(start/"baz")
1119 # assert p2 == "../../d/baz"
1122 # Neither `self` or `dest` has to be real paths or to exist in directories since
1123 # the resolution is only done with string manipulations and without any access to
1124 # the underlying file system.
1126 # If `self` and `dest` are the same directory, the empty string is returned:
1128 # assert "foo".relpath("foo") == ""
1129 # assert "foo/../bar".relpath("bar") == ""
1131 # The empty string and "." designate both the current directory:
1133 # assert "".relpath("foo/bar") == "foo/bar"
1134 # assert ".".relpath("foo/bar") == "foo/bar"
1135 # assert "foo/bar".relpath("") == "../.."
1136 # assert "/" + "/".relpath(".") == getcwd
1137 fun relpath
(dest
: String): String
1140 var from
= (cwd
/self).simplify_path
.split
("/")
1141 if from
.last
.is_empty
then from
.pop
# case for the root directory
1142 var to
= (cwd
/dest
).simplify_path
.split
("/")
1143 if to
.last
.is_empty
then to
.pop
# case for the root directory
1145 # Remove common prefixes
1146 while not from
.is_empty
and not to
.is_empty
and from
.first
== to
.first
do
1151 # Result is going up in `from` with ".." then going down following `to`
1152 var from_len
= from
.length
1153 if from_len
== 0 then return to
.join
("/")
1154 var up
= "../"*(from_len-1
) + ".."
1155 if to
.is_empty
then return up
1156 var res
= up
+ "/" + to
.join
("/")
1160 # Create a directory (and all intermediate directories if needed)
1162 # The optional `mode` parameter specifies the permissions of the directory,
1163 # the default value is `0o777`.
1165 # Return an error object in case of error.
1167 # assert "/etc/".mkdir != null
1168 fun mkdir
(mode
: nullable Int): nullable Error
1170 mode
= mode
or else 0o777
1172 var dirs
= self.split_with
("/")
1173 var path
= new FlatBuffer
1174 if dirs
.is_empty
then return null
1175 if dirs
[0].is_empty
then
1176 # it was a starting /
1179 var error
: nullable Error = null
1181 if d
.is_empty
then continue
1184 var res
= path
.to_s
.to_cstring
.file_mkdir
(mode
)
1185 if not res
and error
== null then
1186 error
= new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
1192 # Delete a directory and all of its content, return `true` on success
1194 # Does not go through symbolic links and may get stuck in a cycle if there
1195 # is a cycle in the filesystem.
1197 # Return an error object in case of error.
1199 # assert "/fail/does not/exist".rmdir != null
1200 fun rmdir
: nullable Error
1207 # Change the current working directory
1210 # assert getcwd == "/etc"
1212 # assert getcwd == "/"
1214 # Return an error object in case of error.
1216 # assert "/etc".chdir == null
1217 # assert "/fail/does no/exist".chdir != null
1218 # assert getcwd == "/etc" # unchanger
1219 fun chdir
: nullable Error
1221 var res
= to_cstring
.file_chdir
1222 if res
then return null
1223 var error
= new IOError("Cannot change directory to `{self}`: {sys.errno.strerror}")
1227 # Return right-most extension (without the dot)
1229 # Only the last extension is returned.
1230 # There is no special case for combined extensions.
1232 # assert "file.txt".file_extension == "txt"
1233 # assert "file.tar.gz".file_extension == "gz"
1235 # For file without extension, `null` is returned.
1236 # Hoever, for trailing dot, `""` is returned.
1238 # assert "file".file_extension == null
1239 # assert "file.".file_extension == ""
1241 # The starting dot of hidden files is never considered.
1243 # assert ".file.txt".file_extension == "txt"
1244 # assert ".file".file_extension == null
1245 fun file_extension
: nullable String
1247 var last_slash
= chars
.last_index_of
('.')
1248 if last_slash
> 0 then
1249 return substring
( last_slash
+1, length
)
1255 # Returns entries contained within the directory represented by self.
1257 # var files = "/etc".files
1258 # assert files.has("issue")
1260 # Returns an empty array in case of error
1262 # files = "/etc/issue".files
1263 # assert files.is_empty
1265 # TODO find a better way to handle errors and to give them back to the user.
1266 fun files
: Array[String]
1268 var res
= new Array[String]
1269 var d
= new NativeDir.opendir
(to_cstring
)
1270 if d
.address_is_null
then return res
1274 if de
.address_is_null
then break
1275 var name
= de
.to_s_with_copy
1276 if name
== "." or name
== ".." then continue
1285 redef class FlatString
1286 redef fun write_native_to
(s
)
1288 s
.write_native
(items
, first_byte
, byte_length
)
1291 redef fun file_extension
do
1295 var st
= _first_byte
1296 while p
>= st
and c
!= '.'.ascii
do
1300 if p
<= st
then return null
1302 return new FlatString.with_infos
(its
, ls
- p
, p
+ 1)
1305 redef fun basename
(extension
) do
1308 var min
= _first_byte
1310 while l
> min
and its
[l
] == sl
do l
-= 1
1311 if l
== min
then return "/"
1313 while ns
>= min
and its
[ns
] != sl
do ns
-= 1
1314 var bname
= new FlatString.with_infos
(its
, l
- ns
, ns
+ 1)
1316 return if extension
!= null then bname
.strip_extension
(extension
) else bname
1320 redef class NativeString
1321 private fun file_exists
: Bool `{
1322 FILE *hdl = fopen(self,"r");
1329 private fun file_stat
: NativeFileStat `{
1331 if(stat(self, &buff) != -1) {
1332 struct stat* stat_element;
1333 stat_element = malloc(sizeof(struct stat));
1334 return memcpy(stat_element, &buff, sizeof(struct stat));
1339 private fun file_lstat
: NativeFileStat `{
1340 struct stat* stat_element;
1342 stat_element = malloc(sizeof(struct stat));
1343 res = lstat(self, stat_element);
1344 if (res == -1) return NULL;
1345 return stat_element;
1348 private fun file_mkdir
(mode
: Int): Bool `{ return !mkdir(self, mode); `}
1350 private fun rmdir: Bool `{ return !rmdir(self); `}
1352 private fun file_delete
: Bool `{
1353 return remove(self) == 0;
1356 private fun file_chdir
: Bool `{ return !chdir(self); `}
1358 private fun file_realpath: NativeString `{ return realpath(self, NULL); `}
1361 # This class is system dependent ... must reify the vfs
1362 private extern class NativeFileStat `{ struct stat * `}
1364 # Returns the permission bits of file
1365 fun mode: Int `{ return self->st_mode; `}
1367 # Returns the last access time
1368 fun atime
: Int `{ return self->st_atime; `}
1370 # Returns the last status change time
1371 fun ctime: Int `{ return self->st_ctime; `}
1373 # Returns the last modification time
1374 fun mtime
: Int `{ return self->st_mtime; `}
1377 fun size: Int `{ return self->st_size; `}
1379 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
1380 fun is_reg
: Bool `{ return S_ISREG(self->st_mode); `}
1382 # Returns true if it is a directory
1383 fun is_dir: Bool `{ return S_ISDIR(self->st_mode); `}
1385 # Returns true if it is a character device
1386 fun is_chr
: Bool `{ return S_ISCHR(self->st_mode); `}
1388 # Returns true if it is a block device
1389 fun is_blk: Bool `{ return S_ISBLK(self->st_mode); `}
1391 # Returns true if the type is fifo
1392 fun is_fifo
: Bool `{ return S_ISFIFO(self->st_mode); `}
1394 # Returns true if the type is a link
1395 fun is_lnk: Bool `{ return S_ISLNK(self->st_mode); `}
1397 # Returns true if the type is a socket
1398 fun is_sock
: Bool `{ return S_ISSOCK(self->st_mode); `}
1401 # Instance of this class are standard FILE * pointers
1402 private extern class NativeFile `{ FILE* `}
1403 fun io_read
(buf
: NativeString, len
: Int): Int `{
1404 return fread(buf, 1, len, self);
1407 fun io_write
(buf
: NativeString, from
, len
: Int): Int `{
1408 return fwrite(buf+from, 1, len, self);
1411 fun write_byte
(value
: Byte): Int `{
1412 unsigned char b = (unsigned char)value;
1413 return fwrite(&b, 1, 1, self);
1416 fun io_close
: Int `{ return fclose(self); `}
1418 fun file_stat: NativeFileStat `{
1420 if(fstat
(fileno
(self), &buff
) != -1) {
1421 struct stat
* stat_element
;
1422 stat_element
= malloc
(sizeof
(struct stat
));
1423 return memcpy
(stat_element
, &buff
, sizeof
(struct stat
));
1428 fun ferror: Bool `{ return ferror(self); `}
1430 fun fileno
: Int `{ return fileno(self); `}
1432 # Flushes the buffer, forcing the write operation
1433 fun flush: Int `{ return fflush(self); `}
1435 # Used to specify how the buffering will be handled for the current stream.
1436 fun set_buffering_type
(buf_length
, mode
: Int): Int `{
1437 return setvbuf(self, NULL, (int)mode, buf_length);
1440 new io_open_read
(path
: NativeString) `{ return fopen(path, "r"); `}
1442 new io_open_write(path: NativeString) `{ return fopen(path, "w"); `}
1444 new native_stdin
`{ return stdin; `}
1446 new native_stdout `{ return stdout; `}
1448 new native_stderr
`{ return stderr; `}
1451 # Standard `DIR*` pointer
1452 private extern class NativeDir `{ DIR* `}
1455 new opendir
(path
: NativeString) `{ return opendir(path); `}
1458 fun closedir `{ closedir(self); `}
1460 # Read the next directory entry
1461 fun readdir
: NativeString `{
1464 if (!de) return NULL;
1472 var stdin
: PollableReader = new Stdin is protected writable, lazy
1475 var stdout
: Writer = new Stdout is protected writable, lazy
1477 # Standard output for errors
1478 var stderr
: Writer = new Stderr is protected writable, lazy
1480 # Enumeration for buffer mode full (flushes when buffer is full)
1481 fun buffer_mode_full
: Int `{ return _IOFBF; `}
1483 # Enumeration for buffer mode line (flushes when a `\n
` is encountered)
1484 fun buffer_mode_line: Int `{ return _IONBF; `}
1486 # Enumeration for buffer mode none (flushes ASAP when something is written)
1487 fun buffer_mode_none
: Int `{ return _IOLBF; `}
1489 # returns first available stream to read or write to
1490 # return null on interruption (possibly a signal)
1491 protected fun poll( streams : Sequence[FileStream] ) : nullable FileStream
1493 var in_fds = new Array[Int]
1494 var out_fds = new Array[Int]
1495 var fd_to_stream = new HashMap[Int,FileStream]
1498 if s isa FileReader then in_fds.add( fd )
1499 if s isa FileWriter then out_fds.add( fd )
1501 fd_to_stream[fd] = s
1504 var polled_fd = intern_poll( in_fds, out_fds )
1506 if polled_fd == null then
1509 return fd_to_stream[polled_fd]
1513 private fun intern_poll(in_fds: Array[Int], out_fds: Array[Int]): nullable Int
1514 import Array[Int].length, Array[Int].[], Int.as(nullable Int) `{
1515 int in_len
, out_len
, total_len
;
1516 struct pollfd
*c_fds
;
1518 int first_polled_fd
= -1;
1521 in_len
= (int
)Array_of_Int_length( in_fds
);
1522 out_len
= (int
)Array_of_Int_length( out_fds
);
1523 total_len
= in_len
+ out_len
;
1524 c_fds
= malloc
( sizeof
(struct pollfd
) * total_len
);
1527 for ( i
=0; i
<in_len
; i
++ ) {
1528 int fd
= (int
)Array_of_Int__index( in_fds
, i
);
1531 c_fds
[i
].events
= POLLIN;
1534 /* output streams
*/
1535 for ( i
=0; i
<out_len
; i
++ ) {
1536 int fd
= (int
)Array_of_Int__index( out_fds
, i
);
1539 c_fds
[i
].events
= POLLOUT;
1542 /* poll all fds
, unlimited timeout
*/
1543 result
= poll
( c_fds
, total_len
, -1 );
1546 /* analyse results
*/
1547 for ( i
=0; i
<total_len
; i
++ )
1548 if ( c_fds
[i
].revents
& c_fds
[i
].events
|| /* awaited event
*/
1549 c_fds
[i
].revents
& POLLHUP ) /* closed
*/
1551 first_polled_fd
= c_fds
[i
].fd
;
1555 return Int_as_nullable( first_polled_fd
);
1557 else if ( result
< 0 )
1558 fprintf
( stderr
, "Error in Stream:poll: %s\n", strerror
( errno
) );
1565 # Print `objects
` on the standard output (`stdout
`).
1566 fun printn(objects: Object...)
1568 sys.stdout.write(objects.plain_to_s)
1571 # Print an `object
` on the standard output (`stdout
`) and add a newline.
1572 fun print(object: Object)
1574 sys.stdout.write(object.to_s)
1575 sys.stdout.write("\n")
1578 # Print `object
` on the error output (`stderr
` or a log system)
1579 fun print_error(object: Object)
1581 sys.stderr.write object.to_s
1582 sys.stderr.write "\n"
1585 # Read a character from the standard input (`stdin
`).
1588 var c = sys.stdin.read_char
1589 if c == null then return '\1'
1593 # Read a line from the standard input (`stdin
`).
1596 return sys.stdin.read_line
1599 # Return the working (current) directory
1600 fun getcwd: String do return native_getcwd.to_s
1602 private fun native_getcwd: NativeString `{ return getcwd(NULL, 0); `}