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