lib: intro `FStream::fd`
[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 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 # Misc
52
53 # Open the same file again.
54 # The original path is reused, therefore the reopened file can be a different file.
55 fun reopen
56 do
57 if not eof then close
58 _file = new NativeFile.io_open_read(path.to_cstring)
59 end_reached = false
60 _buffer_pos = 0
61 _buffer.clear
62 end
63
64 redef fun close
65 do
66 var i = _file.io_close
67 end_reached = true
68 end
69
70 redef fun fill_buffer
71 do
72 var nb = _file.io_read(_buffer.items, _buffer.capacity)
73 if nb <= 0 then
74 end_reached = true
75 nb = 0
76 end
77 _buffer.length = nb
78 _buffer_pos = 0
79 end
80
81 # End of file?
82 redef var end_reached: Bool = false
83
84 # Open the file at `path` for reading.
85 init open(path: String)
86 do
87 self.path = path
88 prepare_buffer(10)
89 _file = new NativeFile.io_open_read(path.to_cstring)
90 assert not _file.address_is_null else
91 print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
92 end
93 end
94
95 private init do end
96 private init without_file do end
97 end
98
99 # File output stream
100 class OFStream
101 super FStream
102 super OStream
103
104 redef fun write(s)
105 do
106 assert _writable
107 if s isa FlatText then
108 write_native(s.to_cstring, s.length)
109 else
110 for i in s.substrings do write_native(i.to_cstring, i.length)
111 end
112 end
113
114 redef fun is_writable do return _writable
115
116 redef fun close
117 do
118 var i = _file.io_close
119 _writable = false
120 end
121
122 # Is the file open in write mode
123 var _writable: Bool
124
125 # Write `len` bytes from `native`.
126 private fun write_native(native: NativeString, len: Int)
127 do
128 assert _writable
129 var err = _file.io_write(native, len)
130 if err != len then
131 # Big problem
132 printn("Problem in writing : ", err, " ", len, "\n")
133 end
134 end
135
136 # Open the file at `path` for writing.
137 init open(path: String)
138 do
139 _file = new NativeFile.io_open_write(path.to_cstring)
140 assert not _file.address_is_null else
141 print "Error: Opening file at '{path}' failed with '{sys.errno.strerror}'"
142 end
143 self.path = path
144 _writable = true
145 end
146
147 private init do end
148 private init without_file do end
149 end
150
151 ###############################################################################
152
153 class Stdin
154 super IFStream
155 super PollableIStream
156
157 private init do
158 _file = new NativeFile.native_stdin
159 path = "/dev/stdin"
160 prepare_buffer(1)
161 end
162
163 redef fun poll_in: Bool is extern "file_stdin_poll_in"
164 end
165
166 class Stdout
167 super OFStream
168 private init do
169 _file = new NativeFile.native_stdout
170 path = "/dev/stdout"
171 _writable = true
172 end
173 end
174
175 class Stderr
176 super OFStream
177 private init do
178 _file = new NativeFile.native_stderr
179 path = "/dev/stderr"
180 _writable = true
181 end
182 end
183
184 ###############################################################################
185
186 redef class Streamable
187 # Like `write_to` but take care of creating the file
188 fun write_to_file(filepath: String)
189 do
190 var stream = new OFStream.open(filepath)
191 write_to(stream)
192 stream.close
193 end
194 end
195
196 redef class String
197 # return true if a file with this names exists
198 fun file_exists: Bool do return to_cstring.file_exists
199
200 # The status of a file. see POSIX stat(2).
201 fun file_stat: FileStat do return to_cstring.file_stat
202
203 # The status of a file or of a symlink. see POSIX lstat(2).
204 fun file_lstat: FileStat do return to_cstring.file_lstat
205
206 # Remove a file, return true if success
207 fun file_delete: Bool do return to_cstring.file_delete
208
209 # Copy content of file at `self` to `dest`
210 fun file_copy_to(dest: String)
211 do
212 var input = new IFStream.open(self)
213 var output = new OFStream.open(dest)
214
215 while not input.eof do
216 var buffer = input.read(1024)
217 output.write buffer
218 end
219
220 input.close
221 output.close
222 end
223
224 # Remove the trailing extension `ext`.
225 #
226 # `ext` usually starts with a dot but could be anything.
227 #
228 # assert "file.txt".strip_extension(".txt") == "file"
229 # assert "file.txt".strip_extension("le.txt") == "fi"
230 # assert "file.txt".strip_extension("xt") == "file.t"
231 #
232 # if `ext` is not present, `self` is returned unmodified.
233 #
234 # assert "file.txt".strip_extension(".tar.gz") == "file.txt"
235 fun strip_extension(ext: String): String
236 do
237 if has_suffix(ext) then
238 return substring(0, length - ext.length)
239 end
240 return self
241 end
242
243 # Extract the basename of a path and remove the extension
244 #
245 # assert "/path/to/a_file.ext".basename(".ext") == "a_file"
246 # assert "path/to/a_file.ext".basename(".ext") == "a_file"
247 # assert "path/to".basename(".ext") == "to"
248 # assert "path/to/".basename(".ext") == "to"
249 # assert "path".basename("") == "path"
250 # assert "/path".basename("") == "path"
251 # assert "/".basename("") == "/"
252 # assert "".basename("") == ""
253 fun basename(ext: String): String
254 do
255 var l = length - 1 # Index of the last char
256 while l > 0 and self.chars[l] == '/' do l -= 1 # remove all trailing `/`
257 if l == 0 then return "/"
258 var pos = chars.last_index_of_from('/', l)
259 var n = self
260 if pos >= 0 then
261 n = substring(pos+1, l-pos)
262 end
263 return n.strip_extension(ext)
264 end
265
266 # Extract the dirname of a path
267 #
268 # assert "/path/to/a_file.ext".dirname == "/path/to"
269 # assert "path/to/a_file.ext".dirname == "path/to"
270 # assert "path/to".dirname == "path"
271 # assert "path/to/".dirname == "path"
272 # assert "path".dirname == "."
273 # assert "/path".dirname == "/"
274 # assert "/".dirname == "/"
275 # assert "".dirname == "."
276 fun dirname: 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 var pos = chars.last_index_of_from('/', l)
281 if pos > 0 then
282 return substring(0, pos)
283 else if pos == 0 then
284 return "/"
285 else
286 return "."
287 end
288 end
289
290 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
291 fun realpath: String do
292 var cs = to_cstring.file_realpath
293 var res = cs.to_s_with_copy
294 # cs.free_malloc # FIXME memory leak
295 return res
296 end
297
298 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
299 # ".." are not resolved if they start the path
300 # starting "/" is not removed
301 # trainling "/" is removed
302 #
303 # Note that the method only wonrk on the string:
304 # * no I/O access is performed
305 # * the validity of the path is not checked
306 #
307 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
308 # assert "../dir/file".simplify_path == "../dir/file"
309 # assert "dir/../../".simplify_path == ".."
310 # assert "dir/..".simplify_path == "."
311 # assert "//absolute//path/".simplify_path == "/absolute/path"
312 # assert "//absolute//../".simplify_path == "/"
313 fun simplify_path: String
314 do
315 var a = self.split_with("/")
316 var a2 = new Array[String]
317 for x in a do
318 if x == "." then continue
319 if x == "" and not a2.is_empty then continue
320 if x == ".." and not a2.is_empty and a2.last != ".." then
321 a2.pop
322 continue
323 end
324 a2.push(x)
325 end
326 if a2.is_empty then return "."
327 if a2.length == 1 and a2.first == "" then return "/"
328 return a2.join("/")
329 end
330
331 # Correctly join two path using the directory separator.
332 #
333 # Using a standard "{self}/{path}" does not work when `self` is the empty string.
334 # This method ensure that the join is valid.
335 #
336 # assert "hello".join_path("world") == "hello/world"
337 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
338 # assert "".join_path("world") == "world"
339 # assert "/hello".join_path("/world") == "/world"
340 #
341 # Note: you may want to use `simplify_path` on the result
342 #
343 # Note: I you want to join a great number of path, you can write
344 #
345 # [p1, p2, p3, p4].join("/")
346 fun join_path(path: String): String
347 do
348 if path.is_empty then return self
349 if self.is_empty then return path
350 if path.chars[0] == '/' then return path
351 return "{self}/{path}"
352 end
353
354 # Create a directory (and all intermediate directories if needed)
355 fun mkdir
356 do
357 var dirs = self.split_with("/")
358 var path = new FlatBuffer
359 if dirs.is_empty then return
360 if dirs[0].is_empty then
361 # it was a starting /
362 path.add('/')
363 end
364 for d in dirs do
365 if d.is_empty then continue
366 path.append(d)
367 path.add('/')
368 path.to_s.to_cstring.file_mkdir
369 end
370 end
371
372 # Delete a directory and all of its content, return `true` on success
373 #
374 # Does not go through symbolic links and may get stuck in a cycle if there
375 # is a cycle in the filesystem.
376 fun rmdir: Bool
377 do
378 var ok = true
379 for file in self.files do
380 var file_path = self.join_path(file)
381 var stat = file_path.file_lstat
382 if stat.is_dir then
383 ok = file_path.rmdir and ok
384 else
385 ok = file_path.file_delete and ok
386 end
387 stat.free
388 end
389
390 # Delete the directory itself
391 if ok then to_cstring.rmdir
392
393 return ok
394 end
395
396 # Change the current working directory
397 #
398 # "/etc".chdir
399 # assert getcwd == "/etc"
400 # "..".chdir
401 # assert getcwd == "/"
402 #
403 # TODO: errno
404 fun chdir do to_cstring.file_chdir
405
406 # Return right-most extension (without the dot)
407 #
408 # Only the last extension is returned.
409 # There is no special case for combined extensions.
410 #
411 # assert "file.txt".file_extension == "txt"
412 # assert "file.tar.gz".file_extension == "gz"
413 #
414 # For file without extension, `null` is returned.
415 # Hoever, for trailing dot, `""` is returned.
416 #
417 # assert "file".file_extension == null
418 # assert "file.".file_extension == ""
419 #
420 # The starting dot of hidden files is never considered.
421 #
422 # assert ".file.txt".file_extension == "txt"
423 # assert ".file".file_extension == null
424 fun file_extension: nullable String
425 do
426 var last_slash = chars.last_index_of('.')
427 if last_slash > 0 then
428 return substring( last_slash+1, length )
429 else
430 return null
431 end
432 end
433
434 # returns files contained within the directory represented by self
435 fun files : Set[ String ] is extern import HashSet[String], HashSet[String].add, NativeString.to_s, String.to_cstring, HashSet[String].as(Set[String]) `{
436 char *dir_path;
437 DIR *dir;
438
439 dir_path = String_to_cstring( recv );
440 if ((dir = opendir(dir_path)) == NULL)
441 {
442 perror( dir_path );
443 exit( 1 );
444 }
445 else
446 {
447 HashSet_of_String results;
448 String file_name;
449 struct dirent *de;
450
451 results = new_HashSet_of_String();
452
453 while ( ( de = readdir( dir ) ) != NULL )
454 if ( strcmp( de->d_name, ".." ) != 0 &&
455 strcmp( de->d_name, "." ) != 0 )
456 {
457 file_name = NativeString_to_s( strdup( de->d_name ) );
458 HashSet_of_String_add( results, file_name );
459 }
460
461 closedir( dir );
462 return HashSet_of_String_as_Set_of_String( results );
463 }
464 `}
465 end
466
467 redef class NativeString
468 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
469 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
470 private fun file_lstat: FileStat `{
471 struct stat* stat_element;
472 int res;
473 stat_element = malloc(sizeof(struct stat));
474 res = lstat(recv, stat_element);
475 if (res == -1) return NULL;
476 return stat_element;
477 `}
478 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
479 private fun rmdir: Bool `{ return rmdir(recv); `}
480 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
481 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
482 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
483 end
484
485 # This class is system dependent ... must reify the vfs
486 extern class FileStat `{ struct stat * `}
487 # Returns the permission bits of file
488 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
489 # Returns the last access time
490 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
491 # Returns the last status change time
492 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
493 # Returns the last modification time
494 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
495 # Returns the size
496 fun size: Int is extern "file_FileStat_FileStat_size_0"
497
498 # Returns true if it is a regular file (not a device file, pipe, sockect, ...)
499 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
500 # Returns true if it is a directory
501 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
502 # Returns true if it is a character device
503 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
504 # Returns true if it is a block device
505 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
506 # Returns true if the type is fifo
507 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
508 # Returns true if the type is a link
509 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
510 # Returns true if the type is a socket
511 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
512 end
513
514 # Instance of this class are standard FILE * pointers
515 private extern class NativeFile `{ FILE* `}
516 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
517 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
518 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
519 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
520 fun fileno: Int `{ return fileno(recv); `}
521
522 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
523 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
524 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
525 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
526 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
527 end
528
529 redef class Sys
530
531 # Standard input
532 var stdin: PollableIStream protected writable = new Stdin
533
534 # Standard output
535 var stdout: OStream protected writable = new Stdout
536
537 # Standard output for errors
538 var stderr: OStream protected writable = new Stderr
539
540 end
541
542 # Print `objects` on the standard output (`stdout`).
543 protected fun printn(objects: Object...)
544 do
545 sys.stdout.write(objects.to_s)
546 end
547
548 # Print an `object` on the standard output (`stdout`) and add a newline.
549 protected fun print(object: Object)
550 do
551 sys.stdout.write(object.to_s)
552 sys.stdout.write("\n")
553 end
554
555 # Read a character from the standard input (`stdin`).
556 protected fun getc: Char
557 do
558 return sys.stdin.read_char.ascii
559 end
560
561 # Read a line from the standard input (`stdin`).
562 protected fun gets: String
563 do
564 return sys.stdin.read_line
565 end
566
567 # Return the working (current) directory
568 protected fun getcwd: String do return file_getcwd.to_s
569 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"