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.)
26 #include <sys/types.h>
34 # File Abstract Stream
35 abstract class FStream
37 # The path of the file.
38 var path
: nullable String = null
41 private var file
: nullable NativeFile = null
43 # The status of a file. see POSIX stat(2).
44 fun file_stat
: FileStat do return _file
.file_stat
46 # File descriptor of this file
47 fun fd
: Int do return _file
.fileno
49 # Sets the buffering mode for the current FStream
51 # If the buf_size is <= 0, its value will be 512 by default
53 # The mode is any of the buffer_mode enumeration in `Sys`:
57 fun set_buffering_mode
(buf_size
, mode
: Int) do
58 if buf_size
<= 0 then buf_size
= 512
59 if _file
.set_buffering_type
(buf_size
, mode
) != 0 then
60 last_error
= new IOError("Error while changing buffering type for FStream, returned error {sys.errno.strerror}")
72 # Open the same file again.
73 # The original path is reused, therefore the reopened file can be a different file.
76 if not eof
and not _file
.address_is_null
then close
78 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
79 if _file
.address_is_null
then
80 last_error
= new IOError("Error: Opening file at '{path.as(not null)}' failed with '{sys.errno.strerror}'")
91 if _file
.address_is_null
then return
92 var i
= _file
.io_close
99 var nb
= _file
.io_read
(_buffer
.items
, _buffer
.capacity
)
109 redef var end_reached
: Bool = false
111 # Open the file at `path` for reading.
112 init open
(path
: String)
116 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
117 if _file
.address_is_null
then
118 last_error
= new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
123 init from_fd
(fd
: Int) do
126 _file
= fd
.fd_to_stream
(read_only
)
127 if _file
.address_is_null
then
128 last_error
= new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
141 if last_error
!= null then return
142 if not _is_writable
then
143 last_error
= new IOError("Cannot write to non-writable stream")
146 if s
isa FlatText then
147 write_native
(s
.to_cstring
, s
.length
)
149 for i
in s
.substrings
do write_native
(i
.to_cstring
, i
.length
)
156 if _file
.address_is_null
then
157 if last_error
!= null then return
158 last_error
= new IOError("Cannot close unopened write stream")
162 var i
= _file
.io_close
164 last_error
= new IOError("Close failed due to error {sys.errno.strerror}")
168 redef var is_writable
= false
170 # Write `len` bytes from `native`.
171 private fun write_native
(native
: NativeString, len
: Int)
173 if last_error
!= null then return
174 if not _is_writable
then
175 last_error
= new IOError("Cannot write to non-writable stream")
178 if _file
.address_is_null
then
179 last_error
= new IOError("Writing on a null stream")
183 var err
= _file
.io_write
(native
, len
)
186 last_error
= new IOError("Problem in writing : {err} {len} \n")
190 # Open the file at `path` for writing.
191 init open
(path
: String)
193 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
196 if _file
.address_is_null
then
197 last_error
= new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
202 # Creates a new File stream from a file descriptor
203 init from_fd
(fd
: Int) do
205 _file
= fd
.fd_to_stream
(wipe_write
)
207 if _file
.address_is_null
then
208 last_error
= new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
215 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
217 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
218 private fun fd_to_stream
(mode
: NativeString): NativeFile is extern "file_int_fdtostream"
221 # Constant for read-only file streams
222 private fun read_only
: NativeString do return "r".to_cstring
224 # Constant for write-only file streams
226 # If a stream is opened on a file with this method,
227 # it will wipe the previous file if any.
228 # Else, it will create the file.
229 private fun wipe_write
: NativeString do return "w".to_cstring
231 ###############################################################################
233 # Standard input stream.
238 _file
= new NativeFile.native_stdin
243 redef fun poll_in
: Bool is extern "file_stdin_poll_in"
246 # Standard output stream.
250 _file
= new NativeFile.native_stdout
256 # Standard error stream.
260 _file
= new NativeFile.native_stderr
266 ###############################################################################
268 redef class Streamable
269 # Like `write_to` but take care of creating the file
270 fun write_to_file
(filepath
: String)
272 var stream
= new OFStream.open
(filepath
)
279 # return true if a file with this names exists
280 fun file_exists
: Bool do return to_cstring
.file_exists
282 # The status of a file. see POSIX stat(2).
283 fun file_stat
: FileStat do return to_cstring
.file_stat
285 # The status of a file or of a symlink. see POSIX lstat(2).
286 fun file_lstat
: FileStat do return to_cstring
.file_lstat
288 # Remove a file, return true if success
289 fun file_delete
: Bool do return to_cstring
.file_delete
291 # Copy content of file at `self` to `dest`
292 fun file_copy_to
(dest
: String)
294 var input
= new IFStream.open
(self)
295 var output
= new OFStream.open
(dest
)
297 while not input
.eof
do
298 var buffer
= input
.read
(1024)
306 # Remove the trailing extension `ext`.
308 # `ext` usually starts with a dot but could be anything.
310 # assert "file.txt".strip_extension(".txt") == "file"
311 # assert "file.txt".strip_extension("le.txt") == "fi"
312 # assert "file.txt".strip_extension("xt") == "file.t"
314 # if `ext` is not present, `self` is returned unmodified.
316 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
317 fun strip_extension
(ext
: String): String
319 if has_suffix
(ext
) then
320 return substring
(0, length
- ext
.length
)
325 # Extract the basename of a path and remove the extension
327 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
328 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
329 # assert "path/to".basename(".ext") == "to"
330 # assert "path/to/".basename(".ext") == "to"
331 # assert "path".basename("") == "path"
332 # assert "/path".basename("") == "path"
333 # assert "/".basename("") == "/"
334 # assert "".basename("") == ""
335 fun basename
(ext
: String): String
337 var l
= length
- 1 # Index of the last char
338 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
339 if l
== 0 then return "/"
340 var pos
= chars
.last_index_of_from
('/', l
)
343 n
= substring
(pos
+1, l-pos
)
345 return n
.strip_extension
(ext
)
348 # Extract the dirname of a path
350 # assert "/path/to/a_file.ext".dirname == "/path/to"
351 # assert "path/to/a_file.ext".dirname == "path/to"
352 # assert "path/to".dirname == "path"
353 # assert "path/to/".dirname == "path"
354 # assert "path".dirname == "."
355 # assert "/path".dirname == "/"
356 # assert "/".dirname == "/"
357 # assert "".dirname == "."
360 var l
= length
- 1 # Index of the last char
361 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
362 var pos
= chars
.last_index_of_from
('/', l
)
364 return substring
(0, pos
)
365 else if pos
== 0 then
372 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
373 fun realpath
: String do
374 var cs
= to_cstring
.file_realpath
375 var res
= cs
.to_s_with_copy
376 # cs.free_malloc # FIXME memory leak
380 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
381 # ".." are not resolved if they start the path
382 # starting "/" is not removed
383 # trainling "/" is removed
385 # Note that the method only wonrk on the string:
386 # * no I/O access is performed
387 # * the validity of the path is not checked
389 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
390 # assert "../dir/file".simplify_path == "../dir/file"
391 # assert "dir/../../".simplify_path == ".."
392 # assert "dir/..".simplify_path == "."
393 # assert "//absolute//path/".simplify_path == "/absolute/path"
394 # assert "//absolute//../".simplify_path == "/"
395 fun simplify_path
: String
397 var a
= self.split_with
("/")
398 var a2
= new Array[String]
400 if x
== "." then continue
401 if x
== "" and not a2
.is_empty
then continue
402 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
408 if a2
.is_empty
then return "."
409 if a2
.length
== 1 and a2
.first
== "" then return "/"
413 # Correctly join two path using the directory separator.
415 # Using a standard "{self}/{path}" does not work in the following cases:
418 # * `path` ends with `'/'`.
419 # * `path` starts with `'/'`.
421 # This method ensures that the join is valid.
423 # assert "hello".join_path("world") == "hello/world"
424 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
425 # assert "".join_path("world") == "world"
426 # assert "hello".join_path("/world") == "/world"
427 # assert "hello/".join_path("world") == "hello/world"
428 # assert "hello/".join_path("/world") == "/world"
430 # Note: You may want to use `simplify_path` on the result.
432 # Note: This method works only with POSIX paths.
433 fun join_path
(path
: String): String
435 if path
.is_empty
then return self
436 if self.is_empty
then return path
437 if path
.chars
[0] == '/' then return path
438 if self.last
== '/' then return "{self}{path}"
439 return "{self}/{path}"
442 # Convert the path (`self`) to a program name.
444 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
445 # used as a program name. In order to do that, prepend `./` if needed.
447 # assert "foo".to_program_name == "./foo"
448 # assert "/foo".to_program_name == "/foo"
449 # assert "".to_program_name == "./" # At least, your shell will detect the error.
450 fun to_program_name
: String do
451 if self.has_prefix
("/") then
458 # Alias for `join_path`
460 # assert "hello" / "world" == "hello/world"
461 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
462 # assert "" / "world" == "world"
463 # assert "/hello" / "/world" == "/world"
465 # This operator is quite useful for chaining changes of path.
466 # The next one being relative to the previous one.
470 # var c = "baz/foobar"
471 # assert a/b/c == "/bar/baz/foobar"
472 fun /(path
: String): String do return join_path
(path
)
474 # Returns the relative path needed to go from `self` to `dest`.
476 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
477 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
479 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
481 # In some cases, the result is still independent of the current directory:
483 # assert "foo/bar".relpath("..") == "../../.."
485 # In other cases, parts of the current directory may be exhibited:
487 # var p = "../foo/bar".relpath("baz")
488 # var c = getcwd.basename("")
489 # assert p == "../../{c}/baz"
491 # For path resolution independent of the current directory (eg. for paths in URL),
492 # or to use an other starting directory than the current directory,
493 # just force absolute paths:
495 # var start = "/a/b/c/d"
496 # var p2 = (start/"../foo/bar").relpath(start/"baz")
497 # assert p2 == "../../d/baz"
500 # Neither `self` or `dest` has to be real paths or to exist in directories since
501 # the resolution is only done with string manipulations and without any access to
502 # the underlying file system.
504 # If `self` and `dest` are the same directory, the empty string is returned:
506 # assert "foo".relpath("foo") == ""
507 # assert "foo/../bar".relpath("bar") == ""
509 # The empty string and "." designate both the current directory:
511 # assert "".relpath("foo/bar") == "foo/bar"
512 # assert ".".relpath("foo/bar") == "foo/bar"
513 # assert "foo/bar".relpath("") == "../.."
514 # assert "/" + "/".relpath(".") == getcwd
515 fun relpath
(dest
: String): String
518 var from
= (cwd
/self).simplify_path
.split
("/")
519 if from
.last
.is_empty
then from
.pop
# case for the root directory
520 var to
= (cwd
/dest
).simplify_path
.split
("/")
521 if to
.last
.is_empty
then to
.pop
# case for the root directory
523 # Remove common prefixes
524 while not from
.is_empty
and not to
.is_empty
and from
.first
== to
.first
do
529 # Result is going up in `from` with ".." then going down following `to`
530 var from_len
= from
.length
531 if from_len
== 0 then return to
.join
("/")
532 var up
= "../"*(from_len-1
) + ".."
533 if to
.is_empty
then return up
534 var res
= up
+ "/" + to
.join
("/")
538 # Create a directory (and all intermediate directories if needed)
541 var dirs
= self.split_with
("/")
542 var path
= new FlatBuffer
543 if dirs
.is_empty
then return
544 if dirs
[0].is_empty
then
545 # it was a starting /
549 if d
.is_empty
then continue
552 path
.to_s
.to_cstring
.file_mkdir
556 # Delete a directory and all of its content, return `true` on success
558 # Does not go through symbolic links and may get stuck in a cycle if there
559 # is a cycle in the filesystem.
563 for file
in self.files
do
564 var file_path
= self.join_path
(file
)
565 var stat
= file_path
.file_lstat
567 ok
= file_path
.rmdir
and ok
569 ok
= file_path
.file_delete
and ok
574 # Delete the directory itself
575 if ok
then to_cstring
.rmdir
580 # Change the current working directory
583 # assert getcwd == "/etc"
585 # assert getcwd == "/"
588 fun chdir
do to_cstring
.file_chdir
590 # Return right-most extension (without the dot)
592 # Only the last extension is returned.
593 # There is no special case for combined extensions.
595 # assert "file.txt".file_extension == "txt"
596 # assert "file.tar.gz".file_extension == "gz"
598 # For file without extension, `null` is returned.
599 # Hoever, for trailing dot, `""` is returned.
601 # assert "file".file_extension == null
602 # assert "file.".file_extension == ""
604 # The starting dot of hidden files is never considered.
606 # assert ".file.txt".file_extension == "txt"
607 # assert ".file".file_extension == null
608 fun file_extension
: nullable String
610 var last_slash
= chars
.last_index_of
('.')
611 if last_slash
> 0 then
612 return substring
( last_slash
+1, length
)
618 # returns files contained within the directory represented by self
619 fun files
: Array[String] is extern import Array[String], Array[String].add
, NativeString.to_s
, String.to_cstring
`{
623 dir_path = String_to_cstring( recv );
624 if ((dir = opendir(dir_path)) == NULL)
631 Array_of_String results;
635 results = new_Array_of_String();
637 while ( ( de = readdir( dir ) ) != NULL )
638 if ( strcmp( de->d_name, ".." ) != 0 &&
639 strcmp( de->d_name, "." ) != 0 )
641 file_name = NativeString_to_s( strdup( de->d_name ) );
642 Array_of_String_add( results, file_name );
651 redef class NativeString
652 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
653 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
654 private fun file_lstat
: FileStat `{
655 struct stat* stat_element;
657 stat_element = malloc(sizeof(struct stat));
658 res = lstat(recv, stat_element);
659 if (res == -1) return NULL;
662 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
663 private fun rmdir
: Bool `{ return rmdir(recv); `}
664 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
665 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
666 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
669 # This class is system dependent ... must reify the vfs
670 extern class FileStat `{ struct stat * `}
671 # Returns the permission bits of file
672 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
673 # Returns the last access time
674 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
675 # Returns the last status change time
676 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
677 # Returns the last modification time
678 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
680 fun size
: Int is extern "file_FileStat_FileStat_size_0"
682 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
683 fun is_reg
: Bool `{ return S_ISREG(recv->st_mode); `}
684 # Returns true if it is a directory
685 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
686 # Returns true if it is a character device
687 fun is_chr
: Bool `{ return S_ISCHR(recv->st_mode); `}
688 # Returns true if it is a block device
689 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
690 # Returns true if the type is fifo
691 fun is_fifo
: Bool `{ return S_ISFIFO(recv->st_mode); `}
692 # Returns true if the type is a link
693 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
694 # Returns true if the type is a socket
695 fun is_sock
: Bool `{ return S_ISSOCK(recv->st_mode); `}
698 # Instance of this class are standard FILE * pointers
699 private extern class NativeFile `{ FILE* `}
700 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
701 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
702 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
703 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
704 fun fileno
: Int `{ return fileno(recv); `}
705 # Flushes the buffer, forcing the write operation
706 fun flush: Int is extern "fflush"
707 # Used to specify how the buffering will be handled for the current stream.
708 fun set_buffering_type(buf_length: Int, mode: Int): Int is extern "file_NativeFile_NativeFile_set_buffering_type_0"
710 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
711 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
712 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
713 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
714 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
720 if stdout isa FStream then stdout.as(FStream).set_buffering_mode(256, buffer_mode_line)
724 var stdin: PollableIStream = new Stdin is protected writable
727 var stdout: OStream = new Stdout is protected writable
729 # Standard output for errors
730 var stderr: OStream = new Stderr is protected writable
732 # Enumeration for buffer mode full (flushes when buffer is full)
733 fun buffer_mode_full: Int is extern "file_Sys_Sys_buffer_mode_full_0"
734 # Enumeration for buffer mode line (flushes when a `\n
` is encountered)
735 fun buffer_mode_line: Int is extern "file_Sys_Sys_buffer_mode_line_0"
736 # Enumeration for buffer mode none (flushes ASAP when something is written)
737 fun buffer_mode_none: Int is extern "file_Sys_Sys_buffer_mode_none_0"
739 # returns first available stream to read or write to
740 # return null on interruption (possibly a signal)
741 protected fun poll( streams : Sequence[FStream] ) : nullable FStream
743 var in_fds = new Array[Int]
744 var out_fds = new Array[Int]
745 var fd_to_stream = new HashMap[Int,FStream]
748 if s isa IFStream then in_fds.add( fd )
749 if s isa OFStream then out_fds.add( fd )
754 var polled_fd = intern_poll( in_fds, out_fds )
756 if polled_fd == null then
759 return fd_to_stream[polled_fd]
763 private fun intern_poll(in_fds: Array[Int], out_fds: Array[Int]) : nullable Int is extern import Array[Int].length, Array[Int].[], Int.as(nullable Int) `{
764 int in_len
, out_len
, total_len
;
765 struct pollfd
*c_fds
;
768 int first_polled_fd
= -1;
771 in_len
= Array_of_Int_length( in_fds
);
772 out_len
= Array_of_Int_length( out_fds
);
773 total_len
= in_len
+ out_len
;
774 c_fds
= malloc
( sizeof
(struct pollfd
) * total_len
);
777 for ( i
=0; i
<in_len
; i
++ ) {
779 fd
= Array_of_Int__index( in_fds
, i
);
782 c_fds
[i
].events
= POLLIN;
786 for ( i
=0; i
<out_len
; i
++ ) {
788 fd
= Array_of_Int__index( out_fds
, i
);
791 c_fds
[i
].events
= POLLOUT;
794 /* poll all fds
, unlimited timeout
*/
795 result
= poll
( c_fds
, total_len
, -1 );
798 /* analyse results
*/
799 for ( i
=0; i
<total_len
; i
++ )
800 if ( c_fds
[i
].revents
& c_fds
[i
].events
|| /* awaited event
*/
801 c_fds
[i
].revents
& POLLHUP ) /* closed
*/
803 first_polled_fd
= c_fds
[i
].fd
;
807 return Int_as_nullable( first_polled_fd
);
809 else if ( result
< 0 )
810 fprintf
( stderr
, "Error in Stream:poll: %s\n", strerror
( errno
) );
817 # Print `objects
` on the standard output (`stdout
`).
818 protected fun printn(objects: Object...)
820 sys.stdout.write(objects.to_s)
823 # Print an `object
` on the standard output (`stdout
`) and add a newline.
824 protected fun print(object: Object)
826 sys.stdout.write(object.to_s)
827 sys.stdout.write("\n")
830 # Read a character from the standard input (`stdin
`).
831 protected fun getc: Char
833 return sys.stdin.read_char.ascii
836 # Read a line from the standard input (`stdin
`).
837 protected fun gets: String
839 return sys.stdin.read_line
842 # Return the working (current) directory
843 protected fun getcwd: String do return file_getcwd.to_s
844 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"