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