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.)
27 #include <sys/types.h>
33 # File Abstract Stream
34 abstract class FStream
36 # The path of the file.
37 var path
: nullable String = null
40 private var file
: nullable NativeFile = null
42 # The status of a file. see POSIX stat(2).
43 fun file_stat
: NativeFileStat 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.
61 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
76 var nb
= _file
.io_read
(_buffer
.items
, _buffer
.capacity
)
86 redef var end_reached
: Bool = false
88 # Open the file at `path` for reading.
89 init open
(path
: String)
93 _file
= new NativeFile.io_open_read
(path
.to_cstring
)
94 assert not _file
.address_is_null
else
95 print
"Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
109 if s
isa FlatText then
110 write_native
(s
.to_cstring
, s
.length
)
112 for i
in s
.substrings
do write_native
(i
.to_cstring
, i
.length
)
122 redef var is_writable
= false
124 # Write `len` bytes from `native`.
125 private fun write_native
(native
: NativeString, len
: Int)
128 var err
= _file
.io_write
(native
, len
)
131 printn
("Problem in writing : ", err
, " ", len
, "\n")
135 # Open the file at `path` for writing.
136 init open
(path
: String)
138 _file
= new NativeFile.io_open_write
(path
.to_cstring
)
139 assert not _file
.address_is_null
else
140 print
"Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
147 ###############################################################################
149 # Standard input stream.
154 _file
= new NativeFile.native_stdin
159 redef fun poll_in
: Bool is extern "file_stdin_poll_in"
162 # Standard output stream.
166 _file
= new NativeFile.native_stdout
172 # Standard error stream.
176 _file
= new NativeFile.native_stderr
182 ###############################################################################
184 redef class Streamable
185 # Like `write_to` but take care of creating the file
186 fun write_to_file
(filepath
: String)
188 var stream
= new OFStream.open
(filepath
)
194 # Utility class to access file system services
196 # Usually created with `Text::to_path`.
199 private var path
: String
202 redef fun to_s
do return path
204 # Name of the file name at `to_s`
207 # var path = "/tmp/somefile".to_path
208 # assert path.filename == "somefile"
210 var filename
: String = path
.basename
("") is lazy
212 # Does the file at `path` exists?
213 fun exists
: Bool do return stat
!= null
215 # Information on the file at `self` following symbolic links
217 # Returns `null` if there is no file at `self`.
220 # var p = "/tmp/".to_path
222 # if stat != null then # Does `p` exist?
223 # print "It's size is {stat.size}"
224 # if stat.is_dir then print "It's a directory"
227 fun stat
: nullable FileStat
229 var stat
= path
.to_cstring
.file_stat
230 if stat
.address_is_null
then return null
231 return new FileStat(stat
)
234 # Information on the file or link at `self`
236 # Do not follow symbolic links.
237 fun link_stat
: nullable FileStat
239 var stat
= path
.to_cstring
.file_lstat
240 if stat
.address_is_null
then return null
241 return new FileStat(stat
)
244 # Delete a file from the file system, return `true` on success
247 fun delete
: Bool do return path
.to_cstring
.file_delete
249 # Copy content of file at `path` to `dest`
255 var output
= dest
.open_wo
257 while not input
.eof
do
258 var buffer
= input
.read
(1024)
266 # Open this file for reading
268 # Require: `exists and not link_stat.is_dir`
269 fun open_ro
: IFStream
271 # TODO manage streams error when they are merged
272 return new IFStream.open
(path
)
275 # Open this file for writing
277 # Require: `not exists or not stat.is_dir`
278 fun open_wo
: OFStream
280 # TODO manage streams error when they are merged
281 return new OFStream.open
(path
)
284 # Lists the name of the files contained within the directory at `path`
286 # Require: `exists and is_dir`
287 fun files
: Array[Path]
289 var files
= new Array[Path]
290 for filename
in path
.files
do
291 files
.add
new Path(path
/ filename
)
296 # Delete a directory and all of its content, return `true` on success
298 # Does not go through symbolic links and may get stuck in a cycle if there
299 # is a cycle in the file system.
303 for file
in self.files
do
304 var stat
= file
.link_stat
306 ok
= file
.rmdir
and ok
308 ok
= file
.delete
and ok
312 # Delete the directory itself
313 if ok
then path
.to_cstring
.rmdir
318 redef fun ==(other
) do return other
isa Path and path
.simplify_path
== other
.path
.simplify_path
319 redef fun hash
do return path
.simplify_path
.hash
322 # Information on a file
324 # Created by `Path::stat` and `Path::link_stat`.
326 # The information within this class is gathered when the instance is initialized
327 # it will not be updated if the targeted file is modified.
333 # The low-level status of a file
336 private var stat
: NativeFileStat
338 private var finalized
= false
342 if not finalized
then
348 # Returns the last access time in seconds since Epoch
349 fun last_access_time
: Int
355 # Returns the last modification time in seconds since Epoch
356 fun last_modification_time
: Int
362 # Size of the file at `path`
369 # Is this a regular file and not a device file, pipe, socket, etc.?
376 # Is this a directory?
383 # Is this a symbolic link?
390 # FIXME Make the following POSIX only? or implement in some other way on Windows
392 # Returns the last status change time in seconds since Epoch
393 fun last_status_change_time
: Int
399 # Returns the permission bits of file
406 # Is this a character device?
413 # Is this a block device?
420 # Is this a FIFO pipe?
427 # Is this a UNIX socket
436 # Access file system related services on the path at `self`
437 fun to_path
: Path do return new Path(to_s
)
441 # return true if a file with this names exists
442 fun file_exists
: Bool do return to_cstring
.file_exists
444 # The status of a file. see POSIX stat(2).
445 fun file_stat
: NativeFileStat do return to_cstring
.file_stat
447 # The status of a file or of a symlink. see POSIX lstat(2).
448 fun file_lstat
: NativeFileStat do return to_cstring
.file_lstat
450 # Remove a file, return true if success
451 fun file_delete
: Bool do return to_cstring
.file_delete
453 # Copy content of file at `self` to `dest`
454 fun file_copy_to
(dest
: String) do to_path
.copy
(dest
.to_path
)
456 # Remove the trailing extension `ext`.
458 # `ext` usually starts with a dot but could be anything.
460 # assert "file.txt".strip_extension(".txt") == "file"
461 # assert "file.txt".strip_extension("le.txt") == "fi"
462 # assert "file.txt".strip_extension("xt") == "file.t"
464 # if `ext` is not present, `self` is returned unmodified.
466 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
467 fun strip_extension
(ext
: String): String
469 if has_suffix
(ext
) then
470 return substring
(0, length
- ext
.length
)
475 # Extract the basename of a path and remove the extension
477 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
478 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
479 # assert "path/to".basename(".ext") == "to"
480 # assert "path/to/".basename(".ext") == "to"
481 # assert "path".basename("") == "path"
482 # assert "/path".basename("") == "path"
483 # assert "/".basename("") == "/"
484 # assert "".basename("") == ""
485 fun basename
(ext
: String): String
487 var l
= length
- 1 # Index of the last char
488 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
489 if l
== 0 then return "/"
490 var pos
= chars
.last_index_of_from
('/', l
)
493 n
= substring
(pos
+1, l-pos
)
495 return n
.strip_extension
(ext
)
498 # Extract the dirname of a path
500 # assert "/path/to/a_file.ext".dirname == "/path/to"
501 # assert "path/to/a_file.ext".dirname == "path/to"
502 # assert "path/to".dirname == "path"
503 # assert "path/to/".dirname == "path"
504 # assert "path".dirname == "."
505 # assert "/path".dirname == "/"
506 # assert "/".dirname == "/"
507 # assert "".dirname == "."
510 var l
= length
- 1 # Index of the last char
511 while l
> 0 and self.chars
[l
] == '/' do l
-= 1 # remove all trailing `/`
512 var pos
= chars
.last_index_of_from
('/', l
)
514 return substring
(0, pos
)
515 else if pos
== 0 then
522 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
523 fun realpath
: String do
524 var cs
= to_cstring
.file_realpath
525 var res
= cs
.to_s_with_copy
526 # cs.free_malloc # FIXME memory leak
530 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
531 # ".." are not resolved if they start the path
532 # starting "/" is not removed
533 # trainling "/" is removed
535 # Note that the method only wonrk on the string:
536 # * no I/O access is performed
537 # * the validity of the path is not checked
539 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
540 # assert "../dir/file".simplify_path == "../dir/file"
541 # assert "dir/../../".simplify_path == ".."
542 # assert "dir/..".simplify_path == "."
543 # assert "//absolute//path/".simplify_path == "/absolute/path"
544 # assert "//absolute//../".simplify_path == "/"
545 fun simplify_path
: String
547 var a
= self.split_with
("/")
548 var a2
= new Array[String]
550 if x
== "." then continue
551 if x
== "" and not a2
.is_empty
then continue
552 if x
== ".." and not a2
.is_empty
and a2
.last
!= ".." then
558 if a2
.is_empty
then return "."
559 if a2
.length
== 1 and a2
.first
== "" then return "/"
563 # Correctly join two path using the directory separator.
565 # Using a standard "{self}/{path}" does not work in the following cases:
568 # * `path` ends with `'/'`.
569 # * `path` starts with `'/'`.
571 # This method ensures that the join is valid.
573 # assert "hello".join_path("world") == "hello/world"
574 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
575 # assert "".join_path("world") == "world"
576 # assert "hello".join_path("/world") == "/world"
577 # assert "hello/".join_path("world") == "hello/world"
578 # assert "hello/".join_path("/world") == "/world"
580 # Note: You may want to use `simplify_path` on the result.
582 # Note: This method works only with POSIX paths.
583 fun join_path
(path
: String): String
585 if path
.is_empty
then return self
586 if self.is_empty
then return path
587 if path
.chars
[0] == '/' then return path
588 if self.last
== '/' then return "{self}{path}"
589 return "{self}/{path}"
592 # Convert the path (`self`) to a program name.
594 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
595 # used as a program name. In order to do that, prepend `./` if needed.
597 # assert "foo".to_program_name == "./foo"
598 # assert "/foo".to_program_name == "/foo"
599 # assert "".to_program_name == "./" # At least, your shell will detect the error.
600 fun to_program_name
: String do
601 if self.has_prefix
("/") then
608 # Alias for `join_path`
610 # assert "hello" / "world" == "hello/world"
611 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
612 # assert "" / "world" == "world"
613 # assert "/hello" / "/world" == "/world"
615 # This operator is quite useful for chaining changes of path.
616 # The next one being relative to the previous one.
620 # var c = "baz/foobar"
621 # assert a/b/c == "/bar/baz/foobar"
622 fun /(path
: String): String do return join_path
(path
)
624 # Returns the relative path needed to go from `self` to `dest`.
626 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
627 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
629 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
631 # In some cases, the result is still independent of the current directory:
633 # assert "foo/bar".relpath("..") == "../../.."
635 # In other cases, parts of the current directory may be exhibited:
637 # var p = "../foo/bar".relpath("baz")
638 # var c = getcwd.basename("")
639 # assert p == "../../{c}/baz"
641 # For path resolution independent of the current directory (eg. for paths in URL),
642 # or to use an other starting directory than the current directory,
643 # just force absolute paths:
645 # var start = "/a/b/c/d"
646 # var p2 = (start/"../foo/bar").relpath(start/"baz")
647 # assert p2 == "../../d/baz"
650 # Neither `self` or `dest` has to be real paths or to exist in directories since
651 # the resolution is only done with string manipulations and without any access to
652 # the underlying file system.
654 # If `self` and `dest` are the same directory, the empty string is returned:
656 # assert "foo".relpath("foo") == ""
657 # assert "foo/../bar".relpath("bar") == ""
659 # The empty string and "." designate both the current directory:
661 # assert "".relpath("foo/bar") == "foo/bar"
662 # assert ".".relpath("foo/bar") == "foo/bar"
663 # assert "foo/bar".relpath("") == "../.."
664 # assert "/" + "/".relpath(".") == getcwd
665 fun relpath
(dest
: String): String
668 var from
= (cwd
/self).simplify_path
.split
("/")
669 if from
.last
.is_empty
then from
.pop
# case for the root directory
670 var to
= (cwd
/dest
).simplify_path
.split
("/")
671 if to
.last
.is_empty
then to
.pop
# case for the root directory
673 # Remove common prefixes
674 while not from
.is_empty
and not to
.is_empty
and from
.first
== to
.first
do
679 # Result is going up in `from` with ".." then going down following `to`
680 var from_len
= from
.length
681 if from_len
== 0 then return to
.join
("/")
682 var up
= "../"*(from_len-1
) + ".."
683 if to
.is_empty
then return up
684 var res
= up
+ "/" + to
.join
("/")
688 # Create a directory (and all intermediate directories if needed)
691 var dirs
= self.split_with
("/")
692 var path
= new FlatBuffer
693 if dirs
.is_empty
then return
694 if dirs
[0].is_empty
then
695 # it was a starting /
699 if d
.is_empty
then continue
702 path
.to_s
.to_cstring
.file_mkdir
706 # Delete a directory and all of its content, return `true` on success
708 # Does not go through symbolic links and may get stuck in a cycle if there
709 # is a cycle in the filesystem.
710 fun rmdir
: Bool do return to_path
.rmdir
712 # Change the current working directory
715 # assert getcwd == "/etc"
717 # assert getcwd == "/"
720 fun chdir
do to_cstring
.file_chdir
722 # Return right-most extension (without the dot)
724 # Only the last extension is returned.
725 # There is no special case for combined extensions.
727 # assert "file.txt".file_extension == "txt"
728 # assert "file.tar.gz".file_extension == "gz"
730 # For file without extension, `null` is returned.
731 # Hoever, for trailing dot, `""` is returned.
733 # assert "file".file_extension == null
734 # assert "file.".file_extension == ""
736 # The starting dot of hidden files is never considered.
738 # assert ".file.txt".file_extension == "txt"
739 # assert ".file".file_extension == null
740 fun file_extension
: nullable String
742 var last_slash
= chars
.last_index_of
('.')
743 if last_slash
> 0 then
744 return substring
( last_slash
+1, length
)
750 # returns files contained within the directory represented by self
751 fun files
: Array[String] is extern import Array[String], Array[String].add
, NativeString.to_s
, String.to_cstring
`{
755 dir_path = String_to_cstring( recv );
756 if ((dir = opendir(dir_path)) == NULL)
763 Array_of_String results;
767 results = new_Array_of_String();
769 while ( ( de = readdir( dir ) ) != NULL )
770 if ( strcmp( de->d_name, ".." ) != 0 &&
771 strcmp( de->d_name, "." ) != 0 )
773 file_name = NativeString_to_s( strdup( de->d_name ) );
774 Array_of_String_add( results, file_name );
783 redef class NativeString
784 private fun file_exists
: Bool is extern "string_NativeString_NativeString_file_exists_0"
785 private fun file_stat
: NativeFileStat is extern "string_NativeString_NativeString_file_stat_0"
786 private fun file_lstat
: NativeFileStat `{
787 struct stat* stat_element;
789 stat_element = malloc(sizeof(struct stat));
790 res = lstat(recv, stat_element);
791 if (res == -1) return NULL;
794 private fun file_mkdir
: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
795 private fun rmdir
: Bool `{ return rmdir(recv); `}
796 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
797 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
798 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
801 # This class is system dependent ... must reify the vfs
802 extern class NativeFileStat `{ struct stat * `}
803 # Returns the permission bits of file
804 fun mode
: Int is extern "file_FileStat_FileStat_mode_0"
805 # Returns the last access time
806 fun atime
: Int is extern "file_FileStat_FileStat_atime_0"
807 # Returns the last status change time
808 fun ctime
: Int is extern "file_FileStat_FileStat_ctime_0"
809 # Returns the last modification time
810 fun mtime
: Int is extern "file_FileStat_FileStat_mtime_0"
812 fun size
: Int is extern "file_FileStat_FileStat_size_0"
814 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
815 fun is_reg
: Bool `{ return S_ISREG(recv->st_mode); `}
816 # Returns true if it is a directory
817 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
818 # Returns true if it is a character device
819 fun is_chr
: Bool `{ return S_ISCHR(recv->st_mode); `}
820 # Returns true if it is a block device
821 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
822 # Returns true if the type is fifo
823 fun is_fifo
: Bool `{ return S_ISFIFO(recv->st_mode); `}
824 # Returns true if the type is a link
825 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
826 # Returns true if the type is a socket
827 fun is_sock
: Bool `{ return S_ISSOCK(recv->st_mode); `}
830 # Instance of this class are standard FILE * pointers
831 private extern class NativeFile `{ FILE* `}
832 fun io_read
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
833 fun io_write
(buf
: NativeString, len
: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
834 fun io_close
: Int is extern "file_NativeFile_NativeFile_io_close_0"
835 fun file_stat
: NativeFileStat is extern "file_NativeFile_NativeFile_file_stat_0"
836 fun fileno
: Int `{ return fileno(recv); `}
838 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
839 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
840 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
841 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
842 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
848 var stdin: PollableIStream = new Stdin is protected writable
851 var stdout: OStream = new Stdout is protected writable
853 # Standard output for errors
854 var stderr: OStream = new Stderr is protected writable
858 # Print `objects
` on the standard output (`stdout
`).
859 protected fun printn(objects: Object...)
861 sys.stdout.write(objects.to_s)
864 # Print an `object
` on the standard output (`stdout
`) and add a newline.
865 protected fun print(object: Object)
867 sys.stdout.write(object.to_s)
868 sys.stdout.write("\n")
871 # Read a character from the standard input (`stdin
`).
872 protected fun getc: Char
874 return sys.stdin.read_char.ascii
877 # Read a line from the standard input (`stdin
`).
878 protected fun gets: String
880 return sys.stdin.read_line
883 # Return the working (current) directory
884 protected fun getcwd: String do return file_getcwd.to_s
885 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"