lib: intro the method String::file_copy_to
[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 string
20 import string_search
21 import time
22
23 redef class Object
24 # Simple I/O
25
26 # Print `objects` on the standard output (`stdout`).
27 protected fun printn(objects: Object...)
28 do
29 stdout.write(objects.to_s)
30 end
31
32 # Print an `object` on the standard output (`stdout`) and add a newline.
33 protected fun print(object: Object)
34 do
35 stdout.write(object.to_s)
36 stdout.write("\n")
37 end
38
39 # Read a character from the standard input (`stdin`).
40 protected fun getc: Char
41 do
42 return stdin.read_char.ascii
43 end
44
45 # Read a line from the standard input (`stdin`).
46 protected fun gets: String
47 do
48 return stdin.read_line
49 end
50
51 # Return the working (current) directory
52 protected fun getcwd: String do return file_getcwd.to_s
53 private fun file_getcwd: NativeString is extern "string_NativeString_NativeString_file_getcwd_0"
54 end
55
56 # File Abstract Stream
57 abstract class FStream
58 super IOS
59 # The path of the file.
60 readable var _path: nullable String = null
61
62 # The FILE *.
63 var _file: nullable NativeFile = null
64
65 fun file_stat: FileStat
66 do return _file.file_stat end
67 end
68
69 # File input stream
70 class IFStream
71 super FStream
72 super BufferedIStream
73 # Misc
74
75 # Open the same file again.
76 # The original path is reused, therefore the reopened file can be a different file.
77 fun reopen
78 do
79 if not eof then close
80 _file = new NativeFile.io_open_read(_path.to_cstring)
81 _end_reached = false
82 _buffer_pos = 0
83 _buffer.clear
84 end
85
86 redef fun close
87 do
88 var i = _file.io_close
89 _end_reached = true
90 end
91
92 redef fun fill_buffer
93 do
94 var nb = _file.io_read(_buffer._items, _buffer._capacity)
95 if nb <= 0 then
96 _end_reached = true
97 nb = 0
98 end
99 _buffer._length = nb
100 _buffer_pos = 0
101 end
102
103 # End of file?
104 redef readable var _end_reached: Bool = false
105
106 # Open the file at `path` for reading.
107 init open(path: String)
108 do
109 _path = path
110 prepare_buffer(10)
111 _file = new NativeFile.io_open_read(_path.to_cstring)
112 assert cant_open_file: _file != null
113 end
114
115 private init do end
116 private init without_file do end
117 end
118
119 # File output stream
120 class OFStream
121 super FStream
122 super OStream
123
124 redef fun write(s)
125 do
126 assert _writable
127 write_native(s.to_cstring, s.length)
128 end
129
130 redef fun is_writable do return _writable
131
132 redef fun close
133 do
134 var i = _file.io_close
135 _writable = false
136 end
137
138 # Is the file open in write mode
139 var _writable: Bool
140
141 # Write `len` bytes from `native`.
142 private fun write_native(native: NativeString, len: Int)
143 do
144 assert _writable
145 var err = _file.io_write(native, len)
146 if err != len then
147 # Big problem
148 printn("Problem in writing : ", err, " ", len, "\n")
149 end
150 end
151
152 # Open the file at `path` for writing.
153 init open(path: String)
154 do
155 _file = new NativeFile.io_open_write(path.to_cstring)
156 assert cant_open_file: _file != null
157 _path = path
158 _writable = true
159 end
160
161 private init do end
162 private init without_file do end
163 end
164
165 ###############################################################################
166
167 class Stdin
168 super IFStream
169 private init do
170 _file = new NativeFile.native_stdin
171 _path = "/dev/stdin"
172 prepare_buffer(1)
173 end
174
175 # Is these something to read? (non blocking)
176 # FIXME: should be generalized
177 fun poll_in: Bool is extern "file_stdin_poll_in"
178 end
179
180 class Stdout
181 super OFStream
182 private init do
183 _file = new NativeFile.native_stdout
184 _path = "/dev/stdout"
185 _writable = true
186 end
187 end
188
189 class Stderr
190 super OFStream
191 private init do
192 _file = new NativeFile.native_stderr
193 _path = "/dev/stderr"
194 _writable = true
195 end
196 end
197
198 ###############################################################################
199
200 redef class String
201 # return true if a file with this names exists
202 fun file_exists: Bool do return to_cstring.file_exists
203
204 fun file_stat: FileStat do return to_cstring.file_stat
205 fun file_lstat: FileStat do return to_cstring.file_lstat
206
207 # Remove a file, return true if success
208 fun file_delete: Bool do return to_cstring.file_delete
209
210 # Copy content of file at `self` to `dest`
211 fun file_copy_to(dest: String)
212 do
213 var input = new IFStream.open(self)
214 var output = new OFStream.open(dest)
215
216 while not input.eof do
217 var buffer = input.read(1024)
218 output.write buffer
219 end
220
221 input.close
222 output.close
223 end
224
225 # remove the trailing extension "ext"
226 fun strip_extension(ext: String): String
227 do
228 if has_suffix(ext) then
229 return substring(0, length - ext.length)
230 end
231 return self
232 end
233
234 # Extract the basename of a path and remove the extension
235 fun basename(ext: String): String
236 do
237 var pos = last_index_of_from('/', _length - 1)
238 var n = self
239 if pos >= 0 then
240 n = substring_from(pos+1)
241 end
242 return n.strip_extension(ext)
243 end
244
245 # Extract the dirname of a path
246 #
247 # assert "/path/to/a_file.ext".dirname == "/path/to"
248 # assert "path/to/a_file.ext".dirname == "path/to"
249 # assert "path/to".dirname == "path"
250 # assert "path/to/".dirname == "path"
251 # assert "path".dirname == "."
252 # assert "/path".dirname == "/"
253 # assert "/".dirname == "/"
254 # assert "".dirname == "."
255 fun dirname: String
256 do
257 var l = _length - 1 # Index of the last char
258 if l > 0 and self[l] == '/' then l -= 1 # remove trailing `/`
259 var pos = last_index_of_from('/', l)
260 if pos > 0 then
261 return substring(0, pos)
262 else if pos == 0 then
263 return "/"
264 else
265 return "."
266 end
267 end
268
269 # Return the canonicalized absolute pathname (see POSIX function `realpath`)
270 fun realpath: String do
271 var cs = to_cstring.file_realpath
272 var res = cs.to_s_with_copy
273 # cs.free_malloc # FIXME memory leak
274 return res
275 end
276
277 # Simplify a file path by remove useless ".", removing "//", and resolving ".."
278 # ".." are not resolved if they start the path
279 # starting "/" is not removed
280 # trainling "/" is removed
281 #
282 # Note that the method only wonrk on the string:
283 # * no I/O access is performed
284 # * the validity of the path is not checked
285 #
286 # assert "some/./complex/../../path/from/../to/a////file//".simplify_path == "path/to/a/file"
287 # assert "../dir/file".simplify_path == "../dir/file"
288 # assert "dir/../../".simplify_path == ".."
289 # assert "dir/..".simplify_path == "."
290 # assert "//absolute//path/".simplify_path == "/absolute/path"
291 fun simplify_path: String
292 do
293 var a = self.split_with("/")
294 var a2 = new Array[String]
295 for x in a do
296 if x == "." then continue
297 if x == "" and not a2.is_empty then continue
298 if x == ".." and not a2.is_empty and a2.last != ".." then
299 a2.pop
300 continue
301 end
302 a2.push(x)
303 end
304 if a2.is_empty then return "."
305 return a2.join("/")
306 end
307
308 # Correctly join two path using the directory separator.
309 #
310 # Using a standard "{self}/{path}" does not work when `self` is the empty string.
311 # This method ensure that the join is valid.
312 #
313 # assert "hello".join_path("world") == "hello/world"
314 # assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
315 # assert "".join_path("world") == "world"
316 # assert "/hello".join_path("/world") == "/world"
317 #
318 # Note: you may want to use `simplify_path` on the result
319 #
320 # Note: I you want to join a great number of path, you can write
321 #
322 # [p1, p2, p3, p4].join("/")
323 fun join_path(path: String): String
324 do
325 if path.is_empty then return self
326 if self.is_empty then return path
327 if path[0] == '/' then return path
328 return "{self}/{path}"
329 end
330
331 # Create a directory (and all intermediate directories if needed)
332 fun mkdir
333 do
334 var dirs = self.split_with("/")
335 var path = new Buffer
336 if dirs.is_empty then return
337 if dirs[0].is_empty then
338 # it was a starting /
339 path.add('/')
340 end
341 for d in dirs do
342 if d.is_empty then continue
343 path.append(d)
344 path.add('/')
345 path.to_s.to_cstring.file_mkdir
346 end
347 end
348
349 # Change the current working directory
350 #
351 # "/etc".chdir
352 # assert getcwd == "/etc"
353 # "..".chdir
354 # assert getcwd == "/"
355 #
356 # TODO: errno
357 fun chdir do to_cstring.file_chdir
358
359 # Return right-most extension (without the dot)
360 fun file_extension : nullable String
361 do
362 var last_slash = last_index_of('.')
363 if last_slash >= 0 then
364 return substring( last_slash+1, length )
365 else
366 return null
367 end
368 end
369
370 # returns files contained within the directory represented by self
371 fun files : Set[ String ] is extern import HashSet, HashSet::add, NativeString::to_s, String::to_cstring, HashSet[String] as( Set[String] ), String as( Object )
372 end
373
374 redef class NativeString
375 private fun file_exists: Bool is extern "string_NativeString_NativeString_file_exists_0"
376 private fun file_stat: FileStat is extern "string_NativeString_NativeString_file_stat_0"
377 private fun file_lstat: FileStat `{
378 struct stat* stat_element;
379 int res;
380 stat_element = malloc(sizeof(struct stat));
381 res = lstat(recv, stat_element);
382 if (res == -1) return NULL;
383 return stat_element;
384 `}
385 private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0"
386 private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0"
387 private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0"
388 private fun file_realpath: NativeString is extern "file_NativeString_realpath"
389 end
390
391 extern FileStat `{ struct stat * `}
392 # This class is system dependent ... must reify the vfs
393 fun mode: Int is extern "file_FileStat_FileStat_mode_0"
394 fun atime: Int is extern "file_FileStat_FileStat_atime_0"
395 fun ctime: Int is extern "file_FileStat_FileStat_ctime_0"
396 fun mtime: Int is extern "file_FileStat_FileStat_mtime_0"
397 fun size: Int is extern "file_FileStat_FileStat_size_0"
398
399 fun is_reg: Bool `{ return S_ISREG(recv->st_mode); `}
400 fun is_dir: Bool `{ return S_ISDIR(recv->st_mode); `}
401 fun is_chr: Bool `{ return S_ISCHR(recv->st_mode); `}
402 fun is_blk: Bool `{ return S_ISBLK(recv->st_mode); `}
403 fun is_fifo: Bool `{ return S_ISFIFO(recv->st_mode); `}
404 fun is_lnk: Bool `{ return S_ISLNK(recv->st_mode); `}
405 fun is_sock: Bool `{ return S_ISSOCK(recv->st_mode); `}
406 end
407
408 # Instance of this class are standard FILE * pointers
409 private extern NativeFile
410 fun io_read(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_read_2"
411 fun io_write(buf: NativeString, len: Int): Int is extern "file_NativeFile_NativeFile_io_write_2"
412 fun io_close: Int is extern "file_NativeFile_NativeFile_io_close_0"
413 fun file_stat: FileStat is extern "file_NativeFile_NativeFile_file_stat_0"
414
415 new io_open_read(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_read_1"
416 new io_open_write(path: NativeString) is extern "file_NativeFileCapable_NativeFileCapable_io_open_write_1"
417 new native_stdin is extern "file_NativeFileCapable_NativeFileCapable_native_stdin_0"
418 new native_stdout is extern "file_NativeFileCapable_NativeFileCapable_native_stdout_0"
419 new native_stderr is extern "file_NativeFileCapable_NativeFileCapable_native_stderr_0"
420 end
421
422 # Standard input.
423 fun stdin: Stdin do return once new Stdin
424
425 # Standard output.
426 fun stdout: OFStream do return once new Stdout
427
428 # Standard output for error.
429 fun stderr: OFStream do return once new Stderr