From: Alexis Laferrière Date: Mon, 7 Jul 2014 16:14:10 +0000 (-0400) Subject: lib: intro String::rmdir X-Git-Tag: v0.6.7~77^2~2 X-Git-Url: http://nitlanguage.org lib: intro String::rmdir Signed-off-by: Alexis Laferrière --- diff --git a/lib/standard/file.nit b/lib/standard/file.nit index b797da1..de0da82 100644 --- a/lib/standard/file.nit +++ b/lib/standard/file.nit @@ -364,6 +364,30 @@ redef class String end end + # Delete a directory and all of its content, return `true` on success + # + # Does not go through symbolic links and may get stuck in a cycle if there + # is a cycle in the filesystem. + fun rmdir: Bool + do + var ok = true + for file in self.files do + var file_path = self.join_path(file) + var stat = file_path.file_lstat + if stat.is_dir then + ok = file_path.rmdir and ok + else + ok = file_path.file_delete and ok + end + stat.free + end + + # Delete the directory itself + if ok then to_cstring.rmdir + + return ok + end + # Change the current working directory # # "/etc".chdir @@ -447,6 +471,7 @@ redef class NativeString return stat_element; `} private fun file_mkdir: Bool is extern "string_NativeString_NativeString_file_mkdir_0" + private fun rmdir: Bool `{ return rmdir(recv); `} private fun file_delete: Bool is extern "string_NativeString_NativeString_file_delete_0" private fun file_chdir is extern "string_NativeString_NativeString_file_chdir_0" private fun file_realpath: NativeString is extern "file_NativeString_realpath"