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 fun file_stat
: FileStat do return _file
.file_stat
45 # File descriptor of this file
46 fun fd
: Int do return _file
.fileno
56 # Open the same file again.
57 # The original path is reused, therefore the reopened file can be a different file.
60 if not eof
and not _file
.address_is_null
then close
62 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
63 if _file
.address_is_null
then
64 last_error
= new IOError("Error: Opening file at '{path.as(not null)}' failed with '{sys.errno.strerror}'")
75 if _file
.address_is_null
then return
76 var i
= _file
.io_close
83 var nb
= _file
.io_read
(_buffer
.items
, _buffer
.capacity
)
93 redef var end_reached
: Bool = false
95 # Open the file at `path` for reading.
96 init open
(path
: String)
100 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
101 if _file
.address_is_null
then
102 last_error
= new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
107 init from_fd
(fd
: Int) do
110 _file
= fd
.fd_to_stream
(read_only
)
111 if _file
.address_is_null
then
112 last_error
= new IOError("Error: Converting fd {fd} to stream failed with '{sys.errno.strerror}'")
125 if last_error
!= null then return
126 if not _is_writable
then
127 last_error
= new IOError("Cannot write to non-writable stream")
130 if s
isa FlatText then
131 write_native
(s
.to_cstring
, s
.length
)
133 for i
in s
.substrings
do write_native
(i
.to_cstring
, i
.length
)
140 if _file
.address_is_null
then
141 if last_error
!= null then return
142 last_error
= new IOError("Cannot close unopened write stream")
146 var i
= _file
.io_close
148 last_error
= new IOError("Close failed due to error {sys.errno.strerror}")
152 redef var is_writable
= false
154 # Write `len` bytes from `native`.
155 private fun write_native
(native
: NativeString, len
: Int)
157 if last_error
!= null then return
158 if not _is_writable
then
159 last_error
= new IOError("Cannot write to non-writable stream")
162 if _file
.address_is_null
then
163 last_error
= new IOError("Writing on a null stream")
167 var err
= _file
.io_write
(native
, len
)
170 last_error
= new IOError("Problem in writing : {err} {len} \n")
174 # Open the file at `path` for writing.
175 init open
(path
: String)
177 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
180 if _file
.address_is_null
then
181 last_error
= new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
186 # Creates a new File stream from a file descriptor
187 init from_fd
(fd
: Int) do
189 _file
= fd
.fd_to_stream
(wipe_write
)
191 if _file
.address_is_null
then
192 last_error
= new IOError("Error: Opening stream from file descriptor {fd} failed with '{sys.errno.strerror}'")
199 # Creates a file stream from a file descriptor `fd` using the file access `mode`.
201 # NOTE: The `mode` specified must be compatible with the one used in the file descriptor.
202 private fun fd_to_stream
(mode
: NativeString): NativeFile is extern "file_int_fdtostream"
205 # Constant for read-only file streams
206 private fun read_only
: NativeString do return "r".to_cstring
208 # Constant for write-only file streams
210 # If a stream is opened on a file with this method,
211 # it will wipe the previous file if any.
212 # Else, it will create the file.
213 private fun wipe_write
: NativeString do return "w".to_cstring
215 ###############################################################################
221 _file
= new NativeFile.native_stdin
226 redef fun poll_in
: Bool is extern "file_stdin_poll_in"
232 _file
= new NativeFile.native_stdout
241 _file
= new NativeFile.native_stderr
247 ###############################################################################
249 redef class Streamable
250 # Like `write_to` but take care of creating the file
251 fun write_to_file
(filepath
: String)
253 var stream
= new OFStream.open
(filepath
)
260 # return true if a file with this names exists
261 fun file_exists
: Bool do return to_cstring
.file_exists
263 # The status of a file. see POSIX stat(2).
264 fun file_stat
: FileStat do return to_cstring
.file_stat
266 # The status of a file or of a symlink. see POSIX lstat(2).
267 fun file_lstat
: FileStat do return to_cstring
.file_lstat
269 # Remove a file, return true if success
270 fun file_delete
: Bool do return to_cstring
.file_delete
272 # Copy content of file at `self` to `dest`
273 fun file_copy_to
(dest
: String)
275 var input
= new IFStream.open
(self)
276 var output
= new OFStream.open
(dest
)
278 while not input
.eof
do
279 var buffer
= input
.read
(1024)
287 # Remove the trailing extension `ext`.
289 # `ext` usually starts with a dot but could be anything.
291 # assert "file.txt".strip_extension(".txt") == "file"
292 # assert "file.txt".strip_extension("le.txt") == "fi"
293 # assert "file.txt".strip_extension("xt") == "file.t"
295 # if `ext` is not present, `self` is returned unmodified.
297 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
298 fun strip_extension
(ext
: String): String
300 if has_suffix
(ext
) then
301 return substring
(0, length
- ext
.length
)
306 # Extract the basename of a path and remove the extension
308 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
309 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
310 # assert "path/to".basename(".ext") == "to"
311 # assert "path/to/".basename(".ext") == "to"
312 # assert "path".basename("") == "path"
313 # assert "/path".basename("") == "path"
314 # assert "/".basename("") == "/"
315 # assert "".basename("") == ""
316 fun basename
(ext
: String): String
318 var l
= length
- 1 # Index of the last char
319 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
320 if l
== 0 then return "/"
321 var pos
= chars
.last_index_of_from
('/', l
)
324 n
= substring
(pos
+1, l-pos
)
326 return n
.strip_extension
(ext
)
329 # Extract the dirname of a path
331 # assert "/path/to/a_file.ext".dirname == "/path/to"
332 # assert "path/to/a_file.ext".dirname == "path/to"
333 # assert "path/to".dirname == "path"
334 # assert "path/to/".dirname == "path"
335 # assert "path".dirname == "."
336 # assert "/path".dirname == "/"
337 # assert "/".dirname == "/"
338 # assert "".dirname == "."
341 var l
= length
- 1 # Index of the last char
342 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
343 var pos
= chars
.last_index_of_from
('/', l
)
345 return substring
(0, pos
)
346 else if pos
== 0 then
353 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
354 fun realpath
: String do
355 var cs
= to_cstring
.file_realpath
356 var res
= cs
.to_s_with_copy
357 # cs.free_malloc # FIXME memory leak
361 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
362 # ".." are not resolved if they start the path
363 # starting "/" is not removed
364 # trainling "/" is removed
366 # Note that the method only wonrk on the string:
367 # * no I/O access is performed
368 # * the validity of the path is not checked
370 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
371 # assert "../dir/file".simplify_path == "../dir/file"
372 # assert "dir/../../".simplify_path == ".."
373 # assert "dir/..".simplify_path == "."
374 # assert "//absolute//path/".simplify_path == "/absolute/path"
375 # assert "//absolute//../".simplify_path == "/"
376 fun simplify_path
: String
378 var a
= self.split_with
("/")
379 var a2
= new Array[String]
381 if x
== "." then continue
382 if x
== "" and not a2
.is_empty
then continue
383 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
389 if a2
.is_empty
then return "."
390 if a2
.length
== 1 and a2
.first
== "" then return "/"
394 # Correctly join two path using the directory separator.
396 # Using a standard "{self}/{path}" does not work in the following cases:
399 # * `path` ends with `'/'`.
400 # * `path` starts with `'/'`.
402 # This method ensures that the join is valid.
404 # assert "hello".join_path("world") == "hello/world"
405 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
406 # assert "".join_path("world") == "world"
407 # assert "hello".join_path("/world") == "/world"
408 # assert "hello/".join_path("world") == "hello/world"
409 # assert "hello/".join_path("/world") == "/world"
411 # Note: You may want to use `simplify_path` on the result.
413 # Note: This method works only with POSIX paths.
414 fun join_path
(path
: String): String
416 if path
.is_empty
then return self
417 if self.is_empty
then return path
418 if path
.chars
[0] == '/' then return path
419 if self.last
== '/' then return "{self}{path}"
420 return "{self}/{path}"
423 # Convert the path (`self`) to a program name.
425 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
426 # used as a program name. In order to do that, prepend `./` if needed.
428 # assert "foo".to_program_name == "./foo"
429 # assert "/foo".to_program_name == "/foo"
430 # assert "".to_program_name == "./" # At least, your shell will detect the error.
431 fun to_program_name
: String do
432 if self.has_prefix
("/") then
439 # Alias for `join_path`
441 # assert "hello" / "world" == "hello/world"
442 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
443 # assert "" / "world" == "world"
444 # assert "/hello" / "/world" == "/world"
446 # This operator is quite useful for chaining changes of path.
447 # The next one being relative to the previous one.
451 # var c = "baz/foobar"
452 # assert a/b/c == "/bar/baz/foobar"
453 fun /(path
: String): String do return join_path
(path
)
455 # Returns the relative path needed to go from `self` to `dest`.
457 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
458 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
460 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
462 # In some cases, the result is still independent of the current directory:
464 # assert "foo/bar".relpath("..") == "../../.."
466 # In other cases, parts of the current directory may be exhibited:
468 # var p = "../foo/bar".relpath("baz")
469 # var c = getcwd.basename("")
470 # assert p == "../../{c}/baz"
472 # For path resolution independent of the current directory (eg. for paths in URL),
473 # or to use an other starting directory than the current directory,
474 # just force absolute paths:
476 # var start = "/a/b/c/d"
477 # var p2 = (start/"../foo/bar").relpath(start/"baz")
478 # assert p2 == "../../d/baz"
481 # Neither `self` or `dest` has to be real paths or to exist in directories since
482 # the resolution is only done with string manipulations and without any access to
483 # the underlying file system.
485 # If `self` and `dest` are the same directory, the empty string is returned:
487 # assert "foo".relpath("foo") == ""
488 # assert "foo/../bar".relpath("bar") == ""
490 # The empty string and "." designate both the current directory:
492 # assert "".relpath("foo/bar") == "foo/bar"
493 # assert ".".relpath("foo/bar") == "foo/bar"
494 # assert "foo/bar".relpath("") == "../.."
495 # assert "/" + "/".relpath(".") == getcwd
496 fun relpath
(dest
: String): String
499 var from
= (cwd
/self).simplify_path
.split
("/")
500 if from
.last
.is_empty
then from
.pop
# case for the root directory
501 var to
= (cwd
/dest
).simplify_path
.split
("/")
502 if to
.last
.is_empty
then to
.pop
# case for the root directory
504 # Remove common prefixes
505 while not from
.is_empty
and not to
.is_empty
and from
.first
== to
.first
do
510 # Result is going up in `from` with ".." then going down following `to`
511 var from_len
= from
.length
512 if from_len
== 0 then return to
.join
("/")
513 var up
= "../"*(from_len-1
) + ".."
514 if to
.is_empty
then return up
515 var res
= up
+ "/" + to
.join
("/")
519 # Create a directory (and all intermediate directories if needed)
522 var dirs
= self.split_with
("/")
523 var path
= new FlatBuffer
524 if dirs
.is_empty
then return
525 if dirs
[0].is_empty
then
526 # it was a starting /
530 if d
.is_empty
then continue
533 path
.to_s
.to_cstring
.file_mkdir
537 # Delete a directory and all of its content, return `true` on success
539 # Does not go through symbolic links and may get stuck in a cycle if there
540 # is a cycle in the filesystem.
544 for file
in self.files
do
545 var file_path
= self.join_path
(file
)
546 var stat
= file_path
.file_lstat
548 ok
= file_path
.rmdir
and ok
550 ok
= file_path
.file_delete
and ok
555 # Delete the directory itself
556 if ok
then to_cstring
.rmdir
561 # Change the current working directory
564 # assert getcwd == "/etc"
566 # assert getcwd == "/"
569 fun chdir
do to_cstring
.file_chdir
571 # Return right-most extension (without the dot)
573 # Only the last extension is returned.
574 # There is no special case for combined extensions.
576 # assert "file.txt".file_extension == "txt"
577 # assert "file.tar.gz".file_extension == "gz"
579 # For file without extension, `null` is returned.
580 # Hoever, for trailing dot, `""` is returned.
582 # assert "file".file_extension == null
583 # assert "file.".file_extension == ""
585 # The starting dot of hidden files is never considered.
587 # assert ".file.txt".file_extension == "txt"
588 # assert ".file".file_extension == null
589 fun file_extension
: nullable String
591 var last_slash
= chars
.last_index_of
('.')
592 if last_slash
> 0 then
593 return substring
( last_slash
+1, length
)
599 # returns files contained within the directory represented by self
600 fun files
: Set[ String ] is extern import HashSet[String], HashSet[String].add
, NativeString.to_s
, String.to_cstring
, HashSet[String].as(Set[String]) `{
604 dir_path = String_to_cstring( recv );
605 if ((dir = opendir(dir_path)) == NULL)
612 HashSet_of_String results;
616 results = new_HashSet_of_String();
618 while ( ( de = readdir( dir ) ) != NULL )
619 if ( strcmp( de->d_name, ".." ) != 0 &&
620 strcmp( de->d_name, "." ) != 0 )
622 file_name = NativeString_to_s( strdup( de->d_name ) );
623 HashSet_of_String_add( results, file_name );
627 return HashSet_of_String_as_Set_of_String( results );
632 redef class NativeString
633 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
634 private fun file_stat
: FileStat is extern "string_NativeString_NativeString_file_stat_0"
635 private fun file_lstat
: FileStat `{
636 struct stat* stat_element;
638 stat_element = malloc(sizeof(struct stat));
639 res = lstat(recv, stat_element);
640 if (res == -1) return NULL;
643 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
644 private fun rmdir
: Bool `{ return rmdir(recv); `}
645 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
646 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
647 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
650 # This class is system dependent ... must reify the vfs
651 extern class FileStat `{ struct stat * `}
652 # Returns the permission bits of file
653 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
654 # Returns the last access time
655 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
656 # Returns the last status change time
657 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
658 # Returns the last modification time
659 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
661 fun size
: Int is extern "file_FileStat_FileStat_size_0"
663 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
664 fun is_reg
: Bool `{ return S_ISREG(recv->st_mode); `}
665 # Returns true if it is a directory
666 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
667 # Returns true if it is a character device
668 fun is_chr
: Bool `{ return S_ISCHR(recv->st_mode); `}
669 # Returns true if it is a block device
670 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
671 # Returns true if the type is fifo
672 fun is_fifo
: Bool `{ return S_ISFIFO(recv->st_mode); `}
673 # Returns true if the type is a link
674 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
675 # Returns true if the type is a socket
676 fun is_sock
: Bool `{ return S_ISSOCK(recv->st_mode); `}
679 # Instance of this class are standard FILE * pointers
680 private extern class NativeFile `{ FILE* `}
681 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
682 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
683 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
684 fun file_stat
: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
685 fun fileno
: Int `{ return fileno(recv); `}
686 # Flushes the buffer, forcing the write operation
687 fun flush: Int is extern "fflush"
689 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
690 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
691 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
692 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
693 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
699 var stdin: PollableIStream = new Stdin is protected writable
702 var stdout: OStream = new Stdout is protected writable
704 # Standard output for errors
705 var stderr: OStream = new Stderr is protected writable
707 # Enumeration for buffer mode full (flushes when buffer is full)
708 fun buffer_mode_full: Int is extern "file_Sys_Sys_buffer_mode_full_0"
709 # Enumeration for buffer mode line (flushes when a `\n
` is encountered)
710 fun buffer_mode_line: Int is extern "file_Sys_Sys_buffer_mode_line_0"
711 # Enumeration for buffer mode none (flushes ASAP when something is written)
712 fun buffer_mode_none: Int is extern "file_Sys_Sys_buffer_mode_none_0"
714 # returns first available stream to read or write to
715 # return null on interruption (possibly a signal)
716 protected fun poll( streams : Sequence[FStream] ) : nullable FStream
718 var in_fds = new Array[Int]
719 var out_fds = new Array[Int]
720 var fd_to_stream = new HashMap[Int,FStream]
723 if s isa IFStream then in_fds.add( fd )
724 if s isa OFStream then out_fds.add( fd )
729 var polled_fd = intern_poll( in_fds, out_fds )
731 if polled_fd == null then
734 return fd_to_stream[polled_fd]
738 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) `{
739 int in_len
, out_len
, total_len
;
740 struct pollfd
*c_fds
;
743 int first_polled_fd
= -1;
746 in_len
= Array_of_Int_length( in_fds
);
747 out_len
= Array_of_Int_length( out_fds
);
748 total_len
= in_len
+ out_len
;
749 c_fds
= malloc
( sizeof
(struct pollfd
) * total_len
);
752 for ( i
=0; i
<in_len
; i
++ ) {
754 fd
= Array_of_Int__index( in_fds
, i
);
757 c_fds
[i
].events
= POLLIN;
761 for ( i
=0; i
<out_len
; i
++ ) {
763 fd
= Array_of_Int__index( out_fds
, i
);
766 c_fds
[i
].events
= POLLOUT;
769 /* poll all fds
, unlimited timeout
*/
770 result
= poll
( c_fds
, total_len
, -1 );
773 /* analyse results
*/
774 for ( i
=0; i
<total_len
; i
++ )
775 if ( c_fds
[i
].revents
& c_fds
[i
].events
|| /* awaited event
*/
776 c_fds
[i
].revents
& POLLHUP ) /* closed
*/
778 first_polled_fd
= c_fds
[i
].fd
;
782 return Int_as_nullable( first_polled_fd
);
784 else if ( result
< 0 )
785 fprintf
( stderr
, "Error in Stream:poll: %s\n", strerror
( errno
) );
792 # Print `objects
` on the standard output (`stdout
`).
793 protected fun printn(objects: Object...)
795 sys.stdout.write(objects.to_s)
798 # Print an `object
` on the standard output (`stdout
`) and add a newline.
799 protected fun print(object: Object)
801 sys.stdout.write(object.to_s)
802 sys.stdout.write("\n")
805 # Read a character from the standard input (`stdin
`).
806 protected fun getc: Char
808 return sys.stdin.read_char.ascii
811 # Read a line from the standard input (`stdin
`).
812 protected fun gets: String
814 return sys.stdin.read_line
817 # Return the working (current) directory
818 protected fun getcwd: String do return file_getcwd.to_s
819 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"