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>
43 # `Stream` used to interact with a File or FileDescriptor
44 abstract class FileStream
46 # The path of the file.
47 var path
: nullable String = null
50 private var file
: nullable NativeFile = null
52 # The status of a file. see POSIX stat(2).
54 # var f = new FileReader.open("/etc/issue")
55 # assert f.file_stat.is_file
57 # Return null in case of error
58 fun file_stat
: nullable FileStat
60 var stat
= _file
.as(not null).file_stat
61 if stat
.address_is_null
then return null
62 return new FileStat(stat
)
65 # File descriptor of this file
66 fun fd
: Int do return _file
.as(not null).fileno
71 if file
== null then return
72 if file
.address_is_null
then
73 if last_error
!= null then return
74 last_error
= new IOError("Cannot close unopened file")
79 last_error
= new IOError("Close failed due to error {sys.errno.strerror}")
84 # Sets the buffering mode for the current FileStream
86 # If the buf_size is <= 0, its value will be 512 by default
88 # The mode is any of the buffer_mode enumeration in `Sys`:
90 # * `buffer_mode_full`
91 # * `buffer_mode_line`
92 # * `buffer_mode_none`
93 fun set_buffering_mode
(buf_size
, mode
: Int) do
94 if buf_size
<= 0 then buf_size
= 512
95 if _file
.as(not null).set_buffering_type
(buf_size
, mode
) != 0 then
96 last_error
= new IOError("Error while changing buffering type for FileStream, returned error {sys.errno.strerror}")
101 # `Stream` that can read from a File
107 # Open the same file again.
108 # The original path is reused, therefore the reopened file can be a different file.
110 # var f = new FileReader.open("/etc/issue")
111 # var l = f.read_line
113 # assert l == f.read_line
117 if fl
!= null and not fl
.address_is_null
then close
119 _file
= new NativeFile.io_open_read
(path
.as(not null).to_cstring
)
120 if _file
.as(not null).address_is_null
then
121 last_error
= new IOError("Cannot open `{path.as(not null)}`: {sys.errno.strerror}")
126 redef fun raw_read_byte
128 var nb
= _file
.as(not null).io_read
(write_buffer
, 1)
129 if last_error
== null and _file
.as(not null).ferror
then
130 last_error
= new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
132 if nb
== 0 then return -1
133 return write_buffer
[0].to_i
136 redef fun raw_read_bytes
(cstr
, max
)
138 var nb
= _file
.as(not null).io_read
(cstr
, max
)
139 if last_error
== null and _file
.as(not null).ferror
then
140 last_error
= new IOError("Cannot read `{path.as(not null)}`: {sys.errno.strerror}")
147 if fl
== null then return true
148 if fl
.address_is_null
then return true
149 if last_error
!= null then return true
151 if last_error
!= null then return true
157 # Open the file at `path` for reading.
159 # var f = new FileReader.open("/etc/issue")
163 # In case of error, `last_error` is set
165 # f = new FileReader.open("/fail/does not/exist")
167 # assert f.last_error != null
168 init open
(path
: String)
171 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
172 if _file
.as(not null).address_is_null
then
173 last_error
= new IOError("Cannot open `{path}`: {sys.errno.strerror}")
177 # Creates a new File stream from a file descriptor
179 # This is a low-level method.
180 init from_fd
(fd
: Int) do
182 _file
= fd
.fd_to_stream
(read_only
)
183 if _file
.as(not null).address_is_null
then
184 last_error
= new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
190 var res
= native_poll_in
(fd
)
192 last_error
= new IOError(errno
.to_s
)
197 private fun native_poll_in
(fd
: Int): Int `{
199 struct pollfd fds = {(int)fd, POLLIN, 0};
200 return poll(&fds, 1, 0);
207 # `Stream` that can write to a File
212 redef fun write_bytes_from_cstring
(cs
, len
) do
213 if last_error
!= null then return
214 if not _is_writable
then
215 last_error
= new IOError("cannot write to non-writable stream")
218 write_native
(cs
, 0, len
)
223 if last_error
!= null then return
224 if not _is_writable
then
225 last_error
= new IOError("cannot write to non-writable stream")
228 s
.write_native_to
(self)
231 redef fun write_byte
(value
)
233 if last_error
!= null then return
234 if not _is_writable
then
235 last_error
= new IOError("Cannot write to non-writable stream")
238 if _file
.as(not null).address_is_null
then
239 last_error
= new IOError("Writing on a null stream")
244 var err
= _file
.as(not null).write_byte
(value
)
247 last_error
= new IOError("Problem writing a byte: {err}")
256 redef var is_writable
= false
258 # Write `len` bytes from `native`.
259 private fun write_native
(native
: CString, from
, len
: Int)
261 if last_error
!= null then return
262 if not _is_writable
then
263 last_error
= new IOError("Cannot write to non-writable stream")
266 if _file
.as(not null).address_is_null
then
267 last_error
= new IOError("Writing on a null stream")
271 var err
= _file
.as(not null).io_write
(native
, from
, len
)
274 last_error
= new IOError("Problem in writing : {err} {len} \n")
278 # Open the file at `path` for writing.
279 init open
(path
: String)
281 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
284 if _file
.as(not null).address_is_null
then
285 last_error
= new IOError("Cannot open `{path}`: {sys.errno.strerror}")
290 # Creates a new File stream from a file descriptor
291 init from_fd
(fd
: Int) do
293 _file
= fd
.fd_to_stream
(wipe_write
)
295 if _file
.as(not null).address_is_null
then
296 last_error
= new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
303 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
305 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
306 private fun fd_to_stream
(mode
: CString): NativeFile `{
307 return fdopen((int)self, mode);
310 # Does the file descriptor `self` refer to a terminal?
311 fun isatty
: Bool `{ return isatty(self); `}
314 # Constant for read-only file streams
315 private fun read_only: CString do return once "r".to_cstring
317 # Constant for write-only file streams
319 # If a stream is opened on a file with this method,
320 # it will wipe the previous file if any.
321 # Else, it will create the file.
322 private fun wipe_write: CString do return once "w".to_cstring
324 ###############################################################################
326 # Standard input stream.
328 # The class of the default value of `sys
.stdin
`.
333 _file = new NativeFile.native_stdin
338 # Standard output stream.
340 # The class of the default value of `sys
.stdout
`.
344 _file = new NativeFile.native_stdout
347 set_buffering_mode(256, sys.buffer_mode_line)
351 # Standard error stream.
353 # The class of the default value of `sys
.stderr
`.
357 _file = new NativeFile.native_stderr
363 ###############################################################################
366 # Like `write_to
` but take care of creating the file
367 fun write_to_file(filepath: String)
369 var stream = new FileWriter.open(filepath)
375 # Utility class to access file system services.
377 # Usually created with `Text::to_path
`.
379 # `Path` objects does not necessarily represent existing files in a file system.
380 # They are sate-less objects that efficiently represent path information.
381 # They also provide an easy to use API on file-system services and are used to store their error status (see `last_error
`)
384 private var path: String
387 redef fun to_s do return path
389 # Short name of the file at `to_s
`
392 # var path = "/tmp/somefile".to_path
393 # assert path.filename == "somefile"
396 # The result does not depend of the file system, thus is cached for efficiency.
397 var filename: String = path.basename is lazy
399 # The path simplified by removing useless `.`, removing `//`, and resolving `..`
402 # var path = "somedir/./tmp/../somefile".to_path
403 # assert path.simplified.to_s == "somedir/somefile"
406 # See `String:simplify_path
` for details.
408 # The result does not depend of the file system, thus is cached for efficiency.
409 var simplified: Path is lazy do
410 var res = path.simplify_path.to_path
415 # Return the directory part of the path.
418 # var path = "/foo/bar/baz".to_path
419 # assert path.dir.to_s == "/foo/bar"
420 # assert path.dir.dir.to_s == "/foo"
421 # assert path.dir.dir.dir.to_s == "/"
424 # See `String:dirname
` for details.
426 # The result does not depend of the file system, thus is cached for efficiency.
427 var dir: Path is lazy do
428 return path.dirname.to_path
431 # Last error produced by I/O operations.
434 # var path = "/does/not/exists".to_path
435 # assert path.last_error == null
437 # assert path.last_error != null
440 # Since `Path` objects are stateless, `last_error
` is reset on most operations and reflect its status.
441 var last_error: nullable IOError = null is writable
443 # Does the file at `path
` exists?
445 # If the file does not exists, `last_error
` is set to the information.
446 fun exists: Bool do return stat != null
448 # Information on the file at `self` following symbolic links
450 # Returns `null` if there is no file at `self`.
451 # `last_error
` is updated to contains the error information on error, and null on success.
453 # assert "/etc/".to_path.stat.is_dir
454 # assert "/etc/issue".to_path.stat.is_file
455 # assert "/fail/does not/exist".to_path.stat == null
458 # var p = "/tmp/".to_path
460 # if stat != null then # Does `p
` exist?
461 # print "It's size is {stat.size}"
462 # if stat.is_dir then print "It's a directory"
464 # print p.last_error.to_s
467 fun stat: nullable FileStat
469 var stat = path.to_cstring.file_stat
470 if stat.address_is_null then
471 last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
475 return new FileStat(stat)
478 # Information on the file or link at `self`
480 # Do not follow symbolic links.
481 fun link_stat: nullable FileStat
483 var stat = path.to_cstring.file_lstat
484 if stat.address_is_null then
485 last_error = new IOError("Cannot open `{path}`: {sys.errno.strerror}")
489 return new FileStat(stat)
492 # Delete a file from the file system.
494 # `last_error
` is updated to contains the error information on error, and null on success.
497 var res = path.to_cstring.file_delete
499 last_error = new IOError("Cannot delete `{path}`: {sys.errno.strerror}")
505 # Copy content of file at `path
` to `dest
`.
507 # `last_error
` is updated to contains the error information on error, and null on success.
512 var output = dest.open_wo
514 var buffer = new CString(4096)
515 while not input.eof do
516 var read = input.read_bytes_to_cstring(buffer, 4096)
517 output.write_bytes_from_cstring(buffer, read)
522 last_error = input.last_error or else output.last_error
525 # Open this file for reading.
528 # var file = "/etc/issue".to_path.open_ro
529 # print file.read_line
533 # Note that it is the user's responsibility to close the stream.
534 # Therefore, for simple use case, look at `read_all
` or `each_line
`.
536 # ENSURE `last_error
== result
.last_error
`
537 fun open_ro: FileReader
539 var res = new FileReader.open(path)
540 last_error = res.last_error
544 # Open this file for writing
547 # var file = "bla.log".to_path.open_wo
548 # file.write "Blabla\n"
552 # Note that it is the user's responsibility to close the stream.
553 # Therefore, for simple use case, look at `Writable::write_to_file
`.
555 # ENSURE `last_error
== result
.last_error
`
556 fun open_wo: FileWriter
558 var res = new FileWriter.open(path)
559 last_error = res.last_error
563 # Read all the content of the file as a string.
566 # var content = "/etc/issue".to_path.read_all
570 # `last_error
` is updated to contains the error information on error, and null on success.
571 # In case of error, the result might be empty or truncated.
573 # See `Reader::read_all
` for details.
574 fun read_all: String do return read_all_bytes.to_s
576 # Read all the content on the file as a raw sequence of bytes.
579 # var content = "/etc/issue".to_path.read_all_bytes
583 # `last_error
` is updated to contains the error information on error, and null on success.
584 # In case of error, the result might be empty or truncated.
585 fun read_all_bytes: Bytes
588 var res = s.read_all_bytes
590 last_error = s.last_error
594 # Read all the lines of the file
597 # var lines = "/etc/passwd".to_path.read_lines
599 # print "{lines.length} users"
602 # var fields = l.split(":")
603 # print "name={fields[0]} uid={fields[2]}"
607 # `last_error
` is updated to contains the error information on error, and null on success.
608 # In case of error, the result might be empty or truncated.
610 # See `Reader::read_lines
` for details.
611 fun read_lines: Array[String]
614 var res = s.read_lines
616 last_error = s.last_error
620 # Return an iterator on each line of the file
623 # for l in "/etc/passwd".to_path.each_line do
624 # var fields = l.split(":")
625 # print "name={fields[0]} uid={fields[2]}"
629 # Note: the stream is automatically closed at the end of the file (see `LineIterator::close_on_finish
`)
631 # `last_error
` is updated to contains the error information on error, and null on success.
633 # See `Reader::each_line
` for details.
634 fun each_line: LineIterator
637 var res = s.each_line
638 res.close_on_finish = true
639 last_error = s.last_error
643 # Correctly join `self` with `subpath
` using the directory separator.
645 # Using a standard "{self}/{path}" does not work in the following cases:
648 # * `path
` starts with `'/'`.
650 # This method ensures that the join is valid.
652 # var hello = "hello".to_path
653 # assert (hello/"world").to_s == "hello/world"
654 # assert ("hel/lo".to_path / "wor/ld").to_s == "hel/lo/wor/ld"
655 # assert ("".to_path / "world").to_s == "world"
656 # assert (hello / "/world").to_s == "/world"
657 # assert ("hello/".to_path / "world").to_s == "hello/world"
658 fun /(subpath: String): Path do return new Path(path / subpath)
660 # Lists the files contained within the directory at `path
`.
662 # var files = "/etc".to_path.files
663 # assert files.has("/etc/issue".to_path)
665 # `last_error
` is updated to contains the error information on error, and null on success.
666 # In case of error, the result might be empty or truncated.
668 # var path = "/etc/issue".to_path
670 # assert files.is_empty
671 # assert path.last_error != null
672 fun files: Array[Path]
675 var res = new Array[Path]
676 var d = new NativeDir.opendir(path.to_cstring)
677 if d.address_is_null then
678 last_error = new IOError("Cannot list directory `{path}`: {sys.errno.strerror}")
684 if de.address_is_null then
685 # readdir cannot fail, so null means end of list
689 if name == "." or name == ".." then continue
697 # Is `self` the path to an existing directory ?
700 # assert ".".to_path.is_dir
701 # assert not "/etc/issue".to_path.is_dir
702 # assert not "/should/not/exist".to_path.is_dir
706 if st == null then return false
710 # Recursively delete a directory and all of its content
712 # Does not go through symbolic links and may get stuck in a cycle if there
713 # is a cycle in the file system.
715 # `last_error
` is updated with the first encountered error, or null on success.
716 # The method does not stop on the first error and tries to remove the most files and directories.
719 # var path = "/does/not/exists/".to_path
721 # assert path.last_error != null
723 # path = "/tmp/path/to/create".to_path
727 # assert path.last_error == null
731 var first_error = null
732 for file in self.files do
733 var stat = file.link_stat
735 if first_error == null then first_error = file.last_error
744 if first_error == null then first_error = file.last_error
747 # Delete the directory itself if things are fine
748 if first_error == null then
749 if not path.to_cstring.rmdir then
750 first_error = new IOError("Cannot remove `{self}`: {sys.errno.strerror}")
753 self.last_error = first_error
756 redef fun ==(other) do return other isa Path and simplified.path == other.simplified.path
757 redef fun hash do return simplified.path.hash
760 # Information on a file
762 # Created by `Path::stat
` and `Path::link_stat
`.
764 # The information within this class is gathered when the instance is initialized
765 # it will not be updated if the targeted file is modified.
771 # The low-level status of a file
774 private var stat: NativeFileStat
776 private var finalized = false
780 if not finalized then
786 # Returns the last access time in seconds since Epoch
787 fun last_access_time: Int
793 # Returns the last access time
795 # alias for `last_access_time
`
796 fun atime: Int do return last_access_time
798 # Returns the last modification time in seconds since Epoch
799 fun last_modification_time: Int
805 # Returns the last modification time
807 # alias for `last_modification_time
`
808 fun mtime: Int do return last_modification_time
811 # Size of the file at `path
`
818 # Is self a regular file and not a device file, pipe, socket, etc.?
825 # Alias for `is_file
`
826 fun is_reg: Bool do return is_file
828 # Is this a directory?
835 # Is this a symbolic link?
842 # FIXME Make the following POSIX only? or implement in some other way on Windows
844 # Returns the last status change time in seconds since Epoch
845 fun last_status_change_time: Int
851 # Returns the last status change time
853 # alias for `last_status_change_time
`
854 fun ctime: Int do return last_status_change_time
856 # Returns the permission bits of file
863 # Is this a character device?
870 # Is this a block device?
877 # Is this a FIFO pipe?
884 # Is this a UNIX socket
893 # Access file system related services on the path at `self`
894 fun to_path: Path do return new Path(to_s)
896 private fun write_native_to(s: FileWriter)
898 for i in substrings do s.write_native(i.to_cstring, 0, i.byte_length)
901 # return true if a file with this names exists
902 fun file_exists: Bool do return to_cstring.file_exists
904 # The status of a file. see POSIX stat(2).
905 fun file_stat: nullable FileStat
907 var stat = to_cstring.file_stat
908 if stat.address_is_null then return null
909 return new FileStat(stat)
912 # The status of a file or of a symlink. see POSIX lstat(2).
913 fun file_lstat: nullable FileStat
915 var stat = to_cstring.file_lstat
916 if stat.address_is_null then return null
917 return new FileStat(stat)
920 # Remove a file, return true if success
921 fun file_delete: Bool do return to_cstring.file_delete
923 # Copy content of file at `self` to `dest
`
924 fun file_copy_to(dest: String) do to_path.copy(dest.to_path)
926 # Remove the trailing `extension
`.
928 # `extension
` usually starts with a dot but could be anything.
930 # assert "file.txt".strip_extension(".txt") == "file"
931 # assert "file.txt".strip_extension("le.txt") == "fi"
932 # assert "file.txt".strip_extension("xt") == "file.t"
934 # If `extension
== null`, the rightmost extension is stripped, including the last dot.
936 # assert "file.txt".strip_extension == "file"
938 # If `extension
` is not present, `self` is returned unmodified.
940 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
941 fun strip_extension(extension: nullable String): String
943 if extension == null then
944 extension = file_extension
945 if extension == null then
947 else extension = ".{extension}"
950 if has_suffix(extension) then
951 return substring(0, length - extension.length).to_s
956 # Extract the basename of a path and strip the `extension
`
958 # The extension is stripped only if `extension
!= null`.
960 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
961 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
962 # assert "path/to/a_file.ext".basename == "a_file.ext"
963 # assert "path/to".basename(".ext") == "to"
964 # assert "path/to/".basename(".ext") == "to"
965 # assert "path/to".basename == "to"
966 # assert "path".basename == "path"
967 # assert "/path".basename == "path"
968 # assert "/".basename == "/"
969 # assert "".basename == ""
971 # On Windows, '\' are replaced by '/':
974 # assert "C:\\path\\to\\a_file.ext".basename(".ext") == "a_file"
975 # assert "C:\\".basename == "C:"
977 fun basename(extension: nullable String): String
980 if is_windows then n = n.replace("\\", "/")
982 var l = length - 1 # Index of the last char
983 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
984 if l == 0 then return "/"
985 var pos = chars.last_index_of_from('/', l)
987 n = substring(pos+1, l-pos)
990 if extension != null then
991 return n.strip_extension(extension)
995 # Extract the dirname of a path
997 # assert "/path/to/a_file.ext".dirname == "/path/to"
998 # assert "path/to/a_file.ext".dirname == "path/to"
999 # assert "path/to".dirname == "path"
1000 # assert "path/to/".dirname == "path"
1001 # assert "path".dirname == "."
1002 # assert "/path".dirname == "/"
1003 # assert "/".dirname == "/"
1004 # assert "".dirname == "."
1006 # On Windows, '\' are replaced by '/':
1009 # assert "C:\\path\\to\\a_file.ext".dirname == "C:/path/to"
1010 # assert "C:\\file".dirname == "C:"
1015 if is_windows then s = s.replace("\\", "/")
1017 var l = length - 1 # Index of the last char
1018 while l > 0 and s.chars[l] == '/' do l -= 1 # remove all trailing `/`
1019 var pos = s.chars.last_index_of_from('/', l)
1021 return s.substring(0, pos).to_s
1022 else if pos == 0 then
1029 # Return the canonicalized absolute pathname (see POSIX function `realpath
`)
1031 # Require: `file_exists
`
1032 fun realpath: String do
1033 var cs = to_cstring.file_realpath
1040 # Simplify a file path by remove useless `.`, removing `//`, and resolving `..`
1042 # * `..` are not resolved if they start the path
1043 # * starting `.` is simplified unless the path is empty
1044 # * starting `/` is not removed
1045 # * trailing `/` is removed
1047 # Note that the method only work on the string:
1049 # * no I/O access is performed
1050 # * the validity of the path is not checked
1053 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
1054 # assert "../dir/file".simplify_path == "../dir/file"
1055 # assert "dir/../../".simplify_path == ".."
1056 # assert "dir/..".simplify_path == "."
1057 # assert "//absolute//path/".simplify_path == "/absolute/path"
1058 # assert "//absolute//../".simplify_path == "/"
1059 # assert "/".simplify_path == "/"
1060 # assert "../".simplify_path == ".."
1061 # assert "./".simplify_path == "."
1062 # assert "././././././".simplify_path == "."
1063 # assert "./../dir".simplify_path == "../dir"
1064 # assert "./dir".simplify_path == "dir"
1067 # On Windows, '\' are replaced by '/':
1070 # assert "C:\\some\\.\\complex\\../../path/to/a_file.ext".simplify_path == "C:/path/to/a_file.ext"
1071 # assert "C:\\".simplify_path == "C:"
1073 fun simplify_path: String
1076 if is_windows then s = s.replace("\\", "/")
1077 var a = s.split_with("/")
1078 var a2 = new Array[String]
1080 if x == "." and not a2.is_empty then continue # skip `././`
1081 if x == "" and not a2.is_empty then continue # skip `//`
1082 if x == ".." and not a2.is_empty and a2.last != ".." then
1083 if a2.last == "." then # do not skip `./../`
1084 a2.pop # reduce `./../` in `../`
1085 else # reduce `dir
/../` in `/`
1089 else if not a2.is_empty and a2.last == "." then
1090 a2.pop # reduce `./dir
` in `dir
`
1094 if a2.is_empty then return "."
1095 if a2.length == 1 and a2.first == "" then return "/"
1099 # Correctly join two path using the directory separator.
1101 # Using a standard "{self}/{path}" does not work in the following cases:
1103 # * `self` is empty.
1104 # * `path
` starts with `'/'`.
1106 # This method ensures that the join is valid.
1108 # assert "hello".join_path("world") == "hello/world"
1109 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
1110 # assert "".join_path("world") == "world"
1111 # assert "hello".join_path("/world") == "/world"
1112 # assert "hello/".join_path("world") == "hello/world"
1113 # assert "hello/".join_path("/world") == "/world"
1115 # Note: You may want to use `simplify_path
` on the result.
1117 # Note: This method works only with POSIX paths.
1118 fun join_path(path: Text): String
1120 if path.is_empty then return self.to_s
1121 if self.is_empty then return path.to_s
1122 if path.chars[0] == '/' then return path.to_s
1123 if self.last == '/' then return "{self}{path}"
1124 return "{self}/{path}"
1127 # Convert the path (`self`) to a program name.
1129 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
1130 # used as a program name. In order to do that, prepend `./` if needed.
1132 # assert "foo".to_program_name == "./foo"
1133 # assert "/foo".to_program_name == "/foo"
1134 # assert "".to_program_name == "./" # At least, your shell will detect the error.
1135 fun to_program_name: String do
1136 if self.has_prefix("/") then
1143 # Alias for `join_path
`
1145 # assert "hello" / "world" == "hello/world"
1146 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
1147 # assert "" / "world" == "world"
1148 # assert "/hello" / "/world" == "/world"
1150 # This operator is quite useful for chaining changes of path.
1151 # The next one being relative to the previous one.
1155 # var c = "baz/foobar"
1156 # assert a/b/c == "/bar/baz/foobar"
1157 fun /(path: Text): String do return join_path(path)
1159 # Returns the relative path needed to go from `self` to `dest
`.
1161 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
1162 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
1164 # If `self` or `dest
` is relative, they are considered relatively to `getcwd
`.
1166 # In some cases, the result is still independent of the current directory:
1168 # assert "foo/bar".relpath("..") == "../../.."
1170 # In other cases, parts of the current directory may be exhibited:
1172 # var p = "../foo/bar".relpath("baz")
1173 # var c = getcwd.basename
1174 # assert p == "../../{c}/baz"
1176 # For path resolution independent of the current directory (eg. for paths in URL),
1177 # or to use an other starting directory than the current directory,
1178 # just force absolute paths:
1180 # var start = "/a/b/c/d"
1181 # var p2 = (start/"../foo/bar").relpath(start/"baz")
1182 # assert p2 == "../../d/baz"
1185 # Neither `self` or `dest
` has to be real paths or to exist in directories since
1186 # the resolution is only done with string manipulations and without any access to
1187 # the underlying file system.
1189 # If `self` and `dest
` are the same directory, the empty string is returned:
1191 # assert "foo".relpath("foo") == ""
1192 # assert "foo/../bar".relpath("bar") == ""
1194 # The empty string and "." designate both the current directory:
1196 # assert "".relpath("foo/bar") == "foo/bar"
1197 # assert ".".relpath("foo/bar") == "foo/bar"
1198 # assert "foo/bar".relpath("") == "../.."
1199 # assert "/" + "/".relpath(".") == getcwd
1200 fun relpath(dest: String): String
1202 # TODO windows support
1204 var from = (cwd/self).simplify_path.split("/")
1205 if from.last.is_empty then from.pop # case for the root directory
1206 var to = (cwd/dest).simplify_path.split("/")
1207 if to.last.is_empty then to.pop # case for the root directory
1209 # Remove common prefixes
1210 while not from.is_empty and not to.is_empty and from.first == to.first do
1215 # Result is going up in `from
` with ".." then going down following `to
`
1216 var from_len = from.length
1217 if from_len == 0 then return to.join("/")
1218 var up = "../"*(from_len-1) + ".."
1219 if to.is_empty then return up
1220 var res = up + "/" + to.join("/")
1224 # Create a directory (and all intermediate directories if needed)
1226 # The optional `mode
` parameter specifies the permissions of the directory,
1227 # the default value is `0o777
`.
1229 # Return an error object in case of error.
1231 # assert "/etc/".mkdir != null
1232 fun mkdir(mode: nullable Int): nullable Error
1234 mode = mode or else 0o777
1236 if is_windows then s = s.replace("\\", "/")
1238 var dirs = s.split_with("/")
1239 var path = new FlatBuffer
1240 if dirs.is_empty then return null
1241 if dirs[0].is_empty then
1242 # it was a starting /
1245 var error: nullable Error = null
1246 for i in [0 .. dirs.length - 1[ do
1248 if d.is_empty then continue
1251 if path.file_exists then continue
1252 var res = path.to_cstring.file_mkdir(mode)
1253 if not res and error == null then
1254 error = new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
1257 var res = s.to_cstring.file_mkdir(mode)
1258 if not res and error == null then
1259 error = new IOError("Cannot create directory `{path}`: {sys.errno.strerror}")
1264 # Delete a directory and all of its content, return `true` on success
1266 # Does not go through symbolic links and may get stuck in a cycle if there
1267 # is a cycle in the filesystem.
1269 # Return an error object in case of error.
1271 # assert "/fail/does not/exist".rmdir != null
1272 fun rmdir: nullable Error
1279 # Change the current working directory
1282 # assert getcwd == "/etc"
1284 # assert getcwd == "/"
1286 # Return an error object in case of error.
1288 # assert "/etc".chdir == null
1289 # assert "/fail/does no/exist".chdir != null
1290 # assert getcwd == "/etc" # unchanger
1291 fun chdir: nullable Error
1293 var res = to_cstring.file_chdir
1294 if res then return null
1295 var error = new IOError("Cannot change directory to `{self}`: {sys.errno.strerror}")
1299 # Return right-most extension (without the dot)
1301 # Only the last extension is returned.
1302 # There is no special case for combined extensions.
1304 # assert "file.txt".file_extension == "txt"
1305 # assert "file.tar.gz".file_extension == "gz"
1307 # For file without extension, `null` is returned.
1308 # Hoever, for trailing dot, `""` is returned.
1310 # assert "file".file_extension == null
1311 # assert "file.".file_extension == ""
1313 # The starting dot of hidden files is never considered.
1315 # assert ".file.txt".file_extension == "txt"
1316 # assert ".file".file_extension == null
1317 fun file_extension: nullable String
1319 var last_slash = chars.last_index_of('.')
1320 if last_slash > 0 then
1321 return substring( last_slash+1, length ).to_s
1327 # Returns entries contained within the directory represented by self.
1329 # var files = "/etc".files
1330 # assert files.has("issue")
1332 # Returns an empty array in case of error
1334 # files = "/etc/issue".files
1335 # assert files.is_empty
1337 # TODO find a better way to handle errors and to give them back to the user.
1338 fun files: Array[String]
1340 var res = new Array[String]
1341 var d = new NativeDir.opendir(to_cstring)
1342 if d.address_is_null then return res
1346 if de.address_is_null then break
1348 if name == "." or name == ".." then continue
1357 redef class FlatString
1358 redef fun write_native_to(s)
1360 s.write_native(items, first_byte, byte_length)
1363 redef fun file_extension do
1367 var st = _first_byte
1368 while p >= st and c != u'.' do
1372 if p <= st then return null
1374 return new FlatString.with_infos(its, ls - p, p + 1)
1377 redef fun basename(extension) do
1379 if is_windows then s = s.replace("\\", "/").as(FlatString)
1384 var min = s._first_byte
1386 while l > min and its[l] == sl do l -= 1
1387 if l == min then return "/"
1389 while ns >= min and its[ns] != sl do ns -= 1
1390 bname = new FlatString.with_infos(its, l - ns, ns + 1)
1392 return if extension != null then bname.strip_extension(extension) else bname
1397 private fun file_exists: Bool `{
1399 DWORD attribs
= GetFileAttributesA(self);
1400 return attribs
!= INVALID_FILE_ATTRIBUTES;
1402 FILE *hdl
= fopen
(self,"r");
1410 private fun file_stat: NativeFileStat `{
1412 if(stat
(self, &buff
) != -1) {
1413 struct stat
* stat_element
;
1414 stat_element
= malloc
(sizeof
(struct stat
));
1415 return memcpy
(stat_element
, &buff
, sizeof
(struct stat
));
1420 private fun file_lstat: NativeFileStat `{
1422 // FIXME use a higher level abstraction to support
WIN32
1425 struct stat
* stat_element
;
1427 stat_element
= malloc
(sizeof
(struct stat
));
1428 res
= lstat
(self, stat_element
);
1429 if (res
== -1) return NULL;
1430 return stat_element
;
1434 private fun file_mkdir(mode: Int): Bool `{
1436 return !mkdir
(self);
1438 return !mkdir
(self, mode
);
1442 private fun rmdir: Bool `{ return !rmdir(self); `}
1444 private fun file_delete
: Bool `{
1445 return remove(self) == 0;
1448 private fun file_chdir
: Bool `{ return !chdir(self); `}
1450 private fun file_realpath: CString `{
1452 DWORD len
= GetFullPathName(self, 0, NULL, NULL);
1453 char
*buf
= malloc
(len
+1); // FIXME don
't leak memory
1454 len = GetFullPathName(self, len+1, buf, NULL);
1457 return realpath(self, NULL);
1462 # This class is system dependent ... must reify the vfs
1463 private extern class NativeFileStat `{ struct stat * `}
1465 # Returns the permission bits of file
1466 fun mode: Int `{ return self->st_mode; `}
1468 # Returns the last access time
1469 fun atime: Int `{ return self->st_atime; `}
1471 # Returns the last status change time
1472 fun ctime: Int `{ return self->st_ctime; `}
1474 # Returns the last modification time
1475 fun mtime: Int `{ return self->st_mtime; `}
1478 fun size: Int `{ return self->st_size; `}
1480 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
1481 fun is_reg: Bool `{ return S_ISREG(self->st_mode); `}
1483 # Returns true if it is a directory
1484 fun is_dir: Bool `{ return S_ISDIR(self->st_mode); `}
1486 # Returns true if it is a character device
1487 fun is_chr: Bool `{ return S_ISCHR(self->st_mode); `}
1489 # Returns true if it is a block device
1490 fun is_blk: Bool `{ return S_ISBLK(self->st_mode); `}
1492 # Returns true if the type is fifo
1493 fun is_fifo: Bool `{ return S_ISFIFO(self->st_mode); `}
1495 # Returns true if the type is a link
1500 return S_ISLNK(self->st_mode);
1504 # Returns true if the type is a socket
1505 fun is_sock: Bool `{
1509 return S_ISSOCK(self->st_mode);
1514 # Instance of this class are standard FILE * pointers
1515 private extern class NativeFile `{ FILE* `}
1516 fun io_read(buf: CString, len: Int): Int `{
1517 return fread(buf, 1, len, self);
1520 fun io_write(buf: CString, from, len: Int): Int `{
1521 size_t res = fwrite(buf+from, 1, len, self);
1523 // Force flushing buffer because end of line does not trigger a flush
1529 fun write_byte(value: Int): Int `{
1530 unsigned char b = (unsigned char)value;
1531 return fwrite(&b, 1, 1, self);
1534 fun io_close: Int `{ return fclose(self); `}
1536 fun file_stat: NativeFileStat `{
1538 if(fstat(fileno(self), &buff) != -1) {
1539 struct stat* stat_element;
1540 stat_element = malloc(sizeof(struct stat));
1541 return memcpy(stat_element, &buff, sizeof(struct stat));
1546 fun ferror: Bool `{ return ferror(self); `}
1548 fun feof: Bool `{ return feof(self); `}
1550 fun fileno: Int `{ return fileno(self); `}
1552 # Flushes the buffer, forcing the write operation
1553 fun flush: Int `{ return fflush(self); `}
1555 # Used to specify how the buffering will be handled for the current stream.
1556 fun set_buffering_type(buf_length, mode: Int): Int `{
1557 return setvbuf(self, NULL, (int)mode, buf_length);
1560 new io_open_read(path: CString) `{ return fopen(path, "r"); `}
1562 new io_open_write(path: CString) `{ return fopen(path, "w"); `}
1564 new native_stdin `{ return stdin; `}
1566 new native_stdout `{ return stdout; `}
1568 new native_stderr `{ return stderr; `}
1571 # Standard `DIR*` pointer
1572 private extern class NativeDir `{ DIR* `}
1575 new opendir(path: CString) `{ return opendir(path); `}
1578 fun closedir `{ closedir(self); `}
1580 # Read the next directory entry
1581 fun readdir: CString `{
1584 if (!de) return NULL;
1592 var stdin: PollableReader = new Stdin is protected writable, lazy
1595 var stdout: Writer = new Stdout is protected writable, lazy
1597 # Standard output for errors
1598 var stderr: Writer = new Stderr is protected writable, lazy
1600 # Enumeration for buffer mode full (flushes when buffer is full)
1601 fun buffer_mode_full: Int `{ return _IOFBF; `}
1603 # Enumeration for buffer mode line (flushes when a `\n` is encountered)
1604 fun buffer_mode_line: Int `{ return _IONBF; `}
1606 # Enumeration for buffer mode none (flushes ASAP when something is written)
1607 fun buffer_mode_none: Int `{ return _IOLBF; `}
1609 # returns first available stream to read or write to
1610 # return null on interruption (possibly a signal)
1611 protected fun poll( streams : Sequence[FileStream] ) : nullable FileStream
1613 var in_fds = new Array[Int]
1614 var out_fds = new Array[Int]
1615 var fd_to_stream = new HashMap[Int,FileStream]
1618 if s isa FileReader then in_fds.add( fd )
1619 if s isa FileWriter then out_fds.add( fd )
1621 fd_to_stream[fd] = s
1624 var polled_fd = intern_poll( in_fds, out_fds )
1626 if polled_fd == null then
1629 return fd_to_stream[polled_fd]
1633 private fun intern_poll(in_fds: Array[Int], out_fds: Array[Int]): nullable Int
1634 import Array[Int].length, Array[Int].[], Int.as(nullable Int) `{
1636 // FIXME use a higher level abstraction to support WIN32
1638 int in_len, out_len, total_len;
1639 struct pollfd *c_fds;
1641 int first_polled_fd = -1;
1644 in_len = (int)Array_of_Int_length( in_fds );
1645 out_len = (int)Array_of_Int_length( out_fds );
1646 total_len = in_len + out_len;
1647 c_fds = malloc( sizeof(struct pollfd) * total_len );
1650 for ( i=0; i<in_len; i ++ ) {
1651 int fd = (int)Array_of_Int__index( in_fds, i );
1654 c_fds[i].events = POLLIN;
1657 /* output streams */
1658 for ( i=0; i<out_len; i ++ ) {
1659 int fd = (int)Array_of_Int__index( out_fds, i );
1662 c_fds[i].events = POLLOUT;
1665 /* poll all fds, unlimited timeout */
1666 result = poll( c_fds, total_len, -1 );
1669 /* analyse results */
1670 for ( i=0; i<total_len; i++ )
1671 if ( c_fds[i].revents & c_fds[i].events || /* awaited event */
1672 c_fds[i].revents & POLLHUP ) /* closed */
1674 first_polled_fd = c_fds[i].fd;
1678 return Int_as_nullable( first_polled_fd );
1680 else if ( result < 0 )
1681 fprintf( stderr, "Error in Stream:poll: %s\n", strerror( errno ) );
1689 # Print `objects` on the standard output (`stdout`).
1690 fun printn(objects: Object...)
1692 sys.stdout.write(objects.plain_to_s)
1695 # Print an `object` on the standard output (`stdout`) and add a newline.
1696 fun print(object: Object)
1698 sys.stdout.write(object.to_s)
1699 sys.stdout.write("\n")
1702 # Print `object` on the error output (`stderr` or a log system)
1703 fun print_error(object: Object)
1705 sys.stderr.write object.to_s
1706 sys.stderr.write "\n"
1709 # Read a character from the standard input (`stdin`).
1712 var c = sys.stdin.read_char
1713 if c == null then return '\
1'
1717 # Read a line from the standard input (`stdin`).
1720 return sys.stdin.read_line
1723 # Return the working (current) directory
1724 fun getcwd: String do return native_getcwd.to_s
1726 private fun native_getcwd: CString `{ return getcwd(NULL, 0); `}