lib/standard/streams: Added simple error management for streams
[nit.git] / lib / standard / file.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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>
6 #
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
13 # another product.
14
15 # File manipulations (create, read, write, etc.)
16 module file
17
18 intrude import stream
19 intrude import ropes
20 import string_search
21 import time
22
23 in "C Header" `{
24 #include <dirent.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 `}
31
32 # File Abstract Stream
33 abstract class FStream
34 super IOS
35 # The path of the file.
36 var path: nullable String = null
37
38 # The FILE *.
39 private var file: nullable NativeFile = null
40
41 fun file_stat: FileStat do return _file.file_stat
42
43 # File descriptor of this file
44 fun fd: Int do return _file.fileno
45 end
46
47 # File input stream
48 class IFStream
49 super FStream
50 super BufferedIStream
51 super PollableIStream
52 # Misc
53
54 # Open the same file again.
55 # The original path is reused, therefore the reopened file can be a different file.
56 fun reopen
57 do
58 if not eof and not _file.address_is_null then close
59 last_error = null
60 _file = new NativeFile.io_open_read(path.to_cstring)
61 if _file.address_is_null then
62 last_error = new IOError("Error: Opening file at '{path.as(not null)}' failed with '{sys.errno.strerror}'")
63 end_reached = true
64 return
65 end
66 end_reached = false
67 _buffer_pos = 0
68 _buffer.clear
69 end
70
71 redef fun close
72 do
73 if _file.address_is_null then return
74 var i = _file.io_close
75 _buffer.clear
76 end_reached = true
77 end
78
79 redef fun fill_buffer
80 do
81 var nb = _file.io_read(_buffer.items, _buffer.capacity)
82 if nb <= 0 then
83 end_reached = true
84 nb = 0
85 end
86 _buffer.length = nb
87 _buffer_pos = 0
88 end
89
90 # End of file?
91 redef var end_reached: Bool = false
92
93 # Open the file at `path` for reading.
94 init open(path: String)
95 do
96 self.path = path
97 prepare_buffer(10)
98 _file = new NativeFile.io_open_read(path.to_cstring)
99 if _file.address_is_null then
100 last_error = new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
101 end_reached = true
102 end
103 end
104
105 end
106
107 # File output stream
108 class OFStream
109 super FStream
110 super OStream
111
112 redef fun write(s)
113 do
114 if last_error != null then return
115 if not _is_writable then
116 last_error = new IOError("Cannot write to non-writable stream")
117 return
118 end
119 if s isa FlatText then
120 write_native(s.to_cstring, s.length)
121 else
122 for i in s.substrings do write_native(i.to_cstring, i.length)
123 end
124 end
125
126 redef fun close
127 do
128 if _file.address_is_null then
129 if last_error != null then return
130 last_error = new IOError("Cannot close unopened write stream")
131 _is_writable = false
132 return
133 end
134 var i = _file.io_close
135 if i != 0 then
136 last_error = new IOError("Close failed due to error {sys.errno.strerror}")
137 end
138 _is_writable = false
139 end
140 redef var is_writable = false
141
142 # Write `len` bytes from `native`.
143 private fun write_native(native: NativeString, len: Int)
144 do
145 if last_error != null then return
146 if not _is_writable then
147 last_error = new IOError("Cannot write to non-writable stream")
148 return
149 end
150 if _file.address_is_null then
151 last_error = new IOError("Writing on a null stream")
152 _is_writable = false
153 return
154 end
155 var err = _file.io_write(native, len)
156 if err != len then
157 # Big problem
158 last_error = new IOError("Problem in writing : {err} {len} \n")
159 end
160 end
161
162 # Open the file at `path` for writing.
163 init open(path: String)
164 do
165 _file = new NativeFile.io_open_write(path.to_cstring)
166 self.path = path
167 _is_writable = true
168 if _file.address_is_null then
169 last_error = new IOError("Error: Opening file at '{path}' failed with '{sys.errno.strerror}'")
170 is_writable = false
171 end
172 end
173 end
174
175 ###############################################################################
176
177 class Stdin
178 super IFStream
179
180 init do
181 _file = new NativeFile.native_stdin
182 path = "/dev/stdin"
183 prepare_buffer(1)
184 end
185
186 redef fun poll_in: Bool is extern "file_stdin_poll_in"
187 end
188
189 class Stdout
190 super OFStream
191 init do
192 _file = new NativeFile.native_stdout
193 path = "/dev/stdout"
194 _is_writable = true
195 end
196 end
197
198 class Stderr
199 super OFStream
200 init do
201 _file = new NativeFile.native_stderr
202 path = "/dev/stderr"
203 _is_writable = true
204 end
205 end
206
207 ###############################################################################
208
209 redef class Streamable
210 # Like `write_to` but take care of creating the file
211 fun write_to_file(filepath: String)
212 do
213 var stream = new OFStream.open(filepath)
214 write_to(stream)
215 stream.close
216 end
217 end
218
219 redef class String
220 # return true if a file with this names exists
221 fun file_exists: Bool do return to_cstring.file_exists
222
223 # The status of a file. see POSIX stat(2).
224 fun file_stat: FileStat do return to_cstring.file_stat
225
226 # The status of a file or of a symlink. see POSIX lstat(2).
227 fun file_lstat: FileStat do return to_cstring.file_lstat
228
229 # Remove a file, return true if success
230 fun file_delete: Bool do return to_cstring.file_delete
231
232 # Copy content of file at `self` to `dest`
233 fun file_copy_to(dest: String)
234 do
235 var input = new IFStream.open(self)
236 var output = new OFStream.open(dest)
237
238 while not input.eof do
239 var buffer = input.read(1024)
240 output.write buffer
241 end
242
243 input.close
244 output.close
245 end
246
247 # Remove the trailing extension `ext`.
248 #
249 # `ext` usually starts with a dot but could be anything.
250 #
251 # assert "file.txt".strip_extension(".txt") == "file"
252 # assert "file.txt".strip_extension("le.txt") == "fi"
253 # assert "file.txt".strip_extension("xt") == "file.t"
254 #
255 # if `ext` is not present, `self` is returned unmodified.
256 #
257 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
258 fun strip_extension(ext: String): String
259 do
260 if has_suffix(ext) then
261 return substring(0, length - ext.length)
262 end
263 return self
264 end
265
266 # Extract the basename of a path and remove the extension
267 #
268 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
269 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
270 # assert "path/to".basename(".ext") == "to"
271 # assert "path/to/".basename(".ext") == "to"
272 # assert "path".basename("") == "path"
273 # assert "/path".basename("") == "path"
274 # assert "/".basename("") == "/"
275 # assert "".basename("") == ""
276 fun basename(ext: String): String
277 do
278 var l = length - 1 # Index of the last char
279 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
280 if l == 0 then return "/"
281 var pos = chars.last_index_of_from('/', l)
282 var n = self
283 if pos >= 0 then
284 n = substring(pos+1, l-pos)
285 end
286 return n.strip_extension(ext)
287 end
288
289 # Extract the dirname of a path
290 #
291 # assert "/path/to/a_file.ext".dirname == "/path/to"
292 # assert "path/to/a_file.ext".dirname == "path/to"
293 # assert "path/to".dirname == "path"
294 # assert "path/to/".dirname == "path"
295 # assert "path".dirname == "."
296 # assert "/path".dirname == "/"
297 # assert "/".dirname == "/"
298 # assert "".dirname == "."
299 fun dirname: String
300 do
301 var l = length - 1 # Index of the last char
302 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
303 var pos = chars.last_index_of_from('/', l)
304 if pos > 0 then
305 return substring(0, pos)
306 else if pos == 0 then
307 return "/"
308 else
309 return "."
310 end
311 end
312
313 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
314 fun realpath: String do
315 var cs = to_cstring.file_realpath
316 var res = cs.to_s_with_copy
317 # cs.free_malloc # FIXME memory leak
318 return res
319 end
320
321 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
322 # ".." are not resolved if they start the path
323 # starting "/" is not removed
324 # trainling "/" is removed
325 #
326 # Note that the method only wonrk on the string:
327 # * no I/O access is performed
328 # * the validity of the path is not checked
329 #
330 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
331 # assert "../dir/file".simplify_path == "../dir/file"
332 # assert "dir/../../".simplify_path == ".."
333 # assert "dir/..".simplify_path == "."
334 # assert "//absolute//path/".simplify_path == "/absolute/path"
335 # assert "//absolute//../".simplify_path == "/"
336 fun simplify_path: String
337 do
338 var a = self.split_with("/")
339 var a2 = new Array[String]
340 for x in a do
341 if x == "." then continue
342 if x == "" and not a2.is_empty then continue
343 if x == ".." and not a2.is_empty and a2.last != ".." then
344 a2.pop
345 continue
346 end
347 a2.push(x)
348 end
349 if a2.is_empty then return "."
350 if a2.length == 1 and a2.first == "" then return "/"
351 return a2.join("/")
352 end
353
354 # Correctly join two path using the directory separator.
355 #
356 # Using a standard "{self}/{path}" does not work in the following cases:
357 #
358 # * `self` is empty.
359 # * `path` ends with `'/'`.
360 # * `path` starts with `'/'`.
361 #
362 # This method ensures that the join is valid.
363 #
364 # assert "hello".join_path("world") == "hello/world"
365 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
366 # assert "".join_path("world") == "world"
367 # assert "hello".join_path("/world") == "/world"
368 # assert "hello/".join_path("world") == "hello/world"
369 # assert "hello/".join_path("/world") == "/world"
370 #
371 # Note: You may want to use `simplify_path` on the result.
372 #
373 # Note: This method works only with POSIX paths.
374 fun join_path(path: String): String
375 do
376 if path.is_empty then return self
377 if self.is_empty then return path
378 if path.chars[0] == '/' then return path
379 if self.last == '/' then return "{self}{path}"
380 return "{self}/{path}"
381 end
382
383 # Convert the path (`self`) to a program name.
384 #
385 # Ensure the path (`self`) will be treated as-is by POSIX shells when it is
386 # used as a program name. In order to do that, prepend `./` if needed.
387 #
388 # assert "foo".to_program_name == "./foo"
389 # assert "/foo".to_program_name == "/foo"
390 # assert "".to_program_name == "./" # At least, your shell will detect the error.
391 fun to_program_name: String do
392 if self.has_prefix("/") then
393 return self
394 else
395 return "./{self}"
396 end
397 end
398
399 # Alias for `join_path`
400 #
401 # assert "hello" / "world" == "hello/world"
402 # assert "hel/lo" / "wor/ld" == "hel/lo/wor/ld"
403 # assert "" / "world" == "world"
404 # assert "/hello" / "/world" == "/world"
405 #
406 # This operator is quite useful for chaining changes of path.
407 # The next one being relative to the previous one.
408 #
409 # var a = "foo"
410 # var b = "/bar"
411 # var c = "baz/foobar"
412 # assert a/b/c == "/bar/baz/foobar"
413 fun /(path: String): String do return join_path(path)
414
415 # Returns the relative path needed to go from `self` to `dest`.
416 #
417 # assert "/foo/bar".relpath("/foo/baz") == "../baz"
418 # assert "/foo/bar".relpath("/baz/bar") == "../../baz/bar"
419 #
420 # If `self` or `dest` is relative, they are considered relatively to `getcwd`.
421 #
422 # In some cases, the result is still independent of the current directory:
423 #
424 # assert "foo/bar".relpath("..") == "../../.."
425 #
426 # In other cases, parts of the current directory may be exhibited:
427 #
428 # var p = "../foo/bar".relpath("baz")
429 # var c = getcwd.basename("")
430 # assert p == "../../{c}/baz"
431 #
432 # For path resolution independent of the current directory (eg. for paths in URL),
433 # or to use an other starting directory than the current directory,
434 # just force absolute paths:
435 #
436 # var start = "/a/b/c/d"
437 # var p2 = (start/"../foo/bar").relpath(start/"baz")
438 # assert p2 == "../../d/baz"
439 #
440 #
441 # Neither `self` or `dest` has to be real paths or to exist in directories since
442 # the resolution is only done with string manipulations and without any access to
443 # the underlying file system.
444 #
445 # If `self` and `dest` are the same directory, the empty string is returned:
446 #
447 # assert "foo".relpath("foo") == ""
448 # assert "foo/../bar".relpath("bar") == ""
449 #
450 # The empty string and "." designate both the current directory:
451 #
452 # assert "".relpath("foo/bar") == "foo/bar"
453 # assert ".".relpath("foo/bar") == "foo/bar"
454 # assert "foo/bar".relpath("") == "../.."
455 # assert "/" + "/".relpath(".") == getcwd
456 fun relpath(dest: String): String
457 do
458 var cwd = getcwd
459 var from = (cwd/self).simplify_path.split("/")
460 if from.last.is_empty then from.pop # case for the root directory
461 var to = (cwd/dest).simplify_path.split("/")
462 if to.last.is_empty then to.pop # case for the root directory
463
464 # Remove common prefixes
465 while not from.is_empty and not to.is_empty and from.first == to.first do
466 from.shift
467 to.shift
468 end
469
470 # Result is going up in `from` with ".." then going down following `to`
471 var from_len = from.length
472 if from_len == 0 then return to.join("/")
473 var up = "../"*(from_len-1) + ".."
474 if to.is_empty then return up
475 var res = up + "/" + to.join("/")
476 return res
477 end
478
479 # Create a directory (and all intermediate directories if needed)
480 fun mkdir
481 do
482 var dirs = self.split_with("/")
483 var path = new FlatBuffer
484 if dirs.is_empty then return
485 if dirs[0].is_empty then
486 # it was a starting /
487 path.add('/')
488 end
489 for d in dirs do
490 if d.is_empty then continue
491 path.append(d)
492 path.add('/')
493 path.to_s.to_cstring.file_mkdir
494 end
495 end
496
497 # Delete a directory and all of its content, return `true` on success
498 #
499 # Does not go through symbolic links and may get stuck in a cycle if there
500 # is a cycle in the filesystem.
501 fun rmdir: Bool
502 do
503 var ok = true
504 for file in self.files do
505 var file_path = self.join_path(file)
506 var stat = file_path.file_lstat
507 if stat.is_dir then
508 ok = file_path.rmdir and ok
509 else
510 ok = file_path.file_delete and ok
511 end
512 stat.free
513 end
514
515 # Delete the directory itself
516 if ok then to_cstring.rmdir
517
518 return ok
519 end
520
521 # Change the current working directory
522 #
523 # "/etc".chdir
524 # assert getcwd == "/etc"
525 # "..".chdir
526 # assert getcwd == "/"
527 #
528 # TODO: errno
529 fun chdir do to_cstring.file_chdir
530
531 # Return right-most extension (without the dot)
532 #
533 # Only the last extension is returned.
534 # There is no special case for combined extensions.
535 #
536 # assert "file.txt".file_extension == "txt"
537 # assert "file.tar.gz".file_extension == "gz"
538 #
539 # For file without extension, `null` is returned.
540 # Hoever, for trailing dot, `""` is returned.
541 #
542 # assert "file".file_extension == null
543 # assert "file.".file_extension == ""
544 #
545 # The starting dot of hidden files is never considered.
546 #
547 # assert ".file.txt".file_extension == "txt"
548 # assert ".file".file_extension == null
549 fun file_extension: nullable String
550 do
551 var last_slash = chars.last_index_of('.')
552 if last_slash > 0 then
553 return substring( last_slash+1, length )
554 else
555 return null
556 end
557 end
558
559 # returns files contained within the directory represented by self
560 fun files : Set[ String ] is extern import HashSet[String], HashSet[String].add, NativeString.to_s, String.to_cstring, HashSet[String].as(Set[String]) `{
561 char *dir_path;
562 DIR *dir;
563
564 dir_path = String_to_cstring( recv );
565 if ((dir = opendir(dir_path)) == NULL)
566 {
567 perror( dir_path );
568 exit( 1 );
569 }
570 else
571 {
572 HashSet_of_String results;
573 String file_name;
574 struct dirent *de;
575
576 results = new_HashSet_of_String();
577
578 while ( ( de = readdir( dir ) ) != NULL )
579 if ( strcmp( de->d_name, ".." ) != 0 &&
580 strcmp( de->d_name, "." ) != 0 )
581 {
582 file_name = NativeString_to_s( strdup( de->d_name ) );
583 HashSet_of_String_add( results, file_name );
584 }
585
586 closedir( dir );
587 return HashSet_of_String_as_Set_of_String( results );
588 }
589 `}
590 end
591
592 redef class NativeString
593 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
594 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
595 private fun file_lstat: FileStat `{
596 struct stat* stat_element;
597 int res;
598 stat_element = malloc(sizeof(struct stat));
599 res = lstat(recv, stat_element);
600 if (res == -1) return NULL;
601 return stat_element;
602 `}
603 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
604 private fun rmdir: Bool `{ return rmdir(recv); `}
605 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
606 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
607 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
608 end
609
610 # This class is system dependent ... must reify the vfs
611 extern class FileStat `{ struct stat * `}
612 # Returns the permission bits of file
613 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
614 # Returns the last access time
615 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
616 # Returns the last status change time
617 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
618 # Returns the last modification time
619 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
620 # Returns the size
621 fun size: Int is extern "file_FileStat_FileStat_size_0"
622
623 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
624 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
625 # Returns true if it is a directory
626 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
627 # Returns true if it is a character device
628 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
629 # Returns true if it is a block device
630 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
631 # Returns true if the type is fifo
632 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
633 # Returns true if the type is a link
634 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
635 # Returns true if the type is a socket
636 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
637 end
638
639 # Instance of this class are standard FILE * pointers
640 private extern class NativeFile `{ FILE* `}
641 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
642 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
643 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
644 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
645 fun fileno: Int `{ return fileno(recv); `}
646
647 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
648 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
649 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
650 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
651 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
652 end
653
654 redef class Sys
655
656 # Standard input
657 var stdin: PollableIStream = new Stdin is protected writable
658
659 # Standard output
660 var stdout: OStream = new Stdout is protected writable
661
662 # Standard output for errors
663 var stderr: OStream = new Stderr is protected writable
664
665 end
666
667 # Print `objects` on the standard output (`stdout`).
668 protected fun printn(objects: Object...)
669 do
670 sys.stdout.write(objects.to_s)
671 end
672
673 # Print an `object` on the standard output (`stdout`) and add a newline.
674 protected fun print(object: Object)
675 do
676 sys.stdout.write(object.to_s)
677 sys.stdout.write("\n")
678 end
679
680 # Read a character from the standard input (`stdin`).
681 protected fun getc: Char
682 do
683 return sys.stdin.read_char.ascii
684 end
685
686 # Read a line from the standard input (`stdin`).
687 protected fun gets: String
688 do
689 return sys.stdin.read_line
690 end
691
692 # Return the working (current) directory
693 protected fun getcwd: String do return file_getcwd.to_s
694 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"